epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 8 | #include "SkRandom.h" |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 9 | #include "SkRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 10 | #include "Test.h" |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 11 | |
reed@google.com | 534240f | 2011-02-07 19:08:59 +0000 | [diff] [blame] | 12 | static float make_zero() { |
| 13 | return sk_float_sin(0); |
| 14 | } |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 15 | |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 16 | struct RectCenter { |
| 17 | SkIRect fRect; |
| 18 | SkIPoint fCenter; |
| 19 | }; |
| 20 | |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 21 | static void test_center(skiatest::Reporter* reporter) { |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 22 | static const RectCenter gData[] = { |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 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 | }; |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 29 | for (size_t index = 0; index < SK_ARRAY_COUNT(gData); ++index) { |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 30 | REPORTER_ASSERT(reporter, |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 31 | gData[index].fRect.centerX() == gData[index].fCenter.x()); |
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.centerY() == gData[index].fCenter.y()); |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 34 | } |
| 35 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 36 | SkRandom rand; |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 37 | for (int i = 0; i < 10000; ++i) { |
| 38 | SkIRect r; |
skia.committer@gmail.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 39 | |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 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(); |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, ((r.left() + r.right()) >> 1) == cx); |
| 45 | REPORTER_ASSERT(reporter, ((r.top() + r.bottom()) >> 1) == cy); |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 49 | static 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.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 53 | REPORTER_ASSERT(reporter, !rect.isFinite()); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 54 | } |
reed@google.com | 534240f | 2011-02-07 19:08:59 +0000 | [diff] [blame] | 55 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 56 | // Tests that isFinite() will reject any rect with +/-inf values |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 57 | // as one of its coordinates. |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 58 | DEF_TEST(InfRect, reporter) { |
reed@google.com | 7b463ac | 2012-05-16 13:35:36 +0000 | [diff] [blame] | 59 | float inf = 1 / make_zero(); // infinity |
| 60 | float nan = inf * 0; |
| 61 | SkASSERT(!(nan == nan)); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 62 | SkScalar small = SkIntToScalar(10); |
| 63 | SkScalar big = SkIntToScalar(100); |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 64 | |
reed@google.com | 7b463ac | 2012-05-16 13:35:36 +0000 | [diff] [blame] | 65 | REPORTER_ASSERT(reporter, SkRect::MakeEmpty().isFinite()); |
| 66 | |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 67 | SkRect rect = SkRect::MakeXYWH(small, small, big, big); |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 68 | REPORTER_ASSERT(reporter, rect.isFinite()); |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 69 | |
reed@google.com | 7b463ac | 2012-05-16 13:35:36 +0000 | [diff] [blame] | 70 | 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.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 77 | |
reed@google.com | 6898d52 | 2012-11-08 22:36:19 +0000 | [diff] [blame] | 78 | test_center(reporter); |
wjmaclean@chromium.org | ff1ec2f | 2011-02-07 17:48:40 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // need tests for SkStrSearch |