Skip to content

Commit 31f4f98

Browse files
committed
Tests 4 W03
1 parent c123b47 commit 31f4f98

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

Week03/test_pyramid.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
3+
4+
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("pyramid")]
5+
for f in files:
6+
exec("import " + f[:-3] + " as " + f[:-3])
7+
print(f"The module {f[:-3]} has been imported.")
8+
9+
10+
def test_names():
11+
for f in files:
12+
assert "calculate_pyramid_height" in dir(eval(f[:-3])), (
13+
"calculate_pyramid_height is not defined in " + f[:-3]
14+
)
15+
16+
17+
def test_types():
18+
for f in files:
19+
assert callable(eval(f[:-3]).calculate_pyramid_height), (
20+
"calculate_pyramid_height is not callable in " + f[:-3]
21+
)
22+
assert isinstance(eval(f[:-3]).calculate_pyramid_height(1), int), (
23+
"calculate_pyramid_height is not returning an int in " + f[:-3]
24+
)
25+
26+
27+
def test_calculate_pyramid_height():
28+
for f in files:
29+
assert eval(f[:-3]).calculate_pyramid_height(1) == 1, (
30+
"calculate_pyramid_height is not working in " + f[:-3]
31+
)
32+
assert eval(f[:-3]).calculate_pyramid_height(2) == 1, (
33+
"calculate_pyramid_height is not working in " + f[:-3]
34+
)
35+
assert eval(f[:-3]).calculate_pyramid_height(6) == 3, (
36+
"calculate_pyramid_height is not working in " + f[:-3]
37+
)
38+
assert eval(f[:-3]).calculate_pyramid_height(20) == 5, (
39+
"calculate_pyramid_height is not working in " + f[:-3]
40+
)
41+
assert eval(f[:-3]).calculate_pyramid_height(100) == 13, (
42+
"calculate_pyramid_height is not working in " + f[:-3]
43+
)
44+
assert eval(f[:-3]).calculate_pyramid_height(1000) == 44, (
45+
"calculate_pyramid_height is not working in " + f[:-3]
46+
)
47+
assert eval(f[:-3]).calculate_pyramid_height(10000) == 140, (
48+
"calculate_pyramid_height is not working in " + f[:-3]
49+
)
50+
assert eval(f[:-3]).calculate_pyramid_height(100000) == 446, (
51+
"calculate_pyramid_height is not working in " + f[:-3]
52+
)

Week03/test_sequences.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import os
2+
3+
4+
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("sequences")]
5+
for f in files:
6+
exec("import " + f[:-3] + " as " + f[:-3])
7+
print(f"The module {f[:-3]} has been imported.")
8+
9+
10+
def test_names():
11+
for f in files:
12+
assert "remove_duplicates" in dir(eval(f[:-3])), (
13+
"remove_duplicates is not defined in " + f[:-3]
14+
)
15+
assert "list_counts" in dir(eval(f[:-3])), (
16+
"list_counts is not defined in " + f[:-3]
17+
)
18+
assert "reverse_dict" in dir(eval(f[:-3])), (
19+
"reverse_dict is not defined in " + f[:-3]
20+
)
21+
22+
23+
def test_types():
24+
for f in files:
25+
assert callable(eval(f[:-3]).remove_duplicates), (
26+
"remove_duplicates is not callable in " + f[:-3]
27+
)
28+
assert callable(eval(f[:-3]).list_counts), (
29+
"list_counts is not callable in " + f[:-3]
30+
)
31+
assert callable(eval(f[:-3]).reverse_dict), (
32+
"reverse_dict is not callable in " + f[:-3]
33+
)
34+
assert isinstance(eval(f[:-3]).remove_duplicates([1, 2, 3, 4, 5, 6]), list), (
35+
"remove_duplicates is not returning a list in " + f[:-3]
36+
)
37+
assert isinstance(eval(f[:-3]).list_counts([1, 2, 3, 4, 5, 6]), dict), (
38+
"list_counts is not returning a dict in " + f[:-3]
39+
)
40+
assert isinstance(eval(f[:-3]).reverse_dict({1: 1, 2: 2, 3: 3}), dict), (
41+
"reverse_dict is not returning a dict in " + f[:-3]
42+
)
43+
44+
45+
def test_remove_duplicates():
46+
for f in files:
47+
assert eval(f[:-3]).remove_duplicates([1, 2, 3, 3, 4, 5, 5, 5, 6]) == [
48+
1,
49+
2,
50+
3,
51+
4,
52+
5,
53+
6,
54+
], (
55+
"remove_duplicates is not working in " + f[:-3]
56+
)
57+
assert eval(f[:-3]).remove_duplicates([1, 2, 3, 4, 5, 6]) == [
58+
1,
59+
2,
60+
3,
61+
4,
62+
5,
63+
6,
64+
], (
65+
"remove_duplicates is not working in " + f[:-3]
66+
)
67+
assert eval(f[:-3]).remove_duplicates([1, 1, 1, 1, 1, 1]) == [1], (
68+
"remove_duplicates is not working in " + f[:-3]
69+
)
70+
assert eval(f[:-3]).remove_duplicates([]) == [], (
71+
"remove_duplicates is not working in " + f[:-3]
72+
)
73+
74+
75+
def test_list_counts():
76+
for f in files:
77+
assert eval(f[:-3]).list_counts([1, 2, 3, 3, 4, 5, 5, 5, 6]) == {
78+
1: 1,
79+
2: 1,
80+
3: 2,
81+
4: 1,
82+
5: 3,
83+
6: 1,
84+
}, (
85+
"list_counts is not working in " + f[:-3]
86+
)
87+
assert eval(f[:-3]).list_counts([1, 2, 3, 4, 5, 6]) == {
88+
1: 1,
89+
2: 1,
90+
3: 1,
91+
4: 1,
92+
5: 1,
93+
6: 1,
94+
}, (
95+
"list_counts is not working in " + f[:-3]
96+
)
97+
assert eval(f[:-3]).list_counts([1, 1, 1, 1, 1, 1]) == {1: 6}, (
98+
"list_counts is not working in " + f[:-3]
99+
)
100+
assert eval(f[:-3]).list_counts([]) == {}, (
101+
"list_counts is not working in " + f[:-3]
102+
)
103+
104+
105+
def test_reverse_dict():
106+
for f in files:
107+
assert eval(f[:-3]).reverse_dict({1: 1, 2: 2, 3: 3}) == {1: 1, 2: 2, 3: 3}, (
108+
"reverse_dict is not working in " + f[:-3]
109+
)
110+
assert eval(f[:-3]).reverse_dict({1: 2, 2: 3, 3: 4}) == {2: 1, 3: 2, 4: 3}, (
111+
"reverse_dict is not working in " + f[:-3]
112+
)
113+
assert eval(f[:-3]).reverse_dict({1: 1, 2: 1, 3: 1}) == {1: 3}, (
114+
"reverse_dict is not working in " + f[:-3]
115+
)
116+
assert eval(f[:-3]).reverse_dict({}) == {}, (
117+
"reverse_dict is not working in " + f[:-3]
118+
)

0 commit comments

Comments
 (0)