Debian Codegraph @

dune/common/typetree/test/testtypetreetreepath.cc

raw
1 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4 #include <config.h>
5
6 #include <cassert>
7 #include <cstddef>
8 #include <iostream>
9 #include <sstream>
10 #include <tuple>
11 #include <type_traits>
12 #include <type_traits>
13 #include <utility>
14
15 #include <dune/common/deprecated.hh>
16 #include <dune/common/indices.hh>
17 #include <dune/common/test/testsuite.hh>
18 #include <dune/common/typetree/treepath.hh>
19 #include <dune/common/version.hh>
20
21 int main(int argc, char** argv)
22 {
23
24 using namespace Dune::Indices;
25 Dune::TestSuite suite("Check treePath()");
26
27 {
28 auto root = Dune::TypeTree::treePath();
29 constexpr auto path = Dune::TypeTree::treePath(_1,3,_2,5);
30
31 static_assert(std::is_same<std::decay_t<decltype(path[_2])>,std::decay_t<decltype(_2)>>{},"wrong entry value");
32 static_assert(path[_0] == 1,"wrong entry value");
33
34 static_assert(std::is_same<std::decay_t<decltype(path[_1])>,std::size_t>{},"wrong entry type");
35 static_assert(std::is_same<std::decay_t<decltype(path[_3])>,std::size_t>{},"wrong entry type");
36
37 suite.check(path[_0] == 1);
38 suite.check(path[_1] == 3);
39 suite.check(path[_2] == 2);
40 suite.check(path[_3] == 5);
41
42 suite.check(path[1] == 3);
43 suite.check(path[3] == 5);
44
45 DUNE_NO_DEPRECATED_BEGIN
46
47 static_assert(std::is_same<std::decay_t<decltype(path.element(_0))>,std::decay_t<decltype(_1)>>{},"wrong entry value");
48 static_assert(path.element(_0) == 1,"wrong entry value");
49
50 static_assert(std::is_same<std::decay_t<decltype(path.element(_1))>,std::size_t>{},"wrong entry type");
51 static_assert(std::is_same<std::decay_t<decltype(path.element(_3))>,std::size_t>{},"wrong entry type");
52
53 suite.check(path.element(_0) == 1);
54 suite.check(path.element(_1) == 3);
55 suite.check(path.element(_2) == 2);
56 suite.check(path.element(_3) == 5);
57
58 suite.check(path.element(1) == 3);
59 suite.check(path.element(3) == 5);
60
61 DUNE_NO_DEPRECATED_END
62
63 // Check free utility functions using qualified calls in
64 // Dune:: and Dune::TypeTree:: and unqualified ADL calls
65
66 suite.check(Dune::back(path) == Dune::TypeTree::back(path));
67 suite.check(Dune::front(path) == Dune::TypeTree::front(path));
68 suite.check(Dune::push_back(path, _3) == Dune::TypeTree::push_back(path, _3));
69 suite.check(Dune::push_front(path, _3) == Dune::TypeTree::push_front(path, _3));
70 suite.check(Dune::pop_back(path) == Dune::TypeTree::pop_back(path));
71 suite.check(Dune::pop_front(path) == Dune::TypeTree::pop_front(path));
72 suite.check(Dune::accumulate_back(path, _3) == Dune::TypeTree::accumulate_back(path, _3));
73 suite.check(Dune::accumulate_front(path, _3) == Dune::TypeTree::accumulate_front(path, _3));
74 suite.check(Dune::join(path, path) == Dune::TypeTree::join(path, path));
75 suite.check(Dune::reverse(path) == Dune::TypeTree::reverse(path));
76
77 suite.check(back(path) == Dune::TypeTree::back(path));
78 suite.check(front(path) == Dune::TypeTree::front(path));
79 suite.check(push_back(path, _3) == Dune::TypeTree::push_back(path, _3));
80 suite.check(push_front(path, _3) == Dune::TypeTree::push_front(path, _3));
81 suite.check(pop_back(path) == Dune::TypeTree::pop_back(path));
82 suite.check(pop_front(path) == Dune::TypeTree::pop_front(path));
83 suite.check(accumulate_back(path, _3) == Dune::TypeTree::accumulate_back(path, _3));
84 suite.check(accumulate_front(path, _3) == Dune::TypeTree::accumulate_front(path, _3));
85 suite.check(join(path, path) == Dune::TypeTree::join(path, path));
86 suite.check(reverse(path) == Dune::TypeTree::reverse(path));
87
88 suite.check(back(path) == 5);
89 static_assert(back(push_back(path, _3)) == 3);
90 suite.check(back(push_back(path, 3)) == 3);
91 static_assert(back(pop_back(path)) == 2);
92 suite.check(back(pop_back(path)) == 2);
93 suite.check(back(pop_back(pop_back(path))) == 3);
94
95 static_assert(front(path) == 1);
96 static_assert(front(push_front(path,_0)) == 0);
97 suite.check(front(push_front(path,0)) == 0);
98 suite.check(front(pop_front(path)) == 3);
99 static_assert(front(pop_front(pop_front(path))) == 2);
100 suite.check(front(pop_front(pop_front(path))) == 2);
101
102 suite.check(back(accumulate_back(path, 3)) == 8);
103 suite.check(back(accumulate_back(path, _3)) == 8);
104 static_assert(back(accumulate_back(pop_back(path), _3)) == _5);
105
106 suite.check(front(accumulate_front(path, 3)) == 4);
107 static_assert(front(accumulate_front(path, _3)) == _4);
108
109 static_assert(pop_front(pop_front(pop_front(pop_front(path)))).size() == 0);
110 static_assert(reverse(Dune::TypeTree::treePath()).size() == 0);
111
112 constexpr auto rpath = reverse(path);
113 suite.check(rpath[_0] == 5);
114 suite.check(rpath[3] == 1);
115
116
117 static_assert(join(root, path) == join(path, root));
118 constexpr auto jpath = join(path, rpath);
119 static_assert(jpath.max_size() == 8);
120 static_assert(jpath == Dune::TypeTree::treePath(_1,3,_2,5,5,_2,3,_1));
121 static_assert(join(path, Dune::TypeTree::treePath(5,_2), Dune::TypeTree::treePath(3, _1)) == Dune::TypeTree::treePath(_1,3,_2,5,5,_2,3,_1));
122 suite.check(jpath[_3] == 5);
123 suite.check(jpath[_4] == 5);
124 }
125
126 { // test the operator== for TreePath
127
128 using Dune::TypeTree::treePath;
129
130 suite.check(treePath(1,2,3) != treePath(1,2));
131 suite.check(treePath(1,2,3) == treePath(1u,2u,3u));
132 suite.check(treePath(1,2,3) != treePath(3,2,1));
133 suite.check(treePath(1,2,3) == treePath(_1,_2,_3));
134 suite.check(treePath(1,2,3) != treePath(_3,_2,_1));
135 suite.check(treePath(_1,_2,_3) != treePath(_3,_2,_1));
136
137 // check whether comparison can be used in constexpr context
138 static_assert(treePath(_1,_2,_3) == treePath(_1,_2,_3));
139 static_assert(treePath(_1,_2,_3) != treePath(_3,_2,_1));
140
141 auto a = treePath(std::integral_constant<int,0>{}, std::integral_constant<int,1>{});
142 auto b = treePath(std::integral_constant<std::size_t,0>{}, std::integral_constant<std::size_t,1>{});
143 static_assert(decltype(a == b)::value);
144
145 /* Note: It is not possible to check mixed integral constant arguments with
146 the purely static overload of operator==
147
148 auto c = treePath(std::integral_constant<std::size_t,0>{}, std::integral_constant<int,3>{});
149 static_assert(decltype(a != c)::value);
150 */
151 }
152
153 {
154 using namespace Dune::TypeTree;
155
156 auto path1 = 1_tp;
157 static_assert(path1[_0] == 1);
158
159 auto path123 = 123_tp;
160 static_assert(path123[_0] == 123);
161 }
162
163 {
164 using namespace Dune::TypeTree;
165 using namespace Dune::Indices;
166 auto tp = Dune::TypeTree::treePath(0, _1, 2, _3);
167
168 auto os = std::stringstream();
169 os << tp;
170 }
171
172 {
173 using namespace Dune::TypeTree;
174 using namespace Dune::Indices;
175 auto tp = Dune::TypeTree::treePath(0, _1, 2, _3);
176
177 suite.check(tp.get<0>() == 0);
178 suite.check(tp.get<1>() == 1);
179 suite.check(tp.get<2>() == 2);
180 suite.check(tp.get<3>() == 3);
181
182 auto [a, b, c, d] = tp;
183
184 suite.check(a == 0);
185 suite.check(b == 1);
186 suite.check(c == 2);
187 suite.check(d == 3);
188
189 suite.check(std::is_same_v<decltype(a), std::size_t>);
190 suite.check(std::is_same_v<decltype(b), Dune::index_constant<1>>);
191 suite.check(std::is_same_v<decltype(c), std::size_t>);
192 suite.check(std::is_same_v<decltype(d), Dune::index_constant<3>>);
193 }
194
195 {
196 using namespace Dune::Indices;
197
198 auto mi = Dune::HybridMultiIndex(3, _2, 1, _0);
199 auto mi_size_t = Dune::HybridMultiIndex(std::size_t(3), std::integral_constant<std::size_t, 2>{}, std::size_t(1), std::integral_constant<std::size_t, 0>{});
200 auto mi_signed = Dune::HybridMultiIndex(int(3), std::integral_constant<int, 2>{}, (signed char)(1), std::integral_constant<signed long, 0>{});
201
202 suite.check(std::is_same_v<decltype(mi), decltype(mi_size_t)>);
203 suite.check(std::is_same_v<decltype(mi), decltype(mi_signed)>);
204 suite.check(mi == mi_size_t);
205 suite.check(mi == mi_signed);
206 }
207
208 {
209 using namespace Dune::Indices;
210
211 auto t = std::make_tuple(_2, _1, std::size_t(0));
212 auto mi_tuple = Dune::HybridMultiIndex(t);
213 auto mi = Dune::HybridMultiIndex(_2, _1, std::size_t(0));
214
215 suite.check(std::is_same_v<decltype(mi), decltype(mi_tuple)>);
216 suite.check(mi == mi_tuple);
217 }
218
219 return suite.exit();
220 }