2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkRandom.h"
|
2011-04-18 14:15:36 +00:00
|
|
|
#include "Test.h"
|
2012-07-26 20:04:23 +00:00
|
|
|
#include "gradients/SkClampRange.h"
|
2011-04-18 14:15:36 +00:00
|
|
|
|
|
|
|
static skiatest::Reporter* gReporter;
|
2014-04-30 16:51:51 +00:00
|
|
|
#define R_ASSERT(cond) if (!(cond)) { \
|
|
|
|
SkDebugf("%d: %s\n", __LINE__, #cond); \
|
|
|
|
REPORTER_ASSERT(gReporter, cond); \
|
2011-04-18 19:59:38 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 16:51:51 +00:00
|
|
|
// Arbitrary sentinel values outside [0, 0xFFFF].
|
|
|
|
static const int kV0 = -42, kV1 = -53, kRamp = -64;
|
|
|
|
|
|
|
|
static void check_value(int64_t bigfx, int expected) {
|
|
|
|
if (bigfx < 0) {
|
|
|
|
R_ASSERT(expected == kV0);
|
2014-12-19 15:40:26 +00:00
|
|
|
} else if (bigfx > kFracMax_SkGradFixed) {
|
2014-04-30 16:51:51 +00:00
|
|
|
R_ASSERT(expected == kV1);
|
2014-12-19 15:40:26 +00:00
|
|
|
} else if (bigfx == kFracMax_SkGradFixed) {
|
2014-04-30 16:51:51 +00:00
|
|
|
// Either one is fine (and we do see both).
|
|
|
|
R_ASSERT(expected == kV1 || expected == kRamp);
|
|
|
|
} else {
|
|
|
|
R_ASSERT(expected == kRamp);
|
2011-04-18 14:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void slow_check(const SkClampRange& range,
|
2014-12-19 15:40:26 +00:00
|
|
|
const SkGradFixed fx, SkGradFixed dx, int count) {
|
2011-04-18 14:15:36 +00:00
|
|
|
SkASSERT(range.fCount0 + range.fCount1 + range.fCount2 == count);
|
2011-04-18 19:59:38 +00:00
|
|
|
|
2014-04-30 16:51:51 +00:00
|
|
|
// If dx is large, fx will overflow if updated naively. So we use more bits.
|
|
|
|
int64_t bigfx = fx;
|
|
|
|
|
2014-04-18 19:36:22 +00:00
|
|
|
for (int i = 0; i < range.fCount0; i++) {
|
2014-04-30 16:51:51 +00:00
|
|
|
check_value(bigfx, range.fV0);
|
|
|
|
bigfx += dx;
|
2014-04-18 19:36:22 +00:00
|
|
|
}
|
2014-04-30 16:51:51 +00:00
|
|
|
|
2014-04-18 19:36:22 +00:00
|
|
|
for (int i = 0; i < range.fCount1; i++) {
|
2014-04-30 16:51:51 +00:00
|
|
|
check_value(bigfx, kRamp);
|
|
|
|
bigfx += dx;
|
2014-04-18 19:36:22 +00:00
|
|
|
}
|
2014-04-30 16:51:51 +00:00
|
|
|
|
2014-04-18 19:36:22 +00:00
|
|
|
for (int i = 0; i < range.fCount2; i++) {
|
2014-04-30 16:51:51 +00:00
|
|
|
check_value(bigfx, range.fV1);
|
|
|
|
bigfx += dx;
|
2011-04-18 14:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2011-04-18 14:15:36 +00:00
|
|
|
static void test_range(SkFixed fx, SkFixed dx, int count) {
|
2014-12-19 15:40:26 +00:00
|
|
|
const SkGradFixed gfx = SkFixedToGradFixed(fx);
|
|
|
|
const SkGradFixed gdx = SkFixedToGradFixed(dx);
|
|
|
|
|
2011-04-18 14:15:36 +00:00
|
|
|
SkClampRange range;
|
2014-12-19 15:40:26 +00:00
|
|
|
range.init(gfx, gdx, count, kV0, kV1);
|
|
|
|
slow_check(range, gfx, gdx, count);
|
2011-04-18 14:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ff(x) SkIntToFixed(x)
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(ClampRange, reporter) {
|
2011-04-18 14:15:36 +00:00
|
|
|
gReporter = reporter;
|
|
|
|
|
|
|
|
test_range(0, 0, 20);
|
|
|
|
test_range(0xFFFF, 0, 20);
|
|
|
|
test_range(-ff(2), 0, 20);
|
|
|
|
test_range( ff(2), 0, 20);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-04-18 14:15:36 +00:00
|
|
|
test_range(-10, 1, 20);
|
|
|
|
test_range(10, -1, 20);
|
|
|
|
test_range(-10, 3, 20);
|
|
|
|
test_range(10, -3, 20);
|
2011-04-18 19:59:38 +00:00
|
|
|
|
|
|
|
test_range(ff(1), ff(16384), 100);
|
|
|
|
test_range(ff(-1), ff(-16384), 100);
|
|
|
|
test_range(ff(1)/2, ff(16384), 100);
|
2015-11-11 16:46:34 +00:00
|
|
|
test_range(ff(1)/2, ff(-16384), 100);
|
2011-04-18 19:59:38 +00:00
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-04-18 14:15:36 +00:00
|
|
|
// test non-overflow cases
|
|
|
|
for (int i = 0; i < 1000000; i++) {
|
|
|
|
SkFixed fx = rand.nextS() >> 1;
|
|
|
|
SkFixed sx = rand.nextS() >> 1;
|
|
|
|
int count = rand.nextU() % 1000 + 1;
|
|
|
|
SkFixed dx = (sx - fx) / count;
|
|
|
|
test_range(fx, dx, count);
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2014-04-30 16:51:51 +00:00
|
|
|
// TODO(reed): skia:2481, fix whatever bug this is, then uncomment
|
|
|
|
/*
|
2011-04-18 14:15:36 +00:00
|
|
|
// test overflow cases
|
2011-04-18 19:59:38 +00:00
|
|
|
for (int i = 0; i < 100000; i++) {
|
2011-04-18 14:15:36 +00:00
|
|
|
SkFixed fx = rand.nextS();
|
2011-04-18 19:59:38 +00:00
|
|
|
SkFixed dx = rand.nextS();
|
2011-04-18 14:15:36 +00:00
|
|
|
int count = rand.nextU() % 1000 + 1;
|
|
|
|
test_range(fx, dx, count);
|
|
|
|
}
|
2014-04-30 16:51:51 +00:00
|
|
|
*/
|
2011-04-18 14:15:36 +00:00
|
|
|
}
|