-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helper_functions.py
331 lines (295 loc) · 11 KB
/
test_helper_functions.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Unit testing
"""
@author: Tobias Van Damme
"""
import itertools
import math
import unittest
import json
from pathlib import Path
from typing import Callable
import numpy as np
import helper_functions
from helper_functions import Coordinate
TEST_FOLDER = Path(__file__).parent
class TestHelperFunctions(unittest.TestCase):
"""Test class to test functions in helper_functions"""
def setUp(self):
"""Setup the tests"""
pass
def tearDown(self):
"""Clean up"""
pass
def test_digits_to_int(self):
"""Test helper_functions.digits_to_int"""
string_num = "12345"
test_grid = ["30373", "25512", "65332", "33549", "35390"]
individual_digits = [False, True]
# Test if single string is processed successfully
expected_result = [12345, [1, 2, 3, 4, 5]]
for individual_digit, result in zip(individual_digits, expected_result):
assert (
helper_functions.digits_to_int(string_num, individual_digit) == result
)
assert helper_functions.digits_to_int(string_num, True, return_type=tuple) == (
1,
2,
3,
4,
5,
)
assert helper_functions.digits_to_int(string_num, True, return_type=tuple) != [
1,
2,
3,
4,
5,
]
# Test if grid is processed successfully
expected_result = [
[30373, 25512, 65332, 33549, 35390],
[
[3, 0, 3, 7, 3],
[2, 5, 5, 1, 2],
[6, 5, 3, 3, 2],
[3, 3, 5, 4, 9],
[3, 5, 3, 9, 0],
],
]
for individual_digit, result in zip(individual_digits, expected_result):
assert (
actual_result := helper_functions.digits_to_int(
test_grid, individual_digit
)
) == result, (
f"Grid strings not processed correctly where {individual_digit = }.\n"
f"Expected result: {result}\n"
f"Actual result: {actual_result}"
)
expected_result_tuple = [
(30373, 25512, 65332, 33549, 35390),
(
(3, 0, 3, 7, 3),
(2, 5, 5, 1, 2),
(6, 5, 3, 3, 2),
(3, 3, 5, 4, 9),
(3, 5, 3, 9, 0),
),
]
for individual_digit, result in zip(individual_digits, expected_result_tuple):
assert (
actual_result := helper_functions.digits_to_int(
test_grid, individual_digit, return_type=tuple
)
) == result, (
f"Grid strings not processed correctly where {individual_digit = }.\n"
f"Expected result: {result}\n"
f"Actual result: {actual_result}"
)
def test_pad_numpy_array(self):
"""Test helper_functions.pad_numpy_array"""
test_grid = np.array([[1, 2], [3, 4]])
padded_grid = np.array(
[[-1, -1, -1, -1], [-1, 1, 2, -1], [-1, 3, 4, -1], [-1, -1, -1, -1]]
)
# Default settings
np.testing.assert_array_equal(
helper_functions.pad_numpy_array(test_grid, -1), padded_grid
)
# pad_width as int
np.testing.assert_array_equal(
helper_functions.pad_numpy_array(test_grid, -1, pad_width=1), padded_grid
)
# Padded with 1 line before and 2 lines after for each axis
unequal_padded_grid = np.array(
[
[-1, -1, -1, -1, -1],
[-1, 1, 2, -1, -1],
[-1, 3, 4, -1, -1],
[-1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1],
]
)
np.testing.assert_array_equal(
helper_functions.pad_numpy_array(test_grid, -1, pad_width=(1, 2)),
unequal_padded_grid,
)
# Padded a different number of lines for each axis
specific_padded_grid = np.array(
[
[-1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, 1, 2, -1, -1, -1, -1],
[-1, -1, -1, 3, 4, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1],
]
)
np.testing.assert_array_equal(
helper_functions.pad_numpy_array(test_grid, -1, pad_width=((1, 2), (3, 4))),
specific_padded_grid,
)
def test_coordinate(self):
"""Test coordinate class"""
coordinate1 = Coordinate(1, 2)
coordinate2 = Coordinate(3, 4)
coordinate3 = Coordinate(-2, 49)
assert coordinate1 + coordinate2 == (4, 6)
assert coordinate1 + coordinate3 == (-1, 51)
assert coordinate1.distance(coordinate2) == math.sqrt(8)
assert not coordinate1.is_touching(coordinate2)
distances = [(0, 1), (0, 0), (1, 1), (-1, 1), (1, 0), (-1, 0)]
not_diagonal = [True, True, False, False, True, True]
for distance, touching_diagonally in zip(distances, not_diagonal):
assert coordinate1.is_touching(coordinate1 + distance)
assert (
coordinate1.is_touching(coordinate1 + distance, diagonal=False)
== touching_diagonally
), (
f"Testing touching cardinal directions only, "
f"expected: {touching_diagonally}, got "
f"{coordinate1.is_touching(coordinate1 + distance, diagonal=False)} "
f"for coordinate at {coordinate1} and distance {distance}"
)
assert not coordinate1.is_touching(coordinate1, overlap=False)
assert coordinate1.is_touching(coordinate1, overlap=True)
assert coordinate2 > coordinate1
assert coordinate1 < coordinate2
assert coordinate2 >= coordinate1 + (0, 2)
assert coordinate1 <= coordinate2 - (2, 0)
# Test creation of origin
assert Coordinate.create_origin() == Coordinate(0, 0)
assert Coordinate.create_origin(3) == Coordinate(0, 0, 0)
# Test manhattan distance
assert coordinate1.manhattan_distance(coordinate2) == 4
def test_get_sign(self):
"""Test helper_functions.get_sign"""
assert helper_functions.get_sign(-5) == -1
assert helper_functions.get_sign(0) == 0
assert helper_functions.get_sign(0, sign_zero=1) == 1
assert helper_functions.get_sign(2.5465) == 1
def test_line_segment(self):
"""Test helper_functions.LineSegment"""
line1 = helper_functions.LineSegment(Coordinate(2, 10), Coordinate(18, 10))
line2 = helper_functions.LineSegment(Coordinate(12, 10), Coordinate(12, 10))
print(line1.merge(line2))
def test_manual(self):
"""Some manual testing"""
print(f"{type(helper_functions.Direction.LEFT.value)}")
def test_full_space(self):
"""Test helper_functions.full_space"""
# 3D space
space_limits = (Coordinate(0, 0, 0), Coordinate(2, 2, 2))
expected_coordinates = [
(0, 0, 0),
(0, 0, 1),
(0, 0, 2),
(0, 1, 0),
(0, 1, 1),
(0, 1, 2),
(0, 2, 0),
(0, 2, 1),
(0, 2, 2),
(1, 0, 0),
(1, 0, 1),
(1, 0, 2),
(1, 1, 0),
(1, 1, 1),
(1, 1, 2),
(1, 2, 0),
(1, 2, 1),
(1, 2, 2),
(2, 0, 0),
(2, 0, 1),
(2, 0, 2),
(2, 1, 0),
(2, 1, 1),
(2, 1, 2),
(2, 2, 0),
(2, 2, 1),
(2, 2, 2),
]
filled_coordinates = helper_functions.full_space(*space_limits)
assert len(filled_coordinates) == len(expected_coordinates), (
f"Number of filled coordinates: {len(filled_coordinates)}, "
f"number of expected coordinates: {len(expected_coordinates)}"
)
for coordinate in expected_coordinates:
assert coordinate in filled_coordinates
# 2d space
space_limits = (Coordinate(0, 0), Coordinate(2, 3))
expected_coordinates = [
(0, 0),
(0, 1),
(0, 2),
(0, 3),
(1, 0),
(1, 1),
(1, 2),
(1, 3),
(2, 0),
(2, 1),
(2, 2),
(2, 3),
]
filled_coordinates = helper_functions.full_space(*space_limits)
assert len(filled_coordinates) == len(expected_coordinates), (
f"Number of filled coordinates: {len(filled_coordinates)}, "
f"number of expected coordinates: {len(expected_coordinates)}"
)
for coordinate in expected_coordinates:
assert coordinate in filled_coordinates
def test_flood_fill(self):
"""Test helper_functions.flood_fill"""
def check_condition(
space_limits: tuple[Coordinate, Coordinate],
is_valid_coordinate: Callable,
expected_coordinates: list[Coordinate],
) -> None:
"""Do test in the given space with the given valid coordinate checker"""
filled_coordinate = helper_functions.flood_fill(
space_limits[0], is_valid_coordinate
)
assert len(filled_coordinate) == len(expected_coordinates), (
f"Number of filled coordinates: {len(filled_coordinate)}, "
f"number of expected coordinates: {len(expected_coordinates)}"
)
for coordinate in expected_coordinates:
assert coordinate in filled_coordinate
# 3D full space
def accept_full_space(coordinate: Coordinate) -> bool:
return space_limits[0] <= coordinate <= space_limits[1]
space_limits = Coordinate(0, 0, 0), Coordinate(2, 2, 2)
check_condition(
space_limits=space_limits,
is_valid_coordinate=accept_full_space,
expected_coordinates=helper_functions.full_space(*space_limits),
)
# 2D space with wall in middle
def wall_in_middle(coordinate: Coordinate) -> bool:
"""Wall in the second row"""
return (space_limits[0] <= coordinate <= space_limits[1]) and (
coordinate[0] < 2
)
space_limits = (Coordinate(0, 0), Coordinate(3, 4))
expected_coordinates = [
Coordinate(coordinate)
for coordinate in [
(0, 0),
(0, 1),
(0, 2),
(0, 3),
(0, 4),
(1, 0),
(1, 1),
(1, 2),
(1, 3),
(1, 4),
]
]
check_condition(
space_limits=space_limits,
is_valid_coordinate=wall_in_middle,
expected_coordinates=expected_coordinates,
)
if __name__ == "__main__":
unittest.main(module="test_helper_functions")