Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: tests/Sk64Test.cpp

Issue 119353003: Revert "Revert "begin to remove SkLONGLONG and wean Skia off of Sk64"" (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/MathTest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #ifdef SkLONGLONG 28 void Sk64::UnitTestWithReporter(void* reporterParam) {
29 static SkLONGLONG asLL(const Sk64& a) 29 skiatest::Reporter* reporter = (skiatest::Reporter*)reporterParam;
30 {
31 return ((SkLONGLONG)a.fHi << 32) | a.fLo;
32 }
33 #endif
34 30
35 DEF_TEST(Sk64Test, reporter) {
36 enum BoolTests { 31 enum BoolTests {
37 kZero_BoolTest, 32 kZero_BoolTest,
38 kPos_BoolTest, 33 kPos_BoolTest,
39 kNeg_BoolTest 34 kNeg_BoolTest
40 }; 35 };
41 static const BoolTable gBoolTable[] = { 36 static const BoolTable gBoolTable[] = {
42 { 1, 0, 0, 0, 0 }, 37 { 1, 0, 0, 0, 0 },
43 { 0, 1, 0, 1, 1 }, 38 { 0, 1, 0, 1, 1 },
44 { 0, 0, 1, 1, -1 } 39 { 0, 0, 1, 1, -1 }
45 }; 40 };
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 c = a; c.add(bb); 84 c = a; c.add(bb);
90 REPORTER_ASSERT(reporter, c.get32() == aa + bb); 85 REPORTER_ASSERT(reporter, c.get32() == aa + bb);
91 c = a; c.add(-bb); 86 c = a; c.add(-bb);
92 REPORTER_ASSERT(reporter, c.get32() == aa - bb); 87 REPORTER_ASSERT(reporter, c.get32() == aa - bb);
93 c = a; c.add(b); 88 c = a; c.add(b);
94 REPORTER_ASSERT(reporter, c.get32() == aa + bb); 89 REPORTER_ASSERT(reporter, c.get32() == aa + bb);
95 c = a; c.sub(b); 90 c = a; c.sub(b);
96 REPORTER_ASSERT(reporter, c.get32() == aa - bb); 91 REPORTER_ASSERT(reporter, c.get32() == aa - bb);
97 } 92 }
98 93
99 #ifdef SkLONGLONG 94 for (i = 0; i < 1000; i++) {
100 for (i = 0; i < 1000; i++)
101 {
102 rand.next64(&a); //a.fHi >>= 1; // avoid overflow 95 rand.next64(&a); //a.fHi >>= 1; // avoid overflow
103 rand.next64(&b); //b.fHi >>= 1; // avoid overflow 96 rand.next64(&b); //b.fHi >>= 1; // avoid overflow
104 97
105 if (!(i & 3)) // want to explicitly test these cases 98 if (!(i & 3)) // want to explicitly test these cases
106 { 99 {
107 a.fLo = 0; 100 a.fLo = 0;
108 b.fLo = 0; 101 b.fLo = 0;
109 } 102 }
110 else if (!(i & 7)) // want to explicitly test these cases 103 else if (!(i & 7)) // want to explicitly test these cases
111 { 104 {
112 a.fHi = 0; 105 a.fHi = 0;
113 b.fHi = 0; 106 b.fHi = 0;
114 } 107 }
115 108
116 SkLONGLONG aa = asLL(a); 109 int64_t aa = a.as64();
117 SkLONGLONG bb = asLL(b); 110 int64_t bb = b.as64();
118 111
119 REPORTER_ASSERT(reporter, (a < b) == (aa < bb)); 112 REPORTER_ASSERT(reporter, (a < b) == (aa < bb));
120 REPORTER_ASSERT(reporter, (a <= b) == (aa <= bb)); 113 REPORTER_ASSERT(reporter, (a <= b) == (aa <= bb));
121 REPORTER_ASSERT(reporter, (a > b) == (aa > bb)); 114 REPORTER_ASSERT(reporter, (a > b) == (aa > bb));
122 REPORTER_ASSERT(reporter, (a >= b) == (aa >= bb)); 115 REPORTER_ASSERT(reporter, (a >= b) == (aa >= bb));
123 REPORTER_ASSERT(reporter, (a == b) == (aa == bb)); 116 REPORTER_ASSERT(reporter, (a == b) == (aa == bb));
124 REPORTER_ASSERT(reporter, (a != b) == (aa != bb)); 117 REPORTER_ASSERT(reporter, (a != b) == (aa != bb));
125 118
126 c = a; c.add(b); 119 c = a; c.add(b);
127 REPORTER_ASSERT(reporter, asLL(c) == aa + bb); 120 REPORTER_ASSERT(reporter, c.as64() == aa + bb);
128 c = a; c.sub(b); 121 c = a; c.sub(b);
129 REPORTER_ASSERT(reporter, asLL(c) == aa - bb); 122 REPORTER_ASSERT(reporter, c.as64() == aa - bb);
130 c = a; c.rsub(b); 123 c = a; c.rsub(b);
131 REPORTER_ASSERT(reporter, asLL(c) == bb - aa); 124 REPORTER_ASSERT(reporter, c.as64() == bb - aa);
132 c = a; c.negate(); 125 c = a; c.negate();
133 REPORTER_ASSERT(reporter, asLL(c) == -aa); 126 REPORTER_ASSERT(reporter, c.as64() == -aa);
134 127
135 int bits = rand.nextU() & 63; 128 int bits = rand.nextU() & 63;
136 c = a; c.shiftLeft(bits); 129 c = a; c.shiftLeft(bits);
137 REPORTER_ASSERT(reporter, asLL(c) == (aa << bits)); 130 REPORTER_ASSERT(reporter, c.as64() == (aa << bits));
138 c = a; c.shiftRight(bits); 131 c = a; c.shiftRight(bits);
139 REPORTER_ASSERT(reporter, asLL(c) == (aa >> bits)); 132 REPORTER_ASSERT(reporter, c.as64() == (aa >> bits));
140 c = a; c.roundRight(bits); 133 c = a; c.roundRight(bits);
141 134
142 SkLONGLONG tmp; 135 int64_t tmp;
143 136
144 tmp = aa; 137 tmp = aa;
145 if (bits > 0) 138 if (bits > 0)
146 tmp += (SkLONGLONG)1 << (bits - 1); 139 tmp += (int64_t)1 << (bits - 1);
147 REPORTER_ASSERT(reporter, asLL(c) == (tmp >> bits)); 140 REPORTER_ASSERT(reporter, c.as64() == (tmp >> bits));
148 141
149 c.setMul(a.fHi, b.fHi); 142 c.setMul(a.fHi, b.fHi);
150 tmp = (SkLONGLONG)a.fHi * b.fHi; 143 tmp = (int64_t)a.fHi * b.fHi;
151 REPORTER_ASSERT(reporter, asLL(c) == tmp); 144 REPORTER_ASSERT(reporter, c.as64() == tmp);
152 } 145 }
153 146
154 147
155 for (i = 0; i < 100000; i++) 148 for (i = 0; i < 100000; i++)
156 { 149 {
157 Sk64 wide; 150 Sk64 wide;
158 int32_t denom = rand.nextS(); 151 int32_t denom = rand.nextS();
159 152
160 while (denom == 0) 153 while (denom == 0)
161 denom = rand.nextS(); 154 denom = rand.nextS();
162 wide.setMul(rand.nextS(), rand.nextS()); 155 wide.setMul(rand.nextS(), rand.nextS());
163 SkLONGLONG check = wide.getLongLong(); 156 int64_t check = wide.getLongLong();
164 157
165 wide.div(denom, Sk64::kTrunc_DivOption); 158 wide.div(denom, Sk64::kTrunc_DivOption);
166 check /= denom; 159 check /= denom;
167 SkLONGLONG w = wide.getLongLong(); 160 int64_t w = wide.getLongLong();
168 161
169 REPORTER_ASSERT(reporter, check == w); 162 REPORTER_ASSERT(reporter, check == w);
170 163
171 wide.setMul(rand.nextS(), rand.nextS()); 164 wide.setMul(rand.nextS(), rand.nextS());
172 wide.abs(); 165 wide.abs();
173 denom = wide.getSqrt(); 166 denom = wide.getSqrt();
174 int32_t ck = (int32_t)sqrt((double)wide.getLongLong()); 167 int32_t ck = (int32_t)sqrt((double)wide.getLongLong());
175 int diff = denom - ck; 168 int diff = denom - ck;
176 REPORTER_ASSERT(reporter, SkAbs32(diff) <= 1); 169 REPORTER_ASSERT(reporter, SkAbs32(diff) <= 1);
177 } 170 }
178 #endif
179 } 171 }
172
173 DEF_TEST(Sk64Test, reporter) {
174 Sk64::UnitTestWithReporter(reporter);
175 }
OLDNEW
« no previous file with comments | « tests/MathTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698