5 #include "gtest/gtest.h"
10 #include <type_traits>
12 #include "flutter/fml/build_config.h"
13 #include "flutter/testing/testing.h"
35 1.0f, 1.0f + std::numeric_limits<float>::epsilon() * 4));
38 TEST(GeometryTest, MakeColumn) {
44 auto expect =
Matrix{1, 2, 3, 4,
49 ASSERT_TRUE(matrix == expect);
52 TEST(GeometryTest, MakeRow) {
58 auto expect =
Matrix{1, 5, 9, 13,
63 ASSERT_TRUE(matrix == expect);
66 TEST(GeometryTest, RotationMatrix) {
68 auto expect =
Matrix{0.707, 0.707, 0, 0,
75 TEST(GeometryTest, InvertMultMatrix) {
78 auto invert = rotation.
Invert();
79 auto expect =
Matrix{0.707, -0.707, 0, 0,
87 auto invert =
scale.Invert();
88 auto expect =
Matrix{0.5, 0, 0, 0,
96 TEST(GeometryTest, MatrixBasis) {
97 auto matrix =
Matrix{1, 2, 3, 4,
101 auto basis = matrix.
Basis();
102 auto expect =
Matrix{1, 2, 3, 0,
109 TEST(GeometryTest, MutliplicationMatrix) {
111 auto invert = rotation.
Invert();
115 TEST(GeometryTest, DeterminantTest) {
116 auto matrix =
Matrix{3, 4, 14, 155, 2, 1, 3, 4, 2, 3, 2, 1, 1, 2, 4, 2};
117 ASSERT_EQ(matrix.GetDeterminant(), -1889);
120 TEST(GeometryTest, InvertMatrix) {
121 auto inverted =
Matrix{10, -9, -12, 8,
128 438.0 / 85123.0, 1751.0 / 85123.0, -7783.0 / 85123.0, 4672.0 / 85123.0,
129 393.0 / 85123.0, -178.0 / 85123.0, -570.0 / 85123.0, 4192 / 85123.0,
130 -5230.0 / 85123.0, 2802.0 / 85123.0, -3461.0 / 85123.0, 962.0 / 85123.0,
131 2690.0 / 85123.0, 1814.0 / 85123.0, 3896.0 / 85123.0, 319.0 / 85123.0};
136 TEST(GeometryTest, TestDecomposition) {
141 ASSERT_TRUE(result.has_value());
149 TEST(GeometryTest, TestDecomposition2) {
154 auto result = (translated * rotated * scaled).Decompose();
156 ASSERT_TRUE(result.has_value());
168 ASSERT_FLOAT_EQ(res.
scale.
x, 2);
169 ASSERT_FLOAT_EQ(res.
scale.
y, 3);
170 ASSERT_FLOAT_EQ(res.
scale.
z, 1);
173 TEST(GeometryTest, TestRecomposition) {
181 ASSERT_TRUE(result.has_value());
195 TEST(GeometryTest, TestRecomposition2) {
202 ASSERT_TRUE(result.has_value());
207 TEST(GeometryTest, MatrixVectorMultiplication) {
212 auto vector =
Vector4(10, 20, 30, 2);
214 Vector4 result = matrix * vector;
215 auto expected =
Vector4(160, 220, 260, 2);
223 auto vector =
Vector3(10, 20, 30);
225 Vector3 result = matrix * vector;
226 auto expected =
Vector3(60, 120, 160);
234 auto vector =
Point(10, 20);
236 Point result = matrix * vector;
237 auto expected =
Point(60, 120);
244 auto vector =
Vector3(3, 3, -3);
246 Vector3 result = matrix * vector;
247 auto expected =
Vector3(-1, -1, 1.3468);
254 auto point =
Point(3, 3);
256 Point result = matrix * point;
257 auto expected =
Point(-1, -1);
264 auto point =
Point(3, 3);
266 Point result = matrix * point;
267 auto expected =
Point(0, 0);
272 TEST(GeometryTest, MatrixMakeRotationFromQuaternion) {
292 TEST(GeometryTest, MatrixTransformDirection) {
297 auto vector =
Vector4(10, 20, 30, 2);
299 Vector4 result = matrix.TransformDirection(vector);
300 auto expected =
Vector4(-40, 20, 60, 2);
308 auto vector =
Vector3(10, 20, 30);
310 Vector3 result = matrix.TransformDirection(vector);
311 auto expected =
Vector3(-40, 20, 60);
319 auto vector =
Point(10, 20);
321 Point result = matrix.TransformDirection(vector);
322 auto expected =
Point(-40, 20);
327 TEST(GeometryTest, MatrixGetMaxBasisLength) {
330 ASSERT_EQ(m.GetMaxBasisLength(), 3);
333 ASSERT_EQ(m.GetMaxBasisLength(), 5);
338 ASSERT_EQ(m.GetMaxBasisLength(), 4);
342 TEST(GeometryTest, MatrixGetMaxBasisLengthXY) {
345 ASSERT_EQ(m.GetMaxBasisLengthXY(), 3);
348 ASSERT_EQ(m.GetMaxBasisLengthXY(), 5);
353 ASSERT_EQ(m.GetMaxBasisLengthXY(), 4);
359 1.0f, 0.0f, 0.0f, 0.0f,
360 0.0f, 1.0f, 0.0f, 0.0f,
361 4.0f, 0.0f, 1.0f, 0.0f,
362 0.0f, 0.0f, 0.0f, 1.0f
365 ASSERT_EQ(m.GetMaxBasisLengthXY(), 1.0f);
369 TEST(GeometryTest, MatrixMakeOrthographic) {
393 TEST(GeometryTest, MatrixMakePerspective) {
417 TEST(GeometryTest, MatrixGetBasisVectors) {
441 TEST(GeometryTest, MatrixGetDirectionScale) {
445 ASSERT_FLOAT_EQ(result, 1);
453 ASSERT_FLOAT_EQ(result, 1);
460 ASSERT_FLOAT_EQ(result, 8);
464 TEST(GeometryTest, MatrixIsAligned) {
474 ASSERT_FALSE(result);
478 TEST(GeometryTest, MatrixTranslationScaleOnly) {
481 bool result = m.IsTranslationScaleOnly();
487 bool result = m.IsTranslationScaleOnly();
493 bool result = m.IsTranslationScaleOnly();
499 bool result = m.IsTranslationScaleOnly();
500 ASSERT_FALSE(result);
504 TEST(GeometryTest, MatrixLookAt) {
550 TEST(GeometryTest, QuaternionLerp) {
554 auto q3 = q1.Slerp(q2, 0.5);
561 TEST(GeometryTest, QuaternionVectorMultiply) {
603 TEST(GeometryTest, CanGenerateMipCounts) {
616 TEST(GeometryTest, CanConvertTTypesExplicitly) {
627 ASSERT_EQ(s2.
width, 1u);
639 TEST(GeometryTest, CanPerformAlgebraicPointOps) {
669 TEST(GeometryTest, CanPerformAlgebraicPointOpsWithArithmeticTypes) {
701 TEST(GeometryTest, PointIntegerCoercesToFloat) {
706 ASSERT_FLOAT_EQ(p2.
x, 2u);
707 ASSERT_FLOAT_EQ(p2.
y, 4u);
713 ASSERT_FLOAT_EQ(p2.
x, 2u);
714 ASSERT_FLOAT_EQ(p2.
y, 4u);
720 ASSERT_FLOAT_EQ(p2.
x, 2u);
721 ASSERT_FLOAT_EQ(p2.
y, 6u);
727 ASSERT_FLOAT_EQ(p2.
x, 1u);
728 ASSERT_FLOAT_EQ(p2.
y, 2u);
735 ASSERT_FLOAT_EQ(p2.
x, 2u);
736 ASSERT_FLOAT_EQ(p2.
y, 4u);
742 ASSERT_FLOAT_EQ(p2.
x, 2u);
743 ASSERT_FLOAT_EQ(p2.
y, 4u);
749 ASSERT_FLOAT_EQ(p2.
x, 2u);
750 ASSERT_FLOAT_EQ(p2.
y, 6u);
756 ASSERT_FLOAT_EQ(p2.
x, 1u);
757 ASSERT_FLOAT_EQ(p2.
y, 2u);
761 TEST(GeometryTest, SizeCoercesToPoint) {
821 TEST(GeometryTest, CanUsePointAssignmentOperators) {
896 TEST(GeometryTest, PointDotProduct) {
900 ASSERT_FLOAT_EQ(s, -1);
906 ASSERT_FLOAT_EQ(s, 0);
912 ASSERT_FLOAT_EQ(s, -5);
916 TEST(GeometryTest, PointCrossProduct) {
920 ASSERT_FLOAT_EQ(s, 0);
926 ASSERT_FLOAT_EQ(s, -1);
932 ASSERT_FLOAT_EQ(s, -10);
936 TEST(GeometryTest, PointReflect) {
940 auto reflected = a.
Reflect(axis);
941 auto expected =
Point(2, -3);
948 auto reflected = a.
Reflect(axis);
949 auto expected =
Point(0, -1);
956 auto reflected = a.
Reflect(axis);
963 auto a_abs = a.
Abs();
964 auto expected =
Point(1, 2);
968 TEST(GeometryTest, PointAngleTo) {
1000 Point expected(0, 2);
1021 Point expected(1, 10);
1035 Vector4 expected(1, 10, 3, 4);
1042 Point expected(1, 2);
1054 Vector4 p(1.5, 2.3, 3.9, 4.0);
1063 Point expected(2, 3);
1075 Vector4 p(1.5, 2.3, 3.9, 4.0);
1084 Point expected(2, 2);
1096 Vector4 p(1.5, 2.3, 3.9, 4.0);
1105 Point expected(4, 8);
1119 Vector4 expected(4, 8, 12, 16);
1123 TEST(GeometryTest, CanUseVector3AssignmentOperators) {
1145 ASSERT_EQ(p.
z, 12u);
1173 TEST(GeometryTest, CanPerformAlgebraicVector3Ops) {
1177 ASSERT_EQ(p2.
x, 2u);
1178 ASSERT_EQ(p2.
y, 4u);
1179 ASSERT_EQ(p2.
z, 6u);
1185 ASSERT_EQ(p2.
x, 2u);
1186 ASSERT_EQ(p2.
y, 4u);
1187 ASSERT_EQ(p2.
z, 6u);
1193 ASSERT_EQ(p2.
x, 2u);
1194 ASSERT_EQ(p2.
y, 6u);
1195 ASSERT_EQ(p2.
z, 12u);
1201 ASSERT_EQ(p2.
x, 1u);
1202 ASSERT_EQ(p2.
y, 2u);
1203 ASSERT_EQ(p2.
z, 3u);
1207 TEST(GeometryTest, CanPerformAlgebraicVector3OpsWithArithmeticTypes) {
1220 ASSERT_EQ(p2.
x, -1);
1255 ASSERT_EQ(p2.
z, -1);
1275 TEST(GeometryTest, ColorPremultiply) {
1277 Color a(1.0, 0.5, 0.2, 0.5);
1284 Color a(0.5, 0.25, 0.1, 0.5);
1291 Color a(0.5, 0.25, 0.1, 0.0);
1298 TEST(GeometryTest, ColorR8G8B8A8) {
1300 Color a(1.0, 0.5, 0.2, 0.5);
1301 std::array<uint8_t, 4> expected = {255, 128, 51, 128};
1306 Color a(0.0, 0.0, 0.0, 0.0);
1307 std::array<uint8_t, 4> expected = {0, 0, 0, 0};
1312 Color a(1.0, 1.0, 1.0, 1.0);
1313 std::array<uint8_t, 4> expected = {255, 255, 255, 255};
1320 Color a(0.0, 0.0, 0.0, 0.0);
1321 Color b(1.0, 1.0, 1.0, 1.0);
1330 Color a(0.2, 0.4, 1.0, 0.5);
1331 Color b(0.4, 1.0, 0.2, 0.3);
1360 TEST(GeometryTest, ColorMakeRGBA8) {
1375 Color b(0.247059, 0.498039, 0.74902, 0.498039);
1380 TEST(GeometryTest, ColorApplyColorMatrix) {
1389 auto expected =
Color(1, 1, 1, 1);
1401 auto expected =
Color(0.11, 0.22, 0.33, 0.44);
1406 TEST(GeometryTest, ColorLinearToSRGB) {
1409 auto expected =
Color(1, 1, 1, 1);
1415 auto expected =
Color(0, 0, 0, 0);
1421 auto expected =
Color(0.484529, 0.665185, 0.797738, 0.8);
1426 TEST(GeometryTest, ColorSRGBToLinear) {
1429 auto expected =
Color(1, 1, 1, 1);
1435 auto expected =
Color(0, 0, 0, 0);
1441 auto expected =
Color(0.0331048, 0.132868, 0.318547, 0.8);
1463 {0.392157, 0.584314, 0.929412, 0.75},
1464 {0.878431, 0.916863, 0.985882, 0.9375},
1465 {0.513726, 0.667451, 0.943529, 0.9375},
1467 {0.392157, 0.584314, 0.929412, 0.5625},
1469 {0.392157, 0.584314, 0.929412, 0.1875},
1470 {0.848039, 0.896078, 0.982353, 0.75},
1471 {0.544118, 0.688235, 0.947059, 0.75},
1472 {0.696078, 0.792157, 0.964706, 0.375},
1474 {0.392157, 0.584314, 0.929412, 0.5625},
1475 {0.878431, 0.916863, 0.985882, 0.9375},
1476 {0.74902, 0.916863, 0.985882, 0.9375},
1477 {0.513726, 0.667451, 0.943529, 0.9375},
1478 {0.878431, 0.916863, 0.985882, 0.9375},
1479 {0.878431, 0.916863, 0.985882, 0.9375},
1480 {0.513725, 0.667451, 0.943529, 0.9375},
1481 {0.878431, 0.916863, 0.985882, 0.9375},
1482 {0.654166, 0.775505, 0.964318, 0.9375},
1483 {0.643137, 0.566275, 0.428235, 0.9375},
1484 {0.643137, 0.566275, 0.428235, 0.9375},
1485 {0.513726, 0.667451, 0.943529, 0.9375},
1486 {0.617208, 0.655639, 0.724659, 0.9375},
1487 {0.617208, 0.655639, 0.724659, 0.9375},
1488 {0.617208, 0.655639, 0.724659, 0.9375},
1489 {0.878431, 0.916863, 0.985882, 0.9375},
1493 {0.196078, 0.803922, 0.196078, 0.75},
1494 {0.392157, 0.584314, 0.929412, 0.75},
1495 {0.235294, 0.76, 0.342745, 0.9375},
1496 {0.352941, 0.628235, 0.782745, 0.9375},
1497 {0.196078, 0.803922, 0.196078, 0.5625},
1498 {0.392157, 0.584314, 0.929412, 0.5625},
1499 {0.196078, 0.803922, 0.196078, 0.1875},
1500 {0.392157, 0.584314, 0.929412, 0.1875},
1501 {0.245098, 0.74902, 0.379412, 0.75},
1502 {0.343137, 0.639216, 0.746078, 0.75},
1503 {0.294118, 0.694118, 0.562745, 0.375},
1504 {0.441176, 1, 0.844118, 1},
1505 {0.0768935, 0.469742, 0.182238, 0.5625},
1506 {0.424452, 0.828743, 0.79105, 0.9375},
1507 {0.209919, 0.779839, 0.757001, 0.9375},
1508 {0.235294, 0.628235, 0.342745, 0.9375},
1509 {0.352941, 0.76, 0.782745, 0.9375},
1510 {0.41033, 0.877647, 0.825098, 0.9375},
1511 {0.117647, 0.567403, 0.609098, 0.9375},
1512 {0.209919, 0.779839, 0.443783, 0.9375},
1513 {0.266006, 0.693915, 0.758818, 0.9375},
1514 {0.235294, 0.409412, 0.665098, 0.9375},
1515 {0.378316, 0.546897, 0.681707, 0.9375},
1516 {0.163783, 0.559493, 0.334441, 0.9375},
1517 {0.266235, 0.748588, 0.373686, 0.9375},
1518 {0.339345, 0.629787, 0.811502, 0.9375},
1519 {0.241247, 0.765953, 0.348698, 0.9375},
1520 {0.346988, 0.622282, 0.776792, 0.9375},
1525 {0.392157, 0.584314, 0.929412, 0.75},
1526 {0.0784314, 0.116863, 0.185882, 0.9375},
1527 {0.313726, 0.467451, 0.743529, 0.9375},
1529 {0.392157, 0.584314, 0.929412, 0.5625},
1531 {0.392157, 0.584314, 0.929412, 0.1875},
1532 {0.0980392, 0.146078, 0.232353, 0.75},
1533 {0.294118, 0.438235, 0.697059, 0.75},
1534 {0.196078, 0.292157, 0.464706, 0.375},
1535 {0.294118, 0.438235, 0.697059, 1},
1537 {0.313726, 0.467451, 0.743529, 0.9375},
1538 {0.0784314, 0.218039, 0.701176, 0.9375},
1539 {0.0784314, 0.116863, 0.185882, 0.9375},
1540 {0.313726, 0.467451, 0.743529, 0.9375},
1541 {0.313726, 0.467451, 0.743529, 0.9375},
1542 {0.0784314, 0.116863, 0.185882, 0.9375},
1543 {0.0784314, 0.116863, 0.185882, 0.9375},
1544 {0.170704, 0.321716, 0.704166, 0.9375},
1545 {0.313726, 0.467451, 0.743529, 0.9375},
1546 {0.313726, 0.467451, 0.743529, 0.9375},
1547 {0.0784314, 0.116863, 0.185882, 0.9375},
1548 {0.417208, 0.455639, 0.524659, 0.9375},
1549 {0.417208, 0.455639, 0.524659, 0.9375},
1550 {0.417208, 0.455639, 0.524659, 0.9375},
1551 {0.0784314, 0.116863, 0.185882, 0.9375},
1579 #define _BLEND_MODE_RESULT_CHECK(blend_mode) \
1580 blend_i = static_cast<BlendT>(BlendMode::k##blend_mode); \
1581 expected = ColorBlendTestData::kExpectedResults[source_i][blend_i]; \
1582 EXPECT_COLOR_NEAR(dst.Blend(src, BlendMode::k##blend_mode), expected);
1584 TEST(GeometryTest, ColorBlendReturnsExpectedResults) {
1585 using BlendT = std::underlying_type_t<BlendMode>;
1587 for (
size_t source_i = 0;
1598 #define _BLEND_MODE_NAME_CHECK(blend_mode) \
1599 case BlendMode::k##blend_mode: \
1600 ASSERT_STREQ(result, #blend_mode); \
1604 using BlendT = std::underlying_type_t<BlendMode>;
1612 TEST(GeometryTest, CanConvertBetweenDegressAndRadians) {
1620 TEST(GeometryTest, MatrixPrinting) {
1622 std::stringstream stream;
1625 ASSERT_EQ(stream.str(), R
"((
1626 1.000000, 0.000000, 0.000000, 0.000000,
1627 0.000000, 1.000000, 0.000000, 0.000000,
1628 0.000000, 0.000000, 1.000000, 0.000000,
1629 0.000000, 0.000000, 0.000000, 1.000000,
1634 std::stringstream stream;
1638 ASSERT_EQ(stream.str(), R"((
1639 1.000000, 0.000000, 0.000000, 10.000000,
1640 0.000000, 1.000000, 0.000000, 20.000000,
1641 0.000000, 0.000000, 1.000000, 30.000000,
1642 0.000000, 0.000000, 0.000000, 1.000000,
1647 TEST(GeometryTest, PointPrinting) {
1649 std::stringstream stream;
1652 ASSERT_EQ(stream.str(), "(0, 0)");
1656 std::stringstream stream;
1659 ASSERT_EQ(stream.str(),
"(13, 37)");
1663 TEST(GeometryTest, Vector3Printing) {
1665 std::stringstream stream;
1668 ASSERT_EQ(stream.str(),
"(0, 0, 0)");
1672 std::stringstream stream;
1675 ASSERT_EQ(stream.str(),
"(1, 2, 3)");
1679 TEST(GeometryTest, Vector4Printing) {
1681 std::stringstream stream;
1684 ASSERT_EQ(stream.str(),
"(0, 0, 0, 1)");
1688 std::stringstream stream;
1691 ASSERT_EQ(stream.str(),
"(1, 2, 3, 4)");
1695 TEST(GeometryTest, ColorPrinting) {
1697 std::stringstream stream;
1700 ASSERT_EQ(stream.str(),
"(0, 0, 0, 0)");
1704 std::stringstream stream;
1705 Color m(1, 2, 3, 4);
1707 ASSERT_EQ(stream.str(),
"(1, 2, 3, 4)");
1722 std::vector<Scalar> stops = {0.0, 1.0};
1727 ASSERT_EQ(gradient.texture_size, 2u);
1734 std::vector<Scalar> stops = {0.0, 0.25, 0.25, 1.0};
1737 ASSERT_EQ(gradient.texture_size, 5u);
1745 std::vector<Scalar> stops = {0.0, 0.33, 0.66, 1.0};
1750 ASSERT_EQ(gradient.texture_size, 4u);
1756 std::vector<Scalar> stops = {0.0, 0.25, 1.0};
1760 std::vector<Color> lerped_colors = {
1768 ASSERT_EQ(gradient.texture_size, 5u);
1773 std::vector<Color> colors = {};
1774 std::vector<Scalar> stops = {};
1775 for (
auto i = 0u; i < 1025; i++) {
1777 stops.push_back(i / 1025.0);
1782 ASSERT_EQ(gradient.texture_size, 1024u);
1783 ASSERT_EQ(gradient.color_bytes.size(), 1024u * 4);
1787 TEST(GeometryTest, HalfConversions) {
1788 #if defined(FML_OS_MACOSX) || defined(FML_OS_IOS) || \
1789 defined(FML_OS_IOS_SIMULATOR)
1813 ASSERT_EQ(
Half(0.5f),
Half(0.5f16));
1814 ASSERT_EQ(
Half(0.5),
Half(0.5f16));
1817 GTEST_SKIP() <<
"Half-precision floats (IEEE 754) are not portable and "
1818 "only used on Apple platforms.";
1819 #endif // FML_OS_MACOSX || FML_OS_IOS || FML_OS_IOS_SIMULATOR