Home | History | Annotate | Download | only in tests
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "Test.h"
      9 #include "SkRegion.h"
     10 #include "SkRandom.h"
     11 
     12 static void rand_rect(SkIRect* rect, SkRandom& rand) {
     13     int bits = 6;
     14     int shift = 32 - bits;
     15     rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
     16               rand.nextU() >> shift, rand.nextU() >> shift);
     17     rect->sort();
     18 }
     19 
     20 static bool test_rects(const SkIRect rect[], int count) {
     21     SkRegion rgn0, rgn1;
     22 
     23     for (int i = 0; i < count; i++) {
     24         rgn0.op(rect[i], SkRegion::kUnion_Op);
     25     }
     26     rgn1.setRects(rect, count);
     27 
     28     if (rgn0 != rgn1) {
     29         SkDebugf("\n");
     30         for (int i = 0; i < count; i++) {
     31             SkDebugf(" { %d, %d, %d, %d },\n",
     32                      rect[i].fLeft, rect[i].fTop,
     33                      rect[i].fRight, rect[i].fBottom);
     34         }
     35         SkDebugf("\n");
     36         return false;
     37     }
     38     return true;
     39 }
     40 
     41 static void TestRegion(skiatest::Reporter* reporter) {
     42     const SkIRect r2[] = {
     43         { 0, 0, 1, 1 },
     44         { 2, 2, 3, 3 },
     45     };
     46     REPORTER_ASSERT(reporter, test_rects(r2, SK_ARRAY_COUNT(r2)));
     47 
     48     const SkIRect rects[] = {
     49         { 0, 0, 1, 2 },
     50         { 2, 1, 3, 3 },
     51         { 4, 0, 5, 1 },
     52         { 6, 0, 7, 4 },
     53     };
     54     REPORTER_ASSERT(reporter, test_rects(rects, SK_ARRAY_COUNT(rects)));
     55 
     56     SkRandom rand;
     57     for (int i = 0; i < 1000; i++) {
     58         SkRegion rgn0, rgn1;
     59 
     60         const int N = 8;
     61         SkIRect rect[N];
     62         for (int j = 0; j < N; j++) {
     63             rand_rect(&rect[j], rand);
     64         }
     65         REPORTER_ASSERT(reporter, test_rects(rect, N));
     66     }
     67 }
     68 
     69 #include "TestClassDef.h"
     70 DEFINE_TESTCLASS("Region", RegionTestClass, TestRegion)
     71