자. enqueue 를 만들고 count를 세어보자.
~cpp
class Queue:
def __init__ (self):
self.qu_list = []
def enqueue (self, n):
pass
def count (self):
return len (self.qu_list)
~cpp
======================================================================
FAIL: testCount (TestCaseQueue.QueueTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\USER\reset\TestCaseQueue.py", line 8, in testCount
self.assertEquals (q.count(), 1)
File "C:\PYTHON22\lib\unittest.py", line 286, in failUnlessEqual
raise self.failureException, \
AssertionError: 0 != 1
----------------------------------------------------------------------
Ran 1 tests in 0.060s
FAILED (failures=1)
enqueue를 보장하기 위한 테스트를 먼저 생각해보자. queue는 말 그대로 안에 들어가는 값이 하나하나 늘어난다. First Input First Out이다.
~cpp
import unittest
from queue import Queue
class QueueTestCase (unittest.TestCase):
def testEnqueue (self):
q = Queue ()
q.enqueue (10)
self.assertEquals (q.last(), 10)
q.enqueue (20)
self.assertEquals (q.last(), 20)
def testCount (self):
q = Queue ()
q.enqueue (10)
self.assertEquals (q.count(), 1)
q.enqueue (20)
self.assertEquals (q.count(), 2)
q.enqueue (30)
self.assertEquals (q.count(), 3)
suite = unittest.makeSuite (QueueTestCase, "test")
runner = unittest.TextTestRunner ()
runner.run (suite)
~cpp
======================================================================
ERROR: testEnqueue (TestCaseQueue.QueueTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\USER\reset\TestCaseQueue.py", line 8, in testEnqueue
self.assertEquals (q.count(), 1)
AttributeError: Queue instance has no attribute 'last'
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (errors=1)
dequeue를 만들어보자.
dequeue 인 경우 두가지를 생각해볼 수 있겠다. 하나는 최초의 값이 제대로 리턴이 되는 것과, queue안에 저장된 갯수가 줄어든다는 점이다.
~cpp
import unittest
from queue import Queue
class QueueTestCase (unittest.TestCase):
def testEnqueue (self):
q = Queue ()
q.enqueue (10)
self.assertEquals (q.last(), 10)
q.enqueue (20)
self.assertEquals (q.last(), 20)
def testCount (self):
q = Queue ()
q.enqueue (10)
self.assertEquals (q.count(), 1)
q.enqueue (20)
self.assertEquals (q.count(), 2)
q.enqueue (30)
self.assertEquals (q.count(), 3)
def testDequeueData (self):
q = Queue ()
q.enqueue (1)
q.enqueue (2)
q.enqueue (3)
self.assertEquals (q.dequeue(),1)
self.assertEquals (q.dequeue(),2)
self.assertEquals (q.dequeue(),3)
suite = unittest.makeSuite (QueueTestCase, "test")
runner = unittest.TextTestRunner ()
runner.run (suite)
~cpp
======================================================================
ERROR: testDequeueData (TestCaseQueue.QueueTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\USER\reset\TestCaseQueue.py", line 26, in testDequeueData
AttributeError: Queue instance has no attribute 'dequeue'
----------------------------------------------------------------------
Ran 3 tests in 0.050s
FAILED (errors=1)
dequeue 메소드가 없다고 나온다. 요구하는데로 채워주자.
~cpp
class Queue:
def __init__ (self):
self.qu_list = []
def enqueue (self, n):
self.qu_list.append(n)
def count (self):
return len (self.qu_list)
def last (self):
return self.qu_list[len(self.qu_list)-1]
def dequeue (self):
return 0
~cpp
======================================================================
FAIL: testDequeueData (TestCaseQueue.QueueTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\USER\reset\TestCaseQueue.py", line 26, in testDequeueData
File "C:\PYTHON22\lib\unittest.py", line 286, in failUnlessEqual
raise self.failureException, \
AssertionError: 0 != 1
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (failures=1)
자.. dequeue를 구현하자.
~cpp
class Queue:
def __init__ (self):
self.qu_list = []
def enqueue (self, n):
self.qu_list.append(n)
def count (self):
return len (self.qu_list)
def last (self):
return self.qu_list[len(self.qu_list)-1]
def dequeue (self):
return self.qu_list.pop(0)
~cpp
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
이쯤 되면 매번
TestCase 작성할 때마다 Queue 인스턴스를 만든다. 이것은 중복되는 일이다. 다음과 같이 setUp 메소드를 구현해주자. 기존에 q를 self.q로 바꾸는것은 replace를 사용하면 쉽다.
~cpp
import unittest
from queue import Queue
class QueueTestCase (unittest.TestCase):
def setUp (self):
self.q = Queue ()
def testEnqueue (self):
self.q = Queue ()
self.q.enqueue (10)
self.assertEquals (self.q.last(), 10)
self.q.enqueue (20)
self.assertEquals (self.q.last(), 20)
def testCount (self):
self.q.enqueue (10)
self.assertEquals (self.q.count(), 1)
self.q.enqueue (20)
self.assertEquals (self.q.count(), 2)
self.q.enqueue (30)
self.assertEquals (self.q.count(), 3)
def testDequeueData (self):
self.q.enqueue (1)
self.q.enqueue (2)
self.q.enqueue (3)
self.assertEquals (self.q.dequeue(),1)
self.assertEquals (self.q.dequeue(),2)
self.assertEquals (self.q.dequeue(),3)
suite = unittest.makeSuite (QueueTestCase, "test")
runner = unittest.TextTestRunner ()
runner.run (suite)
마지막 한가지. dequeue 를 구현할때 예상했던 일중 하나인 갯수를 세어보자.
~cpp
import unittest
from queue import Queue
class QueueTestCase (unittest.TestCase):
def setUp (self):
self.q = Queue ()
def testEnqueue (self):
self.q = Queue ()
self.q.enqueue (10)
self.assertEquals (self.q.last(), 10)
self.q.enqueue (20)
self.assertEquals (self.q.last(), 20)
def testCount (self):
self.q.enqueue (10)
self.assertEquals (self.q.count(), 1)
self.q.enqueue (20)
self.assertEquals (self.q.count(), 2)
self.q.enqueue (30)
self.assertEquals (self.q.count(), 3)
def testDequeueData (self):
self.q.enqueue (1)
self.q.enqueue (2)
self.q.enqueue (3)
self.assertEquals (self.q.dequeue(),1)
self.assertEquals (self.q.dequeue(),2)
self.assertEquals (self.q.dequeue(),3)
def testDequeueCount (self):
self.q.enqueue (1)
self.q.enqueue (2)
self.q.enqueue (3)
self.q.dequeue ()
self.assertEquals (self.q.count(),2)
self.q.dequeue ()
self.assertEquals (self.q.count(),1)
self.q.dequeue ()
self.assertEquals (self.q.count(),0)
suite = unittest.makeSuite (QueueTestCase, "test")
runner = unittest.TextTestRunner ()
runner.run (suite)
~cpp
----------------------------------------------------------------------
Ran 4 tests in 0.050s
OK
완성코드..
TestCaseQueue.py
~cpp
import unittest
from queue import Queue
class QueueTestCase (unittest.TestCase):
def setUp (self):
self.q = Queue ()
def testEnqueue (self):
self.q = Queue ()
self.q.enqueue (10)
self.assertEquals (self.q.last(), 10)
self.q.enqueue (20)
self.assertEquals (self.q.last(), 20)
def testCount (self):
self.q.enqueue (10)
self.assertEquals (self.q.count(), 1)
self.q.enqueue (20)
self.assertEquals (self.q.count(), 2)
self.q.enqueue (30)
self.assertEquals (self.q.count(), 3)
def testDequeueData (self):
self.q.enqueue (1)
self.q.enqueue (2)
self.q.enqueue (3)
self.assertEquals (self.q.dequeue(),1)
self.assertEquals (self.q.dequeue(),2)
self.assertEquals (self.q.dequeue(),3)
def testDequeueCount (self):
self.q.enqueue (1)
self.q.enqueue (2)
self.q.enqueue (3)
self.q.dequeue ()
self.assertEquals (self.q.count(),2)
self.q.dequeue ()
self.assertEquals (self.q.count(),1)
self.q.dequeue ()
self.assertEquals (self.q.count(),0)
suite = unittest.makeSuite (QueueTestCase, "test")
runner = unittest.TextTestRunner ()
runner.run (suite)
Queue.py
~cpp
class Queue:
def __init__ (self):
self.qu_list = []
def enqueue (self, n):
self.qu_list.append(n)
def count (self):
return len (self.qu_list)
def last (self):
return self.qu_list[len(self.qu_list)-1]
def dequeue (self):
return self.qu_list.pop(0)