OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Test.h" | 8 #include "Test.h" |
9 #include "TestClassDef.h" | 9 #include "TestClassDef.h" |
10 #include "SkRandom.h" | 10 #include "SkRandom.h" |
11 #include <math.h> | 11 #include <math.h> |
12 | 12 |
13 struct BoolTable { | 13 struct BoolTable { |
14 int8_t zero, pos, neg, toBool, sign; | 14 int8_t zero, pos, neg, toBool, sign; |
15 }; | 15 }; |
16 | 16 |
17 static void bool_table_test(skiatest::Reporter* reporter, | 17 static void bool_table_test(skiatest::Reporter* reporter, |
18 const Sk64& a, const BoolTable& table) | 18 const Sk64& a, const BoolTable& table) |
19 { | 19 { |
20 REPORTER_ASSERT(reporter, a.isZero() != a.nonZero()); | 20 REPORTER_ASSERT(reporter, a.isZero() != a.nonZero()); |
21 | 21 |
22 REPORTER_ASSERT(reporter, !a.isZero() == !table.zero); | 22 REPORTER_ASSERT(reporter, !a.isZero() == !table.zero); |
23 REPORTER_ASSERT(reporter, !a.isPos() == !table.pos); | 23 REPORTER_ASSERT(reporter, !a.isPos() == !table.pos); |
24 REPORTER_ASSERT(reporter, !a.isNeg() == !table.neg); | 24 REPORTER_ASSERT(reporter, !a.isNeg() == !table.neg); |
25 REPORTER_ASSERT(reporter, a.getSign() == table.sign); | 25 REPORTER_ASSERT(reporter, a.getSign() == table.sign); |
26 } | 26 } |
27 | 27 |
28 void Sk64::UnitTestWithReporter(void* reporterParam) { | 28 #ifdef SkLONGLONG |
29 skiatest::Reporter* reporter = (skiatest::Reporter*)reporterParam; | 29 static SkLONGLONG asLL(const Sk64& a) |
| 30 { |
| 31 return ((SkLONGLONG)a.fHi << 32) | a.fLo; |
| 32 } |
| 33 #endif |
30 | 34 |
| 35 DEF_TEST(Sk64Test, reporter) { |
31 enum BoolTests { | 36 enum BoolTests { |
32 kZero_BoolTest, | 37 kZero_BoolTest, |
33 kPos_BoolTest, | 38 kPos_BoolTest, |
34 kNeg_BoolTest | 39 kNeg_BoolTest |
35 }; | 40 }; |
36 static const BoolTable gBoolTable[] = { | 41 static const BoolTable gBoolTable[] = { |
37 { 1, 0, 0, 0, 0 }, | 42 { 1, 0, 0, 0, 0 }, |
38 { 0, 1, 0, 1, 1 }, | 43 { 0, 1, 0, 1, 1 }, |
39 { 0, 0, 1, 1, -1 } | 44 { 0, 0, 1, 1, -1 } |
40 }; | 45 }; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 c = a; c.add(bb); | 89 c = a; c.add(bb); |
85 REPORTER_ASSERT(reporter, c.get32() == aa + bb); | 90 REPORTER_ASSERT(reporter, c.get32() == aa + bb); |
86 c = a; c.add(-bb); | 91 c = a; c.add(-bb); |
87 REPORTER_ASSERT(reporter, c.get32() == aa - bb); | 92 REPORTER_ASSERT(reporter, c.get32() == aa - bb); |
88 c = a; c.add(b); | 93 c = a; c.add(b); |
89 REPORTER_ASSERT(reporter, c.get32() == aa + bb); | 94 REPORTER_ASSERT(reporter, c.get32() == aa + bb); |
90 c = a; c.sub(b); | 95 c = a; c.sub(b); |
91 REPORTER_ASSERT(reporter, c.get32() == aa - bb); | 96 REPORTER_ASSERT(reporter, c.get32() == aa - bb); |
92 } | 97 } |
93 | 98 |
94 for (i = 0; i < 1000; i++) { | 99 #ifdef SkLONGLONG |
| 100 for (i = 0; i < 1000; i++) |
| 101 { |
95 rand.next64(&a); //a.fHi >>= 1; // avoid overflow | 102 rand.next64(&a); //a.fHi >>= 1; // avoid overflow |
96 rand.next64(&b); //b.fHi >>= 1; // avoid overflow | 103 rand.next64(&b); //b.fHi >>= 1; // avoid overflow |
97 | 104 |
98 if (!(i & 3)) // want to explicitly test these cases | 105 if (!(i & 3)) // want to explicitly test these cases |
99 { | 106 { |
100 a.fLo = 0; | 107 a.fLo = 0; |
101 b.fLo = 0; | 108 b.fLo = 0; |
102 } | 109 } |
103 else if (!(i & 7)) // want to explicitly test these cases | 110 else if (!(i & 7)) // want to explicitly test these cases |
104 { | 111 { |
105 a.fHi = 0; | 112 a.fHi = 0; |
106 b.fHi = 0; | 113 b.fHi = 0; |
107 } | 114 } |
108 | 115 |
109 int64_t aa = a.as64(); | 116 SkLONGLONG aa = asLL(a); |
110 int64_t bb = b.as64(); | 117 SkLONGLONG bb = asLL(b); |
111 | 118 |
112 REPORTER_ASSERT(reporter, (a < b) == (aa < bb)); | 119 REPORTER_ASSERT(reporter, (a < b) == (aa < bb)); |
113 REPORTER_ASSERT(reporter, (a <= b) == (aa <= bb)); | 120 REPORTER_ASSERT(reporter, (a <= b) == (aa <= bb)); |
114 REPORTER_ASSERT(reporter, (a > b) == (aa > bb)); | 121 REPORTER_ASSERT(reporter, (a > b) == (aa > bb)); |
115 REPORTER_ASSERT(reporter, (a >= b) == (aa >= bb)); | 122 REPORTER_ASSERT(reporter, (a >= b) == (aa >= bb)); |
116 REPORTER_ASSERT(reporter, (a == b) == (aa == bb)); | 123 REPORTER_ASSERT(reporter, (a == b) == (aa == bb)); |
117 REPORTER_ASSERT(reporter, (a != b) == (aa != bb)); | 124 REPORTER_ASSERT(reporter, (a != b) == (aa != bb)); |
118 | 125 |
119 c = a; c.add(b); | 126 c = a; c.add(b); |
120 REPORTER_ASSERT(reporter, c.as64() == aa + bb); | 127 REPORTER_ASSERT(reporter, asLL(c) == aa + bb); |
121 c = a; c.sub(b); | 128 c = a; c.sub(b); |
122 REPORTER_ASSERT(reporter, c.as64() == aa - bb); | 129 REPORTER_ASSERT(reporter, asLL(c) == aa - bb); |
123 c = a; c.rsub(b); | 130 c = a; c.rsub(b); |
124 REPORTER_ASSERT(reporter, c.as64() == bb - aa); | 131 REPORTER_ASSERT(reporter, asLL(c) == bb - aa); |
125 c = a; c.negate(); | 132 c = a; c.negate(); |
126 REPORTER_ASSERT(reporter, c.as64() == -aa); | 133 REPORTER_ASSERT(reporter, asLL(c) == -aa); |
127 | 134 |
128 int bits = rand.nextU() & 63; | 135 int bits = rand.nextU() & 63; |
129 c = a; c.shiftLeft(bits); | 136 c = a; c.shiftLeft(bits); |
130 REPORTER_ASSERT(reporter, c.as64() == (aa << bits)); | 137 REPORTER_ASSERT(reporter, asLL(c) == (aa << bits)); |
131 c = a; c.shiftRight(bits); | 138 c = a; c.shiftRight(bits); |
132 REPORTER_ASSERT(reporter, c.as64() == (aa >> bits)); | 139 REPORTER_ASSERT(reporter, asLL(c) == (aa >> bits)); |
133 c = a; c.roundRight(bits); | 140 c = a; c.roundRight(bits); |
134 | 141 |
135 int64_t tmp; | 142 SkLONGLONG tmp; |
136 | 143 |
137 tmp = aa; | 144 tmp = aa; |
138 if (bits > 0) | 145 if (bits > 0) |
139 tmp += (int64_t)1 << (bits - 1); | 146 tmp += (SkLONGLONG)1 << (bits - 1); |
140 REPORTER_ASSERT(reporter, c.as64() == (tmp >> bits)); | 147 REPORTER_ASSERT(reporter, asLL(c) == (tmp >> bits)); |
141 | 148 |
142 c.setMul(a.fHi, b.fHi); | 149 c.setMul(a.fHi, b.fHi); |
143 tmp = (int64_t)a.fHi * b.fHi; | 150 tmp = (SkLONGLONG)a.fHi * b.fHi; |
144 REPORTER_ASSERT(reporter, c.as64() == tmp); | 151 REPORTER_ASSERT(reporter, asLL(c) == tmp); |
145 } | 152 } |
146 | 153 |
147 | 154 |
148 for (i = 0; i < 100000; i++) | 155 for (i = 0; i < 100000; i++) |
149 { | 156 { |
150 Sk64 wide; | 157 Sk64 wide; |
151 int32_t denom = rand.nextS(); | 158 int32_t denom = rand.nextS(); |
152 | 159 |
153 while (denom == 0) | 160 while (denom == 0) |
154 denom = rand.nextS(); | 161 denom = rand.nextS(); |
155 wide.setMul(rand.nextS(), rand.nextS()); | 162 wide.setMul(rand.nextS(), rand.nextS()); |
156 int64_t check = wide.getLongLong(); | 163 SkLONGLONG check = wide.getLongLong(); |
157 | 164 |
158 wide.div(denom, Sk64::kTrunc_DivOption); | 165 wide.div(denom, Sk64::kTrunc_DivOption); |
159 check /= denom; | 166 check /= denom; |
160 int64_t w = wide.getLongLong(); | 167 SkLONGLONG w = wide.getLongLong(); |
161 | 168 |
162 REPORTER_ASSERT(reporter, check == w); | 169 REPORTER_ASSERT(reporter, check == w); |
163 | 170 |
164 wide.setMul(rand.nextS(), rand.nextS()); | 171 wide.setMul(rand.nextS(), rand.nextS()); |
165 wide.abs(); | 172 wide.abs(); |
166 denom = wide.getSqrt(); | 173 denom = wide.getSqrt(); |
167 int32_t ck = (int32_t)sqrt((double)wide.getLongLong()); | 174 int32_t ck = (int32_t)sqrt((double)wide.getLongLong()); |
168 int diff = denom - ck; | 175 int diff = denom - ck; |
169 REPORTER_ASSERT(reporter, SkAbs32(diff) <= 1); | 176 REPORTER_ASSERT(reporter, SkAbs32(diff) <= 1); |
170 } | 177 } |
| 178 #endif |
171 } | 179 } |
172 | |
173 DEF_TEST(Sk64Test, reporter) { | |
174 Sk64::UnitTestWithReporter(reporter); | |
175 } | |
OLD | NEW |