コード例 #1
0
    def testUnionTwoStacks(self):

        for i in range(5):
            for j in range(5):

                stack1 = Stack(20)

                for k in range(10):
                    stack1.push(k+i*100)

                stack2 = Stack(20)

                for k in range(10):
                    stack2.push(k+j*1000)

                resultat = Calculator.union(stack1, stack2)

                self.assertIsInstance(resultat, Stack)

                self.assertEqual(resultat.max_size, stack1.max_size + stack2.max_size)
                self.assertEqual(resultat.size(), stack1.size() + stack2.size())

                for k in range(9, -1, -1):
                    self.assertEqual(resultat.pop(), k+j*1000)

                for k in range(9, -1, -1):
                    self.assertEqual(resultat.pop(), k+i*100)
コード例 #2
0
ファイル: StackTest.py プロジェクト: fx46/3430
class StackTest(unittest.TestCase):
    def setUp(self):
        self.stack = Stack(3)

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

    # Test __init__() -> push() -> pop()
    def test_b_PushPop(self):
        self.stack.push("test")
        self.assertFalse(self.stack.isEmpty())
        self.assertTrue(self.stack.size() == 1)
        self.assertEqual(self.stack.check().value, "test")
        self.assertEqual(self.stack.peek().value, "test")
        self.assertTrue(self.stack.isEmpty())

        self.stack.push("test1")
        self.stack.push("test2")
        self.stack.push("test3")
        self.assertTrue(self.stack.isFull())
        self.assertEqual(self.stack.size(), self.stack.max_size)
        try:
            self.stack.push("test4")
        except Exception as ex:
            self.assertEqual(str(ex), "Stack overflow")

        self.assertEqual(self.stack.pop().value, "test3")
        self.assertEqual(self.stack.pop().value, "test2")
        self.assertEqual(self.stack.pop().value, "test1")
        try:
            self.stack.pop()
        except Exception as ex:
            self.assertEqual(str(ex), "Stack underflow")
コード例 #3
0
ファイル: test_stack.py プロジェクト: fnduister/LOG3430
 def test_04_partiellement_plein_size_isEmpty_should_be_false_isFull_shouldBeFalse(
         self):
     stack = Stack(1)
     stack.push(1)
     self.assertEqual(stack.size(), 1)
     self.assertFalse(stack.isEmpty())
     self.assertTrue(stack.isFull())
コード例 #4
0
 def test_04_plein_puch__size_isEmpty_should_be_true_isFull_shouldBeFalse(self):
     stack = Stack(1)
     stack.push(1)
     self.assertEqual(stack.size(), 1)
     self.assertFalse(stack.isEmpty())
     self.assertTrue(stack.isFull())
     stack.pop()
     self.assertTrue(stack.isEmpty())
     self.assertFalse(stack.isFull())
コード例 #5
0
ファイル: TestStack.py プロジェクト: jmetais2017/LOG3430_TP
    def testPushPop(self):
        for size in range(5, 10):
            stack = Stack(size)

            for i in range(1, size):
                stack.push(i)
                self.assertEqual(stack.size(), i)
                self.assertEqual(stack.check(), i)

                self.assertFalse(stack.isEmpty())
                self.assertFalse(stack.isFull())

            stack.push("last")

            self.assertEqual(stack.size(), size)
            self.assertEqual(stack.check(), "last")

            self.assertFalse(stack.isEmpty())
            self.assertTrue(stack.isFull())

            for i in range(10):
                self.assertRaises(ValueError, stack.push, "fail")

            self.assertEqual(stack.size(), size)
            self.assertEqual(stack.check(), "last")

            self.assertFalse(stack.isEmpty())
            self.assertTrue(stack.isFull())

            self.assertEqual(stack.pop(), "last")

            for i in range(size - 1, 1, -1):
                self.assertEqual(stack.pop(), i)

                self.assertFalse(stack.isEmpty())
                self.assertFalse(stack.isFull())

            self.assertEqual(stack.pop(), 1)

            self.assertTrue(stack.isEmpty())
            self.assertFalse(stack.isFull())
            self.assertRaises(ValueError, stack.pop)
コード例 #6
0
    def testUnionStackAndQueue(self):
        stacks = []

        for n in range(5):
            stack = Stack(20)

            for i in range(10):
                stack.push(i+n*100)

            stacks.append(stack)

        queues = []

        for n in range(5):
            queue = Queue(20)

            for i in range(10):
                queue.enqueue(i+n*1000)

            queues.append(queue)

        for stack in stacks:
            for queue in queues:
                resultat = Calculator.union(stack, queue)

                self.assertIsInstance(resultat, LinkedList)
                self.assertNotIsInstance(resultat, Stack)
                self.assertNotIsInstance(resultat, Queue)

                self.assertEqual(resultat.size(), stack.size() + queue.size())


                noeud = stack.first

                while noeud.next:
                    self.assertTrue(contient(resultat, noeud.value))
                    noeud = noeud.next


                noeud = queue.first

                while noeud.next:
                    self.assertTrue(contient(resultat, noeud.value))
                    noeud = noeud.next
コード例 #7
0
ファイル: test_stack.py プロジェクト: fnduister/LOG3430
class testStack(unittest.TestCase):
    def setUp(self):
        self.MAX_SIZE = 10
        self.stack = Stack(self.MAX_SIZE)

    # Cas de test pour le cas vide

    def test_04_Vide_isEmpty_should_be_true(self):
        self.assertTrue(self.stack.isEmpty())

    def test_04_vide_isFull_should_BeFalse_isEmpty_should_be_true(self):
        self.assertFalse(self.stack.isFull())
        self.assertTrue(self.stack.isEmpty())

    def test_04_vide_Size_should_BeZero_isEmpty_should_be_true(self):
        self.assertEqual(self.stack.size(), 0)
        self.assertTrue(self.stack.isEmpty())

    def test_04_vide_peek_should_raiseError_isEmpty_should_be_true(self):
        self.assertRaises(ValueError, self.stack.peek)
        self.assertTrue(self.stack.isEmpty())

    def test_04_vide_pop_should_raiseError_isEmpty_should_be_true(self):
        self.stack.pop()
        self.assertEqual(0, self.stack.n)
        self.assertTrue(self.stack.isEmpty())

    def test_04_vide___str____isEmpty_should_be_true(self):
        self.stack.__str__()
        self.assertTrue(self.stack.isEmpty())

    # transition de l'etat vide a l'etat partiellement_plein
    def test_04_partiellement_plein_push__isEmpty_should_be_false_isFull_shouldBeFalse(
            self):
        self.stack.push(1)
        self.assertFalse(self.stack.isEmpty())
        self.assertFalse(self.stack.isFull())

    def test_04_partiellement_plein_pop_size_greatanThan_1__isEmpty_should_be_false_isFull_shouldBeFalse(
            self):
        self.stack.push(1)
        self.stack.push(2)
        self.stack.pop()
        self.assertFalse(self.stack.isEmpty())
        self.assertFalse(self.stack.isFull())

    def test_04_partiellement_plein_size_isEmpty_should_be_false_isFull_shouldBeFalse(
            self):
        self.stack.push(1)
        self.assertEqual(self.stack.size(), 1)
        self.assertFalse(self.stack.isEmpty())
        self.assertFalse(self.stack.isFull())

    def test_04_partiellement_plein_pop_isEmpty_should_be_false_isFull_shouldBeFalse(
            self):
        self.stack.push(1)
        self.stack.push(15)
        self.assertEqual(self.stack.peek(), 15)
        self.assertFalse(self.stack.isEmpty())
        self.assertFalse(self.stack.isFull())

    def test_04_partiellement_plein___str____isEmpty_should_be_false_isFull_shouldBeFalse(
            self):
        self.stack.push(1)
        self.stack.__str__()
        self.assertFalse(self.stack.isEmpty())
        self.assertFalse(self.stack.isFull())

    # push[n < max_size] stay in the same state
    def test_04_partiellement_plein_push_size_greatanThan_1__isEmpty_should_be_false_isFull_shouldBeFalse(
            self):
        self.stack.push(1)
        self.stack.push(2)
        self.assertFalse(self.stack.isEmpty())
        self.assertFalse(self.stack.isFull())

    # transition de l'etat  partiellement_plein  a l'etat plein

    def test_04_plein_push_size_greatanThan_1__isEmpty_should_be_false_isFull_shouldBeFalse(
            self):
        for i in range(1, self.MAX_SIZE + 1):
            self.stack.push(i)
        self.assertFalse(self.stack.isEmpty())
        self.assertTrue(self.stack.isFull())

    def test_04_plein_push__isEmpty_should_be_false_isFull_shouldBeFalse(self):
        for i in range(1, self.MAX_SIZE + 1):
            self.stack.push(i)
        self.assertEqual(self.stack.size(), self.MAX_SIZE)
        self.assertTrue(self.stack.isFull())

    def test_04_plein_push_should_be_false_isFull_shouldBeFalse(self):
        for i in range(1, self.MAX_SIZE + 1):
            self.stack.push(i)
        self.assertEqual(self.stack.size(), self.MAX_SIZE)
        self.assertRaises(ValueError, self.stack.push, self.MAX_SIZE + 1)
        self.assertEqual(self.stack.peek(), 10)
        self.stack.push(4)
        self.assertEqual(self.stack.peek(), 4)
        self.assertEqual(self.stack.size(), 9)
        self.assertFalse(self.stack.isFull())

    def test_04_plein_pop_should_be_false_isFull_shouldBeFalse(self):
        for i in range(1, self.MAX_SIZE + 1):
            self.stack.push(i)
        self.stack.pop()
        self.assertEqual(self.stack.size(), self.MAX_SIZE - 1)
        self.assertFalse(self.stack.isEmpty())
        self.assertFalse(self.stack.isFull())

    def test_04_plein___str____isEmpty_should_be_false_isFull_should_be_true(
            self):
        for i in range(self.MAX_SIZE):
            self.stack.push(i)
        self.stack.__str__()
        self.assertFalse(self.stack.isEmpty())
        self.assertTrue(self.stack.isFull())

    def test_04_plein_push_should_not_add_when_trials_max_trials(self):
        for i in range(self.MAX_SIZE):
            self.stack.push(i)
        self.assertEqual(self.stack.size(), self.MAX_SIZE)
        self.assertRaises(ValueError, self.stack.push, self.MAX_SIZE + 1)

    # tests for stack with max_size == 1

    def test_04_partiellement_plein_size_isEmpty_should_be_false_isFull_shouldBeFalse(
            self):
        stack = Stack(1)
        stack.push(1)
        self.assertEqual(stack.size(), 1)
        self.assertFalse(stack.isEmpty())
        self.assertTrue(stack.isFull())

    def test_04_plein_puch__size_isEmpty_should_be_true_isFull_shouldBeFalse(
            self):
        stack = Stack(1)
        stack.push(1)
        self.assertEqual(stack.size(), 1)
        self.assertFalse(stack.isEmpty())
        self.assertTrue(stack.isFull())
        stack.pop()
        self.assertTrue(stack.isEmpty())
        self.assertFalse(stack.isFull())

    def test_04_stack_should_return_correct_value_to_string(self):
        self.stack.push(4)
        self.assertEqual(str(self.stack.peek()), '4')