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 "Test.h" 9 #include "TestClassDef.h" 10 #include "SkRandom.h" 11 #include "SkRect.h" 12 13 #ifdef SK_SCALAR_IS_FLOAT 14 static float make_zero() { 15 return sk_float_sin(0); 16 } 17 #endif 18 19 struct RectCenter { 20 SkIRect fRect; 21 SkIPoint fCenter; 22 }; 23 24 static void test_center(skiatest::Reporter* reporter) { 25 static const RectCenter gData[] = { 26 { { 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 }; 32 for (size_t index = 0; index < SK_ARRAY_COUNT(gData); ++index) { 33 REPORTER_ASSERT(reporter, 34 gData[index].fRect.centerX() == gData[index].fCenter.x()); 35 REPORTER_ASSERT(reporter, 36 gData[index].fRect.centerY() == gData[index].fCenter.y()); 37 } 38 39 SkRandom rand; 40 for (int i = 0; i < 10000; ++i) { 41 SkIRect r; 42 43 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(); 47 REPORTER_ASSERT(reporter, ((r.left() + r.right()) >> 1) == cx); 48 REPORTER_ASSERT(reporter, ((r.top() + r.bottom()) >> 1) == cy); 49 } 50 } 51 52 static 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); 56 REPORTER_ASSERT(reporter, !rect.isFinite()); 57 } 58 59 // Tests that isFinite() will reject any rect with +/-inf values 60 // as one of its coordinates. 61 DEF_TEST(InfRect, reporter) { 62 #ifdef SK_SCALAR_IS_FLOAT 63 float inf = 1 / make_zero(); // infinity 64 float nan = inf * 0; 65 SkASSERT(!(nan == nan)); 66 #else 67 SkFixed inf = SK_FixedNaN; 68 SkFixed nan = SK_FixedNaN; 69 #endif 70 SkScalar small = SkIntToScalar(10); 71 SkScalar big = SkIntToScalar(100); 72 73 REPORTER_ASSERT(reporter, SkRect::MakeEmpty().isFinite()); 74 75 SkRect rect = SkRect::MakeXYWH(small, small, big, big); 76 REPORTER_ASSERT(reporter, rect.isFinite()); 77 78 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 } 85 86 test_center(reporter); 87 } 88 89 // need tests for SkStrSearch 90