1#include "Test.h"
2#include "SkRegion.h"
3#include "SkRandom.h"
4
5static void rand_rect(SkIRect* rect, SkRandom& rand) {
6    int bits = 6;
7    int shift = 32 - bits;
8    rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
9              rand.nextU() >> shift, rand.nextU() >> shift);
10    rect->sort();
11}
12
13static bool test_rects(const SkIRect rect[], int count) {
14    SkRegion rgn0, rgn1;
15
16    for (int i = 0; i < count; i++) {
17        rgn0.op(rect[i], SkRegion::kUnion_Op);
18    }
19    rgn1.setRects(rect, count);
20
21    if (rgn0 != rgn1) {
22        SkDebugf("\n");
23        for (int i = 0; i < count; i++) {
24            SkDebugf(" { %d, %d, %d, %d },\n",
25                     rect[i].fLeft, rect[i].fTop,
26                     rect[i].fRight, rect[i].fBottom);
27        }
28        SkDebugf("\n");
29        return false;
30    }
31    return true;
32}
33
34static void TestRegion(skiatest::Reporter* reporter) {
35    const SkIRect r2[] = {
36        { 0, 0, 1, 1 },
37        { 2, 2, 3, 3 },
38    };
39    REPORTER_ASSERT(reporter, test_rects(r2, SK_ARRAY_COUNT(r2)));
40
41    const SkIRect rects[] = {
42        { 0, 0, 1, 2 },
43        { 2, 1, 3, 3 },
44        { 4, 0, 5, 1 },
45        { 6, 0, 7, 4 },
46    };
47    REPORTER_ASSERT(reporter, test_rects(rects, SK_ARRAY_COUNT(rects)));
48
49    SkRandom rand;
50    for (int i = 0; i < 1000; i++) {
51        SkRegion rgn0, rgn1;
52
53        const int N = 8;
54        SkIRect rect[N];
55        for (int j = 0; j < N; j++) {
56            rand_rect(&rect[j], rand);
57        }
58        REPORTER_ASSERT(reporter, test_rects(rect, N));
59    }
60}
61
62#include "TestClassDef.h"
63DEFINE_TESTCLASS("Region", RegionTestClass, TestRegion)
64