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)
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())
def test_02_should_prepend_new_element_to_first_position(self): testList = LinkedList() testList.prepend(5) testList.prepend(6) self.assertEqual(testList.first.value, 6) self.assertEqual(testList.n, 2)
def test_02_should_prepend_new_element_with_an_empty_list(self): testList = LinkedList() testList.prepend(5) self.assertEqual(testList.first.value, 5)
def test_02_should_prepend_new_element_to_first_position(self): testList = LinkedList() testList.prepend(5) testList.prepend(6) self.assertEqual(testList.list[0], 6) self.assertEqual(testList.size(), 2)