Skip to content

Commit 0f26a3b

Browse files
authored
test(misc): Implemented data-driven testing for barycentric formula (#60)
1. Updated `tests/test_data` submodule to the latest commit containing test data for barycentric formula. 2. Modified `tests/misc/test_barycentric.cpp` to use the new test data.
1 parent 915c359 commit 0f26a3b

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

tests/misc/test_barycentric.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
1-
#include <gtest/gtest.h>
1+
#include <ALFI/misc.h>
22

3-
#include <cmath>
4-
#include <vector>
3+
#include "../test_utils.h"
54

6-
#include <ALFI/misc.h>
5+
void test_barycentric(const toml::parse_result& test_data, double epsilon) {
6+
const auto& test_cases = test_data["test_cases"].ref<toml::array>();
77

8-
template <typename Number>
9-
void test_case(const std::vector<Number>& X, const std::vector<Number>& Y,
10-
const std::vector<Number>& xx,
11-
alfi::dist::Type dist_type,
12-
const std::vector<Number>& yy) {
13-
EXPECT_EQ(alfi::misc::barycentric(X, Y, xx, dist_type), yy);
14-
}
8+
test_cases.for_each([&](const toml::table& test_case) {
9+
const auto& X = to_vector<double>(test_case["X"].ref<toml::array>());
10+
const auto& Y = to_vector<double>(test_case["Y"].ref<toml::array>());
11+
const auto& xx = to_vector<double>(test_case["xx"].ref<toml::array>());
12+
const auto& dist = test_case["dist"].ref<std::string>();
13+
const auto& yy = to_vector<double>(test_case["yy"].ref<toml::array>());
1514

16-
template <typename Number>
17-
void test_case(const std::vector<Number>& X, const std::vector<Number>& Y, const std::vector<Number>& xx, const std::vector<Number>& yy) {
18-
EXPECT_EQ(alfi::misc::barycentric(X, Y, xx), yy);
19-
}
15+
const auto& dist_type
16+
= dist == "uniform" ?
17+
alfi::dist::Type::UNIFORM
18+
: dist == "chebyshev" ?
19+
alfi::dist::Type::CHEBYSHEV
20+
: dist == "chebyshev_2" ?
21+
alfi::dist::Type::CHEBYSHEV_2
22+
: alfi::dist::Type::GENERAL;
2023

21-
template <typename Number>
22-
void test_case(const std::vector<Number>& X, const std::vector<Number>& Y, alfi::dist::Type dist_type) {
23-
EXPECT_EQ(alfi::misc::barycentric(X, Y, X, dist_type), Y);
24+
expect_eq(alfi::misc::barycentric(X, Y, xx, dist_type), yy, epsilon);
25+
});
2426
}
2527

26-
template <typename Number>
27-
void test_case(const std::vector<Number>& X, const std::vector<Number>& Y) {
28-
EXPECT_EQ(alfi::misc::barycentric(X, Y, X), Y);
28+
// barycentric formula can also be tested on the polynomial data
29+
TEST(BarycentricTest, PolynomialData) {
30+
const auto polynomial_test_data_path = TEST_DATA_DIR "/poly/poly.toml";
31+
const auto polynomial_test_data = toml::parse_file(polynomial_test_data_path);
32+
test_barycentric(polynomial_test_data, 1e-12);
2933
}
3034

31-
TEST(BarycentricTest, Basic) {
32-
test_case<double>({0, 1, 2}, {0, 0, 0});
33-
test_case<double>({0, 1, 2}, {1, 1, 1});
34-
test_case<double>({0, 1, 2}, {1, 2, 3});
35+
TEST(BarycentricTest, BarycentricData) {
36+
const auto barycentric_test_data_path = TEST_DATA_DIR "/misc/barycentric.toml";
37+
const auto barycentric_test_data = toml::parse_file(barycentric_test_data_path);
38+
test_barycentric(barycentric_test_data, 1e-13);
3539
}

0 commit comments

Comments
 (0)