Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: third_party/WebKit/Source/wtf/HashSetTest.cpp

Issue 1436153002: Apply clang-format with Chromium-style without column limit. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/HashSet.h ('k') | third_party/WebKit/Source/wtf/HashTable.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Apple Inc. All rights reserved. 2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 17 matching lines...) Expand all
28 28
29 #include "wtf/OwnPtr.h" 29 #include "wtf/OwnPtr.h"
30 #include "wtf/PassOwnPtr.h" 30 #include "wtf/PassOwnPtr.h"
31 #include "wtf/RefCounted.h" 31 #include "wtf/RefCounted.h"
32 #include <gtest/gtest.h> 32 #include <gtest/gtest.h>
33 33
34 namespace WTF { 34 namespace WTF {
35 35
36 namespace { 36 namespace {
37 37
38 template<unsigned size> void testReserveCapacity(); 38 template <unsigned size>
39 template<> void testReserveCapacity<0>() {} 39 void testReserveCapacity();
40 template<unsigned size> void testReserveCapacity() 40 template <>
41 { 41 void testReserveCapacity<0>() {}
42 HashSet<int> testSet; 42 template <unsigned size>
43 void testReserveCapacity() {
44 HashSet<int> testSet;
43 45
44 // Initial capacity is zero. 46 // Initial capacity is zero.
45 EXPECT_EQ(0UL, testSet.capacity()); 47 EXPECT_EQ(0UL, testSet.capacity());
46 48
47 testSet.reserveCapacityForSize(size); 49 testSet.reserveCapacityForSize(size);
48 const unsigned initialCapacity = testSet.capacity(); 50 const unsigned initialCapacity = testSet.capacity();
49 const unsigned minimumTableSize = HashTraits<int>::minimumTableSize; 51 const unsigned minimumTableSize = HashTraits<int>::minimumTableSize;
50 52
51 // reserveCapacityForSize should respect minimumTableSize. 53 // reserveCapacityForSize should respect minimumTableSize.
52 EXPECT_GE(initialCapacity, minimumTableSize); 54 EXPECT_GE(initialCapacity, minimumTableSize);
53 55
54 // Adding items up to size should never change the capacity. 56 // Adding items up to size should never change the capacity.
55 for (size_t i = 0; i < size; ++i) { 57 for (size_t i = 0; i < size; ++i) {
56 testSet.add(i + 1); // Avoid adding '0'. 58 testSet.add(i + 1); // Avoid adding '0'.
57 EXPECT_EQ(initialCapacity, testSet.capacity()); 59 EXPECT_EQ(initialCapacity, testSet.capacity());
58 } 60 }
59 61
60 // Adding items up to less than half the capacity should not change the capa city. 62 // Adding items up to less than half the capacity should not change the capaci ty.
61 unsigned capacityLimit = initialCapacity / 2 - 1; 63 unsigned capacityLimit = initialCapacity / 2 - 1;
62 for (size_t i = size; i < capacityLimit; ++i) { 64 for (size_t i = size; i < capacityLimit; ++i) {
63 testSet.add(i + 1); 65 testSet.add(i + 1);
64 EXPECT_EQ(initialCapacity, testSet.capacity()); 66 EXPECT_EQ(initialCapacity, testSet.capacity());
65 } 67 }
66 68
67 // Adding one more item increases the capacity. 69 // Adding one more item increases the capacity.
68 testSet.add(capacityLimit + 1); 70 testSet.add(capacityLimit + 1);
69 EXPECT_GT(testSet.capacity(), initialCapacity); 71 EXPECT_GT(testSet.capacity(), initialCapacity);
70 72
71 testReserveCapacity<size-1>(); 73 testReserveCapacity<size - 1>();
72 } 74 }
73 75
74 TEST(HashSetTest, ReserveCapacity) 76 TEST(HashSetTest, ReserveCapacity) {
75 { 77 testReserveCapacity<128>();
76 testReserveCapacity<128>();
77 } 78 }
78 79
79 struct Dummy { 80 struct Dummy {
80 Dummy(bool& deleted) : deleted(deleted) { } 81 Dummy(bool& deleted)
82 : deleted(deleted) {}
81 83
82 ~Dummy() 84 ~Dummy() {
83 { 85 deleted = true;
84 deleted = true; 86 }
85 }
86 87
87 bool& deleted; 88 bool& deleted;
88 }; 89 };
89 90
90 TEST(HashSetTest, HashSetOwnPtr) 91 TEST(HashSetTest, HashSetOwnPtr) {
91 { 92 bool deleted1 = false, deleted2 = false;
92 bool deleted1 = false, deleted2 = false;
93 93
94 typedef HashSet<OwnPtr<Dummy>> OwnPtrSet; 94 typedef HashSet<OwnPtr<Dummy>> OwnPtrSet;
95 OwnPtrSet set;
96
97 Dummy* ptr1 = new Dummy(deleted1);
98 {
99 // AddResult in a separate scope to avoid assertion hit,
100 // since we modify the container further.
101 HashSet<OwnPtr<Dummy>>::AddResult res1 = set.add(adoptPtr(ptr1));
102 EXPECT_EQ(ptr1, res1.storedValue->get());
103 }
104
105 EXPECT_FALSE(deleted1);
106 EXPECT_EQ(1UL, set.size());
107 OwnPtrSet::iterator it1 = set.find(ptr1);
108 EXPECT_NE(set.end(), it1);
109 EXPECT_EQ(ptr1, (*it1));
110
111 Dummy* ptr2 = new Dummy(deleted2);
112 {
113 HashSet<OwnPtr<Dummy>>::AddResult res2 = set.add(adoptPtr(ptr2));
114 EXPECT_EQ(res2.storedValue->get(), ptr2);
115 }
116
117 EXPECT_FALSE(deleted2);
118 EXPECT_EQ(2UL, set.size());
119 OwnPtrSet::iterator it2 = set.find(ptr2);
120 EXPECT_NE(set.end(), it2);
121 EXPECT_EQ(ptr2, (*it2));
122
123 set.remove(ptr1);
124 EXPECT_TRUE(deleted1);
125
126 set.clear();
127 EXPECT_TRUE(deleted2);
128 EXPECT_TRUE(set.isEmpty());
129
130 deleted1 = false;
131 deleted2 = false;
132 {
95 OwnPtrSet set; 133 OwnPtrSet set;
134 set.add(adoptPtr(new Dummy(deleted1)));
135 set.add(adoptPtr(new Dummy(deleted2)));
136 }
137 EXPECT_TRUE(deleted1);
138 EXPECT_TRUE(deleted2);
96 139
97 Dummy* ptr1 = new Dummy(deleted1); 140 deleted1 = false;
98 { 141 deleted2 = false;
99 // AddResult in a separate scope to avoid assertion hit, 142 OwnPtr<Dummy> ownPtr1;
100 // since we modify the container further. 143 OwnPtr<Dummy> ownPtr2;
101 HashSet<OwnPtr<Dummy>>::AddResult res1 = set.add(adoptPtr(ptr1)); 144 ptr1 = new Dummy(deleted1);
102 EXPECT_EQ(ptr1, res1.storedValue->get()); 145 ptr2 = new Dummy(deleted2);
103 } 146 {
147 OwnPtrSet set;
148 set.add(adoptPtr(ptr1));
149 set.add(adoptPtr(ptr2));
150 ownPtr1 = set.take(ptr1);
151 EXPECT_EQ(1UL, set.size());
152 ownPtr2 = set.takeAny();
153 EXPECT_TRUE(set.isEmpty());
154 }
155 EXPECT_FALSE(deleted1);
156 EXPECT_FALSE(deleted2);
104 157
105 EXPECT_FALSE(deleted1); 158 EXPECT_EQ(ptr1, ownPtr1);
106 EXPECT_EQ(1UL, set.size()); 159 EXPECT_EQ(ptr2, ownPtr2);
107 OwnPtrSet::iterator it1 = set.find(ptr1);
108 EXPECT_NE(set.end(), it1);
109 EXPECT_EQ(ptr1, (*it1));
110
111 Dummy* ptr2 = new Dummy(deleted2);
112 {
113 HashSet<OwnPtr<Dummy>>::AddResult res2 = set.add(adoptPtr(ptr2));
114 EXPECT_EQ(res2.storedValue->get(), ptr2);
115 }
116
117 EXPECT_FALSE(deleted2);
118 EXPECT_EQ(2UL, set.size());
119 OwnPtrSet::iterator it2 = set.find(ptr2);
120 EXPECT_NE(set.end(), it2);
121 EXPECT_EQ(ptr2, (*it2));
122
123 set.remove(ptr1);
124 EXPECT_TRUE(deleted1);
125
126 set.clear();
127 EXPECT_TRUE(deleted2);
128 EXPECT_TRUE(set.isEmpty());
129
130 deleted1 = false;
131 deleted2 = false;
132 {
133 OwnPtrSet set;
134 set.add(adoptPtr(new Dummy(deleted1)));
135 set.add(adoptPtr(new Dummy(deleted2)));
136 }
137 EXPECT_TRUE(deleted1);
138 EXPECT_TRUE(deleted2);
139
140 deleted1 = false;
141 deleted2 = false;
142 OwnPtr<Dummy> ownPtr1;
143 OwnPtr<Dummy> ownPtr2;
144 ptr1 = new Dummy(deleted1);
145 ptr2 = new Dummy(deleted2);
146 {
147 OwnPtrSet set;
148 set.add(adoptPtr(ptr1));
149 set.add(adoptPtr(ptr2));
150 ownPtr1 = set.take(ptr1);
151 EXPECT_EQ(1UL, set.size());
152 ownPtr2 = set.takeAny();
153 EXPECT_TRUE(set.isEmpty());
154 }
155 EXPECT_FALSE(deleted1);
156 EXPECT_FALSE(deleted2);
157
158 EXPECT_EQ(ptr1, ownPtr1);
159 EXPECT_EQ(ptr2, ownPtr2);
160 } 160 }
161 161
162 class DummyRefCounted : public RefCounted<DummyRefCounted> { 162 class DummyRefCounted : public RefCounted<DummyRefCounted> {
163 public: 163 public:
164 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = fa lse; } 164 DummyRefCounted(bool& isDeleted)
165 ~DummyRefCounted() { m_isDeleted = true; } 165 : m_isDeleted(isDeleted) { m_isDeleted = false; }
166 ~DummyRefCounted() { m_isDeleted = true; }
166 167
167 void ref() 168 void ref() {
168 { 169 WTF::RefCounted<DummyRefCounted>::ref();
169 WTF::RefCounted<DummyRefCounted>::ref(); 170 ++s_refInvokesCount;
170 ++s_refInvokesCount; 171 }
171 }
172 172
173 static int s_refInvokesCount; 173 static int s_refInvokesCount;
174 174
175 private: 175 private:
176 bool& m_isDeleted; 176 bool& m_isDeleted;
177 }; 177 };
178 178
179 int DummyRefCounted::s_refInvokesCount = 0; 179 int DummyRefCounted::s_refInvokesCount = 0;
180 180
181 TEST(HashSetTest, HashSetRefPtr) 181 TEST(HashSetTest, HashSetRefPtr) {
182 { 182 bool isDeleted = false;
183 bool isDeleted = false; 183 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted));
184 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); 184 EXPECT_EQ(0, DummyRefCounted::s_refInvokesCount);
185 EXPECT_EQ(0, DummyRefCounted::s_refInvokesCount); 185 HashSet<RefPtr<DummyRefCounted>> set;
186 HashSet<RefPtr<DummyRefCounted>> set; 186 set.add(ptr);
187 set.add(ptr); 187 // Referenced only once (to store a copy in the container).
188 // Referenced only once (to store a copy in the container). 188 EXPECT_EQ(1, DummyRefCounted::s_refInvokesCount);
189 EXPECT_EQ(1, DummyRefCounted::s_refInvokesCount);
190 189
191 DummyRefCounted* rawPtr = ptr.get(); 190 DummyRefCounted* rawPtr = ptr.get();
192 191
193 EXPECT_TRUE(set.contains(rawPtr)); 192 EXPECT_TRUE(set.contains(rawPtr));
194 EXPECT_NE(set.end(), set.find(rawPtr)); 193 EXPECT_NE(set.end(), set.find(rawPtr));
195 EXPECT_TRUE(set.contains(ptr)); 194 EXPECT_TRUE(set.contains(ptr));
196 EXPECT_NE(set.end(), set.find(ptr)); 195 EXPECT_NE(set.end(), set.find(ptr));
197 196
198 ptr.clear(); 197 ptr.clear();
199 EXPECT_FALSE(isDeleted); 198 EXPECT_FALSE(isDeleted);
200 199
201 set.remove(rawPtr); 200 set.remove(rawPtr);
202 EXPECT_TRUE(isDeleted); 201 EXPECT_TRUE(isDeleted);
203 EXPECT_TRUE(set.isEmpty()); 202 EXPECT_TRUE(set.isEmpty());
204 EXPECT_EQ(1, DummyRefCounted::s_refInvokesCount); 203 EXPECT_EQ(1, DummyRefCounted::s_refInvokesCount);
205 } 204 }
206 205
207 } // anonymous namespace 206 } // anonymous namespace
208 207
209 } // namespace WTF 208 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/HashSet.h ('k') | third_party/WebKit/Source/wtf/HashTable.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698