Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
user-simon committed Nov 21, 2021
1 parent 4a1b798 commit f0febaf
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 33 deletions.
11 changes: 10 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_subdirectory(googletest)

project(tests)
add_executable(tests common.h comparisons.cpp constructors.cpp conversions.cpp math.cpp serialization.cpp traits.cpp)
add_executable(tests
common.h
comparisons.cpp
constructors.cpp
conversions.cpp
math.cpp
serialization.cpp
std_integration.cpp
traits.cpp
)
include_directories(tests ../include googletest/googletest/include)
target_link_libraries(tests PUBLIC gtest_main)
14 changes: 10 additions & 4 deletions tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ using operation_t = decltype(std::declval<vector<T, Size>>() + std::declval<vect
template<class Scalar>
inline Scalar random_scalar()
{
//static std::random_device _device;
static std::mt19937 rng = std::mt19937(std::random_device()());
static std::uniform_int_distribution<std::mt19937::result_type> dist;
auto scalar = dist(rng);

return *(Scalar*)&scalar;
if constexpr(std::is_floating_point_v<Scalar>)
{
static std::uniform_real_distribution<double> dist;
return static_cast<Scalar>(dist(rng));
}
else
{
static std::uniform_int_distribution<uint64_t> dist;
return static_cast<Scalar>(dist(rng));
}
}

template<class Vector>
Expand Down
4 changes: 2 additions & 2 deletions tests/constructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ TYPED_TEST(ConstructorsAll, Repeated)

TEST(Constructors, ComponentNames)
{
int2d a { 1, 2 };
int2d b { 3, 4 };
int2d a { 1.0, 2 };
int2d b { 3.0, 4 };

// make sure components get bound to correct data
EXPECT_EQ(a.x, 1);
Expand Down
64 changes: 38 additions & 26 deletions tests/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ template<class T>
struct MathAll : testing::Test {};
TYPED_TEST_SUITE(MathAll, all_vectors);

template<class T>
struct MathSigned : testing::Test {};
TYPED_TEST_SUITE(MathSigned, signed_vectors);

template<class T>
struct MathFloating : testing::Test {};
TYPED_TEST_SUITE(MathFloating, floating_vectors);

TYPED_TEST(MathAll, Expressions)
{
USING_TYPE_INFO
Expand All @@ -23,10 +15,10 @@ TYPED_TEST(MathAll, Expressions)
auto d = *(1 + -(a + b) * c * 0.5);

EXPECT_EQ(decltype(d)::size, size);
testing::StaticAssertTypeEq<decltype(d)::scalar_t, double>();
testing::StaticAssertTypeEq<typename decltype(d)::scalar_t, double>();

for (size_t i = 0; i < size; i++)
EXPECT_EQ(1 - (a[i] + b[i]) * c[i] * 0.5, d[i]);
EXPECT_EQ(1 + -(a[i] + b[i]) * c[i] * 0.5, d[i]);
}

TYPED_TEST(MathAll, Util)
Expand All @@ -53,30 +45,50 @@ TYPED_TEST(MathAll, Util)
EXPECT_NE(a.product(), 0);
}

TYPED_TEST(MathSigned, Algebra)
TYPED_TEST(MathAll, LinearAlgebra)
{
USING_TYPE_INFO

auto a = vector_t::zero;
auto b = vector_t::identity;

vector_t a = random_vector<vector_t>();
a[0] = -1;

vector_t b = a.abs();

for (size_t i = 0; i < size; i++)
EXPECT_EQ(b[i], std::abs(a[i]));
EXPECT_EQ(a.distance2(b), size);
}

TYPED_TEST(MathFloating, Algebra)
TEST(Math, Length)
{

uint2d a(1, 1);
int2d b(-5, -5);

EXPECT_DOUBLE_EQ(a.length(), std::sqrt(2));
EXPECT_DOUBLE_EQ(a.distance(b), uint2d(6, 6).length());
EXPECT_DOUBLE_EQ(a.normalize().length(), 1);
EXPECT_DOUBLE_EQ(b.set_length(100).length(), 100);
}

TYPED_TEST(MathAll, LinearAlgebra)
TEST(Math, LinearAlgebra)
{
USING_TYPE_INFO

auto a = vector_t::zero;
auto b = vector_t::identity;
double2d a(1, 0);
double2d b(0, 1);

EXPECT_EQ(a.distance2(b), size);
// angles
EXPECT_DOUBLE_EQ(std::cos(a.angle()), 1);
EXPECT_DOUBLE_EQ(std::sin(a.angle()), 0);
EXPECT_DOUBLE_EQ(a.delta_angle(b), b.angle());

// dot p
EXPECT_DOUBLE_EQ(a.dot(b), 0);

double2d c(1.2, 3.4);
double2d d(5.6, 7.8);
EXPECT_DOUBLE_EQ(c.dot(d), 1.2 * 5.6 + 3.4 * 7.8);

// cross p
int3d e(1, 2, 3);
int3d f(4, 5, 6);

EXPECT_EQ(e.cross(f), int3d(-3, 6, -3));

// from angle
EXPECT_EQ(double2d::from_angle(a.angle()), a);
}
13 changes: 13 additions & 0 deletions tests/unordered_key.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "common.h"
#include <unordered_set>

TEST(unordered, set)
{
std::unordered_set<float2d> vectors;

for (uint32_t i = 0; i < 10; i++)
vectors.insert({ i / 3, i / 5 });

for (uint32_t i = 0; i < 10; i++)
EXPECT_EQ(vectors.count({ i / 3, i / 5 }), 1);
}

0 comments on commit f0febaf

Please sign in to comment.