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

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

Issue 1611343002: wtf reformat test Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pydent Created 4 years, 11 months 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 16 matching lines...) Expand all
27 27
28 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
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 32
33 namespace WTF { 33 namespace WTF {
34 34
35 namespace { 35 namespace {
36 36
37 template<unsigned size> void testReserveCapacity(); 37 template <unsigned size>
38 template<> void testReserveCapacity<0>() {} 38 void testReserveCapacity();
39 template<unsigned size> void testReserveCapacity() 39 template <>
40 { 40 void testReserveCapacity<0>() {}
41 HashSet<int> testSet; 41 template <unsigned size>
42 void testReserveCapacity() {
43 HashSet<int> testSet;
42 44
43 // Initial capacity is zero. 45 // Initial capacity is zero.
44 EXPECT_EQ(0UL, testSet.capacity()); 46 EXPECT_EQ(0UL, testSet.capacity());
45 47
46 testSet.reserveCapacityForSize(size); 48 testSet.reserveCapacityForSize(size);
47 const unsigned initialCapacity = testSet.capacity(); 49 const unsigned initialCapacity = testSet.capacity();
48 const unsigned minimumTableSize = HashTraits<int>::minimumTableSize; 50 const unsigned minimumTableSize = HashTraits<int>::minimumTableSize;
49 51
50 // reserveCapacityForSize should respect minimumTableSize. 52 // reserveCapacityForSize should respect minimumTableSize.
51 EXPECT_GE(initialCapacity, minimumTableSize); 53 EXPECT_GE(initialCapacity, minimumTableSize);
52 54
53 // Adding items up to size should never change the capacity. 55 // Adding items up to size should never change the capacity.
54 for (size_t i = 0; i < size; ++i) { 56 for (size_t i = 0; i < size; ++i) {
55 testSet.add(i + 1); // Avoid adding '0'. 57 testSet.add(i + 1); // Avoid adding '0'.
56 EXPECT_EQ(initialCapacity, testSet.capacity()); 58 EXPECT_EQ(initialCapacity, testSet.capacity());
57 } 59 }
58 60
59 // Adding items up to less than half the capacity should not change the capa city. 61 // Adding items up to less than half the capacity should not change the capaci ty.
60 unsigned capacityLimit = initialCapacity / 2 - 1; 62 unsigned capacityLimit = initialCapacity / 2 - 1;
61 for (size_t i = size; i < capacityLimit; ++i) { 63 for (size_t i = size; i < capacityLimit; ++i) {
62 testSet.add(i + 1); 64 testSet.add(i + 1);
63 EXPECT_EQ(initialCapacity, testSet.capacity()); 65 EXPECT_EQ(initialCapacity, testSet.capacity());
64 } 66 }
65 67
66 // Adding one more item increases the capacity. 68 // Adding one more item increases the capacity.
67 testSet.add(capacityLimit + 1); 69 testSet.add(capacityLimit + 1);
68 EXPECT_GT(testSet.capacity(), initialCapacity); 70 EXPECT_GT(testSet.capacity(), initialCapacity);
69 71
70 testReserveCapacity<size-1>(); 72 testReserveCapacity<size - 1>();
71 } 73 }
72 74
73 TEST(HashSetTest, ReserveCapacity) 75 TEST(HashSetTest, ReserveCapacity) {
74 { 76 testReserveCapacity<128>();
75 testReserveCapacity<128>();
76 } 77 }
77 78
78 struct Dummy { 79 struct Dummy {
79 Dummy(bool& deleted) : deleted(deleted) { } 80 Dummy(bool& deleted) : deleted(deleted) {}
80 81
81 ~Dummy() 82 ~Dummy() { deleted = true; }
82 {
83 deleted = true;
84 }
85 83
86 bool& deleted; 84 bool& deleted;
87 }; 85 };
88 86
89 TEST(HashSetTest, HashSetOwnPtr) 87 TEST(HashSetTest, HashSetOwnPtr) {
90 { 88 bool deleted1 = false, deleted2 = false;
91 bool deleted1 = false, deleted2 = false;
92 89
93 typedef HashSet<OwnPtr<Dummy>> OwnPtrSet; 90 typedef HashSet<OwnPtr<Dummy>> OwnPtrSet;
91 OwnPtrSet set;
92
93 Dummy* ptr1 = new Dummy(deleted1);
94 {
95 // AddResult in a separate scope to avoid assertion hit,
96 // since we modify the container further.
97 HashSet<OwnPtr<Dummy>>::AddResult res1 = set.add(adoptPtr(ptr1));
98 EXPECT_EQ(ptr1, res1.storedValue->get());
99 }
100
101 EXPECT_FALSE(deleted1);
102 EXPECT_EQ(1UL, set.size());
103 OwnPtrSet::iterator it1 = set.find(ptr1);
104 EXPECT_NE(set.end(), it1);
105 EXPECT_EQ(ptr1, (*it1));
106
107 Dummy* ptr2 = new Dummy(deleted2);
108 {
109 HashSet<OwnPtr<Dummy>>::AddResult res2 = set.add(adoptPtr(ptr2));
110 EXPECT_EQ(res2.storedValue->get(), ptr2);
111 }
112
113 EXPECT_FALSE(deleted2);
114 EXPECT_EQ(2UL, set.size());
115 OwnPtrSet::iterator it2 = set.find(ptr2);
116 EXPECT_NE(set.end(), it2);
117 EXPECT_EQ(ptr2, (*it2));
118
119 set.remove(ptr1);
120 EXPECT_TRUE(deleted1);
121
122 set.clear();
123 EXPECT_TRUE(deleted2);
124 EXPECT_TRUE(set.isEmpty());
125
126 deleted1 = false;
127 deleted2 = false;
128 {
94 OwnPtrSet set; 129 OwnPtrSet set;
130 set.add(adoptPtr(new Dummy(deleted1)));
131 set.add(adoptPtr(new Dummy(deleted2)));
132 }
133 EXPECT_TRUE(deleted1);
134 EXPECT_TRUE(deleted2);
95 135
96 Dummy* ptr1 = new Dummy(deleted1); 136 deleted1 = false;
97 { 137 deleted2 = false;
98 // AddResult in a separate scope to avoid assertion hit, 138 OwnPtr<Dummy> ownPtr1;
99 // since we modify the container further. 139 OwnPtr<Dummy> ownPtr2;
100 HashSet<OwnPtr<Dummy>>::AddResult res1 = set.add(adoptPtr(ptr1)); 140 ptr1 = new Dummy(deleted1);
101 EXPECT_EQ(ptr1, res1.storedValue->get()); 141 ptr2 = new Dummy(deleted2);
102 } 142 {
143 OwnPtrSet set;
144 set.add(adoptPtr(ptr1));
145 set.add(adoptPtr(ptr2));
146 ownPtr1 = set.take(ptr1);
147 EXPECT_EQ(1UL, set.size());
148 ownPtr2 = set.takeAny();
149 EXPECT_TRUE(set.isEmpty());
150 }
151 EXPECT_FALSE(deleted1);
152 EXPECT_FALSE(deleted2);
103 153
104 EXPECT_FALSE(deleted1); 154 EXPECT_EQ(ptr1, ownPtr1);
105 EXPECT_EQ(1UL, set.size()); 155 EXPECT_EQ(ptr2, ownPtr2);
106 OwnPtrSet::iterator it1 = set.find(ptr1);
107 EXPECT_NE(set.end(), it1);
108 EXPECT_EQ(ptr1, (*it1));
109
110 Dummy* ptr2 = new Dummy(deleted2);
111 {
112 HashSet<OwnPtr<Dummy>>::AddResult res2 = set.add(adoptPtr(ptr2));
113 EXPECT_EQ(res2.storedValue->get(), ptr2);
114 }
115
116 EXPECT_FALSE(deleted2);
117 EXPECT_EQ(2UL, set.size());
118 OwnPtrSet::iterator it2 = set.find(ptr2);
119 EXPECT_NE(set.end(), it2);
120 EXPECT_EQ(ptr2, (*it2));
121
122 set.remove(ptr1);
123 EXPECT_TRUE(deleted1);
124
125 set.clear();
126 EXPECT_TRUE(deleted2);
127 EXPECT_TRUE(set.isEmpty());
128
129 deleted1 = false;
130 deleted2 = false;
131 {
132 OwnPtrSet set;
133 set.add(adoptPtr(new Dummy(deleted1)));
134 set.add(adoptPtr(new Dummy(deleted2)));
135 }
136 EXPECT_TRUE(deleted1);
137 EXPECT_TRUE(deleted2);
138
139 deleted1 = false;
140 deleted2 = false;
141 OwnPtr<Dummy> ownPtr1;
142 OwnPtr<Dummy> ownPtr2;
143 ptr1 = new Dummy(deleted1);
144 ptr2 = new Dummy(deleted2);
145 {
146 OwnPtrSet set;
147 set.add(adoptPtr(ptr1));
148 set.add(adoptPtr(ptr2));
149 ownPtr1 = set.take(ptr1);
150 EXPECT_EQ(1UL, set.size());
151 ownPtr2 = set.takeAny();
152 EXPECT_TRUE(set.isEmpty());
153 }
154 EXPECT_FALSE(deleted1);
155 EXPECT_FALSE(deleted2);
156
157 EXPECT_EQ(ptr1, ownPtr1);
158 EXPECT_EQ(ptr2, ownPtr2);
159 } 156 }
160 157
161 class DummyRefCounted : public RefCounted<DummyRefCounted> { 158 class DummyRefCounted : public RefCounted<DummyRefCounted> {
162 public: 159 public:
163 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = fa lse; } 160 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) {
164 ~DummyRefCounted() { m_isDeleted = true; } 161 m_isDeleted = false;
162 }
163 ~DummyRefCounted() { m_isDeleted = true; }
165 164
166 void ref() 165 void ref() {
167 { 166 WTF::RefCounted<DummyRefCounted>::ref();
168 WTF::RefCounted<DummyRefCounted>::ref(); 167 ++s_refInvokesCount;
169 ++s_refInvokesCount; 168 }
170 }
171 169
172 static int s_refInvokesCount; 170 static int s_refInvokesCount;
173 171
174 private: 172 private:
175 bool& m_isDeleted; 173 bool& m_isDeleted;
176 }; 174 };
177 175
178 int DummyRefCounted::s_refInvokesCount = 0; 176 int DummyRefCounted::s_refInvokesCount = 0;
179 177
180 TEST(HashSetTest, HashSetRefPtr) 178 TEST(HashSetTest, HashSetRefPtr) {
181 { 179 bool isDeleted = false;
182 bool isDeleted = false; 180 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted));
183 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); 181 EXPECT_EQ(0, DummyRefCounted::s_refInvokesCount);
184 EXPECT_EQ(0, DummyRefCounted::s_refInvokesCount); 182 HashSet<RefPtr<DummyRefCounted>> set;
185 HashSet<RefPtr<DummyRefCounted>> set; 183 set.add(ptr);
186 set.add(ptr); 184 // Referenced only once (to store a copy in the container).
187 // Referenced only once (to store a copy in the container). 185 EXPECT_EQ(1, DummyRefCounted::s_refInvokesCount);
188 EXPECT_EQ(1, DummyRefCounted::s_refInvokesCount);
189 186
190 DummyRefCounted* rawPtr = ptr.get(); 187 DummyRefCounted* rawPtr = ptr.get();
191 188
192 EXPECT_TRUE(set.contains(rawPtr)); 189 EXPECT_TRUE(set.contains(rawPtr));
193 EXPECT_NE(set.end(), set.find(rawPtr)); 190 EXPECT_NE(set.end(), set.find(rawPtr));
194 EXPECT_TRUE(set.contains(ptr)); 191 EXPECT_TRUE(set.contains(ptr));
195 EXPECT_NE(set.end(), set.find(ptr)); 192 EXPECT_NE(set.end(), set.find(ptr));
196 193
197 ptr.clear(); 194 ptr.clear();
198 EXPECT_FALSE(isDeleted); 195 EXPECT_FALSE(isDeleted);
199 196
200 set.remove(rawPtr); 197 set.remove(rawPtr);
201 EXPECT_TRUE(isDeleted); 198 EXPECT_TRUE(isDeleted);
202 EXPECT_TRUE(set.isEmpty()); 199 EXPECT_TRUE(set.isEmpty());
203 EXPECT_EQ(1, DummyRefCounted::s_refInvokesCount); 200 EXPECT_EQ(1, DummyRefCounted::s_refInvokesCount);
204 } 201 }
205 202
206 } // anonymous namespace 203 } // anonymous namespace
207 204
208 } // namespace WTF 205 } // 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