ClampRangeTest.cpp revision e4fafb146e85cdfcf9d5418597b6818aa0754ada
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 */
7
8#include "Test.h"
9#include "TestClassDef.h"
10#include "gradients/SkClampRange.h"
11#include "SkRandom.h"
12
13static skiatest::Reporter* gReporter;
14
15static 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
36static 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    }
43    R_ASSERT(false);
44    return 0;
45}
46
47#define V0  -42
48#define V1  1024
49
50static void slow_check(const SkClampRange& range,
51                       SkFixed fx, SkFixed dx, int count) {
52    SkASSERT(range.fCount0 + range.fCount1 + range.fCount2 == count);
53
54    int i;
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);
69            R_ASSERT(false); // bad fFx1
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        }
81    }
82}
83
84
85static 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
93DEF_TEST(ClampRange, reporter) {
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);
100
101    test_range(-10, 1, 20);
102    test_range(10, -1, 20);
103    test_range(-10, 3, 20);
104    test_range(10, -3, 20);
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
111    SkRandom rand;
112
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    }
121
122    // test overflow cases
123    for (int i = 0; i < 100000; i++) {
124        SkFixed fx = rand.nextS();
125        SkFixed dx = rand.nextS();
126        int count = rand.nextU() % 1000 + 1;
127        test_range(fx, dx, count);
128    }
129}
130