Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
PointTest.cpp File Reference
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkScalar.h"
#include "include/core/SkTypes.h"
#include "include/private/base/SkDebug.h"
#include "src/core/SkPointPriv.h"
#include "tests/Test.h"
#include <cfloat>
#include <cstdint>
#include <cstring>
#include <string>

Go to the source code of this file.

Functions

static void test_casts (skiatest::Reporter *reporter)
 
static void test_Normalize (skiatest::Reporter *reporter, SkScalar x, SkScalar y)
 
static void test_normalize_cannormalize_consistent (skiatest::Reporter *reporter)
 
static void test_length (skiatest::Reporter *reporter, SkScalar x, SkScalar y, SkScalar expectedLength)
 
template<typename T >
T get_value (skiatest::Reporter *reporter, T value)
 
static float force_as_float (skiatest::Reporter *reporter, float value)
 
static void test_overflow (skiatest::Reporter *reporter)
 
 DEF_TEST (Point, reporter)
 
 DEF_TEST (Point_setLengthFast, reporter)
 

Function Documentation

◆ DEF_TEST() [1/2]

DEF_TEST ( Point  ,
reporter   
)

Definition at line 132 of file PointTest.cpp.

132 {
134
135 static const struct {
136 SkScalar fX;
137 SkScalar fY;
138 SkScalar fLength;
139 } gRec[] = {
141 { 0.6f, 0.8f, SK_Scalar1 },
142 };
143
144 for (size_t i = 0; i < std::size(gRec); ++i) {
145 test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fLength);
146 }
147
150}
static const struct @223 gRec[]
reporter
#define SK_Scalar1
Definition SkScalar.h:18
#define SkIntToScalar(x)
Definition SkScalar.h:57
float SkScalar
Definition extension.cpp:12
static void test_overflow(skiatest::Reporter *reporter)
static void test_normalize_cannormalize_consistent(skiatest::Reporter *reporter)
Definition PointTest.cpp:44
static void test_length(skiatest::Reporter *reporter, SkScalar x, SkScalar y, SkScalar expectedLength)
Definition PointTest.cpp:64
static void test_casts(skiatest::Reporter *reporter)
Definition PointTest.cpp:21

◆ DEF_TEST() [2/2]

DEF_TEST ( Point_setLengthFast  ,
reporter   
)

Definition at line 152 of file PointTest.cpp.

152 {
153 // Scale a (1,1) point to a bunch of different lengths,
154 // making sure the slow and fast paths are within 0.1%.
155 const float tests[] = { 1.0f, 0.0f, 1.0e-37f, 3.4e38f, 42.0f, 0.00012f };
156
157 const SkPoint kOne = {1.0f, 1.0f};
158 for (unsigned i = 0; i < std::size(tests); i++) {
159 SkPoint slow = kOne, fast = kOne;
160
161 slow.setLength(tests[i]);
163
164 if (slow.length() < FLT_MIN && fast.length() < FLT_MIN) continue;
165
166 SkScalar ratio = slow.length() / fast.length();
167 REPORTER_ASSERT(reporter, ratio > 0.999f);
168 REPORTER_ASSERT(reporter, ratio < 1.001f);
169 }
170}
static BlurTest tests[]
Definition BlurTest.cpp:84
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
static bool SetLengthFast(SkPoint *pt, float length)
Definition SkPoint.cpp:94
bool setLength(float length)
Definition SkPoint.cpp:30
float length() const

◆ force_as_float()

static float force_as_float ( skiatest::Reporter reporter,
float  value 
)
static

Definition at line 96 of file PointTest.cpp.

96 {
97 uint32_t storage;
98 memcpy(&storage, &value, 4);
99 // even the pair of memcpy calls are not sufficient, since those seem to
100 // be no-op'd, so we add a runtime tests (just like get_value) to force
101 // the compiler to give us an actual float.
102 if (nullptr == reporter) {
103 storage = ~storage;
104 }
105 memcpy(&value, &storage, 4);
106 return value;
107}
uint8_t value

◆ get_value()

template<typename T >
T get_value ( skiatest::Reporter reporter,
T  value 
)

Definition at line 85 of file PointTest.cpp.

85 {
86 return reporter ? value : 0;
87}

◆ test_casts()

static void test_casts ( skiatest::Reporter reporter)
static

Definition at line 21 of file PointTest.cpp.

21 {
22 SkPoint p = { 0, 0 };
23 SkRect r = { 0, 0, 0, 0 };
24
25 const SkScalar* pPtr = reinterpret_cast<const SkScalar*>(&p);
26 const SkScalar* rPtr = reinterpret_cast<const SkScalar*>(&r);
27
30}
static const SkScalar * AsScalars(const SkPoint &pt)
Definition SkPointPriv.h:26
const float * asScalars() const
Definition SkRect.h:1340

◆ test_length()

static void test_length ( skiatest::Reporter reporter,
SkScalar  x,
SkScalar  y,
SkScalar  expectedLength 
)
static

Definition at line 64 of file PointTest.cpp.

65 {
66 SkPoint point;
67 point.set(x, y);
68 SkScalar s1 = point.length();
70 //The following should be exactly the same, but need not be.
71 //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
73 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength));
74
76}
static bool SkScalarNearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
Definition SkScalar.h:107
double y
double x
void set(float x, float y)
static float Length(float x, float y)
Definition SkPoint.cpp:79
static void test_Normalize(skiatest::Reporter *reporter, SkScalar x, SkScalar y)
Definition PointTest.cpp:33

◆ test_Normalize()

static void test_Normalize ( skiatest::Reporter reporter,
SkScalar  x,
SkScalar  y 
)
static

Definition at line 33 of file PointTest.cpp.

34 {
35 SkPoint point;
36 point.set(x, y);
37 SkScalar oldLength = point.length();
38 SkScalar returned = SkPoint::Normalize(&point);
39 SkScalar newLength = point.length();
40 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength));
42}
static float Normalize(SkVector *vec)
Definition SkPoint.cpp:71

◆ test_normalize_cannormalize_consistent()

static void test_normalize_cannormalize_consistent ( skiatest::Reporter reporter)
static

Definition at line 44 of file PointTest.cpp.

44 {
45 const SkScalar values[] = { 1, 1e18f, 1e20f, 1e38f, SK_ScalarInfinity, SK_ScalarNaN };
46
47 for (SkScalar val : values) {
48 const SkScalar variants[] = { val, -val, SkScalarInvert(val), -SkScalarInvert(val) };
49
50 for (SkScalar v : variants) {
51 const SkPoint pts[] = { { 0, v }, { v, 0 }, { 1, v }, { v, 1 }, { v, v } };
52
53 for (SkPoint p : pts) {
54 bool can = SkPointPriv::CanNormalize(p.fX, p.fY);
55 bool nor = p.normalize();
56 REPORTER_ASSERT(reporter, can == nor);
57 }
58 }
59 }
60}
#define SkScalarInvert(x)
Definition SkScalar.h:73
#define SK_ScalarNaN
Definition SkScalar.h:28
#define SK_ScalarInfinity
Definition SkScalar.h:26
static bool CanNormalize(SkScalar dx, SkScalar dy)
Definition SkPointPriv.h:28

◆ test_overflow()

static void test_overflow ( skiatest::Reporter reporter)
static

Definition at line 111 of file PointTest.cpp.

111 {
112 SkScalar bigFloat = get_value(reporter, 3.4e38f);
113 SkPoint pt = { bigFloat, bigFloat };
114
115 SkScalar length = pt.length();
117
118 // expect this to be non-finite, but dump the results if not.
119 if (SkIsFinite(length)) {
120 SkDebugf("length(%g, %g) == %g\n", pt.fX, pt.fY, length);
122 }
123
124 // this should succeed, even though we can't represent length
126
127 // now that pt is normalized, we check its length
128 length = pt.length();
130}
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static bool SkIsFinite(T x, Pack... values)
size_t length
float fX
x-axis value
float fY
y-axis value
T get_value(skiatest::Reporter *reporter, T value)
Definition PointTest.cpp:85
static float force_as_float(skiatest::Reporter *reporter, float value)
Definition PointTest.cpp:96