From 0940e8fa440a1accf85ebed7e77e9de303d5ce86 Mon Sep 17 00:00:00 2001 From: maakshishov Date: Fri, 25 Sep 2020 12:36:05 +0300 Subject: [PATCH] documentation and tests bug fix for PIMathVector.h --- libs/main/math/pimathvector.h | 309 +++++++++-------- tests/math/testpimathmatrix.cpp | 2 +- tests/math/testpimathmatrixt.cpp | 2 +- tests/math/testpimathvector.cpp | 467 +++++++++++++++----------- tests/math/testpimathvectort.cpp | 550 ++++++++++++++++++------------- 5 files changed, 740 insertions(+), 590 deletions(-) diff --git a/libs/main/math/pimathvector.h b/libs/main/math/pimathvector.h index 4ea2d8d0..f624b2b2 100644 --- a/libs/main/math/pimathvector.h +++ b/libs/main/math/pimathvector.h @@ -47,7 +47,6 @@ public: /** * @brief Constructor that calls the private resize method * - * @return resized vector of type PIMathMatrixT */ PIMathVectorT() {resize();} @@ -55,7 +54,6 @@ public: * @brief Constructor that fills a vector PIMathVectorT with the values ​​of another vector "PIVector" * * @param val vector of type PIVector which is identified PIMathVectorT - * @return vector of type PIMathVectorT with values ​​of vector val */ PIMathVectorT(const PIVector & val) {resize(); PIMV_FOR(i, 0) c[i] = val[i];} @@ -64,49 +62,48 @@ public: * * @param st vector of type PIMathVectorT * @param fn vector of type PIMathVectorT - * @return vector of type PIMathVectorT with values subtraction vectors "fn" and "st" */ PIMathVectorT(const _CVector & st, const _CVector & fn) {resize(); set(st, fn);} /** - * @brief Method which returns size of the vector + * @brief Method that returns the number of elements contained in the vector * * @return type uint shows number of elements in this vector */ uint size() const {return Size;} /** - * @brief Method that fills a vector with a value + * @brief Method that set this elements to value "v" * * @param v value of which the vector is filled - * @return vector of type PIMathVectorT filled with "v" + * @return reference to this */ _CVector & fill(const Type & v) {PIMV_FOR(i, 0) c[i] = v; return *this;} /** - * @brief Method that fills a vector with the subtraction of two vectors + * @brief Method that set this with the subtraction of two vectors * * @param st vector of type PIMathVectorT * @param fn vector of type PIMathVectorT - * @return vector of type PIMathVectorT with values subtraction vectors "fn" and "st" + * @return reference to this */ _CVector & set(const _CVector & st, const _CVector & fn) {PIMV_FOR(i, 0) c[i] = fn[i] - st[i]; return *this;} /** - * @brief Method that fills a vector with the adittion of vector value and "v" + * @brief Method that sets this using a vector, each element of which is added to the value of "v" * * @param v value of which the vector is filled - * @return vector of type PIMathVectorT with values adittion of vector value and "v" + * @return reference to this */ _CVector & move(const Type & v) {PIMV_FOR(i, 0) c[i] += v; return *this;} /** - * @brief Method that fills a vector with the adittion of vector value and "v" + * @brief Method that sets this with a vector, each element of which is added to each element of the vector "v" * * @param v vector of type PIMathVectorT - * @return vector of type PIMathVectorT with values adittion of vector value and "v" + * @return reference to this */ - _CVector & move(const _CVector & v) {PIMV_FOR(i, 0) c[i] += v[i]; return *this;} + _CVector & move(const _CVector & v) {PIMV_FOR(i, 0) c[i] += v[i]; return *this;} /** * @brief Method that returns sum of the squares of all elements of the vector @@ -116,7 +113,7 @@ public: Type lengthSqr() const {Type tv(0); PIMV_FOR(i, 0) tv += (c[i] * c[i]); return tv;} /** - * @brief Method that returns length of a vector + * @brief Method that returns a scalar physical value equal to the absolute value of vector * * @return value equal to length of a vector */ @@ -131,56 +128,62 @@ public: /** * @brief Method that returns the cos of the current vector and vector "v" + * If the vectors have different dimensions, it returns false * * @param v vector of type PIMathVectorT * @return cos value of the angle between two vectors */ - Type angleCos(const _CVector & v) const {Type tv = v.length() * length(); return (tv == Type(0) ? Type(0) : ((*this) ^ v) / tv);} + Type angleCos(const _CVector & v) const {if(v.size() != Size) return false; Type tv = v.length() * length(); return (tv == Type(0) ? Type(0) : ((*this) ^ v) / tv);} /** - * @brief Method that returns the sin of the current vector and vector "v". Works only with vectors which consists of 3 elements - * + * @brief Method that returns the sin of the current vector and vector "v". Works only with vectors which consists of 3 elements. + * If the vectors have different dimensions, it returns false + * * @param v vector of type PIMathVectorT * @return sin value of the angle between two vector */ - Type angleSin(const _CVector & v) const {Type tv = angleCos(v); return sqrt(Type(1) - tv * tv);} + Type angleSin(const _CVector & v) const {if(v.size() != Size) return false; Type tv = angleCos(v); return sqrt(Type(1) - tv * tv);} /** - * @brief Method that returns the angle between of the current vector and vector "v" in Rad + * @brief Method that returns the angle between of the current vector and vector "v" in Rad. + * If the vectors have different dimensions, it returns false * * @param v vector of type PIMathVectorT * @return value of the angle between two vectors in Rad */ - Type angleRad(const _CVector & v) const {return acos(angleCos(v));} + Type angleRad(const _CVector & v) const {if(v.size() != Size) return false; return acos(angleCos(v));} /** - * @brief Method that returns the angle between of the current vector and vector "v" in Deg + * @brief Method that returns the angle between of the current vector and vector "v" in Deg. + * If the vectors have different dimensions, it returns false * * @param v vector of type PIMathVectorT * @return value of the angle between two vectors in Deg */ - Type angleDeg(const _CVector & v) const {return toDeg(acos(angleCos(v)));} + Type angleDeg(const _CVector & v) const {if(v.size() != Size) return false; return toDeg(acos(angleCos(v)));} /** - * @brief Method that returns the angle elevation between of the current vector and vector "v" in Deg + * @brief Method that returns the angle elevation between of the current vector and vector "v" in Deg. + * If the vectors have different dimensions, it returns false * * @param v vector of type PIMathVectorT * @return value of the angle elevation between two vectors in Deg */ - Type angleElevation(const _CVector & v) const {_CVector z = v - *this; double c = z.angleCos(*this); return 90.0 - acos(c) * rad2deg;} + Type angleElevation(const _CVector & v) const {if(v.size() != Size) return false; _CVector z = v - *this; double c = z.angleCos(*this); return 90.0 - acos(c) * rad2deg;} /** - * @brief Method that returns a vector equal to the projection of the current vector onto the vector "v" + * @brief Method that returns a vector equal to the projection of the current vector onto the vector "v". + * If the vectors have different dimensions, it returns this without changing anything * * @param v vector of type PIMathVectorT * @return vector of type PIMathVectorT equal to the projection of the current vector onto the vector "v" */ - _CVector projection(const _CVector & v) {Type tv = v.length(); return (tv == Type(0) ? _CVector() : v * (((*this) ^ v) / tv));} + _CVector projection(const _CVector & v) {if(v.size() != Size) return _CVector(*this); Type tv = v.length(); return (tv == Type(0) ? _CVector() : v * (((*this) ^ v) / tv));} /** - * @brief Method that returns a normalized vector + * @brief Method that returns this normalized vector * - * @return copy of normalized vector of type PIMathVectorT + * @return reference to this */ _CVector & normalize() {Type tv = length(); if (tv == Type(1)) return *this; if (piAbs(tv) <= Type(1E-100)) {fill(Type(0)); return *this;} PIMV_FOR(i, 0) c[i] /= tv; return *this;} @@ -215,12 +218,13 @@ public: bool isNull() const {PIMV_FOR(i, 0) if (c[i] != Type(0)) return false; return true;} /** - * @brief Method which checks if current vector is orthogonal to vector "v" + * @brief Method which checks if current vector is orthogonal to vector "v". + * If the vectors have different dimensions, it returns false * * @param v vector of type PIMathVectorT * @return true if vectors are orthogonal, else false */ - bool isOrtho(const _CVector & v) const {return ((*this) ^ v) == Type(0);} + bool isOrtho(const _CVector & v) const {if(v.size() != Size) return false; return ((*this) ^ v) == Type(0);} /** * @brief Read-only access to elements reference by index of the vector element "index" @@ -245,10 +249,10 @@ public: * @param index is the index of necessary element * @return element of vector */ - Type operator [](uint index) const {return c[index];} + const Type & operator [](uint index) const {return c[index];} /** - * @brief Vector assignment to vector "v" of type PIMathVectorT + * @brief Vector assignment to vector "v" of type PIMathVectorT * * @param v vector for the assigment * @return vector equal to vector "v" @@ -256,12 +260,12 @@ public: _CVector & operator =(const _CVector & v) {memcpy(c, v.c, sizeof(Type) * Size); return *this;} /** - * @brief Vector assignment to value "v" + * @brief Assignment operation. All vector values ​​become equal to "v" * * @param v value for the assigment - * @return vector, each element of which is equal to the value "v" + * @return reference to this */ - _CVector & operator =(const Type & v) {PIMV_FOR(i, 0) c[i] = v; return *this;} + _CVector & operator =(const Type & v) {PIMV_FOR(i, 0) c[i] = v; return *this;} /** * @brief Compare with vector "v" @@ -280,71 +284,72 @@ public: bool operator !=(const _CVector & v) const {return !(*this == v);} /** - * @brief Addition assignment with vector "v" + * @brief Vector addition this vector with vector "v". If the vectors have different dimensions, it returns void() * * @param v vector for the addition assigment */ - void operator +=(const _CVector & v) {PIMV_FOR(i, 0) c[i] += v[i];} + void operator +=(const _CVector & v) {if(v.size() != Size) return void(); PIMV_FOR(i, 0) c[i] += v[i];} /** - * @brief Subtraction assignment with vector "v" + * @brief Subtraction assignmentthis vector with vector "v". If the vectors have different dimensions, it returns void() * * @param v vector for the subtraction assigment */ - void operator -=(const _CVector & v) {PIMV_FOR(i, 0) c[i] -= v[i];} + void operator -=(const _CVector & v) {if(v.size() != Size) return void(); PIMV_FOR(i, 0) c[i] -= v[i];} /** - * @brief Multiplication assignment with value "v" + * @brief Multiplication assignment this vector with value "v" * * @param v value for the multiplication assigment */ void operator *=(const Type & v) {PIMV_FOR(i, 0) c[i] *= v;} /** - * @brief Multiplication assignment with vector "v" + * @brief Multiplication assignment this vector with vector "v". If the vectors have different dimensions, it returns void() * * @param v vector for the multiplication assigment */ - void operator *=(const _CVector & v) {PIMV_FOR(i, 0) c[i] *= v[i];} + void operator *=(const _CVector & v) {if(v.size() != Size) return void(); PIMV_FOR(i, 0) c[i] *= v[i];} /** - * @brief Division assignment with value "v" + * @brief Division assignment with this vector value "v" * * @param v value for the division assigment */ void operator /=(const Type & v) {PIMV_FOR(i, 0) c[i] /= v;} /** - * @brief Division assignment with vector "v" + * @brief Division assignment this vector with vector "v". If the vectors have different dimensions, it returns void() * * @param v vector for the division assigment */ - void operator /=(const _CVector & v) {PIMV_FOR(i, 0) c[i] /= v[i];} + void operator /=(const _CVector & v) {if(v.size() != Size) return void(); PIMV_FOR(i, 0) c[i] /= v[i];} /** - * @brief Vector substraction + * @brief Vector substraction this vector * * @return the result of vector substraction */ _CVector operator -() const {_CVector tv; PIMV_FOR(i, 0) tv[i] = -c[i]; return tv;} /** - * @brief Matrix addition + * @brief Vector addition this vector with vector "v". If the vectors have different dimensions, it returns this without changing anything * - * @param sm is matrix term - * @return the result of matrix addition + * @param v is vector term + * @return the result of vector addition */ - _CVector operator +(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] += v[i]; return tv;} + _CVector operator +(const _CVector & v) const {if(v.size() != Size) return _CVector(*this); _CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] += v[i]; return tv;} /** - * @brief Vector substraction + * @brief Vector substraction this vector with vector "v". If the vectors have different dimensions, it returns this without changing anything * + * @param v is vector term * @return the result of vector substraction */ - _CVector operator -(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] -= v[i]; return tv;} + _CVector operator -(const _CVector & v) const {if(v.size() != Size) return _CVector(*this); _CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] -= v[i]; return tv;} /** - * @brief Vector multiplication with value "v" + * @brief Vector multiplication this vector with value "v" * * @param v is value factor * @return the result of vector multiplication @@ -352,7 +357,7 @@ public: _CVector operator *(const Type & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v; return tv;} /** - * @brief Vector division with value "v" + * @brief Vector division this vector with value "v" * * @param v is value divider * @return the result of vector division @@ -360,12 +365,12 @@ public: _CVector operator /(const Type & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] /= v; return tv;} /** - * @brief Vector division with vector "v" + * @brief Vector division this vector with vector "v". If the vectors have different dimensions, it returns this without changing anything * * @param v is vector divider * @return the result of vector division */ - _CVector operator /(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] /= v[i]; return tv;} + _CVector operator /(const _CVector & v) const {if(v.size() != Size) return _CVector(*this); _CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] /= v[i]; return tv;} /** * @brief Cross product of two vectors. Works only with vector containing three elements, otherwise returns current vector @@ -376,20 +381,20 @@ public: _CVector operator *(const _CVector & v) const {if (Size != 3) return _CVector(); _CVector tv; tv.fill(Type(1)); tv[0] = c[1]*v[2] - v[1]*c[2]; tv[1] = v[0]*c[2] - c[0]*v[2]; tv[2] = c[0]*v[1] - v[0]*c[1]; return tv;} /** - * @brief Elementwise assignment of multiplication of two vectors + * @brief Elementwise assignment of multiplication of two vectors. If the vectors have different dimensions, it returns this without changing anything * * @param v is vector for multiplication * @return resulting vector */ - _CVector operator &(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v[i]; return tv;} + _CVector operator &(const _CVector & v) const {if(v.size() != Size) return _CVector(*this); _CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v[i]; return tv;} /** - * @brief Absolute value of the dot product + * @brief Absolute value of the dot product. If the vectors have different dimensions, it returns false * * @param v is vector for dot product * @return resulting vector */ - Type operator ^(const _CVector & v) const {Type tv(0); PIMV_FOR(i, 0) tv += c[i] * v[i]; return tv;} + Type operator ^(const _CVector & v) const {if(v.size() != Size) return false; Type tv(0); PIMV_FOR(i, 0) tv += c[i] * v[i]; return tv;} PIMathMatrixT<1, Size, Type> transposed() const { PIMathMatrixT<1, Size, Type> ret; @@ -397,18 +402,6 @@ public: return ret; } - /** - * @brief Returns the distance between two vectors. Works only for 2-element vectors - * - * @param lp0 is vector - * @param lp1 is vector - * @return resulting value - */ - Type distToLine(const _CVector & lp0, const _CVector & lp1) { - _CVector a(lp0, lp1), b(lp0, *this), c(lp1, *this); - Type f = fabs(a[0]*b[1] - a[1]*b[0]) / a.length(); - return f;} - /** * @brief The method returns a part of the selected vector from the given vector * @@ -418,7 +411,7 @@ public: PIMathVectorT turnTo() const {PIMathVectorT tv; uint sz = piMin(Size, Size1); for (uint i = 0; i < sz; ++i) tv[i] = c[i]; return tv;} /** - * @brief Creates a vector filled with a value + * @brief Creates a vector each element of which is equal to value "v" * * @param v this value fills the cells of the vector * @return filled vector of type PIMathVectorT @@ -570,7 +563,6 @@ public: * @brief Constructor that calls the resize method * * @param size vector dimension - * @return resized vector of type PIMathMatrix */ PIMathVector(const uint size = 0) {c.resize(size);} @@ -578,7 +570,6 @@ public: * @brief Constructor that fills a vector PIMathVector with the values ​​of another vector "PIVector" * * @param val vector of type PIVector which is identified PIMathVector - * @return vector of type PIMathVector with values ​​of vector val */ PIMathVector(const PIVector & val) {c.resize(val.size()); PIMV_FOR(i, 0) c[i] = val[i];} @@ -587,15 +578,14 @@ public: * * @param st vector of type PIMathVector * @param fn vector of type PIMathVector - * @return vector of type PIMathVectorT with values subtraction vectors "fn" and "st" */ PIMathVector(const _CVector & st, const _CVector & fn) {c.resize(st.size()); PIMV_FOR(i, 0) c[i] = fn[i] - st[i];} - /** - * @brief Method which returns size of the vector - * - * @return type uint shows number of elements in this vector - */ + /** + * @brief Method that returns the number of elements contained in the vector + * + * @return type uint shows number of elements in this vector + */ uint size() const {return c.size();} /** @@ -603,7 +593,7 @@ public: * * @param size new vector dimension * @param new_value value with which the vector is filled - * @return resized vector + * @return reference to this */ _CVector & resize(uint size, const Type & new_value = Type()) {c.resize(size, new_value); return *this;} @@ -616,37 +606,38 @@ public: */ _CVector resized(uint size, const Type & new_value = Type()) {_CVector tv = _CVector(*this); tv.resize(size, new_value); return tv;} - /** - * @brief Method that fills a vector with a value - * - * @param v value of which the vector is filled - * @return vector of type PIMathVector filled with "v" - */ + /** + * @brief Method that set this elements to value "v" + * + * @param v value of which the vector is filled + * @return reference to this + */ _CVector & fill(const Type & v) {PIMV_FOR(i, 0) c[i] = v; return *this;} - /** - * @brief Method that fills a vector with the adittion of vector value and "v" - * - * @param v value of which the vector is filled - * @return vector of type PIMathVector with values adittion of vector value and "v" - */ + /** + * @brief Method that sets this using a vector, each element of which is added to the value of "v" + * + * @param v value of which the vector is filled + * @return reference to this + */ _CVector & move(const Type & v) {PIMV_FOR(i, 0) c[i] += v; return *this;} - /** - * @brief Method that fills a vector with the adittion of vector value and "v" - * - * @param v vector of type PIMathVectorT - * @return vector of type PIMathVectorT with values adittion of vector value and "v" - */ - _CVector & move(const _CVector & v) {PIMV_FOR(i, 0) c[i] += v[i]; return *this;} + /** + * @brief Method that sets this with a vector, each element of which is added to each element of the vector "v". + * If the vectors have different dimensions, it returns this without changing anything + * + * @param v vector of type PIMathVectorT + * @return reference to this + */ + _CVector & move(const _CVector & v) {if(v.size() != c.size()) return *this; PIMV_FOR(i, 0) c[i] += v[i]; return *this;} /** - * @brief Method that replaces two elements in a vector by indices. You cannot use an index larger than the number vector dimension, + * @brief Method that replaces two elements in this vector by indices. You cannot use an index larger than the number vector dimension, * otherwise there will be "undefined behavior" * * @param fe index of the first element * @param se index of the second element - * @return resulting vector of type PIMathVector + * @return reference to this */ _CVector & swap(uint fe, uint se) {piSwap(c[fe], c[se]); return *this;} @@ -657,11 +648,11 @@ public: */ Type lengthSqr() const {Type tv(0); PIMV_FOR(i, 0) tv += (c[i] * c[i]); return tv;} - /** - * @brief Method that returns length of a vector - * - * @return value equal to length of a vector - */ + /** + * @brief Method that returns a scalar physical value equal to the absolute value of vector + * + * @return value equal to length of a vector + */ Type length() const {return sqrt(lengthSqr());} /** @@ -672,44 +663,48 @@ public: Type manhattanLength() const {Type tv(0); PIMV_FOR(i, 0) tv += fabs(c[i]); return tv;} /** - * @brief Method that returns the cos of the current vector and vector "v" + * @brief Method that returns the cos of the current vector and vector "v". If the vectors have different dimensions, it returns false * * @param v vector of type PIMathVector * @return cos value of the angle between two vectors */ - Type angleCos(const _CVector & v) const {Type tv = v.length() * length(); return (tv == Type(0) ? Type(0) : ((*this) ^ v) / tv);} + Type angleCos(const _CVector & v) const {if(v.size() != c.size()) return false; Type tv = v.length() * length(); return (tv == Type(0) ? Type(0) : ((*this) ^ v) / tv);} /** - * @brief Method that returns the sin of the current vector and vector "v". Works only with vectors which consists of 3 elements + * @brief Method that returns the sin of the current vector and vector "v". Works only with vectors which consists of 3 elements. + * If the vectors have different dimensions, it returns false * * @param v vector of type PIMathVector * @return sin value of the angle between two vector */ - Type angleSin(const _CVector & v) const {Type tv = angleCos(v); return sqrt(Type(1) - tv * tv);} + Type angleSin(const _CVector & v) const {if(v.size() != c.size()) return false; Type tv = angleCos(v); return sqrt(Type(1) - tv * tv);} /** - * @brief Method that returns the angle between of the current vector and vector "v" in Rad + * @brief Method that returns the angle between of the current vector and vector "v" in Rad. + * If the vectors have different dimensions, it returns false * * @param v vector of type PIMathVector * @return value of the angle between two vectors in Rad */ - Type angleRad(const _CVector & v) const {return acos(angleCos(v));} + Type angleRad(const _CVector & v) const {if(v.size() != c.size()) return false; return acos(angleCos(v));} /** - * @brief Method that returns the angle between of the current vector and vector "v" in Deg + * @brief Method that returns the angle between of the current vector and vector "v" in Deg. + * If the vectors have different dimensions, it returns false * * @param v vector of type PIMathVectorT * @return value of the angle between two vectors in Deg */ - Type angleDeg(const _CVector & v) const {return toDeg(acos(angleCos(v)));} + Type angleDeg(const _CVector & v) const {if(v.size() != c.size()) return false; return toDeg(acos(angleCos(v)));} /** - * @brief Method that returns a vector equal to the projection of the current vector onto the vector "v" + * @brief Method that returns a vector equal to the projection of the current vector onto the vector "v". + * If the vectors have different dimensions, it returns this without changing anything * * @param v vector of type PIMathVector * @return vector of type PIMathVector equal to the projection of the current vector onto the vector "v" */ - _CVector projection(const _CVector & v) {Type tv = v.length(); return (tv == Type(0) ? _CVector() : v * (((*this) ^ v) / tv));} + _CVector projection(const _CVector & v) {if(v.size() != c.size()) return *this; Type tv = v.length(); return (tv == Type(0) ? _CVector() : v * (((*this) ^ v) / tv));} /** * @brief Method that returns a normalized vector @@ -740,12 +735,13 @@ public: bool isValid() const {return !c.isEmpty();} /** - * @brief Method which checks if current vector is orthogonal to vector "v" + * @brief Method which checks if current vector is orthogonal to vector "v". + * If the vectors have different dimensions, it returns false * * @param v vector of type PIMathVector * @return true if vectors are orthogonal, else false */ - bool isOrtho(const _CVector & v) const {return ((*this) ^ v) == Type(0);} + bool isOrtho(const _CVector & v) const {if(v.size() != c.size()) return false; return ((*this) ^ v) == Type(0);} /** * @brief Read-only access to elements reference by index of the vector element "index" @@ -770,21 +766,22 @@ public: * @param index is the index of necessary element * @return element of vector */ - Type operator [](uint index) const {return c[index];} + const Type & operator [](uint index) const {return c[index];} /** * @brief Vector assignment to vector "v" of type PIMathVector + * If the vectors have different dimensions, it returns this without changing anything * * @param v vector for the assigment - * @return vector equal to vector "v" + * @return reference to this */ - _CVector & operator =(const _CVector & v) {c = v.c; return *this;} + _CVector & operator =(const _CVector & v) {if(v.size() != c.size()) return *this; c = v.c; return *this;} /** * @brief Vector assignment to value "v" * * @param v value for the assigment - * @return vector, each element of which is equal to the value "v" + * @return reference to this */ _CVector & operator =(const Type & v) {PIMV_FOR(i, 0) c[i] = v; return *this;} @@ -794,7 +791,7 @@ public: * @param v vector for the compare * @return if vectors are equal true, else false */ - bool operator ==(const _CVector & v) const {PIMV_FOR(i, 0) if (c[i] != v[i]) return false; return true;} + bool operator ==(const _CVector & v) const {PIMV_FOR(i, 0) if ((c[i] != v[i]) || (v.size() != c.size())) return false; return true;} /** * @brief Compare with vector "v" @@ -805,71 +802,72 @@ public: bool operator !=(const _CVector & v) const {return !(*this == v);} /** - * @brief Addition assignment with vector "v" + * @brief Addition assignment this vector with vector "v". If the vectors have different dimensions, it returns void() * * @param v vector for the addition assigment */ - void operator +=(const _CVector & v) {PIMV_FOR(i, 0) c[i] += v[i];} + void operator +=(const _CVector & v) {if(v.size() != c.size()) return void(); PIMV_FOR(i, 0) c[i] += v[i];} /** - * @brief Subtraction assignment with vector "v" + * @brief Subtraction assignment this vector with vector "v". If the vectors have different dimensions, it returns void() * * @param v vector for the subtraction assigment */ - void operator -=(const _CVector & v) {PIMV_FOR(i, 0) c[i] -= v[i];} + void operator -=(const _CVector & v) {if(v.size() != c.size()) return void(); PIMV_FOR(i, 0) c[i] -= v[i];} /** - * @brief Multiplication assignment with value "v" + * @brief Multiplication assignment this vector with value "v" * * @param v value for the multiplication assigment */ void operator *=(const Type & v) {PIMV_FOR(i, 0) c[i] *= v;} /** - * @brief Multiplication assignment with vector "v" + * @brief Multiplication assignment this vector with vector "v". If the vectors have different dimensions, it returns void() * * @param v vector for the multiplication assigment */ - void operator *=(const _CVector & v) {PIMV_FOR(i, 0) c[i] *= v[i];} + void operator *=(const _CVector & v) {if(v.size() != c.size()) return void(); PIMV_FOR(i, 0) c[i] *= v[i];} /** - * @brief Division assignment with value "v" + * @brief Division assignment this vector with value "v" * * @param v value for the division assigment */ void operator /=(const Type & v) {PIMV_FOR(i, 0) c[i] /= v;} /** - * @brief Division assignment with vector "v" + * @brief Division assignment this vector with vector "v". If the vectors have different dimensions, it returns void() * * @param v vector for the division assigment */ - void operator /=(const _CVector & v) {PIMV_FOR(i, 0) c[i] /= v[i];} + void operator /=(const _CVector & v) {if(v.size() != c.size()) return void(); PIMV_FOR(i, 0) c[i] /= v[i];} /** - * @brief Vector substraction + * @brief Vector substraction this vector * * @return the result of vector substraction */ _CVector operator -() const {_CVector tv; PIMV_FOR(i, 0) tv[i] = -c[i]; return tv;} /** - * @brief Matrix addition + * @brief Vector addition this vector with vector "v". If the vectors have different dimensions, it returns this without changing anything * - * @param sm is matrix term + * @param v is vector term * @return the result of matrix addition */ - _CVector operator +(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] += v[i]; return tv;} + _CVector operator +(const _CVector & v) const {if(v.size() != c.size()) return _CVector(*this); _CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] += v[i]; return tv;} /** - * @brief Vector substraction + * @brief Vector substraction this vector with vector "v". If the vectors have different dimensions, it returns this without changing anything * + * @param v is vector term * @return the result of vector substraction */ - _CVector operator -(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] -= v[i]; return tv;} + _CVector operator -(const _CVector & v) const {if(v.size() != c.size()) return _CVector(*this); _CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] -= v[i]; return tv;} /** - * @brief Vector multiplication with value "v" + * @brief Vector multiplicationthis vector with value "v" * * @param v is value factor * @return the result of vector multiplication @@ -877,7 +875,7 @@ public: _CVector operator *(const Type & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v; return tv;} /** - * @brief Vector division with value "v" + * @brief Vector division this vector with value "v" * * @param v is value divider * @return the result of vector division @@ -890,36 +888,23 @@ public: * @param v is vector for cross product * @return the result vector equal of cross product */ - _CVector operator *(const _CVector & v) const {if ((c.size() != 3) && (v.size() != 3)) return _CVector(); _CVector tv(3); tv.fill(Type(1)); tv[0] = c[1]*v[2] - v[1]*c[2]; tv[1] = v[0]*c[2] - c[0]*v[2]; tv[2] = c[0]*v[1] - v[0]*c[1]; return tv;} + _CVector operator *(const _CVector & v) const {if ((c.size() != 3) || (v.size() != 3)) return _CVector(); _CVector tv(3); tv.fill(Type(1)); tv[0] = c[1]*v[2] - v[1]*c[2]; tv[1] = v[0]*c[2] - c[0]*v[2]; tv[2] = c[0]*v[1] - v[0]*c[1]; return tv;} /** - * @brief Elementwise assignment of multiplication of two vectors + * @brief Elementwise assignment of multiplication of two vectors. If the vectors have different dimensions, it returns this without changing anything * * @param v is vector for multiplication * @return resulting vector */ - _CVector operator &(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v[i]; return tv;} + _CVector operator &(const _CVector & v) const {if(v.size() != c.size()) return _CVector(*this); _CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v[i]; return tv;} /** - * @brief Absolute value of the dot product + * @brief Value of the dot product. If the vectors have different dimensions, it returns false * * @param v is vector for dot product - * @return resulting vector + * @return resulting value */ - Type operator ^(const _CVector & v) const {Type tv(0); PIMV_FOR(i, 0) tv += c[i] * v[i]; return tv;} - - /** - * @brief Returns the distance between two vectors. Works only for 2-element vectors - * - * @param lp0 is vector - * @param lp1 is vector - * @return resulting value - */ - Type distToLine(const _CVector & lp0, const _CVector & lp1) { - _CVector a(lp0, lp1), b(lp0, *this), c(lp1, *this); - Type f = fabs(a[0]*b[1] - a[1]*b[0]) / a.length(); - return f; - } + Type operator ^(const _CVector & v) const {if(v.size() != c.size()) return false; Type tv(0); PIMV_FOR(i, 0) tv += c[i] * v[i]; return tv;} /** * @brief Converts PIMathVector to PIVector type diff --git a/tests/math/testpimathmatrix.cpp b/tests/math/testpimathmatrix.cpp index f2c8cc10..c6a52ec7 100644 --- a/tests/math/testpimathmatrix.cpp +++ b/tests/math/testpimathmatrix.cpp @@ -5,7 +5,7 @@ bool cmpSquareMatrixWithValue(PIMathMatrix matrix, double val, int num) bool b = true; for(int i = 0; i < num; i++) { for(int j = 0; j < num; j++) { - if(matrix.element(i, j) != val) { + if(matrix.element(i, j) - val >= double(1E-200)) { b = false; } } diff --git a/tests/math/testpimathmatrixt.cpp b/tests/math/testpimathmatrixt.cpp index ff9afc8c..c9ba0ff7 100644 --- a/tests/math/testpimathmatrixt.cpp +++ b/tests/math/testpimathmatrixt.cpp @@ -8,7 +8,7 @@ bool cmpSquareMatrixWithValue(PIMathMatrixT matrix, double v bool b = true; for(int i = 0; i < num; i++) { for(int j = 0; j < num; j++) { - if(matrix.at(i, j) != val) { + if(matrix.at(i, j) - val >= double(1E-200)) { b = false; } } diff --git a/tests/math/testpimathvector.cpp b/tests/math/testpimathvector.cpp index b12ba4bb..4d0bf405 100644 --- a/tests/math/testpimathvector.cpp +++ b/tests/math/testpimathvector.cpp @@ -1,10 +1,12 @@ #include "gtest/gtest.h" #include "pimathvector.h" +const uint SIZE = 3u; + bool cmpVectorWithValue(PIMathVector vector, double val, int num) { bool b = true; for(int i = 0; i < num; i++) { - if(vector[i] != val) { + if(vector[i] - val >= double(1E-200)) { b = false; } } @@ -12,146 +14,181 @@ bool cmpVectorWithValue(PIMathVector vector, double val, int num) { } TEST(PIMathVector_Test, size) { - auto vector = PIMathVector(3u); - ASSERT_TRUE(vector.size() == 3u); + auto vector = PIMathVector(SIZE); + ASSERT_TRUE(vector.size() == SIZE); } TEST(PIMathVector_Test, resize) { + uint newSize = 4u; + double a = 5.0; PIMathVector vector; - vector.resize(4u, 5.0); - ASSERT_TRUE(cmpVectorWithValue(vector, 5.0, vector.size())); + vector.resize(newSize, a); + ASSERT_TRUE(cmpVectorWithValue(vector, a, vector.size())); } TEST(PIMathVector_Test, resized) { + uint newSize = 4u; + double a = 5.0; PIMathVector vector; - vector.resized(4u, 5.0); - ASSERT_TRUE(cmpVectorWithValue(vector, 5.0, vector.size())); + vector.resized(newSize, a); + ASSERT_TRUE(cmpVectorWithValue(vector, a, vector.size())); } TEST(PIMathVector_Test, fill) { - PIMathVector vector(3u); - vector.fill(5.0); - ASSERT_TRUE(cmpVectorWithValue(vector, 5.0, 3u)); + double a = 5.0; + PIMathVector vector(SIZE); + vector.fill(a); + ASSERT_TRUE(cmpVectorWithValue(vector, a, SIZE)); } TEST(PIMathVector_Test, moveVal) { - PIMathVector vector(3u); - vector.fill(5.0); - vector.move(5.0); - ASSERT_TRUE(cmpVectorWithValue(vector, 10.0, 3u)); + double a = 5.0; + PIMathVector vector(SIZE); + vector.fill(a); + vector.move(a); + ASSERT_TRUE(cmpVectorWithValue(vector, 2 * a, SIZE)); } TEST(PIMathVector_Test, moveVec) { - PIMathVector vector(3u); - PIMathVector vec(3u); - vector.fill(5.0); - vec.fill(7.0); + double a = 5.0; + double b = 7.0; + PIMathVector vector(SIZE); + PIMathVector vec(SIZE); + vector.fill(a); + vec.fill(b); vector.move(vec); - ASSERT_TRUE(cmpVectorWithValue(vector, 12.0, 3u)); + ASSERT_TRUE(cmpVectorWithValue(vector, a + b, SIZE)); } TEST(PIMathVector_Test, swap) { - PIMathVector vector(3u); + double b = 5.12; + double c = 3.32; + double d = 7.12; + PIMathVector vector(SIZE); double a[3]; - vector[0] = 5.12; - vector[1] = 3.32; - vector[2] = 7.12; + vector[0] = b; + vector[1] = c; + vector[2] = d; 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])); + ASSERT_DOUBLE_EQ(a[0], vector[1]); + ASSERT_DOUBLE_EQ(a[1], vector[0]); + ASSERT_DOUBLE_EQ(a[2], vector[2]); } TEST(PIMathVector_Test, lengthSqr) { - PIMathVector vector(3u); - vector.fill(1.0); - ASSERT_EQ(3.0, vector.lengthSqr()); + double a = 3.0; + PIMathVector vector(SIZE); + vector.fill(a); + ASSERT_DOUBLE_EQ(SIZE * a * a, vector.lengthSqr()); } TEST(PIMathVector_Test, length) { - PIMathVector vector(3u); - vector.fill(1.0); - ASSERT_DOUBLE_EQ(sqrt(3.0), vector.length()); + double a = 3.32; + PIMathVector vector(SIZE); + vector.fill(a); + ASSERT_DOUBLE_EQ(sqrt(SIZE * a * a), vector.length()); } TEST(PIMathVector_Test, manhattanLength) { - PIMathVector vector(3u); - vector.fill(5.0); - ASSERT_DOUBLE_EQ(15.0, vector.manhattanLength()); + double a = 3.32; + PIMathVector vector(SIZE); + vector.fill(a); + ASSERT_DOUBLE_EQ(SIZE * a, vector.manhattanLength()); } TEST(PIMathVector_Test, angleCos) { - PIMathVector vector(3u); - PIMathVector vec(3u); - vector[0] = 1.0; - vector[1] = 1.0; - vec[1] = 1.0; - ASSERT_DOUBLE_EQ(cos(0.78539816339744830961566084581988), vector.angleCos(vec)); + double a = 3.32; + double angle = 0.78539816339744830961566084581988; + PIMathVector vector(SIZE); + PIMathVector vec(SIZE); + vector[0] = a; + vector[1] = a; + vec[1] = a; + ASSERT_DOUBLE_EQ(cos(angle), vector.angleCos(vec)); } TEST(PIMathVector_Test, angleSin) { - PIMathVector vector(3u); - PIMathVector vec(3u); - vector[0] = 1.0; - vector[1] = 1.0; - vec[1] = 1.0; - ASSERT_DOUBLE_EQ(cos(0.78539816339744830961566084581988), vector.angleSin(vec)); + double a = 3.32; + double angle = 0.78539816339744830961566084581988; + PIMathVector vector(SIZE); + PIMathVector vec(SIZE); + vector[0] = a; + vector[1] = a; + vec[1] = a; + ASSERT_DOUBLE_EQ(sin(angle), vector.angleSin(vec)); } TEST(PIMathVector_Test, angleRad) { - PIMathVector vector(3u); - PIMathVector vec(3u); - vector[0] = 1.0; - vector[1] = 1.0; - vec[1] = 1.0; - ASSERT_DOUBLE_EQ(0.78539816339744830961566084581988, vector.angleRad(vec)); + double a = 3.32; + double angle = 0.78539816339744830961566084581988; + PIMathVector vector(SIZE); + PIMathVector vec(SIZE); + vector[0] = a; + vector[1] = a; + vec[1] = a; + ASSERT_DOUBLE_EQ(angle, vector.angleRad(vec)); } TEST(PIMathVector_Test, angleDeg) { - PIMathVector vector(3u); - PIMathVector vec(3u); - vector[0] = 1.0; - vector[1] = 1.0; - vec[1] = 1.0; - ASSERT_DOUBLE_EQ(45.0, vector.angleDeg(vec)); + double a = 3.32; + double angle = 45.0; + PIMathVector vector(SIZE); + PIMathVector vec(SIZE); + vector[0] = a; + vector[1] = a; + vec[1] = a; + ASSERT_DOUBLE_EQ(angle, vector.angleDeg(vec)); } TEST(PIMathVector_Test, projection) { - PIMathVector vector(2u); - PIMathVector vec(2u); - vec[0] = 1.0; - vector[0] = 1.0; - vector[1] = 1.0; + double a = 2.0; + double b = 2.0; + double res = sqrt(32.0); + PIMathVector vector(SIZE); + PIMathVector vec(SIZE); + vec[0] = a; + vec[2] = b; + vector[0] = a; + vector[1] = b; + vector[2] = a; auto vecProj = vector.projection(vec); - ASSERT_TRUE(vecProj == vec); + ASSERT_DOUBLE_EQ(res, vecProj[0]); + ASSERT_DOUBLE_EQ(0.0, vecProj[1]); + ASSERT_DOUBLE_EQ(res, vecProj[2]); } TEST(PIMathVector_Test, normalize) { - PIMathVector vector(3u); - vector.fill(5.0); - ASSERT_TRUE(cmpVectorWithValue(vector.normalize(), 5.0 / sqrt(75.0), 3u)); + double a = 5.0; + PIMathVector vector(SIZE); + vector.fill(a); + ASSERT_TRUE(cmpVectorWithValue(vector.normalize(), a / sqrt(SIZE * a * a), SIZE)); } TEST(PIMathVector_Test, normalized) { - PIMathVector vector(3u); - vector.fill(5.0); - ASSERT_TRUE(cmpVectorWithValue(vector.normalized(), 5.0 / sqrt(75.0), 3u)); + double a = 5.0; + PIMathVector vector(SIZE); + PIMathVector vectorNew(SIZE); + vector.fill(a); + vectorNew = vector.normalized(); + ASSERT_TRUE(cmpVectorWithValue(vectorNew, a / sqrt(SIZE * a * a), SIZE)); } TEST(PIMathVector_Test, isNullTrue) { - PIMathVector vector(3u); + PIMathVector vector(SIZE); ASSERT_TRUE(vector.isNull()); } TEST(PIMathVector_Test, isNullFalse) { - PIMathVector vector(3u); + PIMathVector vector(SIZE); vector[0] = 6.273; ASSERT_FALSE(vector.isNull()); } TEST(PIMathVector_Test, isValidTrue) { - PIMathVector vector(3u); + PIMathVector vector(SIZE); ASSERT_TRUE(vector.isValid()); } @@ -161,27 +198,35 @@ TEST(PIMathVector_Test, isValidFalse) { } TEST(PIMathVector_Test, isOrthoTrue) { - PIMathVector vector(2u); - PIMathVector vect(2u); - vector[0] = 2.0; - vect[1] = 1.0; + uint sizeNew = 2u; + double a = 2.0; + double b = 1.0; + PIMathVector vector(sizeNew); + PIMathVector vect(sizeNew); + vector[0] = a; + vect[1] = b; ASSERT_TRUE(vector.isOrtho(vect)); } TEST(PIMathVector_Test, isOrthoFalse) { - PIMathVector vector(2u); - PIMathVector vect(2u); - vector[0] = 2.0; - vect[0] = 5.0; - vect[1] = 1.0; + uint sizeNew = 2u; + double a = 2.0; + double b = 1.0; + double c = 5.0; + PIMathVector vector(sizeNew); + PIMathVector vect(sizeNew); + vector[0] = a; + vect[0] = c; + vect[1] = b; ASSERT_FALSE(vector.isOrtho(vect)); } TEST(PIMathVector_Test, at) { - PIMathVector vector(3u); - vector.fill(5.5); - for(uint i = 0; i < 3u; i++){ - if(vector.at(i) != 5.5){ + double a = 5.5; + PIMathVector vector(SIZE); + vector.fill(a); + for(uint i = 0; i < SIZE; i++){ + if(vector.at(i) - a >= double(1E-200)){ ASSERT_TRUE(false); } } @@ -189,176 +234,212 @@ TEST(PIMathVector_Test, at) { } TEST(PIMathVector_Test, operator_AssignmentValue) { - PIMathVector vector(3u); - vector = 3.0; - ASSERT_TRUE(cmpVectorWithValue(vector, 3.0, 3)); + double a = 5.5; + PIMathVector vector(SIZE); + vector = a; + ASSERT_TRUE(cmpVectorWithValue(vector, a, SIZE)); } TEST(PIMathVector_Test, operator_AssignmentVector) { - PIMathVector vector(3u); - PIMathVector vec(3u); - vec = 5.0; + double a = 5.5; + PIMathVector vector(SIZE); + PIMathVector vec(SIZE); + vec = a; vector = vec; - ASSERT_TRUE(cmpVectorWithValue(vector, 5.0, 3)); + ASSERT_TRUE(cmpVectorWithValue(vector, a, SIZE)); } TEST(PIMathVector_Test, operator_EqualTrue) { - PIMathVector vector(2u); - PIMathVector vec(2u); - vector[0] = 5.12; - vector[1] = 7.34; - vec[0] = 5.12; - vec[1] = 7.34; + double a = 5.12; + double b = 7.34; + uint newSize = 2u; + PIMathVector vector(newSize); + PIMathVector vec(newSize); + vector[0] = a; + vector[1] = b; + vec[0] = a; + vec[1] = b; ASSERT_TRUE(vec == vector); } TEST(PIMathVector_Test, operator_EqualFalse) { - PIMathVector vector(2u); - PIMathVector vec(2u); - vector[0] = 5.12; - vector[1] = 7.34; - vec[0] = 5.12; - vec[1] = 0.34; + double a = 5.12; + double b = 7.34; + double c = 7.332; + uint newSize = 2u; + PIMathVector vector(newSize); + PIMathVector vec(newSize); + vector[0] = a; + vector[1] = b; + vec[0] = a; + vec[1] = c; ASSERT_FALSE(vec == vector); } TEST(PIMathVector_Test, operator_Not_EqualTrue) { - PIMathVector vector(2u); - PIMathVector vec(2u); - vector[0] = 5.12; - vector[1] = 7.34; - vec[0] = 5.12; - vec[1] = 0.34; + double a = 5.12; + double b = 7.34; + double c = 7.332; + uint newSize = 2u; + PIMathVector vector(newSize); + PIMathVector vec(newSize); + vector[0] = a; + vector[1] = b; + vec[0] = a; + vec[1] = c; ASSERT_TRUE(vec != vector); } TEST(PIMathVector_Test, operator_Not_EqualFalse) { - PIMathVector vector(2u); - PIMathVector vec(2u); - vector[0] = 5.12; - vector[1] = 7.34; - vec[0] = 5.12; - vec[1] = 7.34; + double a = 5.12; + double b = 7.34; + uint newSize = 2u; + PIMathVector vector(newSize); + PIMathVector vec(newSize); + vector[0] = a; + vector[1] = b; + vec[0] = a; + vec[1] = b; ASSERT_FALSE(vec != vector); } TEST(PIMathVector_Test, operator_Addition_Aassignment) { - PIMathVector vector1(3u); - PIMathVector vector2(3u); - vector1.fill(6.0); - vector2.fill(1.72); + double a = 6.0; + double b = 1.72; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + vector1.fill(a); + vector2.fill(b); vector1 += vector2; - ASSERT_TRUE(cmpVectorWithValue(vector1, 7.72, 3)); + ASSERT_TRUE(cmpVectorWithValue(vector1, a + b, SIZE)); } TEST(PIMathVector_Test, operator_Subtraction_Assignment) { - PIMathVector vector1(3u); - PIMathVector vector2(3u); - vector1.fill(6.0); - vector2.fill(1.72); + double a = 6.0; + double b = 1.72; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + vector1.fill(a); + vector2.fill(b); vector1 -= vector2; - ASSERT_TRUE(cmpVectorWithValue(vector1, 4.28, 3)); + ASSERT_TRUE(cmpVectorWithValue(vector1, a - b, SIZE)); } TEST(PIMathVector_Test, operator_Multiplication_AssignmentValue) { - PIMathVector vector1(3u); - vector1.fill(6.0); - vector1 *= 4.0; - ASSERT_TRUE(cmpVectorWithValue(vector1, 24.0, 3)); + double a = 6.0; + double b = 4.0; + PIMathVector vector1(SIZE); + vector1.fill(a); + vector1 *= b; + ASSERT_TRUE(cmpVectorWithValue(vector1, a * b, SIZE)); } TEST(PIMathVector_Test, operator_Multiplication_AssignmentVector) { - PIMathVector vector1(3u); - PIMathVector vector2(3u); - vector1.fill(6.0); - vector2.fill(1.72); + double a = 6.0; + double b = 1.72; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + vector1.fill(a); + vector2.fill(b); vector1 *= vector2; - ASSERT_TRUE(cmpVectorWithValue(vector1, 10.32, 3)); + ASSERT_TRUE(cmpVectorWithValue(vector1, a * b, SIZE)); } TEST(PIMathVector_Test, operator_Division_AssignmentValue) { - PIMathVector vector1(3u); - vector1.fill(6.0); - vector1 /= 4.0; - ASSERT_TRUE(cmpVectorWithValue(vector1, 1.5, 3)); + double a = 6.0; + double b = 4.0; + PIMathVector vector1(SIZE); + vector1.fill(a); + vector1 /= b; + ASSERT_TRUE(cmpVectorWithValue(vector1, a / b, SIZE)); } TEST(PIMathVector_Test, operator_Division_AssignmentVector) { - PIMathVector vector1(3u); - PIMathVector vector2(3u); - vector1.fill(6.0); - vector2.fill(1.5); + double a = 6.0; + double b = 1.5; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + vector1.fill(a); + vector2.fill(b); vector1 /= vector2; - ASSERT_TRUE(cmpVectorWithValue(vector1, 4.0, 3)); + ASSERT_TRUE(cmpVectorWithValue(vector1, a / b, SIZE)); } TEST(PIMathVector_Test, operator_Addition) { - PIMathVector vector1(3u); - PIMathVector vector2(3u); - vector1.fill(6.0); - vector2.fill(1.72); - ASSERT_TRUE(cmpVectorWithValue(vector1 + vector2, 7.72, 3)); + double a = 6.0; + double b = 1.72; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + vector1.fill(a); + vector2.fill(b); + ASSERT_TRUE(cmpVectorWithValue(vector1 + vector2, a + b, SIZE)); } TEST(PIMathVector_Test, operator_Subtraction) { - PIMathVector vector1(3u); - PIMathVector vector2(3u); - vector1.fill(6.0); - vector2.fill(1.72); - ASSERT_TRUE(cmpVectorWithValue(vector1 - vector2, 4.28, 3)); + double a = 6.0; + double b = 1.72; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + vector1.fill(a); + vector2.fill(b); + ASSERT_TRUE(cmpVectorWithValue(vector1 - vector2, a - b, SIZE)); } TEST(PIMathVector_Test, operator_MultiplicationValue) { - PIMathVector vector1(3u); - PIMathVector vector2(3u); - vector1.fill(6.0); - ASSERT_TRUE(cmpVectorWithValue(vector1 * 4.0, 24.0, 3)); + double a = 6.0; + double b = 4.0; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + vector1.fill(a); + ASSERT_TRUE(cmpVectorWithValue(vector1 * b, a * b, SIZE)); } TEST(PIMathVector_Test, operator_MultiplicationVector1) { - PIMathVector vector1(3u); - PIMathVector vector2(3u); - vector1.fill(6.0); - vector2.fill(1.72); - ASSERT_TRUE(cmpVectorWithValue((vector1 * vector2), 0.0, 3)); + double a = 6.0; + double b = 1.72; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + vector1.fill(a); + vector2.fill(b); + ASSERT_TRUE(cmpVectorWithValue((vector1 * vector2), 0.0, SIZE)); } TEST(PIMathVector_Test, operator_MultiplicationVector2) { - PIMathVector vector1(3u); - PIMathVector 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)); + double a = 1.0; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + 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(PIMathVector_Test, operator_DivisionVector) { - PIMathVector vector1(3u); - vector1.fill(6.0); - ASSERT_TRUE(cmpVectorWithValue(vector1 / 4.0, 1.5, 3)); +TEST(PIMathVector_Test, operator_DivisionValue) { + double a = 6.0; + double b = 4.0; + PIMathVector vector1(SIZE); + vector1.fill(a); + ASSERT_TRUE(cmpVectorWithValue(vector1 / b, a / b, SIZE)); } TEST(PIMathVector_Test, operator_MultiplVect) { - PIMathVector vector1(3u); - PIMathVector vector2(3u); - vector1.fill(6.0); - vector2.fill(5.0); - ASSERT_TRUE(cmpVectorWithValue(vector1 & vector2, 30.0, 3)); + double a = 6.0; + double b = 5.0; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + vector1.fill(a); + vector2.fill(b); + ASSERT_TRUE(cmpVectorWithValue(vector1 & vector2, a * b, SIZE)); } -TEST(PIMathVector_Test, operator_absDotProduct) { - PIMathVector vector1(3u); - PIMathVector vector2(3u); - vector1.fill(6.0); - vector2.fill(5.0); - ASSERT_TRUE(90.0 == (vector1 ^ vector2)); -} - -TEST(PIMathVector_Test, distToLine) { - PIMathVector vect(3u); - PIMathVector vector1(3u); - PIMathVector 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); +TEST(PIMathVector_Test, operator_DotProduct) { + double a = 6.0; + double b = 5.0; + PIMathVector vector1(SIZE); + PIMathVector vector2(SIZE); + vector1.fill(a); + vector2.fill(b); + ASSERT_TRUE(SIZE * a * b == (vector1 ^ vector2)); } diff --git a/tests/math/testpimathvectort.cpp b/tests/math/testpimathvectort.cpp index 82125a28..89905e97 100644 --- a/tests/math/testpimathvectort.cpp +++ b/tests/math/testpimathvectort.cpp @@ -2,188 +2,233 @@ #include "pimathvector.h" #include "pimathmatrix.h" -const uint size = 3u; +const uint SIZE = 3u; -bool cmpVectorWithValue(PIMathVectorT vector, double val, int num) { +bool cmpVectorWithValue(PIMathVectorT vector, double val, int num) { bool b = true; for(int i = 0; i < num; i++) { - if(vector[i] != val) { + if(vector[i] - val >= double(1E-200)) { b = false; } } return b; } -TEST(PIMathVectorT_Test, size) { - PIMathVectorT vector; - ASSERT_TRUE(vector.size() == 3u); +TEST(PIMathVectorT_Test, SIZE) { + PIMathVectorT vector; + ASSERT_TRUE(vector.size() == SIZE); } TEST(PIMathVectorT_Test, fill) { - PIMathVectorT vector; - ASSERT_TRUE(cmpVectorWithValue(vector.fill(5.0), 5.0, 3)); + double a = 5.0; + PIMathVectorT vector; + ASSERT_TRUE(cmpVectorWithValue(vector.fill(a), a, SIZE)); } TEST(PIMathVectorT_Test, set) { - PIMathVectorT vector; - PIMathVectorT vector1; - PIMathVectorT vector2; - ASSERT_TRUE(cmpVectorWithValue(vector.set(vector1.fill(5.0), vector2.fill(3.0)), -2.0, 3)); + double a = 5.0; + double b = 3.0; + PIMathVectorT vector; + PIMathVectorT vector1; + PIMathVectorT vector2; + ASSERT_TRUE(cmpVectorWithValue(vector.set(vector1.fill(a), vector2.fill(b)), b - a, SIZE)); } TEST(PIMathVectorT_Test, MoveVal) { - PIMathVectorT vector; - ASSERT_TRUE(cmpVectorWithValue(vector.move(4.0), 4.0, 3)); + double a = 4.0; + PIMathVectorT vector; + ASSERT_TRUE(cmpVectorWithValue(vector.move(a), a, SIZE)); } TEST(PIMathVectorT_Test, MoveVector) { - PIMathVectorT vector; - PIMathVectorT vector1; - ASSERT_TRUE(cmpVectorWithValue(vector.move(vector1.fill(5.0)), 5.0, 3)); + double a = 5.0; + PIMathVectorT vector; + PIMathVectorT vector1; + ASSERT_TRUE(cmpVectorWithValue(vector.move(vector1.fill(a)), a, SIZE)); } TEST(PIMathVectorT_Test, lengthSqr) { - PIMathVectorT vector; - vector.fill(1.0); - ASSERT_EQ(3.0, vector.lengthSqr()); + double a = 1.0; + PIMathVectorT vector; + vector.fill(a); + ASSERT_DOUBLE_EQ(SIZE * a, vector.lengthSqr()); } TEST(PIMathVectorT_Test, length) { - PIMathVectorT vector; - vector.fill(1.0); - ASSERT_DOUBLE_EQ(sqrt(3.0), vector.length()); + double a = 1.0; + PIMathVectorT vector; + vector.fill(a); + ASSERT_DOUBLE_EQ(sqrt(SIZE * a), vector.length()); } TEST(PIMathVectorT_Test, manhattanLength) { - PIMathVectorT vector; - vector.fill(5.0); - ASSERT_DOUBLE_EQ(15.0, vector.manhattanLength()); + double a = 5.0; + PIMathVectorT vector; + vector.fill(a); + ASSERT_DOUBLE_EQ(SIZE * a, vector.manhattanLength()); } TEST(PIMathVectorT_Test, angleCos) { - PIMathVectorT vector; - PIMathVectorT vec; - vector[0] = 1.0; - vector[1] = 1.0; - vec[1] = 1.0; - ASSERT_DOUBLE_EQ(cos(0.78539816339744830961566084581988), vector.angleCos(vec)); + double a = 1.0; + double angle = 0.78539816339744830961566084581988; + PIMathVectorT vector; + PIMathVectorT vec; + vector[0] = a; + vector[1] = a; + vec[1] = a; + ASSERT_DOUBLE_EQ(cos(angle), vector.angleCos(vec)); } TEST(PIMathVectorT_Test, angleSin) { - PIMathVectorT vector; - PIMathVectorT vec; - vector[0] = 1.0; - vector[1] = 1.0; - vec[1] = 1.0; - ASSERT_DOUBLE_EQ(cos(0.78539816339744830961566084581988), vector.angleSin(vec)); + double a = 1.0; + double angle = 0.78539816339744830961566084581988; + PIMathVectorT vector; + PIMathVectorT vec; + vector[0] = a; + vector[1] = a; + vec[1] = a; + ASSERT_DOUBLE_EQ(sin(angle), vector.angleSin(vec)); } TEST(PIMathVectorT_Test, angleRad) { - PIMathVectorT vector; - PIMathVectorT vec; - vector[0] = 1.0; - vector[1] = 1.0; - vec[1] = 1.0; - ASSERT_DOUBLE_EQ(0.78539816339744830961566084581988, vector.angleRad(vec)); + double a = 1.0; + double angle = 0.78539816339744830961566084581988; + PIMathVectorT vector; + PIMathVectorT vec; + vector[0] = a; + vector[1] = a; + vec[1] = a; + ASSERT_DOUBLE_EQ(angle, vector.angleRad(vec)); } TEST(PIMathVectorT_Test, angleDeg) { - PIMathVectorT vector; - PIMathVectorT vec; - vector[0] = 1.0; - vector[1] = 1.0; - vec[1] = 1.0; - ASSERT_DOUBLE_EQ(45.0, vector.angleDeg(vec)); + double a = 1.0; + double angle = 45.0; + PIMathVectorT vector; + PIMathVectorT vec; + vector[0] = a; + vector[1] = a; + vec[1] = a; + ASSERT_DOUBLE_EQ(angle, vector.angleDeg(vec)); } TEST(PIMathVectorT_Test, angleElevation) { - PIMathVectorT vector; - PIMathVectorT vec; - vector[0] = 1.0; - vector[1] = 1.0; - vec[1] = 1.0; - ASSERT_DOUBLE_EQ(-45.0, vector.angleElevation(vec)); + double a = 1.0; + double angle = 45.0; + PIMathVectorT vector; + PIMathVectorT vec; + vector[0] = a; + vector[1] = a; + vec[1] = a; + ASSERT_DOUBLE_EQ(-angle, vector.angleElevation(vec)); } TEST(PIMathVectorT_Test, projection) { - PIMathVectorT vector; - PIMathVectorT vec; - vec[0] = 1.0; - vector[0] = 1.0; - vector[1] = 1.0; + double a = 2.0; + double b = 2.0; + double res = sqrt(32.0); + PIMathVectorT vector; + PIMathVectorT vec; + vec[0] = a; + vec[2] = b; + vector[0] = a; + vector[1] = b; + vector[2] = a; auto vecProj = vector.projection(vec); - ASSERT_TRUE(vecProj == vec); + ASSERT_DOUBLE_EQ(res, vecProj[0]); + ASSERT_DOUBLE_EQ(0.0, vecProj[1]); + ASSERT_DOUBLE_EQ(res, vecProj[2]); } TEST(PIMathVectorT_Test, normalize) { - PIMathVectorT vector; - vector.fill(5.0); - ASSERT_TRUE(cmpVectorWithValue(vector.normalize(), 5.0 / sqrt(75.0), 3u)); + double a = 5.0; + PIMathVectorT vector; + vector.fill(a); + ASSERT_TRUE(cmpVectorWithValue(vector.normalize(), a / sqrt(SIZE * a * a), SIZE)); } TEST(PIMathVectorT_Test, normalized) { - PIMathVectorT vector; - vector.fill(5.0); - ASSERT_TRUE(cmpVectorWithValue(vector.normalized(), 5.0 / sqrt(75.0), 3u)); + double a = 5.0; + PIMathVectorT vector; + PIMathVectorT vectorNew; + vector.fill(a); + vectorNew = vector.normalized(); + ASSERT_TRUE(cmpVectorWithValue(vectorNew, a / sqrt(SIZE * a * a), SIZE)); } TEST(PIMathVectorT_Test, cross1) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(1.72); - ASSERT_TRUE(cmpVectorWithValue(vector1.cross(vector2), 0.0, 3)); + PIMathVectorT vector1; + PIMathVectorT 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 vector1; - PIMathVectorT vector2; - 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)); + PIMathVectorT vector1; + PIMathVectorT 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) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(5.0); - ASSERT_EQ(vector1.dot(vector2), 90.0); + double a = 6.0; + double b = 5.0; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); + ASSERT_DOUBLE_EQ(vector1.dot(vector2), SIZE * a * b); } TEST(PIMathVectorT_Test, isNullTrue) { - PIMathVectorT vector; + PIMathVectorT vector; ASSERT_TRUE(vector.isNull()); } TEST(PIMathVectorT_Test, isNullFalse) { - PIMathVectorT vector; - vector[0] = 6.273; + double a = 6.273; + PIMathVectorT vector; + vector[0] = a; ASSERT_FALSE(vector.isNull()); } TEST(PIMathVectorT_Test, isOrthoTrue) { - PIMathVectorT vector; - PIMathVectorT vect; - vector[0] = 2.0; - vect[1] = 1.0; + double a = 2.0; + double b = 1.0; + PIMathVectorT vector; + PIMathVectorT vect; + vector[0] = a; + vect[1] = b; ASSERT_TRUE(vector.isOrtho(vect)); } TEST(PIMathVectorT_Test, isOrthoFalse) { - PIMathVectorT vector; - PIMathVectorT vect; - vector[0] = 2.0; - vect[0] = 5.0; - vect[1] = 1.0; + double a = 2.0; + double b = 5.0; + double c = 1.0; + PIMathVectorT vector; + PIMathVectorT vect; + vector[0] = a; + vect[0] = b; + vect[1] = c; ASSERT_FALSE(vector.isOrtho(vect)); } TEST(PIMathVectorT_Test, at) { - PIMathVectorT vector; - vector.fill(5.5); - for(uint i = 0; i < 3u; i++){ - if(vector.at(i) != 5.5){ + double a = 5.5; + PIMathVectorT vector; + vector.fill(a); + for(uint i = 0; i < SIZE; i++){ + if(vector.at(i) != a){ ASSERT_TRUE(false); } } @@ -191,184 +236,226 @@ TEST(PIMathVectorT_Test, at) { } TEST(PIMathVectorT_Test, operator_AssignmentValue) { - PIMathVectorT vector; - vector = 3.0; - ASSERT_TRUE(cmpVectorWithValue(vector, 3.0, 3)); + double a = 3.0; + PIMathVectorT vector; + vector = a; + ASSERT_TRUE(cmpVectorWithValue(vector, a, SIZE)); } TEST(PIMathVectorT_Test, operator_AssignmentVector) { - PIMathVectorT vector; - PIMathVectorT vec; - vec = 5.0; + double a = 5.0; + PIMathVectorT vector; + PIMathVectorT vec; + vec = a; vector = vec; - ASSERT_TRUE(cmpVectorWithValue(vector, 5.0, 3)); + ASSERT_TRUE(cmpVectorWithValue(vector, a, SIZE)); } TEST(PIMathVectorT_Test, operator_EqualTrue) { - PIMathVectorT vector; - PIMathVectorT vec; - vector[0] = 5.12; - vector[1] = 7.34; - vec[0] = 5.12; - vec[1] = 7.34; + double a = 5.12; + double b = 7.34; + PIMathVectorT vector; + PIMathVectorT vec; + vector[0] = a; + vector[1] = b; + vec[0] = a; + vec[1] = b; ASSERT_TRUE(vec == vector); } TEST(PIMathVectorT_Test, operator_EqualFalse) { - PIMathVectorT vector; - PIMathVectorT vec; - vector[0] = 5.12; - vector[1] = 7.34; - vec[0] = 5.12; - vec[1] = 0.34; + double a = 5.12; + double b = 7.34; + double c = 0.34; + PIMathVectorT vector; + PIMathVectorT vec; + vector[0] = a; + vector[1] = b; + vec[0] = a; + vec[1] = c; ASSERT_FALSE(vec == vector); } TEST(PIMathVectorT_Test, operator_Not_EqualTrue) { - PIMathVectorT vector; - PIMathVectorT vec; - vector[0] = 5.12; - vector[1] = 7.34; - vec[0] = 5.12; - vec[1] = 0.34; + double a = 5.12; + double b = 7.34; + double c = 0.34; + PIMathVectorT vector; + PIMathVectorT vec; + vector[0] = a; + vector[1] = b; + vec[0] = a; + vec[1] = c; ASSERT_TRUE(vec != vector); } TEST(PIMathVectorT_Test, operator_Not_EqualFalse) { - PIMathVectorT vector; - PIMathVectorT vec; - vector[0] = 5.12; - vector[1] = 7.34; - vec[0] = 5.12; - vec[1] = 7.34; + double a = 5.12; + double b = 7.34; + PIMathVectorT vector; + PIMathVectorT vec; + vector[0] = a; + vector[1] = b; + vec[0] = a; + vec[1] = b; ASSERT_FALSE(vec != vector); } TEST(PIMathVectorT_Test, operator_Addition_Aassignment) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(1.72); + double a = 6.0; + double b = 1.72; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); vector1 += vector2; - ASSERT_TRUE(cmpVectorWithValue(vector1, 7.72, 3)); + ASSERT_TRUE(cmpVectorWithValue(vector1, a + b, SIZE)); } TEST(PIMathVectorT_Test, operator_Subtraction_Assignment) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(1.72); + double a = 6.0; + double b = 1.72; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); vector1 -= vector2; - ASSERT_TRUE(cmpVectorWithValue(vector1, 4.28, 3)); + ASSERT_TRUE(cmpVectorWithValue(vector1, a - b, SIZE)); } TEST(PIMathVectorT_Test, operator_Multiplication_AssignmentValue) { - PIMathVectorT vector1; - vector1.fill(6.0); - vector1 *= 4.0; - ASSERT_TRUE(cmpVectorWithValue(vector1, 24.0, 3)); + double a = 6.0; + double b = 4.0; + PIMathVectorT vector1; + vector1.fill(a); + vector1 *= b; + ASSERT_TRUE(cmpVectorWithValue(vector1, a * b, SIZE)); } TEST(PIMathVectorT_Test, operator_Multiplication_AssignmentVector) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(1.72); + double a = 6.0; + double b = 1.72; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); vector1 *= vector2; - ASSERT_TRUE(cmpVectorWithValue(vector1, 10.32, 3)); + ASSERT_TRUE(cmpVectorWithValue(vector1, a * b, SIZE)); } TEST(PIMathVectorT_Test, operator_Division_AssignmentValue) { - PIMathVectorT vector1; - vector1.fill(6.0); - vector1 /= 4.0; - ASSERT_TRUE(cmpVectorWithValue(vector1, 1.5, 3)); + double a = 6.0; + double b = 4.0; + PIMathVectorT vector1; + vector1.fill(a); + vector1 /= b; + ASSERT_TRUE(cmpVectorWithValue(vector1, a / b, SIZE)); } TEST(PIMathVectorT_Test, operator_Division_AssignmentVector) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(1.5); + double a = 6.0; + double b = 1.5; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); vector1 /= vector2; - ASSERT_TRUE(cmpVectorWithValue(vector1, 4.0, 3)); + ASSERT_TRUE(cmpVectorWithValue(vector1, a / b, SIZE)); } TEST(PIMathVectorT_Test, operator_Addition) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(1.72); - ASSERT_TRUE(cmpVectorWithValue(vector1 + vector2, 7.72, 3)); + double a = 6.0; + double b = 1.72; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); + ASSERT_TRUE(cmpVectorWithValue(vector1 + vector2, a + b, SIZE)); } TEST(PIMathVectorT_Test, operator_Subtraction) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(1.72); - ASSERT_TRUE(cmpVectorWithValue(vector1 - vector2, 4.28, 3)); + double a = 6.0; + double b = 1.72; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); + ASSERT_TRUE(cmpVectorWithValue(vector1 - vector2, a - b, SIZE)); } TEST(PIMathVectorT_Test, operator_MultiplicationValue) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - ASSERT_TRUE(cmpVectorWithValue(vector1 * 4.0, 24.0, 3)); + double a = 6.0; + double b = 4.0; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + ASSERT_TRUE(cmpVectorWithValue(vector1 * b, a * b, SIZE)); } TEST(PIMathVectorT_Test, operator_MultiplicationVector1) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(1.72); - ASSERT_TRUE(cmpVectorWithValue((vector1 * vector2), 0.0, 3)); + double a = 6.0; + double b = 1.72; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); + ASSERT_TRUE(cmpVectorWithValue((vector1 * vector2), 0.0, SIZE)); } TEST(PIMathVectorT_Test, operator_MultiplicationVector2) { - PIMathVectorT vector1; - PIMathVectorT vector2; - 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)); + double a = 1.0; + PIMathVectorT vector1; + PIMathVectorT 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) { - PIMathVectorT vector1; - vector1.fill(6.0); - ASSERT_TRUE(cmpVectorWithValue(vector1 / 4.0, 1.5, 3)); + double a = 6.0; + double b = 4.0; + PIMathVectorT vector1; + vector1.fill(a); + ASSERT_TRUE(cmpVectorWithValue(vector1 / b, a / b, SIZE)); } TEST(PIMathVectorT_Test, operator_DivisionVector) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(4.0); - ASSERT_TRUE(cmpVectorWithValue(vector1 / vector2, 1.5, 3)); + double a = 6.0; + double b = 4.0; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); + ASSERT_TRUE(cmpVectorWithValue(vector1 / vector2, a / b, SIZE)); } TEST(PIMathVectorT_Test, operator_MultiplVect) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(5.0); - ASSERT_TRUE(cmpVectorWithValue(vector1 & vector2, 30.0, 3)); + double a = 6.0; + double b = 5.0; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); + ASSERT_TRUE(cmpVectorWithValue(vector1 & vector2, a * b, SIZE)); } TEST(PIMathVectorT_Test, operator_absDotProduct) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(5.0); - ASSERT_TRUE(90.0 == (vector1 ^ vector2)); + double a = 6.0; + double b = 5.0; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); + ASSERT_DOUBLE_EQ(vector1 ^ vector2, SIZE * a * b); } TEST(PIMathVectorT_Test, transposed) { - PIMathVectorT vector; - vector.fill(6.0); + double a = 6.0; + PIMathVectorT vector; + vector.fill(a); auto matrix = vector.transposed(); - for(int i = 0; i < size; i++){ - if(matrix[0][i] != 6.0) + for(int i = 0; i < SIZE; i++){ + if(matrix[0][i] != a) { ASSERT_TRUE(false); } @@ -377,51 +464,48 @@ TEST(PIMathVectorT_Test, transposed) { } TEST(PIMathVectorT_Test, filled) { - auto vector = PIMathVectorT::filled(6.0); - ASSERT_TRUE(cmpVectorWithValue(vector, 6.0, 3)); -} - -TEST(PIMathVectorT_Test, distToLine) { - PIMathVectorT vect; - PIMathVectorT vector1; - PIMathVectorT vector2; - vect.fill(6.0); - vector1[0] = 1.0; - vector2[1] = -1.0; - ASSERT_DOUBLE_EQ(vect.distToLine(vector1, vector2), sqrt(2.0)/2.0); + double a = 6.0; + auto vector = PIMathVectorT::filled(a); + ASSERT_TRUE(cmpVectorWithValue(vector, a, SIZE)); } TEST(PIMathVectorT_Test, turnTo) { - PIMathVectorT vect; - vect.fill(6.0); + double a = 6.0; + PIMathVectorT vect; + vect.fill(a); auto vector = vect.turnTo<2u, double>(); - ASSERT_TRUE((vector.size() == 2) && (vector.size() == 2)); + ASSERT_TRUE(vector.size() == 2); } TEST(PIMathVectorT_Test, LogicalOrTrue) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1.fill(6.0); - vector2.fill(1.72); + double a = 6.0; + double b = 1.72; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1.fill(a); + vector2.fill(b); ASSERT_TRUE(vector1 || vector2); } TEST(PIMathVectorT_Test, LogicalOrFalse) { - PIMathVectorT vector1; - PIMathVectorT vector2; - vector1[0] = 1.0; - vector2[1] = 1.0; + double a = 1.0; + PIMathVectorT vector1; + PIMathVectorT vector2; + vector1[0] = a; + vector2[1] = a; ASSERT_FALSE(vector1 || vector2); } TEST(PIMathVectorT_Test, sqrt) { - PIMathVectorT vector1; - vector1.fill(36.0); - ASSERT_TRUE(cmpVectorWithValue(sqrt(vector1), 6.0, 3u)); + double a = 36.0; + PIMathVectorT vector1; + vector1.fill(a); + ASSERT_TRUE(cmpVectorWithValue(sqrt(vector1), sqrt(a), SIZE)); } TEST(PIMathVectorT_Test, sqr) { - PIMathVectorT vector1; - vector1.fill(6.0); - ASSERT_TRUE(cmpVectorWithValue(sqr(vector1), 36.0, 3u)); + double a = 6.0; + PIMathVectorT vector1; + vector1.fill(a); + ASSERT_TRUE(cmpVectorWithValue(sqr(vector1), sqr(a), SIZE)); }