common_test.cxx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. std::vector<point> const line_ends{
  36. {{1, 1}}, {{0, 1}}, {{1, 0}}
  37. };
  38. INSTANTIATE_TEST_CASE_P(ContainsPoint, LineTest,
  39. Combine(ValuesIn(line_ends), ValuesIn(generate(0, 100, 10))));
  40. struct UnitCircleTest : TestWithParam<point> {};
  41. TEST_P(UnitCircleTest, ExistsInCircle) {
  42. circle unit{{{0, 0}}, 1};
  43. EXPECT_TRUE(math::contains(unit, GetParam()));
  44. }
  45. TEST_P(UnitCircleTest, OutsideSmallerCircle) {
  46. circle subunit{{{0, 0}}, 0.9999};
  47. EXPECT_FALSE(math::contains(subunit, GetParam()));
  48. }
  49. point unit_circle_angle(math::degree degs) {
  50. return point(make_vector(math::sin(degs), math::cos(degs)));
  51. }
  52. INSTANTIATE_TEST_CASE_P(LiesOnEdge, UnitCircleTest,
  53. ValuesIn(generate(0.0, 360.0, 5.0, unit_circle_angle)));