shape_test.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // shape_test.cxx
  3. // math
  4. //
  5. // Created by Sam Jaffe on 5/4/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include "game/math/shape.hpp"
  9. #include <testing/xcode_gtest_helper.h>
  10. #include "test_printers.h"
  11. using namespace math::dim2;
  12. using namespace testing;
  13. struct FromOriginTest : TestWithParam<line> {};
  14. TEST_P(FromOriginTest, IntersectsAtOrigin) {
  15. line l1 = {GetParam().first, {{0, 0}}};
  16. line l2 = {{{0, 0}}, GetParam().second};
  17. EXPECT_THAT(math::lines::intersection(l1, l2), Eq(point{{0, 0}}));
  18. }
  19. std::vector<line> const point_pairs{
  20. {{{1, 1}}, {{0, 0}}}, // 0 length
  21. {{{1, 1}}, {{1, 0}}}, // -45deg
  22. {{{1, 1}}, {{0, 1}}}, // +45deg
  23. {{{1, 1}}, {{1, 1}}}, // +0deg
  24. {{{1, 1}}, {{2, 1}}}, // -18deg (approx)
  25. {{{1, 1}}, {{1, 2}}}, // +18deg (approx)
  26. {{{2, 3}}, {{1, 2}}}, //
  27. {{{1, 1}}, {{-1, 0}}}, // +135deg
  28. {{{1, 1}}, {{0, -1}}}, // -135deg
  29. {{{1, 1}}, {{-1, -1}}}, // +180deg
  30. {{{1, 1}}, {{1, -1}}}, // +90deg
  31. {{{1, 1}}, {{-1, 1}}}, // -90deg
  32. };
  33. INSTANTIATE_TEST_SUITE_P(LineIntersection, FromOriginTest,
  34. ValuesIn(point_pairs));
  35. struct UnitLineTest : TestWithParam<point> {};
  36. TEST(LineTest, UnitLineHasLengthOne) {
  37. line const unit{{{0, 0}}, {{1, 0}}};
  38. EXPECT_THAT(unit.length(), Eq(1));
  39. }
  40. TEST(LineTest, ParallelLinesHaveSameSlope) {
  41. line const lhs{{{0, 0}}, {{1, 0}}};
  42. line const rhs{{{-1, 0}}, {{-2, 0}}};
  43. EXPECT_TRUE(math::lines::parallel(lhs, rhs));
  44. }
  45. TEST_P(UnitLineTest, OrthoOnIntersection) {
  46. line const ln{{{0, 0}}, {{1, 0}}};
  47. point const pt = GetParam();
  48. line const expected{pt, {{pt[0], 0}}};
  49. EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
  50. }
  51. TEST_P(UnitLineTest, OrthoOnIntersectionY) {
  52. line const ln{{{0, 0}}, {{0, 1}}};
  53. point const pt = GetParam();
  54. line const expected{pt, {{pt[0], 0}}};
  55. EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
  56. }
  57. std::vector<point> x_orthos{
  58. {{0, 1}}, {{1, 1}}, {{1, 0}}, {{-1, 0}}, {{0, -1}}, {{-1, -1}},
  59. {{0, 2}}, {{2, 2}}, {{2, 0}}, {{-2, 0}}, {{0, -2}}, {{-2, -2}},
  60. {{2, 1}}, {{1, 2}}, {{-2, 1}}, {{-1, 2}}, {{1, -2}}, {{2, -1}}};
  61. INSTANTIATE_TEST_SUITE_P(LineOrthogonal, UnitLineTest, ValuesIn(x_orthos));
  62. struct DiagonalTest : TestWithParam<std::pair<point, float>> {};
  63. TEST_P(DiagonalTest, OrthoOnIntersection) {
  64. line const ln{{{0, 0}}, {{1, 1}}};
  65. point const pt = GetParam().first;
  66. line const expected{pt, {{GetParam().second, GetParam().second}}};
  67. EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
  68. }
  69. std::vector<std::pair<point, float>> diag_orthos{
  70. {{{0, 1}}, 0.5f}, {{{1, 1}}, 1.f}, {{{1, 0}}, 0.5f},
  71. {{{-1, 0}}, -0.5f}, {{{0, -1}}, -0.5f}, {{{-1, -1}}, -1.f},
  72. {{{0, 2}}, 1.f}, {{{2, 2}}, 2.f}, {{{2, 0}}, 1.f},
  73. {{{-2, 0}}, -1.f}, {{{0, -2}}, -1.f}, {{{-2, -2}}, -2.f},
  74. {{{2, 1}}, 1.5f}, {{{1, 2}}, 1.5f}, {{{-2, 1}}, -0.5f},
  75. {{{-1, 2}}, 0.5f}, {{{1, -2}}, -0.5f}, {{{2, -1}}, 0.5f}};
  76. INSTANTIATE_TEST_SUITE_P(LineOrthogonal, DiagonalTest, ValuesIn(diag_orthos));
  77. struct QuadTest : TestWithParam<std::tuple<float, float>> {};
  78. TEST_P(QuadTest, SquareProducesQuadWithCornersAtX) {
  79. float x = std::get<0>(GetParam());
  80. square const square{{{0, 0}}, x};
  81. quad const expected{{{0, 0}}, {{x, 0}}, {{x, x}}, {{0, x}}};
  82. EXPECT_THAT(quad(square), Eq(expected));
  83. }
  84. TEST_P(QuadTest, SquareProducesRectangleWithXX) {
  85. float side = std::get<0>(GetParam());
  86. square const square{{{0, 0}}, side};
  87. EXPECT_THAT(rectangle(square).origin, Eq(square.origin));
  88. EXPECT_THAT(rectangle(square).size[0], Eq(side));
  89. EXPECT_THAT(rectangle(square).size[0], Eq(side));
  90. }
  91. TEST_P(QuadTest, OffsetSquareProducesRectangleWithXX) {
  92. float side = std::get<0>(GetParam());
  93. square const square{{{1, 1}}, side};
  94. EXPECT_THAT(rectangle(square).origin, Eq(square.origin));
  95. EXPECT_THAT(rectangle(square).size[0], Eq(side));
  96. EXPECT_THAT(rectangle(square).size[0], Eq(side));
  97. }
  98. TEST_P(QuadTest, RectProducesQuadWithCornersAtXY) {
  99. float x = std::get<0>(GetParam());
  100. float y = std::get<1>(GetParam());
  101. rectangle const square{{{0, 0}}, {{x, y}}};
  102. quad const expected{{{0, 0}}, {{x, 0}}, {{x, y}}, {{0, y}}};
  103. EXPECT_THAT(quad(square), Eq(expected));
  104. }
  105. INSTANTIATE_TEST_SUITE_P(Upcast, QuadTest,
  106. Combine(Values(0.5, 1.0, 1.5, 2.0),
  107. Values(0.5, 1.0, 1.5, 2.0)));