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

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

Computing rectangle envelope

Useful for tracking changed areas, for example.

  • Property mode set to 100644
File size: 7.7 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
36extern void gfx_coord2_add(gfx_coord2_t *, gfx_coord2_t *, gfx_coord2_t *);
37extern void gfx_coord2_subtract(gfx_coord2_t *, gfx_coord2_t *, gfx_coord2_t *);
38extern void gfx_rect_translate(gfx_coord2_t *, gfx_rect_t *, gfx_rect_t *);
39
40/** gfx_coord2_add should add two coordinate vectors */
41PCUT_TEST(coord2_add)
42{
43 gfx_coord2_t a, b;
44 gfx_coord2_t d;
45
46 a.x = 10;
47 a.y = 11;
48 b.x = 20;
49 b.y = 22;
50
51 gfx_coord2_add(&a, &b, &d);
52
53 PCUT_ASSERT_EQUALS(a.x + b.x, d.x);
54 PCUT_ASSERT_EQUALS(a.y + b.y, d.y);
55}
56
57/** gfx_coord2_subtract should subtract two coordinate vectors */
58PCUT_TEST(coord2_subtract)
59{
60 gfx_coord2_t a, b;
61 gfx_coord2_t d;
62
63 a.x = 10;
64 a.y = 11;
65 b.x = 20;
66 b.y = 22;
67
68 gfx_coord2_subtract(&a, &b, &d);
69
70 PCUT_ASSERT_EQUALS(a.x - b.x, d.x);
71 PCUT_ASSERT_EQUALS(a.y - b.y, d.y);
72}
73
74/** gfx_rect_translate should translate rectangle */
75PCUT_TEST(rect_translate)
76{
77 gfx_coord2_t offs;
78 gfx_rect_t srect;
79 gfx_rect_t drect;
80
81 offs.x = 5;
82 offs.y = 6;
83
84 srect.p0.x = 10;
85 srect.p0.y = 11;
86 srect.p1.x = 20;
87 srect.p1.y = 22;
88
89 gfx_rect_translate(&offs, &srect, &drect);
90
91 PCUT_ASSERT_EQUALS(offs.x + srect.p0.x, drect.p0.x);
92 PCUT_ASSERT_EQUALS(offs.y + srect.p0.y, drect.p0.y);
93 PCUT_ASSERT_EQUALS(offs.x + srect.p1.x, drect.p1.x);
94 PCUT_ASSERT_EQUALS(offs.y + srect.p1.y, drect.p1.y);
95}
96
97/** Sorting span with lower start and higher end point results in the same span. */
98PCUT_TEST(span_points_sort_asc)
99{
100 gfx_coord_t a, b;
101
102 gfx_span_points_sort(1, 2, &a, &b);
103 PCUT_ASSERT_EQUALS(1, a);
104 PCUT_ASSERT_EQUALS(2, b);
105}
106
107/** Sorting span with same start and end point results in the same span. */
108PCUT_TEST(span_points_sort_equal)
109{
110 gfx_coord_t a, b;
111
112 gfx_span_points_sort(1, 1, &a, &b);
113 PCUT_ASSERT_EQUALS(1, a);
114 PCUT_ASSERT_EQUALS(1, b);
115}
116
117/** Sorting span with hight start and lower end point results in transposed span. */
118PCUT_TEST(span_points_sort_decs)
119{
120 gfx_coord_t a, b;
121
122 gfx_span_points_sort(1, 0, &a, &b);
123 PCUT_ASSERT_EQUALS(1, a);
124 PCUT_ASSERT_EQUALS(2, b);
125}
126
127/** Rectangle envelope with first rectangle empty should return the second rectangle. */
128PCUT_TEST(rect_envelope_a_empty)
129{
130 gfx_rect_t a;
131 gfx_rect_t b;
132 gfx_rect_t e;
133
134 a.p0.x = 0;
135 a.p0.y = 0;
136 a.p1.x = 0;
137 a.p1.y = 0;
138
139 b.p0.x = 1;
140 b.p0.y = 2;
141 b.p1.x = 3;
142 b.p1.y = 4;
143
144 gfx_rect_envelope(&a, &b, &e);
145 PCUT_ASSERT_EQUALS(1, e.p0.x);
146 PCUT_ASSERT_EQUALS(2, e.p0.y);
147 PCUT_ASSERT_EQUALS(3, e.p1.x);
148 PCUT_ASSERT_EQUALS(4, e.p1.y);
149}
150
151/** Rectangle envelope with second rectangle empty should return the first rectangle. */
152PCUT_TEST(rect_envelope_b_empty)
153{
154 gfx_rect_t a;
155 gfx_rect_t b;
156 gfx_rect_t e;
157
158 a.p0.x = 1;
159 a.p0.y = 2;
160 a.p1.x = 3;
161 a.p1.y = 4;
162
163 b.p0.x = 0;
164 b.p0.y = 0;
165 b.p1.x = 0;
166 b.p1.y = 0;
167
168 gfx_rect_envelope(&a, &b, &e);
169 PCUT_ASSERT_EQUALS(1, e.p0.x);
170 PCUT_ASSERT_EQUALS(2, e.p0.y);
171 PCUT_ASSERT_EQUALS(3, e.p1.x);
172 PCUT_ASSERT_EQUALS(4, e.p1.y);
173}
174
175/** Rectangle envelope, a has both coordinates lower than b */
176PCUT_TEST(rect_envelope_nonempty_a_lt_b)
177{
178 gfx_rect_t a;
179 gfx_rect_t b;
180 gfx_rect_t e;
181
182 a.p0.x = 1;
183 a.p0.y = 2;
184 a.p1.x = 3;
185 a.p1.y = 4;
186
187 b.p0.x = 5;
188 b.p0.y = 6;
189 b.p1.x = 7;
190 b.p1.y = 8;
191
192 gfx_rect_envelope(&a, &b, &e);
193 PCUT_ASSERT_EQUALS(1, e.p0.x);
194 PCUT_ASSERT_EQUALS(2, e.p0.y);
195 PCUT_ASSERT_EQUALS(7, e.p1.x);
196 PCUT_ASSERT_EQUALS(8, e.p1.y);
197}
198
199/** Rectangle envelope, a has both coordinates higher than b */
200PCUT_TEST(rect_envelope_nonempty_a_gt_b)
201{
202 gfx_rect_t a;
203 gfx_rect_t b;
204 gfx_rect_t e;
205
206 a.p0.x = 5;
207 a.p0.y = 6;
208 a.p1.x = 7;
209 a.p1.y = 8;
210
211 b.p0.x = 1;
212 b.p0.y = 2;
213 b.p1.x = 3;
214 b.p1.y = 4;
215
216 gfx_rect_envelope(&a, &b, &e);
217 PCUT_ASSERT_EQUALS(1, e.p0.x);
218 PCUT_ASSERT_EQUALS(2, e.p0.y);
219 PCUT_ASSERT_EQUALS(7, e.p1.x);
220 PCUT_ASSERT_EQUALS(8, e.p1.y);
221}
222
223/** Rectangle envelope, a is inside b */
224PCUT_TEST(rect_envelope_nonempty_a_inside_b)
225{
226 gfx_rect_t a;
227 gfx_rect_t b;
228 gfx_rect_t e;
229
230 a.p0.x = 1;
231 a.p0.y = 2;
232 a.p1.x = 7;
233 a.p1.y = 8;
234
235 b.p0.x = 3;
236 b.p0.y = 4;
237 b.p1.x = 5;
238 b.p1.y = 6;
239
240 gfx_rect_envelope(&a, &b, &e);
241 PCUT_ASSERT_EQUALS(1, e.p0.x);
242 PCUT_ASSERT_EQUALS(2, e.p0.y);
243 PCUT_ASSERT_EQUALS(7, e.p1.x);
244 PCUT_ASSERT_EQUALS(8, e.p1.y);
245}
246
247/** Rectangle envelope, b is inside a*/
248PCUT_TEST(rect_envelope_nonempty_b_inside_a)
249{
250 gfx_rect_t a;
251 gfx_rect_t b;
252 gfx_rect_t e;
253
254 a.p0.x = 3;
255 a.p0.y = 4;
256 a.p1.x = 5;
257 a.p1.y = 6;
258
259 b.p0.x = 1;
260 b.p0.y = 2;
261 b.p1.x = 7;
262 b.p1.y = 8;
263
264 gfx_rect_envelope(&a, &b, &e);
265 PCUT_ASSERT_EQUALS(1, e.p0.x);
266 PCUT_ASSERT_EQUALS(2, e.p0.y);
267 PCUT_ASSERT_EQUALS(7, e.p1.x);
268 PCUT_ASSERT_EQUALS(8, e.p1.y);
269}
270
271/** Rectangle envelope, a and b cross */
272PCUT_TEST(rect_envelope_nonempty_a_crosses_b)
273{
274 gfx_rect_t a;
275 gfx_rect_t b;
276 gfx_rect_t e;
277
278 a.p0.x = 1;
279 a.p0.y = 2;
280 a.p1.x = 4;
281 a.p1.y = 3;
282
283 b.p0.x = 2;
284 b.p0.y = 1;
285 b.p1.x = 3;
286 b.p1.y = 4;
287
288 gfx_rect_envelope(&a, &b, &e);
289 PCUT_ASSERT_EQUALS(1, e.p0.x);
290 PCUT_ASSERT_EQUALS(1, e.p0.y);
291 PCUT_ASSERT_EQUALS(4, e.p1.x);
292 PCUT_ASSERT_EQUALS(4, e.p1.y);
293}
294
295/** Sort span points that are already sorted should produde indentical points */
296PCUT_TEST(rect_points_sort_sorted)
297{
298 gfx_coord_t s0, s1;
299
300 gfx_span_points_sort(1, 2, &s0, &s1);
301 PCUT_ASSERT_EQUALS(1, s0);
302 PCUT_ASSERT_EQUALS(2, s1);
303}
304
305/** Sort span points that are reversed should transpose them */
306PCUT_TEST(rect_points_sort_reversed)
307{
308 gfx_coord_t s0, s1;
309
310 gfx_span_points_sort(2, 1, &s0, &s1);
311 PCUT_ASSERT_EQUALS(2, s0);
312 PCUT_ASSERT_EQUALS(3, s1);
313}
314
315/** gfx_rect_is_empty for straight rectangle with zero columns returns true */
316PCUT_TEST(rect_is_empty_pos_x)
317{
318 gfx_rect_t rect;
319
320 rect.p0.x = 1;
321 rect.p0.y = 2;
322 rect.p1.x = 1;
323 rect.p1.y = 3;
324 PCUT_ASSERT_TRUE(gfx_rect_is_empty(&rect));
325}
326
327/** gfx_rect_is_empty for straight rectangle with zero rows returns true */
328PCUT_TEST(rect_is_empty_pos_y)
329{
330 gfx_rect_t rect;
331
332 rect.p0.x = 1;
333 rect.p0.y = 2;
334 rect.p1.x = 2;
335 rect.p1.y = 2;
336 PCUT_ASSERT_TRUE(gfx_rect_is_empty(&rect));
337}
338
339/** gfx_rect_is_empty for staright non-empty rectangle returns false */
340PCUT_TEST(rect_is_empty_neg)
341{
342 gfx_rect_t rect;
343
344 rect.p0.x = 1;
345 rect.p0.y = 2;
346 rect.p1.x = 2;
347 rect.p1.y = 3;
348 PCUT_ASSERT_FALSE(gfx_rect_is_empty(&rect));
349}
350
351/** gfx_rect_is_empty for reverse non-empty rectangle returns false */
352PCUT_TEST(rect_is_empty_reverse_neg)
353{
354 gfx_rect_t rect;
355
356 rect.p0.x = 1;
357 rect.p0.y = 2;
358 rect.p1.x = 0;
359 rect.p1.y = 1;
360 PCUT_ASSERT_FALSE(gfx_rect_is_empty(&rect));
361}
362
363PCUT_EXPORT(coord);
Note: See TracBrowser for help on using the repository browser.