blob: 3d964bdccf4b9f40380a5975d9f609e9366ca066 [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
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +00008#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@google.com6898d522012-11-08 22:36:19 +000010#include "SkRandom.h"
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000011#include "SkRect.h"
12
reed@android.comd4134452011-02-09 02:24:26 +000013#ifdef SK_SCALAR_IS_FLOAT
reed@google.com534240f2011-02-07 19:08:59 +000014static float make_zero() {
15 return sk_float_sin(0);
16}
reed@android.comd4134452011-02-09 02:24:26 +000017#endif
18
reed@google.com2b57dc62013-01-08 13:23:32 +000019struct RectCenter {
20 SkIRect fRect;
21 SkIPoint fCenter;
22};
23
reed@google.com6898d522012-11-08 22:36:19 +000024static void test_center(skiatest::Reporter* reporter) {
reed@google.com2b57dc62013-01-08 13:23:32 +000025 static const RectCenter gData[] = {
reed@google.com6898d522012-11-08 22:36:19 +000026 { { 0, 0, 0, 0 }, { 0, 0 } },
27 { { 0, 0, 1, 1 }, { 0, 0 } },
28 { { -1, -1, 0, 0 }, { -1, -1 } },
29 { { 0, 0, 10, 7 }, { 5, 3 } },
30 { { 0, 0, 11, 6 }, { 5, 3 } },
31 };
reed@google.com2b57dc62013-01-08 13:23:32 +000032 for (size_t index = 0; index < SK_ARRAY_COUNT(gData); ++index) {
reed@google.com6898d522012-11-08 22:36:19 +000033 REPORTER_ASSERT(reporter,
reed@google.com2b57dc62013-01-08 13:23:32 +000034 gData[index].fRect.centerX() == gData[index].fCenter.x());
reed@google.com6898d522012-11-08 22:36:19 +000035 REPORTER_ASSERT(reporter,
reed@google.com2b57dc62013-01-08 13:23:32 +000036 gData[index].fRect.centerY() == gData[index].fCenter.y());
reed@google.com6898d522012-11-08 22:36:19 +000037 }
38
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000039 SkRandom rand;
reed@google.com6898d522012-11-08 22:36:19 +000040 for (int i = 0; i < 10000; ++i) {
41 SkIRect r;
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +000042
reed@google.com6898d522012-11-08 22:36:19 +000043 r.set(rand.nextS() >> 2, rand.nextS() >> 2,
44 rand.nextS() >> 2, rand.nextS() >> 2);
45 int cx = r.centerX();
46 int cy = r.centerY();
robertphillips@google.comca47aae2012-12-12 15:58:25 +000047 REPORTER_ASSERT(reporter, ((r.left() + r.right()) >> 1) == cx);
48 REPORTER_ASSERT(reporter, ((r.top() + r.bottom()) >> 1) == cy);
reed@google.com6898d522012-11-08 22:36:19 +000049 }
50}
51
reed@android.comd4134452011-02-09 02:24:26 +000052static void check_invalid(skiatest::Reporter* reporter,
53 SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
54 SkRect rect;
55 rect.set(l, t, r, b);
reed@google.com16078632011-12-06 18:56:37 +000056 REPORTER_ASSERT(reporter, !rect.isFinite());
reed@android.comd4134452011-02-09 02:24:26 +000057}
reed@google.com534240f2011-02-07 19:08:59 +000058
reed@google.com16078632011-12-06 18:56:37 +000059// Tests that isFinite() will reject any rect with +/-inf values
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000060// as one of its coordinates.
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000061DEF_TEST(InfRect, reporter) {
reed@android.comd4134452011-02-09 02:24:26 +000062#ifdef SK_SCALAR_IS_FLOAT
reed@google.com7b463ac2012-05-16 13:35:36 +000063 float inf = 1 / make_zero(); // infinity
64 float nan = inf * 0;
65 SkASSERT(!(nan == nan));
reed@android.comd4134452011-02-09 02:24:26 +000066#else
reed@google.com7b463ac2012-05-16 13:35:36 +000067 SkFixed inf = SK_FixedNaN;
68 SkFixed nan = SK_FixedNaN;
reed@android.comd4134452011-02-09 02:24:26 +000069#endif
70 SkScalar small = SkIntToScalar(10);
71 SkScalar big = SkIntToScalar(100);
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000072
reed@google.com7b463ac2012-05-16 13:35:36 +000073 REPORTER_ASSERT(reporter, SkRect::MakeEmpty().isFinite());
74
reed@android.comd4134452011-02-09 02:24:26 +000075 SkRect rect = SkRect::MakeXYWH(small, small, big, big);
reed@google.com16078632011-12-06 18:56:37 +000076 REPORTER_ASSERT(reporter, rect.isFinite());
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000077
reed@google.com7b463ac2012-05-16 13:35:36 +000078 const SkScalar invalid[] = { nan, inf, -inf };
79 for (size_t i = 0; i < SK_ARRAY_COUNT(invalid); ++i) {
80 check_invalid(reporter, small, small, big, invalid[i]);
81 check_invalid(reporter, small, small, invalid[i], big);
82 check_invalid(reporter, small, invalid[i], big, big);
83 check_invalid(reporter, invalid[i], small, big, big);
84 }
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +000085
reed@google.com6898d522012-11-08 22:36:19 +000086 test_center(reporter);
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000087}
88
89// need tests for SkStrSearch