Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / indexed_db / list_set_unittest.cc
blob658414c950d2a2241679fdaba3f9a8d8e78e27ea
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "content/browser/indexed_db/list_set.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace content {
12 TEST(ListSetTest, ListSetIterator) {
13 list_set<int> set;
14 for (int i = 3; i > 0; --i)
15 set.insert(i);
17 list_set<int>::iterator it = set.begin();
18 EXPECT_EQ(3, *it);
19 ++it;
20 EXPECT_EQ(2, *it);
21 it++;
22 EXPECT_EQ(1, *it);
23 --it;
24 EXPECT_EQ(2, *it);
25 it--;
26 EXPECT_EQ(3, *it);
27 ++it;
28 EXPECT_EQ(2, *it);
29 it++;
30 EXPECT_EQ(1, *it);
31 ++it;
32 EXPECT_EQ(set.end(), it);
35 TEST(ListSetTest, ListSetConstIterator) {
36 list_set<int> set;
37 for (int i = 5; i > 0; --i)
38 set.insert(i);
40 const list_set<int>& ref = set;
42 list_set<int>::const_iterator it = ref.begin();
43 for (int i = 5; i > 0; --i) {
44 EXPECT_EQ(i, *it);
45 ++it;
47 EXPECT_EQ(ref.end(), it);
50 TEST(ListSetTest, ListSetPrimitive) {
51 list_set<int> set;
52 EXPECT_TRUE(set.empty());
53 EXPECT_EQ(0u, set.size());
55 list_set<int>::iterator it = set.begin();
56 EXPECT_EQ(set.end(), it);
59 for (int i = 5; i > 0; --i)
60 set.insert(i);
61 EXPECT_EQ(5u, set.size());
62 EXPECT_FALSE(set.empty());
64 set.erase(3);
65 EXPECT_EQ(4u, set.size());
67 EXPECT_EQ(1u, set.count(2));
68 set.erase(2);
69 EXPECT_EQ(0u, set.count(2));
70 EXPECT_EQ(3u, set.size());
73 list_set<int>::iterator it = set.begin();
74 EXPECT_EQ(5, *it);
75 ++it;
76 EXPECT_EQ(4, *it);
77 ++it;
78 EXPECT_EQ(1, *it);
79 ++it;
80 EXPECT_EQ(set.end(), it);
83 set.erase(1);
84 set.erase(4);
85 set.erase(5);
87 EXPECT_EQ(0u, set.size());
88 EXPECT_TRUE(set.empty());
90 list_set<int>::iterator it = set.begin();
91 EXPECT_EQ(set.end(), it);
95 template <typename T>
96 class Wrapped {
97 public:
98 explicit Wrapped(const T& value) : value_(value) {}
99 explicit Wrapped(const Wrapped<T>& other) : value_(other.value_) {}
100 Wrapped& operator=(const Wrapped<T>& rhs) {
101 value_ = rhs.value_;
102 return *this;
104 int value() const { return value_; }
105 bool operator<(const Wrapped<T>& rhs) const { return value_ < rhs.value_; }
106 bool operator==(const Wrapped<T>& rhs) const { return value_ == rhs.value_; }
108 private:
109 T value_;
112 TEST(ListSetTest, ListSetObject) {
113 list_set<Wrapped<int> > set;
114 EXPECT_EQ(0u, set.size());
116 list_set<Wrapped<int> >::iterator it = set.begin();
117 EXPECT_EQ(set.end(), it);
120 set.insert(Wrapped<int>(0));
121 set.insert(Wrapped<int>(1));
122 set.insert(Wrapped<int>(2));
124 EXPECT_EQ(3u, set.size());
127 list_set<Wrapped<int> >::iterator it = set.begin();
128 EXPECT_EQ(0, it->value());
129 ++it;
130 EXPECT_EQ(1, it->value());
131 ++it;
132 EXPECT_EQ(2, it->value());
133 ++it;
134 EXPECT_EQ(set.end(), it);
137 set.erase(Wrapped<int>(0));
138 set.erase(Wrapped<int>(1));
139 set.erase(Wrapped<int>(2));
141 EXPECT_EQ(0u, set.size());
143 list_set<Wrapped<int> >::iterator it = set.begin();
144 EXPECT_EQ(set.end(), it);
148 TEST(ListSetTest, ListSetPointer) {
149 scoped_ptr<Wrapped<int> > w0(new Wrapped<int>(0));
150 scoped_ptr<Wrapped<int> > w1(new Wrapped<int>(1));
151 scoped_ptr<Wrapped<int> > w2(new Wrapped<int>(2));
153 list_set<Wrapped<int>*> set;
154 EXPECT_EQ(0u, set.size());
156 list_set<Wrapped<int>*>::iterator it = set.begin();
157 EXPECT_EQ(set.end(), it);
160 set.insert(w0.get());
161 set.insert(w1.get());
162 set.insert(w2.get());
164 EXPECT_EQ(3u, set.size());
167 list_set<Wrapped<int>*>::iterator it = set.begin();
168 EXPECT_EQ(0, (*it)->value());
169 ++it;
170 EXPECT_EQ(1, (*it)->value());
171 ++it;
172 EXPECT_EQ(2, (*it)->value());
173 ++it;
174 EXPECT_EQ(set.end(), it);
177 set.erase(w0.get());
178 set.erase(w1.get());
179 set.erase(w2.get());
181 EXPECT_EQ(0u, set.size());
183 list_set<Wrapped<int>*>::iterator it = set.begin();
184 EXPECT_EQ(set.end(), it);
188 template <typename T>
189 class RefCounted : public base::RefCounted<RefCounted<T> > {
190 public:
191 explicit RefCounted(const T& value) : value_(value) {}
192 T value() { return value_; }
194 private:
195 virtual ~RefCounted() {}
196 friend class base::RefCounted<RefCounted<T> >;
197 T value_;
200 TEST(ListSetTest, ListSetRefCounted) {
201 list_set<scoped_refptr<RefCounted<int> > > set;
202 EXPECT_EQ(0u, set.size());
204 list_set<scoped_refptr<RefCounted<int> > >::iterator it = set.begin();
205 EXPECT_EQ(set.end(), it);
208 scoped_refptr<RefCounted<int> > r0(new RefCounted<int>(0));
209 scoped_refptr<RefCounted<int> > r1(new RefCounted<int>(1));
210 scoped_refptr<RefCounted<int> > r2(new RefCounted<int>(2));
212 set.insert(r0);
213 set.insert(r1);
214 set.insert(r2);
216 EXPECT_EQ(3u, set.size());
219 list_set<scoped_refptr<RefCounted<int> > >::iterator it = set.begin();
220 EXPECT_EQ(0, (*it)->value());
221 ++it;
222 EXPECT_EQ(1, (*it)->value());
223 ++it;
224 EXPECT_EQ(2, (*it)->value());
225 ++it;
226 EXPECT_EQ(set.end(), it);
229 set.erase(r0);
230 set.erase(r1);
231 set.erase(r2);
233 EXPECT_EQ(0u, set.size());
235 list_set<scoped_refptr<RefCounted<int> > >::iterator it = set.begin();
236 EXPECT_EQ(set.end(), it);
240 } // namespace content