2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
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,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
23 * THE POSSIBILITY OF SUCH DAMAGE.
28 #include "wtf/HashSet.h"
29 #include "wtf/OwnPtr.h"
30 #include "wtf/PassOwnPtr.h"
31 #include "wtf/RefCounted.h"
32 #include <gtest/gtest.h>
36 template<int initialCapacity>
37 struct InitialCapacityTestHashTraits : public WTF::UnsignedWithZeroKeyHashTraits<int> {
38 static const int minimumTableSize = initialCapacity;
41 template<unsigned size>
42 void testInitialCapacity()
44 const unsigned initialCapacity = WTF::HashTableCapacityForSize<size>::value;
45 HashSet<int, DefaultHash<int>::Hash, InitialCapacityTestHashTraits<initialCapacity> > testSet;
47 // Initial capacity is null.
48 EXPECT_EQ(0UL, testSet.capacity());
50 // Adding items up to size should never change the capacity.
51 for (size_t i = 0; i < size; ++i) {
53 EXPECT_EQ(initialCapacity, testSet.capacity());
56 // Adding items up to less than half the capacity should not change the capacity.
57 unsigned capacityLimit = initialCapacity / 2 - 1;
58 for (size_t i = size; i < capacityLimit; ++i) {
60 EXPECT_EQ(initialCapacity, testSet.capacity());
63 // Adding one more item increase the capacity.
64 testSet.add(initialCapacity);
65 EXPECT_GT(testSet.capacity(), initialCapacity);
68 template<unsigned size> void generateTestCapacityUpToSize();
69 template<> void generateTestCapacityUpToSize<0>()
72 template<unsigned size> void generateTestCapacityUpToSize()
74 generateTestCapacityUpToSize<size - 1>();
75 testInitialCapacity<size>();
78 TEST(HashSetTest, InitialCapacity)
80 generateTestCapacityUpToSize<128>();
84 Dummy(bool& deleted) : deleted(deleted) { }
94 TEST(HashSetTest, HashSetOwnPtr)
96 bool deleted1 = false, deleted2 = false;
98 typedef HashSet<OwnPtr<Dummy> > OwnPtrSet;
101 Dummy* ptr1 = new Dummy(deleted1);
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());
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));
115 Dummy* ptr2 = new Dummy(deleted2);
117 HashSet<OwnPtr<Dummy> >::AddResult res2 = set.add(adoptPtr(ptr2));
118 EXPECT_EQ(res2.storedValue->get(), ptr2);
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));
128 EXPECT_TRUE(deleted1);
131 EXPECT_TRUE(deleted2);
132 EXPECT_TRUE(set.isEmpty());
138 set.add(adoptPtr(new Dummy(deleted1)));
139 set.add(adoptPtr(new Dummy(deleted2)));
141 EXPECT_TRUE(deleted1);
142 EXPECT_TRUE(deleted2);
145 class DummyRefCounted: public WTF::RefCounted<DummyRefCounted> {
147 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = false; }
148 ~DummyRefCounted() { m_isDeleted = true; }
152 WTF::RefCounted<DummyRefCounted>::ref();
156 static int s_refInvokesCount;
162 int DummyRefCounted::s_refInvokesCount = 0;
164 TEST(HashSetTest, HashSetRefPtr)
167 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted));
168 EXPECT_EQ(0, DummyRefCounted::s_refInvokesCount);
169 HashSet<RefPtr<DummyRefCounted> > set;
171 // Referenced only once (to store a copy in the container).
172 EXPECT_EQ(1, DummyRefCounted::s_refInvokesCount);
174 DummyRefCounted* rawPtr = ptr.get();
176 EXPECT_TRUE(set.contains(rawPtr));
177 EXPECT_NE(set.end(), set.find(rawPtr));
178 EXPECT_TRUE(set.contains(ptr));
179 EXPECT_NE(set.end(), set.find(ptr));
182 EXPECT_FALSE(isDeleted);
185 EXPECT_TRUE(isDeleted);
186 EXPECT_TRUE(set.isEmpty());
187 EXPECT_EQ(1, DummyRefCounted::s_refInvokesCount);