btree_test.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright 2013 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "gtest/gtest.h"
  15. #include "btree_map.h"
  16. #include "btree_set.h"
  17. #include "btree_test.h"
  18. namespace btree {
  19. namespace {
  20. template <typename K, int N>
  21. void SetTest() {
  22. typedef TestAllocator<K> TestAlloc;
  23. ASSERT_EQ(sizeof(btree_set<K>), sizeof(void*));
  24. BtreeTest<btree_set<K, std::less<K>, std::allocator<K>, N>, std::set<K> >();
  25. BtreeAllocatorTest<btree_set<K, std::less<K>, TestAlloc, N> >();
  26. }
  27. template <typename K, int N>
  28. void MapTest() {
  29. typedef TestAllocator<K> TestAlloc;
  30. ASSERT_EQ(sizeof(btree_map<K, K>), sizeof(void*));
  31. BtreeTest<btree_map<K, K, std::less<K>, std::allocator<K>, N>, std::map<K, K> >();
  32. BtreeAllocatorTest<btree_map<K, K, std::less<K>, TestAlloc, N> >();
  33. BtreeMapTest<btree_map<K, K, std::less<K>, std::allocator<K>, N> >();
  34. }
  35. TEST(Btree, set_int32_32) { SetTest<int32_t, 32>(); }
  36. TEST(Btree, set_int32_64) { SetTest<int32_t, 64>(); }
  37. TEST(Btree, set_int32_128) { SetTest<int32_t, 128>(); }
  38. TEST(Btree, set_int32_256) { SetTest<int32_t, 256>(); }
  39. TEST(Btree, set_int64_256) { SetTest<int64_t, 256>(); }
  40. TEST(Btree, set_string_256) { SetTest<std::string, 256>(); }
  41. TEST(Btree, set_pair_256) { SetTest<std::pair<int, int>, 256>(); }
  42. TEST(Btree, map_int32_256) { MapTest<int32_t, 256>(); }
  43. TEST(Btree, map_int64_256) { MapTest<int64_t, 256>(); }
  44. TEST(Btree, map_string_256) { MapTest<std::string, 256>(); }
  45. TEST(Btree, map_pair_256) { MapTest<std::pair<int, int>, 256>(); }
  46. // Large-node tests
  47. TEST(Btree, map_int32_1024) { MapTest<int32_t, 1024>(); }
  48. TEST(Btree, map_int32_1032) { MapTest<int32_t, 1032>(); }
  49. TEST(Btree, map_int32_1040) { MapTest<int32_t, 1040>(); }
  50. TEST(Btree, map_int32_1048) { MapTest<int32_t, 1048>(); }
  51. TEST(Btree, map_int32_1056) { MapTest<int32_t, 1056>(); }
  52. TEST(Btree, map_int32_2048) { MapTest<int32_t, 2048>(); }
  53. TEST(Btree, map_int32_4096) { MapTest<int32_t, 4096>(); }
  54. TEST(Btree, set_int32_1024) { SetTest<int32_t, 1024>(); }
  55. TEST(Btree, set_int32_2048) { SetTest<int32_t, 2048>(); }
  56. TEST(Btree, set_int32_4096) { SetTest<int32_t, 4096>(); }
  57. TEST(Btree, map_string_1024) { MapTest<std::string, 1024>(); }
  58. TEST(Btree, map_string_2048) { MapTest<std::string, 2048>(); }
  59. TEST(Btree, map_string_4096) { MapTest<std::string, 4096>(); }
  60. TEST(Btree, set_string_1024) { SetTest<std::string, 1024>(); }
  61. TEST(Btree, set_string_2048) { SetTest<std::string, 2048>(); }
  62. TEST(Btree, set_string_4096) { SetTest<std::string, 4096>(); }
  63. template <typename K, int N>
  64. void MultiSetTest() {
  65. typedef TestAllocator<K> TestAlloc;
  66. ASSERT_EQ(sizeof(btree_multiset<K>), sizeof(void*));
  67. BtreeMultiTest<btree_multiset<K, std::less<K>, std::allocator<K>, N>,
  68. std::multiset<K> >();
  69. BtreeAllocatorTest<btree_multiset<K, std::less<K>, TestAlloc, N> >();
  70. }
  71. template <typename K, int N>
  72. void MultiMapTest() {
  73. typedef TestAllocator<K> TestAlloc;
  74. ASSERT_EQ(sizeof(btree_multimap<K, K>), sizeof(void*));
  75. BtreeMultiTest<btree_multimap<K, K, std::less<K>, std::allocator<K>, N>,
  76. std::multimap<K, K> >();
  77. BtreeMultiMapTest<btree_multimap<K, K, std::less<K>, std::allocator<K>, N> >();
  78. BtreeAllocatorTest<btree_multimap<K, K, std::less<K>, TestAlloc, N> >();
  79. }
  80. TEST(Btree, multiset_int32_256) { MultiSetTest<int32_t, 256>(); }
  81. TEST(Btree, multiset_int64_256) { MultiSetTest<int64_t, 256>(); }
  82. TEST(Btree, multiset_string_256) { MultiSetTest<std::string, 256>(); }
  83. TEST(Btree, multiset_pair_256) { MultiSetTest<std::pair<int, int>, 256>(); }
  84. TEST(Btree, multimap_int32_256) { MultiMapTest<int32_t, 256>(); }
  85. TEST(Btree, multimap_int64_256) { MultiMapTest<int64_t, 256>(); }
  86. TEST(Btree, multimap_string_256) { MultiMapTest<std::string, 256>(); }
  87. TEST(Btree, multimap_pair_256) { MultiMapTest<std::pair<int, int>, 256>(); }
  88. // Large-node tests
  89. TEST(Btree, multimap_int32_1024) { MultiMapTest<int32_t, 1024>(); }
  90. TEST(Btree, multimap_int32_2048) { MultiMapTest<int32_t, 2048>(); }
  91. TEST(Btree, multimap_int32_4096) { MultiMapTest<int32_t, 4096>(); }
  92. TEST(Btree, multiset_int32_1024) { MultiSetTest<int32_t, 1024>(); }
  93. TEST(Btree, multiset_int32_2048) { MultiSetTest<int32_t, 2048>(); }
  94. TEST(Btree, multiset_int32_4096) { MultiSetTest<int32_t, 4096>(); }
  95. TEST(Btree, multimap_string_1024) { MultiMapTest<std::string, 1024>(); }
  96. TEST(Btree, multimap_string_2048) { MultiMapTest<std::string, 2048>(); }
  97. TEST(Btree, multimap_string_4096) { MultiMapTest<std::string, 4096>(); }
  98. TEST(Btree, multiset_string_1024) { MultiSetTest<std::string, 1024>(); }
  99. TEST(Btree, multiset_string_2048) { MultiSetTest<std::string, 2048>(); }
  100. TEST(Btree, multiset_string_4096) { MultiSetTest<std::string, 4096>(); }
  101. // Verify that swapping btrees swaps the key comparision functors.
  102. struct SubstringLess {
  103. SubstringLess() : n(2) {}
  104. SubstringLess(size_t length)
  105. : n(length) {
  106. }
  107. bool operator()(const std::string &a, const std::string &b) const {
  108. std::string as(a.data(), std::min(n, a.size()));
  109. std::string bs(b.data(), std::min(n, b.size()));
  110. return as < bs;
  111. }
  112. size_t n;
  113. };
  114. TEST(Btree, SwapKeyCompare) {
  115. typedef btree_set<std::string, SubstringLess> SubstringSet;
  116. SubstringSet s1(SubstringLess(1), SubstringSet::allocator_type());
  117. SubstringSet s2(SubstringLess(2), SubstringSet::allocator_type());
  118. ASSERT_TRUE(s1.insert("a").second);
  119. ASSERT_FALSE(s1.insert("aa").second);
  120. ASSERT_TRUE(s2.insert("a").second);
  121. ASSERT_TRUE(s2.insert("aa").second);
  122. ASSERT_FALSE(s2.insert("aaa").second);
  123. swap(s1, s2);
  124. ASSERT_TRUE(s1.insert("b").second);
  125. ASSERT_TRUE(s1.insert("bb").second);
  126. ASSERT_FALSE(s1.insert("bbb").second);
  127. ASSERT_TRUE(s2.insert("b").second);
  128. ASSERT_FALSE(s2.insert("bb").second);
  129. }
  130. TEST(Btree, UpperBoundRegression) {
  131. // Regress a bug where upper_bound would default-construct a new key_compare
  132. // instead of copying the existing one.
  133. typedef btree_set<std::string, SubstringLess> SubstringSet;
  134. SubstringSet my_set(SubstringLess(3));
  135. my_set.insert("aab");
  136. my_set.insert("abb");
  137. // We call upper_bound("aaa"). If this correctly uses the length 3
  138. // comparator, aaa < aab < abb, so we should get aab as the result.
  139. // If it instead uses the default-constructed length 2 comparator,
  140. // aa == aa < ab, so we'll get abb as our result.
  141. SubstringSet::iterator it = my_set.upper_bound("aaa");
  142. ASSERT_TRUE(it != my_set.end());
  143. EXPECT_EQ("aab", *it);
  144. }
  145. TEST(Btree, IteratorIncrementBy) {
  146. // Test that increment_by returns the same position as increment.
  147. const int kSetSize = 2341;
  148. btree_set<int32_t> my_set;
  149. for (int i = 0; i < kSetSize; ++i) {
  150. my_set.insert(i);
  151. }
  152. {
  153. // Simple increment vs. increment by.
  154. btree_set<int32_t>::iterator a = my_set.begin();
  155. btree_set<int32_t>::iterator b = my_set.begin();
  156. a.increment();
  157. b.increment_by(1);
  158. EXPECT_EQ(*a, *b);
  159. }
  160. btree_set<int32_t>::iterator a = my_set.begin();
  161. for (int i = 1; i < kSetSize; ++i) {
  162. ++a;
  163. // increment_by
  164. btree_set<int32_t>::iterator b = my_set.begin();
  165. b.increment_by(i);
  166. EXPECT_EQ(*a, *b) << ": i=" << i;
  167. }
  168. }
  169. TEST(Btree, Comparison) {
  170. const int kSetSize = 1201;
  171. btree_set<int64_t> my_set;
  172. for (int i = 0; i < kSetSize; ++i) {
  173. my_set.insert(i);
  174. }
  175. btree_set<int64_t> my_set_copy(my_set);
  176. EXPECT_TRUE(my_set_copy == my_set);
  177. EXPECT_TRUE(my_set == my_set_copy);
  178. EXPECT_FALSE(my_set_copy != my_set);
  179. EXPECT_FALSE(my_set != my_set_copy);
  180. my_set.insert(kSetSize);
  181. EXPECT_FALSE(my_set_copy == my_set);
  182. EXPECT_FALSE(my_set == my_set_copy);
  183. EXPECT_TRUE(my_set_copy != my_set);
  184. EXPECT_TRUE(my_set != my_set_copy);
  185. my_set.erase(kSetSize - 1);
  186. EXPECT_FALSE(my_set_copy == my_set);
  187. EXPECT_FALSE(my_set == my_set_copy);
  188. EXPECT_TRUE(my_set_copy != my_set);
  189. EXPECT_TRUE(my_set != my_set_copy);
  190. btree_map<std::string, int64_t> my_map;
  191. for (int i = 0; i < kSetSize; ++i) {
  192. my_map[std::string(i, 'a')] = i;
  193. }
  194. btree_map<std::string, int64_t> my_map_copy(my_map);
  195. EXPECT_TRUE(my_map_copy == my_map);
  196. EXPECT_TRUE(my_map == my_map_copy);
  197. EXPECT_FALSE(my_map_copy != my_map);
  198. EXPECT_FALSE(my_map != my_map_copy);
  199. ++my_map_copy[std::string(7, 'a')];
  200. EXPECT_FALSE(my_map_copy == my_map);
  201. EXPECT_FALSE(my_map == my_map_copy);
  202. EXPECT_TRUE(my_map_copy != my_map);
  203. EXPECT_TRUE(my_map != my_map_copy);
  204. my_map_copy = my_map;
  205. my_map["hello"] = kSetSize;
  206. EXPECT_FALSE(my_map_copy == my_map);
  207. EXPECT_FALSE(my_map == my_map_copy);
  208. EXPECT_TRUE(my_map_copy != my_map);
  209. EXPECT_TRUE(my_map != my_map_copy);
  210. my_map.erase(std::string(kSetSize - 1, 'a'));
  211. EXPECT_FALSE(my_map_copy == my_map);
  212. EXPECT_FALSE(my_map == my_map_copy);
  213. EXPECT_TRUE(my_map_copy != my_map);
  214. EXPECT_TRUE(my_map != my_map_copy);
  215. }
  216. TEST(Btree, RangeCtorSanity) {
  217. typedef btree_set<int, std::less<int>, std::allocator<int>, 256> test_set;
  218. typedef btree_map<int, int, std::less<int>, std::allocator<int>, 256>
  219. test_map;
  220. typedef btree_multiset<int, std::less<int>, std::allocator<int>, 256>
  221. test_mset;
  222. typedef btree_multimap<int, int, std::less<int>, std::allocator<int>, 256>
  223. test_mmap;
  224. std::vector<int> ivec;
  225. ivec.push_back(1);
  226. std::map<int, int> imap;
  227. imap.insert(std::make_pair(1, 2));
  228. test_mset tmset(ivec.begin(), ivec.end());
  229. test_mmap tmmap(imap.begin(), imap.end());
  230. test_set tset(ivec.begin(), ivec.end());
  231. test_map tmap(imap.begin(), imap.end());
  232. EXPECT_EQ(1, tmset.size());
  233. EXPECT_EQ(1, tmmap.size());
  234. EXPECT_EQ(1, tset.size());
  235. EXPECT_EQ(1, tmap.size());
  236. }
  237. } // namespace
  238. } // namespace btree