b660fdf0bef409b8296b87ec193aa224fe24789e
[framework/graphics/pixman.git] / test / region-contains-test.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "utils.h"
5
6 static void
7 make_random_region (pixman_region32_t *region)
8 {
9     int n_boxes;
10
11     pixman_region32_init (region);
12
13     n_boxes = lcg_rand_n (64);
14     while (n_boxes--)
15     {
16         int32_t x, y;
17         uint32_t w, h;
18
19         x = (int32_t)lcg_rand_u32() >> 2;
20         y = (int32_t)lcg_rand_u32() >> 2;
21         w = lcg_rand_u32() >> 2;
22         h = lcg_rand_u32() >> 2;
23
24         pixman_region32_union_rect (region, region, x, y, w, h);
25     }
26 }
27
28 static void
29 print_box (pixman_box32_t *box)
30 {
31     printf ("    %d %d %d %d\n", box->x1, box->y1, box->x2, box->y2);
32 }
33
34 static int32_t
35 random_coord (pixman_region32_t *region, pixman_bool_t x)
36 {
37     pixman_box32_t *b, *bb;
38     int n_boxes;
39     int begin, end;
40
41     if (lcg_rand_n (14))
42     {
43         bb = pixman_region32_rectangles (region, &n_boxes);
44         if (n_boxes == 0)
45             goto use_extent;
46         b = bb + lcg_rand_n (n_boxes);
47     }
48     else
49     {
50     use_extent:
51         b = pixman_region32_extents (region);
52         n_boxes = 1;
53     }
54
55     if (x)
56     {
57         begin = b->x1;
58         end = b->x2;
59     }
60     else
61     {
62         begin = b->y1;
63         end = b->y2;
64     }
65
66     switch (lcg_rand_n (5))
67     {
68     case 0:
69         return begin - lcg_rand_u32();
70     case 1:
71         return end + lcg_rand_u32 ();
72     case 2:
73         return end;
74     case 3:
75         return begin;
76     default:
77         return (begin + end) / 2;
78     }
79     return 0;
80 }
81
82 static uint32_t
83 compute_crc32_u32 (uint32_t crc32, uint32_t v)
84 {
85     if (!is_little_endian())
86     {
87         v = ((v & 0xff000000) >> 24)    |
88             ((v & 0x00ff0000) >> 8)     |
89             ((v & 0x0000ff00) << 8)     |
90             ((v & 0x000000ff) << 24);
91     }
92
93     return compute_crc32 (crc32, &v, sizeof (int32_t));
94 }
95
96 static uint32_t
97 crc32_box32 (uint32_t crc32, pixman_box32_t *box)
98 {
99     crc32 = compute_crc32_u32 (crc32, box->x1);
100     crc32 = compute_crc32_u32 (crc32, box->y1);
101     crc32 = compute_crc32_u32 (crc32, box->x2);
102     crc32 = compute_crc32_u32 (crc32, box->y2);
103
104     return crc32;
105 }
106
107 static uint32_t
108 test_region_contains_rectangle (int i, int verbose)
109 {
110     pixman_box32_t box;
111     pixman_box32_t rbox = { 0, 0, 0, 0 };
112     pixman_region32_t region;
113     uint32_t r, r1, r2, r3, r4, crc32;
114
115     lcg_srand (i);
116
117     make_random_region (&region);
118
119     box.x1 = random_coord (&region, TRUE);
120     box.x2 = box.x1 + lcg_rand_u32 ();
121     box.y1 = random_coord (&region, FALSE);
122     box.y2 = box.y1 + lcg_rand_u32 ();
123
124     if (verbose)
125     {
126         int n_rects;
127         pixman_box32_t *boxes;
128
129         boxes = pixman_region32_rectangles (&region, &n_rects);
130
131         printf ("region:\n");
132         while (n_rects--)
133             print_box (boxes++);
134         printf ("box:\n");
135         print_box (&box);
136     }
137
138     crc32 = 0;
139
140     r1 = pixman_region32_contains_point (&region, box.x1, box.y1, &rbox);
141     crc32 = crc32_box32 (crc32, &rbox);
142     r2 = pixman_region32_contains_point (&region, box.x1, box.y2, &rbox);
143     crc32 = crc32_box32 (crc32, &rbox);
144     r3 = pixman_region32_contains_point (&region, box.x2, box.y1, &rbox);
145     crc32 = crc32_box32 (crc32, &rbox);
146     r4 = pixman_region32_contains_point (&region, box.x2, box.y2, &rbox);
147     crc32 = crc32_box32 (crc32, &rbox);
148
149     r = pixman_region32_contains_rectangle (&region, &box);
150     r = (i << 8) | (r << 4) | (r1 << 3) | (r2 << 2) | (r3 << 1) | (r4 << 0);
151
152     crc32 = compute_crc32_u32 (crc32, r);
153
154     if (verbose)
155         printf ("results: %d %d %d %d %d\n", (r & 0xf0) >> 4, r1, r2, r3, r4);
156
157     pixman_region32_fini (&region);
158
159     return crc32;
160 }
161
162 int
163 main (int argc, const char *argv[])
164 {
165     return fuzzer_test_main ("region_contains",
166                              1000000,
167                              0xD7C297CC,
168                              test_region_contains_rectangle,
169                              argc, argv);
170 }