preCICE v3.1.1
Loading...
Searching...
No Matches
AlgorithmTest.cpp
Go to the documentation of this file.
1#include <Eigen/Core>
2#include <algorithm>
3#include <array>
4#include <boost/test/unit_test_suite.hpp>
5#include <functional>
6#include <ostream>
7#include <string>
8#include <utility>
9#include <vector>
10#include "math/constants.hpp"
12#include "testing/Testing.hpp"
13#include "utils/String.hpp"
14#include "utils/algorithm.hpp"
15
16using namespace precice;
17namespace pu = precice::utils;
18
19BOOST_AUTO_TEST_SUITE(UtilsTests)
20BOOST_AUTO_TEST_SUITE(AlgorithmTests)
21
23{
24 PRECICE_TEST(1_rank);
25 auto a = pu::make_array(1, 2, 3);
26 BOOST_TEST(a.size() == 3);
27 BOOST_TEST(a.at(0) == 1);
28 BOOST_TEST(a.at(1) == 2);
29 BOOST_TEST(a.at(2) == 3);
30}
31
32BOOST_AUTO_TEST_CASE(UniqueElements)
33{
34 PRECICE_TEST(1_rank);
35 std::vector<int> y{1, 2, 3, 4, 5, 6, 7, 8, 9};
36 BOOST_TEST(pu::unique_elements(y));
37 BOOST_TEST(pu::unique_elements(y, [](int l, int r) { return l == r; }));
38
39 std::vector<int> n1{1, 2, 3, 4, 5, 6, 7, 8, 9, 9};
40 BOOST_TEST(!pu::unique_elements(n1));
41 BOOST_TEST(!pu::unique_elements(n1, [](int l, int r) { return l == r; }));
42
43 std::vector<int> n2{1, 1, 3, 4, 5, 6, 7, 8, 9};
44 BOOST_TEST(!pu::unique_elements(n2));
45 BOOST_TEST(!pu::unique_elements(n2, [](int l, int r) { return l == r; }));
46
48 BOOST_TEST(pu::unique_elements(e));
49 BOOST_TEST(pu::unique_elements(e, [](int l, int r) { return l == r; }));
50}
51
52BOOST_AUTO_TEST_CASE(UniqueEigenElements)
53{
54 PRECICE_TEST(1_rank);
55 Eigen::VectorXd v1(3);
56 v1 << 1.0, 0.1, 0.2;
57 Eigen::VectorXd v2(3);
58 v2 << 0.1, 1.0, 0.2;
59 Eigen::VectorXd v3(3);
60 v3 << 0.1, 0.2, 1.0;
61
63 BOOST_TEST(pu::unique_elements(case1));
64
66 BOOST_TEST(pu::unique_elements(case2));
67
68 std::vector<Eigen::VectorXd> case3{v1, v2};
69 BOOST_TEST(pu::unique_elements(case3));
70
71 std::vector<Eigen::VectorXd> case4{v1, v2, v1};
72 BOOST_TEST(!pu::unique_elements(case4));
73
74 std::vector<Eigen::VectorXd> case5{v1, v2, v3};
75 BOOST_TEST(pu::unique_elements(case5));
76
77 std::vector<Eigen::VectorXd> case6{v1, v2, v3, v1};
78 BOOST_TEST(!pu::unique_elements(case6));
79}
80
82{
83 PRECICE_TEST(1_rank);
84 std::vector<int> a{1, 2, 3, 4, 5, 6, 7, 8, 9};
85 std::vector<int> b{1, 2, 3, 4, 5, 0, 9};
86
87 auto aa = pu::mismatch(
88 a.begin(), a.end(),
89 a.begin(), a.end());
90 BOOST_TEST((aa.first == aa.second));
91 BOOST_TEST((aa.first == a.end()));
92
93 auto ab = pu::mismatch(
94 a.begin(), a.end(),
95 b.begin(), b.end());
96 BOOST_TEST((ab.first != a.end()));
97 BOOST_TEST(*ab.first == 6);
98 BOOST_TEST(*ab.second == 0);
99}
100
101BOOST_AUTO_TEST_SUITE(RangePreview)
102
103BOOST_AUTO_TEST_CASE(NormalRangePreview)
104{
105 PRECICE_TEST(1_rank);
106 std::vector<int> a{1, 2, 3, 4, 5, 6, 0};
108 oss << pu::previewRange(2, a);
109 std::string str{oss.str()};
110 BOOST_TEST(str == "[1, 2, ... , 6, 0] min:0 max:6");
111}
112
113BOOST_AUTO_TEST_CASE(PrintNoElements)
114{
115 PRECICE_TEST(1_rank);
116 std::vector<int> a{1, 2, 3, 4, 5, 6, 0};
118 oss << pu::previewRange(0, a);
119 std::string str{oss.str()};
120 BOOST_TEST(str == "[ ... ] min:0 max:6");
121}
122
124{
125 PRECICE_TEST(1_rank);
128 oss << pu::previewRange(3, a);
129 std::string str{oss.str()};
130 BOOST_TEST(str == "<Empty Range>");
131}
132
134
135BOOST_AUTO_TEST_SUITE(ReorderArray)
136
138{
139 PRECICE_TEST(1_rank);
140 std::array<int, 1> input{1};
141 std::array<int, 1> order{0};
142 std::array<int, 1> expected{1};
143
144 auto reordered = utils::reorder_array(order, input);
145 BOOST_TEST(reordered == expected);
146}
147
149{
150 PRECICE_TEST(1_rank);
151 std::array<int, 3> input{3, 4, 5};
152 std::array<int, 3> order{0, 1, 2};
153 std::array<int, 3> expected{3, 4, 5};
154
155 auto reordered = utils::reorder_array(order, input);
156 BOOST_TEST(reordered == expected);
157}
158
160{
161 PRECICE_TEST(1_rank);
162 std::array<int, 3> input{3, 4, 5};
163 std::array<int, 3> order{2, 1, 0};
164 std::array<int, 3> expected{5, 4, 3};
165
166 auto reordered = utils::reorder_array(order, input);
167 BOOST_TEST(reordered == expected);
168}
169
171{
172 PRECICE_TEST(1_rank);
173 std::array<int, 3> input{3, 4, 5};
174 std::array<int, 3> order{2, 0, 1};
175 std::array<int, 3> expected{5, 3, 4};
176
177 auto reordered = utils::reorder_array(order, input);
178 BOOST_TEST(reordered == expected);
179}
180
181BOOST_AUTO_TEST_CASE(ScramblePointer)
182{
183 PRECICE_TEST(1_rank);
184 int a = 1, b = 2;
185
186 std::array<int *, 3> input{&a, &b, nullptr};
187 std::array<int, 3> order{2, 0, 1};
188 std::array<int *, 3> expected{nullptr, &a, &b};
189
190 auto reordered = utils::reorder_array(order, input);
191 BOOST_TEST(reordered == expected);
192}
193
194BOOST_AUTO_TEST_SUITE_END() // ReorderArray
195
196BOOST_AUTO_TEST_SUITE(FindFirstRange)
197
199{
200 PRECICE_TEST(1_rank);
201 std::vector v{0, 1, 2, 3};
202
203 auto [first, last] = utils::find_first_range(v.begin(), v.end(), [](int i) { return false; });
204 BOOST_TEST((first == v.end()));
205 BOOST_TEST((last == v.end()));
206}
207
209{
210 PRECICE_TEST(1_rank);
211 std::vector v{0, 1, 2, 3};
212
213 auto [first, last] = utils::find_first_range(v.begin(), v.end(), [](int i) { return true; });
214 BOOST_TEST((first == v.begin()));
215 BOOST_TEST((last == v.end() - 1));
216}
217
219{
220 PRECICE_TEST(1_rank);
221 std::vector v{0, 1, 2, 3};
222
223 auto [first, last] = utils::find_first_range(v.begin(), v.end(), [](int i) { return i == 0; });
224 BOOST_TEST((first == v.begin()));
225 BOOST_TEST((last == v.begin()));
226}
227
229{
230 PRECICE_TEST(1_rank);
231 std::vector v{0, 0, 2, 3};
232
233 auto [first, last] = utils::find_first_range(v.begin(), v.end(), [](int i) { return i == 0; });
234 BOOST_TEST((first == v.begin()));
235 BOOST_TEST((last == v.begin() + 1));
236}
237
239{
240 PRECICE_TEST(1_rank);
241 std::vector v{0, 1, 2, 3};
242
243 auto [first, last] = utils::find_first_range(v.begin(), v.end(), [](int i) { return i == 3; });
244 BOOST_TEST((first == v.end() - 1));
245 BOOST_TEST((last == first));
246}
247
249{
250 PRECICE_TEST(1_rank);
251 std::vector v{1, 2, 3, 3};
252
253 auto [first, last] = utils::find_first_range(v.begin(), v.end(), [](int i) { return i == 3; });
254 BOOST_TEST((first == v.end() - 2));
255 BOOST_TEST((last == v.end() - 1));
256}
257
259{
260 PRECICE_TEST(1_rank);
261 std::vector v{1, 2, 3, 4};
262
263 auto [first, last] = utils::find_first_range(v.begin(), v.end(), [](int i) { return i == 2; });
264 BOOST_TEST((first == v.begin() + 1));
265 BOOST_TEST((last == first));
266}
267
268BOOST_AUTO_TEST_CASE(MiddleMultiple)
269{
270 PRECICE_TEST(1_rank);
271 std::vector v{1, 2, 2, 3};
272
273 auto [first, last] = utils::find_first_range(v.begin(), v.end(), [](int i) { return i == 2; });
274 BOOST_TEST((first == v.begin() + 1));
275 BOOST_TEST((last == v.begin() + 2));
276}
277
278BOOST_AUTO_TEST_CASE(MultipleRangesSingleMatch)
279{
280 PRECICE_TEST(1_rank);
281 std::vector v{1, 2, 3, 4, 5, 3};
282
283 auto [first, last] = utils::find_first_range(v.begin(), v.end(), [](int i) { return i == 3; });
284 BOOST_TEST((first == v.begin() + 2));
285 BOOST_TEST((last == first));
286}
287
288BOOST_AUTO_TEST_CASE(MultipleRangesMultipleMatches)
289{
290 PRECICE_TEST(1_rank);
291 std::vector v{1, 2, 2, 4, 2, 2};
292
293 auto [first, last] = utils::find_first_range(v.begin(), v.end(), [](int i) { return i == 2; });
294 BOOST_TEST((first == v.begin() + 1));
295 BOOST_TEST((last == v.begin() + 2));
296}
297
298BOOST_AUTO_TEST_SUITE_END() // FindRange
299
300BOOST_AUTO_TEST_SUITE_END() // Algorithm
301
BOOST_AUTO_TEST_CASE(MakeArray)
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
#define PRECICE_TEST(...)
Definition Testing.hpp:27
contains precice-related utilities.
auto reorder_array(const std::array< Index, n > &order, const std::array< T, n > &elements) -> std::array< T, n >
Reorders an array given an array of unique indices.
auto make_array(Elements &&... elements) -> std::array< typename std::common_type< Elements... >::type, sizeof...(Elements)>
Function that generates an array from given elements.
Definition algorithm.hpp:51
std::pair< InputIt, InputIt > find_first_range(InputIt first, InputIt last, Predicate p)
Finds the first range in [first, last[ that fulfills a predicate.
const RangePreview< Iter > previewRange(Size n, const Range &range)
bool unique_elements(const Container &c, BinaryPredicate p={})
Definition algorithm.hpp:64
std::pair< InputIt1, InputIt2 > mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
Main namespace of the precice library.
T str(T... args)