testBTree.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 
19 #include <boost/shared_ptr.hpp>
20 #include <boost/assign/std/list.hpp> // for +=
21 using namespace boost::assign;
22 
25 
26 using namespace std;
27 using namespace gtsam;
28 
29 typedef pair<size_t, size_t> Range;
32 
33 static std::stringstream ss;
34 static string x1("x1"), x2("x2"), x3("x3"), x4("x4"), x5("x5");
35 typedef pair<string, int> KeyInt;
36 KeyInt p1(x1, 1), p2(x2, 2), p3(x3, 3), p4(x4, 4), p5(x5, 5);
37 
38 /* ************************************************************************* */
39 int f(const string& key, const Range& range) {
40  return range.first;
41 }
42 
43 void g(const string& key, int i) {
44  ss << (string) key;
45 }
46 
47 int add(const string& k, int v, int a) {
48  return v + a;
49 }
50 
51 /* ************************************************************************* */
53 {
55  CHECK(tree.empty())
56  LONGS_EQUAL(0,tree.height())
57 
58  // check the height of tree after adding an element
59  RangeTree tree1 = tree.add(x1, Range(1, 1));
60  LONGS_EQUAL(1,tree1.height())
61  LONGS_EQUAL(1,tree1.size())
62  CHECK(tree1.find(x1) == Range(1,1))
63 
64  RangeTree tree2 = tree1.add(x5, Range(5, 2));
65  RangeTree tree3 = tree2.add(x3, Range(3, 3));
66  LONGS_EQUAL(3,tree3.size())
67  CHECK(tree3.find(x5) == Range(5,2))
68  CHECK(tree3.find(x3) == Range(3,3))
69 
70  RangeTree tree4 = tree3.add(x2, Range(2, 4));
71  RangeTree tree5 = tree4.add(x4, Range(4, 5));
72  LONGS_EQUAL(5,tree5.size())
73  CHECK(tree5.find(x4) == Range(4,5))
74 
75  // Test functional nature: tree5 and tree6 have different values for x4
76  RangeTree tree6 = tree5.add(x4, Range(6, 6));
77  CHECK(tree5.find(x4) == Range(4,5))
78  CHECK(tree6.find(x4) == Range(6,6))
79 
80  // test assignment
81  RangeTree c5 = tree5;
82  LONGS_EQUAL(5,c5.size())
83  CHECK(c5.find(x4) == Range(4,5))
84 
85  // test map
86  // After (map f tree5) tree contains (x1,1), (x2,2), etc...
87  IntTree mapped = tree5.map<int> (f);
88  LONGS_EQUAL(2,mapped.find(x2));
89  LONGS_EQUAL(4,mapped.find(x4));
90 }
91 
92 /* ************************************************************************* */
94 {
95  IntTree tree1 = IntTree().add(p1).add(p2).add(p3).add(p4).add(p5);
96  CHECK(tree1==tree1)
97  CHECK(tree1.same(tree1))
98 
99  IntTree tree2 = IntTree().add(p1).add(p2).add(p3).add(p4).add(p5);
100  CHECK(tree2==tree1)
101  CHECK(!tree2.same(tree1))
102 
103  IntTree tree3 = IntTree().add(p1).add(p2).add(p3).add(p4);
104  CHECK(tree3!=tree1)
105  CHECK(tree3!=tree2)
106  CHECK(!tree3.same(tree1))
107  CHECK(!tree3.same(tree2))
108 
109  IntTree tree4 = tree3.add(p5);
110  CHECK(tree4==tree1)
111  CHECK(!tree4.same(tree1))
112 
113  IntTree tree5 = tree1;
114  CHECK(tree5==tree1)
115  CHECK(tree5==tree2)
116  CHECK(tree5.same(tree1))
117  CHECK(!tree5.same(tree2))
118 }
119 
120 /* ************************************************************************* */
121 TEST( BTree, iterating )
122 {
123  IntTree tree = IntTree().add(p1).add(p2).add(p3).add(p4).add(p5);
124 
125  // test iter
126  tree.iter(g);
127  CHECK(ss.str() == string("x1x2x3x4x5"));
128 
129  // test fold
130  LONGS_EQUAL(25,tree.fold<int>(add,10))
131 
132  // test iterator
133  BTree<string, int>::const_iterator it = tree.begin(), it2 = tree.begin();
134  CHECK(it==it2)
135  CHECK(*it == p1)
136  CHECK(it->first == x1)
137  CHECK(it->second == 1)
138  CHECK(*(++it) == p2)
139  CHECK(it!=it2)
140  CHECK(it==(++it2))
141  CHECK(*(++it) == p3)
142  CHECK(*(it++) == p3)
143  // post-increment, not so efficient
144  CHECK(*it == p4)
145  CHECK(*(++it) == p5)
146  CHECK((++it)==tree.end())
147 
148  // acid iterator test
149  int sum = 0;
150  for(const KeyInt& p: tree)
151 sum += p.second;
152  LONGS_EQUAL(15,sum)
153 
154  // STL iterator test
155  list<KeyInt> expected, actual;
156  expected += p1,p2,p3,p4,p5;
157  copy (tree.begin(),tree.end(),back_inserter(actual));
158  CHECK(actual==expected)
159 }
160 
161 /* ************************************************************************* */
162 TEST( BTree, remove )
163 {
164  IntTree tree5 = IntTree().add(p1).add(p2).add(p3).add(p4).add(p5);
165  LONGS_EQUAL(5,tree5.size())
166  CHECK(tree5.mem(x3))
167  IntTree tree4 = tree5.remove(x3);
168  LONGS_EQUAL(4,tree4.size())
169  CHECK(!tree4.mem(x3))
170 }
171 
172 /* ************************************************************************* */
173 TEST( BTree, stress )
174 {
175  RangeTree tree;
176  list<RangeTree::value_type> expected;
177  int N = 128;
178  for (int i = 1; i <= N; i++) {
179  string key('a', i);
180  Range value(i - 1, i);
181  tree = tree.add(key, value);
182  LONGS_EQUAL(i,tree.size())
183  CHECK(tree.find(key) == value)
184  expected += make_pair(key, value);
185  }
186 
187  // Check height is log(N)
188  LONGS_EQUAL(8,tree.height())
189 
190  // stress test iterator
191  list<RangeTree::value_type> actual;
192  copy(tree.begin(), tree.end(), back_inserter(actual));
193  CHECK(actual==expected)
194 
195  // deconstruct the tree
196  for (int i = N; i >= N; i--) {
197  string key('a', i);
198  tree = tree.remove(key);
199  LONGS_EQUAL(i-1,tree.size())
200  CHECK(!tree.mem(key))
201  }
202 }
203 
204 /* ************************************************************************* */
205 int main() {
206  TestResult tr;
207  return TestRegistry::runAllTests(tr);
208 }
209 /* ************************************************************************* */
size_t height() const
Definition: BTree.h:225
BTree add(const value_type &xd) const
Definition: BTree.h:143
#define CHECK(condition)
Definition: Test.h:109
static string x3("x3")
static int runAllTests(TestResult &result)
int f(const string &key, const Range &range)
Definition: testBTree.cpp:39
Matrix expected
Definition: testMatrix.cpp:974
KeyInt p4(x4, 4)
static string x2("x2")
ArrayXcf v
Definition: Cwise_arg.cpp:1
BTree< string, int > IntTree
Definition: testBTree.cpp:31
Definition: Half.h:150
purely functional binary tree
TEST(BTree, add)
Definition: testBTree.cpp:52
#define N
Definition: gksort.c:12
static string x5("x5")
void g(const string &key, int i)
Definition: testBTree.cpp:43
bool mem(const KEY &x) const
Definition: BTree.h:160
Array33i a
int main()
Definition: testBTree.cpp:205
Const iterator Not trivial: iterator keeps a stack to indicate current path from root_.
Definition: BTree.h:297
void iter(boost::function< void(const KEY &, const VALUE &)> f) const
Definition: BTree.h:263
ACC fold(boost::function< ACC(const KEY &, const VALUE &, const ACC &)> f, const ACC &a) const
Definition: BTree.h:285
const_iterator begin() const
Definition: BTree.h:403
KeyInt p1(x1, 1)
bool same(const BTree &other) const
Definition: BTree.h:170
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
Definition: mpreal.h:2381
KeyInt p5(x5, 5)
BTree< string, Range > RangeTree
Definition: testBTree.cpp:30
pair< size_t, size_t > Range
Definition: testBTree.cpp:29
const VALUE & find(const KEY &k) const
Definition: BTree.h:239
static std::stringstream ss
Definition: testBTree.cpp:33
#define LONGS_EQUAL(expected, actual)
Definition: Test.h:135
BTree< KEY, TO > map(boost::function< TO(const KEY &, const VALUE &)> f) const
Definition: BTree.h:272
KeyInt p3(x3, 3)
traits
Definition: chartTesting.h:28
KeyInt p2(x2, 2)
static string x1("x1")
bool empty() const
Definition: BTree.h:138
const_iterator end() const
Definition: BTree.h:408
float * p
static string x4("x4")
BTree remove(const KEY &x) const
Definition: BTree.h:214
size_t size() const
Definition: BTree.h:230
int add(const string &k, int v, int a)
Definition: testBTree.cpp:47
pair< string, int > KeyInt
Definition: testBTree.cpp:35


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:21