示例#1
0
class QueueTest(unittest.TestCase):
    def setUp(self):
        self.queue = Queue(3)

    # Test __init__()
    def test_a_Init(self):
        self.assertEqual(self.queue.max_size, 3)

    # Test __init__() -> Enqueue() -> Dequeue()
    def test_b_EnqueuDequeue(self):
        self.queue.enqueue("test")
        self.assertFalse(self.queue.isEmpty())
        self.assertTrue(self.queue.size() == 1)
        self.assertEqual(self.queue.check().value, "test")
        self.assertEqual(self.queue.peek().value, "test")
        self.assertTrue(self.queue.isEmpty())

        self.queue.enqueue("test1")
        self.queue.enqueue("test2")
        self.queue.enqueue("test3")
        self.assertTrue(self.queue.isFull())
        self.assertEqual(self.queue.size(), self.queue.max_size)
        try:
            self.queue.enqueue("test4")
        except Exception as ex:
            self.assertEqual(str(ex), "Queue overflow")

        self.assertEqual(self.queue.dequeue().value, "test1")
        self.assertEqual(self.queue.dequeue().value, "test2")
        self.assertEqual(self.queue.dequeue().value, "test3")
        try:
            self.queue.dequeue()
        except Exception as ex:
            self.assertEqual(str(ex), "Stack underflow")
示例#2
0
    def testEnqueueDequeue(self):

        for size in range(5, 10):
            queue = Queue(size)

            for i in range(1, size):
                queue.enqueue(i)

                self.assertEqual(queue.check(), 1)
                self.assertFalse(queue.isEmpty())
                self.assertFalse(queue.isFull())
                self.assertEqual(queue.size(), i)

            queue.enqueue("last")

            self.assertEqual(queue.check(), 1)
            self.assertFalse(queue.isEmpty())
            self.assertTrue(queue.isFull())
            self.assertEqual(queue.size(), size)

            for i in range(10):
                self.assertRaises(ValueError, queue.enqueue, "fail")

            self.assertEqual(queue.check(), 1)
            self.assertFalse(queue.isEmpty())
            self.assertTrue(queue.isFull())
            self.assertEqual(queue.size(), size)

            queueSize = queue.size()

            for i in range(1, size):
                self.assertEqual(queue.dequeue(), i)
                self.assertFalse(queue.isEmpty())
                self.assertFalse(queue.isFull())
                queueSize -= 1
                self.assertEqual(queue.size(), queueSize)

            self.assertEqual(queue.dequeue(), "last")

            self.assertTrue(queue.isEmpty())
            self.assertFalse(queue.isFull())
            self.assertEqual(queue.size(), 0)
            self.assertRaises(ValueError, queue.dequeue)
示例#3
0
class testqueue(unittest.TestCase):
    def setUp(self):
        self.queue = Queue(100)

    # Séquences de tests pour la tranche first
    def test_03_max_size_set_correctly(self):
        self.assertEqual(self.queue.max_size, 100)

    def test_03_tranche_first_sequence_2(self):
        self.assertRaises(ValueError, self.queue.check)
        self.queue.enqueue(48)
        self.assertEqual(48, self.queue.check().value)

    # Séquences de tests pour la tranche last
    def test_03_tranche_last_sequence_1(self):
        self.assertRaises(ValueError, self.queue.check)
        self.queue.enqueue(37)
        self.queue.dequeue()
        self.assertRaises(ValueError, self.queue.check)

    def test_03_tranche_last_sequence_2(self):
        self.assertRaises(ValueError, self.queue.check)
        self.queue.dequeue()
        self.assertEqual(self.queue.n, 0)
        self.assertRaises(ValueError, self.queue.check)
        self.queue.enqueue(48)

    # Séquences de tests pour la tranche n
    def test_03_tranche_n_sequence_1(self):
        self.assertEqual(0, self.queue.size())
        self.queue.enqueue(37)
        self.assertEqual(1, self.queue.size())
        self.queue.dequeue()
        self.assertEqual(0, self.queue.size())

    def test_03_tranche_n_sequence_2(self):
        self.assertEqual(0, self.queue.size())
        self.queue.dequeue()
        self.assertEqual(self.queue.n, 0)
        self.assertEqual(0, self.queue.size())
        self.queue.enqueue(655)
        self.assertEqual(1, self.queue.size())

    # Séquences de tests pour la tranche full
    def test_03_tranche_full_sequence_1(self):
        self.assertFalse(self.queue.isFull())
        self.queue.enqueue(34)
        self.assertFalse(self.queue.isFull())
        self.queue.dequeue()
        self.assertFalse(self.queue.isFull())

    def test_03_tranche_full_sequence_2(self):
        self.assertFalse(self.queue.isFull())
        self.queue.dequeue()
        self.assertEqual(self.queue.n, 0)
        self.queue.enqueue(37)
        self.assertFalse(self.queue.isFull())

    # Séquences de tests pour la tranche isEmpty

    def test_03_tranche_isEmpty_sequence_1(self):
        self.assertTrue(self.queue.isEmpty())
        self.queue.enqueue(34)
        self.assertFalse(self.queue.isEmpty())
        self.queue.dequeue()
        self.assertTrue(self.queue.isEmpty())
        self.assertRaises(ValueError, self.queue.check)

    def test_03_tranche_isEmpty_sequence_2(self):
        self.assertTrue(self.queue.isEmpty())
        self.queue.dequeue()
        self.assertEqual(self.queue.n, 0)
        self.queue.enqueue(37)
        self.assertFalse(self.queue.isEmpty())

    def test_03_check_should_return_last_element(self):
        self.queue.enqueue(37)
        self.queue.enqueue(45)
        self.assertEqual(self.queue.check().value, 37)

    def test_03_enqueue_should_raise_error_when_queue_full(self):
        for i in range(100):
            self.queue.enqueue(i)
        self.assertRaises(ValueError, self.queue.enqueue, 5)

    def test_03_enqueue_should_change_full_to_true(self):
        for i in range(99):
            self.queue.enqueue(i)
        self.queue.enqueue(4)
        self.assertTrue(self.queue.isFull())

    def test_03_enqueue_should_return_correct_values_for_last_and_first(self):
        self.queue.enqueue(4)
        self.queue.enqueue(5)
        self.assertEqual(self.queue.check().value, 4)

    def test_03_enqueue_should_change_last_to_None(self):
        self.queue.enqueue(4)
        self.queue.enqueue(5)
        self.queue.dequeue()
        self.assertEqual(1, self.queue.n)

    def test_03_enqueue_should_return_correct_value_to_string(self):
        self.queue.enqueue(4)
        self.assertEqual(str(self.queue.check()), '4')