epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 8 | #include "Test.h" |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 9 | #include "SkRandom.h" |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 10 | #include "SkRect.h" |
| 11 | |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 12 | #ifdef SK_SCALAR_IS_FLOAT |
reed@google.com | 534240f | 2011-02-07 19:08:59 +0000 | [diff] [blame] | 13 | static float make_zero() { |
| 14 | return sk_float_sin(0); |
| 15 | } |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 16 | #endif |
| 17 | |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 18 | struct RectCenter { |
| 19 | SkIRect fRect; |
| 20 | SkIPoint fCenter; |
| 21 | }; |
| 22 | |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 23 | static void test_center(skiatest::Reporter* reporter) { |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 24 | static const RectCenter gData[] = { |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 25 | { { 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.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 31 | for (size_t index = 0; index < SK_ARRAY_COUNT(gData); ++index) { |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 32 | REPORTER_ASSERT(reporter, |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 33 | gData[index].fRect.centerX() == gData[index].fCenter.x()); |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 34 | REPORTER_ASSERT(reporter, |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 35 | gData[index].fRect.centerY() == gData[index].fCenter.y()); |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 36 | } |
| 37 | |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 38 | SkMWCRandom rand; |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 39 | for (int i = 0; i < 10000; ++i) { |
| 40 | SkIRect r; |
skia.committer@gmail.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 41 | |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 42 | 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.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 46 | REPORTER_ASSERT(reporter, ((r.left() + r.right()) >> 1) == cx); |
| 47 | REPORTER_ASSERT(reporter, ((r.top() + r.bottom()) >> 1) == cy); |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 51 | static 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.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, !rect.isFinite()); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 56 | } |
reed@google.com | 534240f | 2011-02-07 19:08:59 +0000 | [diff] [blame] | 57 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 58 | // Tests that isFinite() will reject any rect with +/-inf values |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 59 | // as one of its coordinates. |
| 60 | static void TestInfRect(skiatest::Reporter* reporter) { |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 61 | #ifdef SK_SCALAR_IS_FLOAT |
reed@google.com | 7b463ac | 2012-05-16 13:35:36 +0000 | [diff] [blame] | 62 | float inf = 1 / make_zero(); // infinity |
| 63 | float nan = inf * 0; |
| 64 | SkASSERT(!(nan == nan)); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 65 | #else |
reed@google.com | 7b463ac | 2012-05-16 13:35:36 +0000 | [diff] [blame] | 66 | SkFixed inf = SK_FixedNaN; |
| 67 | SkFixed nan = SK_FixedNaN; |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 68 | #endif |
| 69 | SkScalar small = SkIntToScalar(10); |
| 70 | SkScalar big = SkIntToScalar(100); |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 71 | |
reed@google.com | 7b463ac | 2012-05-16 13:35:36 +0000 | [diff] [blame] | 72 | REPORTER_ASSERT(reporter, SkRect::MakeEmpty().isFinite()); |
| 73 | |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 74 | SkRect rect = SkRect::MakeXYWH(small, small, big, big); |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, rect.isFinite()); |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 76 | |
reed@google.com | 7b463ac | 2012-05-16 13:35:36 +0000 | [diff] [blame] | 77 | 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.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 84 | |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 85 | test_center(reporter); |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // need tests for SkStrSearch |
| 89 | |
| 90 | #include "TestClassDef.h" |
| 91 | DEFINE_TESTCLASS("InfRect", InfRectTestClass, TestInfRect) |