testTreeTraversal.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
21 
22 #include <vector>
23 #include <list>
24 #include <boost/shared_ptr.hpp>
25 #include <boost/make_shared.hpp>
26 #include <boost/assign/std/list.hpp>
27 
28 using boost::assign::operator+=;
29 using namespace std;
30 using namespace gtsam;
31 
32 struct TestNode {
33  typedef boost::shared_ptr<TestNode> shared_ptr;
34  int data;
35  vector<shared_ptr> children;
36  TestNode() : data(-1) {}
37  TestNode(int data) : data(data) {}
38 };
39 
40 struct TestForest {
41  typedef TestNode Node;
44  const FastVector<sharedNode>& roots() const { return roots_; }
45 };
46 
48  // 0 1
49  // / |
50  // 2 3
51  // |
52  // 4
53  TestForest forest;
54  forest.roots_.push_back(boost::make_shared<TestNode>(0));
55  forest.roots_.push_back(boost::make_shared<TestNode>(1));
56  forest.roots_[0]->children.push_back(boost::make_shared<TestNode>(2));
57  forest.roots_[0]->children.push_back(boost::make_shared<TestNode>(3));
58  forest.roots_[0]->children[1]->children.push_back(boost::make_shared<TestNode>(4));
59  return forest;
60 }
61 
62 /* ************************************************************************* */
64  // This visitor stores the nodes visited so the visit order can be checked later. It also returns
65  // the current node index as the data and checks that the parent index properly matches the index
66  // referenced in the node.
67  std::list<int> visited;
69  PreOrderVisitor() : parentsMatched(true) {}
70  int operator()(const TestNode::shared_ptr& node, int parentData) {
71  visited.push_back(node->data);
72  // Check parent index
73  const int expectedParentIndex =
74  node->data == 0 ? -1 :
75  node->data == 1 ? -1 :
76  node->data == 2 ? 0 :
77  node->data == 3 ? 0 :
78  node->data == 4 ? 3 :
79  node->data == 10 ? 0 :
80  (parentsMatched = false, -1);
81  if(expectedParentIndex != parentData)
82  parentsMatched = false;
83  return node->data;
84  }
85 };
86 
87 /* ************************************************************************* */
89  // This visitor stores the nodes visited so the visit order can be checked later.
90  std::list<int> visited;
91  void operator()(const TestNode::shared_ptr& node, int myData) {
92  visited.push_back(node->data);
93  }
94 };
95 
96 /* ************************************************************************* */
97 std::list<int> getPreorder(const TestForest& forest) {
98  std::list<int> result;
99  PreOrderVisitor preVisitor;
100  int rootData = -1;
101  treeTraversal::DepthFirstForest(forest, rootData, preVisitor);
102  result = preVisitor.visited;
103  return result;
104 }
105 
106 /* ************************************************************************* */
107 TEST(treeTraversal, DepthFirst)
108 {
109  // Get test forest
110  TestForest testForest = makeTestForest();
111 
112  // Expected visit order
113  std::list<int> preOrderExpected;
114  preOrderExpected += 0, 2, 3, 4, 1;
115  std::list<int> postOrderExpected;
116  postOrderExpected += 2, 4, 3, 0, 1;
117 
118  // Actual visit order
119  PreOrderVisitor preVisitor;
120  PostOrderVisitor postVisitor;
121  int rootData = -1;
122  treeTraversal::DepthFirstForest(testForest, rootData, preVisitor, postVisitor);
123 
124  EXPECT(preVisitor.parentsMatched);
125  EXPECT(assert_container_equality(preOrderExpected, preVisitor.visited));
126  EXPECT(assert_container_equality(postOrderExpected, postVisitor.visited));
127 }
128 
129 /* ************************************************************************* */
130 TEST(treeTraversal, CloneForest)
131 {
132  // Get test forest
133  TestForest testForest1 = makeTestForest();
134  TestForest testForest2;
135  testForest2.roots_ = treeTraversal::CloneForest(testForest1);
136 
137  // Check that the original and clone both are expected
138  std::list<int> preOrder1Expected;
139  preOrder1Expected += 0, 2, 3, 4, 1;
140  std::list<int> preOrder1Actual = getPreorder(testForest1);
141  std::list<int> preOrder2Actual = getPreorder(testForest2);
142  EXPECT(assert_container_equality(preOrder1Expected, preOrder1Actual));
143  EXPECT(assert_container_equality(preOrder1Expected, preOrder2Actual));
144 
145  // Modify clone - should not modify original
146  testForest2.roots_[0]->children[1]->data = 10;
147  std::list<int> preOrderModifiedExpected;
148  preOrderModifiedExpected += 0, 2, 10, 4, 1;
149 
150  // Check that original is the same and only the clone is modified
151  std::list<int> preOrder1ModActual = getPreorder(testForest1);
152  std::list<int> preOrder2ModActual = getPreorder(testForest2);
153  EXPECT(assert_container_equality(preOrder1Expected, preOrder1ModActual));
154  EXPECT(assert_container_equality(preOrderModifiedExpected, preOrder2ModActual));
155 }
156 
157 /* ************************************************************************* */
158 int main() {
159  TestResult tr;
160  return TestRegistry::runAllTests(tr);
161 }
162 /* ************************************************************************* */
163 
Provides additional testing facilities for common data structures.
FastVector< sharedNode > roots_
TestForest makeTestForest()
static int runAllTests(TestResult &result)
Definition: Half.h:150
void operator()(const TestNode::shared_ptr &node, int myData)
std::list< int > getPreorder(const TestForest &forest)
int operator()(const TestNode::shared_ptr &node, int parentData)
int main()
bool assert_container_equality(const std::map< size_t, V2 > &expected, const std::map< size_t, V2 > &actual)
void DepthFirstForest(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost)
boost::shared_ptr< TestNode > shared_ptr
std::list< int > visited
Values result
DATA & parentData
#define EXPECT(condition)
Definition: Test.h:151
const FastVector< sharedNode > & roots() const
FastVector< boost::shared_ptr< typename FOREST::Node > > CloneForest(const FOREST &forest)
traits
Definition: chartTesting.h:28
Node::shared_ptr sharedNode
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
TEST(treeTraversal, DepthFirst)
std::list< int > visited
vector< shared_ptr > children
TestNode(int data)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:50:06