Browse Source

Add tests for point exists inside a circle.

Sam Jaffe 6 years ago
parent
commit
04c63f0a24

+ 6 - 0
math/math.xcodeproj/project.pbxproj

@@ -16,6 +16,7 @@
 		CDA34D9522517967008036A7 /* matrix_helpers.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CDA34D8C22517680008036A7 /* matrix_helpers.hpp */; };
 		CDA34D9622517969008036A7 /* matrix.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CDA34D8D22517680008036A7 /* matrix.hpp */; };
 		CDA34D972251796B008036A7 /* vector.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CDA34D9022517689008036A7 /* vector.hpp */; };
+		CDEDC5B9227F2D38003A2E45 /* common_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDEDC5B8227F2D38003A2E45 /* common_test.cxx */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -76,6 +77,8 @@
 		CDA34D8C22517680008036A7 /* matrix_helpers.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = matrix_helpers.hpp; path = matrix/matrix_helpers.hpp; sourceTree = SOURCE_ROOT; };
 		CDA34D8D22517680008036A7 /* matrix.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = matrix.hpp; path = matrix/matrix.hpp; sourceTree = SOURCE_ROOT; };
 		CDA34D9022517689008036A7 /* vector.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = vector.hpp; path = vector/vector.hpp; sourceTree = SOURCE_ROOT; };
+		CDEDC5B8227F2D38003A2E45 /* common_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = common_test.cxx; sourceTree = "<group>"; };
+		CDEDC5BD227F2DB2003A2E45 /* test_printers.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = test_printers.h; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -161,6 +164,8 @@
 			isa = PBXGroup;
 			children = (
 				CD1FCFC8227E193000F9BF93 /* shape_test.cxx */,
+				CDEDC5B8227F2D38003A2E45 /* common_test.cxx */,
+				CDEDC5BD227F2DB2003A2E45 /* test_printers.h */,
 			);
 			path = test;
 			sourceTree = "<group>";
@@ -337,6 +342,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDEDC5B9227F2D38003A2E45 /* common_test.cxx in Sources */,
 				CD1FCFD8227E195B00F9BF93 /* shape_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;

+ 51 - 0
math/test/common_test.cxx

@@ -0,0 +1,51 @@
+//
+//  common_test.cxx
+//  math-test
+//
+//  Created by Sam Jaffe on 5/5/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include <cmath>
+#include <gmock/gmock.h>
+
+#include "game/math/angle.hpp"
+#include "game/math/common.hpp"
+#include "game/math/shape.hpp"
+
+#include "test_printers.h"
+
+using namespace math::dim2;
+
+struct UnitCircleTest : testing::TestWithParam<point> {};
+
+TEST_P(UnitCircleTest, PointExistsInCircle) {
+  circle unit{{{0, 0}}, 1};
+  EXPECT_TRUE(math::contains(unit, GetParam()));
+}
+
+TEST_P(UnitCircleTest, PointExistsOutsideCircle) {
+  circle subunit{{{0, 0}}, 0.9999};
+  EXPECT_FALSE(math::contains(subunit, GetParam()));
+}
+
+struct unit_circle_angle {
+  using value_type = point;
+  unit_circle_angle(int degrees) : degs({static_cast<double>(degrees)}) {}
+  point operator*() const {
+    return point(make_vector(math::sin(degs), math::cos(degs)));
+  }
+  unit_circle_angle & operator++() { ++degs.value; return *this; }
+  math::degree degs;
+};
+
+std::vector<point> unit_circle_pts() {
+  std::vector<point> rval;
+  for (math::degree d{0.0}; d.value <= 360; d.value += 5.0) {
+    rval.emplace_back(point(make_vector(math::sin(d), math::cos(d))));
+  }
+  return rval;
+}
+
+INSTANTIATE_TEST_CASE_P(LiesOnEdge, UnitCircleTest,
+                        testing::ValuesIn(unit_circle_pts()));

+ 1 - 13
math/test/shape_test.cxx

@@ -10,19 +10,7 @@
 
 #include "game/math/shape.hpp"
 
-namespace math { namespace vector {
-  std::ostream & operator<<(std::ostream & os, math::vec2 const & p) {
-    return os << '[' << p[0] << ',' << p[1] << ']';
-  }
-} }
-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 << ']';
-  }
-} }
+#include "test_printers.h"
 
 using namespace math::dim2;
 

+ 29 - 0
math/test/test_printers.h

@@ -0,0 +1,29 @@
+//
+//  test_printers.h
+//  math-test
+//
+//  Created by Sam Jaffe on 5/5/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#ifndef test_printers_h
+#define test_printers_h
+#pragma once
+
+#include "game/math/shape.hpp"
+
+namespace math { namespace vector {
+  inline std::ostream & operator<<(std::ostream & os, math::vec2 const & p) {
+    return os << '[' << p[0] << ',' << p[1] << ']';
+  }
+} }
+namespace math { namespace dim2 {
+  inline bool operator==(line const & lhs, line const & rhs) {
+    return lhs.first == rhs.first && lhs.second == rhs.second;
+  }
+  inline std::ostream & operator<<(std::ostream & os, line const & l) {
+    return os << '[' << l.first << ',' << l.second << ']';
+  }
+} }
+
+#endif /* test_printers_h */