diff --git a/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.t.deref.pass.cpp b/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.compile.pass.cpp similarity index 54% rename from libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.t.deref.pass.cpp rename to libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.compile.pass.cpp index a0f3af8f87fd74..d2eb8c3a6ac008 100644 --- a/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.t.deref.pass.cpp +++ b/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.compile.pass.cpp @@ -10,12 +10,9 @@ // -// [out.ptr.t], class template out_ptr_t -// template -// class out_ptr_t; // since c++23 - -// operator Pointer*() const noexcept; -// operator void**() const noexcept; +// [inout.ptr], function template inout_ptr +// template +// auto inout_ptr(Smart& s, Args&&... args); // since c++23 #include @@ -23,12 +20,15 @@ int main(int, char**) { { std::unique_ptr uPtr; - std::out_ptr_t, int*>{uPtr}; + auto inoutUPtr1 = std::inout_ptr(uPtr); + auto inoutUPtr2 = std::inout_ptr(uPtr); } { - std::unique_ptr> uPtr; + auto deleter = [](auto* p) { delete p; }; + std::unique_ptr uPtr; - std::out_ptr_t>{uPtr, std::default_delete{}}; + auto inoutUPtr1 = std::inout_ptr(uPtr, deleter); + auto inoutUPtr2 = std::inout_ptr(uPtr, deleter); } return 0; diff --git a/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.general.pass.cpp b/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.general.pass.cpp index 1b45d04f7942d7..58f09dc3a9ec3b 100644 --- a/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.general.pass.cpp +++ b/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.general.pass.cpp @@ -14,26 +14,188 @@ // template // auto inout_ptr(Smart& s, Args&&... args); // since c++23 +#include #include -int main(int, char**) { +#include "../types.h" + +// Test helpers. + +void replace_int_p(int** pp) { + assert(**pp == 90); + delete *pp; + *pp = new int{84}; +} + +void replace_int_p_with_nullptr(int** pp) { + assert(**pp == 90); + delete *pp; + *pp = nullptr; +} + +void replace_nullptr_with_int_p(int** pp) { + assert(*pp == nullptr); + *pp = new int{84}; +} + +void replace_int_void_p(void** pp) { + assert(*(static_cast(*pp)) == 90); + delete static_cast(*pp); + *pp = new int{84}; +} + +void replace_int_void_p_with_nullptr(void** pp) { + assert(*(static_cast(*pp)) == 90); + delete static_cast(*pp); + *pp = nullptr; +} + +void replace_nullptr_with_int_void_p(void** pp) { + assert(*pp == nullptr); + *pp = new int{84}; +} + +void replace_SomeInt_p(SomeInt** pp) { + auto si = **pp; + assert(si.value == 90); + delete static_cast(*pp); + *pp = new SomeInt{9084}; +} + +void replace_SomeInt_void_p(void** pp) { + assert(reinterpret_cast(*pp)->value == 90); + delete static_cast(*pp); + *pp = reinterpret_cast(new SomeInt{9084}); +} + +// Test `std::inout_ptr()` function. + +void test_raw_ptr() { + { + auto rPtr = new int{90}; + + replace_int_p(std::inout_ptr(rPtr)); + assert(*rPtr == 84); + + delete rPtr; + } + { + auto rPtr = new int{90}; + + replace_int_p_with_nullptr(std::inout_ptr(rPtr)); + assert(rPtr == nullptr); + } + { + int* rPtr = nullptr; + + replace_nullptr_with_int_p(std::inout_ptr(rPtr)); + assert(*rPtr == 84); + delete rPtr; + } + { + auto rPtr = new int{90}; + + replace_int_void_p(std::inout_ptr(rPtr)); + assert(*rPtr == 84); + delete rPtr; + } + { + auto rPtr = new int{90}; + + replace_int_void_p_with_nullptr(std::inout_ptr(rPtr)); + assert(rPtr == nullptr); + } + { + int* rPtr = nullptr; + + replace_nullptr_with_int_void_p(std::inout_ptr(rPtr)); + assert(*rPtr == 84); + delete rPtr; + } + { + auto* rPtr = new SomeInt{90}; + + replace_SomeInt_p(std::inout_ptr(rPtr)); + assert(rPtr->value == 9084); + delete rPtr; + } + { + auto* rPtr = new SomeInt{90}; + + replace_SomeInt_void_p(std::inout_ptr(rPtr)); + assert(rPtr->value == 9084); + delete rPtr; + } +} + +void test_unique_ptr() { + { + auto uPtr = std::make_unique(90); + + replace_int_p(std::inout_ptr(uPtr)); + assert(*uPtr == 84); + } { std::unique_ptr uPtr; - auto inoutUPtr1 = std::inout_ptr(uPtr); - (void)inoutUPtr1; - auto inoutUPtr2 = std::inout_ptr(uPtr); - (void)inoutUPtr2; + replace_nullptr_with_int_p(std::inout_ptr(uPtr)); + assert(*uPtr == 84); } { - auto deleter = [](auto* p) { delete p; }; - std::unique_ptr uPtr; + auto uPtr = std::make_unique(90); - auto inoutUPtr1 = std::inout_ptr(uPtr, deleter); - (void)inoutUPtr1; - auto inoutUPtr2 = std::inout_ptr(uPtr, deleter); - (void)inoutUPtr2; + replace_int_void_p(std::inout_ptr(uPtr)); + assert(*uPtr == 84); } + { + std::unique_ptr uPtr; + + replace_nullptr_with_int_void_p(std::inout_ptr(uPtr)); + assert(*uPtr == 84); + } + { + auto uPtr = std::make_unique(90); + + replace_SomeInt_p(std::inout_ptr(uPtr)); + assert(uPtr->value == 9084); + } + { + auto uPtr = std::make_unique(90); + + replace_SomeInt_void_p(std::inout_ptr(uPtr)); + assert(uPtr->value == 9084); + } +} + +void test_custom_ptr() { + // ConstructiblePtr + { + ConstructiblePtr cPtr(new int{90}); + + replace_int_p(std::inout_ptr(cPtr)); + assert(cPtr == 84); + } + // ResettablePtr + { + ResettablePtr rPtr(new int{90}); + + replace_int_p(std::inout_ptr(rPtr)); + assert(rPtr == 84); + } + // NonConstructiblePtr + { + NonConstructiblePtr nPtr; + nPtr.reset(new int{90}); + + replace_int_p(std::inout_ptr(nPtr)); + assert(nPtr == 84); + } +} + +int main(int, char**) { + test_raw_ptr(); + test_unique_ptr(); + test_custom_ptr(); return 0; } diff --git a/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.pass.cpp b/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.pass.cpp deleted file mode 100644 index 58f09dc3a9ec3b..00000000000000 --- a/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.pass.cpp +++ /dev/null @@ -1,201 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 - -// - -// [inout.ptr], function template inout_ptr -// template -// auto inout_ptr(Smart& s, Args&&... args); // since c++23 - -#include -#include - -#include "../types.h" - -// Test helpers. - -void replace_int_p(int** pp) { - assert(**pp == 90); - delete *pp; - *pp = new int{84}; -} - -void replace_int_p_with_nullptr(int** pp) { - assert(**pp == 90); - delete *pp; - *pp = nullptr; -} - -void replace_nullptr_with_int_p(int** pp) { - assert(*pp == nullptr); - *pp = new int{84}; -} - -void replace_int_void_p(void** pp) { - assert(*(static_cast(*pp)) == 90); - delete static_cast(*pp); - *pp = new int{84}; -} - -void replace_int_void_p_with_nullptr(void** pp) { - assert(*(static_cast(*pp)) == 90); - delete static_cast(*pp); - *pp = nullptr; -} - -void replace_nullptr_with_int_void_p(void** pp) { - assert(*pp == nullptr); - *pp = new int{84}; -} - -void replace_SomeInt_p(SomeInt** pp) { - auto si = **pp; - assert(si.value == 90); - delete static_cast(*pp); - *pp = new SomeInt{9084}; -} - -void replace_SomeInt_void_p(void** pp) { - assert(reinterpret_cast(*pp)->value == 90); - delete static_cast(*pp); - *pp = reinterpret_cast(new SomeInt{9084}); -} - -// Test `std::inout_ptr()` function. - -void test_raw_ptr() { - { - auto rPtr = new int{90}; - - replace_int_p(std::inout_ptr(rPtr)); - assert(*rPtr == 84); - - delete rPtr; - } - { - auto rPtr = new int{90}; - - replace_int_p_with_nullptr(std::inout_ptr(rPtr)); - assert(rPtr == nullptr); - } - { - int* rPtr = nullptr; - - replace_nullptr_with_int_p(std::inout_ptr(rPtr)); - assert(*rPtr == 84); - delete rPtr; - } - { - auto rPtr = new int{90}; - - replace_int_void_p(std::inout_ptr(rPtr)); - assert(*rPtr == 84); - delete rPtr; - } - { - auto rPtr = new int{90}; - - replace_int_void_p_with_nullptr(std::inout_ptr(rPtr)); - assert(rPtr == nullptr); - } - { - int* rPtr = nullptr; - - replace_nullptr_with_int_void_p(std::inout_ptr(rPtr)); - assert(*rPtr == 84); - delete rPtr; - } - { - auto* rPtr = new SomeInt{90}; - - replace_SomeInt_p(std::inout_ptr(rPtr)); - assert(rPtr->value == 9084); - delete rPtr; - } - { - auto* rPtr = new SomeInt{90}; - - replace_SomeInt_void_p(std::inout_ptr(rPtr)); - assert(rPtr->value == 9084); - delete rPtr; - } -} - -void test_unique_ptr() { - { - auto uPtr = std::make_unique(90); - - replace_int_p(std::inout_ptr(uPtr)); - assert(*uPtr == 84); - } - { - std::unique_ptr uPtr; - - replace_nullptr_with_int_p(std::inout_ptr(uPtr)); - assert(*uPtr == 84); - } - { - auto uPtr = std::make_unique(90); - - replace_int_void_p(std::inout_ptr(uPtr)); - assert(*uPtr == 84); - } - { - std::unique_ptr uPtr; - - replace_nullptr_with_int_void_p(std::inout_ptr(uPtr)); - assert(*uPtr == 84); - } - { - auto uPtr = std::make_unique(90); - - replace_SomeInt_p(std::inout_ptr(uPtr)); - assert(uPtr->value == 9084); - } - { - auto uPtr = std::make_unique(90); - - replace_SomeInt_void_p(std::inout_ptr(uPtr)); - assert(uPtr->value == 9084); - } -} - -void test_custom_ptr() { - // ConstructiblePtr - { - ConstructiblePtr cPtr(new int{90}); - - replace_int_p(std::inout_ptr(cPtr)); - assert(cPtr == 84); - } - // ResettablePtr - { - ResettablePtr rPtr(new int{90}); - - replace_int_p(std::inout_ptr(rPtr)); - assert(rPtr == 84); - } - // NonConstructiblePtr - { - NonConstructiblePtr nPtr; - nPtr.reset(new int{90}); - - replace_int_p(std::inout_ptr(nPtr)); - assert(nPtr == 84); - } -} - -int main(int, char**) { - test_raw_ptr(); - test_unique_ptr(); - test_custom_ptr(); - - return 0; -} diff --git a/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.t.deref.pass.cpp b/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.t.convert.pass.cpp similarity index 55% rename from libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.t.deref.pass.cpp rename to libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.t.convert.pass.cpp index 86a055a7644e25..cee19c11c78c38 100644 --- a/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.t.deref.pass.cpp +++ b/libcxx/test/std/utilities/smartptr/adapt/inout.ptr/inout.ptr.t.convert.pass.cpp @@ -17,18 +17,30 @@ // operator Pointer*() const noexcept; // operator void**() const noexcept; +#include +#include #include int main(int, char**) { { - std::unique_ptr uPtr; + std::unique_ptr uPtr = std::make_unique(84); - std::inout_ptr_t, int*>{uPtr}; + const std::inout_ptr_t, int*> ioPtr{uPtr}; + + static_assert(noexcept(ioPtr.operator int**())); + std::same_as decltype(auto) pPtr = ioPtr.operator int**(); + + assert(**pPtr == 84); } { - std::unique_ptr uPtr; + std::unique_ptr uPtr = std::make_unique(84); + + const std::inout_ptr_t, void*> ioPtr{uPtr}; + + static_assert(noexcept(ioPtr.operator void**())); + std::same_as decltype(auto) pPtr = ioPtr.operator void**(); - std::inout_ptr_t, int*>{uPtr}; + assert(**reinterpret_cast(pPtr) == 84); } return 0; diff --git a/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.general.pass.cpp b/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.compile.pass.cpp similarity index 92% rename from libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.general.pass.cpp rename to libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.compile.pass.cpp index 84bea7248e7a6e..71f78a5bf770a0 100644 --- a/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.general.pass.cpp +++ b/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.compile.pass.cpp @@ -21,17 +21,13 @@ int main(int, char**) { std::unique_ptr uPtr; auto outUPtr1 = std::out_ptr(uPtr); - (void)outUPtr1; auto outUPtr2 = std::out_ptr(uPtr); - (void)outUPtr2; } { std::shared_ptr sPtr; auto outSPtr1 = std::out_ptr(sPtr, [](auto* p) { delete p; }); - (void)outSPtr1; auto outSPtr2 = std::out_ptr(sPtr, [](auto* p) { delete p; }); - (void)outSPtr2; } return 0; diff --git a/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.pass.cpp b/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.general.cpp similarity index 100% rename from libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.pass.cpp rename to libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.general.cpp diff --git a/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.t.convert.pass.cpp b/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.t.convert.pass.cpp new file mode 100644 index 00000000000000..1e19d1ee5b4ec0 --- /dev/null +++ b/libcxx/test/std/utilities/smartptr/adapt/out.ptr/out.ptr.t.convert.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// [out.ptr.t], class template out_ptr_t +// template +// class out_ptr_t; // since c++23 + +// operator Pointer*() const noexcept; +// operator void**() const noexcept; + +#include +#include +#include +#include + +int main(int, char**) { + { + std::unique_ptr uPtr; + + const std::out_ptr_t, int*> oPtr{uPtr}; + + static_assert(noexcept(oPtr.operator int**())); + std::same_as decltype(auto) pPtr = oPtr.operator int**(); + + assert(*pPtr == nullptr); + } + { + std::unique_ptr> uPtr; + + const std::out_ptr_t> oPtr{uPtr, std::default_delete{}}; + + static_assert(noexcept(oPtr.operator int**())); + std::same_as decltype(auto) pPtr = oPtr.operator int**(); + + assert(*pPtr == nullptr); + } + { + std::unique_ptr uPtr; + + const std::out_ptr_t, void*> oPtr{uPtr}; + + static_assert(noexcept(oPtr.operator void**())); + std::same_as decltype(auto) pPtr = oPtr.operator void**(); + + assert(*pPtr == nullptr); + } + + return 0; +}