浏览代码

Add tests for angles.

Sam Jaffe 6 年之前
父节点
当前提交
873f42d331
共有 2 个文件被更改,包括 86 次插入0 次删除
  1. 4 0
      math/math.xcodeproj/project.pbxproj
  2. 82 0
      math/test/angle_test.cxx

+ 4 - 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 */; };
+		CDED9C2422A2D71600AE5CE5 /* angle_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDED9C2322A2D71600AE5CE5 /* angle_test.cxx */; };
 		CDEDC5B9227F2D38003A2E45 /* common_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDEDC5B8227F2D38003A2E45 /* common_test.cxx */; };
 /* End PBXBuildFile section */
 
@@ -77,6 +78,7 @@
 		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; };
+		CDED9C2322A2D71600AE5CE5 /* angle_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = angle_test.cxx; sourceTree = "<group>"; };
 		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 */
@@ -166,6 +168,7 @@
 				CD1FCFC8227E193000F9BF93 /* shape_test.cxx */,
 				CDEDC5B8227F2D38003A2E45 /* common_test.cxx */,
 				CDEDC5BD227F2DB2003A2E45 /* test_printers.h */,
+				CDED9C2322A2D71600AE5CE5 /* angle_test.cxx */,
 			);
 			path = test;
 			sourceTree = "<group>";
@@ -344,6 +347,7 @@
 			files = (
 				CDEDC5B9227F2D38003A2E45 /* common_test.cxx in Sources */,
 				CD1FCFD8227E195B00F9BF93 /* shape_test.cxx in Sources */,
+				CDED9C2422A2D71600AE5CE5 /* angle_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 82 - 0
math/test/angle_test.cxx

@@ -0,0 +1,82 @@
+//
+//  angle_test.cxx
+//  math-test
+//
+//  Created by Sam Jaffe on 6/1/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "game/math/angle.hpp"
+#include <gmock/gmock.h>
+
+using testing::DoubleNear;
+using testing::Eq;
+
+// Degree -> Radian
+TEST(RadianTest, ZeroDegreeIsZeroRadian) {
+  math::degree degrees{0};
+  EXPECT_THAT(math::radian{degrees}.value, Eq(0.0));
+}
+
+TEST(RadianTest, OneRadianIsAbout57Degrees) {
+  math::degree degrees{57.2957795131};
+  EXPECT_THAT(math::radian{degrees}.value, DoubleNear(1, 1E-10));
+}
+
+TEST(RadianTest, OneEightyDegreeIsPiRadians) {
+  math::degree degrees{180};
+  EXPECT_THAT(math::radian{degrees}.value, DoubleNear(M_PI, 1E-12));
+}
+
+TEST(RadianTest, NinetyDegreeIsHalfPiRadians) {
+  math::degree degrees{90};
+  EXPECT_THAT(math::radian{degrees}.value, DoubleNear(M_PI_2, 1E-12));
+}
+
+// Radian -> Degree
+TEST(DegreeTest, ZeroDegreeIsZeroRadian) {
+  math::radian radians{0};
+  EXPECT_THAT(math::degree{radians}.value, Eq(0.0));
+}
+
+TEST(DegreeTest, OneRadianIsAbout57Degrees) {
+  math::radian radians{1.0};
+  // 1E-10 because that's how many digits I wrote down
+  EXPECT_THAT(math::degree{radians}.value, DoubleNear(57.2957795131, 1E-10));
+}
+
+TEST(DegreeTest, OneEightyDegreeIsPiRadians) {
+  math::radian radians{M_PI};
+  EXPECT_THAT(math::degree{radians}.value, DoubleNear(180.0, 1E-12));
+}
+
+TEST(DegreeTest, NinetyDegreeIsHalfPiRadians) {
+  math::radian radians{M_PI_2};
+  EXPECT_THAT(math::degree{radians}.value, DoubleNear(90, 1E-12));
+}
+
+// Trigonometry
+struct TrigonometryTest : testing::TestWithParam<double> {};
+
+TEST_P(TrigonometryTest, SinRadianIsSinDouble) {
+  double const angle = GetParam();
+  math::radian rad{angle};
+  EXPECT_THAT(sin(rad), sin(angle));
+}
+
+TEST_P(TrigonometryTest, CosRadianIsCosDouble) {
+  double const angle = GetParam();
+  math::radian rad{angle};
+  EXPECT_THAT(cos(rad), cos(angle));
+}
+
+TEST_P(TrigonometryTest, TanRadianIsTanDouble) {
+  double const angle = GetParam();
+  math::radian rad{angle};
+  EXPECT_THAT(tan(rad), tan(angle));
+}
+
+using testing::Values;
+INSTANTIATE_TEST_CASE_P(MapsToRawValue, TrigonometryTest,
+                        Values(0.0, M_PI_4, M_PI_2, 3 * M_PI_4, M_PI,
+                               5 * M_PI_4, 3 * M_PI_2, 7 * M_PI_4, 2 * M_PI));