"Fossies" - the Fresh Open Source Software Archive

Member "dune-typetree-2.8.0/test/testtreecontainer.cc" (31 Aug 2021, 4853 Bytes) of package /linux/misc/dune/dune-typetree-2.8.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "config.h"
    2 
    3 #include "typetreetestutility.hh"
    4 
    5 
    6 #include <dune/typetree/traversal.hh>
    7 #include <dune/typetree/treecontainer.hh>
    8 
    9 #include <dune/common/test/testsuite.hh>
   10 
   11 
   12 struct NameVisitor
   13   : public Dune::TypeTree::TreeVisitor
   14   , public Dune::TypeTree::DynamicTraversal
   15 {
   16   std::string s;
   17 
   18   template<typename T, typename TreePath>
   19   void pre(const T& node, TreePath) { s += node.name(); s += "<"; }
   20 
   21   template<typename T, typename TreePath>
   22   void leaf(const T& node, TreePath) { s += node.name(); s += ","; }
   23 
   24   template<typename T, typename TreePath>
   25   void post(const T& node, TreePath) { s += ">"; }
   26 };
   27 
   28 template<class Tree>
   29 std::string treeName(const Tree& tree)
   30 {
   31   NameVisitor nameVisitor;
   32   Dune::TypeTree::applyToTree(tree, nameVisitor);
   33   return nameVisitor.s;
   34 }
   35 
   36 template<class F>
   37 bool notThrown(F&& f)
   38 {
   39   try {
   40     f();
   41     return true;
   42   }
   43   catch(...) {}
   44   return false;
   45 }
   46 
   47 template <class Value, class Tree>
   48 using UniformTreeMatrix
   49   = Dune::TypeTree::UniformTreeContainer<
   50       Dune::TypeTree::UniformTreeContainer<Value,Tree>,Tree>;
   51 
   52 template<class Tree, class Value>
   53 Dune::TestSuite checkTreeContainer(const Tree& tree, const Value& value)
   54 {
   55   Dune::TestSuite test(treeName(tree));
   56 
   57   // construct a container using a factory function
   58   auto container = Dune::TypeTree::makeTreeContainer<Value>(tree);
   59 
   60   // copy construct the container
   61   auto container2{container};
   62   auto container3{container};
   63 
   64   // copy-assign the container
   65   container2 = container;
   66 
   67   // move-construct the container
   68   auto container4{std::move(container2)};
   69 
   70   // move-assign the container
   71   container4 = std::move(container3);
   72 
   73   Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) {
   74       test.check(notThrown([&]() {
   75         container[treePath] = value;
   76       })) << "Assigning desired value to tree container entry failed";
   77     });
   78 
   79   Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) {
   80       test.check(container[treePath] == value)
   81         << "Value in tree container does not match assigned value";
   82     });
   83 
   84   // default construct a container
   85   decltype(container) container5{};
   86   container5.resize(tree);
   87 
   88   Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) {
   89       test.check(notThrown([&]() {
   90         container5[treePath] = value;
   91       })) << "Assigning desired value to tree container entry failed";
   92     });
   93 
   94   Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) {
   95       test.check(container5[treePath] == value)
   96         << "Value in tree container does not match assigned value";
   97     });
   98 
   99 
  100   // default construct a container with size information from tree
  101   decltype(container) container6{tree};
  102 
  103   Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) {
  104       test.check(notThrown([&]() {
  105         container6[treePath] = value;
  106       })) << "Assigning desired value to tree container entry failed";
  107     });
  108 
  109   Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& node, auto treePath) {
  110       test.check(container6[treePath] == value)
  111         << "Value in tree container does not match assigned value";
  112     });
  113 
  114 
  115   // construct a matrix-like container
  116   auto matrix = Dune::TypeTree::makeTreeContainer(tree,
  117     [&](auto const&) { return Dune::TypeTree::makeTreeContainer<Value>(tree); });
  118 
  119   Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& rowNode, auto rowTreePath) {
  120     Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& colNode, auto colTreePath) {
  121       test.check(notThrown([&]() {
  122         matrix[rowTreePath][colTreePath] = value;
  123       })) << "Assigning desired value to tree matrix-container entry failed";
  124     });
  125   });
  126 
  127   Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& rowNode, auto rowTreePath) {
  128     Dune::TypeTree::forEachLeafNode(tree, [&] (auto&& colNode, auto colTreePath) {
  129       test.check(matrix[rowTreePath][colTreePath] == value)
  130         << "Value in tree matrix-container does not match assigned value";
  131     });
  132   });
  133 
  134   return test;
  135 }
  136 
  137 
  138 
  139 int main(int argc, char** argv)
  140 {
  141 
  142   Dune::TestSuite test;
  143 
  144   int v1 = 42;
  145   std::vector<double> v2{1,2,3,4};
  146 
  147   using SL1 = SimpleLeaf;
  148   SL1 sl1;
  149   test.subTest(checkTreeContainer(sl1, v1));
  150   test.subTest(checkTreeContainer(sl1, v2));
  151 
  152   using SP1 = SimplePower<SimpleLeaf,3>;
  153   SP1 sp1(sl1, sl1, sl1);
  154   test.subTest(checkTreeContainer(sp1, v1));
  155   test.subTest(checkTreeContainer(sp1, v2));
  156 
  157   using SDP1 = SimpleDynamicPower<SimpleLeaf>;
  158   SDP1 sdp1(sl1, sl1, sl1);
  159   test.subTest(checkTreeContainer(sdp1, v1));
  160   test.subTest(checkTreeContainer(sdp1, v2));
  161 
  162   using SL2 = SimpleLeaf;
  163   using SP2 = SimplePower<SimpleLeaf,2>;
  164   using SC1 = SimpleComposite<SL1,SP1,SP2>;
  165   SL2 sl2;
  166   SP2 sp2(sl2,sl2);
  167   SC1 sc1_1(sl1,sp1,sp2);
  168   test.subTest(checkTreeContainer(sc1_1, v1));
  169   test.subTest(checkTreeContainer(sc1_1, v2));
  170 
  171   test.report();
  172 
  173   return test.exit();
  174 }