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 <memory>
25 
26 using namespace gtsam;
27 
28 struct TestNode {
29  typedef std::shared_ptr<TestNode> shared_ptr;
30  int data;
31  std::vector<shared_ptr> children;
32  TestNode() : data(-1) {}
33  TestNode(int data) : data(data) {}
34 };
35 
36 struct TestForest {
37  typedef TestNode Node;
40  const FastVector<sharedNode>& roots() const { return roots_; }
41 };
42 
44  // 0 1
45  // / |
46  // 2 3
47  // |
48  // 4
49  TestForest forest;
50  forest.roots_.push_back(std::make_shared<TestNode>(0));
51  forest.roots_.push_back(std::make_shared<TestNode>(1));
52  forest.roots_[0]->children.push_back(std::make_shared<TestNode>(2));
53  forest.roots_[0]->children.push_back(std::make_shared<TestNode>(3));
54  forest.roots_[0]->children[1]->children.push_back(std::make_shared<TestNode>(4));
55  return forest;
56 }
57 
58 /* ************************************************************************* */
60  // This visitor stores the nodes visited so the visit order can be checked later. It also returns
61  // the current node index as the data and checks that the parent index properly matches the index
62  // referenced in the node.
63  std::list<int> visited;
65  PreOrderVisitor() : parentsMatched(true) {}
66  int operator()(const TestNode::shared_ptr& node, int parentData) {
67  visited.push_back(node->data);
68  // Check parent index
69  const int expectedParentIndex =
70  node->data == 0 ? -1 :
71  node->data == 1 ? -1 :
72  node->data == 2 ? 0 :
73  node->data == 3 ? 0 :
74  node->data == 4 ? 3 :
75  node->data == 10 ? 0 :
76  (parentsMatched = false, -1);
77  if(expectedParentIndex != parentData)
78  parentsMatched = false;
79  return node->data;
80  }
81 };
82 
83 /* ************************************************************************* */
85  // This visitor stores the nodes visited so the visit order can be checked later.
86  std::list<int> visited;
87  void operator()(const TestNode::shared_ptr& node, int myData) {
88  visited.push_back(node->data);
89  }
90 };
91 
92 /* ************************************************************************* */
93 std::list<int> getPreorder(const TestForest& forest) {
94  std::list<int> result;
95  PreOrderVisitor preVisitor;
96  int rootData = -1;
97  treeTraversal::DepthFirstForest(forest, rootData, preVisitor);
98  result = preVisitor.visited;
99  return result;
100 }
101 
102 /* ************************************************************************* */
103 TEST(treeTraversal, DepthFirst)
104 {
105  // Get test forest
106  TestForest testForest = makeTestForest();
107 
108  // Expected visit order
109  const std::list<int> preOrderExpected{0, 2, 3, 4, 1};
110  const std::list<int> postOrderExpected{2, 4, 3, 0, 1};
111 
112  // Actual visit order
113  PreOrderVisitor preVisitor;
114  PostOrderVisitor postVisitor;
115  int rootData = -1;
116  treeTraversal::DepthFirstForest(testForest, rootData, preVisitor, postVisitor);
117 
118  EXPECT(preVisitor.parentsMatched);
119  EXPECT(assert_container_equality(preOrderExpected, preVisitor.visited));
120  EXPECT(assert_container_equality(postOrderExpected, postVisitor.visited));
121 }
122 
123 /* ************************************************************************* */
124 TEST(treeTraversal, CloneForest)
125 {
126  // Get test forest
127  TestForest testForest1 = makeTestForest();
128  TestForest testForest2;
129  testForest2.roots_ = treeTraversal::CloneForest(testForest1);
130 
131  // Check that the original and clone both are expected
132  const std::list<int> preOrder1Expected{0, 2, 3, 4, 1};
133  std::list<int> preOrder1Actual = getPreorder(testForest1);
134  std::list<int> preOrder2Actual = getPreorder(testForest2);
135  EXPECT(assert_container_equality(preOrder1Expected, preOrder1Actual));
136  EXPECT(assert_container_equality(preOrder1Expected, preOrder2Actual));
137 
138  // Modify clone - should not modify original
139  testForest2.roots_[0]->children[1]->data = 10;
140  const std::list<int> preOrderModifiedExpected{0, 2, 10, 4, 1};
141 
142  // Check that original is the same and only the clone is modified
143  std::list<int> preOrder1ModActual = getPreorder(testForest1);
144  std::list<int> preOrder2ModActual = getPreorder(testForest2);
145  EXPECT(assert_container_equality(preOrder1Expected, preOrder1ModActual));
146  EXPECT(assert_container_equality(preOrderModifiedExpected, preOrder2ModActual));
147 }
148 
149 /* ************************************************************************* */
150 int main() {
151  TestResult tr;
152  return TestRegistry::runAllTests(tr);
153 }
154 /* ************************************************************************* */
155 
Provides additional testing facilities for common data structures.
FastVector< sharedNode > roots_
TestForest makeTestForest()
static int runAllTests(TestResult &result)
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
std::shared_ptr< TestNode > shared_ptr
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)
std::list< int > visited
Values result
DATA & parentData
std::vector< shared_ptr > children
#define EXPECT(condition)
Definition: Test.h:150
FastVector< std::shared_ptr< typename FOREST::Node > > CloneForest(const FOREST &forest)
traits
Definition: chartTesting.h:28
Node::shared_ptr sharedNode
const FastVector< sharedNode > & roots() const
TEST(SmartFactorBase, Pinhole)
std::list< int > visited
TestNode(int data)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:39:48