InfRectTest.cpp revision ca47aae7ecfdafb5e88baee13737908b79a4c716
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
18static void test_center(skiatest::Reporter* reporter) {
19    static const struct {
20        SkIRect  fRect;
21        SkIPoint fCenter;
22    } data[] = {
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(data); ++index) {
30        REPORTER_ASSERT(reporter,
31                        data[index].fRect.centerX() == data[index].fCenter.x());
32        REPORTER_ASSERT(reporter,
33                        data[index].fRect.centerY() == data[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
49static 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.
58static void TestInfRect(skiatest::Reporter* reporter) {
59#ifdef SK_SCALAR_IS_FLOAT
60    float inf = 1 / make_zero();    // infinity
61    float nan = inf * 0;
62    SkASSERT(!(nan == nan));
63#else
64    SkFixed inf = SK_FixedNaN;
65    SkFixed nan = SK_FixedNaN;
66#endif
67    SkScalar small = SkIntToScalar(10);
68    SkScalar big = SkIntToScalar(100);
69
70    REPORTER_ASSERT(reporter, SkRect::MakeEmpty().isFinite());
71
72    SkRect rect = SkRect::MakeXYWH(small, small, big, big);
73    REPORTER_ASSERT(reporter, rect.isFinite());
74
75    const SkScalar invalid[] = { nan, inf, -inf };
76    for (size_t i = 0; i < SK_ARRAY_COUNT(invalid); ++i) {
77        check_invalid(reporter, small, small, big, invalid[i]);
78        check_invalid(reporter, small, small, invalid[i], big);
79        check_invalid(reporter, small, invalid[i], big, big);
80        check_invalid(reporter, invalid[i], small, big, big);
81    }
82
83    test_center(reporter);
84}
85
86// need tests for SkStrSearch
87
88#include "TestClassDef.h"
89DEFINE_TESTCLASS("InfRect", InfRectTestClass, TestInfRect)
90