1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2011 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci// Unit tests for src/core/SkPoint.cpp and its header 8cb93a386Sopenharmony_ci 9cb93a386Sopenharmony_ci#include "include/core/SkRect.h" 10cb93a386Sopenharmony_ci#include "src/core/SkPointPriv.h" 11cb93a386Sopenharmony_ci#include "tests/Test.h" 12cb93a386Sopenharmony_ci 13cb93a386Sopenharmony_cistatic void test_casts(skiatest::Reporter* reporter) { 14cb93a386Sopenharmony_ci SkPoint p = { 0, 0 }; 15cb93a386Sopenharmony_ci SkRect r = { 0, 0, 0, 0 }; 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_ci const SkScalar* pPtr = reinterpret_cast<const SkScalar*>(&p); 18cb93a386Sopenharmony_ci const SkScalar* rPtr = reinterpret_cast<const SkScalar*>(&r); 19cb93a386Sopenharmony_ci 20cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, SkPointPriv::AsScalars(p) == pPtr); 21cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, r.asScalars() == rPtr); 22cb93a386Sopenharmony_ci} 23cb93a386Sopenharmony_ci 24cb93a386Sopenharmony_ci// Tests SkPoint::Normalize() for this (x,y) 25cb93a386Sopenharmony_cistatic void test_Normalize(skiatest::Reporter* reporter, 26cb93a386Sopenharmony_ci SkScalar x, SkScalar y) { 27cb93a386Sopenharmony_ci SkPoint point; 28cb93a386Sopenharmony_ci point.set(x, y); 29cb93a386Sopenharmony_ci SkScalar oldLength = point.length(); 30cb93a386Sopenharmony_ci SkScalar returned = SkPoint::Normalize(&point); 31cb93a386Sopenharmony_ci SkScalar newLength = point.length(); 32cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength)); 33cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1)); 34cb93a386Sopenharmony_ci} 35cb93a386Sopenharmony_ci 36cb93a386Sopenharmony_cistatic void test_normalize_cannormalize_consistent(skiatest::Reporter* reporter) { 37cb93a386Sopenharmony_ci const SkScalar values[] = { 1, 1e18f, 1e20f, 1e38f, SK_ScalarInfinity, SK_ScalarNaN }; 38cb93a386Sopenharmony_ci 39cb93a386Sopenharmony_ci for (SkScalar val : values) { 40cb93a386Sopenharmony_ci const SkScalar variants[] = { val, -val, SkScalarInvert(val), -SkScalarInvert(val) }; 41cb93a386Sopenharmony_ci 42cb93a386Sopenharmony_ci for (SkScalar v : variants) { 43cb93a386Sopenharmony_ci const SkPoint pts[] = { { 0, v }, { v, 0 }, { 1, v }, { v, 1 }, { v, v } }; 44cb93a386Sopenharmony_ci 45cb93a386Sopenharmony_ci for (SkPoint p : pts) { 46cb93a386Sopenharmony_ci bool can = SkPointPriv::CanNormalize(p.fX, p.fY); 47cb93a386Sopenharmony_ci bool nor = p.normalize(); 48cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, can == nor); 49cb93a386Sopenharmony_ci } 50cb93a386Sopenharmony_ci } 51cb93a386Sopenharmony_ci } 52cb93a386Sopenharmony_ci} 53cb93a386Sopenharmony_ci 54cb93a386Sopenharmony_ci// Tests that SkPoint::length() and SkPoint::Length() both return 55cb93a386Sopenharmony_ci// approximately expectedLength for this (x,y). 56cb93a386Sopenharmony_cistatic void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y, 57cb93a386Sopenharmony_ci SkScalar expectedLength) { 58cb93a386Sopenharmony_ci SkPoint point; 59cb93a386Sopenharmony_ci point.set(x, y); 60cb93a386Sopenharmony_ci SkScalar s1 = point.length(); 61cb93a386Sopenharmony_ci SkScalar s2 = SkPoint::Length(x, y); 62cb93a386Sopenharmony_ci //The following should be exactly the same, but need not be. 63cb93a386Sopenharmony_ci //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323 64cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2)); 65cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength)); 66cb93a386Sopenharmony_ci 67cb93a386Sopenharmony_ci test_Normalize(reporter, x, y); 68cb93a386Sopenharmony_ci} 69cb93a386Sopenharmony_ci 70cb93a386Sopenharmony_ci// Ugh. Windows compiler can dive into other .cpp files, and sometimes 71cb93a386Sopenharmony_ci// notices that I will generate an overflow... which is exactly the point 72cb93a386Sopenharmony_ci// of this test! 73cb93a386Sopenharmony_ci// 74cb93a386Sopenharmony_ci// To avoid this warning, I need to convince the compiler that I might not 75cb93a386Sopenharmony_ci// use that big value, hence this hacky helper function: reporter is 76cb93a386Sopenharmony_ci// ALWAYS non-null. (shhhhhh, don't tell the compiler that). 77cb93a386Sopenharmony_citemplate <typename T> T get_value(skiatest::Reporter* reporter, T value) { 78cb93a386Sopenharmony_ci return reporter ? value : 0; 79cb93a386Sopenharmony_ci} 80cb93a386Sopenharmony_ci 81cb93a386Sopenharmony_ci// On linux gcc, 32bit, we are seeing the compiler propagate up the value 82cb93a386Sopenharmony_ci// of SkPoint::length() as a double (which we use sometimes to avoid overflow 83cb93a386Sopenharmony_ci// during the computation), even though the signature says float (SkScalar). 84cb93a386Sopenharmony_ci// 85cb93a386Sopenharmony_ci// force_as_float is meant to capture our latest technique (horrible as 86cb93a386Sopenharmony_ci// it is) to force the value to be a float, so we can test whether it was 87cb93a386Sopenharmony_ci// finite or not. 88cb93a386Sopenharmony_cistatic float force_as_float(skiatest::Reporter* reporter, float value) { 89cb93a386Sopenharmony_ci uint32_t storage; 90cb93a386Sopenharmony_ci memcpy(&storage, &value, 4); 91cb93a386Sopenharmony_ci // even the pair of memcpy calls are not sufficient, since those seem to 92cb93a386Sopenharmony_ci // be no-op'd, so we add a runtime tests (just like get_value) to force 93cb93a386Sopenharmony_ci // the compiler to give us an actual float. 94cb93a386Sopenharmony_ci if (nullptr == reporter) { 95cb93a386Sopenharmony_ci storage = ~storage; 96cb93a386Sopenharmony_ci } 97cb93a386Sopenharmony_ci memcpy(&value, &storage, 4); 98cb93a386Sopenharmony_ci return value; 99cb93a386Sopenharmony_ci} 100cb93a386Sopenharmony_ci 101cb93a386Sopenharmony_ci// test that we handle very large values correctly. i.e. that we can 102cb93a386Sopenharmony_ci// successfully normalize something whose mag overflows a float. 103cb93a386Sopenharmony_cistatic void test_overflow(skiatest::Reporter* reporter) { 104cb93a386Sopenharmony_ci SkScalar bigFloat = get_value(reporter, 3.4e38f); 105cb93a386Sopenharmony_ci SkPoint pt = { bigFloat, bigFloat }; 106cb93a386Sopenharmony_ci 107cb93a386Sopenharmony_ci SkScalar length = pt.length(); 108cb93a386Sopenharmony_ci length = force_as_float(reporter, length); 109cb93a386Sopenharmony_ci 110cb93a386Sopenharmony_ci // expect this to be non-finite, but dump the results if not. 111cb93a386Sopenharmony_ci if (SkScalarIsFinite(length)) { 112cb93a386Sopenharmony_ci SkDebugf("length(%g, %g) == %g\n", pt.fX, pt.fY, length); 113cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, !SkScalarIsFinite(length)); 114cb93a386Sopenharmony_ci } 115cb93a386Sopenharmony_ci 116cb93a386Sopenharmony_ci // this should succeed, even though we can't represent length 117cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, pt.setLength(SK_Scalar1)); 118cb93a386Sopenharmony_ci 119cb93a386Sopenharmony_ci // now that pt is normalized, we check its length 120cb93a386Sopenharmony_ci length = pt.length(); 121cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, SkScalarNearlyEqual(length, SK_Scalar1)); 122cb93a386Sopenharmony_ci} 123cb93a386Sopenharmony_ci 124cb93a386Sopenharmony_ciDEF_TEST(Point, reporter) { 125cb93a386Sopenharmony_ci test_casts(reporter); 126cb93a386Sopenharmony_ci 127cb93a386Sopenharmony_ci static const struct { 128cb93a386Sopenharmony_ci SkScalar fX; 129cb93a386Sopenharmony_ci SkScalar fY; 130cb93a386Sopenharmony_ci SkScalar fLength; 131cb93a386Sopenharmony_ci } gRec[] = { 132cb93a386Sopenharmony_ci { SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5) }, 133cb93a386Sopenharmony_ci { 0.6f, 0.8f, SK_Scalar1 }, 134cb93a386Sopenharmony_ci }; 135cb93a386Sopenharmony_ci 136cb93a386Sopenharmony_ci for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { 137cb93a386Sopenharmony_ci test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fLength); 138cb93a386Sopenharmony_ci } 139cb93a386Sopenharmony_ci 140cb93a386Sopenharmony_ci test_overflow(reporter); 141cb93a386Sopenharmony_ci test_normalize_cannormalize_consistent(reporter); 142cb93a386Sopenharmony_ci} 143cb93a386Sopenharmony_ci 144cb93a386Sopenharmony_ciDEF_TEST(Point_setLengthFast, reporter) { 145cb93a386Sopenharmony_ci // Scale a (1,1) point to a bunch of different lengths, 146cb93a386Sopenharmony_ci // making sure the slow and fast paths are within 0.1%. 147cb93a386Sopenharmony_ci const float tests[] = { 1.0f, 0.0f, 1.0e-37f, 3.4e38f, 42.0f, 0.00012f }; 148cb93a386Sopenharmony_ci 149cb93a386Sopenharmony_ci const SkPoint kOne = {1.0f, 1.0f}; 150cb93a386Sopenharmony_ci for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); i++) { 151cb93a386Sopenharmony_ci SkPoint slow = kOne, fast = kOne; 152cb93a386Sopenharmony_ci 153cb93a386Sopenharmony_ci slow.setLength(tests[i]); 154cb93a386Sopenharmony_ci SkPointPriv::SetLengthFast(&fast, tests[i]); 155cb93a386Sopenharmony_ci 156cb93a386Sopenharmony_ci if (slow.length() < FLT_MIN && fast.length() < FLT_MIN) continue; 157cb93a386Sopenharmony_ci 158cb93a386Sopenharmony_ci SkScalar ratio = slow.length() / fast.length(); 159cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, ratio > 0.999f); 160cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, ratio < 1.001f); 161cb93a386Sopenharmony_ci } 162cb93a386Sopenharmony_ci} 163