Преглед изворни кода

Add tests for orthogonal(line, point)

Sam Jaffe пре 6 година
родитељ
комит
3e4c6fb3c9
1 измењених фајлова са 51 додато и 5 уклоњено
  1. 51 5
      math/test/shape_test.cxx

+ 51 - 5
math/test/shape_test.cxx

@@ -16,22 +16,27 @@ namespace math { namespace vector {
   }
 } }
 namespace math { namespace dim2 {
+  bool operator==(line const & lhs, line const & rhs) {
+    return lhs.first == rhs.first && lhs.second == rhs.second;
+  }
   std::ostream & operator<<(std::ostream & os, line const & l) {
     return os << '[' << l.first << ',' << l.second << ']';
   }
 } }
 
-struct FromOriginTest : testing::TestWithParam<math::dim2::line> {};
+using namespace math::dim2;
+
+struct FromOriginTest : testing::TestWithParam<line> {};
 
 TEST_P(FromOriginTest, IntersectsAtOrigin) {
-  math::dim2::line l1 = { GetParam().first, {{0, 0}} };
-  math::dim2::line l2 = { {{0, 0}}, GetParam().second };
+  line l1 = { GetParam().first, {{0, 0}} };
+  line l2 = { {{0, 0}}, GetParam().second };
   
   EXPECT_THAT(math::lines::intersection(l1, l2),
-              testing::Eq(math::dim2::point{{0, 0}}));
+              testing::Eq(point{{0, 0}}));
 }
 
-std::vector<math::dim2::line> const point_pairs{
+std::vector<line> const point_pairs{
   {{{1, 1}}, {{0, 0}}},   // 0 length
   {{{1, 1}}, {{1, 0}}},   // -45deg
   {{{1, 1}}, {{0, 1}}},   // +45deg
@@ -48,3 +53,44 @@ std::vector<math::dim2::line> const point_pairs{
 
 INSTANTIATE_TEST_CASE_P(LineIntersection, FromOriginTest,
                         testing::ValuesIn(point_pairs));
+
+struct XUnitTest : testing::TestWithParam<point> {};
+
+TEST_P(XUnitTest, OrthoOnIntersection) {
+  line const ln{{{0, 0}}, {{1, 0}}};
+  point const pt = GetParam();
+  line const expected{pt, {{pt[0], 0}}};
+  EXPECT_THAT(math::lines::orthogonal(ln, pt),
+              testing::Eq(expected));
+}
+
+std::vector<point> x_orthos{
+  {{0, 1}}, {{1, 1}}, {{1, 0}}, {{-1, 0}}, {{0, -1}}, {{-1, -1}},
+  {{0, 2}}, {{2, 2}}, {{2, 0}}, {{-2, 0}}, {{0, -2}}, {{-2, -2}},
+  {{2, 1}}, {{1, 2}}, {{-2, 1}}, {{-1, 2}}, {{1, -2}}, {{2, -1}}
+};
+
+INSTANTIATE_TEST_CASE_P(LineOrthogonal, XUnitTest,
+                        testing::ValuesIn(x_orthos));
+
+struct DiagonalTest : testing::TestWithParam<std::pair<point, float>> {};
+
+TEST_P(DiagonalTest, OrthoOnIntersection) {
+  line const ln{{{0, 0}}, {{1, 1}}};
+  point const pt = GetParam().first;
+  line const expected{pt, {{GetParam().second, GetParam().second}}};
+  EXPECT_THAT(math::lines::orthogonal(ln, pt),
+              testing::Eq(expected));
+}
+
+std::vector<std::pair<point, float>> diag_orthos{
+  {{{0, 1}}, 0.5f}, {{{1, 1}}, 1.f}, {{{1, 0}}, 0.5f},
+  {{{-1, 0}}, -0.5f}, {{{0, -1}}, -0.5f}, {{{-1, -1}}, -1.f},
+  {{{0, 2}}, 1.f}, {{{2, 2}}, 2.f}, {{{2, 0}}, 1.f},
+  {{{-2, 0}}, -1.f}, {{{0, -2}}, -1.f}, {{{-2, -2}}, -2.f},
+  {{{2, 1}}, 1.5f}, {{{1, 2}}, 1.5f}, {{{-2, 1}}, -0.5f},
+  {{{-1, 2}}, 0.5f}, {{{1, -2}}, -0.5f}, {{{2, -1}}, 0.5f}
+};
+
+INSTANTIATE_TEST_CASE_P(LineOrthogonal, DiagonalTest,
+                        testing::ValuesIn(diag_orthos));