Small changes to C API unit tests.

- (1) Changed randomized math structure values to predefined values to prevent cases that could potentially lead to division by zero.
- (2) Removed unused variable(s) due to (1).
- (3) Renamed variable(s) for better clarity.
pull/3147/head
Marc-Antoine Lortie 2020-04-13 14:13:54 -04:00
parent ac16a857ff
commit e399a12f71
7 changed files with 52 additions and 35 deletions

View File

@ -143,7 +143,8 @@ TEST_F(AssimpAPITest_aiMatrix3x3, aiMatrix3TranslationTest) {
} }
TEST_F(AssimpAPITest_aiMatrix3x3, aiMatrix3FromToTest) { TEST_F(AssimpAPITest_aiMatrix3x3, aiMatrix3FromToTest) {
const auto from = random_vec3(), to = random_vec3(); // Use predetermined vectors to prevent running into division by zero.
const auto from = aiVector3D(1,2,1).Normalize(), to = aiVector3D(-1,1,1).Normalize();
aiMatrix3x3::FromToMatrix(from, to, result_cpp); aiMatrix3x3::FromToMatrix(from, to, result_cpp);
aiMatrix3FromTo(&result_c, &from, &to); aiMatrix3FromTo(&result_c, &from, &to);
EXPECT_EQ(result_cpp, result_c); EXPECT_EQ(result_cpp, result_c);

View File

@ -51,6 +51,16 @@ protected:
result_c = result_cpp = aiMatrix4x4(); result_c = result_cpp = aiMatrix4x4();
} }
/* Generates a predetermined transformation matrix to use
for the aiDecompose functions to prevent running into
division by zero. */
aiMatrix4x4 get_predetermined_transformation_matrix_for_decomposition() const {
aiMatrix4x4 t, r;
aiMatrix4x4::Translation(aiVector3D(14,-25,-8), t);
aiMatrix4x4::Rotation(Math::PI<float>() / 4.0f, aiVector3D(1).Normalize(), r);
return t * r;
}
aiMatrix4x4 result_c, result_cpp; aiMatrix4x4 result_c, result_cpp;
}; };
@ -142,7 +152,7 @@ TEST_F(AssimpAPITest_aiMatrix4x4, aiDecomposeMatrixTest) {
position_c, position_cpp; position_c, position_cpp;
aiQuaternion rotation_c, rotation_cpp; aiQuaternion rotation_c, rotation_cpp;
result_c = result_cpp = random_mat4(); result_c = result_cpp = get_predetermined_transformation_matrix_for_decomposition();
result_cpp.Decompose(scaling_cpp, rotation_cpp, position_cpp); result_cpp.Decompose(scaling_cpp, rotation_cpp, position_cpp);
aiDecomposeMatrix(&result_c, &scaling_c, &rotation_c, &position_c); aiDecomposeMatrix(&result_c, &scaling_c, &rotation_c, &position_c);
EXPECT_EQ(scaling_cpp, scaling_c); EXPECT_EQ(scaling_cpp, scaling_c);
@ -155,7 +165,7 @@ TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4DecomposeIntoScalingEulerAnglesPositi
rotation_c, rotation_cpp, rotation_c, rotation_cpp,
position_c, position_cpp; position_c, position_cpp;
result_c = result_cpp = random_mat4(); result_c = result_cpp = get_predetermined_transformation_matrix_for_decomposition();
result_cpp.Decompose(scaling_cpp, rotation_cpp, position_cpp); result_cpp.Decompose(scaling_cpp, rotation_cpp, position_cpp);
aiMatrix4DecomposeIntoScalingEulerAnglesPosition(&result_c, &scaling_c, &rotation_c, &position_c); aiMatrix4DecomposeIntoScalingEulerAnglesPosition(&result_c, &scaling_c, &rotation_c, &position_c);
EXPECT_EQ(scaling_cpp, scaling_c); EXPECT_EQ(scaling_cpp, scaling_c);
@ -169,7 +179,7 @@ TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4DecomposeIntoScalingAxisAnglePosition
position_c, position_cpp; position_c, position_cpp;
float angle_c, angle_cpp; float angle_c, angle_cpp;
result_c = result_cpp = random_mat4(); result_c = result_cpp = get_predetermined_transformation_matrix_for_decomposition();
result_cpp.Decompose(scaling_cpp, axis_cpp, angle_cpp, position_cpp); result_cpp.Decompose(scaling_cpp, axis_cpp, angle_cpp, position_cpp);
aiMatrix4DecomposeIntoScalingAxisAnglePosition(&result_c, &scaling_c, &axis_c, &angle_c, &position_c); aiMatrix4DecomposeIntoScalingAxisAnglePosition(&result_c, &scaling_c, &axis_c, &angle_c, &position_c);
EXPECT_EQ(scaling_cpp, scaling_c); EXPECT_EQ(scaling_cpp, scaling_c);
@ -182,7 +192,7 @@ TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4DecomposeNoScalingTest) {
aiVector3D position_c, position_cpp; aiVector3D position_c, position_cpp;
aiQuaternion rotation_c, rotation_cpp; aiQuaternion rotation_c, rotation_cpp;
result_c = result_cpp = random_mat4(); result_c = result_cpp = get_predetermined_transformation_matrix_for_decomposition();
result_cpp.DecomposeNoScaling(rotation_cpp, position_cpp); result_cpp.DecomposeNoScaling(rotation_cpp, position_cpp);
aiMatrix4DecomposeNoScaling(&result_c, &rotation_c, &position_c); aiMatrix4DecomposeNoScaling(&result_c, &rotation_c, &position_c);
EXPECT_EQ(position_cpp, position_c); EXPECT_EQ(position_cpp, position_c);
@ -242,7 +252,8 @@ TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4ScalingTest) {
} }
TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4FromToTest) { TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4FromToTest) {
const auto from = random_vec3(), to = random_vec3(); // Use predetermined vectors to prevent running into division by zero.
const auto from = aiVector3D(1,2,1).Normalize(), to = aiVector3D(-1,1,1).Normalize();
aiMatrix4x4::FromToMatrix(from, to, result_cpp); aiMatrix4x4::FromToMatrix(from, to, result_cpp);
aiMatrix4FromTo(&result_c, &from, &to); aiMatrix4FromTo(&result_c, &from, &to);
EXPECT_EQ(result_cpp, result_c); EXPECT_EQ(result_cpp, result_c);

View File

@ -55,7 +55,13 @@ protected:
}; };
TEST_F(AssimpAPITest_aiQuaternion, aiCreateQuaternionFromMatrixTest) { TEST_F(AssimpAPITest_aiQuaternion, aiCreateQuaternionFromMatrixTest) {
const auto m = random_mat3(); // Use a predetermined transformation matrix
// to prevent running into division by zero.
aiMatrix3x3 m, r;
aiMatrix3x3::Translation(aiVector2D(14,-25), m);
aiMatrix3x3::RotationZ(Math::PI<float>() / 4.0f, r);
m = m * r;
result_cpp = aiQuaternion(m); result_cpp = aiQuaternion(m);
aiCreateQuaternionFromMatrix(&result_c, &m); aiCreateQuaternionFromMatrix(&result_c, &m);
EXPECT_EQ(result_cpp, result_c); EXPECT_EQ(result_cpp, result_c);
@ -118,9 +124,11 @@ TEST_F(AssimpAPITest_aiQuaternion, aiQuaternionMultiplyTest) {
} }
TEST_F(AssimpAPITest_aiQuaternion, aiQuaternionInterpolateTest) { TEST_F(AssimpAPITest_aiQuaternion, aiQuaternionInterpolateTest) {
const float INTERPOLATION(RandUnit.next()); // Use predetermined quaternions to prevent division by zero
const auto q1 = random_quat(); // during slerp calculations.
const auto q2 = random_quat(); const float INTERPOLATION(0.5f);
const auto q1 = aiQuaternion(aiVector3D(-1,1,1).Normalize(), Math::PI<float>() / 4.0f);
const auto q2 = aiQuaternion(aiVector3D(1,2,1).Normalize(), Math::PI<float>() / 2.0f);
aiQuaternion::Interpolate(result_cpp, q1, q2, INTERPOLATION); aiQuaternion::Interpolate(result_cpp, q1, q2, INTERPOLATION);
aiQuaternionInterpolate(&result_c, &q1, &q2, INTERPOLATION); aiQuaternionInterpolate(&result_c, &q1, &q2, INTERPOLATION);
EXPECT_EQ(result_cpp, result_c); EXPECT_EQ(result_cpp, result_c);

View File

@ -49,7 +49,7 @@ class AssimpAPITest_aiVector2D : public AssimpMathTest {
protected: protected:
virtual void SetUp() { virtual void SetUp() {
result_c = result_cpp = aiVector2D(); result_c = result_cpp = aiVector2D();
temp = random_vec2(); temp = random_vec2(); // Generates a random 2D vector != null vector.
} }
aiVector2D result_c, result_cpp, temp; aiVector2D result_c, result_cpp, temp;
@ -82,7 +82,7 @@ TEST_F(AssimpAPITest_aiVector2D, aiVector2SubtractTest) {
} }
TEST_F(AssimpAPITest_aiVector2D, aiVector2ScaleTest) { TEST_F(AssimpAPITest_aiVector2D, aiVector2ScaleTest) {
const float FACTOR = Rand.next(); const float FACTOR = RandNonZero.next();
result_c = result_cpp = random_vec2(); result_c = result_cpp = random_vec2();
result_cpp *= FACTOR; result_cpp *= FACTOR;
aiVector2Scale(&result_c, FACTOR); aiVector2Scale(&result_c, FACTOR);
@ -97,7 +97,7 @@ TEST_F(AssimpAPITest_aiVector2D, aiVector2SymMulTest) {
} }
TEST_F(AssimpAPITest_aiVector2D, aiVector2DivideByScalarTest) { TEST_F(AssimpAPITest_aiVector2D, aiVector2DivideByScalarTest) {
const float DIVISOR = Rand.next(); const float DIVISOR = RandNonZero.next();
result_c = result_cpp = random_vec2(); result_c = result_cpp = random_vec2();
result_cpp /= DIVISOR; result_cpp /= DIVISOR;
aiVector2DivideByScalar(&result_c, DIVISOR); aiVector2DivideByScalar(&result_c, DIVISOR);

View File

@ -49,7 +49,7 @@ class AssimpAPITest_aiVector3D : public AssimpMathTest {
protected: protected:
virtual void SetUp() { virtual void SetUp() {
result_c = result_cpp = aiVector3D(); result_c = result_cpp = aiVector3D();
temp = random_vec3(); temp = random_vec3(); // Generates a random 3D vector != null vector.
} }
aiVector3D result_c, result_cpp, temp; aiVector3D result_c, result_cpp, temp;
@ -88,7 +88,7 @@ TEST_F(AssimpAPITest_aiVector3D, aiVector3SubtractTest) {
} }
TEST_F(AssimpAPITest_aiVector3D, aiVector3ScaleTest) { TEST_F(AssimpAPITest_aiVector3D, aiVector3ScaleTest) {
const float FACTOR = Rand.next(); const float FACTOR = RandNonZero.next();
result_c = result_cpp = random_vec3(); result_c = result_cpp = random_vec3();
result_cpp *= FACTOR; result_cpp *= FACTOR;
aiVector3Scale(&result_c, FACTOR); aiVector3Scale(&result_c, FACTOR);
@ -103,7 +103,7 @@ TEST_F(AssimpAPITest_aiVector3D, aiVector3SymMulTest) {
} }
TEST_F(AssimpAPITest_aiVector3D, aiVector3DivideByScalarTest) { TEST_F(AssimpAPITest_aiVector3D, aiVector3DivideByScalarTest) {
const float DIVISOR = Rand.next(); const float DIVISOR = RandNonZero.next();
result_c = result_cpp = random_vec3(); result_c = result_cpp = random_vec3();
result_cpp /= DIVISOR; result_cpp /= DIVISOR;
aiVector3DivideByScalar(&result_c, DIVISOR); aiVector3DivideByScalar(&result_c, DIVISOR);

View File

@ -47,13 +47,10 @@ namespace Assimp {
// Initialize epsilon value. // Initialize epsilon value.
const float AssimpMathTest::Epsilon = Math::getEpsilon<float>(); const float AssimpMathTest::Epsilon = Math::getEpsilon<float>();
// Initialize with an interval of [1,100] to avoid null values. // Initialize with an interval of [1,100].
RandomUniformFloatGenerator AssimpMathTest::Rand(1.0f, 100.0f); RandomUniformFloatGenerator AssimpMathTest::RandNonZero(1.0f, 100.0f);
// Initialize with an interval of [-PI,PI] inclusively. // Initialize with an interval of [-PI,PI] inclusively.
RandomUniformFloatGenerator AssimpMathTest::RandPI(-Math::PI<float>(), Math::PI<float>()); RandomUniformFloatGenerator AssimpMathTest::RandPI(-Math::PI<float>(), Math::PI<float>());
// Initialize with an interval of [0,1] inclusively.
RandomUniformFloatGenerator AssimpMathTest::RandUnit(0.0f, 1.0f);
} }

View File

@ -53,14 +53,14 @@ namespace Assimp {
/** Custom test class providing several math related utilities. */ /** Custom test class providing several math related utilities. */
class AssimpMathTest : public ::testing::Test { class AssimpMathTest : public ::testing::Test {
public: public:
/** Return a random 2D vector. */ /** Return a random non-null 2D vector. */
inline static aiVector2D random_vec2() { inline static aiVector2D random_vec2() {
return aiVector2D(Rand.next(), Rand.next()); return aiVector2D(RandNonZero.next(), RandNonZero.next());
} }
/** Return a random 3D vector. */ /** Return a random non-null 3D vector. */
inline static aiVector3D random_vec3() { inline static aiVector3D random_vec3() {
return aiVector3D(Rand.next(), Rand.next(),Rand.next()); return aiVector3D(RandNonZero.next(), RandNonZero.next(),RandNonZero.next());
} }
/** Return a random unit 3D vector. */ /** Return a random unit 3D vector. */
@ -74,28 +74,28 @@ public:
return aiQuaternion(random_unit_vec3(), RandPI.next()); return aiQuaternion(random_unit_vec3(), RandPI.next());
} }
/** Return a random 3x3 matrix. */ /** Return a random non-null 3x3 matrix. */
inline static aiMatrix3x3 random_mat3() { inline static aiMatrix3x3 random_mat3() {
return aiMatrix3x3( return aiMatrix3x3(
Rand.next(), Rand.next(),Rand.next(), RandNonZero.next(), RandNonZero.next(),RandNonZero.next(),
Rand.next(), Rand.next(),Rand.next(), RandNonZero.next(), RandNonZero.next(),RandNonZero.next(),
Rand.next(), Rand.next(),Rand.next()); RandNonZero.next(), RandNonZero.next(),RandNonZero.next());
} }
/** Return a random 4x4 matrix. */ /** Return a random non-null 4x4 matrix. */
inline static aiMatrix4x4 random_mat4() { inline static aiMatrix4x4 random_mat4() {
return aiMatrix4x4( return aiMatrix4x4(
Rand.next(), Rand.next(),Rand.next(), Rand.next(), RandNonZero.next(), RandNonZero.next(),RandNonZero.next(), RandNonZero.next(),
Rand.next(), Rand.next(),Rand.next(), Rand.next(), RandNonZero.next(), RandNonZero.next(),RandNonZero.next(), RandNonZero.next(),
Rand.next(), Rand.next(),Rand.next(), Rand.next(), RandNonZero.next(), RandNonZero.next(),RandNonZero.next(), RandNonZero.next(),
Rand.next(), Rand.next(),Rand.next(), Rand.next()); RandNonZero.next(), RandNonZero.next(),RandNonZero.next(), RandNonZero.next());
} }
/** Epsilon value to use in tests. */ /** Epsilon value to use in tests. */
static const float Epsilon; static const float Epsilon;
/** Random number generators. */ /** Random number generators. */
static RandomUniformFloatGenerator Rand, RandPI, RandUnit; static RandomUniformFloatGenerator RandNonZero, RandPI;
}; };
} }