Add cell-scale-factor to jsonschema
[oam.git] / code / network-generator / tests / test_hexagon.py
1 # Copyright 2023 highstreet technologies GmbH
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from network_generation.model.python.hexagon import (
16     DoubledCoord,
17     Hex,
18     Layout,
19     OffsetCoord,
20 )
21 import network_generation.model.python.hexagon as Hexagon
22 from network_generation.model.python.point import Point
23
24 # Tests
25
26
27 def complain(name: str) -> None:
28     print("FAIL {0}".format(name))
29
30
31 def equal_hex(name: str, a: Hex, b: Hex) -> None:
32     if not (a.q == b.q and a.s == b.s and a.r == b.r):
33         complain(name)
34
35
36 def equal_offsetcoord(name: str, a: OffsetCoord, b: OffsetCoord) -> None:
37     if not (a.col == b.col and a.row == b.row):
38         complain(name)
39
40
41 def equal_doubledcoord(name: str, a: DoubledCoord, b: DoubledCoord) -> None:
42     if not (a.col == b.col and a.row == b.row):
43         complain(name)
44
45
46 def equal_int(name: str, a: int, b: int) -> None:
47     if not (a == b):
48         complain(name)
49
50
51 def equal_hex_array(name: str, a: list[Hex], b: list[Hex]) -> None:
52     equal_int(name, len(a), len(b))
53     for i in range(0, len(a)):
54         equal_hex(name, a[i], b[i])
55
56
57 def test_hex_arithmetic() -> None:
58     equal_hex(
59         "hex_add",
60         Hex(4, -10, 6),
61         Hexagon.hex_add(Hex(1, -3, 2), Hex(3, -7, 4)),
62     )
63     equal_hex(
64         "hex_subtract",
65         Hex(-2, 4, -2),
66         Hexagon.hex_subtract(Hex(1, -3, 2), Hex(3, -7, 4)),
67     )
68
69
70 def test_hex_direction() -> None:
71     equal_hex("hex_direction", Hex(0, -1, 1), Hexagon.hex_direction(2))
72
73
74 def test_hex_neighbor() -> None:
75     equal_hex(
76         "hex_neighbor", Hex(1, -3, 2), Hexagon.hex_neighbor(Hex(1, -2, 1), 2)
77     )
78
79
80 def test_hex_diagonal() -> None:
81     equal_hex(
82         "hex_diagonal",
83         Hex(-1, -1, 2),
84         Hexagon.hex_diagonal_neighbor(Hex(1, -2, 1), 3),
85     )
86
87
88 def test_hex_distance() -> None:
89     equal_int(
90         "hex_distance",
91         7,
92         int(Hexagon.hex_distance(Hex(3, -7, 4), Hex(0, 0, 0))),
93     )
94
95
96 def test_hex_rotate_right() -> None:
97     equal_hex(
98         "hex_rotate_right",
99         Hexagon.hex_rotate_right(Hex(1, -3, 2)),
100         Hex(3, -2, -1),
101     )
102
103
104 def test_hex_rotate_left() -> None:
105     equal_hex(
106         "hex_rotate_left",
107         Hexagon.hex_rotate_left(Hex(1, -3, 2)),
108         Hex(-2, -1, 3),
109     )
110
111
112 def test_hex_round() -> None:
113     a = Hex(0, 0, 0)
114     b = Hex(1, -1, 0)
115     c = Hex(0, -1, 1)
116     equal_hex(
117         "hex_round 1",
118         Hex(5, -10, 5),
119         Hexagon.hex_round(
120             Hexagon.hex_lerp(Hex(0, 0, 0), Hex(10, -20, 10), 0.5)
121         ),
122     )
123     equal_hex(
124         "hex_round 2",
125         Hexagon.hex_round(a),
126         Hexagon.hex_round(Hexagon.hex_lerp(a, b, 0.499)),
127     )
128     equal_hex(
129         "hex_round 3",
130         Hexagon.hex_round(b),
131         Hexagon.hex_round(Hexagon.hex_lerp(a, b, 0.501)),
132     )
133     equal_hex(
134         "hex_round 4",
135         Hexagon.hex_round(a),
136         Hexagon.hex_round(
137             Hex(
138                 a.q * 0.4 + b.q * 0.3 + c.q * 0.3,
139                 a.r * 0.4 + b.r * 0.3 + c.r * 0.3,
140                 a.s * 0.4 + b.s * 0.3 + c.s * 0.3,
141             )
142         ),
143     )
144     equal_hex(
145         "hex_round 5",
146         Hexagon.hex_round(c),
147         Hexagon.hex_round(
148             Hex(
149                 a.q * 0.3 + b.q * 0.3 + c.q * 0.4,
150                 a.r * 0.3 + b.r * 0.3 + c.r * 0.4,
151                 a.s * 0.3 + b.s * 0.3 + c.s * 0.4,
152             )
153         ),
154     )
155
156
157 def test_hex_linedraw() -> None:
158     equal_hex_array(
159         "hex_linedraw",
160         [
161             Hex(0, 0, 0),
162             Hex(0, -1, 1),
163             Hex(0, -2, 2),
164             Hex(1, -3, 2),
165             Hex(1, -4, 3),
166             Hex(1, -5, 4),
167         ],
168         Hexagon.hex_linedraw(Hex(0, 0, 0), Hex(1, -5, 4)),
169     )
170
171
172 def test_layout() -> None:
173     h = Hex(3, 4, -7)
174     flat = Layout(Hexagon.layout_flat, Point(10.0, 15.0), Point(35.0, 71.0))
175     equal_hex(
176         "layout",
177         h,
178         Hexagon.hex_round(
179             Hexagon.pixel_to_hex(flat, Hexagon.hex_to_pixel(flat, h))
180         ),
181     )
182     pointy = Layout(
183         Hexagon.layout_pointy, Point(10.0, 15.0), Point(35.0, 71.0)
184     )
185     equal_hex(
186         "layout",
187         h,
188         Hexagon.hex_round(
189             Hexagon.pixel_to_hex(pointy, Hexagon.hex_to_pixel(pointy, h))
190         ),
191     )
192
193
194 def test_offset_roundtrip() -> None:
195     a = Hex(3, 4, -7)
196     b = OffsetCoord(1, -3)
197     equal_hex(
198         "conversion_roundtrip even-q",
199         a,
200         Hexagon.qoffset_to_cube(
201             Hexagon.EVEN,
202             Hexagon.qoffset_from_cube(Hexagon.EVEN, a),
203         ),
204     )
205     equal_offsetcoord(
206         "conversion_roundtrip even-q",
207         b,
208         Hexagon.qoffset_from_cube(
209             Hexagon.EVEN,
210             Hexagon.qoffset_to_cube(Hexagon.EVEN, b),
211         ),
212     )
213     equal_hex(
214         "conversion_roundtrip odd-q",
215         a,
216         Hexagon.qoffset_to_cube(
217             Hexagon.ODD,
218             Hexagon.qoffset_from_cube(Hexagon.ODD, a),
219         ),
220     )
221     equal_offsetcoord(
222         "conversion_roundtrip odd-q",
223         b,
224         Hexagon.qoffset_from_cube(
225             Hexagon.ODD, Hexagon.qoffset_to_cube(Hexagon.ODD, b)
226         ),
227     )
228     equal_hex(
229         "conversion_roundtrip even-r",
230         a,
231         Hexagon.roffset_to_cube(
232             Hexagon.EVEN, Hexagon.roffset_from_cube(Hexagon.EVEN, a)
233         ),
234     )
235     equal_offsetcoord(
236         "conversion_roundtrip even-r",
237         b,
238         Hexagon.roffset_from_cube(
239             Hexagon.EVEN, Hexagon.roffset_to_cube(Hexagon.EVEN, b)
240         ),
241     )
242     equal_hex(
243         "conversion_roundtrip odd-r",
244         a,
245         Hexagon.roffset_to_cube(
246             Hexagon.ODD, Hexagon.roffset_from_cube(Hexagon.ODD, a)
247         ),
248     )
249     equal_offsetcoord(
250         "conversion_roundtrip odd-r",
251         b,
252         Hexagon.roffset_from_cube(
253             Hexagon.ODD, Hexagon.roffset_to_cube(Hexagon.ODD, b)
254         ),
255     )
256
257
258 def test_offset_from_cube() -> None:
259     equal_offsetcoord(
260         "offset_from_cube even-q",
261         OffsetCoord(1, 3),
262         Hexagon.qoffset_from_cube(Hexagon.EVEN, Hex(1, 2, -3)),
263     )
264     equal_offsetcoord(
265         "offset_from_cube odd-q",
266         OffsetCoord(1, 2),
267         Hexagon.qoffset_from_cube(Hexagon.ODD, Hex(1, 2, -3)),
268     )
269
270
271 def test_offset_to_cube() -> None:
272     equal_hex(
273         "offset_to_cube even-",
274         Hex(1, 2, -3),
275         Hexagon.qoffset_to_cube(Hexagon.EVEN, OffsetCoord(1, 3)),
276     )
277     equal_hex(
278         "offset_to_cube odd-q",
279         Hex(1, 2, -3),
280         Hexagon.qoffset_to_cube(Hexagon.ODD, OffsetCoord(1, 2)),
281     )
282
283
284 def test_doubled_roundtrip() -> None:
285     a = Hex(3, 4, -7)
286     b = DoubledCoord(1, -3)
287     equal_hex(
288         "conversion_roundtrip doubled-q",
289         a,
290         Hexagon.qdoubled_to_cube(Hexagon.qdoubled_from_cube(a)),
291     )
292     equal_doubledcoord(
293         "conversion_roundtrip doubled-q",
294         b,
295         Hexagon.qdoubled_from_cube(Hexagon.qdoubled_to_cube(b)),
296     )
297     equal_hex(
298         "conversion_roundtrip doubled-r",
299         a,
300         Hexagon.rdoubled_to_cube(Hexagon.rdoubled_from_cube(a)),
301     )
302     equal_doubledcoord(
303         "conversion_roundtrip doubled-r",
304         b,
305         Hexagon.rdoubled_from_cube(Hexagon.rdoubled_to_cube(b)),
306     )
307
308
309 def test_doubled_from_cube() -> None:
310     equal_doubledcoord(
311         "doubled_from_cube doubled-q",
312         DoubledCoord(1, 5),
313         Hexagon.qdoubled_from_cube(Hex(1, 2, -3)),
314     )
315     equal_doubledcoord(
316         "doubled_from_cube doubled-r",
317         DoubledCoord(4, 2),
318         Hexagon.rdoubled_from_cube(Hex(1, 2, -3)),
319     )
320
321
322 def test_doubled_to_cube() -> None:
323     equal_hex(
324         "doubled_to_cube doubled-q",
325         Hex(1, 2, -3),
326         Hexagon.qdoubled_to_cube(DoubledCoord(1, 5)),
327     )
328     equal_hex(
329         "doubled_to_cube doubled-r",
330         Hex(1, 2, -3),
331         Hexagon.rdoubled_to_cube(DoubledCoord(4, 2)),
332     )
333
334
335 def test_hexagon() -> None:
336     test_hex_arithmetic()
337     test_hex_direction()
338     test_hex_neighbor()
339     test_hex_diagonal()
340     test_hex_distance()
341     test_hex_rotate_right()
342     test_hex_rotate_left()
343     test_hex_round()
344     test_hex_linedraw()
345     test_layout()
346     test_offset_roundtrip()
347     test_offset_from_cube()
348     test_offset_to_cube()
349     test_doubled_roundtrip()
350     test_doubled_from_cube()
351     test_doubled_to_cube()