[libcxx-commits] [libcxx] 8f2cc88 - [libcxx] [NFC] Add more reinterpret_pointer_cast tests.

via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 14 12:04:27 PDT 2020


Author: zoecarver
Date: 2020-05-14T12:04:05-07:00
New Revision: 8f2cc889b0517c4ad748de1a96f13489e89968a5

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

LOG: [libcxx] [NFC] Add more reinterpret_pointer_cast tests.

 * Add test for inheritance.
 * Test value is preserved through cast.

Added: 
    

Modified: 
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/reinterpret_pointer_cast.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/reinterpret_pointer_cast.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/reinterpret_pointer_cast.pass.cpp
index 221001f91c17..743d60c191c3 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/reinterpret_pointer_cast.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/reinterpret_pointer_cast.pass.cpp
@@ -23,6 +23,9 @@ struct A {
   int x;
 };
 
+struct Base { };
+struct Derived : public Base { };
+
 int main(int, char**) {
   {
     const std::shared_ptr<A> pA(new A);
@@ -38,6 +41,26 @@ int main(int, char**) {
     assert(pA2.get() == pA.get());
     assert(!pi.owner_before(pA) && !pA.owner_before(pi));
   }
+  {
+    const std::shared_ptr<A> pA(new A);
+    std::shared_ptr<int> pi = std::reinterpret_pointer_cast<int>(pA);
+    pA->x = 42;
+    assert(*pi == 42);
+  }
+  {
+    const std::shared_ptr<Derived> pDerived(new Derived);
+    std::shared_ptr<Base> pBase = std::reinterpret_pointer_cast<Base>(pDerived);
+    std::shared_ptr<Derived> pDerived2 = std::reinterpret_pointer_cast<Derived>(pBase);
+    assert(pDerived2.get() == pDerived2.get());
+    assert(!pBase.owner_before(pDerived) && !pDerived.owner_before(pBase));
+  }
+  {
+    const std::shared_ptr<Base> pBase(new Base);
+    std::shared_ptr<Derived> pDerived = std::reinterpret_pointer_cast<Derived>(pBase);
+    std::shared_ptr<Base> pBase2 = std::reinterpret_pointer_cast<Base>(pDerived);
+    assert(pBase2.get() == pBase.get());
+    assert(!pDerived.owner_before(pBase) && !pBase.owner_before(pDerived));
+  }
 #if TEST_STD_VER > 14
   {
     const std::shared_ptr<A[8]> pA;


        


More information about the libcxx-commits mailing list