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"
12 TEST(ListSetTest
, ListSetIterator
) {
14 for (int i
= 3; i
> 0; --i
)
17 list_set
<int>::iterator it
= set
.begin();
32 EXPECT_EQ(set
.end(), it
);
35 TEST(ListSetTest
, ListSetConstIterator
) {
37 for (int i
= 5; i
> 0; --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
) {
47 EXPECT_EQ(ref
.end(), it
);
50 TEST(ListSetTest
, ListSetPrimitive
) {
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
)
61 EXPECT_EQ(5u, set
.size());
62 EXPECT_FALSE(set
.empty());
65 EXPECT_EQ(4u, set
.size());
67 EXPECT_EQ(1u, set
.count(2));
69 EXPECT_EQ(0u, set
.count(2));
70 EXPECT_EQ(3u, set
.size());
73 list_set
<int>::iterator it
= set
.begin();
80 EXPECT_EQ(set
.end(), it
);
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
);
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
) {
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_
; }
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());
130 EXPECT_EQ(1, it
->value());
132 EXPECT_EQ(2, it
->value());
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());
170 EXPECT_EQ(1, (*it
)->value());
172 EXPECT_EQ(2, (*it
)->value());
174 EXPECT_EQ(set
.end(), it
);
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
> > {
191 explicit RefCounted(const T
& value
) : value_(value
) {}
192 T
value() { return value_
; }
195 virtual ~RefCounted() {}
196 friend class base::RefCounted
<RefCounted
<T
> >;
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));
216 EXPECT_EQ(3u, set
.size());
219 list_set
<scoped_refptr
<RefCounted
<int> > >::iterator it
= set
.begin();
220 EXPECT_EQ(0, (*it
)->value());
222 EXPECT_EQ(1, (*it
)->value());
224 EXPECT_EQ(2, (*it
)->value());
226 EXPECT_EQ(set
.end(), it
);
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