| 1 |
// -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
// vi: set et ts=8 sw=2 sts=2: |
| 3 |
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 4 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later |
| 5 |
|
| 6 |
#include <config.h> |
| 7 |
|
| 8 |
#include <utility> |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
#include <dune/common/test/testsuite.hh> |
| 12 |
#include <dune/common/typetree/treecontainer.hh> |
| 13 |
#include <dune/common/typetree/traversal.hh> |
| 14 |
#include <dune/common/typetree/test/testtypetreeutilities.hh> |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
template<class F> |
| 19 |
bool notThrown(F&& f) |
| 20 |
{ |
| 21 |
try { |
| 22 |
f(); |
| 23 |
return true; |
| 24 |
} |
| 25 |
catch(...) {} |
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
template <class Value, class Tree> |
| 30 |
using UniformTreeMatrix |
| 31 |
= Dune::TypeTree::UniformTreeContainer< |
| 32 |
Dune::TypeTree::UniformTreeContainer<Value,Tree>,Tree>; |
| 33 |
|
| 34 |
template<class Tree, class Value> |
| 35 |
Dune::TestSuite checkTreeContainer(const Tree& tree, const Value& value) |
| 36 |
{ |
| 37 |
Dune::TestSuite test(treeName(tree)); |
| 38 |
|
| 39 |
// construct a container using a factory function |
| 40 |
auto container = Dune::TypeTree::makeTreeContainer<Value>(tree); |
| 41 |
|
| 42 |
// copy construct the container |
| 43 |
auto container2{container}; |
| 44 |
auto container3{container}; |
| 45 |
|
| 46 |
// copy-assign the container |
| 47 |
container2 = container; |
| 48 |
|
| 49 |
// move-construct the container |
| 50 |
auto container4{std::move(container2)}; |
| 51 |
|
| 52 |
// move-assign the container |
| 53 |
container4 = std::move(container3); |
| 54 |
|
| 55 |
Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) { |
| 56 |
test.check(notThrown([&]() { |
| 57 |
container[treePath] = value; |
| 58 |
})) << "Assigning desired value to tree container entry failed"; |
| 59 |
}); |
| 60 |
|
| 61 |
Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) { |
| 62 |
test.check(container[treePath] == value) |
| 63 |
<< "Value in tree container does not match assigned value"; |
| 64 |
}); |
| 65 |
|
| 66 |
// default construct a container |
| 67 |
decltype(container) container5{}; |
| 68 |
container5.resize(tree); |
| 69 |
|
| 70 |
Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) { |
| 71 |
test.check(notThrown([&]() { |
| 72 |
container5[treePath] = value; |
| 73 |
})) << "Assigning desired value to tree container entry failed"; |
| 74 |
}); |
| 75 |
|
| 76 |
Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) { |
| 77 |
test.check(container5[treePath] == value) |
| 78 |
<< "Value in tree container does not match assigned value"; |
| 79 |
}); |
| 80 |
|
| 81 |
|
| 82 |
// default construct a container with size information from tree |
| 83 |
decltype(container) container6{tree}; |
| 84 |
|
| 85 |
Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) { |
| 86 |
test.check(notThrown([&]() { |
| 87 |
container6[treePath] = value; |
| 88 |
})) << "Assigning desired value to tree container entry failed"; |
| 89 |
}); |
| 90 |
|
| 91 |
Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) { |
| 92 |
test.check(container6[treePath] == value) |
| 93 |
<< "Value in tree container does not match assigned value"; |
| 94 |
}); |
| 95 |
|
| 96 |
|
| 97 |
// construct a matrix-like container |
| 98 |
auto matrix = Dune::TypeTree::makeTreeContainer(tree, |
| 99 |
[&](auto const&) { return Dune::TypeTree::makeTreeContainer<Value>(tree); }); |
| 100 |
|
| 101 |
Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& rowNode, auto rowTreePath) { |
| 102 |
Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& colNode, auto colTreePath) { |
| 103 |
test.check(notThrown([&]() { |
| 104 |
matrix[rowTreePath][colTreePath] = value; |
| 105 |
})) << "Assigning desired value to tree matrix-container entry failed"; |
| 106 |
}); |
| 107 |
}); |
| 108 |
|
| 109 |
Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& rowNode, auto rowTreePath) { |
| 110 |
Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& colNode, auto colTreePath) { |
| 111 |
test.check(matrix[rowTreePath][colTreePath] == value) |
| 112 |
<< "Value in tree matrix-container does not match assigned value"; |
| 113 |
}); |
| 114 |
}); |
| 115 |
|
| 116 |
return test; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
int main(int argc, char** argv) |
| 122 |
{ |
| 123 |
|
| 124 |
Dune::TestSuite test; |
| 125 |
|
| 126 |
using namespace Dune::Indices; |
| 127 |
|
| 128 |
// Use tags to generate different type nodes |
| 129 |
auto tagA = Dune::index_constant<23>{}; |
| 130 |
auto tagB = Dune::index_constant<42>{}; |
| 131 |
auto tagC = Dune::index_constant<237>{}; |
| 132 |
auto tagD = Dune::index_constant<47>{}; |
| 133 |
|
| 134 |
|
| 135 |
int v1 = 42; |
| 136 |
std::vector<double> v2{1,2,3,4}; |
| 137 |
|
| 138 |
auto l1 = Leaf(tagA); |
| 139 |
test.subTest(checkTreeContainer(l1, v1)); |
| 140 |
test.subTest(checkTreeContainer(l1, v2)); |
| 141 |
|
| 142 |
auto us3_l1 = UniformStaticInner(tagA, Leaf(tagB), _3); |
| 143 |
test.subTest(checkTreeContainer(us3_l1, v1)); |
| 144 |
test.subTest(checkTreeContainer(us3_l1, v2)); |
| 145 |
|
| 146 |
auto ud3_l1 = UniformDynamicInner(tagA, Leaf(tagB), 3); |
| 147 |
test.subTest(checkTreeContainer(ud3_l1, v1)); |
| 148 |
test.subTest(checkTreeContainer(ud3_l1, v2)); |
| 149 |
|
| 150 |
auto n_l1_us2_l2_l3 = NonUniformInner(tagA, Leaf(tagB), UniformStaticInner(tagC, Leaf(tagD), _2), Leaf(tagA)); |
| 151 |
test.subTest(checkTreeContainer(n_l1_us2_l2_l3, v1)); |
| 152 |
test.subTest(checkTreeContainer(n_l1_us2_l2_l3, v2)); |
| 153 |
|
| 154 |
test.report(); |
| 155 |
|
| 156 |
return test.exit(); |
| 157 |
} |