Title: [266506] trunk/Tools
Revision
266506
Author
[email protected]
Date
2020-09-03 00:48:56 -0700 (Thu, 03 Sep 2020)

Log Message

Static assert the return type of compactMap
https://bugs.webkit.org/show_bug.cgi?id=216119

Reviewed by Darin Adler.

Added static_assert for the return types of compactMap in varoius unit tests.

(WTF_Vector.CompactMapStaticFunctionReturnOptional):
(WTF_Vector.CompactMapStaticFunctionReturnRefPtr):
(WTF_Vector.CompactMapStaticFunctionReturnOptionalRef):
(WTF_Vector.CompactMapStaticFunctionReturnOptionalRefPtr):
(WTF_Vector.CompactMapLambdaReturnOptional):
(WTF_Vector.CompactMapLambdaCopyVectorReturnOptionalCountedObject):
(WTF_Vector.CompactMapLambdaMoveVectorReturnOptionalCountedObject):
(WTF_Vector.CompactMapLambdaReturnRefPtr):
(WTF_Vector.CompactMapLambdaReturnRefPtrFromMovedRef):
(WTF_Vector.CompactMapLambdaReturnOptionalRefPtr):

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (266505 => 266506)


--- trunk/Tools/ChangeLog	2020-09-03 05:51:40 UTC (rev 266505)
+++ trunk/Tools/ChangeLog	2020-09-03 07:48:56 UTC (rev 266506)
@@ -1,3 +1,23 @@
+2020-09-03  Ryosuke Niwa  <[email protected]>
+
+        Static assert the return type of compactMap
+        https://bugs.webkit.org/show_bug.cgi?id=216119
+
+        Reviewed by Darin Adler.
+
+        Added static_assert for the return types of compactMap in varoius unit tests.
+
+        (WTF_Vector.CompactMapStaticFunctionReturnOptional):
+        (WTF_Vector.CompactMapStaticFunctionReturnRefPtr):
+        (WTF_Vector.CompactMapStaticFunctionReturnOptionalRef):
+        (WTF_Vector.CompactMapStaticFunctionReturnOptionalRefPtr):
+        (WTF_Vector.CompactMapLambdaReturnOptional):
+        (WTF_Vector.CompactMapLambdaCopyVectorReturnOptionalCountedObject):
+        (WTF_Vector.CompactMapLambdaMoveVectorReturnOptionalCountedObject):
+        (WTF_Vector.CompactMapLambdaReturnRefPtr):
+        (WTF_Vector.CompactMapLambdaReturnRefPtrFromMovedRef):
+        (WTF_Vector.CompactMapLambdaReturnOptionalRefPtr):
+
 2020-09-02  Dean Jackson  <[email protected]>
 
         Update Kimmo's email.

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp (266505 => 266506)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2020-09-03 05:51:40 UTC (rev 266505)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2020-09-03 07:48:56 UTC (rev 266506)
@@ -882,6 +882,8 @@
 {
     Vector<int> vector { 1, 2, 3, 4 };
 
+    static_assert(std::is_same<decltype(WTF::compactMap(vector, evenMultipliedByFive)), typename WTF::Vector<int>>::value,
+        "WTF::compactMap returns Vector<Ref<>> when the mapped function returns Optional<>");
     auto mapped = WTF::compactMap(vector, evenMultipliedByFive);
 
     EXPECT_EQ(2U, mapped.size());
@@ -923,7 +925,9 @@
     Vector<int> vector { 1, 2, 3, 4 };
 
     RefCountedObject::s_totalRefCount = 0;
-    Vector<Ref<RefCountedObject>> mapped = WTF::compactMap(vector, createRefCountedForOdd);
+    static_assert(std::is_same<decltype(WTF::compactMap(vector, createRefCountedForOdd)), typename WTF::Vector<Ref<RefCountedObject>>>::value,
+        "WTF::compactMap returns Vector<int> when the mapped function returns Optional<int>");
+    auto mapped = WTF::compactMap(vector, createRefCountedForOdd);
 
     EXPECT_EQ(0U, RefCountedObject::s_totalRefCount);
     EXPECT_EQ(2U, mapped.size());
@@ -943,6 +947,8 @@
     Vector<int> vector { 1, 2, 3, 4 };
 
     RefCountedObject::s_totalRefCount = 0;
+    static_assert(std::is_same<decltype(WTF::compactMap(vector, createRefCountedForEven)), typename WTF::Vector<Ref<RefCountedObject>>>::value,
+        "WTF::compactMap returns Vector<Ref<>> when the mapped function returns RefPtr<>");
     auto mapped = WTF::compactMap(vector, createRefCountedForEven);
 
     EXPECT_EQ(0U, RefCountedObject::s_totalRefCount);
@@ -965,7 +971,9 @@
     Vector<int> vector { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
     RefCountedObject::s_totalRefCount = 0;
-    Vector<RefPtr<RefCountedObject>> mapped = WTF::compactMap(vector, createRefCountedWhenDivisibleByThree);
+    static_assert(std::is_same<decltype(WTF::compactMap(vector, createRefCountedWhenDivisibleByThree)), typename WTF::Vector<RefPtr<RefCountedObject>>>::value,
+        "WTF::compactMap returns Vector<RefPtr<>> when the mapped function returns Optional<RefPtr<>>");
+    auto mapped = WTF::compactMap(vector, createRefCountedWhenDivisibleByThree);
 
     EXPECT_EQ(0U, RefCountedObject::s_totalRefCount);
     EXPECT_EQ(3U, mapped.size());
@@ -978,11 +986,14 @@
 {
     Vector<String> vector { "a", "b", "hello", "ciao", "world", "webkit" };
 
-    auto mapped = WTF::compactMap(vector, [](const String& value) -> Optional<String> {
+    auto function = [](const String& value) -> Optional<String> {
         if (value.length() < 5)
             return WTF::nullopt;
         return value.convertToASCIIUppercase();
-    });
+    };
+    static_assert(std::is_same<decltype(WTF::compactMap(vector, function)), typename WTF::Vector<String>>::value,
+        "WTF::compactMap returns Vector<String> when the mapped function returns Optional<String>");
+    auto mapped = WTF::compactMap(vector, function);
 
     EXPECT_EQ(3U, mapped.size());
     EXPECT_STREQ("HELLO", mapped[0].ascii().data());
@@ -1022,12 +1033,16 @@
 
     CountedObject::count() = 0;
 
-    auto mapped = WTF::compactMap(vector, [](const CountedObject& object) -> Optional<CountedObject> {
+    auto function = [](const CountedObject& object) -> Optional<CountedObject> {
         if (object.value() % 2)
             return object;
         return WTF::nullopt;
-    });
+    };
 
+    static_assert(std::is_same<decltype(WTF::compactMap(vector, function)), typename WTF::Vector<CountedObject>>::value,
+        "WTF::compactMap returns Vector<CountedObject> when the lambda returns Optional<CountedObject>");
+    auto mapped = WTF::compactMap(vector, function);
+
     EXPECT_EQ(2U, CountedObject::count());
 
     EXPECT_EQ(2U, mapped.size());
@@ -1041,13 +1056,17 @@
 
     CountedObject::count() = 0;
 
-    RefCountedObject::s_totalRefCount = 0;
-    auto mapped = WTF::compactMap(WTFMove(vector), [](CountedObject&& object) -> Optional<CountedObject> {
+    auto function = [](CountedObject&& object) -> Optional<CountedObject> {
         if (object.value() % 2)
             return WTFMove(object);
         return WTF::nullopt;
-    });
+    };
 
+    RefCountedObject::s_totalRefCount = 0;
+    static_assert(std::is_same<decltype(WTF::compactMap(WTFMove(vector), function)), typename WTF::Vector<CountedObject>>::value,
+        "WTF::compactMap returns Vector<CountedObject> when the lambda returns Optional<CountedObject>");
+    auto mapped = WTF::compactMap(WTFMove(vector), function);
+
     EXPECT_EQ(0U, CountedObject::count());
 
     EXPECT_EQ(2U, mapped.size());
@@ -1059,13 +1078,17 @@
 {
     Vector<int> vector { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
-    RefCountedObject::s_totalRefCount = 0;
-    Vector<Ref<RefCountedObject>> mapped = WTF::compactMap(vector, [](int value) -> RefPtr<RefCountedObject> {
+    auto function = [](int value) -> RefPtr<RefCountedObject> {
         if (value % 3)
             return nullptr;
         return RefCountedObject::create(value);
-    });
+    };
 
+    RefCountedObject::s_totalRefCount = 0;
+    static_assert(std::is_same<decltype(WTF::compactMap(vector, function)), typename WTF::Vector<Ref<RefCountedObject>>>::value,
+        "WTF::compactMap returns Vector<Ref<RefCountedObject>> when the lambda returns RefPtr<RefCountedObject>");
+    auto mapped = WTF::compactMap(vector, function);
+
     EXPECT_EQ(0U, RefCountedObject::s_totalRefCount);
     EXPECT_EQ(3U, mapped.size());
     EXPECT_EQ(3, mapped[0]->value);
@@ -1081,13 +1104,17 @@
         return RefCountedObject::create(value);
     });
 
-    RefCountedObject::s_totalRefCount = 0;
-    auto mapped = WTF::compactMap(WTFMove(countedObjects), [](Ref<RefCountedObject>&& object) -> RefPtr<RefCountedObject> {
+    auto function = [](Ref<RefCountedObject>&& object) -> RefPtr<RefCountedObject> {
         if (object->value % 3)
             return nullptr;
         return WTFMove(object);
-    });
+    };
 
+    RefCountedObject::s_totalRefCount = 0;
+    static_assert(std::is_same<decltype(WTF::compactMap(WTFMove(countedObjects), function)), typename WTF::Vector<Ref<RefCountedObject>>>::value,
+        "WTF::compactMap returns Vector<Ref<RefCountedObject>> when the lambda returns RefPtr<RefCountedObject>");
+    auto mapped = WTF::compactMap(WTFMove(countedObjects), function);
+
     EXPECT_EQ(0U, RefCountedObject::s_totalRefCount);
     EXPECT_EQ(3U, mapped.size());
     EXPECT_EQ(3, mapped[0]->value);
@@ -1101,14 +1128,18 @@
 
     RefCountedObject::s_totalRefCount = 0;
 
-    Vector<RefPtr<RefCountedObject>> mapped = WTF::compactMap(vector, [&](int value) -> Optional<RefPtr<RefCountedObject>> {
+    auto function = [&](int value) -> Optional<RefPtr<RefCountedObject>> {
         if (!(value % 2))
             return WTF::nullopt;
         if (!(value % 3))
             return RefPtr<RefCountedObject>();
         return RefPtr<RefCountedObject>(RefCountedObject::create(value));
-    });
+    };
 
+    static_assert(std::is_same<decltype(WTF::compactMap(vector, function)), typename WTF::Vector<RefPtr<RefCountedObject>>>::value,
+        "WTF::compactMap returns Vector<RefPtr<RefCountedObject>> when the lambda returns Optional<RefPtr<RefCountedObject>>");
+    Vector<RefPtr<RefCountedObject>> mapped = WTF::compactMap(vector, function);
+
     EXPECT_EQ(0U, RefCountedObject::s_totalRefCount);
     EXPECT_EQ(5U, mapped.size());
     EXPECT_EQ(1, mapped[0]->value);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to