blob: 95ef4be3efac1c8dc850efc7df428f16b8e47208 [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.com2b57dc62013-01-08 13:23:32 +000018struct RectCenter {
19 SkIRect fRect;
20 SkIPoint fCenter;
21};
22
reed@google.com6898d522012-11-08 22:36:19 +000023static void test_center(skiatest::Reporter* reporter) {
reed@google.com2b57dc62013-01-08 13:23:32 +000024 static const RectCenter gData[] = {
reed@google.com6898d522012-11-08 22:36:19 +000025 { { 0, 0, 0, 0 }, { 0, 0 } },
26 { { 0, 0, 1, 1 }, { 0, 0 } },
27 { { -1, -1, 0, 0 }, { -1, -1 } },
28 { { 0, 0, 10, 7 }, { 5, 3 } },
29 { { 0, 0, 11, 6 }, { 5, 3 } },
30 };
reed@google.com2b57dc62013-01-08 13:23:32 +000031 for (size_t index = 0; index < SK_ARRAY_COUNT(gData); ++index) {
reed@google.com6898d522012-11-08 22:36:19 +000032 REPORTER_ASSERT(reporter,
reed@google.com2b57dc62013-01-08 13:23:32 +000033 gData[index].fRect.centerX() == gData[index].fCenter.x());
reed@google.com6898d522012-11-08 22:36:19 +000034 REPORTER_ASSERT(reporter,
reed@google.com2b57dc62013-01-08 13:23:32 +000035 gData[index].fRect.centerY() == gData[index].fCenter.y());
reed@google.com6898d522012-11-08 22:36:19 +000036 }
37
jvanverth@google.comc490f802013-03-04 13:56:38 +000038 SkMWCRandom rand;
reed@google.com6898d522012-11-08 22:36:19 +000039 for (int i = 0; i < 10000; ++i) {
40 SkIRect r;
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +000041
reed@google.com6898d522012-11-08 22:36:19 +000042 r.set(rand.nextS() >> 2, rand.nextS() >> 2,
43 rand.nextS() >> 2, rand.nextS() >> 2);
44 int cx = r.centerX();
45 int cy = r.centerY();
robertphillips@google.comca47aae2012-12-12 15:58:25 +000046 REPORTER_ASSERT(reporter, ((r.left() + r.right()) >> 1) == cx);
47 REPORTER_ASSERT(reporter, ((r.top() + r.bottom()) >> 1) == cy);
reed@google.com6898d522012-11-08 22:36:19 +000048 }
49}
50
reed@android.comd4134452011-02-09 02:24:26 +000051static void check_invalid(skiatest::Reporter* reporter,
52 SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
53 SkRect rect;
54 rect.set(l, t, r, b);
reed@google.com16078632011-12-06 18:56:37 +000055 REPORTER_ASSERT(reporter, !rect.isFinite());
reed@android.comd4134452011-02-09 02:24:26 +000056}
reed@google.com534240f2011-02-07 19:08:59 +000057
reed@google.com16078632011-12-06 18:56:37 +000058// Tests that isFinite() will reject any rect with +/-inf values
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000059// as one of its coordinates.
60static void TestInfRect(skiatest::Reporter* reporter) {
reed@android.comd4134452011-02-09 02:24:26 +000061#ifdef SK_SCALAR_IS_FLOAT
reed@google.com7b463ac2012-05-16 13:35:36 +000062 float inf = 1 / make_zero(); // infinity
63 float nan = inf * 0;
64 SkASSERT(!(nan == nan));
reed@android.comd4134452011-02-09 02:24:26 +000065#else
reed@google.com7b463ac2012-05-16 13:35:36 +000066 SkFixed inf = SK_FixedNaN;
67 SkFixed nan = SK_FixedNaN;
reed@android.comd4134452011-02-09 02:24:26 +000068#endif
69 SkScalar small = SkIntToScalar(10);
70 SkScalar big = SkIntToScalar(100);
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000071
reed@google.com7b463ac2012-05-16 13:35:36 +000072 REPORTER_ASSERT(reporter, SkRect::MakeEmpty().isFinite());
73
reed@android.comd4134452011-02-09 02:24:26 +000074 SkRect rect = SkRect::MakeXYWH(small, small, big, big);
reed@google.com16078632011-12-06 18:56:37 +000075 REPORTER_ASSERT(reporter, rect.isFinite());
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000076
reed@google.com7b463ac2012-05-16 13:35:36 +000077 const SkScalar invalid[] = { nan, inf, -inf };
78 for (size_t i = 0; i < SK_ARRAY_COUNT(invalid); ++i) {
79 check_invalid(reporter, small, small, big, invalid[i]);
80 check_invalid(reporter, small, small, invalid[i], big);
81 check_invalid(reporter, small, invalid[i], big, big);
82 check_invalid(reporter, invalid[i], small, big, big);
83 }
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +000084
reed@google.com6898d522012-11-08 22:36:19 +000085 test_center(reporter);
wjmaclean@chromium.orgff1ec2f2011-02-07 17:48:40 +000086}
87
88// need tests for SkStrSearch
89
90#include "TestClassDef.h"
91DEFINE_TESTCLASS("InfRect", InfRectTestClass, TestInfRect)