|
| 1 | +""" unittest for linked list operations """ |
| 2 | + |
| 3 | +import unittest |
| 4 | +from linked_list import LinkedList |
| 5 | + |
| 6 | +def linkedlist_to_array(linkedlist): |
| 7 | + """ converting linkedlist data to list """ |
| 8 | + result = [] |
| 9 | + current = linkedlist.get_head() |
| 10 | + while current: |
| 11 | + result.append(current.get_data()) |
| 12 | + current = current.get_next() |
| 13 | + return result |
| 14 | + |
| 15 | + |
| 16 | +class TestLinkedListEmpty(unittest.TestCase): |
| 17 | + """ tests on empty linked list """ |
| 18 | + def setUp(self): |
| 19 | + """ setup empty linked list """ |
| 20 | + self.linkedlist = LinkedList() |
| 21 | + |
| 22 | + def test_lenght(self): |
| 23 | + """ test for linked list lenght operation """ |
| 24 | + self.assertEqual(len(self.linkedlist), 0) |
| 25 | + |
| 26 | + def test_get_head(self): |
| 27 | + """ test for linked list get_head operation """ |
| 28 | + self.assertIsNone(self.linkedlist.head) |
| 29 | + |
| 30 | + def test_is_empty(self): |
| 31 | + """ test for linked list is_empty operation """ |
| 32 | + self.assertTrue(self.linkedlist.is_empty()) |
| 33 | + |
| 34 | + def test_insert_head(self): |
| 35 | + """ test for linked list insert_head operation """ |
| 36 | + self.linkedlist.insert_head(45) |
| 37 | + self.linkedlist.insert_head(34) |
| 38 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [34, 45]) |
| 39 | + |
| 40 | + def test_insert_tail(self): |
| 41 | + """ test for linked list insert_tail operation """ |
| 42 | + self.linkedlist.insert_tail(45) |
| 43 | + self.linkedlist.insert_tail(34) |
| 44 | + self.linkedlist.insert_tail(56) |
| 45 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [45, 34, 56]) |
| 46 | + |
| 47 | + |
| 48 | +class TestLinkedList(unittest.TestCase): |
| 49 | + """ tests on filled linked list """ |
| 50 | + def setUp(self): |
| 51 | + """ setup empty linked list """ |
| 52 | + self.linkedlist = LinkedList() |
| 53 | + self.linkedlist.insert_tail(1) |
| 54 | + self.linkedlist.insert_tail(2) |
| 55 | + self.linkedlist.insert_tail(3) |
| 56 | + self.linkedlist.insert_tail(4) |
| 57 | + self.linkedlist.insert_tail(5) |
| 58 | + |
| 59 | + def test_lenght(self): |
| 60 | + """ test for linked list lenght operation """ |
| 61 | + self.assertEqual(len(self.linkedlist), 5) |
| 62 | + |
| 63 | + def test_get_head(self): |
| 64 | + """ test for linked list get_head operation """ |
| 65 | + self.assertIsNotNone(self.linkedlist.head) |
| 66 | + |
| 67 | + def test_is_empty(self): |
| 68 | + """ test for linked list is_empty operation """ |
| 69 | + self.assertFalse(self.linkedlist.is_empty()) |
| 70 | + |
| 71 | + def test_insert_head(self): |
| 72 | + """ test for linked list insert_head operation """ |
| 73 | + self.linkedlist.insert_head(45) |
| 74 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [45, 1, 2, 3, 4, 5]) |
| 75 | + self.linkedlist.insert_head(34) |
| 76 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [34, 45, 1, 2, 3, 4, 5]) |
| 77 | + |
| 78 | + def test_insert_tail(self): |
| 79 | + """ test for linked list insert_tail operation """ |
| 80 | + self.linkedlist.insert_tail(45) |
| 81 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [1, 2, 3, 4, 5, 45]) |
| 82 | + self.linkedlist.insert_tail(34) |
| 83 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [1, 2, 3, 4, 5, 45, 34]) |
| 84 | + self.linkedlist.insert_tail(56) |
| 85 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [1, 2, 3, 4, 5, 45, 34, 56]) |
| 86 | + |
| 87 | + def test_insert_at(self): |
| 88 | + """ test for linked list insert_at operation """ |
| 89 | + self.linkedlist.insert_at(45, 2) |
| 90 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [1, 45, 2, 3, 4, 5]) |
| 91 | + self.linkedlist.insert_at(34, 4) |
| 92 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [1, 45, 2, 34, 3, 4, 5]) |
| 93 | + self.linkedlist.insert_at(44, 10) |
| 94 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [1, 45, 2, 34, 3, 4, 5, 44]) |
| 95 | + self.linkedlist.insert_at(23, 0) |
| 96 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [23, 1, 45, 2, 34, 3, 4, 5, 44]) |
| 97 | + self.linkedlist.insert_at(22, -2) |
| 98 | + self.assertListEqual(linkedlist_to_array(self.linkedlist), [22, 23, 1, 45, 2, 34, 3, 4, 5, 44]) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + unittest.main() |
0 commit comments