shape_test.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. struct FromOriginTest : testing::TestWithParam<line> {};
  13. TEST_P(FromOriginTest, IntersectsAtOrigin) {
  14. line l1 = { GetParam().first, {{0, 0}} };
  15. line l2 = { {{0, 0}}, GetParam().second };
  16. EXPECT_THAT(math::lines::intersection(l1, l2),
  17. testing::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. testing::ValuesIn(point_pairs));
  35. struct XUnitTest : testing::TestWithParam<point> {};
  36. TEST_P(XUnitTest, 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),
  41. testing::Eq(expected));
  42. }
  43. std::vector<point> x_orthos{
  44. {{0, 1}}, {{1, 1}}, {{1, 0}}, {{-1, 0}}, {{0, -1}}, {{-1, -1}},
  45. {{0, 2}}, {{2, 2}}, {{2, 0}}, {{-2, 0}}, {{0, -2}}, {{-2, -2}},
  46. {{2, 1}}, {{1, 2}}, {{-2, 1}}, {{-1, 2}}, {{1, -2}}, {{2, -1}}
  47. };
  48. INSTANTIATE_TEST_CASE_P(LineOrthogonal, XUnitTest,
  49. testing::ValuesIn(x_orthos));
  50. struct DiagonalTest : testing::TestWithParam<std::pair<point, float>> {};
  51. TEST_P(DiagonalTest, OrthoOnIntersection) {
  52. line const ln{{{0, 0}}, {{1, 1}}};
  53. point const pt = GetParam().first;
  54. line const expected{pt, {{GetParam().second, GetParam().second}}};
  55. EXPECT_THAT(math::lines::orthogonal(ln, pt),
  56. testing::Eq(expected));
  57. }
  58. std::vector<std::pair<point, float>> diag_orthos{
  59. {{{0, 1}}, 0.5f}, {{{1, 1}}, 1.f}, {{{1, 0}}, 0.5f},
  60. {{{-1, 0}}, -0.5f}, {{{0, -1}}, -0.5f}, {{{-1, -1}}, -1.f},
  61. {{{0, 2}}, 1.f}, {{{2, 2}}, 2.f}, {{{2, 0}}, 1.f},
  62. {{{-2, 0}}, -1.f}, {{{0, -2}}, -1.f}, {{{-2, -2}}, -2.f},
  63. {{{2, 1}}, 1.5f}, {{{1, 2}}, 1.5f}, {{{-2, 1}}, -0.5f},
  64. {{{-1, 2}}, 0.5f}, {{{1, -2}}, -0.5f}, {{{2, -1}}, 0.5f}
  65. };
  66. INSTANTIATE_TEST_CASE_P(LineOrthogonal, DiagonalTest,
  67. testing::ValuesIn(diag_orthos));