source: mainline/uspace/lib/gfx/test/coord.c@ e2776ff

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e2776ff was e2776ff, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Unit test for gfx_coord_div_rneg()

  • Property mode set to 100644
File size: 18.4 KB
Line 
1/*
2 * Copyright (c) 2019 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <gfx/coord.h>
30#include <pcut/pcut.h>
31
32PCUT_INIT;
33
34PCUT_TEST_SUITE(coord);
35
36/** gfx_coord_div_rneg rounds towards negative numbers */
37PCUT_TEST(coord_div_rneg)
38{
39 PCUT_ASSERT_INT_EQUALS(-3, gfx_coord_div_rneg(-7, 3));
40 PCUT_ASSERT_INT_EQUALS(-2, gfx_coord_div_rneg(-6, 3));
41 PCUT_ASSERT_INT_EQUALS(-2, gfx_coord_div_rneg(-5, 3));
42 PCUT_ASSERT_INT_EQUALS(-2, gfx_coord_div_rneg(-4, 3));
43 PCUT_ASSERT_INT_EQUALS(-1, gfx_coord_div_rneg(-3, 3));
44 PCUT_ASSERT_INT_EQUALS(-1, gfx_coord_div_rneg(-2, 3));
45 PCUT_ASSERT_INT_EQUALS(-1, gfx_coord_div_rneg(-1, 3));
46 PCUT_ASSERT_INT_EQUALS(0, gfx_coord_div_rneg(0, 3));
47 PCUT_ASSERT_INT_EQUALS(0, gfx_coord_div_rneg(1, 3));
48 PCUT_ASSERT_INT_EQUALS(0, gfx_coord_div_rneg(2, 3));
49 PCUT_ASSERT_INT_EQUALS(1, gfx_coord_div_rneg(3, 3));
50 PCUT_ASSERT_INT_EQUALS(1, gfx_coord_div_rneg(4, 3));
51 PCUT_ASSERT_INT_EQUALS(1, gfx_coord_div_rneg(5, 3));
52 PCUT_ASSERT_INT_EQUALS(2, gfx_coord_div_rneg(6, 3));
53}
54
55/** gfx_coord2_add should add two coordinate vectors */
56PCUT_TEST(coord2_add)
57{
58 gfx_coord2_t a, b;
59 gfx_coord2_t d;
60
61 a.x = 10;
62 a.y = 11;
63 b.x = 20;
64 b.y = 22;
65
66 gfx_coord2_add(&a, &b, &d);
67
68 PCUT_ASSERT_INT_EQUALS(a.x + b.x, d.x);
69 PCUT_ASSERT_INT_EQUALS(a.y + b.y, d.y);
70}
71
72/** gfx_coord2_subtract should subtract two coordinate vectors */
73PCUT_TEST(coord2_subtract)
74{
75 gfx_coord2_t a, b;
76 gfx_coord2_t d;
77
78 a.x = 10;
79 a.y = 11;
80 b.x = 20;
81 b.y = 22;
82
83 gfx_coord2_subtract(&a, &b, &d);
84
85 PCUT_ASSERT_INT_EQUALS(a.x - b.x, d.x);
86 PCUT_ASSERT_INT_EQUALS(a.y - b.y, d.y);
87}
88
89/** gfx_coord2_clip with point to lower-left of clipping rectangle */
90PCUT_TEST(coord2_clip_ll)
91{
92 gfx_coord2_t p;
93 gfx_coord2_t cp;
94 gfx_rect_t rect;
95
96 p.x = 1;
97 p.y = 2;
98
99 rect.p0.x = 3;
100 rect.p0.y = 4;
101 rect.p1.x = 5;
102 rect.p1.y = 6;
103
104 gfx_coord2_clip(&p, &rect, &cp);
105
106 PCUT_ASSERT_INT_EQUALS(3, cp.x);
107 PCUT_ASSERT_INT_EQUALS(4, cp.y);
108}
109
110/** gfx_coord2_clip with point inside the clipping rectangle */
111PCUT_TEST(coord2_clip_mm)
112{
113 gfx_coord2_t p;
114 gfx_coord2_t cp;
115 gfx_rect_t rect;
116
117 p.x = 2;
118 p.y = 3;
119
120 rect.p0.x = 1;
121 rect.p0.y = 2;
122 rect.p1.x = 3;
123 rect.p1.y = 4;
124
125 gfx_coord2_clip(&p, &rect, &cp);
126
127 PCUT_ASSERT_INT_EQUALS(2, cp.x);
128 PCUT_ASSERT_INT_EQUALS(3, cp.y);
129}
130
131/** gfx_coord2_clip with point to upper-right of clipping rectangle */
132PCUT_TEST(coord2_clip_hh)
133{
134 gfx_coord2_t p;
135 gfx_coord2_t cp;
136 gfx_rect_t rect;
137
138 p.x = 5;
139 p.y = 6;
140
141 rect.p0.x = 1;
142 rect.p0.y = 2;
143 rect.p1.x = 3;
144 rect.p1.y = 4;
145
146 gfx_coord2_clip(&p, &rect, &cp);
147
148 PCUT_ASSERT_INT_EQUALS(2, cp.x);
149 PCUT_ASSERT_INT_EQUALS(3, cp.y);
150}
151
152/** gfx_coord2_project projects pixel from one rectangle to another */
153PCUT_TEST(coord2_project)
154{
155 gfx_coord2_t a, d;
156 gfx_rect_t srect, drect;
157
158 srect.p0.x = 10;
159 srect.p0.y = 10;
160 srect.p1.x = 20 + 1;
161 srect.p1.y = 20 + 1;
162
163 drect.p0.x = 100;
164 drect.p0.y = 100;
165 drect.p1.x = 200 + 1;
166 drect.p1.y = 200 + 1;
167
168 a.x = 10;
169 a.y = 10;
170 gfx_coord2_project(&a, &srect, &drect, &d);
171 PCUT_ASSERT_INT_EQUALS(100, d.x);
172 PCUT_ASSERT_INT_EQUALS(100, d.y);
173
174 a.x = 15;
175 a.y = 15;
176 gfx_coord2_project(&a, &srect, &drect, &d);
177 PCUT_ASSERT_INT_EQUALS(150, d.x);
178 PCUT_ASSERT_INT_EQUALS(150, d.y);
179
180 a.x = 12;
181 a.y = 16;
182 gfx_coord2_project(&a, &srect, &drect, &d);
183 PCUT_ASSERT_INT_EQUALS(120, d.x);
184 PCUT_ASSERT_INT_EQUALS(160, d.y);
185
186 a.x = 20;
187 a.y = 20;
188 gfx_coord2_project(&a, &srect, &drect, &d);
189 PCUT_ASSERT_INT_EQUALS(200, d.x);
190 PCUT_ASSERT_INT_EQUALS(200, d.y);
191}
192
193/** gfx_rect_translate should translate rectangle */
194PCUT_TEST(rect_translate)
195{
196 gfx_coord2_t offs;
197 gfx_rect_t srect;
198 gfx_rect_t drect;
199
200 offs.x = 5;
201 offs.y = 6;
202
203 srect.p0.x = 10;
204 srect.p0.y = 11;
205 srect.p1.x = 20;
206 srect.p1.y = 22;
207
208 gfx_rect_translate(&offs, &srect, &drect);
209
210 PCUT_ASSERT_INT_EQUALS(offs.x + srect.p0.x, drect.p0.x);
211 PCUT_ASSERT_INT_EQUALS(offs.y + srect.p0.y, drect.p0.y);
212 PCUT_ASSERT_INT_EQUALS(offs.x + srect.p1.x, drect.p1.x);
213 PCUT_ASSERT_INT_EQUALS(offs.y + srect.p1.y, drect.p1.y);
214}
215
216/** gfx_rect_rtranslate should reverse-translate rectangle */
217PCUT_TEST(rect_rtranslate)
218{
219 gfx_coord2_t offs;
220 gfx_rect_t srect;
221 gfx_rect_t drect;
222
223 offs.x = 5;
224 offs.y = 6;
225
226 srect.p0.x = 10;
227 srect.p0.y = 11;
228 srect.p1.x = 20;
229 srect.p1.y = 22;
230
231 gfx_rect_rtranslate(&offs, &srect, &drect);
232
233 PCUT_ASSERT_INT_EQUALS(srect.p0.x - offs.x, drect.p0.x);
234 PCUT_ASSERT_INT_EQUALS(srect.p0.y - offs.y, drect.p0.y);
235 PCUT_ASSERT_INT_EQUALS(srect.p1.x - offs.x, drect.p1.x);
236 PCUT_ASSERT_INT_EQUALS(srect.p1.y - offs.y, drect.p1.y);
237}
238
239/** Sorting span with lower start and higher end point results in the same span. */
240PCUT_TEST(span_points_sort_asc)
241{
242 gfx_coord_t a, b;
243
244 gfx_span_points_sort(1, 2, &a, &b);
245 PCUT_ASSERT_INT_EQUALS(1, a);
246 PCUT_ASSERT_INT_EQUALS(2, b);
247}
248
249/** Sorting span with same start and end point results in the same span. */
250PCUT_TEST(span_points_sort_equal)
251{
252 gfx_coord_t a, b;
253
254 gfx_span_points_sort(1, 1, &a, &b);
255 PCUT_ASSERT_INT_EQUALS(1, a);
256 PCUT_ASSERT_INT_EQUALS(1, b);
257}
258
259/** Sorting span with hight start and lower end point results in transposed span. */
260PCUT_TEST(span_points_sort_desc)
261{
262 gfx_coord_t a, b;
263
264 gfx_span_points_sort(1, 0, &a, &b);
265 PCUT_ASSERT_INT_EQUALS(1, a);
266 PCUT_ASSERT_INT_EQUALS(2, b);
267}
268
269/** Rectangle envelope with first rectangle empty should return the second rectangle. */
270PCUT_TEST(rect_envelope_a_empty)
271{
272 gfx_rect_t a;
273 gfx_rect_t b;
274 gfx_rect_t e;
275
276 a.p0.x = 0;
277 a.p0.y = 0;
278 a.p1.x = 0;
279 a.p1.y = 0;
280
281 b.p0.x = 1;
282 b.p0.y = 2;
283 b.p1.x = 3;
284 b.p1.y = 4;
285
286 gfx_rect_envelope(&a, &b, &e);
287 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
288 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
289 PCUT_ASSERT_INT_EQUALS(3, e.p1.x);
290 PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
291}
292
293/** Rectangle envelope with second rectangle empty should return the first rectangle. */
294PCUT_TEST(rect_envelope_b_empty)
295{
296 gfx_rect_t a;
297 gfx_rect_t b;
298 gfx_rect_t e;
299
300 a.p0.x = 1;
301 a.p0.y = 2;
302 a.p1.x = 3;
303 a.p1.y = 4;
304
305 b.p0.x = 0;
306 b.p0.y = 0;
307 b.p1.x = 0;
308 b.p1.y = 0;
309
310 gfx_rect_envelope(&a, &b, &e);
311 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
312 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
313 PCUT_ASSERT_INT_EQUALS(3, e.p1.x);
314 PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
315}
316
317/** Rectangle envelope, a has both coordinates lower than b */
318PCUT_TEST(rect_envelope_nonempty_a_lt_b)
319{
320 gfx_rect_t a;
321 gfx_rect_t b;
322 gfx_rect_t e;
323
324 a.p0.x = 1;
325 a.p0.y = 2;
326 a.p1.x = 3;
327 a.p1.y = 4;
328
329 b.p0.x = 5;
330 b.p0.y = 6;
331 b.p1.x = 7;
332 b.p1.y = 8;
333
334 gfx_rect_envelope(&a, &b, &e);
335 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
336 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
337 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
338 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
339}
340
341/** Rectangle envelope, a has both coordinates higher than b */
342PCUT_TEST(rect_envelope_nonempty_a_gt_b)
343{
344 gfx_rect_t a;
345 gfx_rect_t b;
346 gfx_rect_t e;
347
348 a.p0.x = 5;
349 a.p0.y = 6;
350 a.p1.x = 7;
351 a.p1.y = 8;
352
353 b.p0.x = 1;
354 b.p0.y = 2;
355 b.p1.x = 3;
356 b.p1.y = 4;
357
358 gfx_rect_envelope(&a, &b, &e);
359 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
360 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
361 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
362 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
363}
364
365/** Rectangle envelope, a is inside b */
366PCUT_TEST(rect_envelope_nonempty_a_inside_b)
367{
368 gfx_rect_t a;
369 gfx_rect_t b;
370 gfx_rect_t e;
371
372 a.p0.x = 1;
373 a.p0.y = 2;
374 a.p1.x = 7;
375 a.p1.y = 8;
376
377 b.p0.x = 3;
378 b.p0.y = 4;
379 b.p1.x = 5;
380 b.p1.y = 6;
381
382 gfx_rect_envelope(&a, &b, &e);
383 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
384 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
385 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
386 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
387}
388
389/** Rectangle envelope, b is inside a*/
390PCUT_TEST(rect_envelope_nonempty_b_inside_a)
391{
392 gfx_rect_t a;
393 gfx_rect_t b;
394 gfx_rect_t e;
395
396 a.p0.x = 3;
397 a.p0.y = 4;
398 a.p1.x = 5;
399 a.p1.y = 6;
400
401 b.p0.x = 1;
402 b.p0.y = 2;
403 b.p1.x = 7;
404 b.p1.y = 8;
405
406 gfx_rect_envelope(&a, &b, &e);
407 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
408 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
409 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
410 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
411}
412
413/** Rectangle envelope, a and b cross */
414PCUT_TEST(rect_envelope_nonempty_a_crosses_b)
415{
416 gfx_rect_t a;
417 gfx_rect_t b;
418 gfx_rect_t e;
419
420 a.p0.x = 1;
421 a.p0.y = 2;
422 a.p1.x = 4;
423 a.p1.y = 3;
424
425 b.p0.x = 2;
426 b.p0.y = 1;
427 b.p1.x = 3;
428 b.p1.y = 4;
429
430 gfx_rect_envelope(&a, &b, &e);
431 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
432 PCUT_ASSERT_INT_EQUALS(1, e.p0.y);
433 PCUT_ASSERT_INT_EQUALS(4, e.p1.x);
434 PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
435}
436
437/** Clip rectangle with rect completely inside the clipping rectangle */
438PCUT_TEST(rect_clip_rect_inside)
439{
440 gfx_rect_t rect;
441 gfx_rect_t clip;
442 gfx_rect_t dest;
443
444 rect.p0.x = 3;
445 rect.p0.y = 4;
446 rect.p1.x = 5;
447 rect.p1.y = 6;
448
449 clip.p0.x = 1;
450 clip.p0.y = 2;
451 clip.p1.x = 7;
452 clip.p1.y = 8;
453
454 gfx_rect_clip(&rect, &clip, &dest);
455 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
456 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
457 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
458 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
459}
460
461/** Clip rectangle with rect covering the clipping rectangle */
462PCUT_TEST(rect_clip_rect_covering)
463{
464 gfx_rect_t rect;
465 gfx_rect_t clip;
466 gfx_rect_t dest;
467
468 rect.p0.x = 1;
469 rect.p0.y = 2;
470 rect.p1.x = 7;
471 rect.p1.y = 8;
472
473 clip.p0.x = 3;
474 clip.p0.y = 4;
475 clip.p1.x = 5;
476 clip.p1.y = 6;
477
478 gfx_rect_clip(&rect, &clip, &dest);
479 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
480 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
481 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
482 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
483}
484
485/** Clip rectangle with rect outside, having lower coordinates */
486PCUT_TEST(rect_clip_rect_out_ll)
487{
488 gfx_rect_t rect;
489 gfx_rect_t clip;
490 gfx_rect_t dest;
491
492 rect.p0.x = 1;
493 rect.p0.y = 2;
494 rect.p1.x = 3;
495 rect.p1.y = 4;
496
497 clip.p0.x = 5;
498 clip.p0.y = 6;
499 clip.p1.x = 7;
500 clip.p1.y = 8;
501
502 gfx_rect_clip(&rect, &clip, &dest);
503 PCUT_ASSERT_INT_EQUALS(5, dest.p0.x);
504 PCUT_ASSERT_INT_EQUALS(6, dest.p0.y);
505 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
506 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
507}
508
509/** Clip rectangle with rect outside, having higher coordinates */
510PCUT_TEST(rect_clip_rect_out_hh)
511{
512 gfx_rect_t rect;
513 gfx_rect_t clip;
514 gfx_rect_t dest;
515
516 rect.p0.x = 5;
517 rect.p0.y = 6;
518 rect.p1.x = 7;
519 rect.p1.y = 8;
520
521 clip.p0.x = 1;
522 clip.p0.y = 2;
523 clip.p1.x = 3;
524 clip.p1.y = 4;
525
526 gfx_rect_clip(&rect, &clip, &dest);
527 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
528 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
529 PCUT_ASSERT_INT_EQUALS(3, dest.p1.x);
530 PCUT_ASSERT_INT_EQUALS(4, dest.p1.y);
531}
532
533/** Clip rectangle with rect partially outside, having lower coordinates */
534PCUT_TEST(rect_clip_rect_ll)
535{
536 gfx_rect_t rect;
537 gfx_rect_t clip;
538 gfx_rect_t dest;
539
540 rect.p0.x = 1;
541 rect.p0.y = 2;
542 rect.p1.x = 5;
543 rect.p1.y = 6;
544
545 clip.p0.x = 3;
546 clip.p0.y = 4;
547 clip.p1.x = 7;
548 clip.p1.y = 8;
549
550 gfx_rect_clip(&rect, &clip, &dest);
551 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
552 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
553 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
554 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
555}
556
557/** Clip rectangle with rect partially outside, having higher coordinates */
558PCUT_TEST(rect_clip_rect_hh)
559{
560 gfx_rect_t rect;
561 gfx_rect_t clip;
562 gfx_rect_t dest;
563
564 rect.p0.x = 3;
565 rect.p0.y = 4;
566 rect.p1.x = 7;
567 rect.p1.y = 8;
568
569 clip.p0.x = 1;
570 clip.p0.y = 2;
571 clip.p1.x = 5;
572 clip.p1.y = 6;
573
574 gfx_rect_clip(&rect, &clip, &dest);
575 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
576 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
577 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
578 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
579}
580
581/** Clip rectangle with no clipping rectangle */
582PCUT_TEST(rect_clip_rect_noclip)
583{
584 gfx_rect_t rect;
585 gfx_rect_t dest;
586
587 rect.p0.x = 1;
588 rect.p0.y = 2;
589 rect.p1.x = 3;
590 rect.p1.y = 4;
591
592 gfx_rect_clip(&rect, NULL, &dest);
593 PCUT_ASSERT_INT_EQUALS(rect.p0.x, dest.p0.x);
594 PCUT_ASSERT_INT_EQUALS(rect.p0.y, dest.p0.y);
595 PCUT_ASSERT_INT_EQUALS(rect.p1.x, dest.p1.x);
596 PCUT_ASSERT_INT_EQUALS(rect.p1.y, dest.p1.y);
597}
598
599/** Sort span points that are already sorted should produde indentical points */
600PCUT_TEST(rect_points_sort_sorted)
601{
602 gfx_coord_t s0, s1;
603
604 gfx_span_points_sort(1, 2, &s0, &s1);
605 PCUT_ASSERT_INT_EQUALS(1, s0);
606 PCUT_ASSERT_INT_EQUALS(2, s1);
607}
608
609/** Sort span points that are reversed should transpose them */
610PCUT_TEST(rect_points_sort_reversed)
611{
612 gfx_coord_t s0, s1;
613
614 gfx_span_points_sort(2, 1, &s0, &s1);
615 PCUT_ASSERT_INT_EQUALS(2, s0);
616 PCUT_ASSERT_INT_EQUALS(3, s1);
617}
618
619/** Rectangle dimensions for straight rectangle are computed correctly */
620PCUT_TEST(rect_dims_straight)
621{
622 gfx_rect_t rect;
623 gfx_coord2_t dims;
624
625 rect.p0.x = 1;
626 rect.p0.y = 10;
627 rect.p1.x = 100;
628 rect.p1.y = 1000;
629
630 gfx_rect_dims(&rect, &dims);
631
632 PCUT_ASSERT_INT_EQUALS(99, dims.x);
633 PCUT_ASSERT_INT_EQUALS(990, dims.y);
634}
635
636/** Rectangle dimensions for reversed rectangle are computed correctly */
637PCUT_TEST(rect_dims_reversed)
638{
639 gfx_rect_t rect;
640 gfx_coord2_t dims;
641
642 rect.p0.x = 1000;
643 rect.p0.y = 100;
644 rect.p1.x = 10;
645 rect.p1.y = 1;
646
647 gfx_rect_dims(&rect, &dims);
648
649 PCUT_ASSERT_INT_EQUALS(990, dims.x);
650 PCUT_ASSERT_INT_EQUALS(99, dims.y);
651}
652
653/** gfx_rect_is_empty for straight rectangle with zero columns returns true */
654PCUT_TEST(rect_is_empty_pos_x)
655{
656 gfx_rect_t rect;
657
658 rect.p0.x = 1;
659 rect.p0.y = 2;
660 rect.p1.x = 1;
661 rect.p1.y = 3;
662 PCUT_ASSERT_TRUE(gfx_rect_is_empty(&rect));
663}
664
665/** gfx_rect_is_empty for straight rectangle with zero rows returns true */
666PCUT_TEST(rect_is_empty_pos_y)
667{
668 gfx_rect_t rect;
669
670 rect.p0.x = 1;
671 rect.p0.y = 2;
672 rect.p1.x = 2;
673 rect.p1.y = 2;
674 PCUT_ASSERT_TRUE(gfx_rect_is_empty(&rect));
675}
676
677/** gfx_rect_is_empty for straight non-empty rectangle returns false */
678PCUT_TEST(rect_is_empty_neg)
679{
680 gfx_rect_t rect;
681
682 rect.p0.x = 1;
683 rect.p0.y = 2;
684 rect.p1.x = 2;
685 rect.p1.y = 3;
686 PCUT_ASSERT_FALSE(gfx_rect_is_empty(&rect));
687}
688
689/** gfx_rect_is_empty for reverse non-empty rectangle returns false */
690PCUT_TEST(rect_is_empty_reverse_neg)
691{
692 gfx_rect_t rect;
693
694 rect.p0.x = 1;
695 rect.p0.y = 2;
696 rect.p1.x = 0;
697 rect.p1.y = 1;
698 PCUT_ASSERT_FALSE(gfx_rect_is_empty(&rect));
699}
700
701/** gfx_rect_is_incident for neighboring rectangles returns false */
702PCUT_TEST(rect_is_incident_neighbor)
703{
704 gfx_rect_t a;
705 gfx_rect_t b;
706
707 a.p0.x = 1;
708 a.p0.y = 2;
709 a.p1.x = 3;
710 a.p1.y = 4;
711
712 b.p0.x = 3;
713 b.p0.y = 2;
714 b.p1.x = 5;
715 b.p1.y = 6;
716
717 PCUT_ASSERT_FALSE(gfx_rect_is_incident(&a, &b));
718}
719
720/** gfx_rect_is_incident for a inside b returns true */
721PCUT_TEST(rect_is_incident_a_inside_b)
722{
723 gfx_rect_t a;
724 gfx_rect_t b;
725
726 a.p0.x = 2;
727 a.p0.y = 3;
728 a.p1.x = 4;
729 a.p1.y = 5;
730
731 b.p0.x = 1;
732 b.p0.y = 2;
733 b.p1.x = 5;
734 b.p1.y = 6;
735
736 PCUT_ASSERT_TRUE(gfx_rect_is_incident(&a, &b));
737}
738
739/** gfx_rect_is_incident for b inside a returns true */
740PCUT_TEST(rect_is_incident_b_inside_a)
741{
742 gfx_rect_t a;
743 gfx_rect_t b;
744
745 a.p0.x = 1;
746 a.p0.y = 2;
747 a.p1.x = 5;
748 a.p1.y = 6;
749
750 b.p0.x = 2;
751 b.p0.y = 3;
752 b.p1.x = 4;
753 b.p1.y = 5;
754
755 PCUT_ASSERT_TRUE(gfx_rect_is_incident(&a, &b));
756}
757
758/** gfx_rect_is_incident for a and b sharing corner returns true */
759PCUT_TEST(rect_is_incident_corner)
760{
761 gfx_rect_t a;
762 gfx_rect_t b;
763
764 a.p0.x = 1;
765 a.p0.y = 2;
766 a.p1.x = 3;
767 a.p1.y = 4;
768
769 b.p0.x = 2;
770 b.p0.y = 3;
771 b.p1.x = 4;
772 b.p1.y = 5;
773
774 PCUT_ASSERT_TRUE(gfx_rect_is_incident(&a, &b));
775}
776
777/** gfx_rect_is_incident for a == b returns true */
778PCUT_TEST(rect_is_incident_same)
779{
780 gfx_rect_t a;
781 gfx_rect_t b;
782
783 a.p0.x = 1;
784 a.p0.y = 2;
785 a.p1.x = 3;
786 a.p1.y = 4;
787
788 b.p0.x = 1;
789 b.p0.y = 2;
790 b.p1.x = 3;
791 b.p1.y = 4;
792
793 PCUT_ASSERT_TRUE(gfx_rect_is_incident(&a, &b));
794}
795
796/** gfx_rect_is_inside is true for rectangle strictly inside */
797PCUT_TEST(rect_is_inside_strict)
798{
799 gfx_rect_t a;
800 gfx_rect_t b;
801
802 a.p0.x = 2;
803 a.p0.y = 3;
804 a.p1.x = 4;
805 a.p1.y = 5;
806
807 b.p0.x = 1;
808 b.p0.y = 2;
809 b.p1.x = 5;
810 b.p1.y = 6;
811
812 PCUT_ASSERT_TRUE(gfx_rect_is_inside(&a, &b));
813}
814
815/** gfx_rect_is_inside is true for two equal rectangles */
816PCUT_TEST(rect_is_inside_same)
817{
818 gfx_rect_t a;
819 gfx_rect_t b;
820
821 a.p0.x = 1;
822 a.p0.y = 2;
823 a.p1.x = 3;
824 a.p1.y = 4;
825
826 b.p0.x = 1;
827 b.p0.y = 2;
828 b.p1.x = 3;
829 b.p1.y = 4;
830
831 PCUT_ASSERT_TRUE(gfx_rect_is_inside(&a, &b));
832}
833
834/** gfx_rect_is_inside is false for @c a.p0 outside */
835PCUT_TEST(rect_is_inside_p0_outside)
836{
837 gfx_rect_t a;
838 gfx_rect_t b;
839
840 a.p0.x = 0;
841 a.p0.y = 2;
842 a.p1.x = 3;
843 a.p1.y = 4;
844
845 b.p0.x = 1;
846 b.p0.y = 2;
847 b.p1.x = 3;
848 b.p1.y = 4;
849
850 PCUT_ASSERT_FALSE(gfx_rect_is_inside(&a, &b));
851
852 a.p0.x = 1;
853 a.p0.y = 1;
854 a.p1.x = 3;
855 a.p1.y = 4;
856
857 b.p0.x = 1;
858 b.p0.y = 2;
859 b.p1.x = 3;
860 b.p1.y = 4;
861
862 PCUT_ASSERT_FALSE(gfx_rect_is_inside(&a, &b));
863}
864
865/** gfx_rect_is_inside is false for @c a.p1 outside */
866PCUT_TEST(rect_is_inside_p1_outside)
867{
868 gfx_rect_t a;
869 gfx_rect_t b;
870
871 a.p0.x = 1;
872 a.p0.y = 2;
873 a.p1.x = 4;
874 a.p1.y = 4;
875
876 b.p0.x = 1;
877 b.p0.y = 2;
878 b.p1.x = 3;
879 b.p1.y = 4;
880
881 PCUT_ASSERT_FALSE(gfx_rect_is_inside(&a, &b));
882
883 a.p0.x = 1;
884 a.p0.y = 2;
885 a.p1.x = 3;
886 a.p1.y = 5;
887
888 b.p0.x = 1;
889 b.p0.y = 2;
890 b.p1.x = 3;
891 b.p1.y = 4;
892
893 PCUT_ASSERT_FALSE(gfx_rect_is_inside(&a, &b));
894}
895
896/** gfx_pix_inside_rect for */
897PCUT_TEST(pix_inside_rect)
898{
899 gfx_coord2_t coord;
900 gfx_rect_t rect;
901
902 rect.p0.x = 1;
903 rect.p0.y = 2;
904 rect.p1.x = 3;
905 rect.p1.y = 4;
906
907 coord.x = 0;
908 coord.y = 1;
909 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
910
911 coord.x = 1;
912 coord.y = 1;
913 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
914
915 coord.x = 0;
916 coord.y = 2;
917 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
918
919 coord.x = 1;
920 coord.y = 2;
921 PCUT_ASSERT_TRUE(gfx_pix_inside_rect(&coord, &rect));
922
923 coord.x = 2;
924 coord.y = 3;
925 PCUT_ASSERT_TRUE(gfx_pix_inside_rect(&coord, &rect));
926
927 coord.x = 3;
928 coord.y = 3;
929 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
930
931 coord.x = 2;
932 coord.y = 4;
933 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
934
935 coord.x = 3;
936 coord.y = 4;
937 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
938}
939
940PCUT_EXPORT(coord);
Note: See TracBrowser for help on using the repository browser.