def test_push(self): queue = Queue() to_push = [33, 0.0, "thirty three"] for item in to_push: queue.push(item) self.assertEqual(len(queue), len(to_push)) self.assertEqual(queue.peek(), to_push[0])
class TypicalTestCase(unittest.TestCase): """A comprehensive tester of typical behaviour of Queue. """ def setUp(self): """Set up an empty queue. """ self.queue = Queue() def tearDown(self): """Clean up. """ self.queue = None def testAll(self): """Check enqueueing and dequeueing several items. """ for item in range(20): self.queue.enqueue(item) self.assertFalse( self.queue.is_empty(), 'Queue should not be empty after adding item ' + str(item)) item = 0 while not self.queue.is_empty(): front = self.queue.dequeue() self.assertEqual( front, item, 'Wrong item at the front of the Queue. Found ' + str(front) + ' but expected ' + str(item)) item += 1
def enqueue_dequeue(q: Queue, howmany: int): """Enqueue and dequeue 'howmany' items to/from Queue 'q'. """ for i in range(howmany): q.enqueue(42) q.dequeue()
class PriorityQueueElement: def __init__(self, key, value): self.key = key self.values = Queue() self.values.push(value) def pop(self): return self.values.pop() def peek(self): return self.values.peek() def __len__(self): return len(self.values) def __eq__(self, other): if hasattr(other, "key"): return self.key == other.key else: return self.key == other def __lt__(self, other): if hasattr(other, "key"): return self.key < other.key else: return self.key < other
class MutableTestCase(unittest.TestCase): """Test adding a mutable object. Make sure the Queue adds the object, and does not make a copy. """ def setUp(self): """Set up an empty queue. """ self.queue = Queue() def tearDown(self): """Clean up. """ self.queue = None def testMutable(self): """Test with a list. """ item = [1, 2, 3] self.queue.enqueue(item) item.append(42) self.assertEqual(self.queue.front(), item, 'mutable item did not change!') self.assertEqual(self.queue.dequeue(), item, 'mutable item did not change!')
def test_pop(self): queue = Queue() to_push = [-5, 0.0, "five", True, [], [1]] for item in to_push: queue.push(item) for item in to_push: self.assertEqual(queue.pop(), item) self.assertEqual(len(queue), 0)
def traverse_level_order(self): "Traverse tree in level order. Return contents in list" out = [] q = Queue() node = self._head while node is not None: out.append(node._data) for c in node._children: q.push(c) node = q.pop() return out
def time_queue(m: int, n: int): """Return how long it takes to enqueue and dequeue 'm' items to/from a Queue with 'n' items already in it. """ q = Queue() for i in range(n): q.enqueue(1) start = time.time() enqueue_dequeue(q, m) end = time.time() return end - start
class EmptyTestCase(unittest.TestCase): """Test behaviour of an empty Queue. """ def setUp(self): """Set up an empty queue. """ self.queue = Queue() def tearDown(self): """Clean up. """ self.queue = None def testIsEmpty(self): """Test is_empty() on empty Queue. """ self.assertTrue(self.queue.is_empty(), 'is_empty returned False on an empty Queue!')
class SingletonTestCase(unittest.TestCase): """Check whether enqueueing a single item makes it appear at the front. """ def setUp(self): """Set up a queue with a single element. """ self.queue = Queue() self.queue.enqueue('a') def tearDown(self): """Clean up. """ self.queue = None def testIsEmpty(self): """Test is_empty() on non-empty Queue. """ self.assertFalse(self.queue.is_empty(), 'is_empty returned True on non-empty Queue!') def testFront(self): """Test front() on a non-empty Queue. """ front = self.queue.front() self.assertEqual( front, 'a', 'The item at the front should have been "a" but was ' + front + '.') def testDequeue(self): """Test dequeue() on a non-empty Queue. """ front = self.queue.dequeue() self.assertEqual( front, 'a', 'The item at the front should have been "a" but was ' + front + '.') self.assertTrue(self.queue.is_empty(), 'Queue with one element not empty after dequeue().')
def test_init(self): queue = Queue() self.assertEqual(len(queue), 0)
def __init__(self, key, value): self.key = key self.values = Queue() self.values.push(value)
def setUp(self): """Set up an empty queue. """ self.queue = Queue()
def setUp(self): """Set up a queue with a single element. """ self.queue = Queue() self.queue.enqueue('a')