From 3a4198b2bcb33a66e81bb1e04d685c8fc9d4b34b Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Mon, 15 Apr 2013 03:48:12 +0200 Subject: [PATCH] clean up and comment test (hierarchy rebuilding through visitation) --- tests/40core.tests | 5 + .../hierarchy-orientation-indicator-test.cpp | 154 +++++++----------- 2 files changed, 64 insertions(+), 95 deletions(-) diff --git a/tests/40core.tests b/tests/40core.tests index 0922946ec..ed127f57f 100644 --- a/tests/40core.tests +++ b/tests/40core.tests @@ -346,6 +346,11 @@ return: 0 END +TEST "Hierarchy rebuilding" HierarchyOrientationIndicator_test < #include -#include //////////////////////////////TODO #include #include #include #include -//using boost::lexical_cast; -using util::contains; -using std::string; -using util::isnil; -using std::cout; -using std::endl; namespace lib { namespace test { namespace { // test fixture: a random Tree to navigate... - namespace error=lumiera::error; - using std::rand; -// using std::vector; - using std::tr1::ref; using std::tr1::function; - using lib::IterStateWrapper; using lib::transformIterator; + using lib::iter_stl::eachAddress; + using util::contains; + using util::max; - const uint MAX_CHILDREN(5); - const double CHILD_PROBABILITY(0.45); - - const uint CHILDREN_SEED(50); + /* -- size of the test tree ---- */ + const uint MAX_CHILDREN_CNT(5); // children per Node (5 means 0 to 4 children) + const double CHILD_PROBABILITY(0.45); // probability for a Node to have any children + const uint TEST_SEQUENCE_LENGTH(50); // test uses a sequence of Node trees + // 5 - 45% - 50 produce roughly 1000 Nodes and tree depths of about 12 uint nextChildID(1); + /** - * pick a random child count below #MAX_CHILDREN + * pick a random child count below #MAX_CHILDREN_CNT * with a probability to get any count above zero * as defined by CHILD_PROBABILITY */ inline uint pick_random_count() { - uint bottom((1.0/CHILD_PROBABILITY - 1) * MAX_CHILDREN); - uint limit = bottom + MAX_CHILDREN; + uint bottom((1.0/CHILD_PROBABILITY - 1) * MAX_CHILDREN_CNT); + uint limit = bottom + MAX_CHILDREN_CNT; ASSERT (0 < bottom); ASSERT (bottom < limit); int cnt = (rand() % limit) - bottom; - return MAX (0, cnt); + return max(0, cnt); } - + /** (sub)tree of test data */ struct Node : boost::equality_comparable { typedef std::vector Children; - typedef RangeIter ChildSeq; int id_; Children children_; - Node(int i) + + Node(int i) ///< build node explicitly without children : id_(i) { } - Node() + + Node() ///< build a random test subtree : id_(nextChildID++) { uint c = pick_random_count(); - cout << "++-----Node-"<(node.childSequence())); + build(children_to_visit).usingSequence (eachAddress (node->children_)); return children_to_visit; } @@ -203,6 +181,10 @@ namespace test { public: // using default ctor and copy operations + static function + create () { return NodeVisitor(); } + + VisitationData operator() (Node* node) { @@ -230,7 +212,6 @@ namespace test { // visitation continues with children below this level path_.resize(level); path_.push_back(nextNode); - cout << "-----fork-at-"<children_.size()<<")"< start new tree path at root path_.clear(); path_.push_back(nextNode); - cout << "-----new-path-child="<id_<<" ("<children_.size()<<")"<(NodeVisitor()))); + NodeVisitor::create())); } private: @@ -266,8 +254,8 @@ namespace test { struct Builder { Builder (Node& startPoint) - : parent(NULL) - , current(&startPoint) + : parent_(NULL) + , current_(&startPoint) { } void @@ -279,49 +267,46 @@ namespace test { if (direction < 0) { treeVisitation->orientation += 1; - cout << "Node "<id<<" : ^"< 0) { treeVisitation->orientation -= 1; - cout << "Node "<id<<" : V"<id<<" : ++>"<id); ++treeVisitation; }}} private: - Node* parent; - Node* current; + Node* parent_; + Node* current_; void addNode (int id) { - current = & parent->makeChild(id); + current_ = & parent_->makeChild(id); } Node& startChildTransaction() { - Node& oldRefPoint (*parent); - ASSERT (current); - parent = current; // set new ref point + Node& oldRefPoint (*parent_); + ASSERT (current_); + parent_ = current_; // set new ref point return oldRefPoint; } void commitChildTransaction(Node& refPoint) { - parent = &refPoint; - current = parent; + parent_ = &refPoint; + current_ = parent_; } }; @@ -329,7 +314,6 @@ namespace test { Builder builder(*this); // pre-existing implicit root context builder.populateBy (treeVisitation); } - }; @@ -340,11 +324,11 @@ namespace test { /*************************************************************************** - * @test cover various detail aspects regarding - * - weakness of + * @test describing and rebuilding a tree structure + * while visiting the tree in depth first order. * - * @see HashIndexed_test * @see HierarchyOrientationIndicator + * @see DispatcherInterface_test */ class HierarchyOrientationIndicator_test : public Test { @@ -354,47 +338,27 @@ namespace test { demonstrate_tree_rebuilding (); } - /** @test demonstrate a serious weakness of - * When... + /** @test demonstrate how a Node tree structure can be rebuilt + * just based on the visitation sequence of an original tree. + * This visitation captures the local data of the Node (here the ID) + * and the orientation of the visitation path (down, next sibling, up) * - * This problem is especially dangerous when... + * This is a demonstration and blueprint for constructing the scheduler interface. + * The Scheduler accepts a series of new jobs, but jobs may depend on each other, + * and the jobs are created while exploring the dependencies in the render engine's + * node graph (low-level-model). */ void demonstrate_tree_rebuilding ( ) { Node::Children testWood; - for (uint i=0; i < CHILDREN_SEED; ++i) + for (uint i=0; i < TEST_SEQUENCE_LENGTH; ++i) testWood.push_back(Node()); - using iter_stl::eachElm; - using lib::AddressExposingIter; - + TreeRebuilder reconstructed (depthFirst (eachAddress (testWood)) >>= exploreChildren); - TreeRebuilder reconstructed (depthFirst (AddressExposingIter(eachElm (testWood))) >>= exploreChildren); - - cout << "total children "<