From 4a25414b9223c6228b9e788351f9034b73a2f2ea Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 3 Feb 2025 11:59:49 -0500 Subject: [PATCH] Got vector unit tests compiling --- .../index/Matrix.hpp.B9C1D36F0ACA1714.idx | Bin 5336 -> 0 bytes .../matrix-tests.cpp.88D512D6040A12A3.idx | Bin 3174 -> 0 bytes .vscode/settings.json | 5 +- CMakeLists.txt | 12 +-- Vector3D.cpp | 90 +++++++++++------- Vector3D.hpp | 33 ++++--- unit-tests/CMakeLists.txt | 10 ++ unit-tests/vector-tests.cpp | 45 +++++++++ 8 files changed, 137 insertions(+), 58 deletions(-) delete mode 100644 .cache/clangd/index/Matrix.hpp.B9C1D36F0ACA1714.idx delete mode 100644 .cache/clangd/index/matrix-tests.cpp.88D512D6040A12A3.idx create mode 100644 unit-tests/vector-tests.cpp diff --git a/.cache/clangd/index/Matrix.hpp.B9C1D36F0ACA1714.idx b/.cache/clangd/index/Matrix.hpp.B9C1D36F0ACA1714.idx deleted file mode 100644 index 34b906c8263a0bb74a85f68f71a823cbecc34062..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5336 zcmZ8k33OD|8J_>YOy10!JHrbznM+`@?<`qpOk7YPwji5eWvQ(Pp*Et_G!QUFZCV>r z3CNNFiHR(QvIIEMKnMmB77s{(qL>6wK@jw47HH3LScGag_D*L0+-c_|^X9wvd-uJ2 z|Nr~$e}6fC?ATV9$@KW|#xKsxT{Nek$z*cM|AmVRUdjNIX(pOXi#Mz+pF9OKj5j-L z=6^7FFQmP&B!0+;i=zi+Ji8($_mR=#M^$#a`W^dAaJhExgj{GfOTyEUA zGvex#2gdx_>3DbS!mGQUKA7FsG{0-i-RukVdf>JBb*|gxb2fK&wY2}7r>DQzw&1zC z{l^w&u6Xm6Iq!eBuB#zxN_NlUxWmVvzul2t@lfujD}$EJNv^&CSWP@Pp}jd6eZk6&$1dboVT#E%{- zUGVUuS8o5}qW86@Tn|Js>%R+}Vat1V-z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BL zP35&0)4iCb@J$?Y6Vny$L9GYvY9A?P=3-`3*}jCuEnz7NmoQ5Sb0}QO%%#kx_LXHU zaTyz?a5)PpXI$x{oTZktOoi95)HT6)12b=6Hnq=eXXfq9ru4Ie_1nR8g=-krFpI+V z%v{fGN?-LXxt^see2m$Su~3Cun7M`7l>S;+LJJ$Ja62=%Gn>+5J4goSk5w|v)ABeuJD1k1Z39kT?ZX4Mfl#;>QfeVp-Myb_`x&Q~q$9HQc>npB z)J-aq9`8F2Q?oAoXJo>Z+;VI39g3-$i!0!~0)2k02Tsn^Po5x0_Z^j~Sr43_2~#Ra zn(x3&&3fPzO_(x5zJ%}COwIB{1^g0((Ny%n*_!&v_+%mOJ4{nE{+|z0ohg^df3hYb z317aQ?;uUhvh?mfT@x diff --git a/.cache/clangd/index/matrix-tests.cpp.88D512D6040A12A3.idx b/.cache/clangd/index/matrix-tests.cpp.88D512D6040A12A3.idx deleted file mode 100644 index f7707e067e55300c1dc9039859394c655b473160..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3174 zcmWIYbaRX2VPJ4h@vO*AElFfyU|`Dtz|5PufOA*uYLky%+e{KYqdtC~e#tjUevz%p#p=4cjl#wZYzy|}op63Ca`x24 zj)PMC>y-Yi*t+${le^LmmYd6^T|6^< zf*pk)tXUeYBE4nvL+fa%MDNyHZ5llXKkCYV)-22U*V?0JrPg}0p{>ef>s3Le)+3=x zRkg1erXN>6Y*nGN<>q=)*E zQgiGp?`-rxyhUtzab<4OY+$Ia3Fxr#TzgZNmqCDmL4=EeixW&RfI=Mz9;badwf&%? zDh~rMHtEct9-M7WS0Z>A_<_=Fyli4%0-~3JVV)TK$BsL;={yX4Kxsh+K~69MGrajk z*j2&br5U^og4m=Pic-^xxwse@W+yIAaD7KHlusknTI|NQf^u8 zMt6%9j_hHSh}}egix-zj9VxO};ej{7KaMzXHAIcup9?jb$WPgz{0YlVEF$Ekb6A1E z#)VQ4@QAQ0uxr93c|^FmxP@U!kVk}viwB9v%f*Yt-PKs2o7> zK;;08=cvYO{<-*OKHbx@*?Y|mru>_0=x*_)->@)+h7rPUd{P+21L=Vc|UC=EXd~?9aue3@hh>JXJ0u1drTm zPLNAb8kTv0SxQz=3&A6=Xjq#UGBv-}Zynte!iAtmi0Rb1M!%d{V`7@hmVE3P}pdA(<;F4ChVt zJ5~_4M(hzIkBAt%7#B=Ys?JZos!J!H;n0DGTW^KzYfzDLKIACj%D`4-+#BD?5iM8z&<-0M$$2{{R30 diff --git a/.vscode/settings.json b/.vscode/settings.json index d269529..b25d6b6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -71,7 +71,10 @@ "typeinfo": "cpp", "variant": "cpp", "shared_mutex": "cpp", - "charconv": "cpp" + "charconv": "cpp", + "format": "cpp", + "csignal": "cpp", + "span": "cpp" }, "clangd.enable": false, "C_Cpp.dimInactiveRegions": false diff --git a/CMakeLists.txt b/CMakeLists.txt index 9337e92..437c0c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,21 +37,11 @@ set_target_properties(matrix # Vector3d add_library(vector-3d STATIC - Vector3D.hpp -) - -target_include_directories(vector-3d - PUBLIC - . + Vector3D.cpp ) target_link_libraries(vector-3d PUBLIC vector-3d-intf PRIVATE -) - -set_target_properties(vector-3d - PROPERTIES - LINKER_LANGUAGE CXX ) \ No newline at end of file diff --git a/Vector3D.cpp b/Vector3D.cpp index 9c5182c..350851a 100644 --- a/Vector3D.cpp +++ b/Vector3D.cpp @@ -1,73 +1,99 @@ -#ifdef MATRIX_H_ // since the .cpp file has to be included by the .hpp file this - // will evaluate to true -#include "Vector3D.hpp" +#ifdef VECTOR3D_H_ // since the .cpp file has to be included by the .hpp file this + // will evaluate to true +#include +#include template -constexpr V3D::V3D(const Matrix<1, 3> &other) : x(other[0][0]), y(other[0][1]), z(other[0][2]) {} +V3D::V3D(const Matrix<1, 3> &other) +{ + this->x = other.Get(0, 0); + this->y = other.Get(0, 1); + this->z = other.Get(0, 2); +} template -constexpr V3D::V3D(const Matrix<3, 1> &other) : x(other[0][0]), y(other[1][0]), z(other[2][0]) {} +V3D::V3D(const Matrix<3, 1> &other) +{ + this->x = other.Get(0, 0); + this->y = other.Get(1, 0); + this->z = other.Get(2, 0); +} template -constexpr V3D::V3D(const V3D &other) : x(other.x), - y(other.y), - z(other.z) +V3D::V3D(const V3D &other) : x(other.x), + y(other.y), + z(other.z) { static_assert(std::is_arithmetic::value, "Type must be a number"); } template -constexpr V3D::V3D(Type x = 0, Type y = 0, Type z = 0) : x(x), - y(y), - z(z) +V3D::V3D(Type x, Type y, Type z) : x(x), + y(y), + z(z) { static_assert(std::is_arithmetic::value, "Type must be a number"); } -template -constexpr V3D::V3D(const V3D other) : x(static_cast(other.x)), - y(static_cast(other.y)), - z(static_cast(other.z)) +template +template +V3D::V3D(const V3D &other) { static_assert(std::is_arithmetic::value, "Type must be a number"); static_assert(std::is_arithmetic::value, "OtherType must be a number"); + this->x = static_cast(other.x); + this->y = static_cast(other.y); + this->z = static_cast(other.z); } template -std::array V3D::ToArray() +std::array V3D::ToArray() { return {this->x, this->y, this->z}; } template -V3D &V3D::operator=(const V3D &other) +void V3D::operator=(const V3D &other) { this->x = other.x; this->y = other.y; this->z = other.z; - return *this; } template -V3D &V3D::operator+=(const V3D &other) +V3D V3D::operator+(const V3D &other) { - this->x += other.x; - this->y += other.y; - this->z += other.z; - return *this; + return V3D{this->x + other.x, this->y + other.y, this->z + other.z}; } template -V3D &V3D::operator-=(const V3D &other) +V3D V3D::operator-(const V3D &other) { - this->x -= other.x; - this->y -= other.y; - this->z -= other.z; + return V3D{this->x - other.x, this->y - other.y, this->z - other.z}; +} + +template +V3D V3D::operator*(Type scalar) +{ + return V3D{this->x * scalar, this->y * scalar, this->z * scalar}; +} + +template +V3D &V3D::operator+=(const V3D &other) +{ + *this = *this + other; return *this; } template -V3D &V3D::operator/=(const Type scalar) +V3D &V3D::operator-=(const V3D &other) +{ + *this = *this - other; + return *this; +} + +template +V3D &V3D::operator/=(Type scalar) { if (scalar == 0) { @@ -80,7 +106,7 @@ V3D &V3D::operator/=(const Type scalar) } template -V3D &V3D::operator*=(const Type scalar) +V3D &V3D::operator*=(Type scalar) { this->x *= scalar; this->y *= scalar; @@ -89,15 +115,15 @@ V3D &V3D::operator*=(const Type scalar) } template -bool V3D::operator==(const V3D &other) +bool V3D::operator==(const V3D &other) { return this->x == other.x && this->y == other.y && this->z == other.z; } template -float V3D::magnitude() +float V3D::magnitude() { return std::sqrt(static_cast(this->x * this->x + this->y * this->y + this->z * this->z)); } -#endif // MATRIX_H_ \ No newline at end of file +#endif // VECTOR3D_H_ \ No newline at end of file diff --git a/Vector3D.hpp b/Vector3D.hpp index c35ada0..818a565 100644 --- a/Vector3D.hpp +++ b/Vector3D.hpp @@ -2,43 +2,48 @@ #define VECTOR3D_H_ #include -#include -#include - #include "Matrix.hpp" template class V3D { public: - constexpr V3D(const Matrix<1, 3> &other); - constexpr V3D(const Matrix<3, 1> &other); + V3D(const Matrix<1, 3> &other); + V3D(const Matrix<3, 1> &other); - constexpr V3D(const V3D &other); + V3D(const V3D &other); - constexpr V3D(Type x = 0, Type y = 0, Type z = 0); + V3D(Type x = 0, Type y = 0, Type z = 0); template - constexpr V3D(const V3D other); + V3D(const V3D &other); std::array ToArray(); - V3D &operator=(const V3D &other); + V3D operator+(const V3D &other); - V3D &operator+=(const V3D &other); + V3D operator-(const V3D &other); - V3D &operator-=(const V3D &other); + V3D operator*(Type scalar); - V3D &operator/=(const Type scalar); + void operator=(const V3D &other); - V3D &operator*=(const Type scalar); + V3D &operator+=(const V3D &other); - bool operator==(const V3D &other); + V3D &operator-=(const V3D &other); + + V3D &operator/=(Type scalar); + + V3D &operator*=(Type scalar); + + bool operator==(const V3D &other); float magnitude(); + Type x; Type y; Type z; }; +#include "Vector3D.cpp" #endif // VECTOR3D_H_ \ No newline at end of file diff --git a/unit-tests/CMakeLists.txt b/unit-tests/CMakeLists.txt index fd8684e..b464674 100644 --- a/unit-tests/CMakeLists.txt +++ b/unit-tests/CMakeLists.txt @@ -12,10 +12,20 @@ FetchContent_Declare( FetchContent_MakeAvailable(Catch2) +# matrix tests add_executable(matrix-tests matrix-tests.cpp) target_link_libraries(matrix-tests PRIVATE vector-3d-intf Catch2::Catch2WithMain +) + +# vector tests +add_executable(vector-tests vector-tests.cpp) + +target_link_libraries(vector-tests + PRIVATE + vector-3d-intf + Catch2::Catch2WithMain ) \ No newline at end of file diff --git a/unit-tests/vector-tests.cpp b/unit-tests/vector-tests.cpp new file mode 100644 index 0000000..60c18b8 --- /dev/null +++ b/unit-tests/vector-tests.cpp @@ -0,0 +1,45 @@ +// include the unit test framework first +#include +#include + +// include the module you're going to test next +#include "Vector3D.hpp" +#include "Matrix.hpp" + +// any other libraries +#include +#include +#include + +TEST_CASE("Vector Math", "Vector") +{ + V3D v1{1, 2, 3}; + V3D v2{4, 5, 6}; + V3D v3{}; + + SECTION("Initialization") + { + // list initialization + REQUIRE(v1.x == 1); + REQUIRE(v1.y == 2); + REQUIRE(v1.z == 3); + + // copy initialization + V3D v4{v2}; + REQUIRE(v4.x == 4); + REQUIRE(v4.y == 5); + REQUIRE(v4.z == 6); + + // empty initialization + REQUIRE(v3.x == 0); + REQUIRE(v3.y == 0); + REQUIRE(v3.z == 0); + + // matrix initialization + Matrix<1, 3> mat1{v1.ToArray()}; + V3D v5{mat1}; + REQUIRE(v5.x == v1.x); + REQUIRE(v5.y == v1.y); + REQUIRE(v5.z == v1.z); + } +} \ No newline at end of file