2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
12 static float make_zero() {
13 return sk_float_sin(0);
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 } },
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());
37 for (int i = 0; i < 10000; ++i) {
40 r.set(rand.nextS() >> 2, rand.nextS() >> 2,
41 rand.nextS() >> 2, rand.nextS() >> 2);
44 REPORTER_ASSERT(reporter, ((r.left() + r.right()) >> 1) == cx);
45 REPORTER_ASSERT(reporter, ((r.top() + r.bottom()) >> 1) == cy);
49 static void check_invalid(skiatest::Reporter* reporter,
50 SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
53 REPORTER_ASSERT(reporter, !rect.isFinite());
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
61 SkASSERT(!(nan == nan));
62 SkScalar small = SkIntToScalar(10);
63 SkScalar big = SkIntToScalar(100);
65 REPORTER_ASSERT(reporter, SkRect::MakeEmpty().isFinite());
67 SkRect rect = SkRect::MakeXYWH(small, small, big, big);
68 REPORTER_ASSERT(reporter, rect.isFinite());
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);
78 test_center(reporter);
81 // need tests for SkStrSearch