blob: dc520711a3fc5c8c5ae2f8ad72d983e8bea9e008 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com63c1ad82011-04-18 14:15:36 +00008#include "Test.h"
rileya@google.com589708b2012-07-26 20:04:23 +00009#include "gradients/SkClampRange.h"
reed@google.com63c1ad82011-04-18 14:15:36 +000010#include "SkRandom.h"
11
12static skiatest::Reporter* gReporter;
13
reed@google.com13659f12011-04-18 19:59:38 +000014static 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.com63c1ad82011-04-18 14:15:36 +000035static 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.com13659f12011-04-18 19:59:38 +000042 R_ASSERT(false);
reed@google.com63c1ad82011-04-18 14:15:36 +000043 return 0;
44}
45
46#define V0 -42
47#define V1 1024
48
49static void slow_check(const SkClampRange& range,
50 SkFixed fx, SkFixed dx, int count) {
51 SkASSERT(range.fCount0 + range.fCount1 + range.fCount2 == count);
reed@google.com13659f12011-04-18 19:59:38 +000052
reed@google.com63c1ad82011-04-18 14:15:36 +000053 int i;
reed@google.com13659f12011-04-18 19:59:38 +000054 if (range.fOverflowed) {
55 fx = range.fFx1;
56 for (i = 0; i < range.fCount1; i++) {
57 R_ASSERT(fx >= 0 && fx <= 0xFFFF);
58 fx += dx;
59 }
60 } else {
61 for (i = 0; i < range.fCount0; i++) {
62 int v = classify_value(fx, V0, V1);
63 R_ASSERT(v == range.fV0);
64 fx += dx;
65 }
66 if (range.fCount1 > 0 && fx != range.fFx1) {
67 SkDebugf("%x %x\n", fx, range.fFx1);
68 R_ASSERT(!"bad fFx1");
69 return;
70 }
71 for (i = 0; i < range.fCount1; i++) {
72 R_ASSERT(fx >= 0 && fx <= 0xFFFF);
73 fx += dx;
74 }
75 for (i = 0; i < range.fCount2; i++) {
76 int v = classify_value(fx, V0, V1);
77 R_ASSERT(v == range.fV1);
78 fx += dx;
79 }
reed@google.com63c1ad82011-04-18 14:15:36 +000080 }
81}
82
83static void test_range(SkFixed fx, SkFixed dx, int count) {
84 SkClampRange range;
85 range.init(fx, dx, count, V0, V1);
86 slow_check(range, fx, dx, count);
87}
88
89#define ff(x) SkIntToFixed(x)
90
reed@google.com13659f12011-04-18 19:59:38 +000091void TestClampRange(skiatest::Reporter* reporter);
92void TestClampRange(skiatest::Reporter* reporter) {
reed@google.com63c1ad82011-04-18 14:15:36 +000093 gReporter = reporter;
94
95 test_range(0, 0, 20);
96 test_range(0xFFFF, 0, 20);
97 test_range(-ff(2), 0, 20);
98 test_range( ff(2), 0, 20);
rmistry@google.comd6176b02012-08-23 18:14:13 +000099
reed@google.com63c1ad82011-04-18 14:15:36 +0000100 test_range(-10, 1, 20);
101 test_range(10, -1, 20);
102 test_range(-10, 3, 20);
103 test_range(10, -3, 20);
reed@google.com13659f12011-04-18 19:59:38 +0000104
105 test_range(ff(1), ff(16384), 100);
106 test_range(ff(-1), ff(-16384), 100);
107 test_range(ff(1)/2, ff(16384), 100);
108 test_range(ff(1)/2, ff(-16384), 100);
109
jvanverth@google.comc490f802013-03-04 13:56:38 +0000110 SkMWCRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111
reed@google.com63c1ad82011-04-18 14:15:36 +0000112 // test non-overflow cases
113 for (int i = 0; i < 1000000; i++) {
114 SkFixed fx = rand.nextS() >> 1;
115 SkFixed sx = rand.nextS() >> 1;
116 int count = rand.nextU() % 1000 + 1;
117 SkFixed dx = (sx - fx) / count;
118 test_range(fx, dx, count);
119 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120
reed@google.com63c1ad82011-04-18 14:15:36 +0000121 // test overflow cases
reed@google.com13659f12011-04-18 19:59:38 +0000122 for (int i = 0; i < 100000; i++) {
reed@google.com63c1ad82011-04-18 14:15:36 +0000123 SkFixed fx = rand.nextS();
reed@google.com13659f12011-04-18 19:59:38 +0000124 SkFixed dx = rand.nextS();
reed@google.com63c1ad82011-04-18 14:15:36 +0000125 int count = rand.nextU() % 1000 + 1;
reed@google.com63c1ad82011-04-18 14:15:36 +0000126 test_range(fx, dx, count);
127 }
128}
129
reed@google.com13659f12011-04-18 19:59:38 +0000130#ifdef USE_REPORTER
131
reed@google.com63c1ad82011-04-18 14:15:36 +0000132#include "TestClassDef.h"
133DEFINE_TESTCLASS("ClampRange", ClampRangeClass, TestClampRange)
reed@google.com13659f12011-04-18 19:59:38 +0000134
135#endif