365 lines
9.5 KiB
C++
365 lines
9.5 KiB
C++
#include "gtest/gtest.h"
|
|
#include "pimathvector.h"
|
|
|
|
bool cmpVectorWithValue(PIMathVector<double> vector, double val, int num) {
|
|
bool b = true;
|
|
for(int i = 0; i < num; i++) {
|
|
if(vector[i] != val) {
|
|
b = false;
|
|
}
|
|
}
|
|
return b;
|
|
}
|
|
|
|
TEST(PIMathVector_Test, size) {
|
|
auto vector = PIMathVector<double>(3u);
|
|
ASSERT_TRUE(vector.size() == 3u);
|
|
}
|
|
|
|
TEST(PIMathVector_Test, resize) {
|
|
PIMathVector<double> vector;
|
|
vector.resize(4u, 5.0);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector, 5.0, vector.size()));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, resized) {
|
|
PIMathVector<double> vector;
|
|
vector.resized(4u, 5.0);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector, 5.0, vector.size()));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, fill) {
|
|
PIMathVector<double> vector(3u);
|
|
vector.fill(5.0);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector, 5.0, 3u));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, moveVal) {
|
|
PIMathVector<double> vector(3u);
|
|
vector.fill(5.0);
|
|
vector.move(5.0);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector, 10.0, 3u));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, moveVec) {
|
|
PIMathVector<double> vector(3u);
|
|
PIMathVector<double> vec(3u);
|
|
vector.fill(5.0);
|
|
vec.fill(7.0);
|
|
vector.move(vec);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector, 12.0, 3u));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, swap) {
|
|
PIMathVector<double> vector(3u);
|
|
double a[3];
|
|
vector[0] = 5.12;
|
|
vector[1] = 3.32;
|
|
vector[2] = 7.12;
|
|
a[0] = vector[0];
|
|
a[1] = vector[1];
|
|
a[2] = vector[2];
|
|
vector.swap(0u, 1u);
|
|
ASSERT_TRUE((a[0] == vector[1]) && (a[1] == vector[0]) && (a[2] == vector[2]));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, lengthSqr) {
|
|
PIMathVector<double> vector(3u);
|
|
vector.fill(1.0);
|
|
ASSERT_EQ(3.0, vector.lengthSqr());
|
|
}
|
|
|
|
TEST(PIMathVector_Test, length) {
|
|
PIMathVector<double> vector(3u);
|
|
vector.fill(1.0);
|
|
ASSERT_DOUBLE_EQ(sqrt(3.0), vector.length());
|
|
}
|
|
|
|
TEST(PIMathVector_Test, manhattanLength) {
|
|
PIMathVector<double> vector(3u);
|
|
vector.fill(5.0);
|
|
ASSERT_DOUBLE_EQ(15.0, vector.manhattanLength());
|
|
}
|
|
|
|
TEST(PIMathVector_Test, angleCos) {
|
|
PIMathVector<double> vector(3u);
|
|
PIMathVector<double> vec(3u);
|
|
vector[0] = 1.0;
|
|
vector[1] = 1.0;
|
|
vec[1] = 1.0;
|
|
ASSERT_DOUBLE_EQ(cos(0.78539816339744830961566084581988), vector.angleCos(vec));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, angleSin) {
|
|
PIMathVector<double> vector(3u);
|
|
PIMathVector<double> vec(3u);
|
|
vector[0] = 1.0;
|
|
vector[1] = 1.0;
|
|
vec[1] = 1.0;
|
|
ASSERT_DOUBLE_EQ(cos(0.78539816339744830961566084581988), vector.angleSin(vec));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, angleRad) {
|
|
PIMathVector<double> vector(3u);
|
|
PIMathVector<double> vec(3u);
|
|
vector[0] = 1.0;
|
|
vector[1] = 1.0;
|
|
vec[1] = 1.0;
|
|
ASSERT_DOUBLE_EQ(0.78539816339744830961566084581988, vector.angleRad(vec));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, angleDeg) {
|
|
PIMathVector<double> vector(3u);
|
|
PIMathVector<double> vec(3u);
|
|
vector[0] = 1.0;
|
|
vector[1] = 1.0;
|
|
vec[1] = 1.0;
|
|
ASSERT_DOUBLE_EQ(45.0, vector.angleDeg(vec));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, projection) {
|
|
PIMathVector<double> vector(2u);
|
|
PIMathVector<double> vec(2u);
|
|
vec[0] = 1.0;
|
|
vector[0] = 1.0;
|
|
vector[1] = 1.0;
|
|
auto vecProj = vector.projection(vec);
|
|
ASSERT_TRUE(vecProj == vec);
|
|
}
|
|
|
|
TEST(PIMathVector_Test, normalize) {
|
|
PIMathVector<double> vector(3u);
|
|
vector.fill(5.0);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector.normalize(), 5.0 / sqrt(75.0), 3u));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, normalized) {
|
|
PIMathVector<double> vector(3u);
|
|
vector.fill(5.0);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector.normalized(), 5.0 / sqrt(75.0), 3u));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, isNullTrue) {
|
|
PIMathVector<double> vector(3u);
|
|
ASSERT_TRUE(vector.isNull());
|
|
}
|
|
|
|
TEST(PIMathVector_Test, isNullFalse) {
|
|
PIMathVector<double> vector(3u);
|
|
vector[0] = 6.273;
|
|
ASSERT_FALSE(vector.isNull());
|
|
}
|
|
|
|
TEST(PIMathVector_Test, isValidTrue) {
|
|
PIMathVector<double> vector(3u);
|
|
ASSERT_TRUE(vector.isValid());
|
|
}
|
|
|
|
TEST(PIMathVector_Test, isValidFalse) {
|
|
PIMathVector<double> vector;
|
|
ASSERT_FALSE(vector.isValid());
|
|
}
|
|
|
|
TEST(PIMathVector_Test, isOrthoTrue) {
|
|
PIMathVector<double> vector(2u);
|
|
PIMathVector<double> vect(2u);
|
|
vector[0] = 2.0;
|
|
vect[1] = 1.0;
|
|
ASSERT_TRUE(vector.isOrtho(vect));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, isOrthoFalse) {
|
|
PIMathVector<double> vector(2u);
|
|
PIMathVector<double> vect(2u);
|
|
vector[0] = 2.0;
|
|
vect[0] = 5.0;
|
|
vect[1] = 1.0;
|
|
ASSERT_FALSE(vector.isOrtho(vect));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, at) {
|
|
PIMathVector<double> vector(3u);
|
|
vector.fill(5.5);
|
|
for(uint i = 0; i < 3u; i++){
|
|
if(vector.at(i) != 5.5){
|
|
ASSERT_TRUE(false);
|
|
}
|
|
}
|
|
ASSERT_TRUE(true);
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_AssignmentValue) {
|
|
PIMathVector<double> vector(3u);
|
|
vector = 3.0;
|
|
ASSERT_TRUE(cmpVectorWithValue(vector, 3.0, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_AssignmentVector) {
|
|
PIMathVector<double> vector(3u);
|
|
PIMathVector<double> vec(3u);
|
|
vec = 5.0;
|
|
vector = vec;
|
|
ASSERT_TRUE(cmpVectorWithValue(vector, 5.0, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_EqualTrue) {
|
|
PIMathVector<double> vector(2u);
|
|
PIMathVector<double> vec(2u);
|
|
vector[0] = 5.12;
|
|
vector[1] = 7.34;
|
|
vec[0] = 5.12;
|
|
vec[1] = 7.34;
|
|
ASSERT_TRUE(vec == vector);
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_EqualFalse) {
|
|
PIMathVector<double> vector(2u);
|
|
PIMathVector<double> vec(2u);
|
|
vector[0] = 5.12;
|
|
vector[1] = 7.34;
|
|
vec[0] = 5.12;
|
|
vec[1] = 0.34;
|
|
ASSERT_FALSE(vec == vector);
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_Not_EqualTrue) {
|
|
PIMathVector<double> vector(2u);
|
|
PIMathVector<double> vec(2u);
|
|
vector[0] = 5.12;
|
|
vector[1] = 7.34;
|
|
vec[0] = 5.12;
|
|
vec[1] = 0.34;
|
|
ASSERT_TRUE(vec != vector);
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_Not_EqualFalse) {
|
|
PIMathVector<double> vector(2u);
|
|
PIMathVector<double> vec(2u);
|
|
vector[0] = 5.12;
|
|
vector[1] = 7.34;
|
|
vec[0] = 5.12;
|
|
vec[1] = 7.34;
|
|
ASSERT_FALSE(vec != vector);
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_Addition_Aassignment) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1.fill(6.0);
|
|
vector2.fill(1.72);
|
|
vector1 += vector2;
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1, 7.72, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_Subtraction_Assignment) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1.fill(6.0);
|
|
vector2.fill(1.72);
|
|
vector1 -= vector2;
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1, 4.28, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_Multiplication_AssignmentValue) {
|
|
PIMathVector<double> vector1(3u);
|
|
vector1.fill(6.0);
|
|
vector1 *= 4.0;
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1, 24.0, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_Multiplication_AssignmentVector) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1.fill(6.0);
|
|
vector2.fill(1.72);
|
|
vector1 *= vector2;
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1, 10.32, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_Division_AssignmentValue) {
|
|
PIMathVector<double> vector1(3u);
|
|
vector1.fill(6.0);
|
|
vector1 /= 4.0;
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1, 1.5, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_Division_AssignmentVector) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1.fill(6.0);
|
|
vector2.fill(1.5);
|
|
vector1 /= vector2;
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1, 4.0, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_Addition) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1.fill(6.0);
|
|
vector2.fill(1.72);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1 + vector2, 7.72, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_Subtraction) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1.fill(6.0);
|
|
vector2.fill(1.72);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1 - vector2, 4.28, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_MultiplicationValue) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1.fill(6.0);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1 * 4.0, 24.0, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_MultiplicationVector1) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1.fill(6.0);
|
|
vector2.fill(1.72);
|
|
ASSERT_TRUE(cmpVectorWithValue((vector1 * vector2), 0.0, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_MultiplicationVector2) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1[0] = 1.0;
|
|
vector2[1] = 1.0;
|
|
ASSERT_TRUE(((vector1 * vector2)[0] == 0.0) && ((vector1 * vector2)[1] == 0.0) && ((vector1 * vector2)[2] == 1.0));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_DivisionVector) {
|
|
PIMathVector<double> vector1(3u);
|
|
vector1.fill(6.0);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1 / 4.0, 1.5, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_MultiplVect) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1.fill(6.0);
|
|
vector2.fill(5.0);
|
|
ASSERT_TRUE(cmpVectorWithValue(vector1 & vector2, 30.0, 3));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, operator_absDotProduct) {
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vector1.fill(6.0);
|
|
vector2.fill(5.0);
|
|
ASSERT_TRUE(90.0 == (vector1 ^ vector2));
|
|
}
|
|
|
|
TEST(PIMathVector_Test, distToLine) {
|
|
PIMathVector<double> vect(3u);
|
|
PIMathVector<double> vector1(3u);
|
|
PIMathVector<double> vector2(3u);
|
|
vect.fill(6.0);
|
|
vector1[0] = 1.0;
|
|
vector2[1] = -1.0;
|
|
ASSERT_DOUBLE_EQ(vect.distToLine(vector1, vector2), sqrt(2.0)/2.0);
|
|
}
|