1
exercism fetch python list-ops

list_ops_test.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import unittest
import operator

import list_ops


# Tests adapted from problem-specifications//canonical-data.json @ v2.0.0

class ListOpsTest(unittest.TestCase):

    # test for append
    def test_append_empty_lists(self):
        self.assertEqual(list_ops.append([], []), [])

    def test_append_empty_list_to_list(self):
        self.assertEqual(list_ops.append([], [1, 2, 3, 4]), [1, 2, 3, 4])

    def test_append_nonempty_lists(self):
        self.assertEqual(list_ops.append([1, 2], [2, 3, 4, 5]),
                         [1, 2, 2, 3, 4, 5])

    # tests for concat
    def test_concat_empty_list(self):
        self.assertEqual(list_ops.concat([]), [])

    def test_concat_list_of_lists(self):
        self.assertEqual(list_ops.concat([[1, 2], [3], [], [4, 5, 6]]),
                         [1, 2, 3, 4, 5, 6])

    # tests for filter_clone
    def test_filter_empty_list(self):
        self.assertEqual(list_ops.filter_clone(lambda x: x % 2 == 1, []), [])

    def test_filter_nonempty_list(self):
        self.assertEqual(
            list_ops.filter_clone(lambda x: x % 2 == 1, [1, 2, 3, 4, 5]),
            [1, 3, 5])

    # tests for length
    def test_length_empty_list(self):
        self.assertEqual(list_ops.length([]), 0)

    def test_length_nonempty_list(self):
        self.assertEqual(list_ops.length([1, 2, 3, 4]), 4)

    # tests for map_clone
    def test_map_empty_list(self):
        self.assertEqual(list_ops.map_clone(lambda x: x + 1, []), [])

    def test_map_nonempty_list(self):
        self.assertEqual(list_ops.map_clone(lambda x: x + 1, [1, 3, 5, 7]),
                         [2, 4, 6, 8])

    # tests for foldl
    def test_foldl_empty_list(self):
        self.assertEqual(list_ops.foldl(operator.mul, [], 2), 2)

    def test_foldl_nonempty_list_addition(self):
        self.assertEqual(list_ops.foldl(operator.add, [1, 2, 3, 4], 5), 15)

    def test_foldl_nonempty_list_floordiv(self):
        self.assertEqual(list_ops.foldl(operator.floordiv, [2, 5], 5), 0)

    # tests for foldr
    def test_foldr_empty_list(self):
        self.assertEqual(list_ops.foldr(operator.mul, [], 2), 2)

    def test_foldr_nonempty_list_addition(self):
        self.assertEqual(list_ops.foldr(operator.add, [1, 2, 3, 4], 5), 15)

    def test_foldr_nonempty_list_floordiv(self):
        self.assertEqual(list_ops.foldr(operator.floordiv, [2, 5], 5), 2)

    # additional test for foldr
    def test_foldr_add_str(self):
        self.assertEqual(
            list_ops.foldr(operator.add,
                           ["e", "x", "e", "r", "c", "i", "s", "m"], "!"),
            "exercism!")

    # tests for reverse
    def test_reverse_empty_list(self):
        self.assertEqual(list_ops.reverse([]), [])

    def test_reverse_nonempty_list(self):
        self.assertEqual(list_ops.reverse([1, 3, 5, 7]), [7, 5, 3, 1])

    # additional test for reverse
    def test_reverse_mixed_types(self):
        self.assertEqual(
            list_ops.reverse(["xyz", 4.0, "cat", 1]), [1, "cat", 4.0, "xyz"])


if __name__ == '__main__':
    unittest.main()