blob: f1b2677236fe08e928ad8846521fd40b4fb0b5a1 [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 */
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +00008#include "Test.h"
reed@google.com6898d522012-11-08 22:36:19 +00009#include "SkRandom.h"
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000010#include "SkRect.h"
11
reed@android.comd4134452011-02-09 02:24:26 +000012#ifdef SK_SCALAR_IS_FLOAT
reed@google.com534240f2011-02-07 19:08:59 +000013static float make_zero() {
14 return sk_float_sin(0);
15}
reed@android.comd4134452011-02-09 02:24:26 +000016#endif
17
reed@google.com6898d522012-11-08 22:36:19 +000018static void test_center(skiatest::Reporter* reporter) {
19 static const struct {
20 SkIRect fRect;
21 SkIPoint fCenter;
22 } data[] = {
23 { { 0, 0, 0, 0 }, { 0, 0 } },
24 { { 0, 0, 1, 1 }, { 0, 0 } },
25 { { -1, -1, 0, 0 }, { -1, -1 } },
26 { { 0, 0, 10, 7 }, { 5, 3 } },
27 { { 0, 0, 11, 6 }, { 5, 3 } },
28 };
29 for (size_t index = 0; index < SK_ARRAY_COUNT(data); ++index) {
30 REPORTER_ASSERT(reporter,
31 data[index].fRect.centerX() == data[index].fCenter.x());
32 REPORTER_ASSERT(reporter,
33 data[index].fRect.centerY() == data[index].fCenter.y());
34 }
35
36 SkRandom rand;
37 for (int i = 0; i < 10000; ++i) {
38 SkIRect r;
39
40 r.set(rand.nextS() >> 2, rand.nextS() >> 2,
41 rand.nextS() >> 2, rand.nextS() >> 2);
42 int cx = r.centerX();
43 int cy = r.centerY();
44 REPORTER_ASSERT(reporter, (r.left() + r.right() >> 1) == cx);
45 REPORTER_ASSERT(reporter, (r.top() + r.bottom() >> 1) == cy);
46 }
47}
48
reed@android.comd4134452011-02-09 02:24:26 +000049static void check_invalid(skiatest::Reporter* reporter,
50 SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
51 SkRect rect;
52 rect.set(l, t, r, b);
reed@google.com16078632011-12-06 18:56:37 +000053 REPORTER_ASSERT(reporter, !rect.isFinite());
reed@android.comd4134452011-02-09 02:24:26 +000054}
reed@google.com534240f2011-02-07 19:08:59 +000055
reed@google.com16078632011-12-06 18:56:37 +000056// Tests that isFinite() will reject any rect with +/-inf values
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000057// as one of its coordinates.
58static void TestInfRect(skiatest::Reporter* reporter) {
reed@android.comd4134452011-02-09 02:24:26 +000059#ifdef SK_SCALAR_IS_FLOAT
reed@google.com7b463ac2012-05-16 13:35:36 +000060 float inf = 1 / make_zero(); // infinity
61 float nan = inf * 0;
62 SkASSERT(!(nan == nan));
reed@android.comd4134452011-02-09 02:24:26 +000063#else
reed@google.com7b463ac2012-05-16 13:35:36 +000064 SkFixed inf = SK_FixedNaN;
65 SkFixed nan = SK_FixedNaN;
reed@android.comd4134452011-02-09 02:24:26 +000066#endif
67 SkScalar small = SkIntToScalar(10);
68 SkScalar big = SkIntToScalar(100);
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000069
reed@google.com7b463ac2012-05-16 13:35:36 +000070 REPORTER_ASSERT(reporter, SkRect::MakeEmpty().isFinite());
71
reed@android.comd4134452011-02-09 02:24:26 +000072 SkRect rect = SkRect::MakeXYWH(small, small, big, big);
reed@google.com16078632011-12-06 18:56:37 +000073 REPORTER_ASSERT(reporter, rect.isFinite());
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000074
reed@google.com7b463ac2012-05-16 13:35:36 +000075 const SkScalar invalid[] = { nan, inf, -inf };
76 for (size_t i = 0; i < SK_ARRAY_COUNT(invalid); ++i) {
77 check_invalid(reporter, small, small, big, invalid[i]);
78 check_invalid(reporter, small, small, invalid[i], big);
79 check_invalid(reporter, small, invalid[i], big, big);
80 check_invalid(reporter, invalid[i], small, big, big);
81 }
reed@google.com6898d522012-11-08 22:36:19 +000082
83 test_center(reporter);
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000084}
85
86// need tests for SkStrSearch
87
88#include "TestClassDef.h"
89DEFINE_TESTCLASS("InfRect", InfRectTestClass, TestInfRect)