Flutter Impeller
geometry_asserts.h File Reference
#include <array>
#include <iostream>
#include "gtest/gtest.h"
#include "impeller/geometry/matrix.h"
#include "impeller/geometry/point.h"
#include "impeller/geometry/rect.h"
#include "impeller/geometry/size.h"
#include "impeller/geometry/vector.h"

Go to the source code of this file.

Macros

#define ASSERT_MATRIX_NEAR(a, b)   ASSERT_PRED2(&::MatrixNear, a, b)
 
#define ASSERT_QUATERNION_NEAR(a, b)   ASSERT_PRED2(&::QuaternionNear, a, b)
 
#define ASSERT_RECT_NEAR(a, b)   ASSERT_PRED2(&::RectNear, a, b)
 
#define ASSERT_COLOR_NEAR(a, b)   ASSERT_PRED2(&::ColorNear, a, b)
 
#define ASSERT_POINT_NEAR(a, b)   ASSERT_PRED2(&::PointNear, a, b)
 
#define ASSERT_VECTOR3_NEAR(a, b)   ASSERT_PRED2(&::Vector3Near, a, b)
 
#define ASSERT_VECTOR4_NEAR(a, b)   ASSERT_PRED2(&::Vector4Near, a, b)
 
#define ASSERT_SIZE_NEAR(a, b)   ASSERT_PRED2(&::SizeNear, a, b)
 
#define ASSERT_ARRAY_4_NEAR(a, b)   ASSERT_PRED2(&::Array4Near, a, b)
 
#define ASSERT_COLOR_BUFFER_NEAR(a, b)   ASSERT_PRED2(&::ColorBufferNear, a, b)
 
#define ASSERT_COLORS_NEAR(a, b)   ASSERT_PRED2(&::ColorsNear, a, b)
 
#define EXPECT_MATRIX_NEAR(a, b)   EXPECT_PRED2(&::MatrixNear, a, b)
 
#define EXPECT_QUATERNION_NEAR(a, b)   EXPECT_PRED2(&::QuaternionNear, a, b)
 
#define EXPECT_RECT_NEAR(a, b)   EXPECT_PRED2(&::RectNear, a, b)
 
#define EXPECT_COLOR_NEAR(a, b)   EXPECT_PRED2(&::ColorNear, a, b)
 
#define EXPECT_POINT_NEAR(a, b)   EXPECT_PRED2(&::PointNear, a, b)
 
#define EXPECT_VECTOR3_NEAR(a, b)   EXPECT_PRED2(&::Vector3Near, a, b)
 
#define EXPECT_VECTOR4_NEAR(a, b)   EXPECT_PRED2(&::Vector4Near, a, b)
 
#define EXPECT_SIZE_NEAR(a, b)   EXPECT_PRED2(&::SizeNear, a, b)
 
#define EXPECT_ARRAY_4_NEAR(a, b)   EXPECT_PRED2(&::Array4Near, a, b)
 
#define EXPECT_COLOR_BUFFER_NEAR(a, b)   EXPECT_PRED2(&::ColorBufferNear, a, b)
 
#define EXPECT_COLORS_NEAR(a, b)   EXPECT_PRED2(&::ColorsNear, a, b)
 

Functions

bool NumberNear (double a, double b)
 
inline ::testing::AssertionResult MatrixNear (impeller::Matrix a, impeller::Matrix b)
 
inline ::testing::AssertionResult QuaternionNear (impeller::Quaternion a, impeller::Quaternion b)
 
inline ::testing::AssertionResult RectNear (impeller::Rect a, impeller::Rect b)
 
inline ::testing::AssertionResult ColorNear (impeller::Color a, impeller::Color b)
 
inline ::testing::AssertionResult PointNear (impeller::Point a, impeller::Point b)
 
inline ::testing::AssertionResult Vector3Near (impeller::Vector3 a, impeller::Vector3 b)
 
inline ::testing::AssertionResult Vector4Near (impeller::Vector4 a, impeller::Vector4 b)
 
inline ::testing::AssertionResult SizeNear (impeller::Size a, impeller::Size b)
 
inline ::testing::AssertionResult Array4Near (std::array< uint8_t, 4 > a, std::array< uint8_t, 4 > b)
 
inline ::testing::AssertionResult ColorBufferNear (std::vector< uint8_t > a, std::vector< impeller::Color > b)
 
inline ::testing::AssertionResult ColorsNear (std::vector< impeller::Color > a, std::vector< impeller::Color > b)
 

Macro Definition Documentation

◆ ASSERT_ARRAY_4_NEAR

#define ASSERT_ARRAY_4_NEAR (   a,
 
)    ASSERT_PRED2(&::Array4Near, a, b)

Definition at line 197 of file geometry_asserts.h.

◆ ASSERT_COLOR_BUFFER_NEAR

#define ASSERT_COLOR_BUFFER_NEAR (   a,
 
)    ASSERT_PRED2(&::ColorBufferNear, a, b)

Definition at line 198 of file geometry_asserts.h.

◆ ASSERT_COLOR_NEAR

#define ASSERT_COLOR_NEAR (   a,
 
)    ASSERT_PRED2(&::ColorNear, a, b)

Definition at line 192 of file geometry_asserts.h.

◆ ASSERT_COLORS_NEAR

#define ASSERT_COLORS_NEAR (   a,
 
)    ASSERT_PRED2(&::ColorsNear, a, b)

Definition at line 199 of file geometry_asserts.h.

◆ ASSERT_MATRIX_NEAR

#define ASSERT_MATRIX_NEAR (   a,
 
)    ASSERT_PRED2(&::MatrixNear, a, b)

Definition at line 189 of file geometry_asserts.h.

◆ ASSERT_POINT_NEAR

#define ASSERT_POINT_NEAR (   a,
 
)    ASSERT_PRED2(&::PointNear, a, b)

Definition at line 193 of file geometry_asserts.h.

◆ ASSERT_QUATERNION_NEAR

#define ASSERT_QUATERNION_NEAR (   a,
 
)    ASSERT_PRED2(&::QuaternionNear, a, b)

Definition at line 190 of file geometry_asserts.h.

◆ ASSERT_RECT_NEAR

#define ASSERT_RECT_NEAR (   a,
 
)    ASSERT_PRED2(&::RectNear, a, b)

Definition at line 191 of file geometry_asserts.h.

◆ ASSERT_SIZE_NEAR

#define ASSERT_SIZE_NEAR (   a,
 
)    ASSERT_PRED2(&::SizeNear, a, b)

Definition at line 196 of file geometry_asserts.h.

◆ ASSERT_VECTOR3_NEAR

#define ASSERT_VECTOR3_NEAR (   a,
 
)    ASSERT_PRED2(&::Vector3Near, a, b)

Definition at line 194 of file geometry_asserts.h.

◆ ASSERT_VECTOR4_NEAR

#define ASSERT_VECTOR4_NEAR (   a,
 
)    ASSERT_PRED2(&::Vector4Near, a, b)

Definition at line 195 of file geometry_asserts.h.

◆ EXPECT_ARRAY_4_NEAR

#define EXPECT_ARRAY_4_NEAR (   a,
 
)    EXPECT_PRED2(&::Array4Near, a, b)

Definition at line 209 of file geometry_asserts.h.

◆ EXPECT_COLOR_BUFFER_NEAR

#define EXPECT_COLOR_BUFFER_NEAR (   a,
 
)    EXPECT_PRED2(&::ColorBufferNear, a, b)

Definition at line 210 of file geometry_asserts.h.

◆ EXPECT_COLOR_NEAR

#define EXPECT_COLOR_NEAR (   a,
 
)    EXPECT_PRED2(&::ColorNear, a, b)

Definition at line 204 of file geometry_asserts.h.

◆ EXPECT_COLORS_NEAR

#define EXPECT_COLORS_NEAR (   a,
 
)    EXPECT_PRED2(&::ColorsNear, a, b)

Definition at line 211 of file geometry_asserts.h.

◆ EXPECT_MATRIX_NEAR

#define EXPECT_MATRIX_NEAR (   a,
 
)    EXPECT_PRED2(&::MatrixNear, a, b)

Definition at line 201 of file geometry_asserts.h.

◆ EXPECT_POINT_NEAR

#define EXPECT_POINT_NEAR (   a,
 
)    EXPECT_PRED2(&::PointNear, a, b)

Definition at line 205 of file geometry_asserts.h.

◆ EXPECT_QUATERNION_NEAR

#define EXPECT_QUATERNION_NEAR (   a,
 
)    EXPECT_PRED2(&::QuaternionNear, a, b)

Definition at line 202 of file geometry_asserts.h.

◆ EXPECT_RECT_NEAR

#define EXPECT_RECT_NEAR (   a,
 
)    EXPECT_PRED2(&::RectNear, a, b)

Definition at line 203 of file geometry_asserts.h.

◆ EXPECT_SIZE_NEAR

#define EXPECT_SIZE_NEAR (   a,
 
)    EXPECT_PRED2(&::SizeNear, a, b)

Definition at line 208 of file geometry_asserts.h.

◆ EXPECT_VECTOR3_NEAR

#define EXPECT_VECTOR3_NEAR (   a,
 
)    EXPECT_PRED2(&::Vector3Near, a, b)

Definition at line 206 of file geometry_asserts.h.

◆ EXPECT_VECTOR4_NEAR

#define EXPECT_VECTOR4_NEAR (   a,
 
)    EXPECT_PRED2(&::Vector4Near, a, b)

Definition at line 207 of file geometry_asserts.h.

Function Documentation

◆ Array4Near()

inline ::testing::AssertionResult Array4Near ( std::array< uint8_t, 4 >  a,
std::array< uint8_t, 4 >  b 
)

Definition at line 143 of file geometry_asserts.h.

144  {
145  auto equal = NumberNear(a[0], b[0]) && NumberNear(a[1], b[1]) &&
146  NumberNear(a[2], b[2]) && NumberNear(a[3], b[3]);
147 
148  return equal ? ::testing::AssertionSuccess()
149  : ::testing::AssertionFailure() << "Arrays are not equal.";
150 }
bool NumberNear(double a, double b)

References impeller::saturated::b, and NumberNear().

◆ ColorBufferNear()

inline ::testing::AssertionResult ColorBufferNear ( std::vector< uint8_t >  a,
std::vector< impeller::Color b 
)

Definition at line 152 of file geometry_asserts.h.

154  {
155  if (a.size() != b.size() * 4) {
156  return ::testing::AssertionFailure()
157  << "Color buffer length does not match";
158  }
159  for (auto i = 0u; i < b.size(); i++) {
160  auto right = b[i].Premultiply().ToR8G8B8A8();
161  auto j = i * 4;
162  auto equal = NumberNear(a[j], right[0]) && NumberNear(a[j + 1], right[1]) &&
163  NumberNear(a[j + 2], right[2]) &&
164  NumberNear(a[j + 3], right[3]);
165  if (!equal) {
166  ::testing::AssertionFailure() << "Color buffers are not equal.";
167  }
168  }
169  return ::testing::AssertionSuccess();
170 }

References impeller::saturated::b, and NumberNear().

◆ ColorNear()

inline ::testing::AssertionResult ColorNear ( impeller::Color  a,
impeller::Color  b 
)

Definition at line 100 of file geometry_asserts.h.

101  {
102  auto equal = NumberNear(a.red, b.red) && NumberNear(a.green, b.green) &&
103  NumberNear(a.blue, b.blue) && NumberNear(a.alpha, b.alpha);
104 
105  return equal ? ::testing::AssertionSuccess()
106  : ::testing::AssertionFailure() << "Colors are not equal.";
107 }
Scalar blue
Definition: color.h:138
Scalar alpha
Definition: color.h:143
Scalar red
Definition: color.h:128
Scalar green
Definition: color.h:133

References impeller::Color::alpha, impeller::saturated::b, impeller::Color::blue, impeller::Color::green, NumberNear(), and impeller::Color::red.

◆ ColorsNear()

inline ::testing::AssertionResult ColorsNear ( std::vector< impeller::Color a,
std::vector< impeller::Color b 
)

Definition at line 172 of file geometry_asserts.h.

173  {
174  if (a.size() != b.size()) {
175  return ::testing::AssertionFailure() << "Colors length does not match";
176  }
177  for (auto i = 0u; i < b.size(); i++) {
178  auto equal =
179  NumberNear(a[i].red, b[i].red) && NumberNear(a[i].green, b[i].green) &&
180  NumberNear(a[i].blue, b[i].blue) && NumberNear(a[i].alpha, b[i].alpha);
181 
182  if (!equal) {
183  ::testing::AssertionFailure() << "Colors are not equal.";
184  }
185  }
186  return ::testing::AssertionSuccess();
187 }

References impeller::saturated::b, and NumberNear().

◆ MatrixNear()

inline ::testing::AssertionResult MatrixNear ( impeller::Matrix  a,
impeller::Matrix  b 
)

Definition at line 56 of file geometry_asserts.h.

57  {
58  auto equal = NumberNear(a.m[0], b.m[0]) //
59  && NumberNear(a.m[1], b.m[1]) //
60  && NumberNear(a.m[2], b.m[2]) //
61  && NumberNear(a.m[3], b.m[3]) //
62  && NumberNear(a.m[4], b.m[4]) //
63  && NumberNear(a.m[5], b.m[5]) //
64  && NumberNear(a.m[6], b.m[6]) //
65  && NumberNear(a.m[7], b.m[7]) //
66  && NumberNear(a.m[8], b.m[8]) //
67  && NumberNear(a.m[9], b.m[9]) //
68  && NumberNear(a.m[10], b.m[10]) //
69  && NumberNear(a.m[11], b.m[11]) //
70  && NumberNear(a.m[12], b.m[12]) //
71  && NumberNear(a.m[13], b.m[13]) //
72  && NumberNear(a.m[14], b.m[14]) //
73  && NumberNear(a.m[15], b.m[15]);
74 
75  return equal ? ::testing::AssertionSuccess()
76  : ::testing::AssertionFailure()
77  << "Matrixes are not equal " << a << " " << b;
78 }
Scalar m[16]
Definition: matrix.h:39

References impeller::saturated::b, impeller::Matrix::m, and NumberNear().

Referenced by impeller::testing::TEST().

◆ NumberNear()

bool NumberNear ( double  a,
double  b 
)
inline

Definition at line 18 of file geometry_asserts.h.

18  {
19  if (a == b) {
20  return true;
21  }
22  if (std::isnan(a) || std::isnan(b)) {
23  return false;
24  }
25 
26  // We used to compare based on an absolute difference of 1e-3 which
27  // would allow up to 10 bits of mantissa difference in a float
28  // (leaving 14 bits of accuracy being tested). Some numbers in the tests
29  // will fail with a bit difference of up to 19 (a little over 4 bits) even
30  // though the numbers print out identically using the float ostream output
31  // at the default output precision. Choosing a max "units of least precision"
32  // of 32 allows up to 5 bits of imprecision.
33  static constexpr float kImpellerTestingMaxULP = 32;
34 
35  // We also impose a minimum step size so that cases of comparing numbers
36  // very close to 0.0 don't compute a huge number of ULPs due to the ever
37  // increasing precision near 0. This value is approximately the step size
38  // of numbers going less than 1.0f.
39  static constexpr float kMinimumULPStep = (1.0f / (1 << 24));
40 
41  auto adjust_step = [](float v) {
42  return (std::abs(v) < kMinimumULPStep) ? std::copysignf(kMinimumULPStep, v)
43  : v;
44  };
45 
46  float step_ab = adjust_step(a - std::nexttowardf(a, b));
47  float step_ba = adjust_step(b - std::nexttowardf(b, a));
48 
49  float ab_ulps = (a - b) / step_ab;
50  float ba_ulps = (b - a) / step_ba;
51  FML_CHECK(ab_ulps >= 0 && ba_ulps >= 0);
52 
53  return (std::min(ab_ulps, ba_ulps) < kImpellerTestingMaxULP);
54 }

References impeller::saturated::b.

Referenced by Array4Near(), ColorBufferNear(), ColorNear(), ColorsNear(), MatrixNear(), PointNear(), QuaternionNear(), RectNear(), SizeNear(), impeller::testing::TEST_P(), Vector3Near(), and Vector4Near().

◆ PointNear()

inline ::testing::AssertionResult PointNear ( impeller::Point  a,
impeller::Point  b 
)

Definition at line 109 of file geometry_asserts.h.

110  {
111  auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y);
112 
113  return equal ? ::testing::AssertionSuccess()
114  : ::testing::AssertionFailure()
115  << "Points are not equal (" << a << " " << b << ").";
116 }

References impeller::saturated::b, NumberNear(), impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

Referenced by SolidVerticesNear(), and TextureVerticesNear().

◆ QuaternionNear()

inline ::testing::AssertionResult QuaternionNear ( impeller::Quaternion  a,
impeller::Quaternion  b 
)

Definition at line 80 of file geometry_asserts.h.

81  {
82  auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y) &&
83  NumberNear(a.z, b.z) && NumberNear(a.w, b.w);
84 
85  return equal ? ::testing::AssertionSuccess()
86  : ::testing::AssertionFailure() << "Quaternions are not equal.";
87 }

References impeller::saturated::b, NumberNear(), impeller::Quaternion::w, impeller::Quaternion::x, impeller::Quaternion::y, and impeller::Quaternion::z.

◆ RectNear()

inline ::testing::AssertionResult RectNear ( impeller::Rect  a,
impeller::Rect  b 
)

Definition at line 89 of file geometry_asserts.h.

89  {
90  auto equal = NumberNear(a.GetLeft(), b.GetLeft()) &&
91  NumberNear(a.GetTop(), b.GetTop()) &&
92  NumberNear(a.GetRight(), b.GetRight()) &&
93  NumberNear(a.GetBottom(), b.GetBottom());
94 
95  return equal ? ::testing::AssertionSuccess()
96  : ::testing::AssertionFailure()
97  << "Rects are not equal (" << a << " " << b << ")";
98 }
constexpr auto GetBottom() const
Definition: rect.h:361
constexpr auto GetTop() const
Definition: rect.h:357
constexpr auto GetLeft() const
Definition: rect.h:355
constexpr auto GetRight() const
Definition: rect.h:359

References impeller::saturated::b, impeller::TRect< T >::GetBottom(), impeller::TRect< T >::GetLeft(), impeller::TRect< T >::GetRight(), impeller::TRect< T >::GetTop(), and NumberNear().

Referenced by impeller::testing::TEST(), and impeller::testing::TEST_P().

◆ SizeNear()

inline ::testing::AssertionResult SizeNear ( impeller::Size  a,
impeller::Size  b 
)

Definition at line 136 of file geometry_asserts.h.

136  {
137  auto equal = NumberNear(a.width, b.width) && NumberNear(a.height, b.height);
138 
139  return equal ? ::testing::AssertionSuccess()
140  : ::testing::AssertionFailure() << "Sizes are not equal.";
141 }
Type height
Definition: size.h:29
Type width
Definition: size.h:28

References impeller::saturated::b, impeller::TSize< T >::height, NumberNear(), and impeller::TSize< T >::width.

◆ Vector3Near()

inline ::testing::AssertionResult Vector3Near ( impeller::Vector3  a,
impeller::Vector3  b 
)

Definition at line 118 of file geometry_asserts.h.

119  {
120  auto equal =
121  NumberNear(a.x, b.x) && NumberNear(a.y, b.y) && NumberNear(a.z, b.z);
122 
123  return equal ? ::testing::AssertionSuccess()
124  : ::testing::AssertionFailure() << "Vector3s are not equal.";
125 }

References impeller::saturated::b, NumberNear(), impeller::Vector3::x, impeller::Vector3::y, and impeller::Vector3::z.

◆ Vector4Near()

inline ::testing::AssertionResult Vector4Near ( impeller::Vector4  a,
impeller::Vector4  b 
)

Definition at line 127 of file geometry_asserts.h.

128  {
129  auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y) &&
130  NumberNear(a.z, b.z) && NumberNear(a.w, b.w);
131 
132  return equal ? ::testing::AssertionSuccess()
133  : ::testing::AssertionFailure() << "Vector4s are not equal.";
134 }

References impeller::saturated::b, NumberNear(), impeller::Vector4::w, impeller::Vector4::x, impeller::Vector4::y, and impeller::Vector4::z.