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 */ 7 8 #include "SkRandom.h" 9 #include "SkRect.h" 10 #include "Test.h" 11 12 static float make_zero() { 13 return sk_float_sin(0); 14 } 15 16 struct RectCenter { 17 SkIRect fRect; 18 SkIPoint fCenter; 19 }; 20 21 static void test_center(skiatest::Reporter* reporter) { 22 static const RectCenter gData[] = { 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 }; 29 for (size_t index = 0; index < SK_ARRAY_COUNT(gData); ++index) { 30 REPORTER_ASSERT(reporter, 31 gData[index].fRect.centerX() == gData[index].fCenter.x()); 32 REPORTER_ASSERT(reporter, 33 gData[index].fRect.centerY() == gData[index].fCenter.y()); 34 } 35 36 SkRandom rand; 37 for (int i = 0; i < 10000; ++i) { 38 SkIRect r; 39 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(); 44 REPORTER_ASSERT(reporter, ((r.left() + r.right()) >> 1) == cx); 45 REPORTER_ASSERT(reporter, ((r.top() + r.bottom()) >> 1) == cy); 46 } 47 } 48 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); 53 REPORTER_ASSERT(reporter, !rect.isFinite()); 54 } 55 56 // Tests that isFinite() will reject any rect with +/-inf values 57 // as one of its coordinates. 58 DEF_TEST(InfRect, reporter) { 59 float inf = 1 / make_zero(); // infinity 60 float nan = inf * 0; 61 SkASSERT(!(nan == nan)); 62 SkScalar small = SkIntToScalar(10); 63 SkScalar big = SkIntToScalar(100); 64 65 REPORTER_ASSERT(reporter, SkRect::MakeEmpty().isFinite()); 66 67 SkRect rect = SkRect::MakeXYWH(small, small, big, big); 68 REPORTER_ASSERT(reporter, rect.isFinite()); 69 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 } 77 78 test_center(reporter); 79 } 80 81 // need tests for SkStrSearch 82