shape_test.cxx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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));