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 
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
PreOrderVisitor::PreOrderVisitor
PreOrderVisitor()
Definition: testTreeTraversal.cpp:65
makeTestForest
TestForest makeTestForest()
Definition: testTreeTraversal.cpp:43
treeTraversal-inst.h
TestNode::shared_ptr
std::shared_ptr< TestNode > shared_ptr
Definition: testTreeTraversal.cpp:29
PostOrderVisitor::visited
std::list< int > visited
Definition: testTreeTraversal.cpp:86
TestForest
Definition: testTreeTraversal.cpp:36
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
TestNode::children
std::vector< shared_ptr > children
Definition: testTreeTraversal.cpp:31
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
PostOrderVisitor
Definition: testTreeTraversal.cpp:84
TestForest::roots_
FastVector< sharedNode > roots_
Definition: testTreeTraversal.cpp:39
getPreorder
std::list< int > getPreorder(const TestForest &forest)
Definition: testTreeTraversal.cpp:93
PostOrderVisitor::operator()
void operator()(const TestNode::shared_ptr &node, int myData)
Definition: testTreeTraversal.cpp:87
main
int main()
Definition: testTreeTraversal.cpp:150
result
Values result
Definition: OdometryOptimize.cpp:8
TestForest::Node
TestNode Node
Definition: testTreeTraversal.cpp:37
TestNode::TestNode
TestNode(int data)
Definition: testTreeTraversal.cpp:33
TestableAssertions.h
Provides additional testing facilities for common data structures.
TestNode
Definition: testTreeTraversal.cpp:28
data
int data[]
Definition: Map_placement_new.cpp:1
parentData
DATA & parentData
Definition: treeTraversal-inst.h:45
TestNode::data
int data
Definition: testTreeTraversal.cpp:30
TestNode::TestNode
TestNode()
Definition: testTreeTraversal.cpp:32
gtsam::treeTraversal::DepthFirstForest
void DepthFirstForest(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost)
Definition: treeTraversal-inst.h:77
TestResult
Definition: TestResult.h:26
TestForest::sharedNode
Node::shared_ptr sharedNode
Definition: testTreeTraversal.cpp:38
gtsam
traits
Definition: SFMdata.h:40
gtsam::TEST
TEST(SmartFactorBase, Pinhole)
Definition: testSmartFactorBase.cpp:38
gtsam::treeTraversal::CloneForest
FastVector< std::shared_ptr< typename FOREST::Node > > CloneForest(const FOREST &forest)
Definition: treeTraversal-inst.h:191
gtsam::assert_container_equality
bool assert_container_equality(const std::map< size_t, V2 > &expected, const std::map< size_t, V2 > &actual)
Definition: TestableAssertions.h:237
TestForest::roots
const FastVector< sharedNode > & roots() const
Definition: testTreeTraversal.cpp:40
PreOrderVisitor::operator()
int operator()(const TestNode::shared_ptr &node, int parentData)
Definition: testTreeTraversal.cpp:66
PreOrderVisitor
Definition: testTreeTraversal.cpp:59
PreOrderVisitor::parentsMatched
bool parentsMatched
Definition: testTreeTraversal.cpp:64
PreOrderVisitor::visited
std::list< int > visited
Definition: testTreeTraversal.cpp:63


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:08:48