Debian Codegraph @

dune/common/typetree/test/testtypetreenodeconcepts.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 <cstdlib>
5 #include <cstddef>
6
7 #include <dune/common/indices.hh>
8 #include <dune/common/typetree/nodeconcepts.hh>
9
10 // Bring concepts into scope
11 using Dune::TypeTree::Concept::TreeNode;
12 using Dune::TypeTree::Concept::InnerTreeNode;
13 using Dune::TypeTree::Concept::StaticDegreeInnerTreeNode;
14 using Dune::TypeTree::Concept::UniformInnerTreeNode;
15 using Dune::TypeTree::Concept::LeafTreeNode;
16 using namespace Dune::Indices;
17
18 // Leaf with static integral_constant 0
19 struct LeafStaticZero {
20 static constexpr auto degree() { return _0; }
21 };
22 static_assert(TreeNode<LeafStaticZero>);
23 static_assert(LeafTreeNode<LeafStaticZero>);
24 static_assert(!UniformInnerTreeNode<LeafStaticZero>);
25 static_assert(!StaticDegreeInnerTreeNode<LeafStaticZero>);
26 static_assert(!InnerTreeNode<LeafStaticZero>);
27
28 // Leaf that reports non-zero degree but has no child -> should not be Inner nor Leaf
29 struct LeafNonZero {
30 std::size_t degree() const { return 2; }
31 };
32 static_assert(TreeNode<LeafNonZero>);
33 static_assert(LeafTreeNode<LeafNonZero>);
34 static_assert(!UniformInnerTreeNode<LeafNonZero>);
35 static_assert(!StaticDegreeInnerTreeNode<LeafNonZero>);
36 static_assert(!InnerTreeNode<LeafNonZero>);
37
38 // degree() returns signed type (int) convertible to size_t
39 struct SignedDegreeReturn {
40 int degree() const { return 1; }
41 };
42 static_assert(TreeNode<SignedDegreeReturn>);
43 static_assert(LeafTreeNode<SignedDegreeReturn>);
44 static_assert(!UniformInnerTreeNode<SignedDegreeReturn>);
45 static_assert(!StaticDegreeInnerTreeNode<SignedDegreeReturn>);
46 static_assert(!InnerTreeNode<SignedDegreeReturn>);
47
48 // Inner node with both static degree and both child APIs
49 struct InnerMixedChild {
50 static constexpr auto degree() { return _3; }
51 int child(std::size_t i) { return static_cast<int>(i); }
52 template<std::size_t I>
53 int child(Dune::index_constant<I>) { return static_cast<int>(I); }
54 };
55 static_assert(TreeNode<InnerMixedChild>);
56 static_assert(InnerTreeNode<InnerMixedChild>);
57 static_assert(UniformInnerTreeNode<InnerMixedChild>);
58 static_assert(StaticDegreeInnerTreeNode<InnerMixedChild>);
59 static_assert(!LeafTreeNode<InnerMixedChild>);
60
61 // Pure static-degree inner: only compile-time child access
62 struct InnerStaticOnly {
63 static constexpr auto degree() { return _4; }
64 template<std::size_t I>
65 int child(Dune::index_constant<I>) { return static_cast<int>(I); }
66 };
67 static_assert(TreeNode<InnerStaticOnly>);
68 static_assert(InnerTreeNode<InnerStaticOnly>);
69 static_assert(StaticDegreeInnerTreeNode<InnerStaticOnly>);
70 static_assert(!UniformInnerTreeNode<InnerStaticOnly>);
71
72 // Runtime inner with const child (tests permissiveness of const)
73 struct InnerRuntimeConstChild {
74 std::size_t degree() const { return 5; }
75 int child(std::size_t i) const { return static_cast<int>(i); }
76 };
77 static_assert(TreeNode<InnerRuntimeConstChild>);
78 static_assert(InnerTreeNode<InnerRuntimeConstChild>);
79 static_assert(UniformInnerTreeNode<InnerRuntimeConstChild>);
80 static_assert(!StaticDegreeInnerTreeNode<InnerRuntimeConstChild>);
81
82 // Inner with heterogeneous child return types (template version returns long)
83 struct InnerHeteroReturn {
84 static constexpr auto degree() { return _3; }
85 int child(std::size_t i) { return static_cast<int>(i); }
86 template<std::size_t I>
87 long child(Dune::index_constant<I>) { return static_cast<long>(I); }
88 };
89 static_assert(TreeNode<InnerHeteroReturn>);
90 static_assert(InnerTreeNode<InnerHeteroReturn>);
91 static_assert(UniformInnerTreeNode<InnerHeteroReturn>);
92 static_assert(StaticDegreeInnerTreeNode<InnerHeteroReturn>);
93
94
95 // Inner node with static compile-time degree and uniform child API
96 struct UniformInner {
97 // static degree known at compile-time, returns an integral_constant
98 static constexpr auto degree() { return _3; }
99
100 // uniform runtime child access
101 int child(std::size_t i) { return static_cast<int>(i); }
102 };
103
104 static_assert(TreeNode<UniformInner>);
105 static_assert(InnerTreeNode<UniformInner>);
106 static_assert(UniformInnerTreeNode<UniformInner>);
107 static_assert(StaticDegreeInnerTreeNode<UniformInner>);
108 static_assert(!LeafTreeNode<UniformInner>);
109
110
111 // Static degree but runtime child only -> should still be StaticDegree + Uniform
112 struct StaticDegreeWithOnlyRuntimeChild {
113 static constexpr auto degree() { return _2; }
114 int child(std::size_t i) { return int(i); }
115 };
116 static_assert(TreeNode<StaticDegreeWithOnlyRuntimeChild>);
117 static_assert(InnerTreeNode<StaticDegreeWithOnlyRuntimeChild>);
118 static_assert(UniformInnerTreeNode<StaticDegreeWithOnlyRuntimeChild>);
119 static_assert(StaticDegreeInnerTreeNode<StaticDegreeWithOnlyRuntimeChild>);
120
121 // Static degree without static
122 struct StaticDegreeWithNoStaticDegree {
123 auto degree() const { return _2; }
124 template<std::size_t I>
125 auto child(Dune::index_constant<I>) { return int(I); }
126 };
127 static_assert(TreeNode<StaticDegreeWithNoStaticDegree>);
128 static_assert(!InnerTreeNode<StaticDegreeWithNoStaticDegree>);
129 static_assert(!UniformInnerTreeNode<StaticDegreeWithNoStaticDegree>);
130 static_assert(!StaticDegreeInnerTreeNode<StaticDegreeWithNoStaticDegree>);
131 static_assert(LeafTreeNode<StaticDegreeWithNoStaticDegree>);
132
133 // Inner node with static degree == 0 (should not model StaticDegreeInnerTreeNode)
134 struct StaticDegreeZeroInner {
135 static constexpr auto degree() { return _0; }
136
137 template<std::size_t I>
138 int child(Dune::index_constant<I>) { return static_cast<int>(I); }
139 };
140
141 static_assert(TreeNode<StaticDegreeZeroInner>);
142 static_assert(!StaticDegreeInnerTreeNode<StaticDegreeZeroInner>);
143 static_assert(!InnerTreeNode<StaticDegreeZeroInner>);
144 static_assert(!UniformInnerTreeNode<StaticDegreeZeroInner>); // no runtime child
145 static_assert(LeafTreeNode<StaticDegreeZeroInner>);
146
147 // Static degree but missing some child overloads via requires -> should fail StaticDegreeInnerTreeNode
148 struct IncompleteStaticDegree {
149 static constexpr auto degree() { return _3; }
150 template<std::size_t I>
151 requires (I < 2) // Missing child for I==2
152 int child(Dune::index_constant<I>) { return int(I); }
153 };
154 static_assert(TreeNode<IncompleteStaticDegree>);
155 static_assert(!InnerTreeNode<IncompleteStaticDegree>);
156 static_assert(!StaticDegreeInnerTreeNode<IncompleteStaticDegree>);
157 static_assert(!UniformInnerTreeNode<IncompleteStaticDegree>);
158
159 // Node with static degree zero but providing a (unused) child -> should become inner
160 struct ZeroDegreeWithChild {
161 static constexpr auto degree() { return _0; }
162 int child(std::size_t) { return 0; }
163 };
164 static_assert(TreeNode<ZeroDegreeWithChild>);
165 static_assert(InnerTreeNode<ZeroDegreeWithChild>);
166 static_assert(!LeafTreeNode<ZeroDegreeWithChild>);
167 static_assert(UniformInnerTreeNode<ZeroDegreeWithChild>);
168 static_assert(!StaticDegreeInnerTreeNode<ZeroDegreeWithChild>);
169
170 // Runtime degree but compile-time child only -> leaf node as no runtime child access
171 struct RuntimeDegreeCompileTimeChild {
172 std::size_t degree() const { return 3; }
173 template<std::size_t I>
174 int child(Dune::index_constant<I>) { return int(I); }
175 };
176 static_assert(TreeNode<RuntimeDegreeCompileTimeChild>);
177 static_assert(LeafTreeNode<RuntimeDegreeCompileTimeChild>);
178 static_assert(!InnerTreeNode<RuntimeDegreeCompileTimeChild>);
179 static_assert(!StaticDegreeInnerTreeNode<RuntimeDegreeCompileTimeChild>);
180 static_assert(!UniformInnerTreeNode<RuntimeDegreeCompileTimeChild>);
181
182 // degree() returns bool convertible to size_t
183 struct BoolDegree {
184 bool degree() const { return false; }
185 };
186 static_assert(TreeNode<BoolDegree>);
187 static_assert(LeafTreeNode<BoolDegree>);
188 static_assert(!InnerTreeNode<BoolDegree>);
189
190 // Inner node with dependent qualified child return type
191 struct QualifiedDependentChild {
192 constexpr static auto degree() { return _2; }
193 template<class T=void>
194 int child(std::size_t i) { return int(i); }
195 template<std::size_t I>
196 int child(Dune::index_constant<I>) const { return int(I); }
197 };
198 static_assert(TreeNode<QualifiedDependentChild>);
199 static_assert(InnerTreeNode<QualifiedDependentChild>);
200 static_assert(UniformInnerTreeNode<QualifiedDependentChild>);
201 static_assert(!StaticDegreeInnerTreeNode<QualifiedDependentChild>);
202
203 static_assert(TreeNode<QualifiedDependentChild&>);
204 static_assert(InnerTreeNode<QualifiedDependentChild&>);
205 static_assert(UniformInnerTreeNode<QualifiedDependentChild&>);
206 static_assert(!StaticDegreeInnerTreeNode<QualifiedDependentChild&>);
207
208 static_assert(TreeNode<const QualifiedDependentChild&>);
209 static_assert(InnerTreeNode<const QualifiedDependentChild&>);
210 static_assert(!UniformInnerTreeNode<const QualifiedDependentChild&>);
211 static_assert(StaticDegreeInnerTreeNode<const QualifiedDependentChild&>); // because const version has only static child
212
213
214 // degree() returning reference
215 struct DegreeReturnsRef {
216 std::size_t d = 0;
217 std::size_t const& degree() const { return d; }
218 };
219 static_assert(TreeNode<DegreeReturnsRef>);
220 static_assert(LeafTreeNode<DegreeReturnsRef>);
221 static_assert(!InnerTreeNode<DegreeReturnsRef>);
222
223 // degree() returning volatile qualified
224 struct VolatileDegree {
225 mutable volatile std::size_t d = 0;
226 volatile std::size_t& degree() const { return d; }
227 };
228 static_assert(TreeNode<VolatileDegree>);
229 static_assert(LeafTreeNode<VolatileDegree>);
230 static_assert(!InnerTreeNode<VolatileDegree>);
231
232 // Inner with child returning different types depending on index via if constexpr
233 struct InnerVariantReturn {
234 static constexpr auto degree() { return _3; }
235 template<std::size_t I>
236 auto child(Dune::index_constant<I>) {
237 if constexpr (I==0) return 0;
238 else if constexpr (I==1) return 1.0;
239 else return 'c';
240 }
241 };
242 static_assert(TreeNode<InnerVariantReturn>);
243 static_assert(InnerTreeNode<InnerVariantReturn>);
244 static_assert(StaticDegreeInnerTreeNode<InnerVariantReturn>);
245 static_assert(!UniformInnerTreeNode<InnerVariantReturn>);
246
247 // Inner node with static degree returning plain size_t (not a compile-time constant type for the trait)
248 struct WrongStaticDegreeReturn {
249 static constexpr std::size_t degree() { return 2; } // returns size_t, not an integral_constant
250
251 template<std::size_t I>
252 int child(Dune::index_constant<I>) { return static_cast<int>(I); }
253 };
254
255 static_assert(TreeNode<WrongStaticDegreeReturn>);
256 static_assert(!StaticDegreeInnerTreeNode<WrongStaticDegreeReturn>);
257 static_assert(!InnerTreeNode<WrongStaticDegreeReturn>);
258
259 // Node with non-const degree() (should not model TreeNode)
260 struct NonConstDegree {
261 std::size_t degree() { return 1; } // missing const
262 template<std::size_t I>
263 int child(Dune::index_constant<I>) { return static_cast<int>(I); }
264 };
265
266 static_assert(!TreeNode<NonConstDegree>);
267
268 int main() {
269 return EXIT_SUCCESS;
270 }