1 /* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "SkColorPriv.h" 9 #include "SkEndian.h" 10 #include "SkFloatBits.h" 11 #include "SkFloatingPoint.h" 12 #include "SkHalf.h" 13 #include "SkMathPriv.h" 14 #include "SkPoint.h" 15 #include "SkRandom.h" 16 #include "Test.h" 17 18 static void test_clz(skiatest::Reporter* reporter) { 19 REPORTER_ASSERT(reporter, 32 == SkCLZ(0)); 20 REPORTER_ASSERT(reporter, 31 == SkCLZ(1)); 21 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30)); 22 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U)); 23 24 SkRandom rand; 25 for (int i = 0; i < 1000; ++i) { 26 uint32_t mask = rand.nextU(); 27 // need to get some zeros for testing, but in some obscure way so the 28 // compiler won't "see" that, and work-around calling the functions. 29 mask >>= (mask & 31); 30 int intri = SkCLZ(mask); 31 int porta = SkCLZ_portable(mask); 32 REPORTER_ASSERT(reporter, intri == porta); 33 } 34 } 35 36 /////////////////////////////////////////////////////////////////////////////// 37 38 static float sk_fsel(float pred, float result_ge, float result_lt) { 39 return pred >= 0 ? result_ge : result_lt; 40 } 41 42 static float fast_floor(float x) { 43 // float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23); 44 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23)); 45 return (float)(x + big) - big; 46 } 47 48 static float std_floor(float x) { 49 return sk_float_floor(x); 50 } 51 52 static void test_floor_value(skiatest::Reporter* reporter, float value) { 53 float fast = fast_floor(value); 54 float std = std_floor(value); 55 REPORTER_ASSERT(reporter, std == fast); 56 // SkDebugf("value[%1.9f] std[%g] fast[%g] equal[%d]\n", 57 // value, std, fast, std == fast); 58 } 59 60 static void test_floor(skiatest::Reporter* reporter) { 61 static const float gVals[] = { 62 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f 63 }; 64 65 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) { 66 test_floor_value(reporter, gVals[i]); 67 // test_floor_value(reporter, -gVals[i]); 68 } 69 } 70 71 /////////////////////////////////////////////////////////////////////////////// 72 73 // test that SkMul16ShiftRound and SkMulDiv255Round return the same result 74 static void test_muldivround(skiatest::Reporter* reporter) { 75 #if 0 76 // this "complete" test is too slow, so we test a random sampling of it 77 78 for (int a = 0; a <= 32767; ++a) { 79 for (int b = 0; b <= 32767; ++b) { 80 unsigned prod0 = SkMul16ShiftRound(a, b, 8); 81 unsigned prod1 = SkMulDiv255Round(a, b); 82 SkASSERT(prod0 == prod1); 83 } 84 } 85 #endif 86 87 SkRandom rand; 88 for (int i = 0; i < 10000; ++i) { 89 unsigned a = rand.nextU() & 0x7FFF; 90 unsigned b = rand.nextU() & 0x7FFF; 91 92 unsigned prod0 = SkMul16ShiftRound(a, b, 8); 93 unsigned prod1 = SkMulDiv255Round(a, b); 94 95 REPORTER_ASSERT(reporter, prod0 == prod1); 96 } 97 } 98 99 static float float_blend(int src, int dst, float unit) { 100 return dst + (src - dst) * unit; 101 } 102 103 static int blend31(int src, int dst, int a31) { 104 return dst + ((src - dst) * a31 * 2114 >> 16); 105 // return dst + ((src - dst) * a31 * 33 >> 10); 106 } 107 108 static int blend31_slow(int src, int dst, int a31) { 109 int prod = src * a31 + (31 - a31) * dst + 16; 110 prod = (prod + (prod >> 5)) >> 5; 111 return prod; 112 } 113 114 static int blend31_round(int src, int dst, int a31) { 115 int prod = (src - dst) * a31 + 16; 116 prod = (prod + (prod >> 5)) >> 5; 117 return dst + prod; 118 } 119 120 static int blend31_old(int src, int dst, int a31) { 121 a31 += a31 >> 4; 122 return dst + ((src - dst) * a31 >> 5); 123 } 124 125 // suppress unused code warning 126 static int (*blend_functions[])(int, int, int) = { 127 blend31, 128 blend31_slow, 129 blend31_round, 130 blend31_old 131 }; 132 133 static void test_blend31() { 134 int failed = 0; 135 int death = 0; 136 if (false) { // avoid bit rot, suppress warning 137 failed = (*blend_functions[0])(0,0,0); 138 } 139 for (int src = 0; src <= 255; src++) { 140 for (int dst = 0; dst <= 255; dst++) { 141 for (int a = 0; a <= 31; a++) { 142 // int r0 = blend31(src, dst, a); 143 // int r0 = blend31_round(src, dst, a); 144 // int r0 = blend31_old(src, dst, a); 145 int r0 = blend31_slow(src, dst, a); 146 147 float f = float_blend(src, dst, a / 31.f); 148 int r1 = (int)f; 149 int r2 = SkScalarRoundToInt(f); 150 151 if (r0 != r1 && r0 != r2) { 152 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n", 153 src, dst, a, r0, f); 154 failed += 1; 155 } 156 if (r0 > 255) { 157 death += 1; 158 SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n", 159 src, dst, a, r0, f); 160 } 161 } 162 } 163 } 164 SkDebugf("---- failed %d death %d\n", failed, death); 165 } 166 167 static void test_blend(skiatest::Reporter* reporter) { 168 for (int src = 0; src <= 255; src++) { 169 for (int dst = 0; dst <= 255; dst++) { 170 for (int a = 0; a <= 255; a++) { 171 int r0 = SkAlphaBlend255(src, dst, a); 172 float f1 = float_blend(src, dst, a / 255.f); 173 int r1 = SkScalarRoundToInt(f1); 174 175 if (r0 != r1) { 176 float diff = sk_float_abs(f1 - r1); 177 diff = sk_float_abs(diff - 0.5f); 178 if (diff > (1 / 255.f)) { 179 #ifdef SK_DEBUG 180 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n", 181 src, dst, a, r0, f1); 182 #endif 183 REPORTER_ASSERT(reporter, false); 184 } 185 } 186 } 187 } 188 } 189 } 190 191 static void check_length(skiatest::Reporter* reporter, 192 const SkPoint& p, SkScalar targetLen) { 193 float x = SkScalarToFloat(p.fX); 194 float y = SkScalarToFloat(p.fY); 195 float len = sk_float_sqrt(x*x + y*y); 196 197 len /= SkScalarToFloat(targetLen); 198 199 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f); 200 } 201 202 static float nextFloat(SkRandom& rand) { 203 SkFloatIntUnion data; 204 data.fSignBitInt = rand.nextU(); 205 return data.fFloat; 206 } 207 208 /* returns true if a == b as resulting from (int)x. Since it is undefined 209 what to do if the float exceeds 2^32-1, we check for that explicitly. 210 */ 211 static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) { 212 if (!(x == x)) { // NAN 213 return ((int32_t)si) == SK_MaxS32 || ((int32_t)si) == SK_MinS32; 214 } 215 // for out of range, C is undefined, but skia always should return NaN32 216 if (x > SK_MaxS32) { 217 return ((int32_t)si) == SK_MaxS32; 218 } 219 if (x < -SK_MaxS32) { 220 return ((int32_t)si) == SK_MinS32; 221 } 222 return si == ni; 223 } 224 225 static void assert_float_equal(skiatest::Reporter* reporter, const char op[], 226 float x, uint32_t ni, uint32_t si) { 227 if (!equal_float_native_skia(x, ni, si)) { 228 ERRORF(reporter, "%s float %g bits %x native %x skia %x\n", 229 op, x, SkFloat2Bits(x), ni, si); 230 } 231 } 232 233 static void test_float_cast(skiatest::Reporter* reporter, float x) { 234 int ix = (int)x; 235 int iix = SkFloatToIntCast(x); 236 assert_float_equal(reporter, "cast", x, ix, iix); 237 } 238 239 static void test_float_floor(skiatest::Reporter* reporter, float x) { 240 int ix = (int)floor(x); 241 int iix = SkFloatToIntFloor(x); 242 assert_float_equal(reporter, "floor", x, ix, iix); 243 } 244 245 static void test_float_round(skiatest::Reporter* reporter, float x) { 246 double xx = x + 0.5; // need intermediate double to avoid temp loss 247 int ix = (int)floor(xx); 248 int iix = SkFloatToIntRound(x); 249 assert_float_equal(reporter, "round", x, ix, iix); 250 } 251 252 static void test_float_ceil(skiatest::Reporter* reporter, float x) { 253 int ix = (int)ceil(x); 254 int iix = SkFloatToIntCeil(x); 255 assert_float_equal(reporter, "ceil", x, ix, iix); 256 } 257 258 static void test_float_conversions(skiatest::Reporter* reporter, float x) { 259 test_float_cast(reporter, x); 260 test_float_floor(reporter, x); 261 test_float_round(reporter, x); 262 test_float_ceil(reporter, x); 263 } 264 265 static void test_int2float(skiatest::Reporter* reporter, int ival) { 266 float x0 = (float)ival; 267 float x1 = SkIntToFloatCast(ival); 268 REPORTER_ASSERT(reporter, x0 == x1); 269 } 270 271 static void unittest_fastfloat(skiatest::Reporter* reporter) { 272 SkRandom rand; 273 size_t i; 274 275 static const float gFloats[] = { 276 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3, 277 0.000000001f, 1000000000.f, // doesn't overflow 278 0.0000000001f, 10000000000.f // does overflow 279 }; 280 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) { 281 test_float_conversions(reporter, gFloats[i]); 282 test_float_conversions(reporter, -gFloats[i]); 283 } 284 285 for (int outer = 0; outer < 100; outer++) { 286 rand.setSeed(outer); 287 for (i = 0; i < 100000; i++) { 288 float x = nextFloat(rand); 289 test_float_conversions(reporter, x); 290 } 291 292 test_int2float(reporter, 0); 293 test_int2float(reporter, 1); 294 test_int2float(reporter, -1); 295 for (i = 0; i < 100000; i++) { 296 // for now only test ints that are 24bits or less, since we don't 297 // round (down) large ints the same as IEEE... 298 int ival = rand.nextU() & 0xFFFFFF; 299 test_int2float(reporter, ival); 300 test_int2float(reporter, -ival); 301 } 302 } 303 } 304 305 static float make_zero() { 306 return sk_float_sin(0); 307 } 308 309 static void unittest_isfinite(skiatest::Reporter* reporter) { 310 float nan = sk_float_asin(2); 311 float inf = 1.0f / make_zero(); 312 float big = 3.40282e+038f; 313 314 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf)); 315 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf)); 316 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf)); 317 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf)); 318 319 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan)); 320 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big)); 321 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big)); 322 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0)); 323 324 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan)); 325 REPORTER_ASSERT(reporter, SkScalarIsFinite(big)); 326 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big)); 327 REPORTER_ASSERT(reporter, SkScalarIsFinite(0)); 328 } 329 330 static void unittest_half(skiatest::Reporter* reporter) { 331 static const float gFloats[] = { 332 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3, 333 -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3 334 }; 335 336 for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) { 337 SkHalf h = SkFloatToHalf(gFloats[i]); 338 float f = SkHalfToFloat(h); 339 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i])); 340 } 341 342 // check some special values 343 union FloatUnion { 344 uint32_t fU; 345 float fF; 346 }; 347 348 static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) }; 349 SkHalf h = SkFloatToHalf(largestPositiveHalf.fF); 350 float f = SkHalfToFloat(h); 351 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF)); 352 353 static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) }; 354 h = SkFloatToHalf(largestNegativeHalf.fF); 355 f = SkHalfToFloat(h); 356 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF)); 357 358 static const FloatUnion smallestPositiveHalf = { 102 << 23 }; 359 h = SkFloatToHalf(smallestPositiveHalf.fF); 360 f = SkHalfToFloat(h); 361 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF)); 362 363 static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) }; 364 h = SkFloatToHalf(overflowHalf.fF); 365 f = SkHalfToFloat(h); 366 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) ); 367 368 static const FloatUnion underflowHalf = { 101 << 23 }; 369 h = SkFloatToHalf(underflowHalf.fF); 370 f = SkHalfToFloat(h); 371 REPORTER_ASSERT(reporter, f == 0.0f ); 372 373 static const FloatUnion inf32 = { 255 << 23 }; 374 h = SkFloatToHalf(inf32.fF); 375 f = SkHalfToFloat(h); 376 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) ); 377 378 static const FloatUnion nan32 = { 255 << 23 | 1 }; 379 h = SkFloatToHalf(nan32.fF); 380 f = SkHalfToFloat(h); 381 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) ); 382 383 } 384 385 static void test_muldiv255(skiatest::Reporter* reporter) { 386 for (int a = 0; a <= 255; a++) { 387 for (int b = 0; b <= 255; b++) { 388 int ab = a * b; 389 float s = ab / 255.0f; 390 int round = (int)floorf(s + 0.5f); 391 int trunc = (int)floorf(s); 392 393 int iround = SkMulDiv255Round(a, b); 394 int itrunc = SkMulDiv255Trunc(a, b); 395 396 REPORTER_ASSERT(reporter, iround == round); 397 REPORTER_ASSERT(reporter, itrunc == trunc); 398 399 REPORTER_ASSERT(reporter, itrunc <= iround); 400 REPORTER_ASSERT(reporter, iround <= a); 401 REPORTER_ASSERT(reporter, iround <= b); 402 } 403 } 404 } 405 406 static void test_muldiv255ceiling(skiatest::Reporter* reporter) { 407 for (int c = 0; c <= 255; c++) { 408 for (int a = 0; a <= 255; a++) { 409 int product = (c * a + 255); 410 int expected_ceiling = (product + (product >> 8)) >> 8; 411 int webkit_ceiling = (c * a + 254) / 255; 412 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling); 413 int skia_ceiling = SkMulDiv255Ceiling(c, a); 414 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling); 415 } 416 } 417 } 418 419 static void test_copysign(skiatest::Reporter* reporter) { 420 static const int32_t gTriples[] = { 421 // x, y, expected result 422 0, 0, 0, 423 0, 1, 0, 424 0, -1, 0, 425 1, 0, 1, 426 1, 1, 1, 427 1, -1, -1, 428 -1, 0, 1, 429 -1, 1, 1, 430 -1, -1, -1, 431 }; 432 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) { 433 REPORTER_ASSERT(reporter, 434 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]); 435 float x = (float)gTriples[i]; 436 float y = (float)gTriples[i+1]; 437 float expected = (float)gTriples[i+2]; 438 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected); 439 } 440 441 SkRandom rand; 442 for (int j = 0; j < 1000; j++) { 443 int ix = rand.nextS(); 444 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix); 445 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix); 446 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix); 447 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix); 448 449 SkScalar sx = rand.nextSScalar1(); 450 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx); 451 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx); 452 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx); 453 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx); 454 } 455 } 456 457 DEF_TEST(Math, reporter) { 458 int i; 459 SkRandom rand; 460 461 // these should assert 462 #if 0 463 SkToS8(128); 464 SkToS8(-129); 465 SkToU8(256); 466 SkToU8(-5); 467 468 SkToS16(32768); 469 SkToS16(-32769); 470 SkToU16(65536); 471 SkToU16(-5); 472 473 if (sizeof(size_t) > 4) { 474 SkToS32(4*1024*1024); 475 SkToS32(-4*1024*1024); 476 SkToU32(5*1024*1024); 477 SkToU32(-5); 478 } 479 #endif 480 481 test_muldiv255(reporter); 482 test_muldiv255ceiling(reporter); 483 test_copysign(reporter); 484 485 { 486 SkScalar x = SK_ScalarNaN; 487 REPORTER_ASSERT(reporter, SkScalarIsNaN(x)); 488 } 489 490 for (i = 0; i < 1000; i++) { 491 int value = rand.nextS16(); 492 int max = rand.nextU16(); 493 494 int clamp = SkClampMax(value, max); 495 int clamp2 = value < 0 ? 0 : (value > max ? max : value); 496 REPORTER_ASSERT(reporter, clamp == clamp2); 497 } 498 499 for (i = 0; i < 10000; i++) { 500 SkPoint p; 501 502 // These random values are being treated as 32-bit-patterns, not as 503 // ints; calling SkIntToScalar() here produces crashes. 504 p.setLength((SkScalar) rand.nextS(), 505 (SkScalar) rand.nextS(), 506 SK_Scalar1); 507 check_length(reporter, p, SK_Scalar1); 508 p.setLength((SkScalar) (rand.nextS() >> 13), 509 (SkScalar) (rand.nextS() >> 13), 510 SK_Scalar1); 511 check_length(reporter, p, SK_Scalar1); 512 } 513 514 { 515 SkFixed result = SkFixedDiv(100, 100); 516 REPORTER_ASSERT(reporter, result == SK_Fixed1); 517 result = SkFixedDiv(1, SK_Fixed1); 518 REPORTER_ASSERT(reporter, result == 1); 519 } 520 521 unittest_fastfloat(reporter); 522 unittest_isfinite(reporter); 523 unittest_half(reporter); 524 525 for (i = 0; i < 10000; i++) { 526 SkFixed numer = rand.nextS(); 527 SkFixed denom = rand.nextS(); 528 SkFixed result = SkFixedDiv(numer, denom); 529 int64_t check = ((int64_t)numer << 16) / denom; 530 531 (void)SkCLZ(numer); 532 (void)SkCLZ(denom); 533 534 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); 535 if (check > SK_MaxS32) { 536 check = SK_MaxS32; 537 } else if (check < -SK_MaxS32) { 538 check = SK_MinS32; 539 } 540 if (result != (int32_t)check) { 541 ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check); 542 } 543 REPORTER_ASSERT(reporter, result == (int32_t)check); 544 } 545 546 test_blend(reporter); 547 548 if (false) test_floor(reporter); 549 550 // disable for now 551 if (false) test_blend31(); // avoid bit rot, suppress warning 552 553 test_muldivround(reporter); 554 test_clz(reporter); 555 } 556 557 template <typename T> struct PairRec { 558 T fYin; 559 T fYang; 560 }; 561 562 DEF_TEST(TestEndian, reporter) { 563 static const PairRec<uint16_t> g16[] = { 564 { 0x0, 0x0 }, 565 { 0xFFFF, 0xFFFF }, 566 { 0x1122, 0x2211 }, 567 }; 568 static const PairRec<uint32_t> g32[] = { 569 { 0x0, 0x0 }, 570 { 0xFFFFFFFF, 0xFFFFFFFF }, 571 { 0x11223344, 0x44332211 }, 572 }; 573 static const PairRec<uint64_t> g64[] = { 574 { 0x0, 0x0 }, 575 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL }, 576 { 0x1122334455667788ULL, 0x8877665544332211ULL }, 577 }; 578 579 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value); 580 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value); 581 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value); 582 583 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) { 584 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin)); 585 } 586 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) { 587 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin)); 588 } 589 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) { 590 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin)); 591 } 592 } 593 594 template <typename T> 595 static void test_divmod(skiatest::Reporter* r) { 596 const struct { 597 T numer; 598 T denom; 599 } kEdgeCases[] = { 600 {(T)17, (T)17}, 601 {(T)17, (T)4}, 602 {(T)0, (T)17}, 603 // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them. 604 {(T)-17, (T)-17}, 605 {(T)-17, (T)4}, 606 {(T)17, (T)-4}, 607 {(T)-17, (T)-4}, 608 }; 609 610 for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) { 611 const T numer = kEdgeCases[i].numer; 612 const T denom = kEdgeCases[i].denom; 613 T div, mod; 614 SkTDivMod(numer, denom, &div, &mod); 615 REPORTER_ASSERT(r, numer/denom == div); 616 REPORTER_ASSERT(r, numer%denom == mod); 617 } 618 619 SkRandom rand; 620 for (size_t i = 0; i < 10000; i++) { 621 const T numer = (T)rand.nextS(); 622 T denom = 0; 623 while (0 == denom) { 624 denom = (T)rand.nextS(); 625 } 626 T div, mod; 627 SkTDivMod(numer, denom, &div, &mod); 628 REPORTER_ASSERT(r, numer/denom == div); 629 REPORTER_ASSERT(r, numer%denom == mod); 630 } 631 } 632 633 DEF_TEST(divmod_u8, r) { 634 test_divmod<uint8_t>(r); 635 } 636 637 DEF_TEST(divmod_u16, r) { 638 test_divmod<uint16_t>(r); 639 } 640 641 DEF_TEST(divmod_u32, r) { 642 test_divmod<uint32_t>(r); 643 } 644 645 DEF_TEST(divmod_u64, r) { 646 test_divmod<uint64_t>(r); 647 } 648 649 DEF_TEST(divmod_s8, r) { 650 test_divmod<int8_t>(r); 651 } 652 653 DEF_TEST(divmod_s16, r) { 654 test_divmod<int16_t>(r); 655 } 656 657 DEF_TEST(divmod_s32, r) { 658 test_divmod<int32_t>(r); 659 } 660 661 DEF_TEST(divmod_s64, r) { 662 test_divmod<int64_t>(r); 663 } 664