"Fossies" - the Fresh Open Source Software Archive

Member "dune-typetree-2.8.0/test/testtypetree.cc" (31 Aug 2021, 3849 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 <dune/common/classname.hh>
    4 
    5 #include "typetreetestswitch.hh"
    6 
    7 #if TEST_TYPETREE_INVALID
    8 
    9 int main()
   10 {
   11   return 0;
   12 }
   13 
   14 #else
   15 
   16 #include "typetreetestutility.hh"
   17 
   18 template<class Tree, std::size_t depth, std::size_t nodeCount, std::size_t leafCount>
   19 void check(const Tree& tree)
   20 {
   21   namespace Info = Dune::TypeTree::Experimental::Info;
   22 
   23   std::cout << "==================================" << std::endl
   24             << "class: " << Dune::className<Tree>() << std::endl
   25             << "dynamic: " << Info::isDynamic<Tree> << std::endl
   26             << "depth: " << Info::depth(tree) << std::endl
   27             << "nodes: " << Info::nodeCount(tree) << std::endl
   28             << "leafs: " << Info::leafCount(tree) << std::endl;
   29 
   30   if constexpr (Info::isDynamic<Tree>)
   31     static_assert(Dune::Std::is_detected<Dune::TypeTree::Detail::DynamicTraversalConcept,Tree>{});
   32 
   33   TreePrinter treePrinter;
   34   Dune::TypeTree::applyToTree(tree,treePrinter);
   35 
   36   static_assert((decltype(Info::depth(tree)){} == depth),
   37                 "TreeInfo yields wrong information");
   38 
   39   assert(leafCount == Info::leafCount(tree));
   40   assert(nodeCount == Info::nodeCount(tree));
   41 
   42   if constexpr (not Info::isDynamic<Tree>)
   43   {
   44 
   45     static_assert((decltype(Info::nodeCount(tree)){} == nodeCount),
   46                   "TreeInfo yields wrong information");
   47 
   48     static_assert((decltype(Info::leafCount(tree)){} == leafCount),
   49                   "TreeInfo yields wrong information");
   50   }
   51 
   52   std::cout << "==================================" << std::endl;
   53 }
   54 
   55 
   56 int main(int argc, char** argv)
   57 {
   58 
   59   // basic tests
   60 
   61   // leaf node
   62   SimpleLeaf sl1;
   63 
   64   check<SimpleLeaf,1,1,1>(sl1);
   65 
   66   typedef SimplePower<SimpleLeaf,3> SP1;
   67   SP1 sp1_1;
   68   sp1_1.setChild(0,sl1);
   69   sp1_1.setChild(1,sl1);
   70   sp1_1.setChild(2,sl1);
   71 
   72   SimpleLeaf sl2;
   73   SP1 sp1_2(sl2,false);
   74 
   75   SP1 sp1_2a(sl2,true);
   76 
   77   check<SP1,2,4,3>(sp1_2a);
   78 
   79   typedef SimpleComposite<SimpleLeaf,SP1,SimpleLeaf> SC1;
   80   SC1 sc1_1(sl1,sp1_2,sl2);
   81 
   82   check<SC1,3,7,5>(sc1_1);
   83   TreePrinter treePrinter;
   84   Dune::TypeTree::applyToTree(const_cast<const SC1&>(sc1_1),treePrinter);
   85 
   86   typedef SimpleComposite<SimpleLeaf,SimpleLeaf,SimpleLeaf> SC2;
   87   SC2 sc2(sl1,sl1,sl1);
   88 
   89   check<SC2,2,4,3>(sc2);
   90 
   91   typedef SimpleComposite<SimpleLeaf,SP1,SimpleLeaf,SC1> SVC1;
   92   SVC1 svc1_1(sl1,sp1_1,sl2,sc1_1);
   93 
   94   check<SVC1,4,14,10>(svc1_1);
   95 
   96   typedef SimpleDynamicPower<SVC1> SDP1;
   97   SDP1 sdp_1(svc1_1, svc1_1);
   98   Dune::TypeTree::applyToTree(sdp_1,treePrinter);
   99 
  100   SP1 sp1_3(SimpleLeaf(),SimpleLeaf(),sl1);
  101   Dune::TypeTree::applyToTree(sp1_3,TreePrinter());
  102 
  103   SVC1 svc1_2(SimpleLeaf(),SP1(sp1_2),sl2,const_cast<const SC1&>(sc1_1));
  104 
  105   typedef SimpleComposite<SimpleLeaf,SC2,SimpleLeaf,SC1> SVC2;
  106   SVC2 svc2_1(sl1,sc2,sl2,sc1_1);
  107 
  108   Dune::TypeTree::applyToTreePair(svc1_2,svc2_1,PairPrinter());
  109 
  110   check<SVC2,4,14,10>(svc2_1);
  111 
  112   typedef SimpleDynamicPower<SimpleLeaf> SDP;
  113   SDP sdp(sl1,sl1);
  114 
  115   check<SDP,2,3,2>(sdp);
  116 
  117   // Test valid and invalid child access. Invalid access should be caught at compile time
  118   auto const _0 = Dune::TypeTree::index_constant<0>();
  119   auto const _1 = Dune::TypeTree::index_constant<1>();
  120   auto const _2 = Dune::TypeTree::index_constant<2>();
  121 
  122   // 1: valid access
  123   auto x1 = child(sp1_1, _0);
  124 #ifdef FAILURE2
  125   // 2: invalid access (too few children)
  126   {
  127     auto const _3 = Dune::TypeTree::index_constant<3>();
  128     auto x2 = child(sp1_1, _3);
  129   }
  130 #endif
  131 #ifdef FAILURE3
  132   // 3: invalid access (child has no children)
  133   auto x3 = child(sp1_1, _0, _0);
  134 #endif
  135 
  136   // 4: valid access
  137   auto x4 = child(sc1_1, _1, _2);
  138 #ifdef FAILURE5
  139   // 5: invalid access (too few children)
  140   {
  141     auto const _3 = Dune::TypeTree::index_constant<3>();
  142     auto x5 = child(sc1_1, _3);
  143   }
  144 #endif
  145 #ifdef FAILURE6
  146   // 6: invalid access (child has no children)
  147   auto x6 = child(sc1_1, _0, _0);
  148 #endif
  149 
  150   return 0;
  151 }
  152 
  153 #endif