shape_test.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. namespace math { namespace vector {
  11. std::ostream & operator<<(std::ostream & os, math::vec2 const & p) {
  12. return os << '[' << p[0] << ',' << p[1] << ']';
  13. }
  14. } }
  15. namespace math { namespace dim2 {
  16. bool operator==(line const & lhs, line const & rhs) {
  17. return lhs.first == rhs.first && lhs.second == rhs.second;
  18. }
  19. std::ostream & operator<<(std::ostream & os, line const & l) {
  20. return os << '[' << l.first << ',' << l.second << ']';
  21. }
  22. } }
  23. using namespace math::dim2;
  24. struct FromOriginTest : testing::TestWithParam<line> {};
  25. TEST_P(FromOriginTest, IntersectsAtOrigin) {
  26. line l1 = { GetParam().first, {{0, 0}} };
  27. line l2 = { {{0, 0}}, GetParam().second };
  28. EXPECT_THAT(math::lines::intersection(l1, l2),
  29. testing::Eq(point{{0, 0}}));
  30. }
  31. std::vector<line> const point_pairs{
  32. {{{1, 1}}, {{0, 0}}}, // 0 length
  33. {{{1, 1}}, {{1, 0}}}, // -45deg
  34. {{{1, 1}}, {{0, 1}}}, // +45deg
  35. {{{1, 1}}, {{1, 1}}}, // +0deg
  36. {{{1, 1}}, {{2, 1}}}, // -18deg (approx)
  37. {{{1, 1}}, {{1, 2}}}, // +18deg (approx)
  38. {{{2, 3}}, {{1, 2}}}, //
  39. {{{1, 1}}, {{-1, 0}}}, // +135deg
  40. {{{1, 1}}, {{ 0, -1}}}, // -135deg
  41. {{{1, 1}}, {{-1, -1}}}, // +180deg
  42. {{{1, 1}}, {{ 1, -1}}}, // +90deg
  43. {{{1, 1}}, {{-1, 1}}}, // -90deg
  44. };
  45. INSTANTIATE_TEST_CASE_P(LineIntersection, FromOriginTest,
  46. testing::ValuesIn(point_pairs));
  47. struct XUnitTest : testing::TestWithParam<point> {};
  48. TEST_P(XUnitTest, OrthoOnIntersection) {
  49. line const ln{{{0, 0}}, {{1, 0}}};
  50. point const pt = GetParam();
  51. line const expected{pt, {{pt[0], 0}}};
  52. EXPECT_THAT(math::lines::orthogonal(ln, pt),
  53. testing::Eq(expected));
  54. }
  55. std::vector<point> x_orthos{
  56. {{0, 1}}, {{1, 1}}, {{1, 0}}, {{-1, 0}}, {{0, -1}}, {{-1, -1}},
  57. {{0, 2}}, {{2, 2}}, {{2, 0}}, {{-2, 0}}, {{0, -2}}, {{-2, -2}},
  58. {{2, 1}}, {{1, 2}}, {{-2, 1}}, {{-1, 2}}, {{1, -2}}, {{2, -1}}
  59. };
  60. INSTANTIATE_TEST_CASE_P(LineOrthogonal, XUnitTest,
  61. testing::ValuesIn(x_orthos));
  62. struct DiagonalTest : testing::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),
  68. testing::Eq(expected));
  69. }
  70. std::vector<std::pair<point, float>> diag_orthos{
  71. {{{0, 1}}, 0.5f}, {{{1, 1}}, 1.f}, {{{1, 0}}, 0.5f},
  72. {{{-1, 0}}, -0.5f}, {{{0, -1}}, -0.5f}, {{{-1, -1}}, -1.f},
  73. {{{0, 2}}, 1.f}, {{{2, 2}}, 2.f}, {{{2, 0}}, 1.f},
  74. {{{-2, 0}}, -1.f}, {{{0, -2}}, -1.f}, {{{-2, -2}}, -2.f},
  75. {{{2, 1}}, 1.5f}, {{{1, 2}}, 1.5f}, {{{-2, 1}}, -0.5f},
  76. {{{-1, 2}}, 0.5f}, {{{1, -2}}, -0.5f}, {{{2, -1}}, 0.5f}
  77. };
  78. INSTANTIATE_TEST_CASE_P(LineOrthogonal, DiagonalTest,
  79. testing::ValuesIn(diag_orthos));