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

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

gfx_rect_clip, gfx_rect_rtranslate

  • Property mode set to 100644
File size: 11.0 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_rect_translate should translate rectangle */
71PCUT_TEST(rect_translate)
72{
73 gfx_coord2_t offs;
74 gfx_rect_t srect;
75 gfx_rect_t drect;
76
77 offs.x = 5;
78 offs.y = 6;
79
80 srect.p0.x = 10;
81 srect.p0.y = 11;
82 srect.p1.x = 20;
83 srect.p1.y = 22;
84
85 gfx_rect_translate(&offs, &srect, &drect);
86
87 PCUT_ASSERT_INT_EQUALS(offs.x + srect.p0.x, drect.p0.x);
88 PCUT_ASSERT_INT_EQUALS(offs.y + srect.p0.y, drect.p0.y);
89 PCUT_ASSERT_INT_EQUALS(offs.x + srect.p1.x, drect.p1.x);
90 PCUT_ASSERT_INT_EQUALS(offs.y + srect.p1.y, drect.p1.y);
91}
92
93/** gfx_rect_rtranslate should reverse-translate rectangle */
94PCUT_TEST(rect_rtranslate)
95{
96 gfx_coord2_t offs;
97 gfx_rect_t srect;
98 gfx_rect_t drect;
99
100 offs.x = 5;
101 offs.y = 6;
102
103 srect.p0.x = 10;
104 srect.p0.y = 11;
105 srect.p1.x = 20;
106 srect.p1.y = 22;
107
108 gfx_rect_rtranslate(&offs, &srect, &drect);
109
110 PCUT_ASSERT_INT_EQUALS(srect.p0.x - offs.x, drect.p0.x);
111 PCUT_ASSERT_INT_EQUALS(srect.p0.y - offs.y, drect.p0.y);
112 PCUT_ASSERT_INT_EQUALS(srect.p1.x - offs.x, drect.p1.x);
113 PCUT_ASSERT_INT_EQUALS(srect.p1.y - offs.y, drect.p1.y);
114}
115
116/** Sorting span with lower start and higher end point results in the same span. */
117PCUT_TEST(span_points_sort_asc)
118{
119 gfx_coord_t a, b;
120
121 gfx_span_points_sort(1, 2, &a, &b);
122 PCUT_ASSERT_INT_EQUALS(1, a);
123 PCUT_ASSERT_INT_EQUALS(2, b);
124}
125
126/** Sorting span with same start and end point results in the same span. */
127PCUT_TEST(span_points_sort_equal)
128{
129 gfx_coord_t a, b;
130
131 gfx_span_points_sort(1, 1, &a, &b);
132 PCUT_ASSERT_INT_EQUALS(1, a);
133 PCUT_ASSERT_INT_EQUALS(1, b);
134}
135
136/** Sorting span with hight start and lower end point results in transposed span. */
137PCUT_TEST(span_points_sort_decs)
138{
139 gfx_coord_t a, b;
140
141 gfx_span_points_sort(1, 0, &a, &b);
142 PCUT_ASSERT_INT_EQUALS(1, a);
143 PCUT_ASSERT_INT_EQUALS(2, b);
144}
145
146/** Rectangle envelope with first rectangle empty should return the second rectangle. */
147PCUT_TEST(rect_envelope_a_empty)
148{
149 gfx_rect_t a;
150 gfx_rect_t b;
151 gfx_rect_t e;
152
153 a.p0.x = 0;
154 a.p0.y = 0;
155 a.p1.x = 0;
156 a.p1.y = 0;
157
158 b.p0.x = 1;
159 b.p0.y = 2;
160 b.p1.x = 3;
161 b.p1.y = 4;
162
163 gfx_rect_envelope(&a, &b, &e);
164 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
165 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
166 PCUT_ASSERT_INT_EQUALS(3, e.p1.x);
167 PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
168}
169
170/** Rectangle envelope with second rectangle empty should return the first rectangle. */
171PCUT_TEST(rect_envelope_b_empty)
172{
173 gfx_rect_t a;
174 gfx_rect_t b;
175 gfx_rect_t e;
176
177 a.p0.x = 1;
178 a.p0.y = 2;
179 a.p1.x = 3;
180 a.p1.y = 4;
181
182 b.p0.x = 0;
183 b.p0.y = 0;
184 b.p1.x = 0;
185 b.p1.y = 0;
186
187 gfx_rect_envelope(&a, &b, &e);
188 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
189 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
190 PCUT_ASSERT_INT_EQUALS(3, e.p1.x);
191 PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
192}
193
194/** Rectangle envelope, a has both coordinates lower than b */
195PCUT_TEST(rect_envelope_nonempty_a_lt_b)
196{
197 gfx_rect_t a;
198 gfx_rect_t b;
199 gfx_rect_t e;
200
201 a.p0.x = 1;
202 a.p0.y = 2;
203 a.p1.x = 3;
204 a.p1.y = 4;
205
206 b.p0.x = 5;
207 b.p0.y = 6;
208 b.p1.x = 7;
209 b.p1.y = 8;
210
211 gfx_rect_envelope(&a, &b, &e);
212 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
213 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
214 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
215 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
216}
217
218/** Rectangle envelope, a has both coordinates higher than b */
219PCUT_TEST(rect_envelope_nonempty_a_gt_b)
220{
221 gfx_rect_t a;
222 gfx_rect_t b;
223 gfx_rect_t e;
224
225 a.p0.x = 5;
226 a.p0.y = 6;
227 a.p1.x = 7;
228 a.p1.y = 8;
229
230 b.p0.x = 1;
231 b.p0.y = 2;
232 b.p1.x = 3;
233 b.p1.y = 4;
234
235 gfx_rect_envelope(&a, &b, &e);
236 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
237 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
238 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
239 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
240}
241
242/** Rectangle envelope, a is inside b */
243PCUT_TEST(rect_envelope_nonempty_a_inside_b)
244{
245 gfx_rect_t a;
246 gfx_rect_t b;
247 gfx_rect_t e;
248
249 a.p0.x = 1;
250 a.p0.y = 2;
251 a.p1.x = 7;
252 a.p1.y = 8;
253
254 b.p0.x = 3;
255 b.p0.y = 4;
256 b.p1.x = 5;
257 b.p1.y = 6;
258
259 gfx_rect_envelope(&a, &b, &e);
260 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
261 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
262 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
263 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
264}
265
266/** Rectangle envelope, b is inside a*/
267PCUT_TEST(rect_envelope_nonempty_b_inside_a)
268{
269 gfx_rect_t a;
270 gfx_rect_t b;
271 gfx_rect_t e;
272
273 a.p0.x = 3;
274 a.p0.y = 4;
275 a.p1.x = 5;
276 a.p1.y = 6;
277
278 b.p0.x = 1;
279 b.p0.y = 2;
280 b.p1.x = 7;
281 b.p1.y = 8;
282
283 gfx_rect_envelope(&a, &b, &e);
284 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
285 PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
286 PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
287 PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
288}
289
290/** Rectangle envelope, a and b cross */
291PCUT_TEST(rect_envelope_nonempty_a_crosses_b)
292{
293 gfx_rect_t a;
294 gfx_rect_t b;
295 gfx_rect_t e;
296
297 a.p0.x = 1;
298 a.p0.y = 2;
299 a.p1.x = 4;
300 a.p1.y = 3;
301
302 b.p0.x = 2;
303 b.p0.y = 1;
304 b.p1.x = 3;
305 b.p1.y = 4;
306
307 gfx_rect_envelope(&a, &b, &e);
308 PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
309 PCUT_ASSERT_INT_EQUALS(1, e.p0.y);
310 PCUT_ASSERT_INT_EQUALS(4, e.p1.x);
311 PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
312}
313
314/** Clip rectangle with rect completely inside the clipping rectangle */
315PCUT_TEST(rect_clip_rect_inside)
316{
317 gfx_rect_t rect;
318 gfx_rect_t clip;
319 gfx_rect_t dest;
320
321 rect.p0.x = 3;
322 rect.p0.y = 4;
323 rect.p1.x = 5;
324 rect.p1.y = 6;
325
326 clip.p0.x = 1;
327 clip.p0.y = 2;
328 clip.p1.x = 7;
329 clip.p1.y = 8;
330
331 gfx_rect_clip(&rect, &clip, &dest);
332 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
333 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
334 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
335 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
336}
337
338/** Clip rectangle with rect covering the clipping rectangle */
339PCUT_TEST(rect_clip_rect_covering)
340{
341 gfx_rect_t rect;
342 gfx_rect_t clip;
343 gfx_rect_t dest;
344
345 rect.p0.x = 1;
346 rect.p0.y = 2;
347 rect.p1.x = 7;
348 rect.p1.y = 8;
349
350 clip.p0.x = 3;
351 clip.p0.y = 4;
352 clip.p1.x = 5;
353 clip.p1.y = 6;
354
355 gfx_rect_clip(&rect, &clip, &dest);
356 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
357 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
358 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
359 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
360}
361
362/** Clip rectangle with rect outside, having lower coordinates */
363PCUT_TEST(rect_clip_rect_out_ll)
364{
365 gfx_rect_t rect;
366 gfx_rect_t clip;
367 gfx_rect_t dest;
368
369 rect.p0.x = 1;
370 rect.p0.y = 2;
371 rect.p1.x = 3;
372 rect.p1.y = 4;
373
374 clip.p0.x = 5;
375 clip.p0.y = 6;
376 clip.p1.x = 7;
377 clip.p1.y = 8;
378
379 gfx_rect_clip(&rect, &clip, &dest);
380 PCUT_ASSERT_INT_EQUALS(5, dest.p0.x);
381 PCUT_ASSERT_INT_EQUALS(6, dest.p0.y);
382 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
383 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
384}
385
386/** Clip rectangle with rect outside, having higher coordinates */
387PCUT_TEST(rect_clip_rect_out_hh)
388{
389 gfx_rect_t rect;
390 gfx_rect_t clip;
391 gfx_rect_t dest;
392
393 rect.p0.x = 5;
394 rect.p0.y = 6;
395 rect.p1.x = 7;
396 rect.p1.y = 8;
397
398 clip.p0.x = 1;
399 clip.p0.y = 2;
400 clip.p1.x = 3;
401 clip.p1.y = 4;
402
403 gfx_rect_clip(&rect, &clip, &dest);
404 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
405 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
406 PCUT_ASSERT_INT_EQUALS(3, dest.p1.x);
407 PCUT_ASSERT_INT_EQUALS(4, dest.p1.y);
408}
409
410/** Clip rectangle with rect partially outside, having lower coordinates */
411PCUT_TEST(rect_clip_rect_ll)
412{
413 gfx_rect_t rect;
414 gfx_rect_t clip;
415 gfx_rect_t dest;
416
417 rect.p0.x = 1;
418 rect.p0.y = 2;
419 rect.p1.x = 5;
420 rect.p1.y = 6;
421
422 clip.p0.x = 3;
423 clip.p0.y = 4;
424 clip.p1.x = 7;
425 clip.p1.y = 8;
426
427 gfx_rect_clip(&rect, &clip, &dest);
428 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
429 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
430 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
431 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
432}
433
434/** Clip rectangle with rect partially outside, having higher coordinates */
435PCUT_TEST(rect_clip_rect_hh)
436{
437 gfx_rect_t rect;
438 gfx_rect_t clip;
439 gfx_rect_t dest;
440
441 rect.p0.x = 3;
442 rect.p0.y = 4;
443 rect.p1.x = 7;
444 rect.p1.y = 8;
445
446 clip.p0.x = 1;
447 clip.p0.y = 2;
448 clip.p1.x = 5;
449 clip.p1.y = 6;
450
451 gfx_rect_clip(&rect, &clip, &dest);
452 PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
453 PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
454 PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
455 PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
456}
457
458/** Sort span points that are already sorted should produde indentical points */
459PCUT_TEST(rect_points_sort_sorted)
460{
461 gfx_coord_t s0, s1;
462
463 gfx_span_points_sort(1, 2, &s0, &s1);
464 PCUT_ASSERT_INT_EQUALS(1, s0);
465 PCUT_ASSERT_INT_EQUALS(2, s1);
466}
467
468/** Sort span points that are reversed should transpose them */
469PCUT_TEST(rect_points_sort_reversed)
470{
471 gfx_coord_t s0, s1;
472
473 gfx_span_points_sort(2, 1, &s0, &s1);
474 PCUT_ASSERT_INT_EQUALS(2, s0);
475 PCUT_ASSERT_INT_EQUALS(3, s1);
476}
477
478/** gfx_rect_is_empty for straight rectangle with zero columns returns true */
479PCUT_TEST(rect_is_empty_pos_x)
480{
481 gfx_rect_t rect;
482
483 rect.p0.x = 1;
484 rect.p0.y = 2;
485 rect.p1.x = 1;
486 rect.p1.y = 3;
487 PCUT_ASSERT_TRUE(gfx_rect_is_empty(&rect));
488}
489
490/** gfx_rect_is_empty for straight rectangle with zero rows returns true */
491PCUT_TEST(rect_is_empty_pos_y)
492{
493 gfx_rect_t rect;
494
495 rect.p0.x = 1;
496 rect.p0.y = 2;
497 rect.p1.x = 2;
498 rect.p1.y = 2;
499 PCUT_ASSERT_TRUE(gfx_rect_is_empty(&rect));
500}
501
502/** gfx_rect_is_empty for staright non-empty rectangle returns false */
503PCUT_TEST(rect_is_empty_neg)
504{
505 gfx_rect_t rect;
506
507 rect.p0.x = 1;
508 rect.p0.y = 2;
509 rect.p1.x = 2;
510 rect.p1.y = 3;
511 PCUT_ASSERT_FALSE(gfx_rect_is_empty(&rect));
512}
513
514/** gfx_rect_is_empty for reverse non-empty rectangle returns false */
515PCUT_TEST(rect_is_empty_reverse_neg)
516{
517 gfx_rect_t rect;
518
519 rect.p0.x = 1;
520 rect.p0.y = 2;
521 rect.p1.x = 0;
522 rect.p1.y = 1;
523 PCUT_ASSERT_FALSE(gfx_rect_is_empty(&rect));
524}
525
526PCUT_EXPORT(coord);
Note: See TracBrowser for help on using the repository browser.