OLD | NEW |
---|---|
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 |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "config.h" | 26 #include "config.h" |
27 | 27 |
28 #include "wtf/HashSet.h" | 28 #include "wtf/HashSet.h" |
29 #include "wtf/OwnPtr.h" | |
30 #include "wtf/PassOwnPtr.h" | |
31 #include "wtf/RefCounted.h" | |
29 #include <gtest/gtest.h> | 32 #include <gtest/gtest.h> |
30 | 33 |
31 namespace { | 34 namespace { |
32 | 35 |
33 template<int initialCapacity> | 36 template<int initialCapacity> |
34 struct InitialCapacityTestHashTraits : public WTF::UnsignedWithZeroKeyHashTr aits<int> { | 37 struct InitialCapacityTestHashTraits : public WTF::UnsignedWithZeroKeyHashTr aits<int> { |
35 static const int minimumTableSize = initialCapacity; | 38 static const int minimumTableSize = initialCapacity; |
36 }; | 39 }; |
37 | 40 |
38 template<unsigned size> | 41 template<unsigned size> |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 { | 73 { |
71 generateTestCapacityUpToSize<size - 1>(); | 74 generateTestCapacityUpToSize<size - 1>(); |
72 testInitialCapacity<size>(); | 75 testInitialCapacity<size>(); |
73 } | 76 } |
74 | 77 |
75 TEST(HashSetTest, InitialCapacity) | 78 TEST(HashSetTest, InitialCapacity) |
76 { | 79 { |
77 generateTestCapacityUpToSize<128>(); | 80 generateTestCapacityUpToSize<128>(); |
78 } | 81 } |
79 | 82 |
83 struct Dummy { | |
84 Dummy(bool& deleted) : deleted(deleted) { } | |
85 | |
86 ~Dummy() | |
87 { | |
88 deleted = true; | |
89 } | |
90 | |
91 bool& deleted; | |
92 }; | |
93 | |
94 TEST(HashSetTest, HashSetOwnPtr) | |
95 { | |
96 bool deleted1 = false, deleted2 = false; | |
97 | |
98 typedef HashSet<OwnPtr<Dummy> > OwnPtrSet; | |
99 HashSet<OwnPtr<Dummy> > set; | |
100 | |
101 Dummy* ptr1 = new Dummy(deleted1); | |
102 { | |
103 // AddResult in a separate scope to avoid assertion hit, | |
104 // since we modify the container further. | |
105 HashSet<OwnPtr<Dummy> >::AddResult res1 = set.add(adoptPtr(ptr1)); | |
106 EXPECT_EQ(ptr1, res1.storedValue->get()); | |
107 } | |
108 | |
109 EXPECT_FALSE(deleted1); | |
110 EXPECT_EQ(1UL, set.size()); | |
111 OwnPtrSet::iterator it1 = set.find(ptr1); | |
112 EXPECT_NE(set.end(), it1); | |
113 EXPECT_EQ(ptr1, (*it1)); | |
114 | |
115 Dummy* ptr2 = new Dummy(deleted2); | |
116 { | |
117 HashSet<OwnPtr<Dummy> >::AddResult res2 = set.add(adoptPtr(ptr2)); | |
118 EXPECT_EQ(res2.storedValue->get(), ptr2); | |
119 } | |
120 | |
121 EXPECT_FALSE(deleted2); | |
122 EXPECT_EQ(2UL, set.size()); | |
123 OwnPtrSet::iterator it2 = set.find(ptr2); | |
124 EXPECT_NE(set.end(), it2); | |
125 EXPECT_EQ(ptr2, (*it2)); | |
126 | |
127 set.remove(ptr1); | |
128 EXPECT_TRUE(deleted1); | |
129 | |
130 set.clear(); | |
Erik Corry
2014/04/28 07:53:03
It would be great if you could make the test also
Mikhail
2014/04/28 08:00:40
Will add.
| |
131 EXPECT_TRUE(deleted2); | |
132 EXPECT_TRUE(set.isEmpty()); | |
133 } | |
134 | |
135 class DummyRefCounted: public WTF::RefCounted<DummyRefCounted> { | |
136 public: | |
137 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = fa lse; } | |
138 ~DummyRefCounted() { m_isDeleted = true; } | |
139 | |
140 void ref() | |
141 { | |
142 WTF::RefCounted<DummyRefCounted>::ref(); | |
143 ++m_refInvokesCount; | |
144 } | |
145 | |
146 static int m_refInvokesCount; | |
Erik Corry
2014/04/28 07:53:03
Should be called s_refInvokesCount.
| |
147 | |
148 private: | |
149 bool& m_isDeleted; | |
150 }; | |
151 | |
152 int DummyRefCounted::m_refInvokesCount = 0; | |
153 | |
154 TEST(HashSetTest, HashSetRefPtr) | |
155 { | |
156 bool isDeleted; | |
157 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); | |
158 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount); | |
159 HashSet<RefPtr<DummyRefCounted> > set; | |
160 set.add(ptr); | |
161 // Referenced only once (to store a copy in the container). | |
162 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | |
163 | |
164 DummyRefCounted* rawPtr = ptr.get(); | |
165 | |
166 EXPECT_TRUE(set.contains(rawPtr)); | |
167 EXPECT_NE(set.end(), set.find(rawPtr)); | |
168 EXPECT_TRUE(set.contains(ptr)); | |
169 EXPECT_NE(set.end(), set.find(ptr)); | |
170 | |
171 ptr.clear(); | |
172 EXPECT_FALSE(isDeleted); | |
173 | |
174 set.remove(rawPtr); | |
175 EXPECT_TRUE(isDeleted); | |
176 EXPECT_TRUE(set.isEmpty()); | |
177 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | |
178 } | |
179 | |
180 | |
80 } // namespace | 181 } // namespace |
OLD | NEW |