コード例 #1
0
ファイル: linked_list_test.py プロジェクト: a1ip/algorithms-1
class LinkedListTest(unittest.TestCase):
    def setUp(self):
        self.ll = LinkedList()

    def tearDown(self):
        self.ll = None
        pass

    def test_insert(self):
        self.ll.insert(5)
        self.assertEqual(self.ll.toList(), [5])
        self.assertNotEqual(self.ll.toList(), [6])

        self.ll.insert(10)
        self.assertEqual(self.ll.toList(), [5, 10])

    def test_insert_after(self):
        self.ll.insert(5)
        self.ll.insert(10)

        self.ll.insert_after(6, 7)
        self.assertEqual(self.ll.toList(), [5, 7, 10])

        self.ll.insert_after(100, 1000)
        self.assertEqual(self.ll.toList(), [5, 7, 10, 1000])

    def test_delete(self):
        self.ll.insert(5)
        self.ll.delete(5)
        self.assertEqual(self.ll.toList(), [])

        self.ll.insert(10)
        self.ll.insert(100)
        self.ll.insert(1000)
        self.ll.delete(100)
        self.assertEqual(self.ll.toList(), [10, 1000])

        self.ll.delete(1000)
        self.assertEqual(self.ll.toList(), [10])

        self.ll.delete(10)
        self.assertEqual(self.ll.toList(), [])

        self.ll.delete(100)
        self.assertEqual(self.ll.toList(), [])

    def test_delete_at(self):
        self.ll.delete_at(0)
        self.assertEqual(len(self.ll), 0)

        self.ll.insert(5)
        self.ll.delete_at(0)
        self.assertEqual(len(self.ll), 0)

        self.ll.push(10)
        self.ll.push(100)
        self.ll.push(1000)
        self.ll.delete_at(2)
        self.assertEqual(self.ll.toList(), [1000, 100])

    def test_length(self):
        self.assertEqual(len(self.ll), 0)

        self.ll.insert(5)
        self.assertEqual(len(self.ll), 1)

        self.ll.insert(5)
        self.assertEqual(len(self.ll), 2)

        self.ll.insert(10)
        self.ll.insert(100)
        self.ll.insert(1000)
        self.ll.delete(100)
        self.assertEqual(len(self.ll), 4)

    def test_contains(self):
        self.assertFalse(5 in self.ll)

        self.ll.insert(5)
        self.assertTrue(5 in self.ll)

        self.ll.push(0)
        self.assertTrue(0 in self.ll)
        self.assertFalse(3 in self.ll)

    def test_push(self):
        self.ll.push(5)
        self.assertEqual(self.ll.toList(), [5])

        self.ll.push(10)
        self.assertEqual(self.ll.toList(), [10, 5])

    def test_pop(self):
        self.ll.push(5)
        self.assertEqual(self.ll.pop(), 5)
        self.assertEqual(self.ll.toList(), [])

        self.ll.push(1)
        self.ll.push(2)
        self.ll.push(3)
        self.ll.push(4)
        self.ll.push(100)
        self.assertEqual(self.ll.pop(), 100)
        self.assertEqual(self.ll.toList(), [4, 3, 2, 1])

    def test_swap_at(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.ll.swap_at(1, 3)
        self.assertEqual(self.ll.toList(), [1, 4, 3, 2, 5])

        self.ll.swap_at(0, 4)
        self.assertEqual(self.ll.toList(), [5, 4, 3, 2, 1])

        self.ll.swap_at(0, 1)
        self.assertEqual(self.ll.toList(), [4, 5, 3, 2, 1])

        self.ll.swap_at(4, 3)
        self.assertEqual(self.ll.toList(), [4, 5, 3, 1, 2])

    def test_kth(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.assertEqual(self.ll.kth(0), 1)
        self.assertEqual(self.ll.kth(3), 4)

    def test_last_kth(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.assertEqual(self.ll.last_kth(1), 5)
        self.assertEqual(self.ll.last_kth(3), 3)

    def test_count(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(5)
        self.ll.push(1)

        self.assertEqual(self.ll.count(1), 1)
        self.assertEqual(self.ll.count(5), 2)

    def test_reverse(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.ll.reverse()

        self.assertEqual(self.ll.toList(), [5, 4, 3, 2, 1])
コード例 #2
0
class TestLinkedList(TestCase):
    def setUp(self):
        self.al = LinkedList()

    def test_append__when_list_is_empty__expect_append_to_the_end(self):
        self.al.append(1)
        values = list(self.al)

        self.assertListEqual([1], values)

    def test_append__expect_to_return_the_list(self):
        result = self.al.append(1)
        self.assertEqual(self.al, result)

    def test_append__when_list_not_empty__expect_append_to_the_end(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(3)
        values = list(self.al)

        self.assertListEqual([1, 2, 3], values)

    def test_append__1024_values__expect_append_to_the_end(self):
        values = [x for x in range(1024)]
        [self.al.append(x) for x in values]
        list_values = list(self.al)

        self.assertListEqual(values, list_values)

    def test_append__expect_to_increase_size(self):
        self.al.append(1)

        self.assertEqual(1, self.al.size())

    def test_remove__when_index_is_valid__expect_remove_values_and_return_it(
            self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(333)
        self.al.append(4)

        result = self.al.remove(2)
        self.assertListEqual([1, 2, 4], list(self.al))
        self.assertEqual(333, result)

    def test_remove__when_index_is_invalid__expect_to_raise(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(3)

        with self.assertRaises(IndexError):
            self.al.remove(self.al.size())

    def test_get__when_index_is_valid__expect_to_return_it(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(333)
        self.al.append(4)

        result = self.al.get(2)
        self.assertEqual(333, result)

    def test_get__when_index_is_invalid__expect_to_raise(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(3)

        with self.assertRaises(IndexError):
            self.al.get(self.al.size())

    def test_extend__with_empty_iterable__expect_to_be_same(self):
        self.al.append(1)

        self.al.extend([])
        self.assertListEqual([1], list(self.al))

    def test_extend__with_list__expect_to_append_the_list(self):
        self.al.append(1)

        self.al.extend([2])
        self.assertListEqual([1, 2], list(self.al))

    def test_extend__with_generator__expect_to_append_the_list(self):
        self.al.append(1)

        self.al.extend((x for x in range(1)))
        self.assertListEqual([1, 0], list(self.al))

    def test_extend__when_empty__expect_to_append_to_list(self):
        self.al.append(1)

        self.al.extend([1])
        self.assertListEqual([1, 1], list(self.al))

    def test_extend__with_not_iterable_expect_to_raise(self):
        self.al.append(1)

        with self.assertRaises(ValueError):
            self.al.extend(2)

    def test_insert__when_index_is_valid__expect_to_place_value_at_index(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(4)
        self.al.append(5)
        self.al.append(6)
        self.al.append(7)
        self.al.append(8)
        self.al.append(9)

        self.al.insert(2, 333)
        self.assertListEqual([1, 2, 333, 4, 5, 6, 7, 8, 9], list(self.al))

    def test_insert__when_index_is_invalid__expect_to_raise(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(3)

        with self.assertRaises(IndexError):
            self.al.insert(self.al.size() + 1, 2)

    def test_pop__expect_to_remove_the_last_value_and_return_it(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(3)
        self.al.append(4)

        result = self.al.pop()

        self.assertEqual(4, result)
        self.assertListEqual([1, 2, 3], list(self.al))

    def test_pop__when_empty__expect_to_raise(self):
        with self.assertRaises(IndexError):
            self.al.pop()

    def test_clear__expect_to_be_empty(self):
        [self.al.append(x) for x in range(15)]
        self.al.clear()
        self.assertEqual([], list(self.al))

    def test_index__when_item_is_present__expect_return_correct_index(self):
        [self.al.append(x) for x in range(15)]

        index = self.al.index(5)

        self.assertEqual(5, index)

    def test_index__when_item_is_not_present__expect_raise(self):
        [self.al.append(x) for x in range(15)]

        with self.assertRaises(ValueError):
            self.al.index(17)

    def test_count__when_item_is_present_one_time__expect_to_return_1(self):
        [self.al.append(x) for x in range(15)]

        expected_count = 1
        actual_count = self.al.count(5)
        self.assertEqual(expected_count, actual_count)

    def test_count__when_item_is_present_multiple_times__expect_to_return_correct_count(
            self):
        [self.al.append(x) for x in range(15)]
        self.al.append(5)
        self.al.insert(3, 5)
        self.al.insert(7, 5)
        self.al.insert(1, 5)
        self.al.insert(9, 5)

        expected_count = 6
        actual_count = self.al.count(5)
        self.assertEqual(expected_count, actual_count)

    def test_count__when_item_is_present_multiple_times_and_once_poped__expect_to_return_correct_count(
            self):
        [self.al.append(x) for x in range(15)]
        self.al.insert(3, 5)
        self.al.insert(7, 5)
        self.al.insert(1, 5)
        self.al.insert(9, 5)
        self.al.append(5)
        self.al.pop()

        expected_count = 5
        actual_count = self.al.count(5)
        self.assertEqual(expected_count, actual_count)

    def test_count__when_item_is_not_present__expect_to_return_0(self):
        [self.al.append(x) for x in range(15)]

        expected_count = 0
        actual_count = self.al.count(55)
        self.assertEqual(expected_count, actual_count)

    def test_copy__expect_to_return_another_list_with_same_values(self):
        [self.al.append(x) for x in range(5)]

        copied_list = self.al.copy()

        expected_result = [x for x in range(5)]
        actual_result = list(copied_list)

        self.assertNotEqual(copied_list, self.al)
        self.assertListEqual(expected_result, actual_result)

    def test_add_first__when_empty__expect_to_add(self):
        self.al.add_first(1)

        self.assertListEqual([1], list(self.al))

    def test_add_first__when_non_empty__expect_to_add(self):
        [self.al.append(x) for x in range(5)]

        self.al.add_first(1)

        self.assertListEqual([1, 0, 1, 2, 3, 4], list(self.al))
コード例 #3
0
ファイル: linked_list_test.py プロジェクト: a1ip/algorithms-1
class LinkedListTest(unittest.TestCase):
    def setUp(self):
        self.ll = LinkedList()

    def tearDown(self):
        self.ll = None
        pass

    def test_insert(self):
        self.ll.insert(5)
        self.assertEqual(self.ll.toList(), [5])
        self.assertNotEqual(self.ll.toList(), [6])

        self.ll.insert(10)
        self.assertEqual(self.ll.toList(), [5, 10])

    def test_insert_after(self):
        self.ll.insert(5)
        self.ll.insert(10)

        self.ll.insert_after(6, 7)
        self.assertEqual(self.ll.toList(), [5, 7, 10])

        self.ll.insert_after(100, 1000)
        self.assertEqual(self.ll.toList(), [5, 7, 10, 1000])

    def test_delete(self):
        self.ll.insert(5)
        self.ll.delete(5)
        self.assertEqual(self.ll.toList(), [])

        self.ll.insert(10)
        self.ll.insert(100)
        self.ll.insert(1000)
        self.ll.delete(100)
        self.assertEqual(self.ll.toList(), [10, 1000])

        self.ll.delete(1000)
        self.assertEqual(self.ll.toList(), [10])

        self.ll.delete(10)
        self.assertEqual(self.ll.toList(), [])

        self.ll.delete(100)
        self.assertEqual(self.ll.toList(), [])

    def test_delete_at(self):
        self.ll.delete_at(0)
        self.assertEqual(len(self.ll), 0)

        self.ll.insert(5)
        self.ll.delete_at(0)
        self.assertEqual(len(self.ll), 0)

        self.ll.push(10)
        self.ll.push(100)
        self.ll.push(1000)
        self.ll.delete_at(2)
        self.assertEqual(self.ll.toList(), [1000,100])

    def test_length(self):
        self.assertEqual(len(self.ll), 0)

        self.ll.insert(5)
        self.assertEqual(len(self.ll), 1)

        self.ll.insert(5)
        self.assertEqual(len(self.ll), 2)

        self.ll.insert(10)
        self.ll.insert(100)
        self.ll.insert(1000)
        self.ll.delete(100)
        self.assertEqual(len(self.ll), 4)

    def test_contains(self):
        self.assertFalse(5 in self.ll)

        self.ll.insert(5)
        self.assertTrue(5 in self.ll)

        self.ll.push(0)
        self.assertTrue(0 in self.ll)
        self.assertFalse(3 in self.ll)


    def test_push(self):
        self.ll.push(5)
        self.assertEqual(self.ll.toList(), [5])

        self.ll.push(10)
        self.assertEqual(self.ll.toList(), [10, 5])

    def test_pop(self):
        self.ll.push(5)
        self.assertEqual(self.ll.pop(), 5)
        self.assertEqual(self.ll.toList(), [])

        self.ll.push(1)
        self.ll.push(2)
        self.ll.push(3)
        self.ll.push(4)
        self.ll.push(100)
        self.assertEqual(self.ll.pop(), 100)
        self.assertEqual(self.ll.toList(), [4, 3, 2, 1])

    def test_swap_at(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.ll.swap_at(1, 3)
        self.assertEqual(self.ll.toList(), [1, 4, 3, 2, 5])

        self.ll.swap_at(0, 4)
        self.assertEqual(self.ll.toList(), [5, 4, 3, 2, 1])

        self.ll.swap_at(0, 1)
        self.assertEqual(self.ll.toList(), [4, 5, 3, 2, 1])

        self.ll.swap_at(4, 3)
        self.assertEqual(self.ll.toList(), [4, 5, 3, 1, 2])

    def test_kth(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.assertEqual(self.ll.kth(0), 1)
        self.assertEqual(self.ll.kth(3), 4)

    def test_last_kth(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.assertEqual(self.ll.last_kth(1), 5)
        self.assertEqual(self.ll.last_kth(3), 3)

    def test_count(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(5)
        self.ll.push(1)

        self.assertEqual(self.ll.count(1), 1)
        self.assertEqual(self.ll.count(5), 2)

    def test_reverse(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.ll.reverse()

        self.assertEqual(self.ll.toList(), [5, 4, 3, 2, 1])