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

Side by Side Diff: tests/MathTest.cpp

Issue 23576015: Change old PRG to be SkLCGRandom; change new one to SkRandom (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fix some spurious SkMWCRandoms Created 7 years, 3 months 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/LListTest.cpp ('k') | tests/MatrixTest.cpp » ('j') | 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 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "Test.h" 8 #include "Test.h"
9 #include "SkFloatBits.h" 9 #include "SkFloatBits.h"
10 #include "SkFloatingPoint.h" 10 #include "SkFloatingPoint.h"
11 #include "SkMathPriv.h" 11 #include "SkMathPriv.h"
12 #include "SkPoint.h" 12 #include "SkPoint.h"
13 #include "SkRandom.h" 13 #include "SkRandom.h"
14 #include "SkColorPriv.h" 14 #include "SkColorPriv.h"
15 15
16 static void test_clz(skiatest::Reporter* reporter) { 16 static void test_clz(skiatest::Reporter* reporter) {
17 REPORTER_ASSERT(reporter, 32 == SkCLZ(0)); 17 REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
18 REPORTER_ASSERT(reporter, 31 == SkCLZ(1)); 18 REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
19 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30)); 19 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
20 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U)); 20 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
21 21
22 SkMWCRandom rand; 22 SkRandom rand;
23 for (int i = 0; i < 1000; ++i) { 23 for (int i = 0; i < 1000; ++i) {
24 uint32_t mask = rand.nextU(); 24 uint32_t mask = rand.nextU();
25 // need to get some zeros for testing, but in some obscure way so the 25 // need to get some zeros for testing, but in some obscure way so the
26 // compiler won't "see" that, and work-around calling the functions. 26 // compiler won't "see" that, and work-around calling the functions.
27 mask >>= (mask & 31); 27 mask >>= (mask & 31);
28 int intri = SkCLZ(mask); 28 int intri = SkCLZ(mask);
29 int porta = SkCLZ_portable(mask); 29 int porta = SkCLZ_portable(mask);
30 REPORTER_ASSERT(reporter, intri == porta); 30 REPORTER_ASSERT(reporter, intri == porta);
31 } 31 }
32 } 32 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 75
76 for (int a = 0; a <= 32767; ++a) { 76 for (int a = 0; a <= 32767; ++a) {
77 for (int b = 0; b <= 32767; ++b) { 77 for (int b = 0; b <= 32767; ++b) {
78 unsigned prod0 = SkMul16ShiftRound(a, b, 8); 78 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
79 unsigned prod1 = SkMulDiv255Round(a, b); 79 unsigned prod1 = SkMulDiv255Round(a, b);
80 SkASSERT(prod0 == prod1); 80 SkASSERT(prod0 == prod1);
81 } 81 }
82 } 82 }
83 #endif 83 #endif
84 84
85 SkMWCRandom rand; 85 SkRandom rand;
86 for (int i = 0; i < 10000; ++i) { 86 for (int i = 0; i < 10000; ++i) {
87 unsigned a = rand.nextU() & 0x7FFF; 87 unsigned a = rand.nextU() & 0x7FFF;
88 unsigned b = rand.nextU() & 0x7FFF; 88 unsigned b = rand.nextU() & 0x7FFF;
89 89
90 unsigned prod0 = SkMul16ShiftRound(a, b, 8); 90 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
91 unsigned prod1 = SkMulDiv255Round(a, b); 91 unsigned prod1 = SkMulDiv255Round(a, b);
92 92
93 REPORTER_ASSERT(reporter, prod0 == prod1); 93 REPORTER_ASSERT(reporter, prod0 == prod1);
94 } 94 }
95 } 95 }
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 const SkPoint& p, SkScalar targetLen) { 212 const SkPoint& p, SkScalar targetLen) {
213 float x = SkScalarToFloat(p.fX); 213 float x = SkScalarToFloat(p.fX);
214 float y = SkScalarToFloat(p.fY); 214 float y = SkScalarToFloat(p.fY);
215 float len = sk_float_sqrt(x*x + y*y); 215 float len = sk_float_sqrt(x*x + y*y);
216 216
217 len /= SkScalarToFloat(targetLen); 217 len /= SkScalarToFloat(targetLen);
218 218
219 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f); 219 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
220 } 220 }
221 221
222 static float nextFloat(SkMWCRandom& rand) { 222 static float nextFloat(SkRandom& rand) {
223 SkFloatIntUnion data; 223 SkFloatIntUnion data;
224 data.fSignBitInt = rand.nextU(); 224 data.fSignBitInt = rand.nextU();
225 return data.fFloat; 225 return data.fFloat;
226 } 226 }
227 227
228 /* returns true if a == b as resulting from (int)x. Since it is undefined 228 /* returns true if a == b as resulting from (int)x. Since it is undefined
229 what to do if the float exceeds 2^32-1, we check for that explicitly. 229 what to do if the float exceeds 2^32-1, we check for that explicitly.
230 */ 230 */
231 static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) { 231 static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
232 if (!(x == x)) { // NAN 232 if (!(x == x)) { // NAN
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 286
287 static void test_int2float(skiatest::Reporter* reporter, int ival) { 287 static void test_int2float(skiatest::Reporter* reporter, int ival) {
288 float x0 = (float)ival; 288 float x0 = (float)ival;
289 float x1 = SkIntToFloatCast(ival); 289 float x1 = SkIntToFloatCast(ival);
290 float x2 = SkIntToFloatCast_NoOverflowCheck(ival); 290 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
291 REPORTER_ASSERT(reporter, x0 == x1); 291 REPORTER_ASSERT(reporter, x0 == x1);
292 REPORTER_ASSERT(reporter, x0 == x2); 292 REPORTER_ASSERT(reporter, x0 == x2);
293 } 293 }
294 294
295 static void unittest_fastfloat(skiatest::Reporter* reporter) { 295 static void unittest_fastfloat(skiatest::Reporter* reporter) {
296 SkMWCRandom rand; 296 SkRandom rand;
297 size_t i; 297 size_t i;
298 298
299 static const float gFloats[] = { 299 static const float gFloats[] = {
300 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3, 300 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
301 0.000000001f, 1000000000.f, // doesn't overflow 301 0.000000001f, 1000000000.f, // doesn't overflow
302 0.0000000001f, 10000000000.f // does overflow 302 0.0000000001f, 10000000000.f // does overflow
303 }; 303 };
304 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) { 304 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
305 test_float_conversions(reporter, gFloats[i]); 305 test_float_conversions(reporter, gFloats[i]);
306 test_float_conversions(reporter, -gFloats[i]); 306 test_float_conversions(reporter, -gFloats[i]);
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 }; 407 };
408 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) { 408 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
409 REPORTER_ASSERT(reporter, 409 REPORTER_ASSERT(reporter,
410 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2 ]); 410 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2 ]);
411 float x = (float)gTriples[i]; 411 float x = (float)gTriples[i];
412 float y = (float)gTriples[i+1]; 412 float y = (float)gTriples[i+1];
413 float expected = (float)gTriples[i+2]; 413 float expected = (float)gTriples[i+2];
414 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected); 414 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
415 } 415 }
416 416
417 SkMWCRandom rand; 417 SkRandom rand;
418 for (int j = 0; j < 1000; j++) { 418 for (int j = 0; j < 1000; j++) {
419 int ix = rand.nextS(); 419 int ix = rand.nextS();
420 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix); 420 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
421 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix); 421 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
422 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix); 422 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
423 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix); 423 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
424 424
425 SkScalar sx = rand.nextSScalar1(); 425 SkScalar sx = rand.nextSScalar1();
426 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx); 426 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
427 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx); 427 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
428 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx); 428 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
429 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx); 429 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
430 } 430 }
431 } 431 }
432 432
433 static void TestMath(skiatest::Reporter* reporter) { 433 static void TestMath(skiatest::Reporter* reporter) {
434 int i; 434 int i;
435 int32_t x; 435 int32_t x;
436 SkMWCRandom rand; 436 SkRandom rand;
437 437
438 // these should assert 438 // these should assert
439 #if 0 439 #if 0
440 SkToS8(128); 440 SkToS8(128);
441 SkToS8(-129); 441 SkToS8(-129);
442 SkToU8(256); 442 SkToU8(256);
443 SkToU8(-5); 443 SkToU8(-5);
444 444
445 SkToS16(32768); 445 SkToS16(32768);
446 SkToS16(-32769); 446 SkToS16(-32769);
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 } 684 }
685 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) { 685 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
686 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin)); 686 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
687 } 687 }
688 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) { 688 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
689 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin)); 689 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
690 } 690 }
691 } 691 }
692 692
693 DEFINE_TESTCLASS("Endian", EndianTestClass, TestEndian) 693 DEFINE_TESTCLASS("Endian", EndianTestClass, TestEndian)
OLDNEW
« no previous file with comments | « tests/LListTest.cpp ('k') | tests/MatrixTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698