testBTree.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file testBTree.cpp
  10. * @date Feb 3, 2010
  11. * @author Chris Beall
  12. * @author Frank Dellaert
  13. */
  14. #include <boost/shared_ptr.hpp>
  15. #include <boost/assign/std/list.hpp> // for +=
  16. using namespace boost::assign;
  17. #include <CppUnitLite/TestHarness.h>
  18. #include <gtsam_unstable/base/BTree.h>
  19. using namespace std;
  20. using namespace gtsam;
  21. typedef pair<size_t, size_t> Range;
  22. typedef BTree<string, Range> RangeTree;
  23. typedef BTree<string, int> IntTree;
  24. static std::stringstream ss;
  25. static string x1("x1"), x2("x2"), x3("x3"), x4("x4"), x5("x5");
  26. typedef pair<string, int> KeyInt;
  27. KeyInt p1(x1, 1), p2(x2, 2), p3(x3, 3), p4(x4, 4), p5(x5, 5);
  28. /* ************************************************************************* */
  29. int f(const string& key, const Range& range) {
  30. return range.first;
  31. }
  32. void g(const string& key, int i) {
  33. ss << (string) key;
  34. }
  35. int add(const string& k, int v, int a) {
  36. return v + a;
  37. }
  38. /* ************************************************************************* */
  39. TEST( BTree, add )
  40. {
  41. RangeTree tree;
  42. CHECK(tree.empty())
  43. LONGS_EQUAL(0,tree.height())
  44. // check the height of tree after adding an element
  45. RangeTree tree1 = tree.add(x1, Range(1, 1));
  46. LONGS_EQUAL(1,tree1.height())
  47. LONGS_EQUAL(1,tree1.size())
  48. CHECK(tree1.find(x1) == Range(1,1))
  49. RangeTree tree2 = tree1.add(x5, Range(5, 2));
  50. RangeTree tree3 = tree2.add(x3, Range(3, 3));
  51. LONGS_EQUAL(3,tree3.size())
  52. CHECK(tree3.find(x5) == Range(5,2))
  53. CHECK(tree3.find(x3) == Range(3,3))
  54. RangeTree tree4 = tree3.add(x2, Range(2, 4));
  55. RangeTree tree5 = tree4.add(x4, Range(4, 5));
  56. LONGS_EQUAL(5,tree5.size())
  57. CHECK(tree5.find(x4) == Range(4,5))
  58. // Test functional nature: tree5 and tree6 have different values for x4
  59. RangeTree tree6 = tree5.add(x4, Range(6, 6));
  60. CHECK(tree5.find(x4) == Range(4,5))
  61. CHECK(tree6.find(x4) == Range(6,6))
  62. // test assignment
  63. RangeTree c5 = tree5;
  64. LONGS_EQUAL(5,c5.size())
  65. CHECK(c5.find(x4) == Range(4,5))
  66. // test map
  67. // After (map f tree5) tree contains (x1,1), (x2,2), etc...
  68. IntTree mapped = tree5.map<int> (f);
  69. LONGS_EQUAL(2,mapped.find(x2));
  70. LONGS_EQUAL(4,mapped.find(x4));
  71. }
  72. /* ************************************************************************* */
  73. TEST( BTree, equality )
  74. {
  75. IntTree tree1 = IntTree().add(p1).add(p2).add(p3).add(p4).add(p5);
  76. CHECK(tree1==tree1)
  77. CHECK(tree1.same(tree1))
  78. IntTree tree2 = IntTree().add(p1).add(p2).add(p3).add(p4).add(p5);
  79. CHECK(tree2==tree1)
  80. CHECK(!tree2.same(tree1))
  81. IntTree tree3 = IntTree().add(p1).add(p2).add(p3).add(p4);
  82. CHECK(tree3!=tree1)
  83. CHECK(tree3!=tree2)
  84. CHECK(!tree3.same(tree1))
  85. CHECK(!tree3.same(tree2))
  86. IntTree tree4 = tree3.add(p5);
  87. CHECK(tree4==tree1)
  88. CHECK(!tree4.same(tree1))
  89. IntTree tree5 = tree1;
  90. CHECK(tree5==tree1)
  91. CHECK(tree5==tree2)
  92. CHECK(tree5.same(tree1))
  93. CHECK(!tree5.same(tree2))
  94. }
  95. /* ************************************************************************* */
  96. TEST( BTree, iterating )
  97. {
  98. IntTree tree = IntTree().add(p1).add(p2).add(p3).add(p4).add(p5);
  99. // test iter
  100. tree.iter(g);
  101. CHECK(ss.str() == string("x1x2x3x4x5"));
  102. // test fold
  103. LONGS_EQUAL(25,tree.fold<int>(add,10))
  104. // test iterator
  105. BTree<string, int>::const_iterator it = tree.begin(), it2 = tree.begin();
  106. CHECK(it==it2)
  107. CHECK(*it == p1)
  108. CHECK(it->first == x1)
  109. CHECK(it->second == 1)
  110. CHECK(*(++it) == p2)
  111. CHECK(it!=it2)
  112. CHECK(it==(++it2))
  113. CHECK(*(++it) == p3)
  114. CHECK(*(it++) == p3)
  115. // post-increment, not so efficient
  116. CHECK(*it == p4)
  117. CHECK(*(++it) == p5)
  118. CHECK((++it)==tree.end())
  119. // acid iterator test
  120. int sum = 0;
  121. for(const KeyInt& p: tree)
  122. sum += p.second;
  123. LONGS_EQUAL(15,sum)
  124. // STL iterator test
  125. list<KeyInt> expected, actual;
  126. expected += p1,p2,p3,p4,p5;
  127. copy (tree.begin(),tree.end(),back_inserter(actual));
  128. CHECK(actual==expected)
  129. }
  130. /* ************************************************************************* */
  131. TEST( BTree, remove )
  132. {
  133. IntTree tree5 = IntTree().add(p1).add(p2).add(p3).add(p4).add(p5);
  134. LONGS_EQUAL(5,tree5.size())
  135. CHECK(tree5.mem(x3))
  136. IntTree tree4 = tree5.remove(x3);
  137. LONGS_EQUAL(4,tree4.size())
  138. CHECK(!tree4.mem(x3))
  139. }
  140. /* ************************************************************************* */
  141. TEST( BTree, stress )
  142. {
  143. RangeTree tree;
  144. list<RangeTree::value_type> expected;
  145. int N = 128;
  146. for (int i = 1; i <= N; i++) {
  147. string key('a', i);
  148. Range value(i - 1, i);
  149. tree = tree.add(key, value);
  150. LONGS_EQUAL(i,tree.size())
  151. CHECK(tree.find(key) == value)
  152. expected += make_pair(key, value);
  153. }
  154. // Check height is log(N)
  155. LONGS_EQUAL(8,tree.height())
  156. // stress test iterator
  157. list<RangeTree::value_type> actual;
  158. copy(tree.begin(), tree.end(), back_inserter(actual));
  159. CHECK(actual==expected)
  160. // deconstruct the tree
  161. for (int i = N; i >= N; i--) {
  162. string key('a', i);
  163. tree = tree.remove(key);
  164. LONGS_EQUAL(i-1,tree.size())
  165. CHECK(!tree.mem(key))
  166. }
  167. }
  168. /* ************************************************************************* */
  169. int main() {
  170. TestResult tr;
  171. return TestRegistry::runAllTests(tr);
  172. }
  173. /* ************************************************************************* */