Skip to content

Commit

Permalink
Vector3 now has 100% test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
jspark311 committed Oct 5, 2021
1 parent 41a8039 commit 940dc82
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 21 deletions.
130 changes: 113 additions & 17 deletions extras/unit_tests/TestDataStructures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,28 +787,124 @@ int test_KeyValuePair() {
* @return 0 on success. Non-zero on failure.
*/
int vector3_float_test() {
float x = 0.7f;
float y = 0.8f;
float z = 0.01f;
StringBuilder log("===< Vector3<float> >===================================\n");
Vector3<float> test;
Vector3<float> test_vect_0(-0.4f, -0.1f, 0.4f);
Vector3<float> *test1 = &test_vect_0;
log.concatf("\t (test) (%.4f, %.4f, %.4f)\n", (double)(test.x), (double)(test.y), (double)(test.z));
int return_value = -1;
bool print_vectors = true;
Vector3<float>* print0 = nullptr;
Vector3<float>* print1 = nullptr;
Vector3<float>* print2 = nullptr;

test(1.0f, 0.5f, 0.24f);
log.concatf("\t (test) (%.4f, %.4f, %.4f)\n", (double)(test.x), (double)(test.y), (double)(test.z));
Vector3<float> x_axis(1, 0, 0);
Vector3<float> y_axis(0, 1, 0);
Vector3<float> z_axis(0, 0, 1);

test(test_vect_0.x, test_vect_0.y, test_vect_0.z);
log.concatf("\t (test) (%.4f, %.4f, %.4f)\n", (double)(test.x), (double)(test.y), (double)(test.z));
StringBuilder log("===< Vector3<float> >===================================\n");
float x0 = randomUInt32()/(float)randomUInt32();
float y0 = randomUInt32()/(float)randomUInt32();
float z0 = randomUInt32()/(float)randomUInt32();
float x1 = randomUInt32()/(float)randomUInt32();
float y1 = randomUInt32()/(float)randomUInt32();
float z1 = randomUInt32()/(float)randomUInt32();
float x2 = randomUInt32()/(float)randomUInt32();
float y2 = randomUInt32()/(float)randomUInt32();
float z2 = randomUInt32()/(float)randomUInt32();
float x3 = randomUInt32()/(float)randomUInt32();
float y3 = randomUInt32()/(float)randomUInt32();
float z3 = randomUInt32()/(float)randomUInt32();
float xR0 = x0 + x1;
float yR0 = y0 + y1;
float zR0 = z0 + z1;
float xR1 = x0 * 5;
float yR1 = y0 * 5;
float zR1 = z0 * 5;
float xR2 = x0 - x1;
float yR2 = y0 - y1;
float zR2 = z0 - z1;
Vector3<float> test;
Vector3<float> test_vect_0(x0, y0, z0);
Vector3<float> test_vect_1(x1, y1, z1);
Vector3<float> test_vect_2(x2, y2, z2);
Vector3<float> test_vect_4(x2, y2, z2);
Vector3<float> test_vect_3(x3, y3, z3);
Vector3<float> test_vect_5;
test_vect_5.set(x0, y0, z0);

Vector3<float> result_vect_0 = test_vect_0 + test_vect_1;
Vector3<float> result_vect_1 = test_vect_0 * 5;
Vector3<float> result_vect_2 = test_vect_0 - test_vect_1;
Vector3<float> result_vect_3(x3, y3, z3);
Vector3<float> result_vect_5;
result_vect_5.set(&test_vect_0);
result_vect_5 += test_vect_1;

float length_r_0 = test_vect_0.length();
float length_r_1 = test_vect_1.length();

print0 = &test_vect_0;
print1 = &test_vect_1;
print2 = &result_vect_0;

if ((result_vect_0.x == xR0) && (result_vect_0.y == yR0) && (result_vect_0.z == zR0)) {
print2 = &result_vect_5;
if ((result_vect_5.x == xR0) && (result_vect_5.y == yR0) && (result_vect_5.z == zR0)) {
print2 = &result_vect_1;
if ((result_vect_1.x == xR1) && (result_vect_1.y == yR1) && (result_vect_1.z == zR1)) {
print2 = &result_vect_2;
if ((result_vect_2.x == xR2) && (result_vect_2.y == yR2) && (result_vect_2.z == zR2)) {
float length_r_3 = test_vect_3.length();
float scalar_0 = test_vect_3.normalize();
if (1000 == round(1000 * test_vect_3.length())) {
if (1000 == round(1000 * scalar_0*length_r_3)) {
if (result_vect_3 != test_vect_3) {
result_vect_3.normalize(); // Independently normalized vector.
if (result_vect_3 == test_vect_3) {
float angle_0 = Vector3<float>::angle_normalized(result_vect_3, test_vect_3);
if (0 == round(1000 * angle_0)) {
test_vect_2.reflect(x_axis);
float angle_1 = Vector3<float>::angle(test_vect_2, x_axis);
float angle_2 = Vector3<float>::angle(test_vect_2, test_vect_4);
if (round(1000 * angle_1*2) == round(1000 * angle_2)) {
const float RENORM_SCALAR = 6.5f;
result_vect_3 *= RENORM_SCALAR; // Stretch.
if ((RENORM_SCALAR*1000) == round(1000 * result_vect_3.length())) {
// Normalize to a given length.
result_vect_3.normalize(result_vect_3.length());
if (1000 == round(1000 * result_vect_3.length())) {
log.concat("Vector3 tests pass.\n");
print_vectors = false;
return_value = 0;
}
else log.concat("Failed vector Scaling/renormalizing.\n");
}
else log.concatf("Scaled vector should be length 6.5, but got %.3f.\n", result_vect_3.length());
}
else log.concatf("The angle between vector0 and its reflection about vector1 should be twice the angle between vector0 nad vector1, but got %.3f and %.3f, respectively.\n", angle_1, angle_2);
}
else log.concatf("The angle between two equal vectors should be 0.0, but got %.3f.\n", angle_0);
}
else log.concat("Failed vector equality test.\n");
}
else log.concat("Failed vector inequality test.\n");
}
else log.concatf("The scalar value returned by normalize (%.3f) doesn't comport with the original length (%.3f).\n", scalar_0, length_r_3);
}
else log.concatf("Normalized vector should be length 1.0, but got %.3f.\n", test_vect_3.length());
}
else log.concat("Failed vector subtraction.\n");
}
else log.concat("Failed vector multiplication.\n");
}
else log.concat("Failed test of += operator.\n");
}
else log.concat("Failed vector addition.\n");

test(test1->x, test1->y, test1->z);
log.concatf("\t (test) (%.4f, %.4f, %.4f)\n", (double)(test.x), (double)(test.y), (double)(test.z));
if (print_vectors) {
log.concatf("\top0: (%.4f, %.4f, %.4f)\n", (double)(print0->x), (double)(print0->y), (double)(print0->z));
log.concatf("\top1: (%.4f, %.4f, %.4f)\n", (double)(print1->x), (double)(print1->y), (double)(print1->z));
log.concatf("\tresult: (%.4f, %.4f, %.4f)\n", (double)(print2->x), (double)(print2->y), (double)(print2->z));
}

test(x, y, z);
log.concatf("\t (test) (%.4f, %.4f, %.4f)\n", (double)(test.x), (double)(test.y), (double)(test.z));
printf("%s\n\n", (const char*) log.string());
return 0;
return return_value;
}


Expand Down
8 changes: 4 additions & 4 deletions src/Vector3.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,19 @@ template <typename T> class Vector3 {
*this *=stacked_len;
}
}
return stacked_len;
return stacked_len;
}

// Normalize against the supllied length, for code that caches this information.
// Set function with components broken out.
void set(T _x, T _y, T _z) {
x = _x;
y = _y;
z = _z;
}

// Normalize against the supllied length, for code that caches this information.
// Normalize against the supplied length, for code that caches this information.
void normalize(float len) {
if (len) *this/=len;
if (len) *this/=len;
}

// returns the normalized version of this vector
Expand Down

0 comments on commit 940dc82

Please sign in to comment.