2020-07-07 09:09:04 +08:00
|
|
|
import unittest
|
2015-07-03 11:11:35 +08:00
|
|
|
|
|
|
|
|
2020-07-07 09:09:04 +08:00
|
|
|
class TestPartition(unittest.TestCase):
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-07-03 11:11:35 +08:00
|
|
|
def test_partition(self):
|
|
|
|
print('Test: Empty list')
|
|
|
|
linked_list = MyLinkedList(None)
|
|
|
|
linked_list.partition(10)
|
2020-07-07 09:09:04 +08:00
|
|
|
self.assertEqual(linked_list.get_all_data(), [])
|
2015-07-03 11:11:35 +08:00
|
|
|
|
|
|
|
print('Test: One element list, left list empty')
|
|
|
|
linked_list = MyLinkedList(Node(5))
|
|
|
|
linked_list.partition(0)
|
2020-07-07 09:09:04 +08:00
|
|
|
self.assertEqual(linked_list.get_all_data(), [5])
|
2015-07-03 11:11:35 +08:00
|
|
|
|
|
|
|
print('Test: Right list is empty')
|
|
|
|
linked_list = MyLinkedList(Node(5))
|
|
|
|
linked_list.partition(10)
|
2020-07-07 09:09:04 +08:00
|
|
|
self.assertEqual(linked_list.get_all_data(), [5])
|
2015-07-03 11:11:35 +08:00
|
|
|
|
|
|
|
print('Test: General case')
|
|
|
|
# Partition = 10
|
2015-07-14 19:25:17 +08:00
|
|
|
# Input: 4, 3, 13, 8, 10, 1, 14, 10, 12
|
|
|
|
# Output: 4, 3, 8, 1, 10, 10, 13, 14, 12
|
2015-07-03 11:11:35 +08:00
|
|
|
linked_list = MyLinkedList(Node(12))
|
|
|
|
linked_list.insert_to_front(10)
|
2015-07-14 19:25:17 +08:00
|
|
|
linked_list.insert_to_front(14)
|
2015-07-03 11:11:35 +08:00
|
|
|
linked_list.insert_to_front(1)
|
|
|
|
linked_list.insert_to_front(10)
|
|
|
|
linked_list.insert_to_front(8)
|
2015-07-14 19:25:17 +08:00
|
|
|
linked_list.insert_to_front(13)
|
2015-07-03 11:11:35 +08:00
|
|
|
linked_list.insert_to_front(3)
|
|
|
|
linked_list.insert_to_front(4)
|
|
|
|
partitioned_list = linked_list.partition(10)
|
2020-07-07 09:09:04 +08:00
|
|
|
self.assertEqual(partitioned_list.get_all_data(),
|
2015-07-14 19:25:17 +08:00
|
|
|
[4, 3, 8, 1, 10, 10, 13, 14, 12])
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-07-03 11:11:35 +08:00
|
|
|
print('Success: test_partition')
|
|
|
|
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-07-03 11:11:35 +08:00
|
|
|
def main():
|
|
|
|
test = TestPartition()
|
|
|
|
test.test_partition()
|
2015-07-12 03:34:52 +08:00
|
|
|
|
|
|
|
|
2015-07-03 11:11:35 +08:00
|
|
|
if __name__ == '__main__':
|
2020-07-07 09:09:04 +08:00
|
|
|
main()
|