| 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 "SkRandom.h" | 8 #include "SkRandom.h" |
| 9 #include "Test.h" | 9 #include "Test.h" |
| 10 #include "gradients/SkClampRange.h" | 10 #include "gradients/SkClampRange.h" |
| 11 | 11 |
| 12 static skiatest::Reporter* gReporter; | 12 static skiatest::Reporter* gReporter; |
| 13 #define R_ASSERT(cond) if (!(cond)) { \ | 13 #define R_ASSERT(cond) if (!(cond)) { \ |
| 14 SkDebugf("%d: %s\n", __LINE__, #cond); \ | 14 SkDebugf("%d: %s\n", __LINE__, #cond); \ |
| 15 REPORTER_ASSERT(gReporter, cond); \ | 15 REPORTER_ASSERT(gReporter, cond); \ |
| 16 } | 16 } |
| 17 | 17 |
| 18 // Arbitrary sentinel values outside [0, 0xFFFF]. | 18 // Arbitrary sentinel values outside [0, 0xFFFF]. |
| 19 static const int kV0 = -42, kV1 = -53, kRamp = -64; | 19 static const int kV0 = -42, kV1 = -53, kRamp = -64; |
| 20 | 20 |
| 21 static void check_value(int64_t bigfx, int expected) { | 21 static void check_value(int64_t bigfx, int expected) { |
| 22 if (bigfx < 0) { | 22 if (bigfx < 0) { |
| 23 R_ASSERT(expected == kV0); | 23 R_ASSERT(expected == kV0); |
| 24 } else if (bigfx > 0xFFFF) { | 24 } else if (bigfx > kFracMax_SkGradFixed) { |
| 25 R_ASSERT(expected == kV1); | 25 R_ASSERT(expected == kV1); |
| 26 } else if (bigfx == 0xFFFF) { | 26 } else if (bigfx == kFracMax_SkGradFixed) { |
| 27 // Either one is fine (and we do see both). | 27 // Either one is fine (and we do see both). |
| 28 R_ASSERT(expected == kV1 || expected == kRamp); | 28 R_ASSERT(expected == kV1 || expected == kRamp); |
| 29 } else { | 29 } else { |
| 30 R_ASSERT(expected == kRamp); | 30 R_ASSERT(expected == kRamp); |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 | 33 |
| 34 static void slow_check(const SkClampRange& range, | 34 static void slow_check(const SkClampRange& range, |
| 35 const SkFixed fx, SkFixed dx, int count) { | 35 const SkGradFixed fx, SkGradFixed dx, int count) { |
| 36 SkASSERT(range.fCount0 + range.fCount1 + range.fCount2 == count); | 36 SkASSERT(range.fCount0 + range.fCount1 + range.fCount2 == count); |
| 37 | 37 |
| 38 // If dx is large, fx will overflow if updated naively. So we use more bits
. | 38 // If dx is large, fx will overflow if updated naively. So we use more bits
. |
| 39 int64_t bigfx = fx; | 39 int64_t bigfx = fx; |
| 40 | 40 |
| 41 for (int i = 0; i < range.fCount0; i++) { | 41 for (int i = 0; i < range.fCount0; i++) { |
| 42 check_value(bigfx, range.fV0); | 42 check_value(bigfx, range.fV0); |
| 43 bigfx += dx; | 43 bigfx += dx; |
| 44 } | 44 } |
| 45 | 45 |
| 46 for (int i = 0; i < range.fCount1; i++) { | 46 for (int i = 0; i < range.fCount1; i++) { |
| 47 check_value(bigfx, kRamp); | 47 check_value(bigfx, kRamp); |
| 48 bigfx += dx; | 48 bigfx += dx; |
| 49 } | 49 } |
| 50 | 50 |
| 51 for (int i = 0; i < range.fCount2; i++) { | 51 for (int i = 0; i < range.fCount2; i++) { |
| 52 check_value(bigfx, range.fV1); | 52 check_value(bigfx, range.fV1); |
| 53 bigfx += dx; | 53 bigfx += dx; |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 | 57 |
| 58 static void test_range(SkFixed fx, SkFixed dx, int count) { | 58 static void test_range(SkFixed fx, SkFixed dx, int count) { |
| 59 const SkGradFixed gfx = SkFixedToGradFixed(fx); |
| 60 const SkGradFixed gdx = SkFixedToGradFixed(dx); |
| 61 |
| 59 SkClampRange range; | 62 SkClampRange range; |
| 60 range.init(fx, dx, count, kV0, kV1); | 63 range.init(gfx, gdx, count, kV0, kV1); |
| 61 slow_check(range, fx, dx, count); | 64 slow_check(range, gfx, gdx, count); |
| 62 } | 65 } |
| 63 | 66 |
| 64 #define ff(x) SkIntToFixed(x) | 67 #define ff(x) SkIntToFixed(x) |
| 65 | 68 |
| 66 DEF_TEST(ClampRange, reporter) { | 69 DEF_TEST(ClampRange, reporter) { |
| 67 gReporter = reporter; | 70 gReporter = reporter; |
| 68 | 71 |
| 69 test_range(0, 0, 20); | 72 test_range(0, 0, 20); |
| 70 test_range(0xFFFF, 0, 20); | 73 test_range(0xFFFF, 0, 20); |
| 71 test_range(-ff(2), 0, 20); | 74 test_range(-ff(2), 0, 20); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 97 /* | 100 /* |
| 98 // test overflow cases | 101 // test overflow cases |
| 99 for (int i = 0; i < 100000; i++) { | 102 for (int i = 0; i < 100000; i++) { |
| 100 SkFixed fx = rand.nextS(); | 103 SkFixed fx = rand.nextS(); |
| 101 SkFixed dx = rand.nextS(); | 104 SkFixed dx = rand.nextS(); |
| 102 int count = rand.nextU() % 1000 + 1; | 105 int count = rand.nextU() % 1000 + 1; |
| 103 test_range(fx, dx, count); | 106 test_range(fx, dx, count); |
| 104 } | 107 } |
| 105 */ | 108 */ |
| 106 } | 109 } |
| OLD | NEW |