[libcxx-commits] [libcxx] b84da5b - [libc++] [test] Add tests for converting array types in shared_ptr.

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 19 10:03:56 PDT 2021


Author: Konstantin Varlamov
Date: 2021-10-19T13:03:51-04:00
New Revision: b84da5ba6e31e9e264370ac2b47536a7b50ef4fd

URL: https://github.com/llvm/llvm-project/commit/b84da5ba6e31e9e264370ac2b47536a7b50ef4fd
DIFF: https://github.com/llvm/llvm-project/commit/b84da5ba6e31e9e264370ac2b47536a7b50ef4fd.diff

LOG: [libc++] [test] Add tests for converting array types in shared_ptr.

The only possible kind of a conversion in initialization of a shared
pointer to an array is a qualification conversion (i.e., adding
cv-qualifiers). This patch adds tests for converting from `A[]` to
`const A[]` to the following functions:

```
template<class Y> explicit shared_ptr(Y* p);

template<class Y> shared_ptr(const shared_ptr<Y>& r);
template<class Y> shared_ptr(shared_ptr<Y>&& r);

template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r);
template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);

template<class Y> void reset(Y* p);
template<class Y, class D> void reset(Y* p, D d);
template<class Y, class D, class A> void reset(Y* p, D d, A a);
```

Similar tests for converting functions that involve a `weak_ptr` should
be added once LWG issue [3001](https://cplusplus.github.io/LWG/issue3001)
is implemented.

Differential Revision: https://reviews.llvm.org/D112048

Added: 
    

Modified: 
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y_rv.pass.cpp
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer.pass.cpp
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y.pass.cpp
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y_rv.pass.cpp
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer.pass.cpp
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter_allocator.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp
index 7e23e62bff19a..9b7e43ff65af4 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp
@@ -120,5 +120,25 @@ int main(int, char**)
     assert(B::count == 0);
     assert(A::count == 0);
 
+#if TEST_STD_VER > 14
+    {
+        std::shared_ptr<A[]> p1(new A[8]);
+        A* ptr = p1.get();
+        assert(A::count == 8);
+        {
+            std::shared_ptr<const A[]> p2;
+            p2 = p1;
+            assert(A::count == 8);
+            assert(p2.use_count() == 2);
+            assert(p1.use_count() == 2);
+            assert(p1.get() == p2.get());
+            assert(p2.get() == ptr);
+        }
+        assert(p1.use_count() == 1);
+        assert(A::count == 8);
+    }
+    assert(A::count == 0);
+#endif
+
   return 0;
 }

diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y_rv.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y_rv.pass.cpp
index b86cfd4cf0b44..2a0a523592487 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y_rv.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y_rv.pass.cpp
@@ -16,6 +16,7 @@
 
 #include <memory>
 #include <type_traits>
+#include <utility>
 #include <cassert>
 
 #include "test_macros.h"
@@ -122,5 +123,25 @@ int main(int, char**)
     assert(B::count == 0);
     assert(A::count == 0);
 
+#if TEST_STD_VER > 14
+    {
+        std::shared_ptr<A[]> p1(new A[8]);
+        A* ptr = p1.get();
+        assert(A::count == 8);
+        {
+            std::shared_ptr<const A[]> p2;
+            p2 = std::move(p1);
+            assert(A::count == 8);
+            assert(p2.use_count() == 1);
+            assert(p1.use_count() == 0);
+            assert(p1.get() == nullptr);
+            assert(p2.get() == ptr);
+        }
+        assert(p1.use_count() == 0);
+        assert(A::count == 0);
+    }
+    assert(A::count == 0);
+#endif
+
   return 0;
 }

diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer.pass.cpp
index b055543336d56..c88bdd1c6e561 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer.pass.cpp
@@ -59,6 +59,13 @@ int main(int, char**)
       assert(A::count == 8);
     }
     assert(A::count == 0);
+
+    {
+      std::shared_ptr<const A[]> pA(new A[8]);
+      assert(pA.use_count() == 1);
+      assert(A::count == 8);
+    }
+    assert(A::count == 0);
 #endif
 
     return 0;

diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y.pass.cpp
index 9af199d376f67..42f215f9faa7d 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y.pass.cpp
@@ -133,4 +133,22 @@ int main(int, char**)
                                                              private_delete_arr_op*>::value, "");
     }
 #endif
+
+#if TEST_STD_VER > 14
+    {
+        std::shared_ptr<A[]> p1(new A[8]);
+        assert(p1.use_count() == 1);
+        assert(A::count == 8);
+        {
+            std::shared_ptr<const A[]> p2(p1);
+            assert(A::count == 8);
+            assert(p2.use_count() == 2);
+            assert(p1.use_count() == 2);
+            assert(p1.get() == p2.get());
+        }
+        assert(p1.use_count() == 1);
+        assert(A::count == 8);
+    }
+    assert(A::count == 0);
+#endif
 }

diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y_rv.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y_rv.pass.cpp
index 9e47293b37169..167baf87825db 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y_rv.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y_rv.pass.cpp
@@ -16,6 +16,7 @@
 
 #include <memory>
 #include <type_traits>
+#include <utility>
 #include <cassert>
 
 #include "test_macros.h"
@@ -96,7 +97,7 @@ int main(int, char**)
         assert(B::count == 0);
         assert(A::count == 0);
         {
-            std::shared_ptr<B> pB(pA);
+            std::shared_ptr<B> pB(std::move(pA));
             assert(B::count == 0);
             assert(A::count == 0);
             assert(pB.use_count() == 0);
@@ -110,5 +111,23 @@ int main(int, char**)
     assert(B::count == 0);
     assert(A::count == 0);
 
+#if TEST_STD_VER > 14
+    {
+        std::shared_ptr<A[]> p1;
+        assert(p1.use_count() == 0);
+        assert(A::count == 0);
+        {
+            std::shared_ptr<const A[]> p2(p1);
+            assert(A::count == 0);
+            assert(p2.use_count() == 0);
+            assert(p1.use_count() == 0);
+            assert(p1.get() == p2.get());
+        }
+        assert(p1.use_count() == 0);
+        assert(A::count == 0);
+    }
+    assert(A::count == 0);
+#endif
+
   return 0;
 }

diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer.pass.cpp
index d2e6c9f0de2e5..d764e8a312d7a 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer.pass.cpp
@@ -63,5 +63,17 @@ int main(int, char**)
     }
     assert(A::count == 0);
 
+#if TEST_STD_VER > 14
+    {
+        std::shared_ptr<const A[]> p;
+        A* ptr = new A[8];
+        p.reset(ptr);
+        assert(A::count == 8);
+        assert(p.use_count() == 1);
+        assert(p.get() == ptr);
+    }
+    assert(A::count == 0);
+#endif
+
   return 0;
 }

diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp
index f4944db727d0c..14c336b4f59ca 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp
@@ -81,5 +81,17 @@ int main(int, char**)
     assert(test_deleter<A>::count == 0);
     assert(test_deleter<A>::dealloc_count == 2);
 
+#if TEST_STD_VER > 14
+    {
+        std::shared_ptr<const A[]> p;
+        A* ptr = new A[8];
+        p.reset(ptr, CDeleter<A[]>());
+        assert(A::count == 8);
+        assert(p.use_count() == 1);
+        assert(p.get() == ptr);
+    }
+    assert(A::count == 0);
+#endif
+
   return 0;
 }

diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter_allocator.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter_allocator.pass.cpp
index 481284016835d..17afcca06e013 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter_allocator.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter_allocator.pass.cpp
@@ -90,5 +90,17 @@ int main(int, char**)
     assert(test_allocator<A>::count == 0);
     assert(test_allocator<A>::alloc_count == 0);
 
+#if TEST_STD_VER > 14
+    {
+        std::shared_ptr<const A[]> p;
+        A* ptr = new A[8];
+        p.reset(ptr, CDeleter<A[]>(), test_allocator<A[]>());
+        assert(A::count == 8);
+        assert(p.use_count() == 1);
+        assert(p.get() == ptr);
+    }
+    assert(A::count == 0);
+#endif
+
   return 0;
 }


        


More information about the libcxx-commits mailing list