فهرست منبع

Start moving towards 100% coverage of math
- line::length
- lines::parallel
- intersects(triangle, triangle)

Sam Jaffe 6 سال پیش
والد
کامیت
86194d80d4
2فایلهای تغییر یافته به همراه33 افزوده شده و 0 حذف شده
  1. 21 0
      math/test/common_test.cxx
  2. 12 0
      math/test/shape_test.cxx

+ 21 - 0
math/test/common_test.cxx

@@ -18,6 +18,10 @@
 using namespace math::dim2;
 using namespace testing;
 
+namespace math {
+  bool intersects(dim2::triangle const &, dim2::triangle const &);
+}
+
 template <typename T, typename G>
 decltype(auto) generate(T min, T max, T step, G && generator) {
   std::vector<decltype(generator(min))> out;
@@ -243,6 +247,23 @@ TEST(CircleTest, NonIntersecting) {
   EXPECT_FALSE(math::intersects(c1, sq));
 }
 
+TEST(TriangleTest, TriangleIntersectsSelf) {
+  triangle tri{{{0, 0}}, {{0, 1}}, {{1, 0}}};
+  EXPECT_TRUE(math::intersects(tri, tri));
+}
+
+TEST(TriangleTest, DoesNotIntersectOffset) {
+  triangle lhs{{{0, 0}}, {{0, 1}}, {{1, 0}}};
+  triangle rhs{{{-1, -1}}, {{-1, 0}}, {{0, -1}}};
+  EXPECT_FALSE(math::intersects(lhs, rhs));
+}
+
+TEST(TriangleTest, DoesNotIntersectOffset2) {
+  triangle lhs{{{0, 0}}, {{0, 1}}, {{1, 0}}};
+  triangle rhs{{{1, 1}}, {{1, 2}}, {{2, 1}}};
+  EXPECT_FALSE(math::intersects(lhs, rhs));
+}
+
 TEST(RotateTest, RotatingSquareAroundOrigin) {
   math::degree degrees{90.0};
   // A square with a side-length of 2 and a center at the origin

+ 12 - 0
math/test/shape_test.cxx

@@ -44,6 +44,18 @@ INSTANTIATE_TEST_CASE_P(LineIntersection, FromOriginTest,
 
 struct UnitLineTest : TestWithParam<point> {};
 
+TEST(LineTest, UnitLineHasLengthOne) {
+  line const unit{{{0, 0}}, {{1, 0}}};
+  EXPECT_THAT(unit.length(), Eq(1));
+}
+
+
+TEST(LineTest, ParallelLinesHaveSameSlope) {
+  line const lhs{{{0, 0}}, {{1, 0}}};
+  line const rhs{{{-1, 0}}, {{-2, 0}}};
+  EXPECT_TRUE(math::lines::parallel(lhs, rhs));
+}
+
 TEST_P(UnitLineTest, OrthoOnIntersection) {
   line const ln{{{0, 0}}, {{1, 0}}};
   point const pt = GetParam();