فهرست منبع

Test shape constructors
- Fix bug in square -> rectangle

Sam Jaffe 6 سال پیش
والد
کامیت
303e56df32
3فایلهای تغییر یافته به همراه42 افزوده شده و 3 حذف شده
  1. 1 3
      math/src/shape.cpp
  2. 37 0
      math/test/shape_test.cxx
  3. 4 0
      math/test/test_printers.h

+ 1 - 3
math/src/shape.cpp

@@ -23,9 +23,7 @@ namespace math { namespace dim2 {
             origin + vec2{{0.0, size.y()}}};
   }
 
-  square::operator rectangle() const {
-    return {origin, origin + vec2{{size, size}}};
-  }
+  square::operator rectangle() const { return {origin, vec2{{size, size}}}; }
 
   square::operator quad() const {
     return {origin, origin + vec2{{size, 0.0}}, origin + vec2{{size, size}},

+ 37 - 0
math/test/shape_test.cxx

@@ -83,3 +83,40 @@ std::vector<std::pair<point, float>> diag_orthos{
     {{{-1, 2}}, 0.5f},  {{{1, -2}}, -0.5f}, {{{2, -1}}, 0.5f}};
 
 INSTANTIATE_TEST_CASE_P(LineOrthogonal, DiagonalTest, ValuesIn(diag_orthos));
+
+struct QuadTest : TestWithParam<std::tuple<float, float>> {};
+
+TEST_P(QuadTest, SquareProducesQuadWithCornersAtX) {
+  float x = std::get<0>(GetParam());
+  square const square{{{0, 0}}, x};
+  quad const expected{{{0, 0}}, {{x, 0}}, {{x, x}}, {{0, x}}};
+  EXPECT_THAT(quad(square), Eq(expected));
+}
+
+TEST_P(QuadTest, SquareProducesRectangleWithXX) {
+  float side = std::get<0>(GetParam());
+  square const square{{{0, 0}}, side};
+  EXPECT_THAT(rectangle(square).origin, Eq(square.origin));
+  EXPECT_THAT(rectangle(square).size[0], Eq(side));
+  EXPECT_THAT(rectangle(square).size[0], Eq(side));
+}
+
+TEST_P(QuadTest, OffsetSquareProducesRectangleWithXX) {
+  float side = std::get<0>(GetParam());
+  square const square{{{1, 1}}, side};
+  EXPECT_THAT(rectangle(square).origin, Eq(square.origin));
+  EXPECT_THAT(rectangle(square).size[0], Eq(side));
+  EXPECT_THAT(rectangle(square).size[0], Eq(side));
+}
+
+TEST_P(QuadTest, RectProducesQuadWithCornersAtXY) {
+  float x = std::get<0>(GetParam());
+  float y = std::get<1>(GetParam());
+  rectangle const square{{{0, 0}}, {{x, y}}};
+  quad const expected{{{0, 0}}, {{x, 0}}, {{x, y}}, {{0, y}}};
+  EXPECT_THAT(quad(square), Eq(expected));
+}
+
+INSTANTIATE_TEST_CASE_P(Upcast, QuadTest,
+                        Combine(Values(0.5, 1.0, 1.5, 2.0),
+                                Values(0.5, 1.0, 1.5, 2.0)));

+ 4 - 0
math/test/test_printers.h

@@ -21,6 +21,10 @@ namespace math { namespace dim2 {
   inline bool operator==(line const & lhs, line const & rhs) {
     return lhs.first == rhs.first && lhs.second == rhs.second;
   }
+  inline bool operator==(quad const & lhs, quad const & rhs) {
+    return lhs.ll == rhs.ll && lhs.lr == rhs.lr && lhs.ul == rhs.ul &&
+           lhs.ur == rhs.ur;
+  }
   inline std::ostream & operator<<(std::ostream & os, line const & l) {
     return os << '[' << l.first << ',' << l.second << ']';
   }