Debian Codegraph @

pencil/test/unit/geometry_connector_test.c

raw
1 /* File: geometry_connector_test.c; Copyright and License: see below */
2
3 #include "geometry_connector_test.h"
4 #include "geometry/geometry_connector.h"
5 #include "geometry/geometry_connector_segment.h"
6 #include "geometry/geometry_rectangle.h"
7 #include "test_fixture.h"
8 #include "test_expect.h"
9 #include "test_environment_assert.h"
10 #include "test_case_result.h"
11
12 static test_fixture_t * set_up();
13 static void tear_down( test_fixture_t *fix );
14 static test_case_result_t test_base_methods( test_fixture_t *fix );
15 static test_case_result_t test_bounding_rectangle( test_fixture_t *fix );
16 static test_case_result_t test_segment_bounds( test_fixture_t *fix );
17 static test_case_result_t test_intersecting_rectangle_simple( test_fixture_t *fix );
18 static test_case_result_t test_intersecting_rectangle_corner( test_fixture_t *fix );
19 static test_case_result_t test_intersecting_rectangle_length( test_fixture_t *fix );
20 static test_case_result_t test_connector_intersects( test_fixture_t *fix );
21 static test_case_result_t test_get_same_path_length_rect( test_fixture_t *fix );
22 static test_case_result_t test_get_same_path_length_conn( test_fixture_t *fix );
23 static test_case_result_t test_calc_waypoint_good( test_fixture_t *fix );
24 static test_case_result_t test_calc_waypoint_zero( test_fixture_t *fix );
25
26 test_suite_t geometry_connector_test_get_suite(void)
27 {
28 test_suite_t result;
29 test_suite_init( &result,
30 "geometry_connector_test_get_suite",
31 TEST_CATEGORY_UNIT | TEST_CATEGORY_CONTINUOUS | TEST_CATEGORY_COVERAGE,
32 &set_up,
33 &tear_down
34 );
35 test_suite_add_test_case( &result, "test_base_methods", &test_base_methods );
36 test_suite_add_test_case( &result, "test_bounding_rectangle", &test_bounding_rectangle );
37 test_suite_add_test_case( &result, "test_segment_bounds", &test_segment_bounds );
38 test_suite_add_test_case( &result, "test_intersecting_rectangle_simple", &test_intersecting_rectangle_simple );
39 test_suite_add_test_case( &result, "test_intersecting_rectangle_corner", &test_intersecting_rectangle_corner );
40 test_suite_add_test_case( &result, "test_intersecting_rectangle_length", &test_intersecting_rectangle_length );
41 test_suite_add_test_case( &result, "test_connector_intersects", &test_connector_intersects );
42 test_suite_add_test_case( &result, "test_get_same_path_length_rect", &test_get_same_path_length_rect );
43 test_suite_add_test_case( &result, "test_get_same_path_length_conn", &test_get_same_path_length_conn );
44 test_suite_add_test_case( &result, "test_calc_waypoint_good", &test_calc_waypoint_good );
45 test_suite_add_test_case( &result, "test_calc_waypoint_zero", &test_calc_waypoint_zero );
46 return result;
47 }
48
49 static test_fixture_t * set_up()
50 {
51 return NULL;
52 }
53
54 static void tear_down( test_fixture_t *fix )
55 {
56 }
57
58 static test_case_result_t test_base_methods( test_fixture_t *fix )
59 {
60 geometry_connector_t my_connector;
61 geometry_connector_init_empty( &my_connector );
62
63 /* init 3 segments line */
64 geometry_connector_reinit_vertical( &my_connector,
65 10.0 /*source_end_x*/,
66 10.0 /*source_end_y*/,
67 30.0 /*destination_end_x*/,
68 30.0 /*destination_end_y*/,
69 20.0 /*main_line_x*/
70 );
71 TEST_EXPECT_EQUAL_DOUBLE( 40.0, geometry_connector_get_length( &my_connector ) );
72 TEST_EXPECT_EQUAL_INT( true, geometry_connector_is_close( &my_connector, 21.0, 9.0, 1.5 /* max_distance */ ) );
73 TEST_EXPECT_EQUAL_INT( false, geometry_connector_is_close( &my_connector, 21.0, 9.0, 0.5 /* max_distance */ ) );
74
75 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_connector_get_source_end_x( &my_connector ) );
76 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_connector_get_source_end_y( &my_connector ) );
77 TEST_EXPECT_EQUAL_DOUBLE( 20.0, geometry_connector_get_main_line_source_x( &my_connector ) );
78 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_connector_get_main_line_source_y( &my_connector ) );
79 TEST_EXPECT_EQUAL_DOUBLE( 20.0, geometry_connector_get_main_line_destination_x( &my_connector ) );
80 TEST_EXPECT_EQUAL_DOUBLE( 30.0, geometry_connector_get_main_line_destination_y( &my_connector ) );
81 TEST_EXPECT_EQUAL_DOUBLE( 30.0, geometry_connector_get_destination_end_x( &my_connector ) );
82 TEST_EXPECT_EQUAL_DOUBLE( 30.0, geometry_connector_get_destination_end_y( &my_connector ) );
83
84 /* init 1 segment line */
85 geometry_connector_reinit_vertical( &my_connector,
86 10.0 /*source_end_x*/,
87 10.0 /*source_end_y*/,
88 10.0 /*destination_end_x*/,
89 30.0 /*destination_end_y*/,
90 10.0 /*main_line_x*/
91 );
92 TEST_EXPECT_EQUAL_DOUBLE( 20.0, geometry_connector_get_length( &my_connector ) );
93 TEST_EXPECT_EQUAL_INT( true, geometry_connector_is_close( &my_connector, 9.0, 20.0, 1.5 /* max_distance */ ) );
94 TEST_EXPECT_EQUAL_INT( false, geometry_connector_is_close( &my_connector, 10.0, 8.0, 1.5 /* max_distance */ ) );
95
96 /* init 3 segments horizontal line */
97 geometry_connector_reinit_horizontal( &my_connector,
98 30.0 /*source_end_x*/,
99 30.0 /*source_end_y*/,
100 10.0 /*destination_end_x*/,
101 10.0 /*destination_end_y*/,
102 20.0 /*main_line_y*/
103 );
104 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_connector_get_source_length( &my_connector ) );
105 TEST_EXPECT_EQUAL_DOUBLE( 20.0, geometry_connector_get_main_length( &my_connector ) );
106 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_connector_get_destination_length( &my_connector ) );
107 TEST_EXPECT_EQUAL_INT( true, geometry_connector_is_close( &my_connector, 11.0, 9.0, 1.5 /* max_distance */ ) );
108 TEST_EXPECT_EQUAL_INT( false, geometry_connector_is_close( &my_connector, 11.0, 9.0, 0.5 /* max_distance */ ) );
109
110 /* check that the trace function returns */
111 geometry_connector_trace( &my_connector );
112
113 geometry_connector_destroy ( &my_connector );
114 return TEST_CASE_RESULT_OK;
115 }
116
117 static test_case_result_t test_bounding_rectangle( test_fixture_t *fix )
118 {
119 geometry_connector_t my_connector;
120 geometry_rectangle_t bounds;
121
122 /* init 3 segments line, Z-Form */
123 geometry_connector_init_vertical ( &my_connector,
124 10.0 /*source_end_x*/,
125 10.0 /*source_end_y*/,
126 30.0 /*destination_end_x*/,
127 30.0 /*destination_end_y*/,
128 20.0 /*main_line_x*/
129 );
130 bounds = geometry_connector_get_bounding_rectangle ( &my_connector );
131 TEST_EXPECT_EQUAL_DOUBLE( 400.0, geometry_rectangle_get_area( &bounds ) );
132 geometry_rectangle_destroy ( &bounds );
133
134 /* init 3 segments line, U-Form */
135 geometry_connector_reinit_horizontal ( &my_connector,
136 10.0 /*source_end_x*/,
137 30.0 /*source_end_y*/,
138 30.0 /*destination_end_x*/,
139 30.0 /*destination_end_y*/,
140 10.0 /*main_line_y*/
141 );
142 bounds = geometry_connector_get_bounding_rectangle ( &my_connector );
143 TEST_EXPECT_EQUAL_DOUBLE( 400.0, geometry_rectangle_get_area( &bounds ) );
144 geometry_rectangle_destroy ( &bounds );
145
146 /* init 1 segment line */
147 geometry_connector_reinit_vertical ( &my_connector,
148 10.0 /*source_end_x*/,
149 10.0 /*source_end_y*/,
150 10.0 /*destination_end_x*/,
151 30.0 /*destination_end_y*/,
152 10.0 /*main_line_x*/
153 );
154 bounds = geometry_connector_get_bounding_rectangle ( &my_connector );
155 TEST_EXPECT_EQUAL_DOUBLE( 0.0, geometry_rectangle_get_area( &bounds ) );
156 geometry_rectangle_destroy ( &bounds );
157
158 geometry_connector_destroy ( &my_connector );
159 return TEST_CASE_RESULT_OK;
160 }
161
162 static test_case_result_t test_segment_bounds( test_fixture_t *fix )
163 {
164 geometry_connector_t my_connector;
165 geometry_rectangle_t bounds;
166
167 /* init 3 segments line, L-Form */
168 geometry_connector_init_vertical ( &my_connector,
169 10.0 /*source_end_x*/,
170 10.0 /*source_end_y*/,
171 30.0 /*destination_end_x*/,
172 30.0 /*destination_end_y*/,
173 10.0 /*main_line_x*/
174 );
175 bounds = geometry_connector_get_segment_bounds ( &my_connector, GEOMETRY_CONNECTOR_SEGMENT_SOURCE );
176 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_rectangle_get_left( &bounds ) );
177 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_rectangle_get_top( &bounds ) );
178 TEST_EXPECT_EQUAL_DOUBLE( 0.0, geometry_rectangle_get_width( &bounds ) );
179 TEST_EXPECT_EQUAL_DOUBLE( 0.0, geometry_rectangle_get_height( &bounds ) );
180 geometry_rectangle_destroy ( &bounds );
181
182 bounds = geometry_connector_get_segment_bounds ( &my_connector, GEOMETRY_CONNECTOR_SEGMENT_MAIN );
183 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_rectangle_get_left( &bounds ) );
184 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_rectangle_get_top( &bounds ) );
185 TEST_EXPECT_EQUAL_DOUBLE( 0.0, geometry_rectangle_get_width( &bounds ) );
186 TEST_EXPECT_EQUAL_DOUBLE( 20.0, geometry_rectangle_get_height( &bounds ) );
187 geometry_rectangle_destroy ( &bounds );
188
189 bounds = geometry_connector_get_segment_bounds ( &my_connector, GEOMETRY_CONNECTOR_SEGMENT_DESTINATION );
190 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_rectangle_get_left( &bounds ) );
191 TEST_EXPECT_EQUAL_DOUBLE( 30.0, geometry_rectangle_get_top( &bounds ) );
192 TEST_EXPECT_EQUAL_DOUBLE( 20.0, geometry_rectangle_get_width( &bounds ) );
193 TEST_EXPECT_EQUAL_DOUBLE( 0.0, geometry_rectangle_get_height( &bounds ) );
194 geometry_rectangle_destroy ( &bounds );
195
196 geometry_connector_destroy ( &my_connector );
197 return TEST_CASE_RESULT_OK;
198 }
199
200 static test_case_result_t test_intersecting_rectangle_simple( test_fixture_t *fix )
201 {
202 geometry_connector_t my_connector;
203 geometry_rectangle_t overlap;
204 bool intersects;
205
206 /* init 3 segments line, Z-Form */
207 geometry_connector_init_vertical ( &my_connector,
208 10.0 /*source_end_x*/,
209 10.0 /*source_end_y*/,
210 30.0 /*destination_end_x*/,
211 30.0 /*destination_end_y*/,
212 20.0 /*main_line_x*/
213 );
214 geometry_rectangle_init( &overlap, 19.0, 19.0, 5.0 /*W*/, 5.0 /*H*/ );
215 intersects = geometry_connector_is_intersecting_rectangle( &my_connector, &overlap );
216 TEST_EXPECT_EQUAL_INT( true, intersects );
217
218 geometry_rectangle_destroy ( &overlap );
219
220 /* init 3 segments line, U-Form */
221 geometry_connector_reinit_horizontal ( &my_connector,
222 10.0 /*source_end_x*/,
223 30.0 /*source_end_y*/,
224 30.0 /*destination_end_x*/,
225 30.0 /*destination_end_y*/,
226 10.0 /*main_line_y*/
227 );
228 geometry_rectangle_init( &overlap, 11.0, 11.0, 18.0 /*W*/, 25.0 /*H*/ );
229 intersects = geometry_connector_is_intersecting_rectangle( &my_connector, &overlap );
230 TEST_EXPECT_EQUAL_INT( false, intersects );
231
232 geometry_rectangle_destroy ( &overlap );
233
234 /* init 1 segment line */
235 geometry_connector_reinit_vertical ( &my_connector,
236 10.0 /*source_end_x*/,
237 10.0 /*source_end_y*/,
238 10.0 /*destination_end_x*/,
239 30.0 /*destination_end_y*/,
240 10.0 /*main_line_x*/
241 );
242 geometry_rectangle_init( &overlap, 9.0, 11.0, 10.0 /*W*/, 10.0 /*H*/ );
243 intersects = geometry_connector_is_intersecting_rectangle( &my_connector, &overlap );
244 TEST_EXPECT_EQUAL_INT( true, intersects );
245
246 geometry_rectangle_destroy ( &overlap );
247 geometry_connector_destroy ( &my_connector );
248 return TEST_CASE_RESULT_OK;
249 }
250
251 static test_case_result_t test_intersecting_rectangle_corner( test_fixture_t *fix )
252 {
253 geometry_connector_t my_connector;
254 geometry_rectangle_t touching;
255 bool intersects;
256
257 /* init 3 segments line, Z-Form */
258 geometry_connector_init_vertical ( &my_connector,
259 10.0 /*source_end_x*/,
260 10.0 /*source_end_y*/,
261 30.0 /*destination_end_x*/,
262 30.0 /*destination_end_y*/,
263 20.0 /*main_line_x*/
264 );
265
266 /* touch source end */
267 geometry_rectangle_init( &touching, 9.0, 9.0, 1.0 /*W*/, 2.0 /*H*/ );
268 intersects = geometry_connector_is_intersecting_rectangle( &my_connector, &touching );
269 TEST_EXPECT_EQUAL_INT( false, intersects );
270
271 /* touch source end by 0-width */
272 geometry_rectangle_init( &touching, 10.0, 9.0, 0.0 /*W*/, 2.0 /*H*/ );
273 intersects = geometry_connector_is_intersecting_rectangle( &my_connector, &touching );
274 TEST_EXPECT_EQUAL_INT( false, intersects );
275
276 /* touch destination end */
277 geometry_rectangle_reinit( &touching, 30.0, 29.0, 1.0 /*W*/, 2.0 /*H*/ );
278 intersects = geometry_connector_is_intersecting_rectangle( &my_connector, &touching );
279 TEST_EXPECT_EQUAL_INT( false, intersects );
280
281 /* touch main line */
282 geometry_rectangle_reinit( &touching, 20.0, 9.0, 1.0 /*W*/, 21.0 /*H*/ );
283 intersects = geometry_connector_is_intersecting_rectangle( &my_connector, &touching );
284 TEST_EXPECT_EQUAL_INT( false, intersects );
285
286 /* init 1 segment line */
287 geometry_connector_reinit_vertical ( &my_connector,
288 10.0 /*source_end_x*/,
289 10.0 /*source_end_y*/,
290 10.0 /*destination_end_x*/,
291 30.0 /*destination_end_y*/,
292 10.0 /*main_line_x*/
293 );
294
295 /* touch source end */
296 geometry_rectangle_reinit( &touching, 9.0, 9.0, 2.0 /*W*/, 1.0 /*H*/ );
297 intersects = geometry_connector_is_intersecting_rectangle( &my_connector, &touching );
298 TEST_EXPECT_EQUAL_INT( false, intersects );
299
300 /* touch destination end */
301 geometry_rectangle_reinit( &touching, 9.0, 30.0, 2.0 /*W*/, 1.0 /*H*/ );
302 intersects = geometry_connector_is_intersecting_rectangle( &my_connector, &touching );
303 TEST_EXPECT_EQUAL_INT( false, intersects );
304
305 /* touch destination end by 0-height */
306 geometry_rectangle_reinit( &touching, 9.0, 30.0, 2.0 /*W*/, 0.0 /*H*/ );
307 intersects = geometry_connector_is_intersecting_rectangle( &my_connector, &touching );
308 TEST_EXPECT_EQUAL_INT( false, intersects );
309
310 geometry_rectangle_destroy ( &touching );
311 geometry_connector_destroy ( &my_connector );
312 return TEST_CASE_RESULT_OK;
313 }
314
315 static test_case_result_t test_intersecting_rectangle_length( test_fixture_t *fix )
316 {
317 geometry_connector_t my_connector;
318 geometry_rectangle_t label_rect;
319
320 /* init 3 segments line, C-Form */
321 geometry_connector_init_vertical( &my_connector,
322 2.0 /*source_end_x*/,
323 1.0 /*source_end_y*/,
324 2.0 /*destination_end_x*/,
325 2.0 /*destination_end_y*/,
326 1.0 /*main_line_x*/
327 );
328
329 /* rect within C-connector */
330 geometry_rectangle_init( &label_rect, 1.1, 1.1, 0.8, 0.8 );
331 const double no_transit = geometry_connector_get_transit_length( &my_connector, &label_rect );
332 TEST_EXPECT_EQUAL_FLOAT( 0.0, no_transit );
333
334 /* rect touching C-connector */
335 geometry_rectangle_init( &label_rect, 1.0, 1.0, 10.0, 1.0 );
336 const double all_touch = geometry_connector_get_transit_length( &my_connector, &label_rect );
337 TEST_EXPECT_EQUAL_FLOAT( 3.0, all_touch );
338
339 /* rect around C-connector */
340 geometry_rectangle_init( &label_rect, 0.9, 0.9, 1.2, 1.2 );
341 const double all_in = geometry_connector_get_transit_length( &my_connector, &label_rect );
342 TEST_EXPECT_EQUAL_FLOAT( 3.0, all_in );
343
344 /* rect cut C-connector */
345 geometry_rectangle_init( &label_rect, 0.5, 1.5, 1.0, 1.0 );
346 const double part_transit = geometry_connector_get_transit_length( &my_connector, &label_rect );
347 TEST_EXPECT_EQUAL_FLOAT( 1.0, part_transit );
348
349 return TEST_CASE_RESULT_OK;
350 }
351
352 static test_case_result_t test_connector_intersects( test_fixture_t *fix )
353 {
354 geometry_connector_t my_connector_1;
355 geometry_connector_t my_connector_2;
356 uint32_t intersect_count;
357
358 /* double empty */
359 geometry_connector_init_empty( &my_connector_1 );
360 geometry_connector_init_empty( &my_connector_2 );
361
362 intersect_count = geometry_connector_count_connector_intersects ( &my_connector_1, &my_connector_2 );
363 TEST_EXPECT_EQUAL_INT( 9, intersect_count );
364
365 geometry_connector_destroy ( &my_connector_1 );
366 geometry_connector_destroy ( &my_connector_2 );
367
368 /* completely separated */
369 geometry_connector_init_vertical( &my_connector_1,
370 10.0 /*source_end_x*/,
371 10.0 /*source_end_y*/,
372 10.0 /*destination_end_x*/,
373 30.0 /*destination_end_y*/,
374 20.0 /*main_line_x*/
375 );
376 geometry_connector_init_vertical( &my_connector_2,
377 40.0 /*source_end_x*/,
378 10.0 /*source_end_y*/,
379 40.0 /*destination_end_x*/,
380 30.0 /*destination_end_y*/,
381 50.0 /*main_line_x*/
382 );
383
384 intersect_count = geometry_connector_count_connector_intersects ( &my_connector_1, &my_connector_2 );
385 TEST_EXPECT_EQUAL_INT( 0, intersect_count );
386
387 geometry_connector_destroy ( &my_connector_1 );
388 geometry_connector_destroy ( &my_connector_2 );
389
390 /* close but not overlapping */
391 geometry_connector_init_horizontal( &my_connector_1,
392 10.0 /*source_end_x*/,
393 30.0 /*source_end_y*/,
394 30.0 /*destination_end_x*/,
395 30.0 /*destination_end_y*/,
396 10.0 /*main_line_y*/
397 );
398 geometry_connector_init_horizontal( &my_connector_2,
399 15.0 /*source_end_x*/,
400 30.0 /*source_end_y*/,
401 25.0 /*destination_end_x*/,
402 30.0 /*destination_end_y*/,
403 11.0 /*main_line_y*/
404 );
405
406 intersect_count = geometry_connector_count_connector_intersects ( &my_connector_1, &my_connector_2 );
407 TEST_EXPECT_EQUAL_INT( 0, intersect_count );
408
409 geometry_connector_destroy ( &my_connector_1 );
410 geometry_connector_destroy ( &my_connector_2 );
411
412 /* 2x touching but not overlapping */
413 geometry_connector_init_horizontal( &my_connector_1,
414 10.0 /*source_end_x*/,
415 10.0 /*source_end_y*/,
416 30.0 /*destination_end_x*/,
417 10.0 /*destination_end_y*/,
418 20.0 /*main_line_y*/
419 );
420 geometry_connector_init_horizontal( &my_connector_2,
421 15.0 /*source_end_x*/,
422 20.0 /*source_end_y*/,
423 25.0 /*destination_end_x*/,
424 20.0 /*destination_end_y*/,
425 10.0 /*main_line_y*/
426 );
427
428 intersect_count = geometry_connector_count_connector_intersects ( &my_connector_1, &my_connector_2 );
429 TEST_EXPECT_EQUAL_INT( 2, intersect_count );
430
431 geometry_connector_destroy ( &my_connector_1 );
432 geometry_connector_destroy ( &my_connector_2 );
433
434 /* 2 segments identical, 1 empty */
435 geometry_connector_init_horizontal( &my_connector_1,
436 10.0 /*source_end_x*/,
437 10.0 /*source_end_y*/,
438 30.0 /*destination_end_x*/,
439 20.0 /*destination_end_y*/,
440 20.0 /*main_line_y*/
441 );
442 geometry_connector_replace ( &my_connector_2, &my_connector_1 );
443
444 intersect_count = geometry_connector_count_connector_intersects ( &my_connector_1, &my_connector_2 );
445 TEST_EXPECT_EQUAL_INT( 7, intersect_count );
446
447 geometry_connector_destroy ( &my_connector_1 );
448 geometry_connector_destroy ( &my_connector_2 );
449
450 /* three real intersects */
451 geometry_connector_init_horizontal( &my_connector_1,
452 10.0 /*source_end_x*/,
453 10.0 /*source_end_y*/,
454 30.0 /*destination_end_x*/,
455 12.0 /*destination_end_y*/,
456 20.0 /*main_line_y*/
457 );
458 geometry_connector_init_vertical( &my_connector_2,
459 5.0 /*source_end_x*/,
460 11.0 /*source_end_y*/,
461 5.0 /*destination_end_x*/,
462 19.0 /*destination_end_y*/,
463 40.0 /*main_line_x*/
464 );
465
466 intersect_count = geometry_connector_count_connector_intersects ( &my_connector_1, &my_connector_2 );
467 TEST_EXPECT_EQUAL_INT( 3, intersect_count );
468
469 geometry_connector_destroy ( &my_connector_1 );
470 geometry_connector_destroy ( &my_connector_2 );
471 return TEST_CASE_RESULT_OK;
472 }
473
474 static test_case_result_t test_get_same_path_length_rect( test_fixture_t *fix )
475 {
476 double result;
477 geometry_connector_t my_connector_1;
478 geometry_rectangle_t my_rectangle_2;
479
480 /* test case J around rect */
481 geometry_connector_init_vertical( &my_connector_1,
482 10.0 /*source_end_x*/,
483 10.0 /*source_end_y*/,
484 10.0 /*destination_end_x*/,
485 30.0 /*destination_end_y*/,
486 20.0 /*main_line_x*/
487 );
488 geometry_rectangle_init( &my_rectangle_2, 8.0, 9.0, 5.0 /*W*/, 21.0 /*H*/ );
489 result = geometry_connector_get_same_path_length_rect( &my_connector_1, &my_rectangle_2, 2.1 /* max_distance */ );
490 TEST_EXPECT_EQUAL_DOUBLE( 6.0, result );
491
492 /* test case U around rect */
493 geometry_connector_init_horizontal( &my_connector_1,
494 10.0 /*source_end_x*/,
495 10.0 /*source_end_y*/,
496 30.0 /*destination_end_x*/,
497 10.0 /*destination_end_y*/,
498 20.0 /*main_line_y*/
499 );
500 geometry_rectangle_init( &my_rectangle_2, 13.0, 9.0, 19.0 /*W*/, 12.0 /*H*/ );
501 result = geometry_connector_get_same_path_length_rect( &my_connector_1, &my_rectangle_2, 2.1 /* max_distance */ );
502 TEST_EXPECT_EQUAL_DOUBLE( 27.0, result );
503
504 /* test case U in proximity to rect */
505 geometry_connector_init_horizontal( &my_connector_1,
506 10.0 /*source_end_x*/,
507 10.0 /*source_end_y*/,
508 30.0 /*destination_end_x*/,
509 10.0 /*destination_end_y*/,
510 20.0 /*main_line_y*/
511 );
512 geometry_rectangle_init( &my_rectangle_2, 31.0, 21.0, 1.0 /*W*/, 1.0 /*H*/ );
513 result = geometry_connector_get_same_path_length_rect( &my_connector_1, &my_rectangle_2, 2.1 /* max_distance */ );
514 TEST_EXPECT_EQUAL_DOUBLE( 0.0, result );
515
516 /* -- */
517 /* to get the line coverage up, additional tests */
518 geometry_rectangle_t my_rectangle_10;
519 geometry_rectangle_init( &my_rectangle_10, 10.0, 10.0, 10.0 /*W*/, 10.0 /*H*/ );
520
521 /* test case C around rect */
522 geometry_connector_init_vertical( &my_connector_1,
523 20.0 /*source_end_x*/,
524 20.0 /*source_end_y*/,
525 20.0 /*destination_end_x*/,
526 10.0 /*destination_end_y*/,
527 10.0 /*main_line_x*/
528 );
529 result = geometry_connector_get_same_path_length_rect( &my_connector_1, &my_rectangle_10, 2.1 /* max_distance */ );
530 TEST_EXPECT_EQUAL_DOUBLE( 30.0, result );
531
532 /* test case n around rect */
533 geometry_connector_init_horizontal( &my_connector_1,
534 20.0 /*source_end_x*/,
535 20.0 /*source_end_y*/,
536 10.0 /*destination_end_x*/,
537 20.0 /*destination_end_y*/,
538 10.0 /*main_line_y*/
539 );
540 result = geometry_connector_get_same_path_length_rect( &my_connector_1, &my_rectangle_10, 2.1 /* max_distance */ );
541 TEST_EXPECT_EQUAL_DOUBLE( 30.0, result );
542
543 /* test case J around rect */
544 geometry_connector_init_vertical( &my_connector_1,
545 10.0 /*source_end_x*/,
546 10.0 /*source_end_y*/,
547 10.0 /*destination_end_x*/,
548 20.0 /*destination_end_y*/,
549 20.0 /*main_line_x*/
550 );
551 result = geometry_connector_get_same_path_length_rect( &my_connector_1, &my_rectangle_10, 2.1 /* max_distance */ );
552 TEST_EXPECT_EQUAL_DOUBLE( 30.0, result );
553
554 /* test case U around rect */
555 geometry_connector_init_horizontal( &my_connector_1,
556 10.0 /*source_end_x*/,
557 10.0 /*source_end_y*/,
558 20.0 /*destination_end_x*/,
559 10.0 /*destination_end_y*/,
560 20.0 /*main_line_y*/
561 );
562 result = geometry_connector_get_same_path_length_rect( &my_connector_1, &my_rectangle_10, 2.1 /* max_distance */ );
563 TEST_EXPECT_EQUAL_DOUBLE( 30.0, result );
564
565 return TEST_CASE_RESULT_OK;
566 }
567
568 static test_case_result_t test_get_same_path_length_conn( test_fixture_t *fix )
569 {
570 double result;
571 geometry_connector_t my_connector_1;
572 geometry_connector_t my_connector_2;
573
574 /* test case C around C */
575 geometry_connector_init_vertical( &my_connector_1,
576 20.0 /*source_end_x*/,
577 10.0 /*source_end_y*/,
578 20.0 /*destination_end_x*/,
579 30.0 /*destination_end_y*/,
580 10.0 /*main_line_x*/
581 );
582 geometry_connector_init_vertical( &my_connector_2,
583 25.0 /*source_end_x*/,
584 11.0 /*source_end_y*/,
585 25.0 /*destination_end_x*/,
586 20.0 /*destination_end_y*/,
587 9.0 /*main_line_x*/
588 );
589 result = geometry_connector_get_same_path_length_conn( &my_connector_1, &my_connector_2, 2.1 /* max_distance */ );
590 TEST_EXPECT_EQUAL_DOUBLE( 19.0, result );
591 result = geometry_connector_get_same_path_length_conn( &my_connector_2, &my_connector_1, 2.1 /* max_distance */ );
592 TEST_EXPECT_EQUAL_DOUBLE( 19.0, result );
593
594 /* test case C cuts U */
595 geometry_connector_init_vertical( &my_connector_1,
596 20.0 /*source_end_x*/,
597 10.0 /*source_end_y*/,
598 20.0 /*destination_end_x*/,
599 30.0 /*destination_end_y*/,
600 10.0 /*main_line_x*/
601 );
602 geometry_connector_init_horizontal( &my_connector_2,
603 13.0 /*source_end_x*/,
604 8.0 /*source_end_y*/,
605 17.0 /*destination_end_x*/,
606 8.0 /*destination_end_y*/,
607 31.0 /*main_line_x*/
608 );
609 result = geometry_connector_get_same_path_length_conn( &my_connector_1, &my_connector_2, 2.1 /* max_distance */ );
610 TEST_EXPECT_EQUAL_DOUBLE( 4.0, result );
611 result = geometry_connector_get_same_path_length_conn( &my_connector_2, &my_connector_1, 2.1 /* max_distance */ );
612 TEST_EXPECT_EQUAL_DOUBLE( 4.0, result );
613
614 /* -- */
615 /* to get the line coverage up, additional tests */
616
617 /* test case upper Z */
618 geometry_connector_t my_connector_upper_Z;
619 geometry_connector_init_vertical( &my_connector_upper_Z,
620 10.0 /*source_end_x*/,
621 10.0 /*source_end_y*/,
622 20.0 /*destination_end_x*/,
623 30.0 /*destination_end_y*/,
624 15.0 /*main_line_x*/
625 );
626 /* test case lower Z */
627 geometry_connector_t my_connector_lower_Z;
628 geometry_connector_init_vertical( &my_connector_lower_Z,
629 20.3 /*source_end_x*/,
630 30.3 /*source_end_y*/,
631 10.3 /*destination_end_x*/,
632 10.3 /*destination_end_y*/,
633 15.3 /*main_line_x*/
634 );
635 /* test case upper L7 */
636 geometry_connector_t my_connector_upper_L7;
637 geometry_connector_init_horizontal( &my_connector_upper_L7,
638 15.0 /*source_end_x*/,
639 10.0 /*source_end_y*/,
640 25.0 /*destination_end_x*/,
641 50.0 /*destination_end_y*/,
642 30.0 /*main_line_x*/
643 );
644 /* test case lower L7 */
645 geometry_connector_t my_connector_lower_L7;
646 geometry_connector_init_horizontal( &my_connector_lower_L7,
647 25.3 /*source_end_x*/,
648 50.3 /*source_end_y*/,
649 15.3 /*destination_end_x*/,
650 10.3 /*destination_end_y*/,
651 30.3 /*main_line_x*/
652 );
653
654 result = geometry_connector_get_same_path_length_conn( &my_connector_upper_Z, &my_connector_upper_Z, 2.1 /* max_distance */ );
655 TEST_EXPECT_EQUAL_DOUBLE( 30.0, result );
656 result = geometry_connector_get_same_path_length_conn( &my_connector_upper_Z, &my_connector_lower_Z, 2.1 /* max_distance */ );
657 TEST_EXPECT_EQUAL_DOUBLE( 29.1, result );
658 result = geometry_connector_get_same_path_length_conn( &my_connector_lower_Z, &my_connector_upper_Z, 2.1 /* max_distance */ );
659 TEST_EXPECT_EQUAL_DOUBLE( 29.1, result );
660 result = geometry_connector_get_same_path_length_conn( &my_connector_upper_Z, &my_connector_lower_Z, 0.2 /* max_distance */ );
661 TEST_EXPECT_EQUAL_DOUBLE( 0.0, result );
662
663 result = geometry_connector_get_same_path_length_conn( &my_connector_upper_L7, &my_connector_upper_L7, 2.1 /* max_distance */ );
664 TEST_EXPECT_EQUAL_DOUBLE( 50.0, result );
665 result = geometry_connector_get_same_path_length_conn( &my_connector_upper_L7, &my_connector_lower_L7, 2.1 /* max_distance */ );
666 TEST_EXPECT_EQUAL_DOUBLE( 49.1, result );
667 result = geometry_connector_get_same_path_length_conn( &my_connector_lower_L7, &my_connector_upper_L7, 2.1 /* max_distance */ );
668 TEST_EXPECT_EQUAL_DOUBLE( 49.1, result );
669 result = geometry_connector_get_same_path_length_conn( &my_connector_upper_L7, &my_connector_lower_L7, 0.2 /* max_distance */ );
670 TEST_EXPECT_EQUAL_DOUBLE( 0.0, result );
671
672 result = geometry_connector_get_same_path_length_conn( &my_connector_upper_Z, &my_connector_upper_L7, 2.1 /* max_distance */ );
673 TEST_EXPECT_EQUAL_DOUBLE( 25.0, result );
674 result = geometry_connector_get_same_path_length_conn( &my_connector_upper_L7, &my_connector_upper_Z, 2.1 /* max_distance */ );
675 TEST_EXPECT_EQUAL_DOUBLE( 25.0, result );
676 result = geometry_connector_get_same_path_length_conn( &my_connector_lower_Z, &my_connector_lower_L7, 2.1 /* max_distance */ );
677 TEST_EXPECT_EQUAL_DOUBLE( 25.0, result );
678 result = geometry_connector_get_same_path_length_conn( &my_connector_lower_L7, &my_connector_lower_Z, 2.1 /* max_distance */ );
679 TEST_EXPECT_EQUAL_DOUBLE( 25.0, result );
680
681 result = geometry_connector_get_same_path_length_conn( &my_connector_upper_Z, &my_connector_lower_L7, 2.1 /* max_distance */ );
682 TEST_EXPECT_EQUAL_DOUBLE( 24.4, result );
683 result = geometry_connector_get_same_path_length_conn( &my_connector_lower_L7, &my_connector_upper_Z, 2.1 /* max_distance */ );
684 TEST_EXPECT_EQUAL_DOUBLE( 24.4, result );
685 result = geometry_connector_get_same_path_length_conn( &my_connector_lower_Z, &my_connector_upper_L7, 2.1 /* max_distance */ );
686 TEST_EXPECT_EQUAL_DOUBLE( 24.7, result );
687 result = geometry_connector_get_same_path_length_conn( &my_connector_upper_L7, &my_connector_lower_Z, 2.1 /* max_distance */ );
688 TEST_EXPECT_EQUAL_DOUBLE( 24.7, result );
689
690 return TEST_CASE_RESULT_OK;
691 }
692
693 static test_case_result_t test_calc_waypoint_good( test_fixture_t *fix )
694 {
695 geometry_connector_t my_connector_1;
696
697 geometry_connector_init_vertical( &my_connector_1,
698 10.0 /*source_end_x*/,
699 10.0 /*source_end_y*/,
700 10.0 /*destination_end_x*/,
701 30.0 /*destination_end_y*/,
702 20.0 /*main_line_x*/
703 );
704
705 geometry_point_t pos_before = geometry_connector_calculate_waypoint( &my_connector_1, -0.1 );
706 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_point_get_x( &pos_before ) );
707 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_point_get_y( &pos_before ) );
708
709 geometry_point_t pos_first = geometry_connector_calculate_waypoint( &my_connector_1, 1.0 );
710 TEST_EXPECT_EQUAL_DOUBLE( 11.0, geometry_point_get_x( &pos_first ) );
711 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_point_get_y( &pos_first ) );
712
713 geometry_point_t pos_main = geometry_connector_calculate_waypoint( &my_connector_1, 29.0 );
714 TEST_EXPECT_EQUAL_DOUBLE( 20.0, geometry_point_get_x( &pos_main ) );
715 TEST_EXPECT_EQUAL_DOUBLE( 29.0, geometry_point_get_y( &pos_main ) );
716
717 geometry_point_t pos_third = geometry_connector_calculate_waypoint( &my_connector_1, 39.0 );
718 TEST_EXPECT_EQUAL_DOUBLE( 11.0, geometry_point_get_x( &pos_third ) );
719 TEST_EXPECT_EQUAL_DOUBLE( 30.0, geometry_point_get_y( &pos_third ) );
720
721 geometry_point_t pos_after = geometry_connector_calculate_waypoint( &my_connector_1, 40.1 );
722 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_point_get_x( &pos_after ) );
723 TEST_EXPECT_EQUAL_DOUBLE( 30.0, geometry_point_get_y( &pos_after ) );
724 return TEST_CASE_RESULT_OK;
725 }
726
727 static test_case_result_t test_calc_waypoint_zero( test_fixture_t *fix )
728 {
729 geometry_connector_t my_connector_2; /* main line length = 0 */
730 geometry_connector_t my_connector_3; /* srd+dest end line length = 0 */
731
732 geometry_connector_init_horizontal( &my_connector_2,
733 10.0 /*source_end_x*/,
734 10.0 /*source_end_y*/,
735 10.0 /*destination_end_x*/,
736 10.0 /*destination_end_y*/,
737 20.0 /*main_line_y*/
738 );
739 geometry_connector_init_horizontal( &my_connector_3,
740 10.0 /*source_end_x*/,
741 10.0 /*source_end_y*/,
742 20.0 /*destination_end_x*/,
743 10.0 /*destination_end_y*/,
744 10.0 /*main_line_y*/
745 );
746
747 geometry_point_t pos3_source = geometry_connector_calculate_waypoint( &my_connector_3, 0.0 );
748 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_point_get_x( &pos3_source ) );
749 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_point_get_y( &pos3_source ) );
750
751 geometry_point_t pos2_main = geometry_connector_calculate_waypoint( &my_connector_2, 10.0 );
752 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_point_get_x( &pos2_main ) );
753 TEST_EXPECT_EQUAL_DOUBLE( 20.0, geometry_point_get_y( &pos2_main ) );
754
755 geometry_point_t pos3_dest = geometry_connector_calculate_waypoint( &my_connector_3, 10.0 );
756 TEST_EXPECT_EQUAL_DOUBLE( 20.0, geometry_point_get_x( &pos3_dest ) );
757 TEST_EXPECT_EQUAL_DOUBLE( 10.0, geometry_point_get_y( &pos3_dest ) );
758 return TEST_CASE_RESULT_OK;
759 }
760
761
762 /*
763 * Copyright 2017-2026 Andreas Warnke
764 *
765 * Licensed under the Apache License, Version 2.0 (the "License");
766 * you may not use this file except in compliance with the License.
767 * You may obtain a copy of the License at
768 *
769 * http://www.apache.org/licenses/LICENSE-2.0
770 *
771 * Unless required by applicable law or agreed to in writing, software
772 * distributed under the License is distributed on an "AS IS" BASIS,
773 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
774 * See the License for the specific language governing permissions and
775 * limitations under the License.
776 */