blob: 9027e9b31146323afd58e97f7b84529214b74c9c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.orge4fafb12013-12-12 21:11:12 +00007
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00008#include "SkRandom.h"
reed@google.com63c1ad82011-04-18 14:15:36 +00009#include "Test.h"
rileya@google.com589708b2012-07-26 20:04:23 +000010#include "gradients/SkClampRange.h"
reed@google.com63c1ad82011-04-18 14:15:36 +000011
12static skiatest::Reporter* gReporter;
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000013#define R_ASSERT(cond) if (!(cond)) { \
14 SkDebugf("%d: %s\n", __LINE__, #cond); \
15 REPORTER_ASSERT(gReporter, cond); \
reed@google.com13659f12011-04-18 19:59:38 +000016}
17
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000018// Arbitrary sentinel values outside [0, 0xFFFF].
19static const int kV0 = -42, kV1 = -53, kRamp = -64;
reed@google.com13659f12011-04-18 19:59:38 +000020
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000021static void check_value(int64_t bigfx, int expected) {
22 if (bigfx < 0) {
23 R_ASSERT(expected == kV0);
reed438b0d72014-12-19 07:40:26 -080024 } else if (bigfx > kFracMax_SkGradFixed) {
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000025 R_ASSERT(expected == kV1);
reed438b0d72014-12-19 07:40:26 -080026 } else if (bigfx == kFracMax_SkGradFixed) {
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000027 // Either one is fine (and we do see both).
28 R_ASSERT(expected == kV1 || expected == kRamp);
29 } else {
30 R_ASSERT(expected == kRamp);
reed@google.com63c1ad82011-04-18 14:15:36 +000031 }
reed@google.com63c1ad82011-04-18 14:15:36 +000032}
33
reed@google.com63c1ad82011-04-18 14:15:36 +000034static void slow_check(const SkClampRange& range,
reed438b0d72014-12-19 07:40:26 -080035 const SkGradFixed fx, SkGradFixed dx, int count) {
reed@google.com63c1ad82011-04-18 14:15:36 +000036 SkASSERT(range.fCount0 + range.fCount1 + range.fCount2 == count);
reed@google.com13659f12011-04-18 19:59:38 +000037
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000038 // If dx is large, fx will overflow if updated naively. So we use more bits.
39 int64_t bigfx = fx;
40
reed@google.com33a94e22014-04-18 19:36:22 +000041 for (int i = 0; i < range.fCount0; i++) {
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000042 check_value(bigfx, range.fV0);
43 bigfx += dx;
reed@google.com33a94e22014-04-18 19:36:22 +000044 }
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000045
reed@google.com33a94e22014-04-18 19:36:22 +000046 for (int i = 0; i < range.fCount1; i++) {
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000047 check_value(bigfx, kRamp);
48 bigfx += dx;
reed@google.com33a94e22014-04-18 19:36:22 +000049 }
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000050
reed@google.com33a94e22014-04-18 19:36:22 +000051 for (int i = 0; i < range.fCount2; i++) {
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000052 check_value(bigfx, range.fV1);
53 bigfx += dx;
reed@google.com63c1ad82011-04-18 14:15:36 +000054 }
55}
56
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000057
reed@google.com63c1ad82011-04-18 14:15:36 +000058static void test_range(SkFixed fx, SkFixed dx, int count) {
reed438b0d72014-12-19 07:40:26 -080059 const SkGradFixed gfx = SkFixedToGradFixed(fx);
60 const SkGradFixed gdx = SkFixedToGradFixed(dx);
61
reed@google.com63c1ad82011-04-18 14:15:36 +000062 SkClampRange range;
reed438b0d72014-12-19 07:40:26 -080063 range.init(gfx, gdx, count, kV0, kV1);
64 slow_check(range, gfx, gdx, count);
reed@google.com63c1ad82011-04-18 14:15:36 +000065}
66
67#define ff(x) SkIntToFixed(x)
68
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000069DEF_TEST(ClampRange, reporter) {
reed@google.com63c1ad82011-04-18 14:15:36 +000070 gReporter = reporter;
71
72 test_range(0, 0, 20);
73 test_range(0xFFFF, 0, 20);
74 test_range(-ff(2), 0, 20);
75 test_range( ff(2), 0, 20);
rmistry@google.comd6176b02012-08-23 18:14:13 +000076
reed@google.com63c1ad82011-04-18 14:15:36 +000077 test_range(-10, 1, 20);
78 test_range(10, -1, 20);
79 test_range(-10, 3, 20);
80 test_range(10, -3, 20);
reed@google.com13659f12011-04-18 19:59:38 +000081
82 test_range(ff(1), ff(16384), 100);
83 test_range(ff(-1), ff(-16384), 100);
84 test_range(ff(1)/2, ff(16384), 100);
benjaminwagner0b2a1892015-11-11 08:46:34 -080085 test_range(ff(1)/2, ff(-16384), 100);
reed@google.com13659f12011-04-18 19:59:38 +000086
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000087 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +000088
reed@google.com63c1ad82011-04-18 14:15:36 +000089 // test non-overflow cases
90 for (int i = 0; i < 1000000; i++) {
91 SkFixed fx = rand.nextS() >> 1;
92 SkFixed sx = rand.nextS() >> 1;
93 int count = rand.nextU() % 1000 + 1;
94 SkFixed dx = (sx - fx) / count;
95 test_range(fx, dx, count);
96 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000097
commit-bot@chromium.org86398e52014-04-30 16:51:51 +000098 // TODO(reed): skia:2481, fix whatever bug this is, then uncomment
99 /*
reed@google.com63c1ad82011-04-18 14:15:36 +0000100 // test overflow cases
reed@google.com13659f12011-04-18 19:59:38 +0000101 for (int i = 0; i < 100000; i++) {
reed@google.com63c1ad82011-04-18 14:15:36 +0000102 SkFixed fx = rand.nextS();
reed@google.com13659f12011-04-18 19:59:38 +0000103 SkFixed dx = rand.nextS();
reed@google.com63c1ad82011-04-18 14:15:36 +0000104 int count = rand.nextU() % 1000 + 1;
reed@google.com63c1ad82011-04-18 14:15:36 +0000105 test_range(fx, dx, count);
106 }
commit-bot@chromium.org86398e52014-04-30 16:51:51 +0000107 */
reed@google.com63c1ad82011-04-18 14:15:36 +0000108}