Example #1
0
    def testPrependPeek(self):
        linkedList = LinkedList()
        linkedList.append("debut")

        for i in range(5):
            linkedList.prepend(i)
            self.assertEqual(linkedList.check(), i)
            self.assertFalse(linkedList.isEmpty())
            self.assertEqual(linkedList.size(), i + 2)

        listSize = linkedList.size()

        for i in range(4, -1, -1):
            self.assertEqual(linkedList.peek(), i)
            self.assertFalse(linkedList.isEmpty())
            listSize -= 1
            self.assertEqual(linkedList.size(), listSize)

        self.assertEqual(linkedList.peek(), "debut")

        self.assertTrue(linkedList.isEmpty())
        self.assertEqual(linkedList.size(), 0)

        self.assertRaises(ValueError, linkedList.check)
        self.assertRaises(ValueError, linkedList.peek)
Example #2
0
class LinkedListTest(unittest.TestCase):
    def setUp(self):
        self.linkedList = LinkedList()

    # Test __init__()
    def test_a_Init(self):
        self.assertTrue(self.linkedList.first == None)
        self.assertTrue(self.linkedList.n == 0)

    # Test __init__() -> IsEmpty() -> size() -> check() -> peek() -> append() -> size() -> check() -> peek()
    def test_b_IsEmptyThenAppendThenIsNotEmpty(self):
        self.assertTrue(self.linkedList.isEmpty())
        self.assertEqual(self.linkedList.size(), 0)
        self.assertRaises(ValueError, self.linkedList.check)
        self.assertRaises(ValueError, self.linkedList.peek)

        self.linkedList.append('test')
        self.assertFalse(self.linkedList.isEmpty())
        self.assertEqual(self.linkedList.size(), 1)
        self.assertEqual(self.linkedList.check().value, 'test')
        self.assertEqual(self.linkedList.peek().value, 'test')
        self.assertTrue(self.linkedList.isEmpty())
        self.assertEqual(self.linkedList.size(), 0)

        self.linkedList.append('test1')
        self.linkedList.append('test2')
        self.linkedList.append('test3')
        self.assertFalse(self.linkedList.isEmpty())
        self.assertEqual(self.linkedList.size(), 3)
        self.assertEqual(self.linkedList.check().value, 'test1')
        self.assertEqual(self.linkedList.peek().value, 'test1')
        self.assertEqual(self.linkedList.size(), 2)
        self.assertEqual(self.linkedList.peek().value, 'test2')
        self.assertEqual(self.linkedList.size(), 1)
        self.assertEqual(self.linkedList.peek().value, 'test3')
        self.assertEqual(self.linkedList.size(), 0)
        self.assertTrue(self.linkedList.isEmpty())

    # Test __init__() -> IsEmpty() -> Prepend() -> Prepend() -> check() -> peek()
    def test_c_Prepend(self):
        self.assertTrue(self.linkedList.isEmpty())
        self.linkedList.prepend('test1')

        self.linkedList.prepend('test2')
        self.assertEqual(self.linkedList.check().value, 'test2')

        self.assertEqual(self.linkedList.peek().value, 'test2')
        self.assertEqual(self.linkedList.peek().value, 'test1')
        self.assertTrue(self.linkedList.isEmpty())
Example #3
0
 def test_02_should_return_element_when_peeking(self):
     testList = LinkedList()
     testList.append(5)
     testList.append(15)
     firstElement = testList.peek()
     self.assertEqual(firstElement, 5)
     self.assertEqual(testList.list[0], 15)
Example #4
0
 def test_02_should_return_element_when_peeking(self):
     testList = LinkedList()
     testList.append(5)
     testList.append(15)
     firstNode = testList.peek()
     self.assertEqual(testList.n, 1)
     self.assertEqual(firstNode, 5)
     self.assertEqual(testList.first.value, 15)