summaryrefslogtreecommitdiff
path: root/xdelta3/cpp-btree/safe_btree_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'xdelta3/cpp-btree/safe_btree_test.cc')
-rw-r--r--xdelta3/cpp-btree/safe_btree_test.cc116
1 files changed, 116 insertions, 0 deletions
diff --git a/xdelta3/cpp-btree/safe_btree_test.cc b/xdelta3/cpp-btree/safe_btree_test.cc
new file mode 100644
index 0000000..0d77ae0
--- /dev/null
+++ b/xdelta3/cpp-btree/safe_btree_test.cc
@@ -0,0 +1,116 @@
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
15// TODO(pmattis): Add some tests that iterators are not invalidated by
16// insertion and deletion.
17
18#include <functional>
19#include <map>
20#include <set>
21#include <string>
22#include <utility>
23
24#include "gtest/gtest.h"
25#include "btree_test.h"
26#include "safe_btree_map.h"
27#include "safe_btree_set.h"
28
29class UnsafeArena;
30
31namespace btree {
32namespace {
33
34template <typename K, int N>
35void SetTest() {
36 typedef TestAllocator<K> TestAlloc;
37 BtreeTest<safe_btree_set<K, std::less<K>, std::allocator<K>, N>, std::set<K> >();
38 BtreeAllocatorTest<safe_btree_set<K, std::less<K>, TestAlloc, N> >();
39}
40
41template <typename K, int N>
42void MapTest() {
43 typedef TestAllocator<K> TestAlloc;
44 BtreeTest<safe_btree_map<K, K, std::less<K>, std::allocator<K>, N>, std::map<K, K> >();
45 BtreeAllocatorTest<safe_btree_map<K, K, std::less<K>, TestAlloc, N> >();
46 BtreeMapTest<safe_btree_map<K, K, std::less<K>, std::allocator<K>, N> >();
47}
48
49TEST(SafeBtree, set_int32_32) { SetTest<int32_t, 32>(); }
50TEST(SafeBtree, set_int32_64) { SetTest<int32_t, 64>(); }
51TEST(SafeBtree, set_int32_128) { SetTest<int32_t, 128>(); }
52TEST(SafeBtree, set_int32_256) { SetTest<int32_t, 256>(); }
53TEST(SafeBtree, set_int64_256) { SetTest<int64_t, 256>(); }
54TEST(SafeBtree, set_string_256) { SetTest<std::string, 256>(); }
55TEST(SafeBtree, set_pair_256) { SetTest<std::pair<int, int>, 256>(); }
56TEST(SafeBtree, map_int32_256) { MapTest<int32_t, 256>(); }
57TEST(SafeBtree, map_int64_256) { MapTest<int64_t, 256>(); }
58TEST(SafeBtree, map_string_256) { MapTest<std::string, 256>(); }
59TEST(SafeBtree, map_pair_256) { MapTest<std::pair<int, int>, 256>(); }
60
61TEST(SafeBtree, Comparison) {
62 const int kSetSize = 1201;
63 safe_btree_set<int64_t> my_set;
64 for (int i = 0; i < kSetSize; ++i) {
65 my_set.insert(i);
66 }
67 safe_btree_set<int64_t> my_set_copy(my_set);
68 EXPECT_TRUE(my_set_copy == my_set);
69 EXPECT_TRUE(my_set == my_set_copy);
70 EXPECT_FALSE(my_set_copy != my_set);
71 EXPECT_FALSE(my_set != my_set_copy);
72
73 my_set.insert(kSetSize);
74 EXPECT_FALSE(my_set_copy == my_set);
75 EXPECT_FALSE(my_set == my_set_copy);
76 EXPECT_TRUE(my_set_copy != my_set);
77 EXPECT_TRUE(my_set != my_set_copy);
78
79 my_set.erase(kSetSize - 1);
80 EXPECT_FALSE(my_set_copy == my_set);
81 EXPECT_FALSE(my_set == my_set_copy);
82 EXPECT_TRUE(my_set_copy != my_set);
83 EXPECT_TRUE(my_set != my_set_copy);
84
85 safe_btree_map<std::string, int64_t> my_map;
86 for (int i = 0; i < kSetSize; ++i) {
87 my_map[std::string(i, 'a')] = i;
88 }
89 safe_btree_map<std::string, int64_t> my_map_copy(my_map);
90 EXPECT_TRUE(my_map_copy == my_map);
91 EXPECT_TRUE(my_map == my_map_copy);
92 EXPECT_FALSE(my_map_copy != my_map);
93 EXPECT_FALSE(my_map != my_map_copy);
94
95 ++my_map_copy[std::string(7, 'a')];
96 EXPECT_FALSE(my_map_copy == my_map);
97 EXPECT_FALSE(my_map == my_map_copy);
98 EXPECT_TRUE(my_map_copy != my_map);
99 EXPECT_TRUE(my_map != my_map_copy);
100
101 my_map_copy = my_map;
102 my_map["hello"] = kSetSize;
103 EXPECT_FALSE(my_map_copy == my_map);
104 EXPECT_FALSE(my_map == my_map_copy);
105 EXPECT_TRUE(my_map_copy != my_map);
106 EXPECT_TRUE(my_map != my_map_copy);
107
108 my_map.erase(std::string(kSetSize - 1, 'a'));
109 EXPECT_FALSE(my_map_copy == my_map);
110 EXPECT_FALSE(my_map == my_map_copy);
111 EXPECT_TRUE(my_map_copy != my_map);
112 EXPECT_TRUE(my_map != my_map_copy);
113}
114
115} // namespace
116} // namespace btree