epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 8 | #include "Test.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "TestClassDef.h" |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 10 | #include "gradients/SkClampRange.h" |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 11 | #include "SkRandom.h" |
| 12 | |
| 13 | static skiatest::Reporter* gReporter; |
| 14 | |
reed@google.com | 13659f1 | 2011-04-18 19:59:38 +0000 | [diff] [blame] | 15 | static void debug_me() { |
| 16 | if (NULL == gReporter) { |
| 17 | SkDebugf("dsfdssd\n"); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | #ifdef USE_REPORTER |
| 22 | |
| 23 | #define R_ASSERT(cond) \ |
| 24 | do { if (!(cond)) { \ |
| 25 | debug_me(); \ |
| 26 | REPORTER_ASSERT(gReporter, cond); \ |
| 27 | }} while (0) |
| 28 | |
| 29 | #else |
| 30 | #define R_ASSERT(cond) \ |
| 31 | do { if (!(cond)) { \ |
| 32 | debug_me(); \ |
| 33 | }} while (0) |
| 34 | #endif |
| 35 | |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 36 | static int classify_value(SkFixed fx, int v0, int v1) { |
| 37 | if (fx <= 0) { |
| 38 | return v0; |
| 39 | } |
| 40 | if (fx >= 0xFFFF) { |
| 41 | return v1; |
| 42 | } |
reed@google.com | 13659f1 | 2011-04-18 19:59:38 +0000 | [diff] [blame] | 43 | R_ASSERT(false); |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | #define V0 -42 |
| 48 | #define V1 1024 |
| 49 | |
| 50 | static void slow_check(const SkClampRange& range, |
| 51 | SkFixed fx, SkFixed dx, int count) { |
| 52 | SkASSERT(range.fCount0 + range.fCount1 + range.fCount2 == count); |
reed@google.com | 13659f1 | 2011-04-18 19:59:38 +0000 | [diff] [blame] | 53 | |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 54 | int i; |
reed@google.com | 13659f1 | 2011-04-18 19:59:38 +0000 | [diff] [blame] | 55 | if (range.fOverflowed) { |
| 56 | fx = range.fFx1; |
| 57 | for (i = 0; i < range.fCount1; i++) { |
| 58 | R_ASSERT(fx >= 0 && fx <= 0xFFFF); |
| 59 | fx += dx; |
| 60 | } |
| 61 | } else { |
| 62 | for (i = 0; i < range.fCount0; i++) { |
| 63 | int v = classify_value(fx, V0, V1); |
| 64 | R_ASSERT(v == range.fV0); |
| 65 | fx += dx; |
| 66 | } |
| 67 | if (range.fCount1 > 0 && fx != range.fFx1) { |
| 68 | SkDebugf("%x %x\n", fx, range.fFx1); |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 69 | R_ASSERT(false); // bad fFx1 |
reed@google.com | 13659f1 | 2011-04-18 19:59:38 +0000 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | for (i = 0; i < range.fCount1; i++) { |
| 73 | R_ASSERT(fx >= 0 && fx <= 0xFFFF); |
| 74 | fx += dx; |
| 75 | } |
| 76 | for (i = 0; i < range.fCount2; i++) { |
| 77 | int v = classify_value(fx, V0, V1); |
| 78 | R_ASSERT(v == range.fV1); |
| 79 | fx += dx; |
| 80 | } |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 84 | |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 85 | static void test_range(SkFixed fx, SkFixed dx, int count) { |
| 86 | SkClampRange range; |
| 87 | range.init(fx, dx, count, V0, V1); |
| 88 | slow_check(range, fx, dx, count); |
| 89 | } |
| 90 | |
| 91 | #define ff(x) SkIntToFixed(x) |
| 92 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 93 | DEF_TEST(ClampRange, reporter) { |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 94 | gReporter = reporter; |
| 95 | |
| 96 | test_range(0, 0, 20); |
| 97 | test_range(0xFFFF, 0, 20); |
| 98 | test_range(-ff(2), 0, 20); |
| 99 | test_range( ff(2), 0, 20); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 100 | |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 101 | test_range(-10, 1, 20); |
| 102 | test_range(10, -1, 20); |
| 103 | test_range(-10, 3, 20); |
| 104 | test_range(10, -3, 20); |
reed@google.com | 13659f1 | 2011-04-18 19:59:38 +0000 | [diff] [blame] | 105 | |
| 106 | test_range(ff(1), ff(16384), 100); |
| 107 | test_range(ff(-1), ff(-16384), 100); |
| 108 | test_range(ff(1)/2, ff(16384), 100); |
| 109 | test_range(ff(1)/2, ff(-16384), 100); |
| 110 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 111 | SkRandom rand; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 112 | |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 113 | // test non-overflow cases |
| 114 | for (int i = 0; i < 1000000; i++) { |
| 115 | SkFixed fx = rand.nextS() >> 1; |
| 116 | SkFixed sx = rand.nextS() >> 1; |
| 117 | int count = rand.nextU() % 1000 + 1; |
| 118 | SkFixed dx = (sx - fx) / count; |
| 119 | test_range(fx, dx, count); |
| 120 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 121 | |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 122 | // test overflow cases |
reed@google.com | 13659f1 | 2011-04-18 19:59:38 +0000 | [diff] [blame] | 123 | for (int i = 0; i < 100000; i++) { |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 124 | SkFixed fx = rand.nextS(); |
reed@google.com | 13659f1 | 2011-04-18 19:59:38 +0000 | [diff] [blame] | 125 | SkFixed dx = rand.nextS(); |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 126 | int count = rand.nextU() % 1000 + 1; |
reed@google.com | 63c1ad8 | 2011-04-18 14:15:36 +0000 | [diff] [blame] | 127 | test_range(fx, dx, count); |
| 128 | } |
| 129 | } |