Skip to content

Commit 9bdf645

Browse files
committed
tests: examples: scalar: rewamped to be parameterized
now using GTest parameterized test cases to generate separate test cases for each number of nodes in range
1 parent 38f6a8e commit 9bdf645

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

tests/examples/scalar/test_scalar.cpp

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,77 @@
11
/*
22
* Tests for the scalar example solving the test equation
3-
*/
4-
3+
*/
54
#include <cmath>
5+
66
#include <gtest/gtest.h>
77
#include <gmock/gmock.h>
88

9+
using namespace ::testing;
10+
911
#define PFASST_UNIT_TESTING
1012
#include "../examples/scalar/scalar_sdc.cpp"
1113
#undef PFASST_UNIT_TESTING
1214

15+
/*
16+
* parameterized test fixture with number of nodes as parameter
17+
*/
18+
class ConvergenceTest
19+
: public ::testing::TestWithParam<size_t>
20+
{
21+
protected:
22+
size_t nnodes; // parameter
23+
24+
const complex<double> lambda = complex<double>(-1.0, 1.0);
25+
const double Tend = 4.0;
26+
const vector<size_t> nsteps = { 2, 5, 10, 15, 20 };
27+
size_t nsteps_l;
28+
vector<double> err;
29+
vector<double> convrate;
30+
double dt;
31+
size_t niters;
32+
33+
public:
34+
virtual void SetUp()
35+
{
36+
this->nnodes = this->GetParam();
37+
this->nsteps_l = this->nsteps.size();
38+
this->err.resize(this->nsteps.size());
39+
this->convrate.resize(this->nsteps.size());
40+
41+
// Expect convergence rate of 2*nodes-2 from collocation formula,
42+
// doing an identical number of iteration should suffice to reach this as each
43+
// iteration should increase order by one
44+
this->niters = 2 * nnodes - 2;
45+
46+
for (size_t i = 0; i <= nsteps_l - 1; ++i) {
47+
dt = Tend / double(nsteps[i]);
48+
err[i] = run_scalar_sdc(nsteps[i], dt, nnodes, niters, lambda);
49+
}
50+
}
51+
52+
virtual void TearDown()
53+
{}
54+
};
1355

1456
/*
1557
* For Lobatto nodes, the resulting method should of order 2*M-2 with M=number of nodes
1658
* The test below verifies that the code approximately (up to a safety factor) reproduces
1759
* the theoretically expected rate of convergence
1860
*/
19-
TEST(ConvergenceTest, ScalarSDC)
61+
TEST_P(ConvergenceTest, GaussLobattoNodes)
2062
{
21-
const complex<double> lambda = complex<double>(-1.0, 1.0);
22-
const double Tend = 4.0;
23-
const vector<size_t> nsteps = { 2, 5, 10, 15, 20 };
24-
size_t nsteps_l = nsteps.size();
25-
26-
vector<double> err(nsteps.size());
27-
vector<double> convrate(nsteps.size()-1);
28-
29-
double dt;
30-
31-
// Test converge up to 6 nodes: For more nodes, errors are of the order of machine
32-
// precision and convergence can no longer be monitored
33-
for ( size_t nnodes = 2; nnodes<=6; ++nnodes)
34-
{
35-
// Expect convergence rate of 2*nodes-2 from collocation formula,
36-
// doing an identical number of iteration should suffice to reach this as each
37-
// iteration should increase order by one
38-
size_t niters = 2*nnodes-2;
39-
40-
for ( size_t i = 0; i<=nsteps_l-1; ++i)
41-
{
42-
dt = Tend/double(nsteps[i]);
43-
err[i] = run_scalar_sdc(nsteps[i], dt, nnodes, niters, lambda);
44-
}
45-
46-
for ( size_t i = 0; i<=nsteps_l-2; ++i)
47-
{
48-
convrate[i] = log10(err[i+1]/err[i])/log10(double(nsteps[i])/double(nsteps[i+1]));
63+
for (size_t i = 0; i <= nsteps_l - 2; ++i) {
64+
convrate[i] = log10(err[i+1] / err[i]) / log10(double(nsteps[i]) / double(nsteps[i + 1]));
4965

50-
EXPECT_THAT(convrate[i],
51-
testing::DoubleNear(double(2*nnodes-2), 0.99)) << "Convergence rate at node "
52-
<< i
53-
<< " not within expected range";
54-
}
66+
EXPECT_THAT(convrate[i],
67+
testing::DoubleNear(double(2 * nnodes - 2), 0.99)) << "Convergence rate at node "
68+
<< i
69+
<< " not within expected range";
5570
}
5671
}
5772

73+
INSTANTIATE_TEST_CASE_P(ScalarSDC, ConvergenceTest, Range<size_t>(2, 7));
74+
5875
int main(int argc, char** argv)
5976
{
6077
testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)