| 1 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
// vi: set et ts=4 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 <cstddef> |
| 9 |
#include <type_traits> |
| 10 |
#include <utility> |
| 11 |
|
| 12 |
#include <dune/common/indices.hh> |
| 13 |
#include <dune/common/test/testsuite.hh> |
| 14 |
#include <dune/common/typetree/test/testtypetreeutilities.hh> |
| 15 |
#include <dune/common/typetree/traversal.hh> |
| 16 |
|
| 17 |
int main() |
| 18 |
{ |
| 19 |
Dune::TestSuite test("tree traversal check"); |
| 20 |
|
| 21 |
using Payload = int; |
| 22 |
|
| 23 |
auto tree = NonUniformInner( |
| 24 |
Payload(0), |
| 25 |
UniformStaticInner( |
| 26 |
Payload(0), |
| 27 |
Leaf(Payload(0)), |
| 28 |
Dune::Indices::_3 |
| 29 |
), |
| 30 |
Leaf(Payload(0))); |
| 31 |
|
| 32 |
{ |
| 33 |
std::size_t all = 0; |
| 34 |
Dune::TypeTree::forEachNode(tree, [&](auto&& node, auto&& path) { |
| 35 |
++all; |
| 36 |
}); |
| 37 |
test.check(all==6) |
| 38 |
<< "Counting all nodes with forEachNode failed. Result is " << all << " but should be " << 6; |
| 39 |
} |
| 40 |
|
| 41 |
{ |
| 42 |
std::size_t all = 0; |
| 43 |
auto countNode = [&](auto&& node, auto&& path) { |
| 44 |
++all; |
| 45 |
}; |
| 46 |
auto nop = [&](auto&& node, auto&& path) {}; |
| 47 |
Dune::TypeTree::forEachNode(tree, countNode, countNode, nop); |
| 48 |
test.check(all==6) |
| 49 |
<< "Counting all nodes with forEachNode failed. Result is " << all << " but should be " << 6; |
| 50 |
} |
| 51 |
|
| 52 |
{ |
| 53 |
std::size_t all = 0; |
| 54 |
auto countNode = [&](auto&& node, auto&& path) { |
| 55 |
++all; |
| 56 |
}; |
| 57 |
auto nop = [&](auto&& node, auto&& path) {}; |
| 58 |
Dune::TypeTree::forEachNode(tree, nop, countNode, countNode); |
| 59 |
test.check(all==6) |
| 60 |
<< "Counting all nodes with forEachNode failed. Result is " << all << " but should be " << 6; |
| 61 |
} |
| 62 |
|
| 63 |
{ |
| 64 |
std::size_t all = 0; |
| 65 |
auto countNode = [&](auto&& node, auto&& path) { |
| 66 |
++all; |
| 67 |
}; |
| 68 |
auto nop = [&](auto&& node, auto&& path) {}; |
| 69 |
Dune::TypeTree::forEachNode(tree, nop, countNode, nop); |
| 70 |
test.check(all==4) |
| 71 |
<< "Counting all nodes with forEachNode failed. Result is " << all << " but should be " << 4; |
| 72 |
} |
| 73 |
|
| 74 |
{ |
| 75 |
std::size_t all = 0; |
| 76 |
auto countNode = [&](auto&& node, auto&& path) { |
| 77 |
++all; |
| 78 |
}; |
| 79 |
Dune::TypeTree::forEachNode(tree, countNode, countNode, countNode); |
| 80 |
test.check(all==8) |
| 81 |
<< "Counting all nodes with forEachNode failed. Result is " << all << " but should be " << 8; |
| 82 |
} |
| 83 |
|
| 84 |
{ |
| 85 |
std::size_t leaf = 0; |
| 86 |
Dune::TypeTree::forEachLeafNode(tree, [&](auto&& node, auto&& path) { |
| 87 |
++leaf; |
| 88 |
}); |
| 89 |
test.check(leaf==4) |
| 90 |
<< "Counting leaf nodes with forEachLeafNode failed. Result is " << leaf << " but should be " << 4; |
| 91 |
} |
| 92 |
|
| 93 |
{ |
| 94 |
auto countVisit = [] (auto&& node, auto&& path) { |
| 95 |
++(node.value()); |
| 96 |
}; |
| 97 |
Dune::TypeTree::forEachNode(tree, countVisit, countVisit, countVisit); |
| 98 |
|
| 99 |
std::size_t visits=0; |
| 100 |
Dune::TypeTree::forEachNode(tree, [&](auto&& node, auto&& path) { |
| 101 |
visits += node.value(); |
| 102 |
}); |
| 103 |
|
| 104 |
test.check(visits==8) |
| 105 |
<< "Counting all node visitations failed. Result is " << visits << " but should be " << 8; |
| 106 |
} |
| 107 |
|
| 108 |
return test.exit(); |
| 109 |
} |