diff --git a/sources/reactphysics3d/testing/mathematics/Matrix3x3Test.h b/sources/reactphysics3d/testing/mathematics/Matrix3x3Test.h new file mode 100644 index 00000000..e8995e0e --- /dev/null +++ b/sources/reactphysics3d/testing/mathematics/Matrix3x3Test.h @@ -0,0 +1,409 @@ + +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +#ifndef MATRIX3DTEST_H +#define MATRIX3DTEST_H + +// Libraries +#include "../TestSuite/Test.h" +#include "../../mathematics/Matrix3x3.h" +#include +#include +#include + +// Namespaces +using namespace reactphysics3d; + +// Class MatrixTest +class Matrix3x3Test : public TestSuite::Test { + private : + Matrix3x3 matrix1; + Matrix3x3 matrix2; + public : + + // Constructor + Matrix3x3Test() { + matrix1.setAllValues(3, 7, -5, 13, -1, 2, 6, 5, 9); + matrix2.setAllValues(-13, 8, 2, 5, -25, 11, -7, 6, 21); + } + + // Run method of the Test + void run() { + testConstructors(); + testGetValue(); + testSetValue(); + testSetAllValues() ; + testGetTranspose(); + testGetInverse(); + testGetDeterminant(); + testGetTrace(); + testGetQuaternion(); + testIdentityMatrix(); + testOperatorAddition(); + testOperatorSubstraction(); + testOperatorMultiplicationWithConstant(); + testOperatorMultiplicationWithMatrix(); + testOperatorAssignment(); + testOperatorEquality(); + } + + // Test the constructors + void testConstructors() { + + // Constructor without argument + Matrix3x3 matrix; // This shouldn't throw an exception + test_(matrix.getValue(0,0) == 0); + test_(matrix.getValue(1,2) == 0); + test_(matrix.getValue(0,2) == 0); + + // Constructor with arguments + Matrix3x3 matrix3(1, 2, 3, 4, 5, 6, 7, 8, 9); + test_(matrix3.getValue(0,0) == 1); + test_(matrix3.getValue(0,1) == 2); + test_(matrix3.getValue(0,2) == 3); + test_(matrix3.getValue(1,0) == 4); + test_(matrix3.getValue(1,1) == 5); + test_(matrix3.getValue(1,2) == 6); + test_(matrix3.getValue(2,0) == 7); + test_(matrix3.getValue(2,1) == 8); + test_(matrix3.getValue(2,2) == 9); + + // Copy-constructor + Matrix3x3 matrix4 = matrix3; + test_(matrix4.getValue(0,0) == 1); + test_(matrix4.getValue(0,1) == 2); + test_(matrix4.getValue(0,2) == 3); + test_(matrix4.getValue(1,0) == 4); + test_(matrix4.getValue(1,1) == 5); + test_(matrix4.getValue(1,2) == 6); + test_(matrix4.getValue(2,0) == 7); + test_(matrix4.getValue(2,1) == 8); + test_(matrix4.getValue(2,2) == 9); + + // Conversion-constructor (Quaternion --> Matrix3x3) + + // Rotation matrix of a rotation of 180 degrees around x axis + Matrix3x3 rotation1(1, 0, 0, 0, -1, 0, 0, 0, -1); + Quaternion quaternion1(1, 0, 0, 0); + Matrix3x3 matrix5(quaternion1); // Convert the quaternion into a matrix + test_(matrix5 == rotation1); // Check if the matrix result and the rotation matrix are the same + + // Rotation matrix of a rotation of 180 degrees around y axis + Matrix3x3 rotation2(-1, 0, 0, 0, 1, 0, 0, 0, -1); + Quaternion quaternion2(0, 1, 0, 0); + Matrix3x3 matrix6(quaternion2); // Convert the quaternion into a matrix + test_(matrix6 == rotation2); // Check if the matrix result and the rotation matrix are the same + + // Rotation matrix of a rotation of 180 degrees around z axis + Matrix3x3 rotation3(-1, 0, 0, 0, -1, 0, 0, 0, 1); + Quaternion quaternion3(0, 0, 1, 0); + Matrix3x3 matrix7(quaternion3); // Convert the quaternion into a matrix + test_(matrix7 == rotation3); // Check if the matrix result and the rotation matrix are the same + } + + // Test getValue() + void testGetValue() { + // Try a valid getValue() + try { + test_(matrix1.getValue(0, 0) == 3); // This shouldn't throw an exception + test_(matrix1.getValue(1, 0) == 13); // This shouldn't throw an exception + test_(matrix1.getValue(1, 2) == 2); // This shouldn't throw an exception + test_(matrix1.getValue(1, 1) == -1); // This shouldn't throw an exception + succeed_(); // Succeed if no exceptions have been thrown + } + catch(std::invalid_argument& ex) { + fail_("Valid getValue() call throws an exception"); // Failed if an exception has been thrown + } + + // Try an invalid getValue() call + try { + matrix1.getValue(-1, 0); // This should throw an exception + fail_("Invalid getValue() call undetected"); + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try an invalid getValue() call + try { + matrix1.getValue(0, 3); // This should throw an exception + fail_("Invalid getValue() call undetected"); + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test setValue() + void testSetValue() { + + Matrix3x3 matrix; + + // Try a valid setValue() + try { + matrix.setValue(0, 0, 18); // This shouldn't throw an exception + matrix.setValue(0, 2, -6); // This shouldn't throw an exception + matrix.setValue(1, 0, -44); // This shouldn't throw an exception + matrix.setValue(1, 2, 21); // This shouldn't throw an exception + matrix.setValue(1, 1, 5); // This shouldn't throw an exception + test_(matrix.getValue(0, 0) == 18); + test_(matrix.getValue(0, 2) == -6); + test_(matrix.getValue(1, 0) == -44); + test_(matrix.getValue(1, 2) == 21); + test_(matrix.getValue(1, 1) == 5); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(std::invalid_argument& ex) { + fail_("Valid setValue() call throws an exception"); // Failed if an exception has been thrown + } + + // Try an invalid setValue() call + try { + matrix.setValue(-1, 0, 42); // This should throw an exception + fail_("Invalid setValue() call undetected"); // Failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try an invalid setValue() call + try { + matrix1.setValue(0, 3, 53); // This should throw an exception + fail_("Invalid setValue() call undetected"); // Failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test setAllValues() + void testSetAllValues() { + Matrix3x3 matrix; + matrix.setAllValues(1,2,3,4,5,6,7,8,9); + test_(matrix.getValue(0,0) == 1); + test_(matrix.getValue(0,1) == 2); + test_(matrix.getValue(0,2) == 3); + test_(matrix.getValue(1,0) == 4); + test_(matrix.getValue(1,1) == 5); + test_(matrix.getValue(1,2) == 6); + test_(matrix.getValue(2,0) == 7); + test_(matrix.getValue(2,1) == 8); + test_(matrix.getValue(2,2) == 9); + } + + // Test getTranspose() + void testGetTranspose() { + // Get the transpose of matrix1 + Matrix3x3 matrix = matrix1.getTranspose(); + + // Test the transpose matrix + test_(matrix.getValue(0, 0) == 3); + test_(matrix.getValue(0, 1) == 13); + test_(matrix.getValue(0, 2) == 6); + test_(matrix.getValue(1, 0) == 7); + test_(matrix.getValue(1, 1) == -1); + test_(matrix.getValue(1, 2) == 5); + test_(matrix.getValue(2, 0) == -5); + test_(matrix.getValue(2, 1) == 2); + test_(matrix.getValue(2, 2) == 9); + } + + // Test getInverse() + void testGetInverse() { + + // Construct a 3x3 matrix + Matrix3x3 matrix(0, 1, 2, 1, 0, 3, 4, -3, 8); + + // Try to inverse a invertible matrix + try { + Matrix3x3 result = matrix.getInverse(); // This shouldn't thrown an exception + test_(result.getValue(0, 0) == -4.5); + test_(result.getValue(0, 1) == 7); + test_(result.getValue(0, 2) == -3.0/2.0); + test_(result.getValue(1, 0) == -2); + test_(result.getValue(1, 1) == 4); + test_(result.getValue(1, 2) == -1); + test_(result.getValue(2, 0) == 3.0/2.0); + test_(result.getValue(2, 1) == -2); + test_(result.getValue(2, 2) == 1.0/2.0); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid getInverse() call throws an exception"); // Failed if an exception has been thrown + } + + // Try to inverse a square non-invertible matrix (determinant equal to zero) + try { + Matrix3x3 matrix4; + matrix4.getInverse(); // This should throw an exception + fail_("Invalid getInverse() call undetected (non-invertible matrix)"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test getDeterminant() + void testGetDeterminant() { + Matrix3x3 matrix; + test_(matrix.getDeterminant() == 0); + test_(matrix1.getDeterminant() == -1147); + test_(matrix2.getDeterminant() == 5937); + } + + // Test getTrace() + void testGetTrace() { + Matrix3x3 matrix; + test_(matrix.getTrace() == 0); + test_(matrix1.getTrace() == 11); + test_(matrix2.getTrace() == -17); + } + + // Test getQuaternion() + void testGetQuaternion() { + // Rotation matrix of a rotation of 180 degrees around x axis + Matrix3x3 rotation1(1, 0, 0, 0, -1, 0, 0, 0, -1); + + // Convert the matrix into a quaternion + Quaternion quaternion1 = rotation1.getQuaternion(); + test_(quaternion1.getX() == 1); + test_(quaternion1.getY() == 0); + test_(quaternion1.getZ() == 0); + test_(quaternion1.getW() == 0); + + // Rotation matrix of a rotation of 180 degrees around y axis + Matrix3x3 rotation2(-1, 0, 0, 0, 1, 0, 0, 0, -1); + + // Convert the matrix into a quaternion + Quaternion quaternion2 = rotation2.getQuaternion(); + test_(quaternion2.getX() == 0); + test_(quaternion2.getY() == 1); + test_(quaternion2.getZ() == 0); + test_(quaternion2.getW() == 0); + + // Rotation matrix of a rotation of 180 degrees around z axis + Matrix3x3 rotation3(-1, 0, 0, 0, -1, 0, 0, 0, 1); + + // Convert the matrix into a quaternion + Quaternion quaternion3 = rotation3.getQuaternion(); + test_(quaternion3.getX() == 0); + test_(quaternion3.getY() == 0); + test_(quaternion3.getZ() == 1); + test_(quaternion3.getW() == 0); + } + + // Test identityMatrix() + void testIdentityMatrix() { + Matrix3x3 matrix = Matrix3x3::identity(); + test_(matrix.getValue(0, 0) == 1); + test_(matrix.getValue(0, 1) == 0); + test_(matrix.getValue(0, 2) == 0); + test_(matrix.getValue(1, 0) == 0); + test_(matrix.getValue(1, 1) == 1); + test_(matrix.getValue(1, 2) == 0); + test_(matrix.getValue(2, 0) == 0); + test_(matrix.getValue(2, 1) == 0); + test_(matrix.getValue(2, 2) == 1); + } + + // Test operator+() + void testOperatorAddition() { + Matrix3x3 result = matrix1 + matrix2; + test_(result.getValue(0,0) == -10); + test_(result.getValue(0,1) == 15); + test_(result.getValue(0,2) == -3); + test_(result.getValue(1,0) == 18); + test_(result.getValue(1,1) == -26); + test_(result.getValue(1,2) == 13); + test_(result.getValue(2,0) == -1); + test_(result.getValue(2,1) == 11); + test_(result.getValue(2,2) == 30); + } + + // Test operator-() + void testOperatorSubstraction() { + Matrix3x3 result = matrix1 - matrix2; + test_(result.getValue(0,0) == 16); + test_(result.getValue(0,1) == -1); + test_(result.getValue(0,2) == -7); + test_(result.getValue(1,0) == 8); + test_(result.getValue(1,1) == 24); + test_(result.getValue(1,2) == -9); + test_(result.getValue(2,0) == 13); + test_(result.getValue(2,1) == -1); + test_(result.getValue(2,2) == -12); + } + + // Test operator* (multiplication with a constant number) + void testOperatorMultiplicationWithConstant() { + Matrix3x3 result = matrix1 * 2; + test_(result.getValue(0,0) == 6); + test_(result.getValue(0,1) == 14); + test_(result.getValue(0,2) == -10); + test_(result.getValue(1,0) == 26); + test_(result.getValue(1,1) == -2); + test_(result.getValue(1,2) == 4); + test_(result.getValue(2,0) == 12); + test_(result.getValue(2,1) == 10); + test_(result.getValue(2,2) == 18); + } + + // Test operator* (multiplication with matrix) + void testOperatorMultiplicationWithMatrix() { + Matrix3x3 result = matrix1 * matrix2; + test_(result.getValue(0,0) == 31); + test_(result.getValue(0,1) == -181); + test_(result.getValue(0,2) == -22); + test_(result.getValue(1,0) == -188); + test_(result.getValue(1,1) == 141); + test_(result.getValue(1,2) == 57); + test_(result.getValue(2,0) == -116); + test_(result.getValue(2,1) == -23); + test_(result.getValue(2,2) == 256); + } + + // Test operator=() + void testOperatorAssignment() { + Matrix3x3 matrix; + matrix = matrix1; + test_(matrix.getValue(0,0) == 3); + test_(matrix.getValue(0,1) == 7); + test_(matrix.getValue(0,2) == -5); + test_(matrix.getValue(1,0) == 13); + test_(matrix.getValue(1,1) == -1); + test_(matrix.getValue(1,2) == 2); + test_(matrix.getValue(2,0) == 6); + test_(matrix.getValue(2,1) == 5); + test_(matrix.getValue(2,2) == 9); + } + + // Test operator==() + void testOperatorEquality() { + Matrix3x3 matrix(3, 7, -5, 13, -1, 2, 6, 5, 9); + test_(matrix == matrix1); + test_(matrix1 == matrix); + test_(matrix == matrix); + matrix.setValue(1,1, 100); + test_(!(matrix == matrix1)); + test_(!(matrix1 == matrix)); + } +}; + +#endif diff --git a/sources/reactphysics3d/testing/mathematics/MatrixTest.h b/sources/reactphysics3d/testing/mathematics/MatrixTest.h new file mode 100644 index 00000000..a11d77eb --- /dev/null +++ b/sources/reactphysics3d/testing/mathematics/MatrixTest.h @@ -0,0 +1,648 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +#ifndef MATRIXTEST_H +#define MATRIXTEST_H + +// Libraries +#include "../TestSuite/Test.h" +#include "../../mathematics/Matrix.h" +#include + +// Namespaces +using namespace reactphysics3d; + +// Class MatrixTest +class MatrixTest : public TestSuite::Test { + private : + Matrix matrix1; + Matrix matrix2; + Matrix matrix3; + public : + + // Constructor + MatrixTest() : matrix1(2,3), matrix2(2,3), matrix3(3,2) { + matrix1.setValue(0, 0, 4); + matrix1.setValue(0, 1, 5); + matrix1.setValue(0, 2, 7); + matrix1.setValue(1, 0, 2); + matrix1.setValue(1, 1, 3); + matrix1.setValue(1, 2, -4); + + matrix2.setValue(0, 0, -12); + matrix2.setValue(0, 1, 3); + matrix2.setValue(0, 2, 16); + matrix2.setValue(1, 0, -7); + matrix2.setValue(1, 1, 4); + matrix2.setValue(1, 2, 6); + + matrix3.setValue(0, 0, -4); + matrix3.setValue(0, 1, -2); + matrix3.setValue(1, 0, 7); + matrix3.setValue(1, 1, 9); + matrix3.setValue(2, 0, 18); + matrix3.setValue(2, 1, 33); + } + + // Run method of the Test + void run() { + testConstructors(); + testGetValue(); + testSetValue(); + testGetNbRow(); + testGetNbColumn(); + testGetCofactor(); + testGetTranspose(); + testGetInverse(); + testGetDeterminant(); + testGetTrace(); + testIdentityMatrix(); + testOperatorAddition(); + testOperatorSubstraction(); + testOperatorMultiplicationWithConstant(); + testOperatorMultiplicationWithMatrix(); + testOperatorAssignment(); + testOperatorEquality(); + } + + // Test the constructors + void testConstructors() { + + // Try a valid constructor call + try { + // Constructor + Matrix matrix(4,6); // This shouldn't throw an exception + test_(matrix.getNbRow() == 4); + test_(matrix.getNbColumn() == 6); + test_(matrix.getValue(0,0) == 0); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(std::invalid_argument& ex) { + fail_("Valid constructor call throws an exception"); // Failed if an exception has been thrown + } + + // Try an invalid constructor call + try { + // Constructor + Matrix matrix(-2,6); // This should throw an exception + fail_("Invalid constructor call undetected (argument -2) "); // Failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try an invalid constructor call + try { + // Constructor + Matrix matrix(3,0); // This should throw an exception + fail_("Invalid constructor call undetected (argument 0)"); // Failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Copy-constructor + Matrix matrix4 = matrix1; + test_(matrix4.getNbRow() == 2); + test_(matrix4.getNbColumn() == 3); + test_(matrix4.getValue(0, 0) == 4); + test_(matrix4.getValue(0, 1) == 5); + test_(matrix4.getValue(0, 2) == 7); + test_(matrix4.getValue(1, 0) == 2); + test_(matrix4.getValue(1, 1) == 3); + test_(matrix4.getValue(1, 2) == -4); + } + + // Test getValue() + void testGetValue() { + // Try a valid getValue() + try { + test_(matrix1.getValue(0, 0) == 4); // This shouldn't throw an exception + test_(matrix1.getValue(0, 2) == 7); // This shouldn't throw an exception + test_(matrix1.getValue(1, 0) == 2); // This shouldn't throw an exception + test_(matrix1.getValue(1, 2) == -4); // This shouldn't throw an exception + test_(matrix1.getValue(1, 1) == 3); // This shouldn't throw an exception + succeed_(); // Succeed if no exceptions have been thrown + } + catch(std::invalid_argument& ex) { + fail_("Valid getValue() call throws an exception"); // Failed if an exception has been thrown + } + + // Try an invalid getValue() call + try { + matrix1.getValue(-1, 0); // This should throw an exception + fail_("Invalid getValue() call undetected"); + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try an invalid getValue() call + try { + matrix1.getValue(0, 3); // This should throw an exception + fail_("Invalid getValue() call undetected"); + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test setValue() + void testSetValue() { + + Matrix matrix(2,3); + + // Try a valid setValue() + try { + matrix.setValue(0, 0, 18); // This shouldn't throw an exception + matrix.setValue(0, 2, -6); // This shouldn't throw an exception + matrix.setValue(1, 0, -44); // This shouldn't throw an exception + matrix.setValue(1, 2, 21); // This shouldn't throw an exception + matrix.setValue(1, 1, 5); // This shouldn't throw an exception + test_(matrix.getValue(0, 0) == 18); + test_(matrix.getValue(0, 2) == -6); + test_(matrix.getValue(1, 0) == -44); + test_(matrix.getValue(1, 2) == 21); + test_(matrix.getValue(1, 1) == 5); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(std::invalid_argument& ex) { + fail_("Valid setValue() call throws an exception"); // Failed if an exception has been thrown + } + + // Try an invalid setValue() call + try { + matrix.setValue(-1, 0, 42); // This should throw an exception + fail_("Invalid setValue() call undetected"); // Failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try an invalid setValue() call + try { + matrix1.setValue(0, 3, 53); // This should throw an exception + fail_("Invalid setValue() call undetected"); // Failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test getNbRow() + void testGetNbRow() { + test_(matrix1.getNbRow() == 2); + test_(matrix3.getNbRow() == 3); + } + + // Test getNbColumn() + void testGetNbColumn() { + test_(matrix1.getNbColumn() == 3); + test_(matrix3.getNbColumn() == 2); + } + + // Test getCofactor() + void testGetCofactor() { + + // Try a valid getCofactor() + try { + Matrix matrix = matrix1.getCofactor(0,1); // This shouldn't throw an exception + + test_(matrix.getNbRow() == 1); + test_(matrix.getNbColumn() == 2); + test_(matrix.getValue(0, 0) == 2); + test_(matrix.getValue(0, 1) == -4); + + Matrix matrix4 = matrix1.getCofactor(1,2); // This shouldn't throw an exception + + test_(matrix4.getNbRow() == 1); + test_(matrix4.getNbColumn() == 2); + test_(matrix4.getValue(0, 0) == 4); + test_(matrix4.getValue(0, 1) == 5); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(std::invalid_argument& ex) { + fail_("Valid getCofactor() call throws an exception"); // Failed if an exception has been thrown + } + + // Try an invalid getCofactor() call + try { + matrix1.getCofactor(-1,0); // This should throw an exception + fail_("Invalid getCofactor() call undetected"); // Failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try an invalid getCofactor() call + try { + matrix1.getCofactor(0,3); // This should throw an exception + fail_("Invalid getCofactor() call undetected"); // Failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test getTranspose() + void testGetTranspose() { + // Get the transpose of matrix1 + Matrix matrix = matrix1.getTranspose(); + + // Test the transpose matrix + test_(matrix.getNbRow() == 3); + test_(matrix.getNbColumn() == 2); + test_(matrix.getValue(0, 0) == 4); + test_(matrix.getValue(0, 1) == 2); + test_(matrix.getValue(1, 0) == 5); + test_(matrix.getValue(1, 1) == 3); + test_(matrix.getValue(2, 0) == 7); + test_(matrix.getValue(2, 1) == -4); + } + + // Test getInverse() + void testGetInverse() { + + // Construct a 3x3 matrix + Matrix matrix(3,3); + matrix.setValue(0, 0, 0); + matrix.setValue(0, 1, 1); + matrix.setValue(0, 2, 2); + matrix.setValue(1, 0, 1); + matrix.setValue(1, 1, 0); + matrix.setValue(1, 2, 3); + matrix.setValue(2, 0, 4); + matrix.setValue(2, 1, -3); + matrix.setValue(2, 2, 8); + + // Try to inverse a invertible matrix + try { + Matrix result = matrix.getInverse(); // This shouldn't thrown an exception + test_(result.getValue(0, 0) == -4.5); + test_(result.getValue(0, 1) == 7); + test_(result.getValue(0, 2) == -3.0/2.0); + test_(result.getValue(1, 0) == -2); + test_(result.getValue(1, 1) == 4); + test_(result.getValue(1, 2) == -1); + test_(result.getValue(2, 0) == 3.0/2.0); + test_(result.getValue(2, 1) == -2); + test_(result.getValue(2, 2) == 1.0/2.0); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid getInverse() call throws an exception"); // Failed if an exception has been thrown + } + + // Try to inverse a non-square matrix + try { + matrix1.getInverse(); // This should throw an exception + fail_("Invalid getInverse() call undetected (non-square matrix)"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try to inverse a square non-invertible matrix (determinant equal to zero) + try { + Matrix matrix4(2,2); + matrix4.setValue(0, 0, 3); + matrix4.setValue(0, 1, 2); + matrix4.setValue(1, 0, 3); + matrix4.setValue(1, 1, 2); + matrix4.getInverse(); // This should throw an exception + fail_("Invalid getInverse() call undetected (non-invertible matrix)"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test getDeterminant() + void testGetDeterminant() { + + // Try to compute the determinant of a square matrix + try { + Matrix matrix(2,2); + test_(matrix.getDeterminant() == 0); // This shouldn't throw an exception + matrix.setValue(0,0, 4); + matrix.setValue(0,1, -9); + matrix.setValue(1,0, 0); + matrix.setValue(1,1, 5); + test_(matrix.getDeterminant() == 20); // This shouldn't throw an exception + matrix.setValue(0,0, 6); + matrix.setValue(0,1, -9); + matrix.setValue(1,0, -4); + matrix.setValue(1,1, 6); + test_(matrix.getDeterminant() == 0); // This shouldn't throw an exception + succeed_(); // Succeed if no exceptions have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid getDeterminant() call throws an exception"); // Failed if an exception has been thrown + } + + // Try to compute the determinant of a non-square matrix + try { + Matrix matrix5(2,8); + matrix5.setValue(0, 2, 3); + matrix5.setValue(1, 1, 2); + matrix5.getDeterminant(); // This should throw an exception + fail_("getDeterminant() call with a non-square matrix undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test getTrace() + void testGetTrace() { + + // Try to compute the trace of a square matrix + try { + // Construct a 3x3 matrix + Matrix matrix(3,3); + matrix.setValue(0, 0, -2); + matrix.setValue(0, 1, 1); + matrix.setValue(0, 2, 2); + matrix.setValue(1, 0, 1); + matrix.setValue(1, 1, 5); + matrix.setValue(1, 2, 3); + matrix.setValue(2, 0, 4); + matrix.setValue(2, 1, -3); + matrix.setValue(2, 2, 8); + test_(matrix.getTrace() == 11); // This shouldn't throw an exception + succeed_(); // Succeed if no exceptions have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid getTrace() call throws an exception"); // Failed if an exception has been thrown + } + + // Try to compute the trace of a non-square matrix + try { + Matrix matrix5(2,8); + matrix5.getTrace(); // This should throw an exception + fail_("getTrace() call with a non-square matrix undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test identityMatrix() + void testIdentityMatrix() { + + + // Try to compute a valid identity matrix + try { + Matrix matrix = Matrix::identity(2); // This shouldn't throw an exception + test_(matrix.getNbRow() == 2); + test_(matrix.getNbColumn() == 2); + test_(matrix.getValue(0, 0) == 1); + test_(matrix.getValue(0, 1) == 0); + test_(matrix.getValue(1, 0) == 0); + test_(matrix.getValue(1, 1) == 1); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(std::invalid_argument& ex) { + fail_("Valid identity() call throws an exception"); // Failed if an exception has been thrown + } + + // Try to compute an invalid identity matrix + try { + Matrix matrix5 = Matrix::identity(0); // This should throw an exception + fail_("Invalid identity() call (argument 0) undetected"); // Failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try to compute an invalid identity matrix + try { + Matrix matrix5 = Matrix::identity(-1); // This should throw an exception + fail_("Invalid identity() call (argument -1) undetected"); // Failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test operator+() + void testOperatorAddition() { + + // Try to compute a valid addition + try { + Matrix result = matrix1 + matrix2; // This shouldn't throw an exception + test_(result.getNbRow() == 2); + test_(result.getNbColumn() == 3); + test_(result.getValue(0,0) == -8); + test_(result.getValue(0,1) == 8); + test_(result.getValue(0,2) == 23); + test_(result.getValue(1,0) == -5); + test_(result.getValue(1,1) == 7); + test_(result.getValue(1,2) == 2); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid matrix addition throws an exception"); // Failed if an exception has been thrown + } + + // Try to compute an invalid addition + try { + Matrix matrix5(2,4); + matrix1 + matrix5; // This should throw an exception + fail_("Invalid matrix addition undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try to compute an invalid addition + try { + Matrix matrix5(1,3); + matrix1 + matrix5; // This should throw an exception + fail_("Invalid matrix addition undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test operator-() + void testOperatorSubstraction() { + + // Try to compute a valid substraction + try { + Matrix result = matrix1 - matrix2; // This shouldn't throw an exception + test_(result.getNbRow() == 2); + test_(result.getNbColumn() == 3); + test_(result.getValue(0,0) == 16); + test_(result.getValue(0,1) == 2); + test_(result.getValue(0,2) == -9); + test_(result.getValue(1,0) == 9); + test_(result.getValue(1,1) == -1); + test_(result.getValue(1,2) == -10); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid matrix substraction throws an exception"); // Failed if an exception has been thrown + } + + // Try to compute an invalid substraction + try { + Matrix matrix5(2,4); + matrix1 - matrix5; // This should throw an exception + fail_("Invalid matrix substraction undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try to compute an invalid substraction + try { + Matrix matrix5(1,3); + matrix1 - matrix5; // This should throw an exception + fail_("Invalid matrix substraction undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test operator* (multiplication with a constant number) + void testOperatorMultiplicationWithConstant() { + Matrix matrix = matrix1 * 2; + test_(matrix.getNbRow() == 2); + test_(matrix.getNbColumn() == 3); + test_(matrix.getValue(0,0) == 8); + test_(matrix.getValue(0,1) == 10); + test_(matrix.getValue(0,2) == 14); + test_(matrix.getValue(1,0) == 4); + test_(matrix.getValue(1,1) == 6); + test_(matrix.getValue(1,2) == -8); + } + + // Test operator* (multiplication with matrix) + void testOperatorMultiplicationWithMatrix() { + + // Try to compute a valid multiplication + try { + Matrix result = matrix1 * matrix3; // This shouldn't throw an exception + test_(result.getNbRow() == 2); + test_(result.getNbColumn() == 2); + test_(result.getValue(0,0) == 145); + test_(result.getValue(0,1) == 268); + test_(result.getValue(1,0) == -59); + test_(result.getValue(1,1) == -109); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid matrix multiplication throws an exception"); // Failed if an exception has been thrown + } + + // Try to compute an invalid multiplication + try { + Matrix matrix5(1,3); + matrix1 * matrix5; // This should throw an exception + fail_("Invalid matrix substraction undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + + // Try to compute an invalid multiplication + try { + Matrix matrix5(2,2); + matrix1 * matrix5; // This should throw an exception + fail_("Invalid matrix substraction undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test operator=() + void testOperatorAssignment() { + + // Try to compute a valid assignment + try { + Matrix matrix(2,3); + matrix = matrix1; // This shouldn't throw an exception + test_(matrix.getValue(0, 0) == 4); + test_(matrix.getValue(0, 2) == 7); + test_(matrix.getValue(1, 0) == 2); + test_(matrix.getValue(1, 2) == -4); + test_(matrix.getValue(1, 1) == 3); + succeed_(); // Succeed if no exceptions have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid matrix assignment throws an exception"); // Failed if an exception has been thrown + } + + // Try to compute an invalid assignment + try { + Matrix matrix(2,2); + matrix = matrix1; // This should throw an exception + fail_("Invalid matrix assignment undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test operator==() + void testOperatorEquality() { + + + // Try to test a valid equality + try { + Matrix matrix(2,3); + matrix.setValue(0, 0, 4); + matrix.setValue(0, 1, 5); + matrix.setValue(0, 2, 7); + matrix.setValue(1, 0, 2); + matrix.setValue(1, 1, 3); + matrix.setValue(1, 2, -4); + test_(matrix == matrix1); // This shouldn't throw an exception + test_(matrix1 == matrix); // This shouldn't throw an exception + test_(matrix == matrix); // This shouldn't throw an exception + matrix.setValue(1,1, 5); + test_(!(matrix == matrix1)); // This shouldn't throw an exception + test_(!(matrix1 == matrix)); // This shouldn't throw an exception + succeed_(); // Succeed if no exceptions have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid matrix equality test throws an exception"); // Failed if an exception has been thrown + } + + // Try to test a invalid equality + try { + Matrix matrix(2,2); + matrix == matrix1; // This should throw an exception + fail_("Invalid matrix assignment undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + + +}; + +#endif + diff --git a/sources/reactphysics3d/testing/mathematics/QuaternionTest.h b/sources/reactphysics3d/testing/mathematics/QuaternionTest.h new file mode 100644 index 00000000..72eba747 --- /dev/null +++ b/sources/reactphysics3d/testing/mathematics/QuaternionTest.h @@ -0,0 +1,322 @@ + +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +#ifndef QUATERNIONTEST_H +#define QUATERNIONTEST_H + +// Libraries +#include "../TestSuite/Test.h" +#include "../../mathematics/Quaternion.h" +#include +#include + +// Namespaces +using namespace reactphysics3d; + +// Class MatrixTest +class QuaternionTest : public TestSuite::Test { + private : + Quaternion quaternion1; + Quaternion quaternion2; + public : + + // Constructor + QuaternionTest() : quaternion1(2,3,4,5), quaternion2(6,7,8,9) { + + } + + // Run method of the Test + void run() { + testConstructors(); + testVectorV(); + testLength(); + testGetUnit(); + testGetConjugate(); + testGetInverse(); + testOperatorAddition(); + testOperatorSubstraction(); + testOperatorMultiplicationWithConstant(); + testOperatorMultiplicationWithQuaternion(); + testOperatorAssignment(); + testOperatorEquality(); + } + + // Test the constructors + void testConstructors() { + // Constructor without argument + Quaternion quaternion; + test_(quaternion.getX() == 0); + test_(quaternion.getY() == 0); + test_(quaternion.getZ() == 0); + test_(quaternion.getW() == 0); + + // Constructor with argument + Quaternion quaternion3(1,2,3,4); + test_(quaternion3.getX() == 1); + test_(quaternion3.getY() == 2); + test_(quaternion3.getZ() == 3); + test_(quaternion3.getW() == 4); + + // Constructor with vector + Vector3D vector(2,3,4); + Quaternion quaternion4(5, vector); + test_(quaternion4.getX() == 2); + test_(quaternion4.getY() == 3); + test_(quaternion4.getZ() == 4); + test_(quaternion4.getW() == 5); + + // Copy-constructor + Quaternion quaternion5 = quaternion3; + test_(quaternion5.getX() == 1); + test_(quaternion5.getY() == 2); + test_(quaternion5.getZ() == 3); + test_(quaternion5.getW() == 4); + } + + // Test getX() + void testGetX() { + test_(quaternion1.getX() == 2); + test_(quaternion2.getX() == 6); + } + + // Test getY() + void testGetY() { + test_(quaternion1.getY() == 3); + test_(quaternion2.getY() == 7); + } + + // Test getZ() + void testGetZ() { + test_(quaternion1.getZ() == 4); + test_(quaternion2.getZ() == 8); + } + + // Test getW() + void testGetW() { + test_(quaternion1.getW() == 5); + test_(quaternion2.getW() == 9); + } + + // Test setX() + void testSetX() { + Quaternion quaternion; + quaternion.setX(3); + test_(quaternion.getX() == 3); + test_(quaternion.getY() == 0); + test_(quaternion.getZ() == 0); + test_(quaternion.getW() == 0); + } + + // Test setY() + void testSetY() { + Quaternion quaternion; + quaternion.setY(3); + test_(quaternion.getX() == 0); + test_(quaternion.getY() == 3); + test_(quaternion.getZ() == 0); + test_(quaternion.getW() == 0); + } + + // Test setZ() + void testSetZ() { + Quaternion quaternion; + quaternion.setZ(3); + test_(quaternion.getX() == 0); + test_(quaternion.getY() == 0); + test_(quaternion.getZ() == 3); + test_(quaternion.getW() == 0); + } + + // Test setW() + void testSetW() { + Quaternion quaternion; + quaternion.setW(3); + test_(quaternion.getX() == 0); + test_(quaternion.getY() == 0); + test_(quaternion.getZ() == 0); + test_(quaternion.getW() == 3); + } + + // Test vectorV() + void testVectorV() { + Vector3D vector1(2,3,4); + Vector3D vector2(6,7,8); + + Vector3D vectorTest1 = quaternion1.vectorV(); + Vector3D vectorTest2 = quaternion2.vectorV(); + test_(vectorTest1 == vector1); + test_(vectorTest2 == vector2); + } + + // Test length() + void testLength() { + Quaternion quaternion; + test_(quaternion.length() == 0); + + Quaternion quaternion3(3, 4, 0, 0); + test_(quaternion3.length() == 5); + + Quaternion quaternion4(0, 4, 3, 0); + test_(quaternion4.length() == 5); + + Quaternion quaternion5(0, 0, 3, 4); + test_(quaternion5.length() == 5); + } + + // Test getUnit() + void testGetUnit() { + // Try to compute a valid unit quaternion + try { + Quaternion quaternion(3, 4, 0, 0); + Quaternion unit = quaternion.getUnit(); // This shouldn't throw an exception + test_(unit.getX() == 3.0/5.0); + test_(unit.getY() == 4.0/5.0); + test_(unit.getZ() == 0); + test_(unit.getW() == 0); + + Quaternion quaternion3(0, 0, 4, 3); + Quaternion unit2 = quaternion3.getUnit(); // This shouldn't throw an exception + test_(unit2.getX() == 0); + test_(unit2.getY() == 0); + test_(unit2.getZ() == 4.0/5.0); + test_(unit2.getW() == 3.0/5.0); + succeed_(); // Succeed if no exception have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid getUnit() call throw an exception"); // Failed if an exception has been thrown + } + + // Try to compute an invalid unit quaternion + try { + Quaternion quaternion(0, 0, 0, 0); + quaternion.getUnit(); // This should throw an exception + fail_("Invalid getUnit() call undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test getConjugate() + void testGetConjugate() { + Quaternion conjugate1 = quaternion1.getConjugate(); + test_(conjugate1.getX() == -2); + test_(conjugate1.getY() == -3); + test_(conjugate1.getZ() == -4); + test_(conjugate1.getW() == 5); + + Quaternion conjugate2 = quaternion2.getConjugate(); + test_(conjugate2.getX() == -6); + test_(conjugate2.getY() == -7); + test_(conjugate2.getZ() == -8); + test_(conjugate2.getW() == 9); + } + + // Test getInverse() + void testGetInverse() { + // Try to compute a valid inverse quaternion + try { + Quaternion quaternion(3, 4, 0, 0); + Quaternion inverse = quaternion.getInverse(); // This shouldn't throw an exception + test_(inverse.getX() == -3.0/25.0); + test_(inverse.getY() == -4.0/25.0); + test_(inverse.getZ() == 0); + test_(inverse.getW() == 0); + + Quaternion quaternion3(0, 0, 4, 3); + Quaternion inverse2 = quaternion3.getInverse(); // This shouldn't throw an exception + test_(inverse2.getX() == 0); + test_(inverse2.getY() == 0); + test_(inverse2.getZ() == -4.0/25.0); + test_(inverse2.getW() == 3.0/25.0); + succeed_(); // Succeed if no exception have been thrown + } + catch(MathematicsException& ex) { + fail_("Valid getInverse() call throw an exception"); // Failed if an exception has been thrown + } + + // Try to compute an invalid unit quaternion + try { + Quaternion quaternion(0, 0, 0, 0); + quaternion.getInverse(); // This should throw an exception + fail_("Invalid getInverse() call undetected"); // Failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // Succeed if an exception has been thrown + } + } + + // Test operator+() + void testOperatorAddition() { + Quaternion result = quaternion1 + quaternion2; + test_(result.getX() == 8); + test_(result.getY() == 10); + test_(result.getZ() == 12); + test_(result.getW() == 14); + } + + // Test operator-() + void testOperatorSubstraction() { + Quaternion result = quaternion1 - quaternion2; + test_(result.getX() == -4); + test_(result.getY() == -4); + test_(result.getZ() == -4); + test_(result.getW() == -4); + } + + // Test operator* (multiplication with a constant number) + void testOperatorMultiplicationWithConstant() { + Quaternion result = quaternion1 * 3; + test_(result.getX() == 6); + test_(result.getY() == 9); + test_(result.getZ() == 12); + test_(result.getW() == 15); + } + + // Test operator* (multiplication with quaternion) + void testOperatorMultiplicationWithQuaternion() { + Quaternion result = quaternion1 * quaternion2; + test_(result.getX() == 44); + test_(result.getY() == 70); + test_(result.getZ() == 72); + test_(result.getW() == -20); + } + + // Test operator=() + void testOperatorAssignment() { + Quaternion quaternion; + quaternion = quaternion1; + test_(quaternion.getX() == 2); + test_(quaternion.getY() == 3); + test_(quaternion.getZ() == 4); + test_(quaternion.getW() == 5); + } + + // Test operator==() + void testOperatorEquality() { + Quaternion quaternion(2,3,4,5); + test_(quaternion == quaternion1); + test_(quaternion1 == quaternion); + test_(!(quaternion2 == quaternion1)); + test_(!(quaternion1 == quaternion2)); + } +}; + +#endif diff --git a/sources/reactphysics3d/testing/mathematics/Vector3DTest.h b/sources/reactphysics3d/testing/mathematics/Vector3DTest.h new file mode 100644 index 00000000..35831565 --- /dev/null +++ b/sources/reactphysics3d/testing/mathematics/Vector3DTest.h @@ -0,0 +1,272 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +#ifndef VECTOR3DTEST_H +#define VECTOR3DTEST_H + +// Libraries +#include "../TestSuite/Test.h" +#include "../../mathematics/Vector3D.h" +#include + +// Namespaces +using namespace reactphysics3d; + +// Class Vector3DTest +class Vector3DTest : public TestSuite::Test { + private : + Vector3D vector1; + Vector3D vector2; + public : + + // Constructor + Vector3DTest() : vector1(1,2,3), vector2(-3,5,7) { + + } + + // Run method of the Test + void run() { + testConstructors(); + testGetX(); + testGetY(); + testGetZ(); + testSetX(); + testSetY(); + testSetZ(); + testSetAllValues(); + testLength(); + testGetUnit(); + testScalarProduct(); + testCrossProduct(); + testOperatorAddition(); + testOperatorSubstraction(); + testOperatorConstantMultiplications(); + testOperatorAssignment(); + } + + // Test the constructors + void testConstructors() { + + // Constructor without arguments + Vector3D vector; + test_(vector.getX() == 0); + test_(vector.getY() == 0); + test_(vector.getZ() == 0); + + // Constructor with arguments + Vector3D vector3(4, -67, 21); + test_(vector3.getX() == 4); + test_(vector3.getY() == -67); + test_(vector3.getZ() == 21); + + // Copy-constructor + Vector3D vector4 = vector1; + test_(vector4.getX() == 1); + test_(vector4.getY() == 2); + test_(vector4.getZ() == 3); + } + + // Test getX() + void testGetX() { + test_(vector1.getX() == 1); + } + + // Test getY() + void testGetY() { + test_(vector1.getY() == 2); + } + + // Test getZ() + void testGetZ() { + test_(vector1.getZ() == 3); + } + + // Test setX() + void testSetX() { + Vector3D vector(5, 6, 7); + vector.setX(8); + test_(vector.getX() == 8); + test_(vector.getY() == 6); + test_(vector.getZ() == 7); + } + + // Test setY() + void testSetY() { + Vector3D vector(5, 6, 7); + vector.setY(8); + test_(vector.getX() == 5); + test_(vector.getY() == 8); + test_(vector.getZ() == 7); + } + + // Test setZ() + void testSetZ() { + Vector3D vector(5, 6, 7); + vector.setZ(8); + test_(vector.getX() == 5); + test_(vector.getY() == 6); + test_(vector.getZ() == 8); + } + + + // Test setAllValues() + void testSetAllValues() { + Vector3D vector(5, 6, 7); + vector1.setAllValues(4,3,9); + test_(vector1.getX() == 4); + test_(vector1.getY() == 3); + test_(vector1.getZ() == 9); + } + + // Test length() + void testLength() { + Vector3D vector3; + test_(vector3.length() == 0); + vector3.setAllValues(3, 4, 0); + test_(vector3.length() == 5); + vector3.setAllValues(0, -3, 4); + test_(vector3.length() == 5); + } + + // Test getUnit() + void testGetUnit() { + + Vector3D vector3(-23, 0, 0); + test_(vector3.getUnit().length() == 1); + test_(vector3.getUnit().getX() == -1); + test_(vector3.getUnit().getY() == 0); + test_(vector3.getUnit().getZ() == 0); + + vector3.setAllValues(0, 6, 0); + test_(vector3.getUnit().length() == 1); + test_(vector3.getUnit().getX() == 0); + test_(vector3.getUnit().getY() == 1); + test_(vector3.getUnit().getZ() == 0); + + vector3.setAllValues(0, 0, 13); + test_(vector3.getUnit().length() == 1); + test_(vector3.getUnit().getX() == 0); + test_(vector3.getUnit().getY() == 0); + test_(vector3.getUnit().getZ() == 1); + + vector3.setAllValues(0, 0, 0); // Vector of length equal to zero + try { + vector3.getUnit(); // This should throw an exception + fail_("getUnit() with a vector of length equals to zero undetected"); // The test failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + } + + // Test scalarProduct() + void testScalarProduct() { + Vector3D vector3(2, -3, 5); + Vector3D vector4(7, 4, 6); + + // Test the scalar product result + test_(vector3.scalarProduct(vector4) == 32); + } + + // Test crossProduct() + void testCrossProduct() { + Vector3D vector3(4, -5, 2); + Vector3D vector4(3, 2, 6); + + // Compute the cross product + Vector3D result = vector3.crossProduct(vector4); + + // Test the result + test_(result.getX() == -34); + test_(result.getY() == -18); + test_(result.getZ() == 23); + } + + // Test operator+() + void testOperatorAddition() { + Vector3D vector3(4, -5, 2); + Vector3D vector4(3, 2, 6); + + // Compute the sum + Vector3D result = vector3 + vector4; + + // Test the result + test_(result.getX() == 7); + test_(result.getY() == -3); + test_(result.getZ() == 8); + } + + // Test operator-() + void testOperatorSubstraction() { + Vector3D vector3(4, -5, 2); + Vector3D vector4(3, 2, 6); + + // Compute the substraction + Vector3D result = vector3 - vector4; + + // Test the result + test_(result.getX() == 1); + test_(result.getY() == -7); + test_(result.getZ() == -4); + } + + // Test operator*() (with a constant number) + void testOperatorConstantMultiplications() { + Vector3D vector3(4, -5, 2); + + // Compute the multiplication + Vector3D result = vector3 * 5; + + // Test the result + test_(result.getX() == 20); + test_(result.getY() == -25); + test_(result.getZ() == 10); + } + + // Test operator=() + void testOperatorAssignment() { + Vector3D vector3(4, -5, 2); + + // Assignment + Vector3D result; + result = vector3; + + // Test the result + test_(result.getX() == 4); + test_(result.getY() == -5); + test_(result.getZ() == 2); + } + + // Test operator==() + void testOperatorEquality() { + Vector3D vector3(4, -5, 2); + Vector3D vector4(4, -5, 2); + Vector3D vector5(3, -5, -2); + + // Test the equality + test_(vector3 == vector4); + test_(vector4 == vector3); + test_(vector3 == vector3); + test_(!(vector3 == vector5)); + test_(!(vector5 == vector3)); + } +}; + +#endif + diff --git a/sources/reactphysics3d/testing/mathematics/VectorTest.h b/sources/reactphysics3d/testing/mathematics/VectorTest.h new file mode 100644 index 00000000..0dda5787 --- /dev/null +++ b/sources/reactphysics3d/testing/mathematics/VectorTest.h @@ -0,0 +1,450 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +#ifndef VECTORTEST_H +#define VECTORTEST_H + +// Libraries +#include "../TestSuite/Test.h" +#include "../../mathematics/Vector.h" +#include + +// Namespaces +using namespace reactphysics3d; + +// Class VectorTest +class VectorTest : public TestSuite::Test { + private : + Vector vect2; // Vector of dimension 2 + Vector vect4; // Vector of dimension 4 + public : + + // Constructor + VectorTest() : vect2(2), vect4(4) { + vect2.setValue(0, 3); + vect2.setValue(1, 5); + + vect4.setValue(0, 1); + vect4.setValue(1, -5); + vect4.setValue(2, 10); + vect4.setValue(3, 3); + } + + // Run method of the Test + void run() { + testConstructors(); + testGetValue(); + testSetValue(); + testGetNbComponent(); + testLength(); + testGetUnit(); + testScalarProduct(); + testCrossProduct(); + testOperatorAddition(); + testOperatorSubstraction(); + testOperatorConstantMultiplications(); + testOperatorAssignment(); + } + + // Test the constructors + void testConstructors() { + + // Try a valid constructor call + try { + // Constructor + Vector vector(3); // This shouldn't throw an exception + test_(vector.getValue(0) == 0); + test_(vector.getValue(1) == 0); + test_(vector.getValue(2) == 0); + succeed_(); // Test succeed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + fail_("Valid constructor call throws an exception"); // Test failed if an exception has been thrown + } + + // Try a invalid constructor call + try { + // Constructor + Vector vector3(-1); // This should throw an exception + fail_("Invalid constructor (argument -1) call undetected"); // Test failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Test succeed if an exception has been thrown + } + + // Try a invalid constructor call + try { + // Constructor + Vector vector4(0); // This should throw an exception + fail_("Invalid constructor call (argument 0) undetected"); // Test failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // Test succeed if an exception has been thrown + } + + // Copy-Constructor + Vector vector5 = vect2; + test_(vector5.getValue(0) == 3); + test_(vector5.getValue(1) == 5); + } + + // Test getValue() + void testGetValue() { + test_(vect2.getValue(0) == 3); + test_(vect2.getValue(1) == 5); + + // Try to get an invalid value + try { + vect2.getValue(-1); // This should throw an exception + fail_("Invalid getValue(-1) undetected"); // The test failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + + // Try to get an invalid value + try { + vect2.getValue(2); // This should throw an exception + fail_("Invalid getValue(2) undetected"); // The test failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + } + + // Test setValue() + void testSetValue() { + Vector vector(5); + + // Try to set valid values + try { + vector.setValue(0, 5); + vector.setValue(3, -1); + vector.setValue(4, 14); + test_(vector.getValue(0) == 5); + test_(vector.getValue(3) == -1); + test_(vector.getValue(4) == 14); + succeed_(); // The test succeed if an exception have been thrown + } + catch(std::invalid_argument& ex) { + fail_("Valid setValue() throws an exception"); // The failed if an exception has been thrown + } + + // Try to set an invalid value + try { + vector.setValue(-1, 4); // This should throw an exception + fail_("Invalid setValue(-1,4) undetected"); // The test failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + + // Try to set an invalid value + try { + vector.setValue(5, 2); // This should throw an exception + fail_("Invalid setValue(5,2) undetected"); // The test failed if no exception have been thrown + } + catch(std::invalid_argument& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + } + + // Test getNbComponent() + void testGetNbComponent() { + test_(vect2.getNbComponent() == 2); + test_(vect4.getNbComponent() == 4); + } + + // Test length() + void testLength() { + Vector vector1(2); + test_(vector1.length() == 0); + vector1.setValue(0, 4); + test_(vector1.length() == 4); + vector1.setValue(1, -3); + test_(vector1.length() == 5); + } + + // Test getUnit() + void testGetUnit() { + Vector vector1(3); + vector1.setValue(0, 3); + test_(vector1.getUnit().length() == 1); + test_(vector1.getUnit().getValue(0) == 1); + test_(vector1.getUnit().getValue(1) == 0); + test_(vector1.getUnit().getValue(2) == 0); + + Vector vector2(8); + vector2.setValue(2, 54); + test_(vector2.getUnit().length() == 1); + test_(vector2.getUnit().getValue(0) == 0); + test_(vector2.getUnit().getValue(1) == 0); + test_(vector2.getUnit().getValue(2) == 1); + + Vector vector3(7); // Vector of length equal to zero + try { + vector3.getUnit(); // This should throw an exception + fail_("getUnit() with a vector of length equals to zero undetected"); // The test failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + } + + // Test scalarProduct() + void testScalarProduct() { + Vector vector1(2); + vector1.setValue(0, 4); + vector1.setValue(1, -5); + + Vector vector2(2); + vector2.setValue(0, 3); + vector2.setValue(1, 2); + + // Try to compute a valid scalar product + try { + test_(vector1.scalarProduct(vector2) == 2); + succeed_(); // The test succeed if no exception have been thrown + } + catch(MathematicsException& ex) { + fail_("scalarProduct() thrown an exception during a valid scalar product computation"); // The test failed if an exception has been thrown + } + + // Try to compute a invalid scalar product + Vector vector3(5); + try { + vector1.scalarProduct(vector3); // This should throw an exception + fail_("Invalid dimensions in scalarProduct() undetected"); // The test failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + } + + // Test crossProduct() + void testCrossProduct() { + Vector vector1(3); + vector1.setValue(0, 4); + vector1.setValue(1, -5); + vector1.setValue(2, 2); + + Vector vector2(3); + vector2.setValue(0, 3); + vector2.setValue(1, 2); + vector2.setValue(2, 6); + + // Try to compute a valid scalar product + try { + Vector result = vector1.crossProduct(vector2); + test_(result.getValue(0) == -34); + test_(result.getValue(1) == -18); + test_(result.getValue(2) == 23); + succeed_(); // The test succeed if no exception have been thrown + } + catch(MathematicsException& ex) { + fail_("crossProduct() thrown an exception during a valid cross product computation"); // The test failed if an exception has been thrown + } + + // Try to compute a invalid cross product + Vector vector3(5); + try { + vector1.crossProduct(vector3); // This should throw an exception + fail_("Invalid dimensions in crossProduct() undetected"); // The test failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + } + + // Test operator+() + void testOperatorAddition() { + Vector vector1(4); + vector1.setValue(0, 3); + vector1.setValue(3, -2); + + Vector vector2(4); + vector2.setValue(0, 9); + vector2.setValue(2, 6); + vector2.setValue(3, 9); + + // Try to compute a valid sum (two vectors with the same dimensions) + try { + // Compute the sum + Vector sum = vector1 + vector2; // This shouldn't throw an exception + + test_(sum.getValue(0) == 12); + test_(sum.getValue(1) == 0); + test_(sum.getValue(2) == 6); + test_(sum.getValue(3) == 7); + succeed_(); // The test succeed if no exception have been thrown + } + catch(MathematicsException& ex) { + fail_("A valid sum of two vectors throws an excception"); // The test failed if an exception has been thrown + } + + // Try to compute an invalid sum (vectors with different dimensions) + Vector vector3(3); + Vector vector4(5); + try { + // Compute the sum + Vector sum = vector3 + vector4; // This should throw an exception + fail_("An invalid sum of two vectors undetected"); // The test failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + } + + // Test operator-() + void testOperatorSubstraction() { + Vector vector1(4); + vector1.setValue(0, 3); + vector1.setValue(3, -2); + + Vector vector2(4); + vector2.setValue(0, 9); + vector2.setValue(2, 6); + vector2.setValue(3, 9); + + // Try to compute a valid subtraction (two vectors with the same dimensions) + try { + // Compute the substraction + Vector sub = vector1 - vector2; // This shouldn't throw an exception + + test_(sub.getValue(0) == -6); + test_(sub.getValue(1) == 0); + test_(sub.getValue(2) == -6); + test_(sub.getValue(3) == -11); + succeed_(); // The test succeed if no exception have been thrown + } + catch(MathematicsException& ex) { + fail_("A valid subtraction of two vectors throws an excception"); // The test failed if an exception has been thrown + } + + // Try to compute an invalid substraction (vectors with different dimensions) + Vector vector3(3); + Vector vector4(5); + try { + // Compute the substraction + Vector sub = vector3 - vector4; // This should throw an exception + fail_("An invalid substraction of two vectors undetected"); // The test failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + } + + // Test operator*() (with a constant number) + void testOperatorConstantMultiplications() { + Vector vector(4); + vector.setValue(0, 3); + vector.setValue(3, -2); + + // Compute the multiplication + Vector sum = vector * 3.0; + test_(sum.getValue(0) == 9); + test_(sum.getValue(1) == 0); + test_(sum.getValue(2) == 0); + test_(sum.getValue(3) == -6); + } + + // Test operator=() + void testOperatorAssignment() { + Vector vector1(2); + vector1.setValue(0, 4); + vector1.setValue(1, 7); + + Vector vector2(8); + + + // Try to compute a valid assignment (two vectors with the same dimensions) + try { + Vector vector(2); + vector = vector1; // This shouldn't throw an exception + test_(vector == vector1); + + vector = vector; // This shouldn't throw an exception + succeed_(); // The test succeed if no exception have been thrown + } + catch(MathematicsException& ex) { + fail_("A valid vector assignment throws an excception"); // The test failed if an exception has been thrown + } + + // Try to compute an invalid assignment (vectors with different dimensions) + try { + Vector vector3(2); + vector3 = vector2; // This should throw an exception + fail_("An invalid vector assignment undetected"); // The test failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + + // Try to compute an invalid assignment (vectors with different dimensions) + try { + Vector vector3(2); + vector2 = vector3; // This should throw an exception + fail_("An invalid vector assignment undetected"); // The test failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + } + + // Test operator==() + void testOperatorEquality() { + Vector vector1(2); + vector1.setValue(0, 4); + vector1.setValue(1, 7); + + Vector vector2(2); + vector2.setValue(0, 4); + vector2.setValue(1, 7); + + Vector vector3(2); + vector3.setValue(0, 5); + vector3.setValue(1, 7); + + Vector vector4(8); + + // Try to test a valid equality (two vectors with the same dimensions) + try { + test_(vector1 == vector2); // This shouldn't throw an exception + test_(vector2 == vector1); // This shouldn't throw an exception + test_(vector1 == vector1); // This shouldn't throw an exception + test_(!(vector1 == vector3)); // This shouldn't throw an exception + test_(!(vector3 == vector1)); // This shouldn't throw an exception + succeed_(); // The test succeed if no exception have been thrown + } + catch(MathematicsException& ex) { + fail_("A valid vector equality test throws an excception"); // The test failed if an exception has been thrown + } + + // Try to test an invalid equality (vectors with different dimensions) + try { + vector4 == vector1; // This should throw an exception + fail_("An invalid equality test of two vectors undetected"); // The test failed if no exception have been thrown + } + catch(MathematicsException& ex) { + succeed_(); // The test succeed if an exception has been thrown + } + } +}; + +#endif