blob: 3da0e5e90275da0afc16cf95b8cdd2ceb26b9f08 [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
reed@google.com6898d522012-11-08 22:36:19 +00008#include "SkRandom.h"
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +00009#include "SkRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.h"
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000011
reed@google.com534240f2011-02-07 19:08:59 +000012static float make_zero() {
13 return sk_float_sin(0);
14}
reed@android.comd4134452011-02-09 02:24:26 +000015
reed@google.com2b57dc62013-01-08 13:23:32 +000016struct RectCenter {
17 SkIRect fRect;
18 SkIPoint fCenter;
19};
20
reed@google.com6898d522012-11-08 22:36:19 +000021static void test_center(skiatest::Reporter* reporter) {
reed@google.com2b57dc62013-01-08 13:23:32 +000022 static const RectCenter gData[] = {
reed@google.com6898d522012-11-08 22:36:19 +000023 { { 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 };
reed@google.com2b57dc62013-01-08 13:23:32 +000029 for (size_t index = 0; index < SK_ARRAY_COUNT(gData); ++index) {
reed@google.com6898d522012-11-08 22:36:19 +000030 REPORTER_ASSERT(reporter,
reed@google.com2b57dc62013-01-08 13:23:32 +000031 gData[index].fRect.centerX() == gData[index].fCenter.x());
reed@google.com6898d522012-11-08 22:36:19 +000032 REPORTER_ASSERT(reporter,
reed@google.com2b57dc62013-01-08 13:23:32 +000033 gData[index].fRect.centerY() == gData[index].fCenter.y());
reed@google.com6898d522012-11-08 22:36:19 +000034 }
35
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000036 SkRandom rand;
reed@google.com6898d522012-11-08 22:36:19 +000037 for (int i = 0; i < 10000; ++i) {
38 SkIRect r;
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +000039
reed@google.com6898d522012-11-08 22:36:19 +000040 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();
robertphillips@google.comca47aae2012-12-12 15:58:25 +000044 REPORTER_ASSERT(reporter, ((r.left() + r.right()) >> 1) == cx);
45 REPORTER_ASSERT(reporter, ((r.top() + r.bottom()) >> 1) == cy);
reed@google.com6898d522012-11-08 22:36:19 +000046 }
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.
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000058DEF_TEST(InfRect, reporter) {
reed@google.com7b463ac2012-05-16 13:35:36 +000059 float inf = 1 / make_zero(); // infinity
60 float nan = inf * 0;
61 SkASSERT(!(nan == nan));
reed@android.comd4134452011-02-09 02:24:26 +000062 SkScalar small = SkIntToScalar(10);
63 SkScalar big = SkIntToScalar(100);
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000064
reed@google.com7b463ac2012-05-16 13:35:36 +000065 REPORTER_ASSERT(reporter, SkRect::MakeEmpty().isFinite());
66
reed@android.comd4134452011-02-09 02:24:26 +000067 SkRect rect = SkRect::MakeXYWH(small, small, big, big);
reed@google.com16078632011-12-06 18:56:37 +000068 REPORTER_ASSERT(reporter, rect.isFinite());
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000069
reed@google.com7b463ac2012-05-16 13:35:36 +000070 const SkScalar invalid[] = { nan, inf, -inf };
71 for (size_t i = 0; i < SK_ARRAY_COUNT(invalid); ++i) {
72 check_invalid(reporter, small, small, big, invalid[i]);
73 check_invalid(reporter, small, small, invalid[i], big);
74 check_invalid(reporter, small, invalid[i], big, big);
75 check_invalid(reporter, invalid[i], small, big, big);
76 }
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +000077
reed@google.com6898d522012-11-08 22:36:19 +000078 test_center(reporter);
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000079}
80
81// need tests for SkStrSearch