|
1 | 1 | import unittest |
2 | | -import operator |
3 | 2 |
|
4 | | -import list_ops |
| 3 | +from list_ops import ( |
| 4 | + append, |
| 5 | + concat, |
| 6 | + foldl, |
| 7 | + foldr, |
| 8 | + length, |
| 9 | + reverse, |
| 10 | + filter as list_ops_filter, |
| 11 | + map as list_ops_map, |
| 12 | +) |
5 | 13 |
|
| 14 | +# Tests adapted from `problem-specifications//canonical-data.json` @ v2.4.1 |
6 | 15 |
|
7 | | -# Tests adapted from `problem-specifications//canonical-data.json` @ v2.4.0 |
8 | 16 |
|
9 | 17 | class ListOpsTest(unittest.TestCase): |
10 | | - |
11 | | - # test for append |
12 | 18 | def test_append_empty_lists(self): |
13 | | - self.assertEqual(list_ops.append([], []), []) |
| 19 | + self.assertEqual(append([], []), []) |
14 | 20 |
|
15 | | - def test_append_empty_list_to_list(self): |
16 | | - self.assertEqual(list_ops.append([], [1, 2, 3, 4]), [1, 2, 3, 4]) |
| 21 | + def test_append_list_to_empty_list(self): |
| 22 | + self.assertEqual(append([], [1, 2, 3, 4]), [1, 2, 3, 4]) |
17 | 23 |
|
18 | | - def test_append_nonempty_lists(self): |
19 | | - self.assertEqual(list_ops.append([1, 2], [2, 3, 4, 5]), |
20 | | - [1, 2, 2, 3, 4, 5]) |
| 24 | + def test_append_non_empty_lists(self): |
| 25 | + self.assertEqual(append([1, 2], [2, 3, 4, 5]), [1, 2, 2, 3, 4, 5]) |
21 | 26 |
|
22 | | - # tests for concat |
23 | 27 | def test_concat_empty_list(self): |
24 | | - self.assertEqual(list_ops.concat([]), []) |
| 28 | + self.assertEqual(concat([]), []) |
25 | 29 |
|
26 | 30 | def test_concat_list_of_lists(self): |
27 | | - self.assertEqual(list_ops.concat([[1, 2], [3], [], [4, 5, 6]]), |
28 | | - [1, 2, 3, 4, 5, 6]) |
| 31 | + self.assertEqual(concat([[1, 2], [3], [], [4, 5, 6]]), [1, 2, 3, 4, 5, 6]) |
29 | 32 |
|
30 | 33 | def test_concat_list_of_nested_lists(self): |
31 | 34 | self.assertEqual( |
32 | | - list_ops.concat([[[1], [2]], [[3]], [[]], [[4, 5, 6]]]), |
33 | | - [[1], [2], [3], [], [4, 5, 6]]) |
| 35 | + concat([[[1], [2]], [[3]], [[]], [[4, 5, 6]]]), |
| 36 | + [[1], [2], [3], [], [4, 5, 6]], |
| 37 | + ) |
34 | 38 |
|
35 | | - # tests for filter |
36 | 39 | def test_filter_empty_list(self): |
37 | | - self.assertEqual(list_ops.filter(lambda x: x % 2 == 1, []), []) |
| 40 | + self.assertEqual(list_ops_filter(lambda x: x % 2 == 1, []), []) |
38 | 41 |
|
39 | | - def test_filter_nonempty_list(self): |
40 | | - self.assertEqual( |
41 | | - list_ops.filter(lambda x: x % 2 == 1, [1, 2, 3, 4, 5]), |
42 | | - [1, 3, 5]) |
| 42 | + def test_filter_non_empty_list(self): |
| 43 | + self.assertEqual(list_ops_filter(lambda x: x % 2 == 1, [1, 2, 3, 5]), [1, 3, 5]) |
43 | 44 |
|
44 | | - # tests for length |
45 | 45 | def test_length_empty_list(self): |
46 | | - self.assertEqual(list_ops.length([]), 0) |
| 46 | + self.assertEqual(length([]), 0) |
47 | 47 |
|
48 | | - def test_length_nonempty_list(self): |
49 | | - self.assertEqual(list_ops.length([1, 2, 3, 4]), 4) |
| 48 | + def test_length_non_empty_list(self): |
| 49 | + self.assertEqual(length([1, 2, 3, 4]), 4) |
50 | 50 |
|
51 | | - # tests for map |
52 | 51 | def test_map_empty_list(self): |
53 | | - self.assertEqual(list_ops.map(lambda x: x + 1, []), []) |
| 52 | + self.assertEqual(list_ops_map(lambda x: x + 1, []), []) |
54 | 53 |
|
55 | | - def test_map_nonempty_list(self): |
56 | | - self.assertEqual(list_ops.map(lambda x: x + 1, [1, 3, 5, 7]), |
57 | | - [2, 4, 6, 8]) |
| 54 | + def test_map_non_empty_list(self): |
| 55 | + self.assertEqual(list_ops_map(lambda x: x + 1, [1, 3, 5, 7]), [2, 4, 6, 8]) |
58 | 56 |
|
59 | | - # tests for foldl |
60 | 57 | def test_foldl_empty_list(self): |
61 | | - self.assertEqual(list_ops.foldl(operator.mul, [], 2), 2) |
| 58 | + self.assertEqual(foldl(lambda x, y: x * y, [], 2), 2) |
62 | 59 |
|
63 | | - def test_foldl_nonempty_list_addition(self): |
64 | | - self.assertEqual(list_ops.foldl(operator.add, [1, 2, 3, 4], 5), 15) |
| 60 | + def test_foldl_direction_independent_function_applied_to_non_empty_list(self): |
| 61 | + self.assertEqual(foldl(lambda x, y: x + y, [1, 2, 3, 4], 5), 15) |
65 | 62 |
|
66 | | - def test_foldl_nonempty_list_floordiv(self): |
67 | | - self.assertEqual(list_ops.foldl(operator.floordiv, [2, 5], 5), 0) |
| 63 | + def test_foldl_direction_dependent_function_applied_to_non_empty_list(self): |
| 64 | + self.assertEqual(foldl(lambda x, y: x // y, [2, 5], 5), 0) |
68 | 65 |
|
69 | | - # tests for foldr |
70 | 66 | def test_foldr_empty_list(self): |
71 | | - self.assertEqual(list_ops.foldr(operator.mul, [], 2), 2) |
| 67 | + self.assertEqual(foldr(lambda x, y: x * y, [], 2), 2) |
72 | 68 |
|
73 | | - def test_foldr_nonempty_list_addition(self): |
74 | | - self.assertEqual(list_ops.foldr(operator.add, [1, 2, 3, 4], 5), 15) |
| 69 | + def test_foldr_direction_independent_function_applied_to_non_empty_list(self): |
| 70 | + self.assertEqual(foldr(lambda x, y: x + y, [1, 2, 3, 4], 5), 15) |
75 | 71 |
|
76 | | - def test_foldr_nonempty_list_floordiv(self): |
77 | | - self.assertEqual(list_ops.foldr(operator.floordiv, [2, 5], 5), 2) |
78 | | - |
79 | | - # additional test for foldr |
80 | | - def test_foldr_add_str(self): |
81 | | - self.assertEqual( |
82 | | - list_ops.foldr(operator.add, |
83 | | - ["e", "x", "e", "r", "c", "i", "s", "m"], "!"), |
84 | | - "exercism!") |
| 72 | + def test_foldr_direction_dependent_function_applied_to_non_empty_list(self): |
| 73 | + self.assertEqual(foldr(lambda x, y: x // y, [2, 5], 5), 2) |
85 | 74 |
|
86 | | - # tests for reverse |
87 | 75 | def test_reverse_empty_list(self): |
88 | | - self.assertEqual(list_ops.reverse([]), []) |
| 76 | + self.assertEqual(reverse([]), []) |
89 | 77 |
|
90 | | - def test_reverse_nonempty_list(self): |
91 | | - self.assertEqual(list_ops.reverse([1, 3, 5, 7]), [7, 5, 3, 1]) |
| 78 | + def test_reverse_non_empty_list(self): |
| 79 | + self.assertEqual(reverse([1, 3, 5, 7]), [7, 5, 3, 1]) |
92 | 80 |
|
93 | | - def test_reverse_list_of_lists_not_flattened(self): |
94 | | - self.assertEqual(list_ops.reverse([[1, 2], [3], [], [4, 5, 6]]), |
95 | | - [[4, 5, 6], [], [3], [1, 2]]) |
| 81 | + def test_reverse_list_of_lists_is_not_flattened(self): |
| 82 | + self.assertEqual( |
| 83 | + reverse([[1, 2], [3], [], [4, 5, 6]]), [[4, 5, 6], [], [3], [1, 2]] |
| 84 | + ) |
| 85 | + |
| 86 | + # Additional tests for this track |
96 | 87 |
|
97 | | - # additional test for reverse |
98 | | - def test_reverse_mixed_types(self): |
| 88 | + def test_foldr_foldr_add_string(self): |
99 | 89 | self.assertEqual( |
100 | | - list_ops.reverse(["xyz", 4.0, "cat", 1]), [1, "cat", 4.0, "xyz"]) |
| 90 | + foldr(lambda x, y: x + y, ["e", "x", "e", "r", "c", "i", "s", "m"], "!"), |
| 91 | + "exercism!", |
| 92 | + ) |
| 93 | + |
| 94 | + def test_reverse_reverse_mixed_types(self): |
| 95 | + self.assertEqual(reverse(["xyz", 4.0, "cat", 1]), [1, "cat", 4.0, "xyz"]) |
101 | 96 |
|
102 | 97 |
|
103 | | -if __name__ == '__main__': |
| 98 | +if __name__ == "__main__": |
104 | 99 | unittest.main() |
0 commit comments