shape_test.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <gmock/gmock.h>
  9. #include "game/math/shape.hpp"
  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_CASE_P(LineIntersection, FromOriginTest,
  34. ValuesIn(point_pairs));
  35. struct UnitLineTest : TestWithParam<point> {};
  36. TEST_P(UnitLineTest, OrthoOnIntersection) {
  37. line const ln{{{0, 0}}, {{1, 0}}};
  38. point const pt = GetParam();
  39. line const expected{pt, {{pt[0], 0}}};
  40. EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
  41. }
  42. TEST_P(UnitLineTest, OrthoOnIntersectionY) {
  43. line const ln{{{0, 0}}, {{0, 1}}};
  44. point const pt = GetParam();
  45. line const expected{pt, {{pt[0], 0}}};
  46. EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
  47. }
  48. std::vector<point> x_orthos{
  49. {{0, 1}}, {{1, 1}}, {{1, 0}}, {{-1, 0}}, {{0, -1}}, {{-1, -1}},
  50. {{0, 2}}, {{2, 2}}, {{2, 0}}, {{-2, 0}}, {{0, -2}}, {{-2, -2}},
  51. {{2, 1}}, {{1, 2}}, {{-2, 1}}, {{-1, 2}}, {{1, -2}}, {{2, -1}}};
  52. INSTANTIATE_TEST_CASE_P(LineOrthogonal, UnitLineTest, ValuesIn(x_orthos));
  53. struct DiagonalTest : TestWithParam<std::pair<point, float>> {};
  54. TEST_P(DiagonalTest, OrthoOnIntersection) {
  55. line const ln{{{0, 0}}, {{1, 1}}};
  56. point const pt = GetParam().first;
  57. line const expected{pt, {{GetParam().second, GetParam().second}}};
  58. EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
  59. }
  60. std::vector<std::pair<point, float>> diag_orthos{
  61. {{{0, 1}}, 0.5f}, {{{1, 1}}, 1.f}, {{{1, 0}}, 0.5f},
  62. {{{-1, 0}}, -0.5f}, {{{0, -1}}, -0.5f}, {{{-1, -1}}, -1.f},
  63. {{{0, 2}}, 1.f}, {{{2, 2}}, 2.f}, {{{2, 0}}, 1.f},
  64. {{{-2, 0}}, -1.f}, {{{0, -2}}, -1.f}, {{{-2, -2}}, -2.f},
  65. {{{2, 1}}, 1.5f}, {{{1, 2}}, 1.5f}, {{{-2, 1}}, -0.5f},
  66. {{{-1, 2}}, 0.5f}, {{{1, -2}}, -0.5f}, {{{2, -1}}, 0.5f}};
  67. INSTANTIATE_TEST_CASE_P(LineOrthogonal, DiagonalTest, ValuesIn(diag_orthos));
  68. struct QuadTest : TestWithParam<std::tuple<float, float>> {};
  69. TEST_P(QuadTest, SquareProducesQuadWithCornersAtX) {
  70. float x = std::get<0>(GetParam());
  71. square const square{{{0, 0}}, x};
  72. quad const expected{{{0, 0}}, {{x, 0}}, {{x, x}}, {{0, x}}};
  73. EXPECT_THAT(quad(square), Eq(expected));
  74. }
  75. TEST_P(QuadTest, SquareProducesRectangleWithXX) {
  76. float side = std::get<0>(GetParam());
  77. square const square{{{0, 0}}, side};
  78. EXPECT_THAT(rectangle(square).origin, Eq(square.origin));
  79. EXPECT_THAT(rectangle(square).size[0], Eq(side));
  80. EXPECT_THAT(rectangle(square).size[0], Eq(side));
  81. }
  82. TEST_P(QuadTest, OffsetSquareProducesRectangleWithXX) {
  83. float side = std::get<0>(GetParam());
  84. square const square{{{1, 1}}, side};
  85. EXPECT_THAT(rectangle(square).origin, Eq(square.origin));
  86. EXPECT_THAT(rectangle(square).size[0], Eq(side));
  87. EXPECT_THAT(rectangle(square).size[0], Eq(side));
  88. }
  89. TEST_P(QuadTest, RectProducesQuadWithCornersAtXY) {
  90. float x = std::get<0>(GetParam());
  91. float y = std::get<1>(GetParam());
  92. rectangle const square{{{0, 0}}, {{x, y}}};
  93. quad const expected{{{0, 0}}, {{x, 0}}, {{x, y}}, {{0, y}}};
  94. EXPECT_THAT(quad(square), Eq(expected));
  95. }
  96. INSTANTIATE_TEST_CASE_P(Upcast, QuadTest,
  97. Combine(Values(0.5, 1.0, 1.5, 2.0),
  98. Values(0.5, 1.0, 1.5, 2.0)));