2020-07-07 09:09:04 +08:00
|
|
|
import unittest
|
2015-06-30 19:31:34 +08:00
|
|
|
|
|
|
|
|
2020-07-07 09:09:04 +08:00
|
|
|
class TestPalindrome(unittest.TestCase):
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-06-30 19:31:34 +08:00
|
|
|
def test_palindrome(self):
|
|
|
|
print('Test: Empty list')
|
|
|
|
linked_list = MyLinkedList()
|
2020-07-07 09:09:04 +08:00
|
|
|
self.assertEqual(linked_list.is_palindrome(), False)
|
2015-06-30 19:31:34 +08:00
|
|
|
|
|
|
|
print('Test: Single element list')
|
|
|
|
head = Node(1)
|
|
|
|
linked_list = MyLinkedList(head)
|
2020-07-07 09:09:04 +08:00
|
|
|
self.assertEqual(linked_list.is_palindrome(), False)
|
2015-06-30 19:31:34 +08:00
|
|
|
|
|
|
|
print('Test: Two element list, not a palindrome')
|
|
|
|
linked_list.append(2)
|
2020-07-07 09:09:04 +08:00
|
|
|
self.assertEqual(linked_list.is_palindrome(), False)
|
2015-06-30 19:31:34 +08:00
|
|
|
|
|
|
|
print('Test: General case: Palindrome with even length')
|
|
|
|
head = Node('a')
|
|
|
|
linked_list = MyLinkedList(head)
|
|
|
|
linked_list.append('b')
|
|
|
|
linked_list.append('b')
|
|
|
|
linked_list.append('a')
|
2020-07-07 09:09:04 +08:00
|
|
|
self.assertEqual(linked_list.is_palindrome(), True)
|
2015-06-30 19:31:34 +08:00
|
|
|
|
|
|
|
print('Test: General case: Palindrome with odd length')
|
|
|
|
head = Node(1)
|
|
|
|
linked_list = MyLinkedList(head)
|
|
|
|
linked_list.append(2)
|
|
|
|
linked_list.append(3)
|
|
|
|
linked_list.append(2)
|
|
|
|
linked_list.append(1)
|
2020-07-07 09:09:04 +08:00
|
|
|
self.assertEqual(linked_list.is_palindrome(), True)
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-06-30 19:31:34 +08:00
|
|
|
print('Success: test_palindrome')
|
|
|
|
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-06-30 19:31:34 +08:00
|
|
|
def main():
|
|
|
|
test = TestPalindrome()
|
|
|
|
test.test_palindrome()
|
|
|
|
|
2015-07-12 03:34:52 +08:00
|
|
|
|
2015-06-30 19:31:34 +08:00
|
|
|
if __name__ == '__main__':
|
2020-07-07 09:09:04 +08:00
|
|
|
main()
|