Skip to content

Commit c848eb9

Browse files
authored
test(spline): Implemented data-driven testing for PolyEqvSpline (#62)
Test data for polynomials is used.
1 parent d4a4f46 commit c848eb9

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ add_test_executable(test_poly poly/test_poly.cpp)
2121

2222
add_test_executable(test_barycentric misc/test_barycentric.cpp)
2323

24+
add_test_executable(test_polyeqv spline/test_polyeqv.cpp)
25+
2426
add_test_executable(test_step spline/test_step.cpp)
2527
add_test_executable(test_linear spline/test_linear.cpp)
2628
add_test_executable(test_quadratic spline/test_quadratic.cpp)

tests/spline/test_polyeqv.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <ALFI/spline/polyeqv.h>
2+
3+
#include "../test_utils.h"
4+
5+
const auto test_data_path = TEST_DATA_DIR "/poly/poly.toml";
6+
7+
const auto test_data = toml::parse_file(test_data_path);
8+
9+
void test_polyeqv_spline(double epsilon) {
10+
const auto& test_cases = test_data["test_cases"].ref<toml::array>();
11+
12+
test_cases.for_each([&](const toml::table& test_case) {
13+
const auto& X = to_vector<double>(test_case["X"].ref<toml::array>());
14+
const auto& Y = to_vector<double>(test_case["Y"].ref<toml::array>());
15+
const auto& xx = to_vector<double>(test_case["xx"].ref<toml::array>());
16+
const auto& yy = to_vector<double>(test_case["yy"].ref<toml::array>());
17+
18+
expect_eq(alfi::spline::PolyEqvSpline<>(X, Y).eval(xx), yy, epsilon);
19+
});
20+
}
21+
22+
TEST(PolyEqvSplineTest, PolynomialData) {
23+
test_polyeqv_spline(1e-10);
24+
}
25+
26+
TEST(PolyEqvSplineTest, General) {
27+
const std::vector<double> X = {0, 1, 2, 3};
28+
const std::vector<double> Y = {5, 2, 10, 4};
29+
const auto spline = alfi::spline::PolyEqvSpline<>(X, Y);
30+
for (size_t i = 0; i < X.size() - 1; ++i) {
31+
EXPECT_EQ(spline(X[i]), Y[i]);
32+
}
33+
EXPECT_NEAR(spline(X[X.size()-1]), Y[Y.size()-1], 1e-14);
34+
}
35+
36+
TEST(PolyEqvSplineTest, Moving) {
37+
std::vector<double> X = {0, 1, 2, 3};
38+
const std::vector<double> Y = {5, 2, 10, 4};
39+
auto spline = alfi::spline::PolyEqvSpline<>(std::move(X), Y);
40+
EXPECT_TRUE(X.empty());
41+
EXPECT_FALSE(spline.X().empty());
42+
EXPECT_FALSE(spline.coeffs().empty());
43+
X = spline.X();
44+
auto coeffs = spline.coeffs();
45+
EXPECT_FALSE(X.empty());
46+
EXPECT_FALSE(coeffs.empty());
47+
EXPECT_FALSE(spline.X().empty());
48+
EXPECT_FALSE(spline.coeffs().empty());
49+
X = std::move(spline).X();
50+
coeffs = std::move(spline).coeffs();
51+
EXPECT_FALSE(X.empty());
52+
EXPECT_FALSE(coeffs.empty());
53+
EXPECT_TRUE(spline.X().empty());
54+
EXPECT_TRUE(spline.coeffs().empty());
55+
}

0 commit comments

Comments
 (0)