Added tests for WeakHashSet::computesEmpty and WeakHashSet::computeSize
authorrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 7 Apr 2019 03:55:59 +0000 (03:55 +0000)
committerrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 7 Apr 2019 03:55:59 +0000 (03:55 +0000)
https://bugs.webkit.org/show_bug.cgi?id=196669

Reviewed by Geoffrey Garen.

Source/WTF:

Removed the superflous type names from forward declarations, and made WeakHashSet::add
take a const object to match other container types in WTF.

* wtf/WeakHashSet.h:
(WTF::WeakHashSet::add):
* wtf/WeakPtr.h:

Tools:

Added three new unit tests for WeakHashSet.

* TestWebKitAPI/Tests/WTF/WeakPtr.cpp:
(WTF_WeakPtr.WeakHashSetConstObjects):
(WTF_WeakPtr.WeakHashSetComputesEmpty):
(WTF_WeakPtr.WeakHashSetComputeSize):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@243965 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/wtf/WeakHashSet.h
Source/WTF/wtf/WeakPtr.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp

index e625b64..02d0b75 100644 (file)
@@ -1,3 +1,17 @@
+2019-04-06  Ryosuke Niwa  <rniwa@webkit.org>
+
+        Added tests for WeakHashSet::computesEmpty and WeakHashSet::computeSize
+        https://bugs.webkit.org/show_bug.cgi?id=196669
+
+        Reviewed by Geoffrey Garen.
+
+        Removed the superflous type names from forward declarations, and made WeakHashSet::add
+        take a const object to match other container types in WTF.
+
+        * wtf/WeakHashSet.h:
+        (WTF::WeakHashSet::add):
+        * wtf/WeakPtr.h:
+
 2019-04-05  Ryosuke Niwa  <rniwa@webkit.org>
 
         Make WeakPtr<Element> possible and deploy it in form associated elements code
index c390232..8c8987b 100644 (file)
@@ -74,7 +74,7 @@ public:
         }
 
     private:
-        template <typename U> friend class WeakHashSet;
+        template <typename> friend class WeakHashSet;
 
         typename WeakReferenceSet::const_iterator m_position;
         typename WeakReferenceSet::const_iterator m_endPosition;
@@ -86,9 +86,10 @@ public:
     const_iterator begin() const { return WeakHashSetConstIterator(m_set, m_set.begin()); }
     const_iterator end() const { return WeakHashSetConstIterator(m_set, m_set.end()); }
 
-    void add(T& value)
+    template <typename U>
+    void add(const U& value)
     {
-        m_set.add(*makeWeakPtr(value).m_ref);
+        m_set.add(*makeWeakPtr<T>(const_cast<U&>(value)).m_ref);
     }
 
     template <typename U>
index 65c6e95..2715d1f 100644 (file)
@@ -33,9 +33,9 @@
 
 namespace WTF {
 
-template<typename U> class WeakHashSet;
-template<typename T> class WeakPtr;
-template<typename T> class WeakPtrFactory;
+template<typename> class WeakHashSet;
+template<typename> class WeakPtr;
+template<typename> class WeakPtrFactory;
 
 // Note: WeakReference is an implementation detail, and should not be used directly.
 template<typename T>
@@ -86,8 +86,8 @@ public:
     void clear() { m_ref = nullptr; }
 
 private:
-    template<typename U> friend class WeakHashSet;
-    template<typename U> friend class WeakPtr;
+    template<typename> friend class WeakHashSet;
+    template<typename> friend class WeakPtr;
     template<typename U> friend WeakPtr<U> makeWeakPtr(U&);
 
     RefPtr<WeakReference<T>> m_ref;
@@ -131,7 +131,7 @@ public:
     }
 
 private:
-    template<typename U> friend class WeakHashSet;
+    template<typename> friend class WeakHashSet;
 
     mutable RefPtr<WeakReference<T>> m_ref;
 };
index 3687d18..46cabe7 100644 (file)
@@ -1,3 +1,17 @@
+2019-04-06  Ryosuke Niwa  <rniwa@webkit.org>
+
+        Added tests for WeakHashSet::computesEmpty and WeakHashSet::computeSize
+        https://bugs.webkit.org/show_bug.cgi?id=196669
+
+        Reviewed by Geoffrey Garen.
+
+        Added three new unit tests for WeakHashSet.
+
+        * TestWebKitAPI/Tests/WTF/WeakPtr.cpp:
+        (WTF_WeakPtr.WeakHashSetConstObjects):
+        (WTF_WeakPtr.WeakHashSetComputesEmpty):
+        (WTF_WeakPtr.WeakHashSetComputeSize):
+
 2019-04-06  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         Hide next and previous form control buttons when WKWebView is editable
index 635309f..e0e7c31 100644 (file)
@@ -492,6 +492,72 @@ TEST(WTF_WeakPtr, WeakHashSetBasic)
     EXPECT_EQ(s_baseWeakReferences, 0u);
 }
 
+TEST(WTF_WeakPtr, WeakHashSetConstObjects)
+{
+    {
+        WeakHashSet<Base> weakHashSet;
+        const Base object;
+        EXPECT_FALSE(weakHashSet.contains(object));
+        EXPECT_EQ(s_baseWeakReferences, 0u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 0u);
+        weakHashSet.add(object);
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 1u);
+        EXPECT_TRUE(weakHashSet.contains(object));
+        weakHashSet.checkConsistency();
+        weakHashSet.add(object);
+        EXPECT_TRUE(weakHashSet.contains(object));
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 1u);
+        weakHashSet.checkConsistency();
+        weakHashSet.remove(object);
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 0u);
+    }
+
+    {
+        WeakHashSet<Base> weakHashSet;
+        const Derived object;
+        EXPECT_FALSE(weakHashSet.contains(object));
+        EXPECT_EQ(s_baseWeakReferences, 0u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 0u);
+        weakHashSet.add(object);
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 1u);
+        EXPECT_TRUE(weakHashSet.contains(object));
+        weakHashSet.checkConsistency();
+        weakHashSet.add(object);
+        EXPECT_TRUE(weakHashSet.contains(object));
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 1u);
+        weakHashSet.checkConsistency();
+        weakHashSet.remove(object);
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 0u);
+    }
+
+    {
+        WeakHashSet<Derived> weakHashSet;
+        const Derived object;
+        EXPECT_FALSE(weakHashSet.contains(object));
+        EXPECT_EQ(s_baseWeakReferences, 0u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 0u);
+        weakHashSet.add(object);
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 1u);
+        EXPECT_TRUE(weakHashSet.contains(object));
+        weakHashSet.checkConsistency();
+        weakHashSet.add(object);
+        EXPECT_TRUE(weakHashSet.contains(object));
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 1u);
+        weakHashSet.checkConsistency();
+        weakHashSet.remove(object);
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), 0u);
+    }
+}
+
 TEST(WTF_WeakPtr, WeakHashSetExpansion)
 {
     unsigned initialCapacity;
@@ -569,4 +635,154 @@ TEST(WTF_WeakPtr, WeakHashSetExpansion)
     }
 }
 
+TEST(WTF_WeakPtr, WeakHashSetComputesEmpty)
+{
+    {
+        WeakHashSet<Base> weakHashSet;
+        {
+            Base object;
+            EXPECT_EQ(s_baseWeakReferences, 0u);
+            weakHashSet.add(object);
+            EXPECT_FALSE(weakHashSet.computesEmpty());
+        }
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_TRUE(weakHashSet.computesEmpty());
+    }
+
+    {
+        WeakHashSet<Base> weakHashSet;
+        Base object1;
+        EXPECT_EQ(s_baseWeakReferences, 0u);
+        weakHashSet.add(object1);
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        {
+            Base object2;
+            weakHashSet.add(object2);
+            EXPECT_FALSE(weakHashSet.computesEmpty());
+        }
+        EXPECT_EQ(s_baseWeakReferences, 2u);
+        EXPECT_FALSE(weakHashSet.computesEmpty());
+        weakHashSet.remove(object1);
+        EXPECT_TRUE(weakHashSet.computesEmpty());
+    }
+
+    {
+        WeakHashSet<Base> weakHashSet;
+        Vector<std::unique_ptr<Base>> objects;
+        auto firstObject = std::make_unique<Base>();
+        weakHashSet.add(*firstObject);
+        do {
+            auto object = std::make_unique<Base>();
+            weakHashSet.add(*object);
+            objects.append(WTFMove(object));
+        } while (weakHashSet.begin().get() == firstObject.get());
+
+        EXPECT_EQ(s_baseWeakReferences, objects.size() + 1);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), objects.size() + 1);
+        EXPECT_FALSE(weakHashSet.computesEmpty());
+        firstObject = nullptr;
+        EXPECT_FALSE(weakHashSet.computesEmpty());
+        EXPECT_EQ(s_baseWeakReferences, objects.size() + 1);
+        EXPECT_EQ(computeSizeOfWeakHashSet(weakHashSet), objects.size());
+    }
+}
+
+TEST(WTF_WeakPtr, WeakHashSetComputeSize)
+{
+    {
+        WeakHashSet<Base> weakHashSet;
+        {
+            Base object;
+            EXPECT_EQ(s_baseWeakReferences, 0u);
+            weakHashSet.add(object);
+            EXPECT_EQ(s_baseWeakReferences, 1u);
+            EXPECT_EQ(weakHashSet.computeSize(), 1u);
+            weakHashSet.checkConsistency();
+        }
+        EXPECT_TRUE(weakHashSet.computesEmpty());
+        EXPECT_EQ(weakHashSet.computeSize(), 0u);
+        EXPECT_EQ(s_baseWeakReferences, 0u);
+        EXPECT_TRUE(weakHashSet.computesEmpty());
+        weakHashSet.checkConsistency();
+    }
+
+    {
+        WeakHashSet<Base> weakHashSet;
+        {
+            Base object1;
+            EXPECT_EQ(s_baseWeakReferences, 0u);
+            weakHashSet.add(object1);
+            EXPECT_EQ(s_baseWeakReferences, 1u);
+            {
+                Base object2;
+                weakHashSet.add(object2);
+                EXPECT_EQ(s_baseWeakReferences, 2u);
+                EXPECT_EQ(weakHashSet.computeSize(), 2u);
+                weakHashSet.checkConsistency();
+            }
+            EXPECT_EQ(s_baseWeakReferences, 2u);
+            EXPECT_EQ(weakHashSet.computeSize(), 1u);
+            EXPECT_EQ(s_baseWeakReferences, 1u);
+            weakHashSet.checkConsistency();
+            weakHashSet.remove(object1);
+            EXPECT_EQ(s_baseWeakReferences, 1u);
+            EXPECT_EQ(weakHashSet.computeSize(), 0u);
+            EXPECT_EQ(s_baseWeakReferences, 1u);
+            weakHashSet.checkConsistency();
+        }
+        EXPECT_EQ(s_baseWeakReferences, 0u);
+        weakHashSet.checkConsistency();
+    }
+
+    while (1) {
+        WeakHashSet<Base> weakHashSet;
+        auto firstObject = std::make_unique<Base>();
+        auto lastObject = std::make_unique<Base>();
+        weakHashSet.add(*firstObject);
+        weakHashSet.add(*lastObject);
+        if (weakHashSet.begin().get() != firstObject.get())
+            continue;
+        EXPECT_EQ(s_baseWeakReferences, 2u);
+        EXPECT_EQ(weakHashSet.computeSize(), 2u);
+        EXPECT_EQ(s_baseWeakReferences, 2u);
+        weakHashSet.checkConsistency();
+        firstObject = nullptr;
+        EXPECT_EQ(weakHashSet.computeSize(), 1u);
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        weakHashSet.checkConsistency();
+        lastObject = nullptr;
+        EXPECT_EQ(s_baseWeakReferences, 1u);
+        EXPECT_EQ(weakHashSet.computeSize(), 0u);
+        EXPECT_EQ(s_baseWeakReferences, 0u);
+        weakHashSet.checkConsistency();
+        break;
+    }
+
+    {
+        WeakHashSet<Base> weakHashSet;
+        Vector<std::unique_ptr<Base>> objects;
+        auto nonFirstObject = std::make_unique<Base>();
+        weakHashSet.add(*nonFirstObject);
+        do {
+            auto object = std::make_unique<Base>();
+            weakHashSet.add(*object);
+            objects.append(WTFMove(object));
+        } while (weakHashSet.begin().get() == nonFirstObject.get());
+
+        unsigned objectsCount = objects.size();
+        EXPECT_EQ(s_baseWeakReferences, objectsCount + 1);
+        EXPECT_EQ(weakHashSet.computeSize(), objectsCount + 1);
+        EXPECT_EQ(s_baseWeakReferences, objectsCount + 1);
+        weakHashSet.checkConsistency();
+        nonFirstObject = nullptr;
+        EXPECT_EQ(weakHashSet.computeSize(), objectsCount);
+        EXPECT_EQ(s_baseWeakReferences, objectsCount);
+        weakHashSet.checkConsistency();
+        objects.clear();
+        EXPECT_EQ(s_baseWeakReferences, objectsCount);
+        EXPECT_EQ(weakHashSet.computeSize(), 0u);
+        EXPECT_EQ(s_baseWeakReferences, 0u);
+    }
+}
+
 } // namespace TestWebKitAPI