Skip to content

Commit 3effd6f

Browse files
committed
test: Refactor test and introduce assertion_helper to separate tests and boilerplate
Signed-off-by: Sietze van Buuren <s.van.buuren@gmail.com>
1 parent 1ded91d commit 3effd6f

File tree

3 files changed

+116
-113
lines changed

3 files changed

+116
-113
lines changed

tests/assertion_helpers.hpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <vector>
2+
#include <gtest/gtest.h>
3+
4+
5+
namespace tcip {
6+
7+
8+
using Vector = std::vector<double>;
9+
using Vector2 = std::vector<Vector>;
10+
using Vector3 = std::vector<Vector2>;
11+
12+
template <typename T>
13+
testing::AssertionResult Interp1DAssertions(Vector x, Vector f, Vector x_fine, Vector f_fine) {
14+
T interp(x, f);
15+
for ( auto i = 0; i < x_fine.size(); i++ ) {
16+
auto val = interp.eval(x_fine[i]);
17+
if (!testing::internal::CmpHelperFloatingPointEQ<double>("expected", "actual", val, f_fine[i]) ) {
18+
return testing::AssertionFailure()
19+
<< "for x = " << x_fine[i] << "expected " << f_fine[i] << " but got " << val;
20+
}
21+
}
22+
return testing::AssertionSuccess();
23+
}
24+
25+
26+
27+
template <typename T>
28+
testing::AssertionResult Interp2DEvalAssertions(const tcip::Vector &x, const tcip::Vector &y, const Vector2 &f, const tcip::Vector &x_fine, const tcip::Vector &y_fine, const Vector2 &f_fine) {
29+
T interp2(x, y, f);
30+
for ( auto i = 0; i < x_fine.size(); ++i ) {
31+
for ( auto j = 0; j < y_fine.size(); ++j ) {
32+
auto val = interp2.eval(x_fine[i], y_fine[j]);
33+
if (!testing::internal::CmpHelperFloatingPointEQ<double>("expected", "actual", val, f_fine[i][j]) ) {
34+
return testing::AssertionFailure()
35+
<< "for x = " << x_fine[i] << ", y = " << y_fine[j] << " expected " << f_fine[i][j] << " but got " << val;
36+
}
37+
}
38+
}
39+
return testing::AssertionSuccess();
40+
}
41+
42+
43+
template <typename T>
44+
testing::AssertionResult Interp3DEvalAssertions(const tcip::Vector &x, const tcip::Vector &y, const tcip::Vector &z, const Vector3 &f, const tcip::Vector &x_fine, const tcip::Vector &y_fine, const tcip::Vector &z_fine, const Vector3 &f_fine) {
45+
T interp3(x, y, z, f);
46+
for ( auto i = 0; i < x_fine.size(); ++i ) {
47+
for ( auto j = 0; j < y_fine.size(); ++j ) {
48+
for ( auto k = 0; k < y_fine.size(); ++k ) {
49+
auto val = interp3.eval(x_fine[i], y_fine[j], z_fine[k]);
50+
if (!testing::internal::CmpHelperFloatingPointEQ<double>("expected", "actual", val, f_fine[i][j][k]) ) {
51+
return testing::AssertionFailure()
52+
<< "for x = " << x_fine[i] << ", y = " << y_fine[j] << ", z = " << z_fine[j] << " expected " << f_fine[i][j][k] << " but got " << val;
53+
}
54+
}
55+
}
56+
}
57+
return testing::AssertionSuccess();
58+
}
59+
60+
61+
} // namespace tcip

tests/test_cubic_spline.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
#include <vector>
21
#include <gtest/gtest.h>
32
#include <cubic_spline.hpp>
4-
5-
using Vector = std::vector<double>;
3+
#include "assertion_helpers.hpp"
64

75

8-
testing::AssertionResult MonotonicSpline1DAssertions(Vector x, Vector f, Vector x_fine, Vector f_fine) {
9-
cip::MonotonicSpline1D<double> interp(x, f);
10-
for ( auto i = 0; i < x_fine.size(); i++ ) {
11-
auto val = interp.eval(x_fine[i]);
12-
if (!testing::internal::CmpHelperFloatingPointEQ<double>("expected", "actual", val, f_fine[i]) ) {
13-
return testing::AssertionFailure()
14-
<< "for x = " << x_fine[i] << "expected " << f_fine[i] << " but got " << val;
15-
}
16-
}
17-
return testing::AssertionSuccess();
18-
}
6+
using Vector = std::vector<double>;
7+
using MonotonicSpline = cip::MonotonicSpline1D<double>;
198

209

2110
TEST(TestCubicSpline1D, test_monotonic_spline_1d) {
22-
Vector x = { 1, 2, 3, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10 };
23-
Vector f = { 0, 0, 0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.3, 0.6 };
24-
Vector x_fine = { 1.0, 1.375, 1.75, 2.125, 2.5, 2.875, 3.25, 3.625, 4.0, 4.375, 4.75, 5.125, 5.5, 5.875, 6.25, 6.625, 7.0, 7.375, 7.75, 8.125, 8.5, 8.875, 9.25, 9.625, 10.0 };
25-
Vector f_fine = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.07450161897651952, 0.3304738093015729, 0.5, 0.4542055428769416, 0.37848534450908033, 0.5453815182574999, 1.2, 1.2, 1.2, 1.2, 1.2000000000003865, 0.8762920673080998, 0.31081730769267324, 0.06762319711538467, 0.0009615384615386802, -0.01053185096153797, 0.11971153846116067, 0.3749999999999991, 0.5999999999999988 };
26-
ASSERT_TRUE(MonotonicSpline1DAssertions(x, f, x_fine, f_fine));
11+
tcip::Vector x = { 1, 2, 3, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10 };
12+
tcip::Vector f = { 0, 0, 0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.3, 0.6 };
13+
tcip::Vector x_fine = { 1.0, 1.375, 1.75, 2.125, 2.5, 2.875, 3.25, 3.625, 4.0, 4.375, 4.75, 5.125, 5.5, 5.875, 6.25, 6.625, 7.0, 7.375, 7.75, 8.125, 8.5, 8.875, 9.25, 9.625, 10.0 };
14+
tcip::Vector f_fine = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.07450161897651952, 0.3304738093015729, 0.5, 0.4542055428769416, 0.37848534450908033, 0.5453815182574999, 1.2, 1.2, 1.2, 1.2, 1.2000000000003865, 0.8762920673080998, 0.31081730769267324, 0.06762319711538467, 0.0009615384615386802, -0.01053185096153797, 0.11971153846116067, 0.3749999999999991, 0.5999999999999988 };
15+
ASSERT_TRUE(tcip::Interp1DAssertions<MonotonicSpline>(x, f, x_fine, f_fine));
2716
}

tests/test_linear_interp.cpp

Lines changed: 47 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
1-
#include <vector>
21
#include <gtest/gtest.h>
32
#include <utility>
43
#include "linear_interp.hpp"
4+
#include "assertion_helpers.hpp"
5+
56

6-
using Vector = std::vector<double>;
7-
using Vector2 = std::vector<Vector>;
8-
using Vector3 = std::vector<Vector2>;
97
using VectorN1 = cip::VectorN<double, 1>;
108
using VectorN2 = cip::VectorN<double, 2>;
119
using VectorN3 = cip::VectorN<double, 3>;
1210
using Span = std::span<const double>;
1311
using Pr = std::pair<size_t, size_t>;
1412

1513

16-
testing::AssertionResult Interp1DAssertions(Vector x, Vector f, Vector x_fine, Vector f_fine) {
17-
cip::LinearInterp1D<double> interp(x, f);
18-
for ( auto i = 0; i < x_fine.size(); i++ ) {
19-
auto val = interp.eval(x_fine[i]);
20-
if (!testing::internal::CmpHelperFloatingPointEQ<double>("expected", "actual", val, f_fine[i]) ) {
21-
return testing::AssertionFailure()
22-
<< "for x = " << x_fine[i] << "expected " << f_fine[i] << " but got " << val;
23-
}
24-
}
25-
return testing::AssertionSuccess();
26-
}
27-
28-
29-
3014
TEST(TestLinearCell1D, test_linear_cell_1d) {
31-
Vector x = {0, 1, 2};
32-
Vector f = {3, 3, 4};
15+
tcip::Vector x = {0, 1, 2};
16+
tcip::Vector f = {3, 3, 4};
3317

3418
VectorN1 fvec(f);
3519
size_t i = 1;
@@ -45,27 +29,27 @@ TEST(TestLinearCell1D, test_linear_cell_1d) {
4529

4630

4731
TEST(TestInterp1D, test_linear_interp_1d_akima) {
48-
Vector x = { 1.0, 2.0, 3.0, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10.0 };
49-
Vector f = { 0.0, 0.0, 0.0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.3, 0.6 };
50-
Vector x_fine = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
51-
Vector f_fine = { 0.0, 0.0, 0.0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.6 };
52-
ASSERT_TRUE(Interp1DAssertions(x, f, x_fine, f_fine));
32+
tcip::Vector x = { 1.0, 2.0, 3.0, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10.0 };
33+
tcip::Vector f = { 0.0, 0.0, 0.0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.3, 0.6 };
34+
tcip::Vector x_fine = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
35+
tcip::Vector f_fine = { 0.0, 0.0, 0.0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.6 };
36+
ASSERT_TRUE(tcip::Interp1DAssertions<cip::LinearInterp1D<double>>(x, f, x_fine, f_fine));
5337
}
5438

5539

5640
TEST(TestInterp1D, test_linear_interp_1d_random) {
57-
Vector x = { 1.0, 1.5714285714285714, 2.142857142857143, 2.7142857142857144, 3.2857142857142856, 3.8571428571428568, 4.428571428571429, 5.0 };
58-
Vector f = { 4.0, 0.0, 6.0, 2.0, 3.0, 8.0, 4.0, 9.0 };
59-
Vector x_fine = { 1.0, 1.4444444444444444, 1.8888888888888888, 2.333333333333333, 2.7777777777777777, 3.2222222222222223, 3.6666666666666665, 4.111111111111111, 4.555555555555555, 5.0 };
60-
Vector f_fine = { 4.0, 0.8888888888888888, 3.3333333333333335, 4.66666666666667, 2.1111111111111107, 2.8888888888888893, 6.333333333333335, 6.222222222222224, 5.111111111111107, 9.0 };
61-
ASSERT_TRUE(Interp1DAssertions(x, f, x_fine, f_fine));
41+
tcip::Vector x = { 1.0, 1.5714285714285714, 2.142857142857143, 2.7142857142857144, 3.2857142857142856, 3.8571428571428568, 4.428571428571429, 5.0 };
42+
tcip::Vector f = { 4.0, 0.0, 6.0, 2.0, 3.0, 8.0, 4.0, 9.0 };
43+
tcip::Vector x_fine = { 1.0, 1.4444444444444444, 1.8888888888888888, 2.333333333333333, 2.7777777777777777, 3.2222222222222223, 3.6666666666666665, 4.111111111111111, 4.555555555555555, 5.0 };
44+
tcip::Vector f_fine = { 4.0, 0.8888888888888888, 3.3333333333333335, 4.66666666666667, 2.1111111111111107, 2.8888888888888893, 6.333333333333335, 6.222222222222224, 5.111111111111107, 9.0 };
45+
ASSERT_TRUE(tcip::Interp1DAssertions<cip::LinearInterp1D<double>>(x, f, x_fine, f_fine));
6246
}
6347

6448

6549
TEST(TestLinearCell2D, test_linear_cell_2d) {
66-
Vector x = {0, 1, 2};
67-
Vector y = {0, 1, 2};
68-
Vector2 f = {{1, 2, 2},
50+
tcip::Vector x = {0, 1, 2};
51+
tcip::Vector y = {0, 1, 2};
52+
tcip::Vector2 f = {{1, 2, 2},
6953
{2, 3, 3},
7054
{3, 3, 4}};
7155

@@ -85,105 +69,74 @@ TEST(TestLinearCell2D, test_linear_cell_2d) {
8569
}
8670

8771

88-
testing::AssertionResult Interp2DEvalAssertions(const Vector &x, const Vector &y, const Vector2 &f, const Vector &x_fine, const Vector &y_fine, const Vector2 &f_fine) {
89-
cip::LinearInterp2D<double> interp2(x, y, f);
90-
for ( auto i = 0; i < x_fine.size(); ++i ) {
91-
for ( auto j = 0; j < y_fine.size(); ++j ) {
92-
auto val = interp2.eval(x_fine[i], y_fine[j]);
93-
if (!testing::internal::CmpHelperFloatingPointEQ<double>("expected", "actual", val, f_fine[i][j]) ) {
94-
return testing::AssertionFailure()
95-
<< "for x = " << x_fine[i] << ", y = " << y_fine[j] << " expected " << f_fine[i][j] << " but got " << val;
96-
}
97-
}
98-
}
99-
return testing::AssertionSuccess();
100-
}
101-
102-
10372
TEST(TestInterp2D, test_linear_interp_2d_normalized) {
104-
Vector x = { 0.0, 1.0, 2.0 };
105-
Vector y = { 0.0, 1.0, 2.0 };
106-
Vector2 f = {
73+
tcip::Vector x = { 0.0, 1.0, 2.0 };
74+
tcip::Vector y = { 0.0, 1.0, 2.0 };
75+
tcip::Vector2 f = {
10776
{ 1.0, 2.0, 2.0 },
10877
{ 2.0, 3.0, 3.0 },
10978
{ 3.0, 3.0, 4.0 }
11079
};
111-
Vector x_fine = { 0.0, 0.5, 1.0, 1.5, 2.0 };
112-
Vector y_fine = { 0.0, 0.5, 1.0, 1.5, 2.0 };
113-
Vector2 f_fine = {
80+
tcip::Vector x_fine = { 0.0, 0.5, 1.0, 1.5, 2.0 };
81+
tcip::Vector y_fine = { 0.0, 0.5, 1.0, 1.5, 2.0 };
82+
tcip::Vector2 f_fine = {
11483
{ 1.0, 1.5, 2.0, 2.0, 2.0 },
11584
{ 1.5, 2.0, 2.5, 2.5, 2.5 },
11685
{ 2.0, 2.5, 3.0, 3.0, 3.0 },
11786
{ 2.5, 2.75, 3.0, 3.25, 3.5 },
11887
{ 3.0, 3.0, 3.0, 3.5, 4.0 }
11988
};
120-
ASSERT_TRUE(Interp2DEvalAssertions(x, y, f, x_fine, y_fine, f_fine));
89+
ASSERT_TRUE(tcip::Interp2DEvalAssertions<cip::LinearInterp2D<double>>(x, y, f, x_fine, y_fine, f_fine));
12190
}
12291

12392

12493
TEST(TestInterp2D, test_linear_interp_2d_standard) {
125-
Vector x = { 0.0, 1.5, 3.0 };
126-
Vector y = { 0.0, 2.0, 4.0 };
127-
Vector2 f = {
94+
tcip::Vector x = { 0.0, 1.5, 3.0 };
95+
tcip::Vector y = { 0.0, 2.0, 4.0 };
96+
tcip::Vector2 f = {
12897
{ 1.0, 2.0, 2.0 },
12998
{ 2.0, 3.0, 3.0 },
13099
{ 3.0, 3.0, 4.0 }
131100
};
132-
Vector x_fine = { 0.0, 0.75, 1.5, 2.25, 3.0 };
133-
Vector y_fine = { 0.0, 1.0, 2.0, 3.0, 4.0 };
134-
Vector2 f_fine = {
101+
tcip::Vector x_fine = { 0.0, 0.75, 1.5, 2.25, 3.0 };
102+
tcip::Vector y_fine = { 0.0, 1.0, 2.0, 3.0, 4.0 };
103+
tcip::Vector2 f_fine = {
135104
{ 1.0, 1.5, 2.0, 2.0, 2.0 },
136105
{ 1.5, 2.0, 2.5, 2.5, 2.5 },
137106
{ 2.0, 2.5, 3.0, 3.0, 3.0 },
138107
{ 2.5, 2.75, 3.0, 3.25, 3.5 },
139108
{ 3.0, 3.0, 3.0, 3.5, 4.0 }
140109
};
141-
ASSERT_TRUE(Interp2DEvalAssertions(x, y, f, x_fine, y_fine, f_fine));
110+
ASSERT_TRUE(tcip::Interp2DEvalAssertions<cip::LinearInterp2D<double>>(x, y, f, x_fine, y_fine, f_fine));
142111
}
143112

144113

145114
TEST(TestInterp2D, test_linear_interp_2d_non_monotonic) {
146-
Vector x = { 0.0, 1.0, 1.5 };
147-
Vector y = { 0.0, 0.5, 3.0 };
148-
Vector2 f = {
115+
tcip::Vector x = { 0.0, 1.0, 1.5 };
116+
tcip::Vector y = { 0.0, 0.5, 3.0 };
117+
tcip::Vector2 f = {
149118
{ 1.0, 2.0, 2.0 },
150119
{ 2.0, 3.0, 3.0 },
151120
{ 3.0, 3.0, 4.0 }
152121
};
153-
Vector x_fine = { 0.0, 0.375, 0.75, 1.125, 1.5 };
154-
Vector y_fine = { 0.0, 0.75, 1.5, 2.25, 3.0 };
155-
Vector2 f_fine = {
122+
tcip::Vector x_fine = { 0.0, 0.375, 0.75, 1.125, 1.5 };
123+
tcip::Vector y_fine = { 0.0, 0.75, 1.5, 2.25, 3.0 };
124+
tcip::Vector2 f_fine = {
156125
{ 1.0, 2.0, 2.0, 2.0, 2.0 },
157126
{ 1.375, 2.375, 2.375, 2.375, 2.375 },
158127
{ 1.75, 2.75, 2.75, 2.75, 2.75 },
159128
{ 2.25, 3.025, 3.1, 3.175, 3.25 },
160129
{ 3.0, 3.1, 3.4, 3.7, 4.0 }
161130
};
162-
ASSERT_TRUE(Interp2DEvalAssertions(x, y, f, x_fine, y_fine, f_fine));
131+
ASSERT_TRUE(tcip::Interp2DEvalAssertions<cip::LinearInterp2D<double>>(x, y, f, x_fine, y_fine, f_fine));
163132
}
164133

165134

166-
testing::AssertionResult Interp3DEvalAssertions(const Vector &x, const Vector &y, const Vector &z, const Vector3 &f, const Vector &x_fine, const Vector &y_fine, const Vector &z_fine, const Vector3 &f_fine) {
167-
cip::LinearInterp3D<double> interp3(x, y, z, f);
168-
for ( auto i = 0; i < x_fine.size(); ++i ) {
169-
for ( auto j = 0; j < y_fine.size(); ++j ) {
170-
for ( auto k = 0; k < y_fine.size(); ++k ) {
171-
auto val = interp3.eval(x_fine[i], y_fine[j], z_fine[k]);
172-
if (!testing::internal::CmpHelperFloatingPointEQ<double>("expected", "actual", val, f_fine[i][j][k]) ) {
173-
return testing::AssertionFailure()
174-
<< "for x = " << x_fine[i] << ", y = " << y_fine[j] << ", z = " << z_fine[j] << " expected " << f_fine[i][j][k] << " but got " << val;
175-
}
176-
}
177-
}
178-
}
179-
return testing::AssertionSuccess();
180-
}
181-
182135
TEST(TestInterp3D, test_linear_interp_3d) {
183-
Vector x = { 0.0, 1.0, 2.0 };
184-
Vector y = { 0.0, 1.0, 2.0 };
185-
Vector z = { 0.0, 1.0, 2.0 };
186-
Vector3 f = {
136+
tcip::Vector x = { 0.0, 1.0, 2.0 };
137+
tcip::Vector y = { 0.0, 1.0, 2.0 };
138+
tcip::Vector z = { 0.0, 1.0, 2.0 };
139+
tcip::Vector3 f = {
187140
{
188141
{ 1.0, 2.0, 2.0 },
189142
{ 2.0, 3.0, 3.0 },
@@ -200,10 +153,10 @@ TEST(TestInterp3D, test_linear_interp_3d) {
200153
{ 4.0, 4.0, 5.0 }
201154
}
202155
};
203-
Vector x_fine = { 0.0, 0.5, 1.0, 1.5, 2.0 };
204-
Vector y_fine = { 0.0, 0.5, 1.0, 1.5, 2.0 };
205-
Vector z_fine = { 0.0, 0.5, 1.0, 1.5, 2.0 };
206-
Vector3 f_fine = {
156+
tcip::Vector x_fine = { 0.0, 0.5, 1.0, 1.5, 2.0 };
157+
tcip::Vector y_fine = { 0.0, 0.5, 1.0, 1.5, 2.0 };
158+
tcip::Vector z_fine = { 0.0, 0.5, 1.0, 1.5, 2.0 };
159+
tcip::Vector3 f_fine = {
207160
{
208161
{ 1.0, 1.5, 2.0, 2.0, 2.0 },
209162
{ 1.5, 2.0, 2.5, 2.5, 2.5 },
@@ -241,6 +194,6 @@ TEST(TestInterp3D, test_linear_interp_3d) {
241194
}
242195
};
243196

244-
ASSERT_TRUE(Interp3DEvalAssertions(x, y, z, f, x_fine, y_fine, z_fine, f_fine));
197+
ASSERT_TRUE(tcip::Interp3DEvalAssertions<cip::LinearInterp3D<double>>(x, y, z, f, x_fine, y_fine, z_fine, f_fine));
245198

246199
}

0 commit comments

Comments
 (0)