blob: a9fd40d124c7b8fcc950fea19abb3590f6a02efd [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"
22
23static void dump(const GrTDArray<int>& array) {
24#if 0
25 for (int i = 0; i < array.count(); i++) {
26 printf(" %d", array[i]);
27 }
28 printf("\n");
29#endif
30}
31
32static void test_tdarray() {
33 GrTDArray<int> array;
34
35 *array.append() = 0; dump(array);
36 *array.append() = 2; dump(array);
37 *array.append() = 4; dump(array);
38 *array.append() = 6; dump(array);
39 GrAssert(array.count() == 4);
40
41 *array.insert(0) = -1; dump(array);
42 *array.insert(2) = 1; dump(array);
43 *array.insert(4) = 3; dump(array);
44 *array.insert(7) = 7; dump(array);
45 GrAssert(array.count() == 8);
46 array.remove(3); dump(array);
47 array.remove(0); dump(array);
48 array.removeShuffle(4); dump(array);
49 array.removeShuffle(1); dump(array);
50 GrAssert(array.count() == 4);
51}
52
53static bool LT(const int& elem, int value) {
54 return elem < value;
55}
56static bool EQ(const int& elem, int value) {
57 return elem == value;
58}
59
60static void test_bsearch() {
61 const int array[] = {
62 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
63 };
64
65 for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
66 for (size_t i = 0; i < n; i++) {
67 int index = GrTBSearch<int, int>(array, n, array[i]);
68 GrAssert(index == i);
69 index = GrTBSearch<int, int>(array, n, -array[i]);
70 GrAssert(index < 0);
71 }
72 }
73}
74
75static void dump(const GrClip& clip, const char message[]) {
76 GrPrintf("--- dump clip %s\n", message);
77 GrClipIter iter(clip);
78 while (!iter.isDone()) {
79 GrIRect r;
80 iter.getRect(&r);
81 GrPrintf("--- [%d %d %d %d]\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
82 iter.next();
83 }
84}
85
86static void test_clip() {
87 GrClip clip;
88 GrAssert(clip.isEmpty());
89 GrAssert(!clip.isRect());
90 GrAssert(!clip.isComplex());
91 GrAssert(clip.getBounds().equalsLTRB(0, 0, 0, 0));
92 GrAssert(0 == clip.countRects());
93
94 clip.setRect(GrIRect(10, 10, 10, 10));
95 GrAssert(clip.isEmpty());
96 GrAssert(!clip.isRect());
97 GrAssert(!clip.isComplex());
98 GrAssert(clip.getBounds().equalsLTRB(0, 0, 0, 0));
99 GrAssert(0 == clip.countRects());
100 dump(clip, "empty");
101
102 clip.setRect(GrIRect(10, 10, 20, 20));
103 GrAssert(!clip.isEmpty());
104 GrAssert(clip.isRect());
105 GrAssert(!clip.isComplex());
106 GrAssert(clip.getBounds().equalsLTRB(10, 10, 20, 20));
107 GrAssert(1 == clip.countRects());
108 GrAssert(clip.getRects()[0] == clip.getBounds());
109 dump(clip, "rect");
110
111 clip.addRect(GrIRect(20, 20, 25, 25));
112 GrAssert(!clip.isEmpty());
113 GrAssert(!clip.isRect());
114 GrAssert(clip.isComplex());
115 GrAssert(clip.getBounds().equalsLTRB(10, 10, 25, 25));
116 GrAssert(2 == clip.countRects());
117 dump(clip, "complex");
118
119 GrClip c1(clip);
120 GrAssert(c1 == clip);
121 GrClip c2;
122 GrAssert(c2 != c1);
123 c2 = clip;
124 GrAssert(c2 == clip);
125
126 clip.setEmpty();
127 GrAssert(clip.isEmpty());
128 GrAssert(!clip.isRect());
129 GrAssert(!clip.isComplex());
130 GrAssert(clip.getBounds().equalsLTRB(0, 0, 0, 0));
131
132 GrAssert(c1 != clip);
133 GrAssert(c2 != clip);
134}
135
136void gr_run_unittests() {
137 test_tdarray();
138 test_bsearch();
139 test_clip();
140 GrMatrix::UnitTest();
141}
142
143