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>;
97using VectorN1 = cip::VectorN<double , 1 >;
108using VectorN2 = cip::VectorN<double , 2 >;
119using VectorN3 = cip::VectorN<double , 3 >;
1210using Span = std::span<const double >;
1311using 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-
3014TEST (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
4731TEST (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
5640TEST (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
6549TEST (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-
10372TEST (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
12493TEST (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
145114TEST (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-
182135TEST (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