Got vector unit tests compiling

This commit is contained in:
2025-02-03 11:59:49 -05:00
parent ae4806510b
commit 4a25414b92
8 changed files with 137 additions and 58 deletions

View File

@@ -71,7 +71,10 @@
"typeinfo": "cpp", "typeinfo": "cpp",
"variant": "cpp", "variant": "cpp",
"shared_mutex": "cpp", "shared_mutex": "cpp",
"charconv": "cpp" "charconv": "cpp",
"format": "cpp",
"csignal": "cpp",
"span": "cpp"
}, },
"clangd.enable": false, "clangd.enable": false,
"C_Cpp.dimInactiveRegions": false "C_Cpp.dimInactiveRegions": false

View File

@@ -37,12 +37,7 @@ set_target_properties(matrix
# Vector3d # Vector3d
add_library(vector-3d add_library(vector-3d
STATIC STATIC
Vector3D.hpp Vector3D.cpp
)
target_include_directories(vector-3d
PUBLIC
.
) )
target_link_libraries(vector-3d target_link_libraries(vector-3d
@@ -50,8 +45,3 @@ target_link_libraries(vector-3d
vector-3d-intf vector-3d-intf
PRIVATE PRIVATE
) )
set_target_properties(vector-3d
PROPERTIES
LINKER_LANGUAGE CXX
)

View File

@@ -1,15 +1,26 @@
#ifdef MATRIX_H_ // since the .cpp file has to be included by the .hpp file this #ifdef VECTOR3D_H_ // since the .cpp file has to be included by the .hpp file this
// will evaluate to true // will evaluate to true
#include "Vector3D.hpp" #include <cmath>
#include <type_traits>
template <typename Type> template <typename Type>
constexpr V3D<type>::V3D(const Matrix<1, 3> &other) : x(other[0][0]), y(other[0][1]), z(other[0][2]) {} V3D<Type>::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 <typename Type> template <typename Type>
constexpr V3D<type>::V3D(const Matrix<3, 1> &other) : x(other[0][0]), y(other[1][0]), z(other[2][0]) {} V3D<Type>::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 <typename Type> template <typename Type>
constexpr V3D<type>::V3D(const V3D &other) : x(other.x), V3D<Type>::V3D(const V3D &other) : x(other.x),
y(other.y), y(other.y),
z(other.z) z(other.z)
{ {
@@ -17,57 +28,72 @@ constexpr V3D<type>::V3D(const V3D &other) : x(other.x),
} }
template <typename Type> template <typename Type>
constexpr V3D<type>::V3D(Type x = 0, Type y = 0, Type z = 0) : x(x), V3D<Type>::V3D(Type x, Type y, Type z) : x(x),
y(y), y(y),
z(z) z(z)
{ {
static_assert(std::is_arithmetic<Type>::value, "Type must be a number"); static_assert(std::is_arithmetic<Type>::value, "Type must be a number");
} }
template <typename Type, typename OtherType> template <typename Type>
constexpr V3D<type>::V3D(const V3D<OtherType> other) : x(static_cast<Type>(other.x)), template <typename OtherType>
y(static_cast<Type>(other.y)), V3D<Type>::V3D(const V3D<OtherType> &other)
z(static_cast<Type>(other.z))
{ {
static_assert(std::is_arithmetic<Type>::value, "Type must be a number"); static_assert(std::is_arithmetic<Type>::value, "Type must be a number");
static_assert(std::is_arithmetic<OtherType>::value, "OtherType must be a number"); static_assert(std::is_arithmetic<OtherType>::value, "OtherType must be a number");
this->x = static_cast<Type>(other.x);
this->y = static_cast<Type>(other.y);
this->z = static_cast<Type>(other.z);
} }
template <typename Type> template <typename Type>
std::array<Type, 3> V3D<type>::ToArray() std::array<Type, 3> V3D<Type>::ToArray()
{ {
return {this->x, this->y, this->z}; return {this->x, this->y, this->z};
} }
template <typename Type> template <typename Type>
V3D &V3D<type>::operator=(const V3D &other) void V3D<Type>::operator=(const V3D<Type> &other)
{ {
this->x = other.x; this->x = other.x;
this->y = other.y; this->y = other.y;
this->z = other.z; this->z = other.z;
return *this;
} }
template <typename Type> template <typename Type>
V3D &V3D<type>::operator+=(const V3D &other) V3D<Type> V3D<Type>::operator+(const V3D<Type> &other)
{ {
this->x += other.x; return V3D<Type>{this->x + other.x, this->y + other.y, this->z + other.z};
this->y += other.y;
this->z += other.z;
return *this;
} }
template <typename Type> template <typename Type>
V3D &V3D<type>::operator-=(const V3D &other) V3D<Type> V3D<Type>::operator-(const V3D<Type> &other)
{ {
this->x -= other.x; return V3D<Type>{this->x - other.x, this->y - other.y, this->z - other.z};
this->y -= other.y; }
this->z -= other.z;
template <typename Type>
V3D<Type> V3D<Type>::operator*(Type scalar)
{
return V3D<Type>{this->x * scalar, this->y * scalar, this->z * scalar};
}
template <typename Type>
V3D<Type> &V3D<Type>::operator+=(const V3D<Type> &other)
{
*this = *this + other;
return *this; return *this;
} }
template <typename Type> template <typename Type>
V3D &V3D<type>::operator/=(const Type scalar) V3D<Type> &V3D<Type>::operator-=(const V3D<Type> &other)
{
*this = *this - other;
return *this;
}
template <typename Type>
V3D<Type> &V3D<Type>::operator/=(Type scalar)
{ {
if (scalar == 0) if (scalar == 0)
{ {
@@ -80,7 +106,7 @@ V3D &V3D<type>::operator/=(const Type scalar)
} }
template <typename Type> template <typename Type>
V3D &V3D<type>::operator*=(const Type scalar) V3D<Type> &V3D<Type>::operator*=(Type scalar)
{ {
this->x *= scalar; this->x *= scalar;
this->y *= scalar; this->y *= scalar;
@@ -89,15 +115,15 @@ V3D &V3D<type>::operator*=(const Type scalar)
} }
template <typename Type> template <typename Type>
bool V3D<type>::operator==(const V3D &other) bool V3D<Type>::operator==(const V3D<Type> &other)
{ {
return this->x == other.x && this->y == other.y && this->z == other.z; return this->x == other.x && this->y == other.y && this->z == other.z;
} }
template <typename Type> template <typename Type>
float V3D<type>::magnitude() float V3D<Type>::magnitude()
{ {
return std::sqrt(static_cast<float>(this->x * this->x + this->y * this->y + this->z * this->z)); return std::sqrt(static_cast<float>(this->x * this->x + this->y * this->y + this->z * this->z));
} }
#endif // MATRIX_H_ #endif // VECTOR3D_H_

View File

@@ -2,43 +2,48 @@
#define VECTOR3D_H_ #define VECTOR3D_H_
#include <cstdint> #include <cstdint>
#include <cmath>
#include <type_traits>
#include "Matrix.hpp" #include "Matrix.hpp"
template <typename Type> template <typename Type>
class V3D class V3D
{ {
public: public:
constexpr V3D(const Matrix<1, 3> &other); V3D(const Matrix<1, 3> &other);
constexpr V3D(const Matrix<3, 1> &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 <typename OtherType> template <typename OtherType>
constexpr V3D(const V3D<OtherType> other); V3D(const V3D<OtherType> &other);
std::array<Type, 3> ToArray(); std::array<Type, 3> ToArray();
V3D &operator=(const V3D &other); V3D<Type> operator+(const V3D<Type> &other);
V3D &operator+=(const V3D &other); V3D<Type> operator-(const V3D<Type> &other);
V3D &operator-=(const V3D &other); V3D<Type> operator*(Type scalar);
V3D &operator/=(const Type scalar); void operator=(const V3D<Type> &other);
V3D &operator*=(const Type scalar); V3D<Type> &operator+=(const V3D<Type> &other);
bool operator==(const V3D &other); V3D<Type> &operator-=(const V3D<Type> &other);
V3D<Type> &operator/=(Type scalar);
V3D<Type> &operator*=(Type scalar);
bool operator==(const V3D<Type> &other);
float magnitude(); float magnitude();
Type x; Type x;
Type y; Type y;
Type z; Type z;
}; };
#include "Vector3D.cpp"
#endif // VECTOR3D_H_ #endif // VECTOR3D_H_

View File

@@ -12,6 +12,7 @@ FetchContent_Declare(
FetchContent_MakeAvailable(Catch2) FetchContent_MakeAvailable(Catch2)
# matrix tests
add_executable(matrix-tests matrix-tests.cpp) add_executable(matrix-tests matrix-tests.cpp)
target_link_libraries(matrix-tests target_link_libraries(matrix-tests
@@ -19,3 +20,12 @@ target_link_libraries(matrix-tests
vector-3d-intf vector-3d-intf
Catch2::Catch2WithMain Catch2::Catch2WithMain
) )
# vector tests
add_executable(vector-tests vector-tests.cpp)
target_link_libraries(vector-tests
PRIVATE
vector-3d-intf
Catch2::Catch2WithMain
)

View File

@@ -0,0 +1,45 @@
// include the unit test framework first
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
// include the module you're going to test next
#include "Vector3D.hpp"
#include "Matrix.hpp"
// any other libraries
#include <array>
#include <cmath>
#include <iostream>
TEST_CASE("Vector Math", "Vector")
{
V3D<float> v1{1, 2, 3};
V3D<float> v2{4, 5, 6};
V3D<float> v3{};
SECTION("Initialization")
{
// list initialization
REQUIRE(v1.x == 1);
REQUIRE(v1.y == 2);
REQUIRE(v1.z == 3);
// copy initialization
V3D<float> 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<float> v5{mat1};
REQUIRE(v5.x == v1.x);
REQUIRE(v5.y == v1.y);
REQUIRE(v5.z == v1.z);
}
}