| OLD | NEW |
| 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" |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 const SkPoint& p, SkScalar targetLen) { | 166 const SkPoint& p, SkScalar targetLen) { |
| 167 float x = SkScalarToFloat(p.fX); | 167 float x = SkScalarToFloat(p.fX); |
| 168 float y = SkScalarToFloat(p.fY); | 168 float y = SkScalarToFloat(p.fY); |
| 169 float len = sk_float_sqrt(x*x + y*y); | 169 float len = sk_float_sqrt(x*x + y*y); |
| 170 | 170 |
| 171 len /= SkScalarToFloat(targetLen); | 171 len /= SkScalarToFloat(targetLen); |
| 172 | 172 |
| 173 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f); | 173 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f); |
| 174 } | 174 } |
| 175 | 175 |
| 176 static float nextFloat(SkRandom& rand) { | 176 static float nextFloat(SkMWCRandom& rand) { |
| 177 SkFloatIntUnion data; | 177 SkFloatIntUnion data; |
| 178 data.fSignBitInt = rand.nextU(); | 178 data.fSignBitInt = rand.nextU(); |
| 179 return data.fFloat; | 179 return data.fFloat; |
| 180 } | 180 } |
| 181 | 181 |
| 182 /* returns true if a == b as resulting from (int)x. Since it is undefined | 182 /* returns true if a == b as resulting from (int)x. Since it is undefined |
| 183 what to do if the float exceeds 2^32-1, we check for that explicitly. | 183 what to do if the float exceeds 2^32-1, we check for that explicitly. |
| 184 */ | 184 */ |
| 185 static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) { | 185 static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) { |
| 186 if (!(x == x)) { // NAN | 186 if (!(x == x)) { // NAN |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 | 240 |
| 241 static void test_int2float(skiatest::Reporter* reporter, int ival) { | 241 static void test_int2float(skiatest::Reporter* reporter, int ival) { |
| 242 float x0 = (float)ival; | 242 float x0 = (float)ival; |
| 243 float x1 = SkIntToFloatCast(ival); | 243 float x1 = SkIntToFloatCast(ival); |
| 244 float x2 = SkIntToFloatCast_NoOverflowCheck(ival); | 244 float x2 = SkIntToFloatCast_NoOverflowCheck(ival); |
| 245 REPORTER_ASSERT(reporter, x0 == x1); | 245 REPORTER_ASSERT(reporter, x0 == x1); |
| 246 REPORTER_ASSERT(reporter, x0 == x2); | 246 REPORTER_ASSERT(reporter, x0 == x2); |
| 247 } | 247 } |
| 248 | 248 |
| 249 static void unittest_fastfloat(skiatest::Reporter* reporter) { | 249 static void unittest_fastfloat(skiatest::Reporter* reporter) { |
| 250 SkRandom rand; | 250 SkMWCRandom rand; |
| 251 size_t i; | 251 size_t i; |
| 252 | 252 |
| 253 static const float gFloats[] = { | 253 static const float gFloats[] = { |
| 254 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3, | 254 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3, |
| 255 0.000000001f, 1000000000.f, // doesn't overflow | 255 0.000000001f, 1000000000.f, // doesn't overflow |
| 256 0.0000000001f, 10000000000.f // does overflow | 256 0.0000000001f, 10000000000.f // does overflow |
| 257 }; | 257 }; |
| 258 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) { | 258 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) { |
| 259 test_float_conversions(reporter, gFloats[i]); | 259 test_float_conversions(reporter, gFloats[i]); |
| 260 test_float_conversions(reporter, -gFloats[i]); | 260 test_float_conversions(reporter, -gFloats[i]); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 }; | 361 }; |
| 362 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) { | 362 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) { |
| 363 REPORTER_ASSERT(reporter, | 363 REPORTER_ASSERT(reporter, |
| 364 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2
]); | 364 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2
]); |
| 365 float x = (float)gTriples[i]; | 365 float x = (float)gTriples[i]; |
| 366 float y = (float)gTriples[i+1]; | 366 float y = (float)gTriples[i+1]; |
| 367 float expected = (float)gTriples[i+2]; | 367 float expected = (float)gTriples[i+2]; |
| 368 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected); | 368 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected); |
| 369 } | 369 } |
| 370 | 370 |
| 371 SkRandom rand; | 371 SkMWCRandom rand; |
| 372 for (int j = 0; j < 1000; j++) { | 372 for (int j = 0; j < 1000; j++) { |
| 373 int ix = rand.nextS(); | 373 int ix = rand.nextS(); |
| 374 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix); | 374 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix); |
| 375 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix); | 375 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix); |
| 376 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix); | 376 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix); |
| 377 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix); | 377 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix); |
| 378 | 378 |
| 379 SkScalar sx = rand.nextSScalar1(); | 379 SkScalar sx = rand.nextSScalar1(); |
| 380 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx); | 380 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx); |
| 381 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx); | 381 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx); |
| 382 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx); | 382 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx); |
| 383 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx); | 383 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx); |
| 384 } | 384 } |
| 385 } | 385 } |
| 386 | 386 |
| 387 static void TestMath(skiatest::Reporter* reporter) { | 387 static void TestMath(skiatest::Reporter* reporter) { |
| 388 int i; | 388 int i; |
| 389 int32_t x; | 389 int32_t x; |
| 390 SkRandom rand; | 390 SkMWCRandom rand; |
| 391 | 391 |
| 392 // these should assert | 392 // these should assert |
| 393 #if 0 | 393 #if 0 |
| 394 SkToS8(128); | 394 SkToS8(128); |
| 395 SkToS8(-129); | 395 SkToS8(-129); |
| 396 SkToU8(256); | 396 SkToU8(256); |
| 397 SkToU8(-5); | 397 SkToU8(-5); |
| 398 | 398 |
| 399 SkToS16(32768); | 399 SkToS16(32768); |
| 400 SkToS16(-32769); | 400 SkToS16(-32769); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 #endif | 592 #endif |
| 593 | 593 |
| 594 if (false) test_floor(reporter); | 594 if (false) test_floor(reporter); |
| 595 | 595 |
| 596 // disable for now | 596 // disable for now |
| 597 if (false) test_blend31(); // avoid bit rot, suppress warning | 597 if (false) test_blend31(); // avoid bit rot, suppress warning |
| 598 } | 598 } |
| 599 | 599 |
| 600 #include "TestClassDef.h" | 600 #include "TestClassDef.h" |
| 601 DEFINE_TESTCLASS("Math", MathTestClass, TestMath) | 601 DEFINE_TESTCLASS("Math", MathTestClass, TestMath) |
| OLD | NEW |