예제 #1
0
파일: test_ch2.py 프로젝트: rmartind/ctci
class TestRmTableDups(unittest.TestCase):
    def setUp(self):
        self.a = SinglyLinkedList()

    def test_table_no_dup(self):
        self.a.insert(4)
        self.a.insert(2)
        self.a.insert(3)
        rmdups.remove_with_table(self.a)

    def test_table_one_dup(self):
        self.a.insert(3)
        self.a.insert(2)
        self.a.insert(6)
        self.a.insert(2)
        self.a.insert(1)

        rmdups.remove_with_table(self.a)
        self.a.delete(2)
        self.assertEqual(self.a.search(2), None)

    def test_table_multi_dup(self):
        self.a.insert(3)
        self.a.insert(2)
        self.a.insert(6)
        self.a.insert(2)
        self.a.insert(1)
        self.a.insert(1)

        rmdups.remove_with_table(self.a)
        self.a.delete(2)
        self.a.delete(1)
        self.assertEqual(self.a.search(2), None)
        self.assertEqual(self.a.search(1), None)

    def test_table_edge_dup(self):
        self.a.insert(1)
        self.a.insert(2)
        self.a.insert(6)
        self.a.insert(2)
        self.a.insert(1)
        self.a.insert(1)

        rmdups.remove_with_table(self.a)
        self.a.delete(2)
        self.a.delete(1)
        self.assertEqual(self.a.search(2), None)
        self.assertEqual(self.a.search(1), None)
예제 #2
0
class TestSinglyLinkedList(unittest.TestCase):
    def setUp(self):
        self.a = SinglyLinkedList()

    def test_len_empty(self):

        self.assertEqual(len(self.a), 0)

    def test_len_one(self):
        self.a.insert(3)
        self.a.insert(2)

        self.assertEqual(len(self.a), 2)

    def test_len_two(self):
        self.a.insert(3)
        self.a.insert(2)
        self.a.insert(2)
        self.a.insert(2)
        self.a.insert(2)
        self.a.insert(2)

        self.assertEqual(len(self.a), 6)

    def test_insert_zero(self):
        self.a.insert(0)
        self.assertEqual(self.a.head.value, 0)

    def test_insert_one(self):
        self.a.insert(1)
        self.a.insert(2)
        self.a.insert(4)
        self.assertEqual(self.a.head.value, 4)

    def test_search_one(self):
        self.a.insert(44)
        found_node = self.a.search(44)
        self.assertIsInstance(found_node, Node)
        self.assertEqual(found_node.value, 44)

    def test_search_two(self):
        self.a.insert(44)
        self.a.insert(66)
        self.a.insert(67)
        self.a.insert(34)
        self.a.insert(78)
        found_node = self.a.search(78)
        self.assertIsInstance(found_node, Node)
        self.assertEqual(found_node.value, 78)

    def test_search_no_exist(self):
        self.a.insert(44)
        self.a.insert(66)
        self.a.insert(67)
        self.a.insert(34)
        self.a.insert(78)
        self.assertEqual(self.a.search(100), None)

    def test_delete_zero(self):
        self.a.insert(44)
        self.a.delete(44)
        self.assertEqual(self.a.search(66), None)

    def test_delete_one(self):
        self.a.insert(44)
        self.a.insert(66)
        self.a.insert(67)
        self.a.delete(66)
        self.assertEqual(self.a.search(66), None)

    def test_delete_two(self):
        self.a.insert(44)
        self.a.insert(66)
        self.a.insert(67)
        self.a.delete(67)
        self.assertEqual(self.a.search(67), None)