-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_utils_test.py
More file actions
169 lines (145 loc) · 5.34 KB
/
test_utils_test.py
File metadata and controls
169 lines (145 loc) · 5.34 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import unittest
import warnings
import math
from dalpy.arrays import Array
from dalpy.graphs import Graph, Vertex
from dalpy.test_utils import dalpy_equals, dalpy_to_string, generic_test, build_and_run_watched_suite, UnexpectedReturnWarning
from dalpy.factory_utils import make_array
from dalpy.trees import BinaryTreeNode, NaryTreeNode
class WarningTest(unittest.TestCase):
def test_trigger_warning_and_pass(self):
a = Array(0)
b = Array(0)
with warnings.catch_warnings(record=True) as w:
generic_test(a, b, lambda x: make_array([1]), in_place=True)
assert len(w) == 1
assert issubclass(w[-1].category, UnexpectedReturnWarning)
assert "modify its argument(s)" in str(w[-1].message)
def test_trigger_warning_and_fail(self):
a = Array(0)
b = Array(1)
with warnings.catch_warnings(record=True) as w:
try:
generic_test(a, b, lambda x: make_array([1]), in_place=True)
except AssertionError:
pass
assert len(w) == 1
assert issubclass(w[-1].category, UnexpectedReturnWarning)
assert "modify its argument(s)" in str(w[-1].message)
def test_trigger_warning_and_display(self):
a = Array(0)
b = Array(0)
generic_test(a, b, lambda x: make_array([1]), in_place=True)
def test_trigger_warning_and_display2(self):
a = Array(0)
b = Array(0)
generic_test(a, b, lambda x: make_array([1]), in_place=True)
class GenericTesterTest(unittest.TestCase):
def basic_generic_test(self):
a = Array(1)
b = Array(1)
generic_test(a, b, lambda x: x, enforce_no_mod=True)
def test_requiring_no_modification_fail(self):
a = Array(1)
b = Array(1)
b[0] = 1
def fnc_that_modifies(x):
x[0] = 1
return x
try:
generic_test(a, b, fnc_that_modifies, enforce_no_mod=True)
except AssertionError as e:
assert "1st" in e.args[0], e.args[0]
assert "[1]" in e.args[0], e.args[0]
def test_requiring_no_modification_pass(self):
a = Array(1)
b = Array(1)
b[0] = 1
def fnc_that_doesnt_modify(x):
c = Array(1)
c[0] = 1
return c
generic_test(a, b, fnc_that_doesnt_modify, enforce_no_mod=True)
def test_requiring_second_argument_no_modification(self):
a = make_array([1, 2])
b = make_array([1, 2])
c = make_array([1, 3])
def fnc_that_modifies_second(x, y):
x[1] = 4
y[1] = 3
return y
try:
generic_test([a,b], c, fnc_that_modifies_second, enforce_no_mod=[False, True])
except AssertionError as e:
assert "[[1, 4], [1, 3]]" in e.args[0], e.args[0]
assert "2nd" in e.args[0], e.args[0]
def test_multiple_larger(self):
g = Graph()
a = Vertex('a')
b = Vertex('b')
c = Vertex('c')
g.add_vertex(a)
g.add_vertex(b)
g.add_vertex(c)
g.add_edge(a, b, 0)
g.add_edge(a, c, 0)
g.add_edge(b, c, 2)
g.add_edge(c, a, 3)
x = list(range(15))
def fnc_that_modifies(graph, x):
e = Vertex('e')
g.add_vertex(e)
g.add_edge(e, c, 1)
try:
generic_test([g,x], None, fnc_that_modifies, enforce_no_mod=True, custom_comparator=lambda x,y:True)
except AssertionError as e:
assert '1st' in e.args[0], e.args[0]
assert 'e: c <1>' in e.args[0], e.args[0]
def test_larger_object(self):
g = Graph()
a = Vertex('a')
b = Vertex('b')
c = Vertex('c')
g.add_vertex(a)
g.add_vertex(b)
g.add_vertex(c)
g.add_edge(a, b, 0)
g.add_edge(a, c, 0)
g.add_edge(b, c, 2)
g.add_edge(c, a, 3)
def fnc_that_modifies(graph):
e = Vertex('e')
g.add_vertex(e)
g.add_edge(e, c, 1)
try:
generic_test(g, None, fnc_that_modifies, enforce_no_mod=True, custom_comparator=lambda x,y:True)
except AssertionError as e:
assert 'e: c <1>' in e.args[0], e.args[0]
def test_requiring_second_argument_no_modification_arrays(self):
a = make_array([1, 2, 3, 4, 5])
b = make_array([6, 7, 8, 9,])
c = 1
def fnc_that_modifies_second(x, y):
y[0] = -1000
return 1
try:
generic_test([a,b], c, fnc_that_modifies_second, enforce_no_mod=[True, True])
except AssertionError as e:
assert '[-1000, 7, 8, 9]' in e.args[0], e.args[0]
assert '2nd' in e.args[0], e.args[0]
class DALPyEqualsTest(unittest.TestCase):
def test_floats(self):
x = math.sqrt(7)*math.sqrt(7)
y = 7.0
assert dalpy_equals(x, y), f'y={y}, x={x}'
class DALPyToStringTest(unittest.TestCase):
def test_BTN(self):
t = BinaryTreeNode(10)
expected = f'[{t.data}]'
self.assertEqual(dalpy_to_string(t), f'[{t.data}]')
def test_NTN(self):
t = NaryTreeNode(10)
expected = f'[{t.data}]'
self.assertEqual(dalpy_to_string(t), f'[{t.data}]')
if __name__ == '__main__':
build_and_run_watched_suite([WarningTest, GenericTesterTest, DALPyEqualsTest])