summaryrefslogtreecommitdiffstats
path: root/src/math/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/math/test')
-rw-r--r--src/math/test/CMakeLists.txt40
-rw-r--r--src/math/test/geometry_test.cpp84
-rw-r--r--src/math/test/matrix_test.cpp124
-rw-r--r--src/math/test/vector_test.cpp76
4 files changed, 73 insertions, 251 deletions
diff --git a/src/math/test/CMakeLists.txt b/src/math/test/CMakeLists.txt
index c736022..87121a0 100644
--- a/src/math/test/CMakeLists.txt
+++ b/src/math/test/CMakeLists.txt
@@ -1,33 +1,23 @@
cmake_minimum_required(VERSION 2.8)
set(CMAKE_BUILD_TYPE debug)
-set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0")
+set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wold-style-cast -std=gnu++0x")
-add_executable(matrix_test matrix_test.cpp)
-add_executable(vector_test vector_test.cpp)
-add_executable(geometry_test geometry_test.cpp ../old/math3d.cpp ../old/d3dmath.cpp ../../graphics/d3d/d3dutil.cpp)
-
-enable_testing()
-
-add_test(matrix_test ./matrix_test)
-add_test(vector_test ./vector_test)
-add_test(geometry_test ./geometry_test)
-
-# Change to DirectX SDK directory
-include_directories("c:/dxsdk/include")
+include_directories(
+.
+../../..
+${GTEST_DIR}/include
+)
-add_definitions(-DSTRICT -DD3D_OVERLOADS)
+add_executable(matrix_test matrix_test.cpp)
+target_link_libraries(matrix_test gtest)
-# 'make check' will compile the required test programs
-# Note that 'make test' will still fail without compiled programs
-add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS matrix_test vector_test)
+add_executable(vector_test vector_test.cpp)
+target_link_libraries(vector_test gtest)
-# Files to be removed in distclean
-set(REMOVE_FILES
- CMakeFiles Testing cmake_install.cmake CMakeCache.txt CTestTestfile.cmake Makefile
- ./matrix_test
- ./vector_test
- ./geometry_test
-)
+add_executable(geometry_test geometry_test.cpp)
+target_link_libraries(geometry_test gtest)
-add_custom_target(distclean COMMAND rm -rf ${REMOVE_FILES})
+add_test(matrix_test matrix_test)
+add_test(vector_test vector_test)
+add_test(geometry_test geometry_test)
diff --git a/src/math/test/geometry_test.cpp b/src/math/test/geometry_test.cpp
index 07fa2cf..8b83b8d 100644
--- a/src/math/test/geometry_test.cpp
+++ b/src/math/test/geometry_test.cpp
@@ -20,53 +20,41 @@
#include "../func.h"
#include "../geometry.h"
-#include "../conv.h"
-#include "../../old/math3d.h"
-#include "../../old/d3dutil.h"
-#include <d3d.h>
-#include <cstdio>
+#include "gtest/gtest.h"
-using namespace std;
const float TEST_TOLERANCE = 1e-5;
// Test for rewritten function RotateAngle()
-int TestRotateAngle()
+TEST(GeometryTest, RotateAngleTest)
{
- if (! Math::IsEqual(Math::RotateAngle(0.0f, 0.0f), 0.0f, TEST_TOLERANCE))
- return __LINE__;
+ EXPECT_TRUE(Math::IsEqual(Math::RotateAngle(0.0f, 0.0f), 0.0f, TEST_TOLERANCE));
- if (! Math::IsEqual(Math::RotateAngle(1.0f, 0.0f), 0.0f, TEST_TOLERANCE))
- return __LINE__;
+ EXPECT_TRUE(Math::IsEqual(Math::RotateAngle(1.0f, 0.0f), 0.0f, TEST_TOLERANCE));
- if (! Math::IsEqual(Math::RotateAngle(1.0f, 1.0f), 0.25f * Math::PI, TEST_TOLERANCE))
- return __LINE__;
+ EXPECT_TRUE(Math::IsEqual(Math::RotateAngle(1.0f, 1.0f), 0.25f * Math::PI, TEST_TOLERANCE));
- if (! Math::IsEqual(Math::RotateAngle(0.0f, 2.0f), 0.5f * Math::PI, TEST_TOLERANCE))
- return __LINE__;
+ EXPECT_TRUE(Math::IsEqual(Math::RotateAngle(0.0f, 2.0f), 0.5f * Math::PI, TEST_TOLERANCE));
- if (! Math::IsEqual(Math::RotateAngle(-0.5f, 0.5f), 0.75f * Math::PI, TEST_TOLERANCE))
- return __LINE__;
+ EXPECT_TRUE(Math::IsEqual(Math::RotateAngle(-0.5f, 0.5f), 0.75f * Math::PI, TEST_TOLERANCE));
- if (! Math::IsEqual(Math::RotateAngle(-1.0f, 0.0f), Math::PI, TEST_TOLERANCE))
- return __LINE__;
+ EXPECT_TRUE(Math::IsEqual(Math::RotateAngle(-1.0f, 0.0f), Math::PI, TEST_TOLERANCE));
- if (! Math::IsEqual(Math::RotateAngle(-1.0f, -1.0f), 1.25f * Math::PI, TEST_TOLERANCE))
- return __LINE__;
+ EXPECT_TRUE(Math::IsEqual(Math::RotateAngle(-1.0f, -1.0f), 1.25f * Math::PI, TEST_TOLERANCE));
- if (! Math::IsEqual(Math::RotateAngle(0.0f, -2.0f), 1.5f * Math::PI, TEST_TOLERANCE))
- return __LINE__;
+ EXPECT_TRUE(Math::IsEqual(Math::RotateAngle(0.0f, -2.0f), 1.5f * Math::PI, TEST_TOLERANCE));
- if (! Math::IsEqual(Math::RotateAngle(1.0f, -1.0f), 1.75f * Math::PI, TEST_TOLERANCE))
- return __LINE__;
-
- return 0;
+ EXPECT_TRUE(Math::IsEqual(Math::RotateAngle(1.0f, -1.0f), 1.75f * Math::PI, TEST_TOLERANCE));
}
// Tests for other altered, complex or uncertain functions
+/*
+
+ TODO: write meaningful tests with proper test values
+
int TestAngle()
{
const Math::Vector u(-0.0786076246943884, 0.2231249091714256, -1.1601361718477805);
@@ -360,42 +348,12 @@ int TestTransform()
return 0;
}
-int main()
-{
- // Functions to test
- int (*TESTS[])() =
- {
- TestRotateAngle,
- TestAngle,
- TestRotateView,
- TestLookatPoint,
- TestProjection,
- TestLoadViewMatrix,
- TestLoadProjectionMatrix,
- TestLoadTranslationMatrix,
- TestLoadScaleMatrix,
- TestLoadRotationXMatrix,
- TestLoadRotationYMatrix,
- TestLoadRotationZMatrix,
- TestLoadRotationMatrix,
- TestLoadRotationXZYMatrix,
- TestLoadRotationZXYMatrix,
- TestTransform
- };
- const int TESTS_SIZE = sizeof(TESTS) / sizeof(*TESTS);
-
- int result = 0;
- for (int i = 0; i < TESTS_SIZE; ++i)
- {
- result = TESTS[i]();
- if (result != 0)
- {
- fprintf(stderr, "Test function %d failed at line %d\n", i+1, result);
- return result;
- }
- }
+*/
- fprintf(stderr, "All tests successful\n");
- return 0;
+int main(int argc, char* argv[])
+{
+ ::testing::InitGoogleTest(&argc, argv);
+
+ return RUN_ALL_TESTS();
}
diff --git a/src/math/test/matrix_test.cpp b/src/math/test/matrix_test.cpp
index 663234c..867e0ec 100644
--- a/src/math/test/matrix_test.cpp
+++ b/src/math/test/matrix_test.cpp
@@ -26,13 +26,13 @@
#include "../func.h"
#include "../matrix.h"
-#include <cstdio>
+#include "gtest/gtest.h"
-using namespace std;
const float TEST_TOLERANCE = 1e-6;
-int TestTranspose()
+
+TEST(MatrixTest, TransposeTest)
{
const Math::Matrix mat(
(float[4][4])
@@ -56,16 +56,10 @@ int TestTranspose()
Math::Matrix transpose = Math::Transpose(mat);
- if (! Math::MatricesEqual(transpose, expectedTranspose, TEST_TOLERANCE))
- {
- fprintf(stderr, "Transpose mismatch!\n");
- return __LINE__;
- }
-
- return 0;
+ EXPECT_TRUE(Math::MatricesEqual(transpose, expectedTranspose, TEST_TOLERANCE));
}
-int TestCofactor()
+TEST(MatrixTest, CofactorTest)
{
const Math::Matrix mat1(
(float[4][4])
@@ -93,12 +87,7 @@ int TestCofactor()
{
float ret = mat1.Cofactor(r, c);
float exp = expectedCofactors1.m[4*c+r];
- if (! Math::IsEqual(ret, exp, TEST_TOLERANCE))
- {
- fprintf(stderr, "Cofactors 1 mismatch!\n");
- fprintf(stderr, "r=%d, c=%d, %f (returned) != %f (expected)\n", r, c, ret, exp);
- return __LINE__;
- }
+ EXPECT_TRUE(Math::IsEqual(ret, exp, TEST_TOLERANCE));
}
}
@@ -129,19 +118,12 @@ int TestCofactor()
{
float ret = mat2.Cofactor(r, c);
float exp = expectedCofactors2.m[4*c+r];
- if (! Math::IsEqual(ret, exp, TEST_TOLERANCE))
- {
- fprintf(stderr, "Cofactors 2 mismatch!\n");
- fprintf(stderr, "r=%d, c=%d, %f (returned) != %f (expected)\n", r, c, ret, exp);
- return __LINE__;
- }
+ EXPECT_TRUE(Math::IsEqual(ret, exp, TEST_TOLERANCE));
}
}
-
- return 0;
}
-int TestDet()
+TEST(MatrixTest, DetTest)
{
const Math::Matrix mat1(
(float[4][4])
@@ -156,12 +138,7 @@ int TestDet()
const float expectedDet1 = 4.07415413729671;
float ret1 = mat1.Det();
- if (! Math::IsEqual(ret1, expectedDet1, TEST_TOLERANCE))
- {
- fprintf(stderr, "Det mismatch!\n");
- fprintf(stderr, "%f (returned) != %f (expected)\n", ret1, expectedDet1);
- return __LINE__;
- }
+ EXPECT_TRUE(Math::IsEqual(ret1, expectedDet1, TEST_TOLERANCE));
const Math::Matrix mat2(
(float[4][4])
@@ -176,17 +153,10 @@ int TestDet()
const float expectedDet2 = -6.35122307880942;
float ret2 = mat2.Det();
- if (! Math::IsEqual(ret2, expectedDet2, TEST_TOLERANCE))
- {
- fprintf(stderr, "Det mismatch!\n");
- fprintf(stderr, "%f (returned) != %f (expected)\n", ret2, expectedDet2);
- return __LINE__;
- }
-
- return 0;
+ EXPECT_TRUE(Math::IsEqual(ret2, expectedDet2, TEST_TOLERANCE));
}
-int TestInverse()
+TEST(MatrixTest, InverseTest)
{
const Math::Matrix mat1(
(float[4][4])
@@ -210,11 +180,7 @@ int TestInverse()
Math::Matrix inverse1 = mat1.Inverse();
- if (! Math::MatricesEqual(inverse1, expectedInverse1, TEST_TOLERANCE))
- {
- fprintf(stderr, "Inverse 1 mismatch!\n");
- return __LINE__;
- }
+ EXPECT_TRUE(Math::MatricesEqual(inverse1, expectedInverse1, TEST_TOLERANCE));
const Math::Matrix mat2(
(float[4][4])
@@ -238,16 +204,10 @@ int TestInverse()
Math::Matrix inverse2 = mat2.Inverse();
- if (! Math::MatricesEqual(inverse2, expectedInverse2, TEST_TOLERANCE))
- {
- fprintf(stderr, "Inverse 2 mismatch!\n");
- return __LINE__;
- }
-
- return 0;
+ EXPECT_TRUE(Math::MatricesEqual(inverse2, expectedInverse2, TEST_TOLERANCE));
}
-int TestMultiply()
+TEST(MatrixTest, MultiplyTest)
{
const Math::Matrix mat1A(
(float[4][4])
@@ -280,11 +240,7 @@ int TestMultiply()
);
Math::Matrix multiply1 = Math::MultiplyMatrices(mat1A, mat1B);
- if (! Math::MatricesEqual(multiply1, expectedMultiply1, TEST_TOLERANCE ) )
- {
- fprintf(stderr, "Multiply 1 mismath!\n");
- return __LINE__;
- }
+ EXPECT_TRUE(Math::MatricesEqual(multiply1, expectedMultiply1, TEST_TOLERANCE));
const Math::Matrix mat2A(
(float[4][4])
@@ -317,16 +273,10 @@ int TestMultiply()
);
Math::Matrix multiply2 = Math::MultiplyMatrices(mat2A, mat2B);
- if (! Math::MatricesEqual(multiply2, expectedMultiply2, TEST_TOLERANCE ) )
- {
- fprintf(stderr, "Multiply 2 mismath!\n");
- return __LINE__;
- }
-
- return 0;
+ EXPECT_TRUE(Math::MatricesEqual(multiply2, expectedMultiply2, TEST_TOLERANCE));
}
-int TestMultiplyVector()
+TEST(MatrixTest, MultiplyVectorTest)
{
const Math::Matrix mat1(
(float[4][4])
@@ -343,11 +293,7 @@ int TestMultiplyVector()
const Math::Vector expectedMultiply1(0.608932463260470, -1.356893266403749, 3.457156276255142);
Math::Vector multiply1 = Math::MatrixVectorMultiply(mat1, vec1, false);
- if (! Math::VectorsEqual(multiply1, expectedMultiply1, TEST_TOLERANCE ) )
- {
- fprintf(stderr, "Multiply vector 1 mismath!\n");
- return __LINE__;
- }
+ EXPECT_TRUE(Math::VectorsEqual(multiply1, expectedMultiply1, TEST_TOLERANCE));
const Math::Matrix mat2(
(float[4][4])
@@ -364,39 +310,13 @@ int TestMultiplyVector()
const Math::Vector expectedMultiply2(0.2816820577317669, 0.0334468811767428, 0.1996974284970455);
Math::Vector multiply2 = Math::MatrixVectorMultiply(mat2, vec2, true);
- if (! Math::VectorsEqual(multiply2, expectedMultiply2, TEST_TOLERANCE ) )
- {
- fprintf(stderr, "Multiply vector 2 mismath!\n");
- return __LINE__;
- }
-
- return 0;
+ EXPECT_TRUE(Math::VectorsEqual(multiply2, expectedMultiply2, TEST_TOLERANCE));
}
-int main()
+int main(int argc, char* argv[])
{
- // Functions to test
- int (*TESTS[])() =
- {
- TestTranspose,
- TestCofactor,
- TestDet,
- TestInverse,
- TestMultiply,
- TestMultiplyVector
- };
- const int TESTS_SIZE = sizeof(TESTS) / sizeof(*TESTS);
-
- int result = 0;
- for (int i = 0; i < TESTS_SIZE; ++i)
- {
- result = TESTS[i]();
- if (result != 0)
- return result;
- }
-
- fprintf(stderr, "All tests successful\n");
+ ::testing::InitGoogleTest(&argc, argv);
- return 0;
+ return RUN_ALL_TESTS();
}
diff --git a/src/math/test/vector_test.cpp b/src/math/test/vector_test.cpp
index 899a580..ead2fd2 100644
--- a/src/math/test/vector_test.cpp
+++ b/src/math/test/vector_test.cpp
@@ -26,59 +26,41 @@
#include "../func.h"
#include "../vector.h"
-#include <cstdio>
+#include "gtest/gtest.h"
-using namespace std;
const float TEST_TOLERANCE = 1e-6;
-int TestLength()
+
+TEST(VectorTest, LengthTest)
{
Math::Vector vec(-1.288447945923275, 0.681452565308134, -0.633761098985957);
const float expectedLength = 1.58938001708428;
- if (! Math::IsEqual(vec.Length(), expectedLength, TEST_TOLERANCE) )
- {
- fprintf(stderr, "Length mismatch!\n");
- return __LINE__;
- }
-
- return 0;
+ EXPECT_TRUE(Math::IsEqual(vec.Length(), expectedLength, TEST_TOLERANCE));
}
-int TestNormalize()
+TEST(VectorTest, NormalizeTest)
{
Math::Vector vec(1.848877241804398, -0.157262961268577, -1.963031403332377);
const Math::Vector expectedNormalized(0.6844609421393856, -0.0582193085618106, -0.7267212194481797);
vec.Normalize();
- if (! Math::VectorsEqual(vec, expectedNormalized, TEST_TOLERANCE))
- {
- fprintf(stderr, "Normalize mismatch!\n");
- return __LINE__;
- }
-
- return 0;
+ EXPECT_TRUE(Math::VectorsEqual(vec, expectedNormalized, TEST_TOLERANCE));
}
-int TestDot()
+TEST(VectorTest, DotTest)
{
Math::Vector vecA(0.8202190530968309, 0.0130926060162780, 0.2411914183883510);
Math::Vector vecB(-0.0524083951404069, 1.5564932716738220, -0.8971342631500536);
float expectedDot = -0.238988896477326;
- if (! Math::IsEqual(Math::DotProduct(vecA, vecB), expectedDot, TEST_TOLERANCE) )
- {
- fprintf(stderr, "Dot product mismatch!\n");
- return __LINE__;
- }
-
- return 0;
+ EXPECT_TRUE(Math::IsEqual(Math::DotProduct(vecA, vecB), expectedDot, TEST_TOLERANCE));
}
-int TestCross()
+TEST(VectorTest, CrossTest)
{
Math::Vector vecA(1.37380499798567, 1.18054518384682, 1.95166361293121);
Math::Vector vecB(0.891657855926886, 0.447591335394532, -0.901604070087823);
@@ -86,42 +68,14 @@ int TestCross()
Math::Vector expectedCross(-1.937932065431669, 2.978844370287636, -0.437739173833581);
Math::Vector expectedReverseCross = -expectedCross;
- if (! Math::VectorsEqual(vecA.CrossMultiply(vecB), expectedCross, TEST_TOLERANCE) )
- {
- fprintf(stderr, "Cross product mismatch!\n");
- return __LINE__;
- }
-
- if (! Math::VectorsEqual(vecB.CrossMultiply(vecA), expectedReverseCross, TEST_TOLERANCE) )
- {
- fprintf(stderr, "Reverse cross product mismatch!\n");
- return __LINE__;
- }
+ EXPECT_TRUE(Math::VectorsEqual(vecA.CrossMultiply(vecB), expectedCross, TEST_TOLERANCE));
- return 0;
+ EXPECT_TRUE(Math::VectorsEqual(vecB.CrossMultiply(vecA), expectedReverseCross, TEST_TOLERANCE));
}
-int main()
+int main(int argc, char* argv[])
{
- // Functions to test
- int (*TESTS[])() =
- {
- TestLength,
- TestNormalize,
- TestDot,
- TestCross
- };
- const int TESTS_SIZE = sizeof(TESTS) / sizeof(*TESTS);
-
- int result = 0;
- for (int i = 0; i < TESTS_SIZE; ++i)
- {
- result = TESTS[i]();
- if (result != 0)
- return result;
- }
-
- fprintf(stderr, "All tests successful\n");
-
- return 0;
+ ::testing::InitGoogleTest(&argc, argv);
+
+ return RUN_ALL_TESTS();
}