21#include <winpr/print.h>
23#include <freerdp/codec/region.h>
27 for (
int i = 0; i < nb; i++, src1++, src2++)
32 "expecting rect %d (%" PRIu16
",%" PRIu16
"-%" PRIu16
",%" PRIu16
33 ") and have (%" PRIu16
",%" PRIu16
"-%" PRIu16
",%" PRIu16
")\n",
34 i, src2->left, src2->top, src2->right, src2->bottom, src1->left,
35 src1->top, src1->right, src1->bottom);
43static int test_basic(
void)
45 REGION16 region = WINPR_C_ARRAY_INIT;
52 RECTANGLE_16 r1_r2[] = { { 0, 101, 200, 201 }, { 150, 301, 250, 401 } };
54 region16_init(®ion);
56 if (!region16_union_rect(®ion, ®ion, &r1))
59 rects = region16_rects(®ion, &nbRects);
61 if (!rects || nbRects != 1 || memcmp(rects, &r1,
sizeof(
RECTANGLE_16)) != 0)
65 if (!region16_union_rect(®ion, ®ion, &r2))
68 rects = region16_rects(®ion, &nbRects);
70 if (!rects || nbRects != 2 ||
71 !compareRectangles(rects, r1_r2, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
75 region16_clear(®ion);
76 if (!region16_rects(®ion, &nbRects))
87 region16_uninit(®ion);
91static int test_r1_r3(
void)
99 RECTANGLE_16 r1_r3[] = { { 0, 101, 200, 151 }, { 0, 151, 250, 201 }, { 150, 201, 250, 251 } };
100 region16_init(®ion);
114 if (!region16_union_rect(®ion, ®ion, &r1))
117 if (!region16_union_rect(®ion, ®ion, &r3))
120 rects = region16_rects(®ion, &nbRects);
122 if (!rects || nbRects != 3 ||
123 !compareRectangles(rects, r1_r3, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
127 region16_clear(®ion);
129 if (!region16_union_rect(®ion, ®ion, &r3))
132 if (!region16_union_rect(®ion, ®ion, &r1))
135 rects = region16_rects(®ion, &nbRects);
137 if (!rects || nbRects != 3 ||
138 !compareRectangles(rects, r1_r3, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
143 region16_uninit(®ion);
147static int test_r9_r10(
void)
169 { 200, 0, 300, 100 },
170 { 0, 100, 400, 200 },
171 { 200, 200, 300, 300 },
173 region16_init(®ion);
175 if (!region16_union_rect(®ion, ®ion, &r9))
178 if (!region16_union_rect(®ion, ®ion, &r10))
181 rects = region16_rects(®ion, &nbRects);
183 if (!rects || nbRects != 3 ||
184 !compareRectangles(rects, r9_r10, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
189 region16_uninit(®ion);
193static int test_r1_r5(
void)
201 RECTANGLE_16 r1_r5[] = { { 0, 101, 200, 121 }, { 0, 121, 300, 131 }, { 0, 131, 200, 201 } };
202 region16_init(®ion);
217 if (!region16_union_rect(®ion, ®ion, &r1))
220 if (!region16_union_rect(®ion, ®ion, &r5))
223 rects = region16_rects(®ion, &nbRects);
225 if (!rects || nbRects != 3 ||
226 !compareRectangles(rects, r1_r5, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
231 region16_uninit(®ion);
235static int test_r1_r6(
void)
237 REGION16 region = WINPR_C_ARRAY_INIT;
243 region16_init(®ion);
256 region16_clear(®ion);
258 if (!region16_union_rect(®ion, ®ion, &r1))
261 if (!region16_union_rect(®ion, ®ion, &r6))
264 rects = region16_rects(®ion, &nbRects);
266 if (!rects || nbRects != 1 ||
267 !compareRectangles(rects, &r1, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
272 region16_uninit(®ion);
276static int test_r1_r2_r4(
void)
285 RECTANGLE_16 r1_r2_r4[] = { { 0, 101, 200, 201 }, { 150, 251, 250, 401 } };
303 region16_init(®ion);
305 if (!region16_union_rect(®ion, ®ion, &r1))
308 if (!region16_union_rect(®ion, ®ion, &r2))
311 if (!region16_union_rect(®ion, ®ion, &r4))
314 rects = region16_rects(®ion, &nbRects);
316 if (!rects || nbRects != 2 ||
317 !compareRectangles(rects, r1_r2_r4, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
322 region16_uninit(®ion);
326static int test_r1_r7_r8(
void)
336 { 0, 101, 200, 121 }, { 300, 101, 500, 121 }, { 0, 121, 500, 131 },
337 { 0, 131, 200, 201 }, { 300, 131, 500, 201 },
351 region16_init(®ion);
353 if (!region16_union_rect(®ion, ®ion, &r1))
356 if (!region16_union_rect(®ion, ®ion, &r7))
359 if (!region16_union_rect(®ion, ®ion, &r8))
362 rects = region16_rects(®ion, &nbRects);
364 if (!rects || nbRects != 5 ||
365 !compareRectangles(rects, r1_r7_r8, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
368 region16_clear(®ion);
370 if (!region16_union_rect(®ion, ®ion, &r1))
373 if (!region16_union_rect(®ion, ®ion, &r8))
376 if (!region16_union_rect(®ion, ®ion, &r7))
379 rects = region16_rects(®ion, &nbRects);
381 if (!rects || nbRects != 5 ||
382 !compareRectangles(rects, r1_r7_r8, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
385 region16_clear(®ion);
387 if (!region16_union_rect(®ion, ®ion, &r8))
390 if (!region16_union_rect(®ion, ®ion, &r7))
393 if (!region16_union_rect(®ion, ®ion, &r1))
396 rects = region16_rects(®ion, &nbRects);
398 if (!rects || nbRects != 5 ||
399 !compareRectangles(rects, r1_r7_r8, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
404 region16_uninit(®ion);
408static int test_r1_r2_r3_r4(
void)
419 { 0, 101, 200, 151 }, { 0, 151, 250, 201 }, { 150, 201, 250, 251 }, { 150, 301, 250, 401 }
422 { 0, 151, 250, 201 },
423 { 150, 201, 250, 401 } };
424 region16_init(®ion);
441 if (!region16_union_rect(®ion, ®ion, &r1))
444 if (!region16_union_rect(®ion, ®ion, &r2))
447 if (!region16_union_rect(®ion, ®ion, &r3))
450 rects = region16_rects(®ion, &nbRects);
452 if (!rects || nbRects != 4 || !compareRectangles(rects, r1_r2_r3, 4))
471 if (!region16_union_rect(®ion, ®ion, &r4))
474 rects = region16_rects(®ion, &nbRects);
476 if (!rects || nbRects != 3 || !compareRectangles(rects, r1_r2_r3_r4, 3))
481 region16_uninit(®ion);
485static int test_from_weston(
void)
499 RECTANGLE_16 r1_r2_r3[] = { { 0, 0, 640, 32 }, { 236, 169, 268, 201 }, { 246, 258, 278, 290 } };
500 region16_init(®ion);
517 if (!region16_union_rect(®ion, ®ion, &r1))
520 if (!region16_union_rect(®ion, ®ion, &r2))
523 if (!region16_union_rect(®ion, ®ion, &r3))
526 rects = region16_rects(®ion, &nbRects);
528 if (!rects || nbRects != 3 || !compareRectangles(rects, r1_r2_r3, 3))
533 region16_uninit(®ion);
537static int test_r1_inter_r3(
void)
547 { 150, 151, 200, 201 },
549 region16_init(®ion);
550 region16_init(&intersection);
563 if (!region16_union_rect(®ion, ®ion, &r1))
566 if (!region16_intersects_rect(®ion, &r3))
569 if (!region16_intersect_rect(&intersection, ®ion, &r3))
572 rects = region16_rects(&intersection, &nbRects);
574 if (!rects || nbRects != 1 ||
575 !compareRectangles(rects, r1_inter_r3, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
580 region16_uninit(®ion);
581 region16_uninit(&intersection);
585static int test_r1_r3_inter_r11(
void)
596 { 170, 151, 250, 251 },
598 region16_init(®ion);
599 region16_init(&intersection);
621 if (!region16_union_rect(®ion, ®ion, &r1))
624 if (!region16_union_rect(®ion, ®ion, &r3))
627 if (!region16_intersects_rect(®ion, &r11))
630 if (!region16_intersect_rect(&intersection, ®ion, &r11))
633 rects = region16_rects(&intersection, &nbRects);
635 if (!rects || nbRects != 1 ||
636 !compareRectangles(rects, r1_r3_inter_r11, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
641 region16_uninit(&intersection);
642 region16_uninit(®ion);
646static int test_norbert_case(
void)
653 RECTANGLE_16 inRectangles[5] = { { 1680, 0, 1920, 242 },
654 { 294, 242, 971, 776 },
655 { 1680, 242, 1920, 776 },
656 { 1680, 776, 1920, 1036 },
657 { 2, 1040, 53, 1078 } };
659 RECTANGLE_16 expected_inter_extents = { 2, 0, 1920, 1078 };
660 region16_init(®ion);
661 region16_init(&intersection);
689 for (
int i = 0; i < 5; i++)
691 if (!region16_union_rect(®ion, ®ion, &inRectangles[i]))
695 if (!compareRectangles(region16_extents(®ion), &expected_inter_extents, 1))
698 if (!region16_intersect_rect(&intersection, ®ion, &screenRect))
701 rects = region16_rects(&intersection, &nbRects);
703 if (!rects || nbRects != 5 ||
704 !compareRectangles(rects, inRectangles, WINPR_ASSERTING_INT_CAST(
int, nbRects)))
707 if (!compareRectangles(region16_extents(&intersection), &expected_inter_extents, 1))
712 region16_uninit(&intersection);
713 region16_uninit(®ion);
717static int test_norbert2_case(
void)
725 region16_init(®ion);
727 if (!region16_union_rect(®ion, ®ion, &rect1))
729 (void)fprintf(stderr,
"%s: Error 1 - region16_union_rect failed\n", __func__);
733 if (!(rects = region16_rects(®ion, &nbRects)))
735 (void)fprintf(stderr,
"%s: Error 2 - region16_rects failed\n", __func__);
741 (void)fprintf(stderr,
"%s: Error 3 - expected nbRects == 1 but got %" PRIu32
"\n", __func__,
746 if (!compareRectangles(&rects[0], &rect1, 1))
748 (void)fprintf(stderr,
"%s: Error 4 - compare failed\n", __func__);
752 if (!region16_union_rect(®ion, ®ion, &rect2))
754 (void)fprintf(stderr,
"%s: Error 5 - region16_union_rect failed\n", __func__);
758 if (!(rects = region16_rects(®ion, &nbRects)))
760 (void)fprintf(stderr,
"%s: Error 6 - region16_rects failed\n", __func__);
766 (void)fprintf(stderr,
"%s: Error 7 - expected nbRects == 2 but got %" PRIu32
"\n", __func__,
771 if (!compareRectangles(&rects[0], &rect2, 1))
773 (void)fprintf(stderr,
"%s: Error 8 - compare failed\n", __func__);
777 if (!compareRectangles(&rects[1], &rect1, 1))
779 (void)fprintf(stderr,
"%s: Error 9 - compare failed\n", __func__);
785 region16_uninit(®ion);
789static int test_empty_rectangle(
void)
794 RECTANGLE_16 emptyRectangles[3] = { { 0, 0, 0, 0 }, { 10, 10, 10, 11 }, { 10, 10, 11, 10 } };
798 region16_init(®ion);
799 region16_init(&intersection);
802 for (
int i = 0; i < 3; i++)
804 if (!rectangle_is_empty(&emptyRectangles[i]))
809 if (rectangle_is_empty(&firstRect))
813 if (!region16_union_rect(®ion, ®ion, &firstRect))
816 if (!region16_intersect_rect(®ion, ®ion, &anotherRect))
819 if (!compareRectangles(region16_extents(®ion), &expected_inter_extents, 1))
822 if (!region16_is_empty(®ion))
825 if (!rectangle_is_empty(region16_extents(&intersection)))
830 region16_uninit(&intersection);
831 region16_uninit(®ion);
835typedef int (*TestFunction)(void);
839 WINPR_ATTR_NODISCARD TestFunction func;
842static struct UnitaryTest tests[] = { {
"Basic trivial tests", test_basic },
843 {
"R1+R3 and R3+R1", test_r1_r3 },
844 {
"R1+R5", test_r1_r5 },
845 {
"R1+R6", test_r1_r6 },
846 {
"R9+R10", test_r9_r10 },
847 {
"R1+R2+R4", test_r1_r2_r4 },
848 {
"R1+R7+R8 in many orders", test_r1_r7_r8 },
849 {
"R1+R2+R3+R4", test_r1_r2_r3_r4 },
850 {
"data from weston", test_from_weston },
851 {
"R1 & R3", test_r1_inter_r3 },
852 {
"(R1+R3)&R11 (band merge)", test_r1_r3_inter_r11 },
853 {
"norbert's case", test_norbert_case },
854 {
"norbert's case 2", test_norbert2_case },
855 {
"empty rectangle case", test_empty_rectangle },
857 {
nullptr,
nullptr } };
859int TestFreeRDPRegion(
int argc,
char* argv[])
866 for (
int i = 0; tests[i].func; i++)
869 (void)fprintf(stderr,
"%d: %s\n", testNb, tests[i].name);
870 retCode = tests[i].func();
877 (void)fprintf(stderr,
"failed for test %d\n", testNb);