forked from mwshinn/CanD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_metrics.py
201 lines (175 loc) · 9.05 KB
/
test_metrics.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from cand import *
import numpy as np
lower_points = [Point(.3, .2, "newunit"),
Point(.02, .4, "absolute")]
def points_close(p1, p2):
return np.isclose(p1.x, p2.x) and np.isclose(p1.y, p2.y)
vectors = []
points = []
axes = ["default", "figure", "newunit", "axis_ax1", "ax1", "absolute"]
for ax in axes:
vectors.append(Vector(.3, .7, ax))
vectors.append(Vector(-.2, -.9, ax)+Vector(.22, .1, "absolute"))
vectors.append(Vector(0, .3, ax)+Vector(.01, -.01, "ax1")-Vector(1.1, 1.1, "default"))
vectors.append((Point(.2, .3, ax) - Point(.5, .1, "newunit")))
vectors.append((Point(.2, .3, ax) >> Point(1, 1, "absolute")) - Point(.5, .1, "newunit"))
vectors.append((Point(.2, .3, ax) << Point(1, 1, "absolute")) - Point(.5, .1, "newunit"))
vectors.append((Point(.2, .3, ax) | Point(1, 1, "absolute")) - Point(.5, .1, "newunit"))
for v in [(0, 0), (1, 0), (0, 1), (1.1, -1.3)]:
vectors.append(Vector(*v, "figure"))
vectors.append(Vector(*v, "absolute"))
vectors.append(Vector(*v, "newunit"))
for ax in axes:
points.append(Point(.3, .2, ax))
points.append(Point(0, 0, ax))
points.append(Point(0, 1, ax))
points.append(Point(100, 0, ax))
points.append(Point(.1, -.2, ax) + Vector(1, 1, "absolute"))
points.append(Point(.1, -.2, ax) - Vector(1, 1, "absolute"))
points.append(Point(1, 1, ax) >> Point(.3, .1, "absolute"))
points.append(Point(1, 1, ax) << Point(.3, .1, "figure"))
points.append(Point(1, 1, ax) | Point(.3, .1, "figure"))
points.append(Point(.1, -.2, "figure") + (Point(1.1, 2.1, ax) - Point(.3, .6, "absolute"))/2)
def test_width_and_height_methods():
c = Canvas(5, 5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", lower_points[0], Point(.9, .95))
for v1 in vectors:
h = c.convert_to_absolute_coord(v1.height())
w = c.convert_to_absolute_length(v1.width())
v = c.convert_to_absolute_coord(v1)
assert points_close(v, h+w)
assert points_close(h, v.height())
assert points_close(w, v.width())
def test_associative_vector_multiplication():
for l in lower_points:
c = Canvas(5, 5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", l, Point(.9, .95))
for v1 in vectors:
p1 = c.convert_to_absolute_coord(2.1*(1.2*v1))
p2 = c.convert_to_absolute_coord((2.1*1.2)*v1)
assert points_close(p1, p2), f"Failed for {p1} and {p2}"
def test_associative_vector_division():
for l in lower_points:
c = Canvas(5, 5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", l, Point(.9, .95))
for v1 in vectors:
p1 = c.convert_to_absolute_coord(v1/1.2/2.1)
p2 = c.convert_to_absolute_coord(v1/(1.2*2.1))
assert points_close(p1, p2), f"Failed for {p1} and {p2}"
def test_vector_identities():
c = Canvas(5, 5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", Point(.4, .4), Point(.9, .95))
for v in vectors:
assert points_close(c.convert_to_absolute_coord(v), c.convert_to_absolute_coord(v+Vector(0,0))) # Additive identity
assert points_close(Vector(0, 0), c.convert_to_absolute_coord(v-v)) # Additive identity via subtraction
assert points_close(c.convert_to_absolute_coord(v), c.convert_to_absolute_coord(v*1)) # Multiplicative identity
assert points_close(c.convert_to_absolute_coord(v), c.convert_to_absolute_coord(v*1)) # Multiplicative identity
def test_point_indentities():
c = Canvas(5,5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", Point(.4, .4), Point(.9, .95))
for p in points:
assert points_close(c.convert_to_absolute_coord(p+Vector(0,0,"ax1")), c.convert_to_absolute_coord(p)+c.convert_to_absolute_length(Vector(0,0,"ax1")))
def test_point_meet_right():
c = Canvas(5,5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", Point(.4, .4), Point(.9, .95))
for p1 in points:
for p2 in points:
assert points_close(c.convert_to_absolute_coord(p1) >> c.convert_to_absolute_coord(p2), c.convert_to_absolute_coord(p1 >> p2))
def test_point_meet_left():
c = Canvas(5,5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", Point(.4, .4), Point(.9, .95))
for p1 in points:
for p2 in points:
assert points_close(c.convert_to_absolute_coord(p1) << c.convert_to_absolute_coord(p2), c.convert_to_absolute_coord(p1 << p2))
def test_rotation():
c = Canvas(5,5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", Point(.4, .4), Point(.9, .95))
p = Point(.5, .5, "figure")
for v in vectors:
assert points_close(c.convert_to_absolute_coord(p+v), c.convert_to_absolute_coord(p+(v @ 360)))
assert points_close(c.convert_to_absolute_coord(p+v), c.convert_to_absolute_coord(p+(v @ 720)))
assert points_close(c.convert_to_absolute_coord(p-v), c.convert_to_absolute_coord(p+(v @ 180)))
if not points_close(c.convert_to_absolute_coord(p+v), c.convert_to_absolute_coord(p)): # Non-zero
assert not points_close(c.convert_to_absolute_coord(p+v), c.convert_to_absolute_coord(p + (v @ 90)))
def test_point_mean():
c = Canvas(5,5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", Point(.4, .4), Point(.9, .95))
for p1 in points:
for p2 in points:
assert points_close(c.convert_to_absolute_coord(p1) | c.convert_to_absolute_coord(p2), c.convert_to_absolute_coord(p1 | p2))
def test_linear_vector_multiplication():
for l in lower_points:
c = Canvas(5, 5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", l, Point(.9, .95))
for v1 in vectors:
p1 = 2.3*c.convert_to_absolute_coord(v1)
p2 = c.convert_to_absolute_coord(2.3*v1)
assert points_close(p1, p2), f"Failed for {v1}"
def test_linear_vector_division():
for l in lower_points:
c = Canvas(5, 5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", l, Point(.9, .95))
for v1 in vectors:
p1 = c.convert_to_absolute_coord(v1)/2.3
p2 = c.convert_to_absolute_coord(v1/2.3)
assert points_close(p1, p2), f"Failed for {v1}"
def test_associativity():
c = Canvas(5, 5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", lower_points[0], Point(.9, .95))
for v1 in vectors:
for v2 in vectors:
for p0 in [Point(.2, .2, "ax1"), Point(0, 2, "newunit")]:
p1 = c.convert_to_absolute_coord(p0 + (v1+v2))
p2 = c.convert_to_absolute_coord((p0 + v1) + v2)
assert points_close(p1, p2), f"Failed for {p1} and {p2}"
p1 = c.convert_to_absolute_coord(v1 + (p0+v2))
p2 = c.convert_to_absolute_coord((v1 + p0) + v2)
assert points_close(p1, p2), f"Failed for {p1} and {p2}"
def test_linear_vector_addition():
for l in lower_points:
c = Canvas(5, 5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", l, Point(.9, .95))
for v1 in vectors:
for v2 in vectors:
p1 = c.convert_to_absolute_coord(v1) + c.convert_to_absolute_coord(v2)
p2 = c.convert_to_absolute_coord(v1+v2)
assert points_close(p1, p2), f"Failed for {p1} and {p2}"
def test_commutative_vector_addition():
for l in lower_points:
c = Canvas(5, 5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", l, Point(.9, .95))
for v1 in vectors:
for v2 in vectors:
p1 = c.convert_to_absolute_coord(v1 + v2)
p2 = c.convert_to_absolute_coord(v2 + v1)
assert points_close(p1, p2), f"Failed for {p1} and {p2}"
def test_commutative_vector_multiplication():
for l in lower_points:
c = Canvas(5, 5)
c.add_unit("newunit", Width(.5, "figure") + Height(.6, "figure"), Point(.3, .3))
c.add_axis("ax1", l, Point(.9, .95))
for v1 in vectors:
p1 = c.convert_to_absolute_coord(v1*3)
p2 = c.convert_to_absolute_coord(3*v1)
assert points_close(p1, p2), f"Failed for {p1} and {p2}"
def test_example_canvas():
c = Canvas(4, 4, fontsize=9)
c.add_axis("ax1", Point(.1, .1, "figure"), Point(.5, .5, "figure"))
c.add_axis("ax2", Point(2.3, 2.3, "absolute"), Point(-.1, -.1, "-absolute"))
c.ax("ax1").plot(np.linspace(0, 4, 100), np.sin(np.linspace(0, 4, 100)))
c.ax("ax2").plot(np.linspace(0, 4, 5), np.linspace(0, 4, 5))
# TODO