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",
"variant": "cpp",
"shared_mutex": "cpp",
"charconv": "cpp"
"charconv": "cpp",
"format": "cpp",
"csignal": "cpp",
"span": "cpp"
},
"clangd.enable": false,
"C_Cpp.dimInactiveRegions": false

View File

@@ -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
)

View File

@@ -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 <cmath>
#include <type_traits>
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>
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>
constexpr V3D<type>::V3D(const V3D &other) : x(other.x),
y(other.y),
z(other.z)
V3D<Type>::V3D(const V3D &other) : x(other.x),
y(other.y),
z(other.z)
{
static_assert(std::is_arithmetic<Type>::value, "Type must be a number");
}
template <typename Type>
constexpr V3D<type>::V3D(Type x = 0, Type y = 0, Type z = 0) : x(x),
y(y),
z(z)
V3D<Type>::V3D(Type x, Type y, Type z) : x(x),
y(y),
z(z)
{
static_assert(std::is_arithmetic<Type>::value, "Type must be a number");
}
template <typename Type, typename OtherType>
constexpr V3D<type>::V3D(const V3D<OtherType> other) : x(static_cast<Type>(other.x)),
y(static_cast<Type>(other.y)),
z(static_cast<Type>(other.z))
template <typename Type>
template <typename OtherType>
V3D<Type>::V3D(const V3D<OtherType> &other)
{
static_assert(std::is_arithmetic<Type>::value, "Type 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>
std::array<Type, 3> V3D<type>::ToArray()
std::array<Type, 3> V3D<Type>::ToArray()
{
return {this->x, this->y, this->z};
}
template <typename Type>
V3D &V3D<type>::operator=(const V3D &other)
void V3D<Type>::operator=(const V3D<Type> &other)
{
this->x = other.x;
this->y = other.y;
this->z = other.z;
return *this;
}
template <typename Type>
V3D &V3D<type>::operator+=(const V3D &other)
V3D<Type> V3D<Type>::operator+(const V3D<Type> &other)
{
this->x += other.x;
this->y += other.y;
this->z += other.z;
return *this;
return V3D<Type>{this->x + other.x, this->y + other.y, this->z + other.z};
}
template <typename Type>
V3D &V3D<type>::operator-=(const V3D &other)
V3D<Type> V3D<Type>::operator-(const V3D<Type> &other)
{
this->x -= other.x;
this->y -= other.y;
this->z -= other.z;
return V3D<Type>{this->x - other.x, 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;
}
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)
{
@@ -80,7 +106,7 @@ V3D &V3D<type>::operator/=(const Type scalar)
}
template <typename Type>
V3D &V3D<type>::operator*=(const Type scalar)
V3D<Type> &V3D<Type>::operator*=(Type scalar)
{
this->x *= scalar;
this->y *= scalar;
@@ -89,15 +115,15 @@ V3D &V3D<type>::operator*=(const Type scalar)
}
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;
}
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));
}
#endif // MATRIX_H_
#endif // VECTOR3D_H_

View File

@@ -2,43 +2,48 @@
#define VECTOR3D_H_
#include <cstdint>
#include <cmath>
#include <type_traits>
#include "Matrix.hpp"
template <typename Type>
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 <typename OtherType>
constexpr V3D(const V3D<OtherType> other);
V3D(const V3D<OtherType> &other);
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();
Type x;
Type y;
Type z;
};
#include "Vector3D.cpp"
#endif // VECTOR3D_H_

View File

@@ -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
)

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);
}
}