Skip to content

Commit f0febaf

Browse files
committed
Added tests
1 parent 4a1b798 commit f0febaf

File tree

5 files changed

+73
-33
lines changed

5 files changed

+73
-33
lines changed

tests/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
55
add_subdirectory(googletest)
66

77
project(tests)
8-
add_executable(tests common.h comparisons.cpp constructors.cpp conversions.cpp math.cpp serialization.cpp traits.cpp)
8+
add_executable(tests
9+
common.h
10+
comparisons.cpp
11+
constructors.cpp
12+
conversions.cpp
13+
math.cpp
14+
serialization.cpp
15+
std_integration.cpp
16+
traits.cpp
17+
)
918
include_directories(tests ../include googletest/googletest/include)
1019
target_link_libraries(tests PUBLIC gtest_main)

tests/common.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ using operation_t = decltype(std::declval<vector<T, Size>>() + std::declval<vect
1010
template<class Scalar>
1111
inline Scalar random_scalar()
1212
{
13-
//static std::random_device _device;
1413
static std::mt19937 rng = std::mt19937(std::random_device()());
15-
static std::uniform_int_distribution<std::mt19937::result_type> dist;
16-
auto scalar = dist(rng);
1714

18-
return *(Scalar*)&scalar;
15+
if constexpr(std::is_floating_point_v<Scalar>)
16+
{
17+
static std::uniform_real_distribution<double> dist;
18+
return static_cast<Scalar>(dist(rng));
19+
}
20+
else
21+
{
22+
static std::uniform_int_distribution<uint64_t> dist;
23+
return static_cast<Scalar>(dist(rng));
24+
}
1925
}
2026

2127
template<class Vector>

tests/constructors.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ TYPED_TEST(ConstructorsAll, Repeated)
5252

5353
TEST(Constructors, ComponentNames)
5454
{
55-
int2d a { 1, 2 };
56-
int2d b { 3, 4 };
55+
int2d a { 1.0, 2 };
56+
int2d b { 3.0, 4 };
5757

5858
// make sure components get bound to correct data
5959
EXPECT_EQ(a.x, 1);

tests/math.cpp

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ template<class T>
44
struct MathAll : testing::Test {};
55
TYPED_TEST_SUITE(MathAll, all_vectors);
66

7-
template<class T>
8-
struct MathSigned : testing::Test {};
9-
TYPED_TEST_SUITE(MathSigned, signed_vectors);
10-
11-
template<class T>
12-
struct MathFloating : testing::Test {};
13-
TYPED_TEST_SUITE(MathFloating, floating_vectors);
14-
157
TYPED_TEST(MathAll, Expressions)
168
{
179
USING_TYPE_INFO
@@ -23,10 +15,10 @@ TYPED_TEST(MathAll, Expressions)
2315
auto d = *(1 + -(a + b) * c * 0.5);
2416

2517
EXPECT_EQ(decltype(d)::size, size);
26-
testing::StaticAssertTypeEq<decltype(d)::scalar_t, double>();
18+
testing::StaticAssertTypeEq<typename decltype(d)::scalar_t, double>();
2719

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

3224
TYPED_TEST(MathAll, Util)
@@ -53,30 +45,50 @@ TYPED_TEST(MathAll, Util)
5345
EXPECT_NE(a.product(), 0);
5446
}
5547

56-
TYPED_TEST(MathSigned, Algebra)
48+
TYPED_TEST(MathAll, LinearAlgebra)
5749
{
5850
USING_TYPE_INFO
51+
52+
auto a = vector_t::zero;
53+
auto b = vector_t::identity;
5954

60-
vector_t a = random_vector<vector_t>();
61-
a[0] = -1;
62-
63-
vector_t b = a.abs();
64-
65-
for (size_t i = 0; i < size; i++)
66-
EXPECT_EQ(b[i], std::abs(a[i]));
55+
EXPECT_EQ(a.distance2(b), size);
6756
}
6857

69-
TYPED_TEST(MathFloating, Algebra)
58+
TEST(Math, Length)
7059
{
71-
60+
uint2d a(1, 1);
61+
int2d b(-5, -5);
62+
63+
EXPECT_DOUBLE_EQ(a.length(), std::sqrt(2));
64+
EXPECT_DOUBLE_EQ(a.distance(b), uint2d(6, 6).length());
65+
EXPECT_DOUBLE_EQ(a.normalize().length(), 1);
66+
EXPECT_DOUBLE_EQ(b.set_length(100).length(), 100);
7267
}
7368

74-
TYPED_TEST(MathAll, LinearAlgebra)
69+
TEST(Math, LinearAlgebra)
7570
{
76-
USING_TYPE_INFO
77-
78-
auto a = vector_t::zero;
79-
auto b = vector_t::identity;
71+
double2d a(1, 0);
72+
double2d b(0, 1);
8073

81-
EXPECT_EQ(a.distance2(b), size);
74+
// angles
75+
EXPECT_DOUBLE_EQ(std::cos(a.angle()), 1);
76+
EXPECT_DOUBLE_EQ(std::sin(a.angle()), 0);
77+
EXPECT_DOUBLE_EQ(a.delta_angle(b), b.angle());
78+
79+
// dot p
80+
EXPECT_DOUBLE_EQ(a.dot(b), 0);
81+
82+
double2d c(1.2, 3.4);
83+
double2d d(5.6, 7.8);
84+
EXPECT_DOUBLE_EQ(c.dot(d), 1.2 * 5.6 + 3.4 * 7.8);
85+
86+
// cross p
87+
int3d e(1, 2, 3);
88+
int3d f(4, 5, 6);
89+
90+
EXPECT_EQ(e.cross(f), int3d(-3, 6, -3));
91+
92+
// from angle
93+
EXPECT_EQ(double2d::from_angle(a.angle()), a);
8294
}

tests/unordered_key.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "common.h"
2+
#include <unordered_set>
3+
4+
TEST(unordered, set)
5+
{
6+
std::unordered_set<float2d> vectors;
7+
8+
for (uint32_t i = 0; i < 10; i++)
9+
vectors.insert({ i / 3, i / 5 });
10+
11+
for (uint32_t i = 0; i < 10; i++)
12+
EXPECT_EQ(vectors.count({ i / 3, i / 5 }), 1);
13+
}

0 commit comments

Comments
 (0)