blob: b909762f07b6774824afb72ab7a170f9caa9f5dd [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
epoger@google.com1fd56dc2011-06-15 18:04:58 +00008// Unit tests for src/core/SkPoint.cpp and its header
9
10#include "SkPoint.h"
reed@google.comc7d0ea32013-03-08 16:07:54 +000011#include "SkRect.h"
epoger@google.com1fd56dc2011-06-15 18:04:58 +000012#include "Test.h"
13
reed@google.comc7d0ea32013-03-08 16:07:54 +000014static void test_casts(skiatest::Reporter* reporter) {
15 SkPoint p = { 0, 0 };
16 SkRect r = { 0, 0, 0, 0 };
17
18 const SkScalar* pPtr = SkTCast<const SkScalar*>(&p);
19 const SkScalar* rPtr = SkTCast<const SkScalar*>(&r);
20
21 REPORTER_ASSERT(reporter, p.asScalars() == pPtr);
22 REPORTER_ASSERT(reporter, r.asScalars() == rPtr);
23}
24
epoger@google.com1fd56dc2011-06-15 18:04:58 +000025// Tests SkPoint::Normalize() for this (x,y)
26static void test_Normalize(skiatest::Reporter* reporter,
27 SkScalar x, SkScalar y) {
28 SkPoint point;
29 point.set(x, y);
30 SkScalar oldLength = point.length();
31 SkScalar returned = SkPoint::Normalize(&point);
32 SkScalar newLength = point.length();
bungeman@google.comb7b5d932012-08-24 19:53:58 +000033 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength));
epoger@google.com1fd56dc2011-06-15 18:04:58 +000034 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1));
35}
36
reed@google.com5a5fe582013-05-03 15:59:39 +000037// Tests that SkPoint::length() and SkPoint::Length() both return
38// approximately expectedLength for this (x,y).
39static void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y,
40 SkScalar expectedLength) {
41 SkPoint point;
42 point.set(x, y);
43 SkScalar s1 = point.length();
44 SkScalar s2 = SkPoint::Length(x, y);
45 //The following should be exactly the same, but need not be.
46 //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
47 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2));
48 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength));
49
50 test_Normalize(reporter, x, y);
51}
52
reed@google.com25720b42013-05-03 16:30:44 +000053// Ugh. Windows compiler can dive into other .cpp files, and sometimes
54// notices that I will generate an overflow... which is exactly the point
55// of this test!
56//
57// To avoid this warning, I need to convince the compiler that I might not
58// use that big value, hence this hacky helper function: reporter is
59// ALWAYS non-null. (shhhhhh, don't tell the compiler that).
60template <typename T> T get_value(skiatest::Reporter* reporter, T value) {
61 return reporter ? value : 0;
62}
63
reed@google.com5a5fe582013-05-03 15:59:39 +000064// test that we handle very large values correctly. i.e. that we can
65// successfully normalize something whose mag overflows a float.
66static void test_overflow(skiatest::Reporter* reporter) {
reed@google.com25720b42013-05-03 16:30:44 +000067 SkScalar bigFloat = get_value(reporter, SkFloatToScalar(3.4e38f));
68 SkPoint pt = { bigFloat, bigFloat };
reed@google.com5a5fe582013-05-03 15:59:39 +000069
70 SkScalar length = pt.length();
71 REPORTER_ASSERT(reporter, !SkScalarIsFinite(length));
72
73 // this should succeed, even though we can't represent length
74 REPORTER_ASSERT(reporter, pt.setLength(SK_Scalar1));
75
76 // now that pt is normalized, we check its length
77 length = pt.length();
78 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(length, SK_Scalar1));
79}
80
81// test that we handle very small values correctly. i.e. that we can
82// report failure if we try to normalize them.
83static void test_underflow(skiatest::Reporter* reporter) {
84 SkPoint pt = { SkFloatToScalar(1.0e-37f), SkFloatToScalar(1.0e-37f) };
85 SkPoint copy = pt;
86
87 REPORTER_ASSERT(reporter, 0 == SkPoint::Normalize(&pt));
88 REPORTER_ASSERT(reporter, pt == copy); // pt is unchanged
89
90 REPORTER_ASSERT(reporter, !pt.setLength(SK_Scalar1));
91 REPORTER_ASSERT(reporter, pt == copy); // pt is unchanged
92}
93
caryclark@google.com42639cd2012-06-06 12:03:39 +000094static void PointTest(skiatest::Reporter* reporter) {
reed@google.comc7d0ea32013-03-08 16:07:54 +000095 test_casts(reporter);
96
reed@google.com5a5fe582013-05-03 15:59:39 +000097 static const struct {
98 SkScalar fX;
99 SkScalar fY;
100 SkScalar fLength;
101 } gRec[] = {
102 { SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5) },
103 { SkFloatToScalar(0.6f), SkFloatToScalar(0.8f), SK_Scalar1 },
104 };
105
106 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
107 test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fLength);
108 }
109
110 test_underflow(reporter);
111 test_overflow(reporter);
epoger@google.com1fd56dc2011-06-15 18:04:58 +0000112}
113
114#include "TestClassDef.h"
115DEFINE_TESTCLASS("Point", PointTestClass, PointTest)