Files
pip/tests/math/testpimathvectort.cpp

512 lines
13 KiB
C++

#include "gtest/gtest.h"
#include "pimathvector.h"
#include "pimathmatrix.h"
const uint SIZE = 3u;
bool cmpVectorWithValue(PIMathVectorT<SIZE, double> vector, double val, int num) {
bool b = true;
for(int i = 0; i < num; i++) {
if(vector[i] - val >= double(1E-200)) {
b = false;
}
}
return b;
}
TEST(PIMathVectorT_Test, size) {
PIMathVectorT<SIZE, double> vector;
ASSERT_TRUE(vector.size() == SIZE);
}
TEST(PIMathVectorT_Test, fill) {
double a = 5.0;
PIMathVectorT<SIZE, double> vector;
ASSERT_TRUE(cmpVectorWithValue(vector.fill(a), a, SIZE));
}
TEST(PIMathVectorT_Test, set) {
double a = 5.0;
double b = 3.0;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE, double> vector2;
ASSERT_TRUE(cmpVectorWithValue(vector.set(vector1.fill(a), vector2.fill(b)), b - a, SIZE));
}
TEST(PIMathVectorT_Test, MoveVal) {
double a = 4.0;
PIMathVectorT<SIZE, double> vector;
ASSERT_TRUE(cmpVectorWithValue(vector.move(a), a, SIZE));
}
TEST(PIMathVectorT_Test, MoveVector) {
double a = 5.0;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vector1;
ASSERT_TRUE(cmpVectorWithValue(vector.move(vector1.fill(a)), a, SIZE));
}
TEST(PIMathVectorT_Test, lengthSqr) {
double a = 1.0;
PIMathVectorT<SIZE, double> vector;
vector.fill(a);
ASSERT_DOUBLE_EQ(SIZE * a, vector.lengthSqr());
}
TEST(PIMathVectorT_Test, length) {
double a = 1.0;
PIMathVectorT<SIZE, double> vector;
vector.fill(a);
ASSERT_DOUBLE_EQ(sqrt(SIZE * a), vector.length());
}
TEST(PIMathVectorT_Test, manhattanLength) {
double a = 5.0;
PIMathVectorT<SIZE, double> vector;
vector.fill(a);
ASSERT_DOUBLE_EQ(SIZE * a, vector.manhattanLength());
}
TEST(PIMathVectorT_Test, angleCos) {
double a = 1.0;
double angle = 0.78539816339744830961566084581988;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vector[0] = a;
vector[1] = a;
vec[1] = a;
ASSERT_DOUBLE_EQ(cos(angle), vector.angleCos(vec));
}
TEST(PIMathVectorT_Test, angleSin) {
double a = 1.0;
double angle = 0.78539816339744830961566084581988;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vector[0] = a;
vector[1] = a;
vec[1] = a;
ASSERT_DOUBLE_EQ(sin(angle), vector.angleSin(vec));
}
TEST(PIMathVectorT_Test, angleRad) {
double a = 1.0;
double angle = 0.78539816339744830961566084581988;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vector[0] = a;
vector[1] = a;
vec[1] = a;
ASSERT_DOUBLE_EQ(angle, vector.angleRad(vec));
}
TEST(PIMathVectorT_Test, angleDeg) {
double a = 1.0;
double angle = 45.0;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vector[0] = a;
vector[1] = a;
vec[1] = a;
ASSERT_DOUBLE_EQ(angle, vector.angleDeg(vec));
}
TEST(PIMathVectorT_Test, angleElevation) {
double a = 1.0;
double angle = 45.0;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vector[0] = a;
vector[1] = a;
vec[1] = a;
ASSERT_DOUBLE_EQ(-angle, vector.angleElevation(vec));
}
TEST(PIMathVectorT_Test, projection) {
double a = 2.0;
double b = 2.0;
double res = sqrt(32.0);
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vec[0] = a;
vec[2] = b;
vector[0] = a;
vector[1] = b;
vector[2] = a;
auto vecProj = vector.projection(vec);
ASSERT_DOUBLE_EQ(res, vecProj[0]);
ASSERT_DOUBLE_EQ(0.0, vecProj[1]);
ASSERT_DOUBLE_EQ(res, vecProj[2]);
}
TEST(PIMathVectorT_Test, normalize) {
double a = 5.0;
PIMathVectorT<SIZE, double> vector;
vector.fill(a);
ASSERT_TRUE(cmpVectorWithValue(vector.normalize(), a / sqrt(SIZE * a * a), SIZE));
}
TEST(PIMathVectorT_Test, normalized) {
double a = 5.0;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vectorNew;
vector.fill(a);
vectorNew = vector.normalized();
ASSERT_TRUE(cmpVectorWithValue(vectorNew, a / sqrt(SIZE * a * a), SIZE));
}
TEST(PIMathVectorT_Test, cross1) {
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE, double> vector2;
double a = 5.0;
double b = 1.72;
double c = 0.0;
vector1.fill(a);
vector2.fill(b);
ASSERT_TRUE(cmpVectorWithValue(vector1.cross(vector2), c, SIZE));
}
TEST(PIMathVectorT_Test, cross2) {
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE, double> vector2;
double a = 1.0;
vector1[0] = a;
vector2[1] = a;
auto crossVec = vector1 * vector2;
ASSERT_DOUBLE_EQ(crossVec[0], 0.0);
ASSERT_DOUBLE_EQ(crossVec[1], 0.0);
ASSERT_DOUBLE_EQ(crossVec[2], a);
}
TEST(PIMathVectorT_Test, dot) {
double a = 6.0;
double b = 5.0;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE, double> vector2;
vector1.fill(a);
vector2.fill(b);
ASSERT_DOUBLE_EQ(vector1.dot(vector2), SIZE * a * b);
}
TEST(PIMathVectorT_Test, isNullTrue) {
PIMathVectorT<SIZE, double> vector;
ASSERT_TRUE(vector.isNull());
}
TEST(PIMathVectorT_Test, isNullFalse) {
double a = 6.273;
PIMathVectorT<SIZE, double> vector;
vector[0] = a;
ASSERT_FALSE(vector.isNull());
}
TEST(PIMathVectorT_Test, isOrthoTrue) {
double a = 2.0;
double b = 1.0;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vect;
vector[0] = a;
vect[1] = b;
ASSERT_TRUE(vector.isOrtho(vect));
}
TEST(PIMathVectorT_Test, isOrthoFalse) {
double a = 2.0;
double b = 5.0;
double c = 1.0;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vect;
vector[0] = a;
vect[0] = b;
vect[1] = c;
ASSERT_FALSE(vector.isOrtho(vect));
}
TEST(PIMathVectorT_Test, at) {
double a = 5.5;
PIMathVectorT<SIZE, double> vector;
vector.fill(a);
for(uint i = 0; i < SIZE; i++){
if(vector.at(i) != a){
ASSERT_TRUE(false);
}
}
ASSERT_TRUE(true);
}
TEST(PIMathVectorT_Test, operator_AssignmentValue) {
double a = 3.0;
PIMathVectorT<SIZE, double> vector;
vector = a;
ASSERT_TRUE(cmpVectorWithValue(vector, a, SIZE));
}
TEST(PIMathVectorT_Test, operator_AssignmentVector) {
double a = 5.0;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vec = a;
vector = vec;
ASSERT_TRUE(cmpVectorWithValue(vector, a, SIZE));
}
TEST(PIMathVectorT_Test, operator_EqualTrue) {
double a = 5.12;
double b = 7.34;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vector[0] = a;
vector[1] = b;
vec[0] = a;
vec[1] = b;
ASSERT_TRUE(vec == vector);
}
TEST(PIMathVectorT_Test, operator_EqualFalse) {
double a = 5.12;
double b = 7.34;
double c = 0.34;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vector[0] = a;
vector[1] = b;
vec[0] = a;
vec[1] = c;
ASSERT_FALSE(vec == vector);
}
TEST(PIMathVectorT_Test, operator_Not_EqualTrue) {
double a = 5.12;
double b = 7.34;
double c = 0.34;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vector[0] = a;
vector[1] = b;
vec[0] = a;
vec[1] = c;
ASSERT_TRUE(vec != vector);
}
TEST(PIMathVectorT_Test, operator_Not_EqualFalse) {
double a = 5.12;
double b = 7.34;
PIMathVectorT<SIZE, double> vector;
PIMathVectorT<SIZE, double> vec;
vector[0] = a;
vector[1] = b;
vec[0] = a;
vec[1] = b;
ASSERT_FALSE(vec != vector);
}
TEST(PIMathVectorT_Test, operator_Addition_Aassignment) {
double a = 6.0;
double b = 1.72;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
vector2.fill(b);
vector1 += vector2;
ASSERT_TRUE(cmpVectorWithValue(vector1, a + b, SIZE));
}
TEST(PIMathVectorT_Test, operator_Subtraction_Assignment) {
double a = 6.0;
double b = 1.72;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
vector2.fill(b);
vector1 -= vector2;
ASSERT_TRUE(cmpVectorWithValue(vector1, a - b, SIZE));
}
TEST(PIMathVectorT_Test, operator_Multiplication_AssignmentValue) {
double a = 6.0;
double b = 4.0;
PIMathVectorT<SIZE, double> vector1;
vector1.fill(a);
vector1 *= b;
ASSERT_TRUE(cmpVectorWithValue(vector1, a * b, SIZE));
}
TEST(PIMathVectorT_Test, operator_Multiplication_AssignmentVector) {
double a = 6.0;
double b = 1.72;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
vector2.fill(b);
vector1 *= vector2;
ASSERT_TRUE(cmpVectorWithValue(vector1, a * b, SIZE));
}
TEST(PIMathVectorT_Test, operator_Division_AssignmentValue) {
double a = 6.0;
double b = 4.0;
PIMathVectorT<SIZE, double> vector1;
vector1.fill(a);
vector1 /= b;
ASSERT_TRUE(cmpVectorWithValue(vector1, a / b, SIZE));
}
TEST(PIMathVectorT_Test, operator_Division_AssignmentVector) {
double a = 6.0;
double b = 1.5;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
vector2.fill(b);
vector1 /= vector2;
ASSERT_TRUE(cmpVectorWithValue(vector1, a / b, SIZE));
}
TEST(PIMathVectorT_Test, operator_Addition) {
double a = 6.0;
double b = 1.72;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
vector2.fill(b);
ASSERT_TRUE(cmpVectorWithValue(vector1 + vector2, a + b, SIZE));
}
TEST(PIMathVectorT_Test, operator_Subtraction) {
double a = 6.0;
double b = 1.72;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
vector2.fill(b);
ASSERT_TRUE(cmpVectorWithValue(vector1 - vector2, a - b, SIZE));
}
TEST(PIMathVectorT_Test, operator_MultiplicationValue) {
double a = 6.0;
double b = 4.0;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
ASSERT_TRUE(cmpVectorWithValue(vector1 * b, a * b, SIZE));
}
TEST(PIMathVectorT_Test, operator_MultiplicationVector1) {
double a = 6.0;
double b = 1.72;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
vector2.fill(b);
ASSERT_TRUE(cmpVectorWithValue((vector1 * vector2), 0.0, SIZE));
}
TEST(PIMathVectorT_Test, operator_MultiplicationVector2) {
double a = 1.0;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1[0] = a;
vector2[1] = a;
ASSERT_TRUE(((vector1 * vector2)[0] < double(1E-200)) && ((vector1 * vector2)[1] < double(1E-200)) && ((vector1 * vector2)[2] - a < double(1E-200)));
}
TEST(PIMathVectorT_Test, operator_DivisionVal) {
double a = 6.0;
double b = 4.0;
PIMathVectorT<SIZE, double> vector1;
vector1.fill(a);
ASSERT_TRUE(cmpVectorWithValue(vector1 / b, a / b, SIZE));
}
TEST(PIMathVectorT_Test, operator_DivisionVector) {
double a = 6.0;
double b = 4.0;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE, double> vector2;
vector1.fill(a);
vector2.fill(b);
ASSERT_TRUE(cmpVectorWithValue(vector1 / vector2, a / b, SIZE));
}
TEST(PIMathVectorT_Test, operator_MultiplVect) {
double a = 6.0;
double b = 5.0;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
vector2.fill(b);
ASSERT_TRUE(cmpVectorWithValue(vector1 & vector2, a * b, SIZE));
}
TEST(PIMathVectorT_Test, operator_absDotProduct) {
double a = 6.0;
double b = 5.0;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
vector2.fill(b);
ASSERT_DOUBLE_EQ(vector1 ^ vector2, SIZE * a * b);
}
TEST(PIMathVectorT_Test, transposed) {
double a = 6.0;
PIMathVectorT<SIZE, double> vector;
vector.fill(a);
auto matrix = vector.transposed();
for(int i = 0; i < SIZE; i++){
if(matrix[0][i] != a)
{
ASSERT_TRUE(false);
}
}
ASSERT_TRUE(true);
}
TEST(PIMathVectorT_Test, filled) {
double a = 6.0;
auto vector = PIMathVectorT<SIZE, double>::filled(a);
ASSERT_TRUE(cmpVectorWithValue(vector, a, SIZE));
}
TEST(PIMathVectorT_Test, turnTo) {
double a = 6.0;
PIMathVectorT<SIZE, double> vect;
vect.fill(a);
auto vector = vect.turnTo<2u, double>();
ASSERT_TRUE(vector.size() == 2);
}
TEST(PIMathVectorT_Test, LogicalOrTrue) {
double a = 6.0;
double b = 1.72;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1.fill(a);
vector2.fill(b);
ASSERT_TRUE(vector1 || vector2);
}
TEST(PIMathVectorT_Test, LogicalOrFalse) {
double a = 1.0;
PIMathVectorT<SIZE, double> vector1;
PIMathVectorT<SIZE,double> vector2;
vector1[0] = a;
vector2[1] = a;
ASSERT_FALSE(vector1 || vector2);
}
TEST(PIMathVectorT_Test, sqrt) {
double a = 36.0;
PIMathVectorT<SIZE, double> vector1;
vector1.fill(a);
ASSERT_TRUE(cmpVectorWithValue(sqrt(vector1), sqrt(a), SIZE));
}
TEST(PIMathVectorT_Test, sqr) {
double a = 6.0;
PIMathVectorT<SIZE, double> vector1;
vector1.fill(a);
ASSERT_TRUE(cmpVectorWithValue(sqr(vector1), sqr(a), SIZE));
}