-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_ops_test.py
104 lines (74 loc) · 3.33 KB
/
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
96
97
98
99
100
101
102
103
104
import unittest
from list_ops import (
append,
concat,
foldl,
foldr,
length,
reverse,
filter as list_ops_filter,
map as list_ops_map,
)
# Tests adapted from `problem-specifications//canonical-data.json`
class ListOpsTest(unittest.TestCase):
def test_append_empty_lists(self):
self.assertEqual(append([], []), [])
def test_append_list_to_empty_list(self):
self.assertEqual(append([], [1, 2, 3, 4]), [1, 2, 3, 4])
def test_append_empty_list_to_list(self):
self.assertEqual(append([1, 2, 3, 4], []), [1, 2, 3, 4])
def test_append_non_empty_lists(self):
self.assertEqual(append([1, 2], [2, 3, 4, 5]), [1, 2, 2, 3, 4, 5])
def test_concat_empty_list(self):
self.assertEqual(concat([]), [])
def test_concat_list_of_lists(self):
self.assertEqual(concat([[1, 2], [3], [], [4, 5, 6]]), [1, 2, 3, 4, 5, 6])
def test_concat_list_of_nested_lists(self):
self.assertEqual(
concat([[[1], [2]], [[3]], [[]], [[4, 5, 6]]]),
[[1], [2], [3], [], [4, 5, 6]],
)
def test_filter_empty_list(self):
self.assertEqual(list_ops_filter(lambda x: x % 2 == 1, []), [])
def test_filter_non_empty_list(self):
self.assertEqual(list_ops_filter(lambda x: x % 2 == 1, [1, 2, 3, 5]), [1, 3, 5])
def test_length_empty_list(self):
self.assertEqual(length([]), 0)
def test_length_non_empty_list(self):
self.assertEqual(length([1, 2, 3, 4]), 4)
def test_map_empty_list(self):
self.assertEqual(list_ops_map(lambda x: x + 1, []), [])
def test_map_non_empty_list(self):
self.assertEqual(list_ops_map(lambda x: x + 1, [1, 3, 5, 7]), [2, 4, 6, 8])
def test_foldl_empty_list(self):
self.assertEqual(foldl(lambda acc, el: el * acc, [], 2), 2)
def test_foldl_direction_independent_function_applied_to_non_empty_list(self):
self.assertEqual(foldl(lambda acc, el: el + acc, [1, 2, 3, 4], 5), 15)
def test_foldl_direction_dependent_function_applied_to_non_empty_list(self):
self.assertEqual(foldl(lambda acc, el: el / acc, [1, 2, 3, 4], 24), 64)
def test_foldr_empty_list(self):
self.assertEqual(foldr(lambda acc, el: el * acc, [], 2), 2)
def test_foldr_direction_independent_function_applied_to_non_empty_list(self):
self.assertEqual(foldr(lambda acc, el: el + acc, [1, 2, 3, 4], 5), 15)
def test_foldr_direction_dependent_function_applied_to_non_empty_list(self):
self.assertEqual(foldr(lambda acc, el: el / acc, [1, 2, 3, 4], 24), 9)
def test_reverse_empty_list(self):
self.assertEqual(reverse([]), [])
def test_reverse_non_empty_list(self):
self.assertEqual(reverse([1, 3, 5, 7]), [7, 5, 3, 1])
def test_reverse_list_of_lists_is_not_flattened(self):
self.assertEqual(
reverse([[1, 2], [3], [], [4, 5, 6]]), [[4, 5, 6], [], [3], [1, 2]]
)
# Additional tests for this track
def test_foldr_foldr_add_string(self):
self.assertEqual(
foldr(
lambda acc, el: el + acc, ["e", "x", "e", "r", "c", "i", "s", "m"], "!"
),
"exercism!",
)
def test_reverse_reverse_mixed_types(self):
self.assertEqual(reverse(["xyz", 4.0, "cat", 1]), [1, "cat", 4.0, "xyz"])
if __name__ == "__main__":
unittest.main()