Skip to content

Commit

Permalink
[out_ptr] Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zingam committed Nov 30, 2023
1 parent 4bc779e commit b23e91a
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@

// <memory>

// [out.ptr.t], class template out_ptr_t
// template<class Smart, class Pointer, class... Args>
// class out_ptr_t; // since c++23

// operator Pointer*() const noexcept;
// operator void**() const noexcept;
// [inout.ptr], function template inout_ptr
// template<class Pointer = void, class Smart, class... Args>
// auto inout_ptr(Smart& s, Args&&... args); // since c++23

#include <memory>

int main(int, char**) {
{
std::unique_ptr<int> uPtr;

std::out_ptr_t<std::unique_ptr<int>, int*>{uPtr};
auto inoutUPtr1 = std::inout_ptr(uPtr);
auto inoutUPtr2 = std::inout_ptr<int*>(uPtr);
}
{
std::unique_ptr<int, std::default_delete<int>> uPtr;
auto deleter = [](auto* p) { delete p; };
std::unique_ptr<int, decltype(deleter)> uPtr;

std::out_ptr_t<decltype(uPtr), int*, std::default_delete<int>>{uPtr, std::default_delete<int>{}};
auto inoutUPtr1 = std::inout_ptr(uPtr, deleter);
auto inoutUPtr2 = std::inout_ptr<int*>(uPtr, deleter);
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,188 @@
// template<class Pointer = void, class Smart, class... Args>
// auto inout_ptr(Smart& s, Args&&... args); // since c++23

#include <cassert>
#include <memory>

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<int*>(*pp)) == 90);
delete static_cast<int*>(*pp);
*pp = new int{84};
}

void replace_int_void_p_with_nullptr(void** pp) {
assert(*(static_cast<int*>(*pp)) == 90);
delete static_cast<int*>(*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<SomeInt*>(*pp);
*pp = new SomeInt{9084};
}

void replace_SomeInt_void_p(void** pp) {
assert(reinterpret_cast<SomeInt*>(*pp)->value == 90);
delete static_cast<SomeInt*>(*pp);
*pp = reinterpret_cast<void*>(new SomeInt{9084});
}

// Test `std::inout_ptr()` function.

void test_raw_ptr() {
{
auto rPtr = new int{90};

replace_int_p(std::inout_ptr<int*>(rPtr));
assert(*rPtr == 84);

delete rPtr;
}
{
auto rPtr = new int{90};

replace_int_p_with_nullptr(std::inout_ptr<int*>(rPtr));
assert(rPtr == nullptr);
}
{
int* rPtr = nullptr;

replace_nullptr_with_int_p(std::inout_ptr<int*>(rPtr));
assert(*rPtr == 84);
delete rPtr;
}
{
auto rPtr = new int{90};

replace_int_void_p(std::inout_ptr<int*>(rPtr));
assert(*rPtr == 84);
delete rPtr;
}
{
auto rPtr = new int{90};

replace_int_void_p_with_nullptr(std::inout_ptr<int*>(rPtr));
assert(rPtr == nullptr);
}
{
int* rPtr = nullptr;

replace_nullptr_with_int_void_p(std::inout_ptr<int*>(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<SomeInt*>(rPtr));
assert(rPtr->value == 9084);
delete rPtr;
}
}

void test_unique_ptr() {
{
auto uPtr = std::make_unique<int>(90);

replace_int_p(std::inout_ptr(uPtr));
assert(*uPtr == 84);
}
{
std::unique_ptr<int> uPtr;

auto inoutUPtr1 = std::inout_ptr(uPtr);
(void)inoutUPtr1;
auto inoutUPtr2 = std::inout_ptr<int*>(uPtr);
(void)inoutUPtr2;
replace_nullptr_with_int_p(std::inout_ptr(uPtr));
assert(*uPtr == 84);
}
{
auto deleter = [](auto* p) { delete p; };
std::unique_ptr<int, decltype(deleter)> uPtr;
auto uPtr = std::make_unique<int>(90);

auto inoutUPtr1 = std::inout_ptr(uPtr, deleter);
(void)inoutUPtr1;
auto inoutUPtr2 = std::inout_ptr<int*>(uPtr, deleter);
(void)inoutUPtr2;
replace_int_void_p(std::inout_ptr(uPtr));
assert(*uPtr == 84);
}
{
std::unique_ptr<int> uPtr;

replace_nullptr_with_int_void_p(std::inout_ptr(uPtr));
assert(*uPtr == 84);
}
{
auto uPtr = std::make_unique<SomeInt>(90);

replace_SomeInt_p(std::inout_ptr(uPtr));
assert(uPtr->value == 9084);
}
{
auto uPtr = std::make_unique<SomeInt>(90);

replace_SomeInt_void_p(std::inout_ptr<SomeInt*>(uPtr));
assert(uPtr->value == 9084);
}
}

void test_custom_ptr() {
// ConstructiblePtr
{
ConstructiblePtr<int> cPtr(new int{90});

replace_int_p(std::inout_ptr(cPtr));
assert(cPtr == 84);
}
// ResettablePtr
{
ResettablePtr<int> rPtr(new int{90});

replace_int_p(std::inout_ptr(rPtr));
assert(rPtr == 84);
}
// NonConstructiblePtr
{
NonConstructiblePtr<int> 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;
}
Loading

0 comments on commit b23e91a

Please sign in to comment.