blob: 8ef61b1605a28c054894750419f770e088729ccd [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#include "GrClip.h"
19#include "GrTDArray.h"
20#include "GrTBSearch.h"
21#include "GrMatrix.h"
bsalomon@google.com6034c502011-02-22 16:37:47 +000022#include "GrRedBlackTree.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
24static void dump(const GrTDArray<int>& array) {
25#if 0
26 for (int i = 0; i < array.count(); i++) {
27 printf(" %d", array[i]);
28 }
29 printf("\n");
30#endif
31}
32
33static void test_tdarray() {
34 GrTDArray<int> array;
bsalomon@google.com6034c502011-02-22 16:37:47 +000035
reed@google.comac10a2d2010-12-22 21:39:39 +000036 *array.append() = 0; dump(array);
37 *array.append() = 2; dump(array);
38 *array.append() = 4; dump(array);
39 *array.append() = 6; dump(array);
40 GrAssert(array.count() == 4);
41
42 *array.insert(0) = -1; dump(array);
43 *array.insert(2) = 1; dump(array);
44 *array.insert(4) = 3; dump(array);
45 *array.insert(7) = 7; dump(array);
46 GrAssert(array.count() == 8);
47 array.remove(3); dump(array);
48 array.remove(0); dump(array);
49 array.removeShuffle(4); dump(array);
50 array.removeShuffle(1); dump(array);
51 GrAssert(array.count() == 4);
52}
53
54static bool LT(const int& elem, int value) {
55 return elem < value;
56}
57static bool EQ(const int& elem, int value) {
58 return elem == value;
59}
60
61static void test_bsearch() {
62 const int array[] = {
63 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
64 };
65
66 for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
67 for (size_t i = 0; i < n; i++) {
68 int index = GrTBSearch<int, int>(array, n, array[i]);
69 GrAssert(index == i);
70 index = GrTBSearch<int, int>(array, n, -array[i]);
71 GrAssert(index < 0);
72 }
73 }
74}
75
76static void dump(const GrClip& clip, const char message[]) {
77 GrPrintf("--- dump clip %s\n", message);
78 GrClipIter iter(clip);
79 while (!iter.isDone()) {
80 GrIRect r;
81 iter.getRect(&r);
82 GrPrintf("--- [%d %d %d %d]\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
83 iter.next();
84 }
85}
86
87static void test_clip() {
88 GrClip clip;
89 GrAssert(clip.isEmpty());
90 GrAssert(!clip.isRect());
91 GrAssert(!clip.isComplex());
92 GrAssert(clip.getBounds().equalsLTRB(0, 0, 0, 0));
93 GrAssert(0 == clip.countRects());
94
95 clip.setRect(GrIRect(10, 10, 10, 10));
96 GrAssert(clip.isEmpty());
97 GrAssert(!clip.isRect());
98 GrAssert(!clip.isComplex());
99 GrAssert(clip.getBounds().equalsLTRB(0, 0, 0, 0));
100 GrAssert(0 == clip.countRects());
101 dump(clip, "empty");
102
103 clip.setRect(GrIRect(10, 10, 20, 20));
104 GrAssert(!clip.isEmpty());
105 GrAssert(clip.isRect());
106 GrAssert(!clip.isComplex());
107 GrAssert(clip.getBounds().equalsLTRB(10, 10, 20, 20));
108 GrAssert(1 == clip.countRects());
109 GrAssert(clip.getRects()[0] == clip.getBounds());
110 dump(clip, "rect");
111
112 clip.addRect(GrIRect(20, 20, 25, 25));
113 GrAssert(!clip.isEmpty());
114 GrAssert(!clip.isRect());
115 GrAssert(clip.isComplex());
116 GrAssert(clip.getBounds().equalsLTRB(10, 10, 25, 25));
117 GrAssert(2 == clip.countRects());
118 dump(clip, "complex");
119
120 GrClip c1(clip);
121 GrAssert(c1 == clip);
122 GrClip c2;
123 GrAssert(c2 != c1);
124 c2 = clip;
125 GrAssert(c2 == clip);
126
127 clip.setEmpty();
128 GrAssert(clip.isEmpty());
129 GrAssert(!clip.isRect());
130 GrAssert(!clip.isComplex());
131 GrAssert(clip.getBounds().equalsLTRB(0, 0, 0, 0));
132
133 GrAssert(c1 != clip);
134 GrAssert(c2 != clip);
135}
136
137void gr_run_unittests() {
138 test_tdarray();
139 test_bsearch();
140 test_clip();
141 GrMatrix::UnitTest();
bsalomon@google.com6034c502011-02-22 16:37:47 +0000142 GrRedBlackTree<int>::UnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000143}
144
145