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

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

Get display resolution by querying display device

  • Property mode set to 100644
File size: 12.9 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_coord2_add should add two coordinate vectors */
37PCUT_TEST(coord2_add)
38{
39 gfx_coord2_t a, b;
40 gfx_coord2_t d;
41
42 a.x = 10;
43 a.y = 11;
44 b.x = 20;
45 b.y = 22;
46
47 gfx_coord2_add(&a, &b, &d);
48
49 PCUT_ASSERT_INT_EQUALS(a.x + b.x, d.x);
50 PCUT_ASSERT_INT_EQUALS(a.y + b.y, d.y);
51}
52
53/** gfx_coord2_subtract should subtract two coordinate vectors */
54PCUT_TEST(coord2_subtract)
55{
56 gfx_coord2_t a, b;
57 gfx_coord2_t d;
58
59 a.x = 10;
60 a.y = 11;
61 b.x = 20;
62 b.y = 22;
63
64 gfx_coord2_subtract(&a, &b, &d);
65
66 PCUT_ASSERT_INT_EQUALS(a.x - b.x, d.x);
67 PCUT_ASSERT_INT_EQUALS(a.y - b.y, d.y);
68}
69
70/** gfx_coord2_clip with point to lower-left of clipping rectangle */
71PCUT_TEST(coord2_clip_ll)
72{
73 gfx_coord2_t p;
74 gfx_coord2_t cp;
75 gfx_rect_t rect;
76
77 p.x = 1;
78 p.y = 2;
79
80 rect.p0.x = 3;
81 rect.p0.y = 4;
82 rect.p1.x = 5;
83 rect.p1.y = 6;
84
85 gfx_coord2_clip(&p, &rect, &cp);
86
87 PCUT_ASSERT_INT_EQUALS(3, cp.x);
88 PCUT_ASSERT_INT_EQUALS(4, cp.y);
89}
90
91/** gfx_coord2_clip with point inside the clipping rectangle */
92PCUT_TEST(coord2_clip_mm)
93{
94 gfx_coord2_t p;
95 gfx_coord2_t cp;
96 gfx_rect_t rect;
97
98 p.x = 2;
99 p.y = 3;
100
101 rect.p0.x = 1;
102 rect.p0.y = 2;
103 rect.p1.x = 3;
104 rect.p1.y = 4;
105
106 gfx_coord2_clip(&p, &rect, &cp);
107
108 PCUT_ASSERT_INT_EQUALS(2, cp.x);
109 PCUT_ASSERT_INT_EQUALS(3, cp.y);
110}
111
112/** gfx_coord2_clip with point to upper-right of clipping rectangle */
113PCUT_TEST(coord2_clip_hh)
114{
115 gfx_coord2_t p;
116 gfx_coord2_t cp;
117 gfx_rect_t rect;
118
119 p.x = 5;
120 p.y = 6;
121
122 rect.p0.x = 1;
123 rect.p0.y = 2;
124 rect.p1.x = 3;
125 rect.p1.y = 4;
126
127 gfx_coord2_clip(&p, &rect, &cp);
128
129 PCUT_ASSERT_INT_EQUALS(2, cp.x);
130 PCUT_ASSERT_INT_EQUALS(3, cp.y);
131}
132
133/** gfx_rect_translate should translate rectangle */
134PCUT_TEST(rect_translate)
135{
136 gfx_coord2_t offs;
137 gfx_rect_t srect;
138 gfx_rect_t drect;
139
140 offs.x = 5;
141 offs.y = 6;
142
143 srect.p0.x = 10;
144 srect.p0.y = 11;
145 srect.p1.x = 20;
146 srect.p1.y = 22;
147
148 gfx_rect_translate(&offs, &srect, &drect);
149
150 PCUT_ASSERT_INT_EQUALS(offs.x + srect.p0.x, drect.p0.x);
151 PCUT_ASSERT_INT_EQUALS(offs.y + srect.p0.y, drect.p0.y);
152 PCUT_ASSERT_INT_EQUALS(offs.x + srect.p1.x, drect.p1.x);
153 PCUT_ASSERT_INT_EQUALS(offs.y + srect.p1.y, drect.p1.y);
154}
155
156/** gfx_rect_rtranslate should reverse-translate rectangle */
157PCUT_TEST(rect_rtranslate)
158{
159 gfx_coord2_t offs;
160 gfx_rect_t srect;
161 gfx_rect_t drect;
162
163 offs.x = 5;
164 offs.y = 6;
165
166 srect.p0.x = 10;
167 srect.p0.y = 11;
168 srect.p1.x = 20;
169 srect.p1.y = 22;
170
171 gfx_rect_rtranslate(&offs, &srect, &drect);
172
173 PCUT_ASSERT_INT_EQUALS(srect.p0.x - offs.x, drect.p0.x);
174 PCUT_ASSERT_INT_EQUALS(srect.p0.y - offs.y, drect.p0.y);
175 PCUT_ASSERT_INT_EQUALS(srect.p1.x - offs.x, drect.p1.x);
176 PCUT_ASSERT_INT_EQUALS(srect.p1.y - offs.y, drect.p1.y);
177}
178
179/** Sorting span with lower start and higher end point results in the same span. */
180PCUT_TEST(span_points_sort_asc)
181{
182 gfx_coord_t a, b;
183
184 gfx_span_points_sort(1, 2, &a, &b);
185 PCUT_ASSERT_INT_EQUALS(1, a);
186 PCUT_ASSERT_INT_EQUALS(2, b);
187}
188
189/** Sorting span with same start and end point results in the same span. */
190PCUT_TEST(span_points_sort_equal)
191{
192 gfx_coord_t a, b;
193
194 gfx_span_points_sort(1, 1, &a, &b);
195 PCUT_ASSERT_INT_EQUALS(1, a);
196 PCUT_ASSERT_INT_EQUALS(1, b);
197}
198
199/** Sorting span with hight start and lower end point results in transposed span. */
200PCUT_TEST(span_points_sort_decs)
201{
202 gfx_coord_t a, b;
203
204 gfx_span_points_sort(1, 0, &a, &b);
205 PCUT_ASSERT_INT_EQUALS(1, a);
206 PCUT_ASSERT_INT_EQUALS(2, b);
207}
208
209/** Rectangle envelope with first rectangle empty should return the second rectangle. */
210PCUT_TEST(rect_envelope_a_empty)
211{
212 gfx_rect_t a;
213 gfx_rect_t b;
214 gfx_rect_t e;
215
216 a.p0.x = 0;
217 a.p0.y = 0;
218 a.p1.x = 0;
219 a.p1.y = 0;
220
221 b.p0.x = 1;
222 b.p0.y = 2;
223 b.p1.x = 3;
224 b.p1.y = 4;
225
226 gfx_rect_envelope(&a, &b, &e);
227 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
228 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
229 PCUT_ASSERT_INT_EQUALS(3, e.p1.x);
230 PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
231}
232
233/** Rectangle envelope with second rectangle empty should return the first rectangle. */
234PCUT_TEST(rect_envelope_b_empty)
235{
236 gfx_rect_t a;
237 gfx_rect_t b;
238 gfx_rect_t e;
239
240 a.p0.x = 1;
241 a.p0.y = 2;
242 a.p1.x = 3;
243 a.p1.y = 4;
244
245 b.p0.x = 0;
246 b.p0.y = 0;
247 b.p1.x = 0;
248 b.p1.y = 0;
249
250 gfx_rect_envelope(&a, &b, &e);
251 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
252 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
253 PCUT_ASSERT_INT_EQUALS(3, e.p1.x);
254 PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
255}
256
257/** Rectangle envelope, a has both coordinates lower than b */
258PCUT_TEST(rect_envelope_nonempty_a_lt_b)
259{
260 gfx_rect_t a;
261 gfx_rect_t b;
262 gfx_rect_t e;
263
264 a.p0.x = 1;
265 a.p0.y = 2;
266 a.p1.x = 3;
267 a.p1.y = 4;
268
269 b.p0.x = 5;
270 b.p0.y = 6;
271 b.p1.x = 7;
272 b.p1.y = 8;
273
274 gfx_rect_envelope(&a, &b, &e);
275 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
276 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
277 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
278 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
279}
280
281/** Rectangle envelope, a has both coordinates higher than b */
282PCUT_TEST(rect_envelope_nonempty_a_gt_b)
283{
284 gfx_rect_t a;
285 gfx_rect_t b;
286 gfx_rect_t e;
287
288 a.p0.x = 5;
289 a.p0.y = 6;
290 a.p1.x = 7;
291 a.p1.y = 8;
292
293 b.p0.x = 1;
294 b.p0.y = 2;
295 b.p1.x = 3;
296 b.p1.y = 4;
297
298 gfx_rect_envelope(&a, &b, &e);
299 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
300 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
301 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
302 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
303}
304
305/** Rectangle envelope, a is inside b */
306PCUT_TEST(rect_envelope_nonempty_a_inside_b)
307{
308 gfx_rect_t a;
309 gfx_rect_t b;
310 gfx_rect_t e;
311
312 a.p0.x = 1;
313 a.p0.y = 2;
314 a.p1.x = 7;
315 a.p1.y = 8;
316
317 b.p0.x = 3;
318 b.p0.y = 4;
319 b.p1.x = 5;
320 b.p1.y = 6;
321
322 gfx_rect_envelope(&a, &b, &e);
323 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
324 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
325 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
326 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
327}
328
329/** Rectangle envelope, b is inside a*/
330PCUT_TEST(rect_envelope_nonempty_b_inside_a)
331{
332 gfx_rect_t a;
333 gfx_rect_t b;
334 gfx_rect_t e;
335
336 a.p0.x = 3;
337 a.p0.y = 4;
338 a.p1.x = 5;
339 a.p1.y = 6;
340
341 b.p0.x = 1;
342 b.p0.y = 2;
343 b.p1.x = 7;
344 b.p1.y = 8;
345
346 gfx_rect_envelope(&a, &b, &e);
347 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
348 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
349 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
350 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
351}
352
353/** Rectangle envelope, a and b cross */
354PCUT_TEST(rect_envelope_nonempty_a_crosses_b)
355{
356 gfx_rect_t a;
357 gfx_rect_t b;
358 gfx_rect_t e;
359
360 a.p0.x = 1;
361 a.p0.y = 2;
362 a.p1.x = 4;
363 a.p1.y = 3;
364
365 b.p0.x = 2;
366 b.p0.y = 1;
367 b.p1.x = 3;
368 b.p1.y = 4;
369
370 gfx_rect_envelope(&a, &b, &e);
371 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
372 PCUT_ASSERT_INT_EQUALS(1, e.p0.y);
373 PCUT_ASSERT_INT_EQUALS(4, e.p1.x);
374 PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
375}
376
377/** Clip rectangle with rect completely inside the clipping rectangle */
378PCUT_TEST(rect_clip_rect_inside)
379{
380 gfx_rect_t rect;
381 gfx_rect_t clip;
382 gfx_rect_t dest;
383
384 rect.p0.x = 3;
385 rect.p0.y = 4;
386 rect.p1.x = 5;
387 rect.p1.y = 6;
388
389 clip.p0.x = 1;
390 clip.p0.y = 2;
391 clip.p1.x = 7;
392 clip.p1.y = 8;
393
394 gfx_rect_clip(&rect, &clip, &dest);
395 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
396 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
397 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
398 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
399}
400
401/** Clip rectangle with rect covering the clipping rectangle */
402PCUT_TEST(rect_clip_rect_covering)
403{
404 gfx_rect_t rect;
405 gfx_rect_t clip;
406 gfx_rect_t dest;
407
408 rect.p0.x = 1;
409 rect.p0.y = 2;
410 rect.p1.x = 7;
411 rect.p1.y = 8;
412
413 clip.p0.x = 3;
414 clip.p0.y = 4;
415 clip.p1.x = 5;
416 clip.p1.y = 6;
417
418 gfx_rect_clip(&rect, &clip, &dest);
419 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
420 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
421 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
422 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
423}
424
425/** Clip rectangle with rect outside, having lower coordinates */
426PCUT_TEST(rect_clip_rect_out_ll)
427{
428 gfx_rect_t rect;
429 gfx_rect_t clip;
430 gfx_rect_t dest;
431
432 rect.p0.x = 1;
433 rect.p0.y = 2;
434 rect.p1.x = 3;
435 rect.p1.y = 4;
436
437 clip.p0.x = 5;
438 clip.p0.y = 6;
439 clip.p1.x = 7;
440 clip.p1.y = 8;
441
442 gfx_rect_clip(&rect, &clip, &dest);
443 PCUT_ASSERT_INT_EQUALS(5, dest.p0.x);
444 PCUT_ASSERT_INT_EQUALS(6, dest.p0.y);
445 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
446 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
447}
448
449/** Clip rectangle with rect outside, having higher coordinates */
450PCUT_TEST(rect_clip_rect_out_hh)
451{
452 gfx_rect_t rect;
453 gfx_rect_t clip;
454 gfx_rect_t dest;
455
456 rect.p0.x = 5;
457 rect.p0.y = 6;
458 rect.p1.x = 7;
459 rect.p1.y = 8;
460
461 clip.p0.x = 1;
462 clip.p0.y = 2;
463 clip.p1.x = 3;
464 clip.p1.y = 4;
465
466 gfx_rect_clip(&rect, &clip, &dest);
467 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
468 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
469 PCUT_ASSERT_INT_EQUALS(3, dest.p1.x);
470 PCUT_ASSERT_INT_EQUALS(4, dest.p1.y);
471}
472
473/** Clip rectangle with rect partially outside, having lower coordinates */
474PCUT_TEST(rect_clip_rect_ll)
475{
476 gfx_rect_t rect;
477 gfx_rect_t clip;
478 gfx_rect_t dest;
479
480 rect.p0.x = 1;
481 rect.p0.y = 2;
482 rect.p1.x = 5;
483 rect.p1.y = 6;
484
485 clip.p0.x = 3;
486 clip.p0.y = 4;
487 clip.p1.x = 7;
488 clip.p1.y = 8;
489
490 gfx_rect_clip(&rect, &clip, &dest);
491 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
492 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
493 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
494 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
495}
496
497/** Clip rectangle with rect partially outside, having higher coordinates */
498PCUT_TEST(rect_clip_rect_hh)
499{
500 gfx_rect_t rect;
501 gfx_rect_t clip;
502 gfx_rect_t dest;
503
504 rect.p0.x = 3;
505 rect.p0.y = 4;
506 rect.p1.x = 7;
507 rect.p1.y = 8;
508
509 clip.p0.x = 1;
510 clip.p0.y = 2;
511 clip.p1.x = 5;
512 clip.p1.y = 6;
513
514 gfx_rect_clip(&rect, &clip, &dest);
515 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
516 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
517 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
518 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
519}
520
521/** Sort span points that are already sorted should produde indentical points */
522PCUT_TEST(rect_points_sort_sorted)
523{
524 gfx_coord_t s0, s1;
525
526 gfx_span_points_sort(1, 2, &s0, &s1);
527 PCUT_ASSERT_INT_EQUALS(1, s0);
528 PCUT_ASSERT_INT_EQUALS(2, s1);
529}
530
531/** Sort span points that are reversed should transpose them */
532PCUT_TEST(rect_points_sort_reversed)
533{
534 gfx_coord_t s0, s1;
535
536 gfx_span_points_sort(2, 1, &s0, &s1);
537 PCUT_ASSERT_INT_EQUALS(2, s0);
538 PCUT_ASSERT_INT_EQUALS(3, s1);
539}
540
541/** gfx_rect_is_empty for straight rectangle with zero columns returns true */
542PCUT_TEST(rect_is_empty_pos_x)
543{
544 gfx_rect_t rect;
545
546 rect.p0.x = 1;
547 rect.p0.y = 2;
548 rect.p1.x = 1;
549 rect.p1.y = 3;
550 PCUT_ASSERT_TRUE(gfx_rect_is_empty(&rect));
551}
552
553/** gfx_rect_is_empty for straight rectangle with zero rows returns true */
554PCUT_TEST(rect_is_empty_pos_y)
555{
556 gfx_rect_t rect;
557
558 rect.p0.x = 1;
559 rect.p0.y = 2;
560 rect.p1.x = 2;
561 rect.p1.y = 2;
562 PCUT_ASSERT_TRUE(gfx_rect_is_empty(&rect));
563}
564
565/** gfx_rect_is_empty for staright non-empty rectangle returns false */
566PCUT_TEST(rect_is_empty_neg)
567{
568 gfx_rect_t rect;
569
570 rect.p0.x = 1;
571 rect.p0.y = 2;
572 rect.p1.x = 2;
573 rect.p1.y = 3;
574 PCUT_ASSERT_FALSE(gfx_rect_is_empty(&rect));
575}
576
577/** gfx_rect_is_empty for reverse non-empty rectangle returns false */
578PCUT_TEST(rect_is_empty_reverse_neg)
579{
580 gfx_rect_t rect;
581
582 rect.p0.x = 1;
583 rect.p0.y = 2;
584 rect.p1.x = 0;
585 rect.p1.y = 1;
586 PCUT_ASSERT_FALSE(gfx_rect_is_empty(&rect));
587}
588
589/** gfx_pix_inside_rect for */
590PCUT_TEST(pix_inside_rect)
591{
592 gfx_coord2_t coord;
593 gfx_rect_t rect;
594
595 rect.p0.x = 1;
596 rect.p0.y = 2;
597 rect.p1.x = 3;
598 rect.p1.y = 4;
599
600 coord.x = 0;
601 coord.y = 1;
602 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
603
604 coord.x = 1;
605 coord.y = 1;
606 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
607
608 coord.x = 0;
609 coord.y = 2;
610 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
611
612 coord.x = 1;
613 coord.y = 2;
614 PCUT_ASSERT_TRUE(gfx_pix_inside_rect(&coord, &rect));
615
616 coord.x = 2;
617 coord.y = 3;
618 PCUT_ASSERT_TRUE(gfx_pix_inside_rect(&coord, &rect));
619
620 coord.x = 3;
621 coord.y = 3;
622 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
623
624 coord.x = 2;
625 coord.y = 4;
626 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
627
628 coord.x = 3;
629 coord.y = 4;
630 PCUT_ASSERT_FALSE(gfx_pix_inside_rect(&coord, &rect));
631}
632
633PCUT_EXPORT(coord);
Note: See TracBrowser for help on using the repository browser.