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