common_test.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // common_test.cxx
  3. // math-test
  4. //
  5. // Created by Sam Jaffe on 5/5/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include <cmath>
  9. #include <gmock/gmock.h>
  10. #include "game/math/angle.hpp"
  11. #include "game/math/common.hpp"
  12. #include "game/math/shape.hpp"
  13. #include "test_printers.h"
  14. using namespace math::dim2;
  15. using namespace testing;
  16. template <typename T, typename G>
  17. decltype(auto) generate(T min, T max, T step, G && generator) {
  18. std::vector<decltype(generator(min))> out;
  19. for (T val = min; val < max; val += step) {
  20. out.emplace_back(generator(val));
  21. }
  22. return out;
  23. }
  24. template <typename T> decltype(auto) generate(T min, T max, T step) {
  25. auto identity = [](T const & val) { return val; };
  26. return generate(min, max, step, identity);
  27. }
  28. struct LineTest : TestWithParam<std::tuple<point, int>> {};
  29. TEST_P(LineTest, ExistsOnOriginLine) {
  30. point const end = std::get<0>(GetParam());
  31. line const ln{{{0, 0}}, end};
  32. point const pt = end * std::get<1>(GetParam()) / 100.f;
  33. EXPECT_TRUE(math::contains(ln, pt));
  34. }
  35. TEST_P(LineTest, ColinearOutsideDoesNotContain) {
  36. point const end = std::get<0>(GetParam());
  37. line const ln{end, end * 2};
  38. point const pt = end * std::get<1>(GetParam()) / 100.f;
  39. EXPECT_FALSE(math::contains(ln, pt));
  40. }
  41. std::vector<point> const line_ends{{{1, 1}}, {{0, 1}}, {{1, 0}},
  42. {{-1, -1}}, {{0, -1}}, {{-1, 0}}};
  43. INSTANTIATE_TEST_CASE_P(Contains, LineTest,
  44. Combine(ValuesIn(line_ends),
  45. ValuesIn(generate(0, 100, 10))));
  46. struct UnitCircleTest : TestWithParam<point> {};
  47. TEST_P(UnitCircleTest, ExistsInCircle) {
  48. circle unit{{{0, 0}}, 1};
  49. EXPECT_TRUE(math::contains(unit, GetParam()));
  50. }
  51. TEST_P(UnitCircleTest, OutsideSmallerCircle) {
  52. circle subunit{{{0, 0}}, 0.9999};
  53. EXPECT_FALSE(math::contains(subunit, GetParam()));
  54. }
  55. point unit_circle_angle(math::degree degs) {
  56. return point(make_vector(math::sin(degs), math::cos(degs)));
  57. }
  58. INSTANTIATE_TEST_CASE_P(Contains, UnitCircleTest,
  59. ValuesIn(generate(0.0, 360.0, 5.0, unit_circle_angle)));
  60. struct UnitSquareTest : TestWithParam<std::tuple<float, float>> {};
  61. TEST_P(UnitSquareTest, PointInSquare) {
  62. square unit{{{0, 0}}, 1};
  63. point pt{{std::get<0>(GetParam()), std::get<1>(GetParam())}};
  64. EXPECT_TRUE(math::contains(unit, pt));
  65. }
  66. TEST_F(UnitSquareTest, PointOutsideSquare) {
  67. square unit{{{0, 0}}, 1};
  68. EXPECT_FALSE(math::contains(unit, {{0.f, 1.1f}}));
  69. EXPECT_FALSE(math::contains(unit, {{0.f, -0.1f}}));
  70. EXPECT_FALSE(math::contains(unit, {{-0.1f, 0.0f}}));
  71. EXPECT_FALSE(math::contains(unit, {{1.1f, 0.0f}}));
  72. }
  73. INSTANTIATE_TEST_CASE_P(Contains, UnitSquareTest,
  74. Combine(ValuesIn(generate(0.f, 1.f, 0.25f)),
  75. ValuesIn(generate(0.f, 1.f, 0.25f))));
  76. struct LineQuadTest : TestWithParam<point> {};
  77. TEST_P(LineQuadTest, OriginLineIntersectsUnitSquare) {
  78. square const unit{{{0, 0}}, 1};
  79. line const ln{{{0, 0}}, GetParam()};
  80. EXPECT_TRUE(math::intersects(ln, unit));
  81. EXPECT_TRUE(math::intersects(unit, ln));
  82. }
  83. TEST_P(LineQuadTest, CrossingLineIntersectsUnitSquare) {
  84. square const unit{{{0, 0}}, 1};
  85. line const ln{{{0.5, 0.5}}, GetParam()};
  86. EXPECT_TRUE(math::intersects(ln, unit));
  87. EXPECT_TRUE(math::intersects(unit, ln));
  88. }
  89. TEST_P(LineQuadTest, CrossingLineIntersectsSquare) {
  90. square const unit{{{0, 0}}, 0.9};
  91. line const ln{{{0.5, 0.5}}, GetParam()};
  92. EXPECT_TRUE(math::intersects(ln, unit));
  93. EXPECT_TRUE(math::intersects(unit, ln));
  94. }
  95. TEST_F(LineQuadTest, JustPointIntersection) {
  96. square const unit{{{0, 0}}, 1};
  97. line const ln{{{1, 1}}, {{2, 1}}};
  98. EXPECT_TRUE(math::intersects(ln, unit));
  99. EXPECT_TRUE(math::intersects(unit, ln));
  100. }
  101. TEST_F(LineQuadTest, EntirelyEncasedIntersection) {
  102. square const unit{{{0, 0}}, 1};
  103. line const ln{{{0.5, 0.5}}, {{0.75, 0.75}}};
  104. EXPECT_TRUE(math::intersects(ln, unit));
  105. EXPECT_TRUE(math::intersects(unit, ln));
  106. }
  107. TEST_F(LineQuadTest, OutsideByAnInch) {
  108. square const unit{{{0, 0}}, 1};
  109. line const ln{{{1.001, 1}}, {{2, 1}}};
  110. math::intersects(ln, unit);
  111. EXPECT_FALSE(math::intersects(ln, unit));
  112. EXPECT_FALSE(math::intersects(unit, ln));
  113. }
  114. INSTANTIATE_TEST_CASE_P(Intersects, LineQuadTest, ValuesIn(line_ends));