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 "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 SkScalar sx = rand.nextSScalar1(); | 420 SkScalar sx = rand.nextSScalar1(); |
421 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx); | 421 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx); |
422 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx); | 422 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx); |
423 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx); | 423 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx); |
424 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx); | 424 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx); |
425 } | 425 } |
426 } | 426 } |
427 | 427 |
428 DEF_TEST(Math, reporter) { | 428 DEF_TEST(Math, reporter) { |
429 int i; | 429 int i; |
430 int32_t x; | |
431 SkRandom rand; | 430 SkRandom rand; |
432 | 431 |
433 // these should assert | 432 // these should assert |
434 #if 0 | 433 #if 0 |
435 SkToS8(128); | 434 SkToS8(128); |
436 SkToS8(-129); | 435 SkToS8(-129); |
437 SkToU8(256); | 436 SkToU8(256); |
438 SkToU8(-5); | 437 SkToU8(-5); |
439 | 438 |
440 SkToS16(32768); | 439 SkToS16(32768); |
(...skipping 11 matching lines...) Expand all Loading... |
452 | 451 |
453 test_muldiv255(reporter); | 452 test_muldiv255(reporter); |
454 test_muldiv255ceiling(reporter); | 453 test_muldiv255ceiling(reporter); |
455 test_copysign(reporter); | 454 test_copysign(reporter); |
456 | 455 |
457 { | 456 { |
458 SkScalar x = SK_ScalarNaN; | 457 SkScalar x = SK_ScalarNaN; |
459 REPORTER_ASSERT(reporter, SkScalarIsNaN(x)); | 458 REPORTER_ASSERT(reporter, SkScalarIsNaN(x)); |
460 } | 459 } |
461 | 460 |
462 for (i = 1; i <= 10; i++) { | |
463 x = SkCubeRootBits(i*i*i, 11); | |
464 REPORTER_ASSERT(reporter, x == i); | |
465 } | |
466 | |
467 x = SkFixedSqrt(SK_Fixed1); | |
468 REPORTER_ASSERT(reporter, x == SK_Fixed1); | |
469 x = SkFixedSqrt(SK_Fixed1/4); | |
470 REPORTER_ASSERT(reporter, x == SK_Fixed1/2); | |
471 x = SkFixedSqrt(SK_Fixed1*4); | |
472 REPORTER_ASSERT(reporter, x == SK_Fixed1*2); | |
473 | |
474 x = SkFractSqrt(SK_Fract1); | |
475 REPORTER_ASSERT(reporter, x == SK_Fract1); | |
476 x = SkFractSqrt(SK_Fract1/4); | |
477 REPORTER_ASSERT(reporter, x == SK_Fract1/2); | |
478 x = SkFractSqrt(SK_Fract1/16); | |
479 REPORTER_ASSERT(reporter, x == SK_Fract1/4); | |
480 | |
481 for (i = 1; i < 100; i++) { | |
482 x = SkFixedSqrt(SK_Fixed1 * i * i); | |
483 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i); | |
484 } | |
485 | |
486 for (i = 0; i < 1000; i++) { | 461 for (i = 0; i < 1000; i++) { |
487 int value = rand.nextS16(); | 462 int value = rand.nextS16(); |
488 int max = rand.nextU16(); | 463 int max = rand.nextU16(); |
489 | 464 |
490 int clamp = SkClampMax(value, max); | 465 int clamp = SkClampMax(value, max); |
491 int clamp2 = value < 0 ? 0 : (value > max ? max : value); | 466 int clamp2 = value < 0 ? 0 : (value > max ? max : value); |
492 REPORTER_ASSERT(reporter, clamp == clamp2); | 467 REPORTER_ASSERT(reporter, clamp == clamp2); |
493 } | 468 } |
494 | 469 |
495 for (i = 0; i < 10000; i++) { | 470 for (i = 0; i < 10000; i++) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 (void)SkCLZ(denom); | 503 (void)SkCLZ(denom); |
529 | 504 |
530 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); | 505 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); |
531 if (check > SK_MaxS32) { | 506 if (check > SK_MaxS32) { |
532 check = SK_MaxS32; | 507 check = SK_MaxS32; |
533 } else if (check < -SK_MaxS32) { | 508 } else if (check < -SK_MaxS32) { |
534 check = SK_MinS32; | 509 check = SK_MinS32; |
535 } | 510 } |
536 REPORTER_ASSERT(reporter, result == (int32_t)check); | 511 REPORTER_ASSERT(reporter, result == (int32_t)check); |
537 | 512 |
538 result = SkFractDiv(numer, denom); | |
539 check = ((SkLONGLONG)numer << 30) / denom; | |
540 | |
541 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); | |
542 if (check > SK_MaxS32) { | |
543 check = SK_MaxS32; | |
544 } else if (check < -SK_MaxS32) { | |
545 check = SK_MinS32; | |
546 } | |
547 REPORTER_ASSERT(reporter, result == (int32_t)check); | |
548 | |
549 // make them <= 2^24, so we don't overflow in fixmul | 513 // make them <= 2^24, so we don't overflow in fixmul |
550 numer = numer << 8 >> 8; | 514 numer = numer << 8 >> 8; |
551 denom = denom << 8 >> 8; | 515 denom = denom << 8 >> 8; |
552 | 516 |
553 result = SkFixedMul(numer, denom); | 517 result = SkFixedMul(numer, denom); |
554 SkFixed r2 = symmetric_fixmul(numer, denom); | 518 SkFixed r2 = symmetric_fixmul(numer, denom); |
555 // SkASSERT(result == r2); | 519 // SkASSERT(result == r2); |
556 | 520 |
557 result = SkFixedMul(numer, numer); | 521 result = SkFixedMul(numer, numer); |
558 r2 = SkFixedSquare(numer); | 522 r2 = SkFixedSquare(numer); |
559 REPORTER_ASSERT(reporter, result == r2); | 523 REPORTER_ASSERT(reporter, result == r2); |
560 | |
561 if (numer >= 0 && denom >= 0) { | |
562 SkFixed mean = SkFixedMean(numer, denom); | |
563 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom); | |
564 float fm = sk_float_sqrt(sk_float_abs(prod)); | |
565 SkFixed mean2 = SkFloatToFixed(fm); | |
566 int diff = SkAbs32(mean - mean2); | |
567 REPORTER_ASSERT(reporter, diff <= 1); | |
568 } | |
569 | |
570 { | |
571 SkFixed mod = SkFixedMod(numer, denom); | |
572 float n = SkFixedToFloat(numer); | |
573 float d = SkFixedToFloat(denom); | |
574 float m = sk_float_mod(n, d); | |
575 // ensure the same sign | |
576 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0)); | |
577 int diff = SkAbs32(mod - SkFloatToFixed(m)); | |
578 REPORTER_ASSERT(reporter, (diff >> 7) == 0); | |
579 } | |
580 } | 524 } |
581 #endif | 525 #endif |
582 | 526 |
583 for (i = 0; i < 10000; i++) { | |
584 SkFract x = rand.nextU() >> 1; | |
585 double xx = (double)x / SK_Fract1; | |
586 SkFract xr = SkFractSqrt(x); | |
587 SkFract check = SkFloatToFract(sqrt(xx)); | |
588 REPORTER_ASSERT(reporter, xr == check || | |
589 xr == check-1 || | |
590 xr == check+1); | |
591 | |
592 xr = SkFixedSqrt(x); | |
593 xx = (double)x / SK_Fixed1; | |
594 check = SkFloatToFixed(sqrt(xx)); | |
595 REPORTER_ASSERT(reporter, xr == check || xr == check-1); | |
596 | |
597 xr = SkSqrt32(x); | |
598 xx = (double)x; | |
599 check = (int32_t)sqrt(xx); | |
600 REPORTER_ASSERT(reporter, xr == check || xr == check-1); | |
601 } | |
602 | |
603 test_blend(reporter); | 527 test_blend(reporter); |
604 | 528 |
605 if (false) test_floor(reporter); | 529 if (false) test_floor(reporter); |
606 | 530 |
607 // disable for now | 531 // disable for now |
608 if (false) test_blend31(); // avoid bit rot, suppress warning | 532 if (false) test_blend31(); // avoid bit rot, suppress warning |
609 | 533 |
610 test_muldivround(reporter); | 534 test_muldivround(reporter); |
611 test_clz(reporter); | 535 test_clz(reporter); |
612 } | 536 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 test_divmod<int16_t>(r); | 635 test_divmod<int16_t>(r); |
712 } | 636 } |
713 | 637 |
714 DEF_TEST(divmod_s32, r) { | 638 DEF_TEST(divmod_s32, r) { |
715 test_divmod<int32_t>(r); | 639 test_divmod<int32_t>(r); |
716 } | 640 } |
717 | 641 |
718 DEF_TEST(divmod_s64, r) { | 642 DEF_TEST(divmod_s64, r) { |
719 test_divmod<int64_t>(r); | 643 test_divmod<int64_t>(r); |
720 } | 644 } |
OLD | NEW |