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