From caf804b1795575d7714c62dd45b649831598055e Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 21 Mar 2024 23:07:56 +0000 Subject: libstdc++: Implement C++26 std::indirect [PR119152] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch implements C++26 std::indirect as specified in P3019 with amendment to move assignment from LWG 4251. PR libstdc++/119152 libstdc++-v3/ChangeLog: * doc/doxygen/stdheader.cc: Added indirect.h file. * include/Makefile.am: Add new header. * include/Makefile.in: Regenerate. * include/bits/indirect.h: New file. * include/bits/version.def (indirect): Define. * include/bits/version.h: Regenerate. * include/std/memory: Include new header. * testsuite/std/memory/indirect/copy.cc * testsuite/std/memory/indirect/copy_alloc.cc * testsuite/std/memory/indirect/ctor.cc * testsuite/std/memory/indirect/incomplete.cc * testsuite/std/memory/indirect/invalid_neg.cc * testsuite/std/memory/indirect/move.cc * testsuite/std/memory/indirect/move_alloc.cc * testsuite/std/memory/indirect/relops.cc Co-authored-by: Tomasz Kamiński Signed-off-by: Tomasz Kamiński --- libstdc++-v3/testsuite/std/memory/indirect/copy.cc | 121 +++++++++ .../testsuite/std/memory/indirect/copy_alloc.cc | 228 ++++++++++++++++ libstdc++-v3/testsuite/std/memory/indirect/ctor.cc | 203 ++++++++++++++ .../testsuite/std/memory/indirect/incomplete.cc | 38 +++ .../testsuite/std/memory/indirect/invalid_neg.cc | 28 ++ libstdc++-v3/testsuite/std/memory/indirect/move.cc | 144 ++++++++++ .../testsuite/std/memory/indirect/move_alloc.cc | 296 +++++++++++++++++++++ .../testsuite/std/memory/indirect/relops.cc | 82 ++++++ 8 files changed, 1140 insertions(+) create mode 100644 libstdc++-v3/testsuite/std/memory/indirect/copy.cc create mode 100644 libstdc++-v3/testsuite/std/memory/indirect/copy_alloc.cc create mode 100644 libstdc++-v3/testsuite/std/memory/indirect/ctor.cc create mode 100644 libstdc++-v3/testsuite/std/memory/indirect/incomplete.cc create mode 100644 libstdc++-v3/testsuite/std/memory/indirect/invalid_neg.cc create mode 100644 libstdc++-v3/testsuite/std/memory/indirect/move.cc create mode 100644 libstdc++-v3/testsuite/std/memory/indirect/move_alloc.cc create mode 100644 libstdc++-v3/testsuite/std/memory/indirect/relops.cc (limited to 'libstdc++-v3/testsuite/std') diff --git a/libstdc++-v3/testsuite/std/memory/indirect/copy.cc b/libstdc++-v3/testsuite/std/memory/indirect/copy.cc new file mode 100644 index 0000000..0ac6e92 --- /dev/null +++ b/libstdc++-v3/testsuite/std/memory/indirect/copy.cc @@ -0,0 +1,121 @@ +// { dg-do run { target c++26 } } + +#include +#include +#include +#include + +#include +#include + +using __gnu_test::tracker_allocator; +using Counter = __gnu_test::tracker_allocator_counter; +using Vector = std::vector; +using Indirect = std::indirect>; +const Indirect src(std::in_place, {1, 2, 3}); + +constexpr void +test_ctor() +{ + Counter::reset(); + Indirect i1(src); + VERIFY( *i1 == *src ); + VERIFY( &*i1 != &*src ); + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 1 ); + VERIFY( Counter::get_destruct_count() == 0 ); + + Counter::reset(); + Indirect i2(std::allocator_arg, {}, src); + VERIFY( *i2 == *src ); + VERIFY( &*i2 != &*src ); + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 1 ); + VERIFY( Counter::get_destruct_count() == 0 ); +} + +constexpr void +test_assign() +{ + Indirect i1; + Counter::reset(); + + i1 = src; + VERIFY( *i1 == *src ); + VERIFY( &*i1 != &*src ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); + + auto(std::move(i1)); + Counter::reset(); + + i1 = src; + VERIFY( *i1 == *src ); + VERIFY( &*i1 != &*src ); + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 1 ); + VERIFY( Counter::get_destruct_count() == 0 ); +} + +constexpr void +test_valueless() +{ + Indirect e; + auto(std::move(e)); + VERIFY( e.valueless_after_move() ); + + Counter::reset(); + Indirect i1(e); + VERIFY( i1.valueless_after_move() ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); + + Indirect i2(std::allocator_arg, {}, e); + VERIFY( i2.valueless_after_move() ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); + + Indirect i3(src); + Counter::reset(); + i3 = e; + VERIFY( i3.valueless_after_move() ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 1 ); + + Counter::reset(); + i3 = e; + VERIFY( i3.valueless_after_move() ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); +} + +constexpr void +test_all() +{ + test_ctor(); + test_assign(); + test_valueless(); +} + +int main() +{ + test_all(); + + static_assert([] { + test_all(); + return true; + }); +} diff --git a/libstdc++-v3/testsuite/std/memory/indirect/copy_alloc.cc b/libstdc++-v3/testsuite/std/memory/indirect/copy_alloc.cc new file mode 100644 index 0000000..d5865b9 --- /dev/null +++ b/libstdc++-v3/testsuite/std/memory/indirect/copy_alloc.cc @@ -0,0 +1,228 @@ +// { dg-do run { target c++26 } } + +#include +#include +#include +#include + +#include +#include + +using __gnu_test::propagating_allocator; +using __gnu_test::tracker_allocator; +using Counter = __gnu_test::tracker_allocator_counter; + +template +constexpr void +test_ctor() +{ + using PropAlloc = propagating_allocator; + using Vector = std::vector; + using ScopedAlloc = std::scoped_allocator_adaptor< + propagating_allocator>, + PropAlloc>; + using Indirect = std::indirect; + + const Indirect src(std::allocator_arg, ScopedAlloc{11, 22}, + std::in_place, {1, 2, 3}); + + Counter::reset(); + Indirect i1(src); + VERIFY( *i1 == *src ); + VERIFY( &*i1 != &*src ); + if (Propagate) + { + VERIFY( i1->get_allocator().get_personality() == 22 ); + VERIFY( i1.get_allocator().get_personality() == 11 ); + } + else + { + VERIFY( i1->get_allocator().get_personality() == 0 ); + VERIFY( i1.get_allocator().get_personality() == 0 ); + } + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 1 ); + VERIFY( Counter::get_destruct_count() == 0 ); + + + Counter::reset(); + Indirect i2(std::allocator_arg, ScopedAlloc{33, 44}, src); + VERIFY( *i2 == *src ); + VERIFY( &*i2 != &*src ); + VERIFY( i2->get_allocator().get_personality() == 44 ); + VERIFY( i2.get_allocator().get_personality() == 33 ); + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 1 ); + VERIFY( Counter::get_destruct_count() == 0 ); +} + +template +constexpr void +test_assign() +{ + using PropAlloc = propagating_allocator; + using Vector = std::vector; + using ScopedAlloc = std::scoped_allocator_adaptor< + propagating_allocator>, + PropAlloc>; + using Indirect = std::indirect; + + const Indirect src(std::allocator_arg, ScopedAlloc{11, 22}, + std::in_place, {1, 2, 3}); + + Indirect i1(std::allocator_arg, ScopedAlloc{11, 22}); + Counter::reset(); + + i1 = src; + VERIFY( *i1 == *src ); + VERIFY( &*i1 != &*src ); + VERIFY( i1->get_allocator().get_personality() == 22 ); + VERIFY( i1.get_allocator().get_personality() == 11 ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); + + Indirect i2(std::allocator_arg, ScopedAlloc{33, 44}); + Counter::reset(); + + i2 = src; + VERIFY( *i2 == *src ); + VERIFY( &*i2 != &*src ); + if (Propagate) + { + VERIFY( i2->get_allocator().get_personality() == 22 ); + VERIFY( i2.get_allocator().get_personality() == 11 ); + } + else + { + VERIFY( i2->get_allocator().get_personality() == 44 ); + VERIFY( i2.get_allocator().get_personality() == 33 ); + } + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_deallocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_construct_count() == 1 ); + VERIFY( Counter::get_destruct_count() == 1 ); + + Indirect i3(std::allocator_arg, ScopedAlloc{11, 22}); + auto(std::move(i3)); + Counter::reset(); + + i3 = src; + VERIFY( *i3 == *src ); + VERIFY( &*i3 != &*src ); + VERIFY( i3->get_allocator().get_personality() == 22 ); + VERIFY( i3.get_allocator().get_personality() == 11 ); + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 1 ); + VERIFY( Counter::get_destruct_count() == 0 ); + + Indirect i4(std::allocator_arg, ScopedAlloc{33, 44}); + auto(std::move(i4)); + Counter::reset(); + + i4 = src; + VERIFY( *i4 == *src ); + VERIFY( &*i4 != &*src ); + if (Propagate) + { + VERIFY( i4->get_allocator().get_personality() == 22 ); + VERIFY( i4.get_allocator().get_personality() == 11 ); + } + else + { + VERIFY( i4->get_allocator().get_personality() == 44 ); + VERIFY( i4.get_allocator().get_personality() == 33 ); + } + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 1 ); + VERIFY( Counter::get_destruct_count() == 0 ); +} + +template +constexpr void +test_valueless() +{ + using PropAlloc = propagating_allocator; + using Vector = std::vector; + using ScopedAlloc = std::scoped_allocator_adaptor< + propagating_allocator>, + PropAlloc>; + using Indirect = std::indirect; + + Indirect e(std::allocator_arg, ScopedAlloc{11, 22}); + auto(std::move(e)); + VERIFY( e.valueless_after_move() ); + + Counter::reset(); + Indirect i1(e); + VERIFY( i1.valueless_after_move() ); + if (Propagate) + VERIFY( i1.get_allocator().get_personality() == 11 ); + else + VERIFY( i1.get_allocator().get_personality() == 0 ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); + + Counter::reset(); + Indirect i2(std::allocator_arg, ScopedAlloc{33, 44}, e); + VERIFY( i2.valueless_after_move() ); + VERIFY( i2.get_allocator().get_personality() == 33 ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); + + Indirect i3(std::allocator_arg, ScopedAlloc{33, 44}); + Counter::reset(); + + i3 = e; + VERIFY( i3.valueless_after_move() ); + if (Propagate) + VERIFY( i3.get_allocator().get_personality() == 11 ); + else + VERIFY( i3.get_allocator().get_personality() == 33 ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 1 ); + + Counter::reset(); + i2 = e; + VERIFY( i2.valueless_after_move() ); + if (Propagate) + VERIFY( i2.get_allocator().get_personality() == 11 ); + else + VERIFY( i2.get_allocator().get_personality() == 33 ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); +} + +template +constexpr void +test_all() +{ + test_ctor(); + test_assign(); + test_valueless(); +} + +int main() +{ + test_all(); + test_all(); + + static_assert([] { + test_all(); + test_all(); + return true; + }); +} diff --git a/libstdc++-v3/testsuite/std/memory/indirect/ctor.cc b/libstdc++-v3/testsuite/std/memory/indirect/ctor.cc new file mode 100644 index 0000000..67e7a8a --- /dev/null +++ b/libstdc++-v3/testsuite/std/memory/indirect/ctor.cc @@ -0,0 +1,203 @@ +// { dg-do run { target c++26 } } + +#include +#include +#include +#include + +#ifndef __cpp_lib_indirect +# error __cpp_lib_indirect feature test macro missing in +#elif __cpp_lib_indirect != 202502 +# error __cpp_lib_indirect feature test macro has wrong value in +#endif + +#include +#include + +using __gnu_test::uneq_allocator; +using UneqAlloc = uneq_allocator; +using ScopedAlloc = std::scoped_allocator_adaptor< + uneq_allocator>, + UneqAlloc>; + +struct Obj +{ + int i; + char c[2]; +}; + +constexpr void +test_deduction_guides() +{ + const Obj o{}; + std::indirect i1(o); + static_assert(std::is_same_v>); + + using Alloc = __gnu_test::SimpleAllocator; + Alloc a; + std::indirect i2(std::allocator_arg, a, o); + static_assert(std::is_same_v>); +} + +constexpr void +test_default_ctor() +{ + using __gnu_test::default_init_allocator; + + std::indirect> i1; + default_init_allocator a{}; + + // The contained object and the allocator should be value-initialized. + VERIFY( i1->i == 0 ); + VERIFY( i1->c[0] == 0 ); + VERIFY( i1->c[1] == 0 ); + VERIFY( i1.get_allocator() == a ); + + a.state = 5; + // Allocator-extended default constructor: + std::indirect> i2(std::allocator_arg, a); + VERIFY( i2.get_allocator() == a ); + + // Object is constructed using allocator-aware constructor. + std::indirect, ScopedAlloc> + i3(std::allocator_arg, ScopedAlloc(11, 22)); + VERIFY( i3->empty() ); + VERIFY( i3->get_allocator().get_personality() == 22 ); + VERIFY( i3.get_allocator().get_personality() == 11 ); +} + +constexpr void +test_forwarding_ctor() +{ + Obj obj{1, {'2', '3'}}; + auto verify = [](std::indirect const& i) + { + VERIFY( i->i == 1 ); + VERIFY( i->c[0] == '2' ); + VERIFY( i->c[1] == '3' ); + }; + + std::indirect i1(std::as_const(obj)); + verify(i1); + std::indirect i2(std::move(std::as_const(obj))); + verify(i2); + std::indirect i3(obj); + verify(i3); + std::indirect i4(std::move(obj)); + verify(i4); + + std::indirect i5({1, {'2', '3'}}); + verify(i5); + + // Aggregate parens init + std::indirect i6(7); + VERIFY( i6->i == 7 ); + + std::vector v{1, 2, 3, 4, 5}; + // Object is constructed using allocator-aware constructor. + std::indirect, ScopedAlloc> + i7(std::allocator_arg, ScopedAlloc(11, 22), v); + VERIFY( i7->size() == 5 ); + VERIFY( v.size() == 5 ); + VERIFY( i7->get_allocator().get_personality() == 22 ); + VERIFY( i7.get_allocator().get_personality() == 11 ); + + std::indirect, ScopedAlloc> + i8(std::allocator_arg, ScopedAlloc(11, 22), std::move(v)); + VERIFY( i8->size() == 5 ); + VERIFY( v.size() == 0 ); + VERIFY( i8->get_allocator().get_personality() == 22 ); + VERIFY( i8.get_allocator().get_personality() == 11 ); +} + +constexpr void +test_inplace_ctor() +{ + std::indirect i1(std::in_place); + VERIFY( i1->i == 0 ); + VERIFY( i1->c[0] == 0 ); + VERIFY( i1->c[1] == 0 ); + + std::indirect i2(std::in_place, 10); + VERIFY( i2->i == 10 ); + VERIFY( i2->c[0] == 0 ); + VERIFY( i2->c[1] == 0 ); + + std::indirect> + i3(std::allocator_arg, 42, std::in_place); + VERIFY( i3->i == 0 ); + VERIFY( i3->c[0] == 0 ); + VERIFY( i3->c[1] == 0 ); + VERIFY( i3.get_allocator().get_personality() == 42 ); + + std::indirect> + i4(std::allocator_arg, 42, std::in_place, 10); + VERIFY( i4->i == 10 ); + VERIFY( i4->c[0] == 0 ); + VERIFY( i4->c[1] == 0 ); + VERIFY( i4.get_allocator().get_personality() == 42 ); + + std::indirect> i5(std::in_place); + VERIFY( i5->size() == 0 ); + VERIFY( i5->at(0) == 13 ); + + std::indirect> i6(std::in_place, 5, 13); + VERIFY( i6->size() == 5 ); + VERIFY( i6->at(0) == 13 ); + + std::indirect> i7(std::in_place, {1, 2, 3, 4}); + VERIFY( i7->size() == 4 ); + VERIFY( i7->at(2) == 3 ); + + std::indirect> + i8(std::in_place, UneqAlloc{42}); + VERIFY( i8->size() == 0 ); + VERIFY( i8->get_allocator().get_personality() == 42 ); + + std::indirect> + i9(std::in_place, 5, 13, UneqAlloc{42}); + VERIFY( i9->size() == 5 ); + VERIFY( i9->at(0) == 13 ); + VERIFY( i9->get_allocator().get_personality() == 42 ); + + std::indirect> + i10(std::in_place, {1, 2, 3, 4}, UneqAlloc{42}); + VERIFY( i10->size() == 4 ); + VERIFY( i10->at(2) == 3 ); + VERIFY( i10->get_allocator().get_personality() == 42 ); + + std::indirect, ScopedAlloc> + i14(std::allocator_arg, ScopedAlloc(11, 22), + std::in_place); + VERIFY( i14->size() == 0 ); + VERIFY( i14->get_allocator().get_personality() == 22 ); + VERIFY( i14.get_allocator().get_personality() == 11 ); + + std::indirect, ScopedAlloc> + i15(std::allocator_arg, ScopedAlloc(11, 22), + std::in_place, 5, 13); + VERIFY( i15->size() == 5 ); + VERIFY( i15->at(0) == 13 ); + VERIFY( i15->get_allocator().get_personality() == 22 ); + VERIFY( i15.get_allocator().get_personality() == 11 ); + + std::indirect, ScopedAlloc> + i16(std::allocator_arg, ScopedAlloc(11, 22), + std::in_place, {1, 2, 3, 4}); + VERIFY( i16->size() == 4 ); + VERIFY( i16->at(2) == 3 ); + VERIFY( i16->get_allocator().get_personality() == 22 ); + VERIFY( i16.get_allocator().get_personality() == 11 ); +} + +int main() +{ + test_default_ctor(); + test_forwarding_ctor(); + + static_assert([] { + test_default_ctor(); + test_forwarding_ctor(); + return true; + }); +} diff --git a/libstdc++-v3/testsuite/std/memory/indirect/incomplete.cc b/libstdc++-v3/testsuite/std/memory/indirect/incomplete.cc new file mode 100644 index 0000000..1faf13d --- /dev/null +++ b/libstdc++-v3/testsuite/std/memory/indirect/incomplete.cc @@ -0,0 +1,38 @@ +// { dg-do compile { target c++26 } } + +#include + +struct Incomplete; +bool operator==(const Incomplete&, const Incomplete&); +std::strong_ordering operator<=>(const Incomplete&, const Incomplete&); + +template<> +struct std::hash +{ + static std::size_t operator()(const Incomplete& c); +}; + +std::indirect* +test_move(std::indirect& i1, std::indirect& i2) +{ + i2.swap(i2); + return new std::indirect(std::move(i1)); +} + +void +test_relops(std::indirect const& i1, Incomplete const& o) +{ + void(i1 == i1); + void(i1 < i1); + void(i1 >= i1); + + void(i1 != o); + void(i1 < o); +} + +void +test_hash(std::indirect const& i1) +{ + std::hash> h; + h(i1); +} diff --git a/libstdc++-v3/testsuite/std/memory/indirect/invalid_neg.cc b/libstdc++-v3/testsuite/std/memory/indirect/invalid_neg.cc new file mode 100644 index 0000000..82e7e84 --- /dev/null +++ b/libstdc++-v3/testsuite/std/memory/indirect/invalid_neg.cc @@ -0,0 +1,28 @@ +// { dg-do compile { target c++26 } } + +#include + +// In every specialization indirect, if the type +// allocator_traits::value_type is not the same type as T, +// the program is ill-formed. +using T1 = std::indirect>::value_type; // { dg-error "here" } + +// A program that instantiates the definition of the template +// indirect with a type for the T parameter that is +// a non-object type, an array type, in_place_t, +// a specialization of in_place_type_t, or a cv-qualified type is ill-formed. + +using T2 = std::indirect::value_type; // { dg-error "here" } + +using T3 = std::indirect::value_type; // { dg-error "here" } + +using T4 = std::indirect::value_type; // { dg-error "here" } + +using T5 = std::indirect>::value_type; // { dg-error "here" } + +using T6 = std::indirect::value_type; // { dg-error "here" } + +using T7 = std::indirect::value_type; // { dg-error "here" } + +// { dg-error "static assertion failed" "" { target *-*-* } 0 } +// { dg-prune-output "forming pointer to reference" } diff --git a/libstdc++-v3/testsuite/std/memory/indirect/move.cc b/libstdc++-v3/testsuite/std/memory/indirect/move.cc new file mode 100644 index 0000000..6e87c60 --- /dev/null +++ b/libstdc++-v3/testsuite/std/memory/indirect/move.cc @@ -0,0 +1,144 @@ +// { dg-do run { target c++26 } } + +#include +#include +#include +#include +#include + +#include +#include + +using __gnu_test::tracker_allocator; +using Counter = __gnu_test::tracker_allocator_counter; +using Vector = std::vector; +using Indirect = std::indirect>; +const Indirect val(std::in_place, {1, 2, 3}); + +constexpr void +verifyNoAllocations() +{ + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); +} + +constexpr void +test_ctor() +{ + std::optional src; + auto make = [&src] -> Indirect&& { + src.emplace(val); + Counter::reset(); + return std::move(*src); + }; + + Indirect i1(make()); + VERIFY( src->valueless_after_move() ); + VERIFY( *i1 == *val ); + verifyNoAllocations(); + + Indirect i2(std::allocator_arg, {}, make()); + VERIFY( src->valueless_after_move() ); + VERIFY( *i2 == *val ); + verifyNoAllocations(); +} + +constexpr void +test_assign() +{ + std::optional src; + auto make = [&src] -> Indirect&& { + src.emplace(val); + Counter::reset(); + return std::move(*src); + }; + + Indirect i1; + + i1 = make(); + VERIFY( src->valueless_after_move() ); + VERIFY( *i1 == *val ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 1 ); + + auto(std::move(i1)); + i1 = make(); + VERIFY( *i1 == *val ); + VERIFY( src->valueless_after_move() ); + verifyNoAllocations(); +} + +constexpr void +test_swap() +{ + const Indirect val1(std::in_place, {1, 2, 3}); + const Indirect val2(std::in_place, {2, 4, 6}); + + Indirect i1(val1); + Indirect i2(val2); + Counter::reset(); + i1.swap(i2); + VERIFY( *i2 == *val1 ); + VERIFY( *i1 == *val2 ); + verifyNoAllocations(); + + auto(std::move(i1)); + + Counter::reset(); + i1.swap(i2); + VERIFY( *i1 == *val1 ); + VERIFY( i2.valueless_after_move() ); + verifyNoAllocations(); +} + +constexpr void +test_valueless() +{ + auto e = [] { + Indirect res; + auto(std::move(res)); + Counter::reset(); + return res; + }; + + Indirect i1(e()); + VERIFY( i1.valueless_after_move() ); + verifyNoAllocations(); + + Indirect i2(std::allocator_arg, {}, e()); + VERIFY( i2.valueless_after_move() ); + verifyNoAllocations(); + + Indirect i3(val); + i3 = e(); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 1 ); + + i3 = e(); + verifyNoAllocations(); +} + +constexpr void +test_all() +{ + test_ctor(); + test_assign(); + test_swap(); + test_valueless(); +} + +int main() +{ + test_all(); + + static_assert([] { + test_all(); + return true; + }); +} diff --git a/libstdc++-v3/testsuite/std/memory/indirect/move_alloc.cc b/libstdc++-v3/testsuite/std/memory/indirect/move_alloc.cc new file mode 100644 index 0000000..cd6f90d --- /dev/null +++ b/libstdc++-v3/testsuite/std/memory/indirect/move_alloc.cc @@ -0,0 +1,296 @@ +// { dg-do run { target c++26 } } + +#include +#include +#include +#include +#include + +#include +#include + +using __gnu_test::propagating_allocator; +using __gnu_test::tracker_allocator; +using Counter = __gnu_test::tracker_allocator_counter; + +constexpr void +verifyNoAllocations() +{ + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); +} + +template +constexpr void +test_ctor() +{ + using PropAlloc = propagating_allocator; + using Vector = std::vector; + using ScopedAlloc = std::scoped_allocator_adaptor< + propagating_allocator>, + PropAlloc>; + using Indirect = std::indirect; + + const Indirect val(std::in_place, {1, 2, 3}); + std::optional src; + auto make = [&val, &src] -> Indirect&& { + src.emplace(std::allocator_arg, ScopedAlloc{11, 22}, val); + Counter::reset(); + return std::move(*src); + }; + + Indirect i1(make()); + VERIFY( src->valueless_after_move() ); + VERIFY( *i1 == *val ); + VERIFY( i1->get_allocator().get_personality() == 22 ); + VERIFY( i1.get_allocator().get_personality() == 11 ); + verifyNoAllocations(); + + Indirect i2(std::allocator_arg, ScopedAlloc{11, 22}, make()); + VERIFY( src->valueless_after_move() ); + VERIFY( *i2 == *val ); + VERIFY( i2->get_allocator().get_personality() == 22 ); + VERIFY( i2.get_allocator().get_personality() == 11 ); + verifyNoAllocations(); + + Indirect i3(std::allocator_arg, ScopedAlloc{33, 44}, make()); + // We move-from contained object + VERIFY( !src->valueless_after_move() ); + VERIFY( *i3 == *val ); + VERIFY( i3->get_allocator().get_personality() == 44 ); + VERIFY( i3.get_allocator().get_personality() == 33 ); + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 1 ); + VERIFY( Counter::get_destruct_count() == 0 ); +} + +template +constexpr void +test_assign() +{ + using PropAlloc = propagating_allocator; + using Vector = std::vector; + using ScopedAlloc = std::scoped_allocator_adaptor< + propagating_allocator>, + PropAlloc>; + using Indirect = std::indirect; + + const Indirect val(std::in_place, {1, 2, 3}); + std::optional src; + auto make = [&val, &src] -> Indirect&& { + src.emplace(std::allocator_arg, ScopedAlloc{11, 22}, val); + Counter::reset(); + return std::move(*src); + }; + + Indirect i1(std::allocator_arg, ScopedAlloc{11, 22}); + + i1 = make(); + VERIFY( src->valueless_after_move() ); + VERIFY( *i1 == *val ); + VERIFY( i1->get_allocator().get_personality() == 22 ); + VERIFY( i1.get_allocator().get_personality() == 11 ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 1 ); + + Indirect i2(std::allocator_arg, ScopedAlloc{33, 44}); + + i2 = make(); + VERIFY( *i2 == *val ); + if (Propagate) + { + VERIFY( src->valueless_after_move() ); + VERIFY( i2->get_allocator().get_personality() == 22 ); + VERIFY( i2.get_allocator().get_personality() == 11 ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + } + else + { + // We allocate new holder and move-from contained object + VERIFY( !src->valueless_after_move() ); + VERIFY( i2->get_allocator().get_personality() == 44 ); + VERIFY( i2.get_allocator().get_personality() == 33 ); + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_construct_count() == 1 ); + } + VERIFY( Counter::get_deallocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_destruct_count() == 1 ); + + Indirect i3(std::allocator_arg, ScopedAlloc{11, 22}); + auto(std::move(i3)); + + i3 = make(); + VERIFY( *i3 == *val ); + VERIFY( src->valueless_after_move() ); + VERIFY( i3->get_allocator().get_personality() == 22 ); + VERIFY( i3.get_allocator().get_personality() == 11 ); + verifyNoAllocations(); + + Indirect i4(std::allocator_arg, ScopedAlloc{33, 44}); + auto(std::move(i4)); + + i4 = make(); + VERIFY( *i4 == *val ); + if (Propagate) + { + VERIFY( src->valueless_after_move() ); + VERIFY( i4->get_allocator().get_personality() == 22 ); + VERIFY( i4.get_allocator().get_personality() == 11 ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_construct_count() == 0 ); + } + else + { + // We allocate new holder and move-from contained object + VERIFY( !src->valueless_after_move() ); + VERIFY( i4->get_allocator().get_personality() == 44 ); + VERIFY( i4.get_allocator().get_personality() == 33 ); + VERIFY( Counter::get_allocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_construct_count() == 1 ); + } + VERIFY( Counter::get_deallocation_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 0 ); +} + +template +constexpr void +test_swap() +{ + using PropAlloc = propagating_allocator; + using Vector = std::vector; + using ScopedAlloc = std::scoped_allocator_adaptor< + propagating_allocator>, + PropAlloc>; + using Indirect = std::indirect; + + const Indirect val1(std::in_place, {1, 2, 3}); + const Indirect val2(std::in_place, {2, 4, 6}); + + Indirect i1(std::allocator_arg, ScopedAlloc{11, 22}, val1); + Indirect i2(std::allocator_arg, ScopedAlloc{11, 22}, val2); + Counter::reset(); + i1.swap(i2); + VERIFY( *i2 == *val1 ); + VERIFY( *i1 == *val2 ); + verifyNoAllocations(); + + auto(std::move(i1)); + + Counter::reset(); + i1.swap(i2); + VERIFY( *i1 == *val1 ); + VERIFY( i2.valueless_after_move() ); + verifyNoAllocations(); + + if (!Propagate) + return; + + Indirect i3(std::allocator_arg, ScopedAlloc{33, 44}, val2); + Counter::reset(); + i1.swap(i3); + VERIFY( *i1 == *val2 ); + VERIFY( i1->get_allocator().get_personality() == 44 ); + VERIFY( i1.get_allocator().get_personality() == 33 ); + VERIFY( *i3 == *val1 ); + VERIFY( i3->get_allocator().get_personality() == 22 ); + VERIFY( i3.get_allocator().get_personality() == 11 ); + verifyNoAllocations(); + + i1.swap(i2); + VERIFY( i1.valueless_after_move() ); + VERIFY( i1.get_allocator().get_personality() == 11 ); + VERIFY( *i2 == *val2 ); + VERIFY( i2->get_allocator().get_personality() == 44 ); + VERIFY( i2.get_allocator().get_personality() == 33 ); + verifyNoAllocations(); +} + +template +constexpr void +test_valueless() +{ + using PropAlloc = propagating_allocator; + using Vector = std::vector; + using ScopedAlloc = std::scoped_allocator_adaptor< + propagating_allocator>, + PropAlloc>; + using Indirect = std::indirect; + + auto e = [] { + Indirect res(std::allocator_arg, ScopedAlloc{11, 22}); + auto(std::move(res)); + Counter::reset(); + return res; + }; + + Indirect i1(e()); + VERIFY( i1.valueless_after_move() ); + VERIFY( i1.get_allocator().get_personality() == 11 ); + verifyNoAllocations(); + + Indirect i2(std::allocator_arg, ScopedAlloc{33, 44}, e()); + VERIFY( i2.valueless_after_move() ); + VERIFY( i2.get_allocator().get_personality() == 33 ); + verifyNoAllocations(); + + Indirect i3(std::allocator_arg, ScopedAlloc{33, 44}); + + i3 = e(); + VERIFY( i3.valueless_after_move() ); + if (Propagate) + VERIFY( i3.get_allocator().get_personality() == 11 ); + else + VERIFY( i3.get_allocator().get_personality() == 33 ); + VERIFY( Counter::get_allocation_count() == 0 ); + VERIFY( Counter::get_deallocation_count() == sizeof(Vector) ); + VERIFY( Counter::get_construct_count() == 0 ); + VERIFY( Counter::get_destruct_count() == 1 ); + + i2 = e(); + VERIFY( i2.valueless_after_move() ); + if (Propagate) + VERIFY( i2.get_allocator().get_personality() == 11 ); + else + VERIFY( i2.get_allocator().get_personality() == 33 ); + verifyNoAllocations(); + + i3.swap(i2); + VERIFY( i2.valueless_after_move() ); + VERIFY( i1.valueless_after_move() ); + verifyNoAllocations(); + + if (!Propagate) + return; + + Indirect i4(std::allocator_arg, ScopedAlloc{33, 44}, e()); + i4.swap(i1); + verifyNoAllocations(); +} + +template +constexpr void +test_all() +{ + test_ctor(); + test_assign(); + test_swap(); + test_valueless(); +} + +int main() +{ + test_all(); + test_all(); + + static_assert([] { + test_all(); + test_all(); + return true; + }); +} diff --git a/libstdc++-v3/testsuite/std/memory/indirect/relops.cc b/libstdc++-v3/testsuite/std/memory/indirect/relops.cc new file mode 100644 index 0000000..d77fef2 --- /dev/null +++ b/libstdc++-v3/testsuite/std/memory/indirect/relops.cc @@ -0,0 +1,82 @@ +// { dg-do run { target c++26 } } + +#include +#include + +struct Obj +{ + int i; + constexpr auto operator<=>(const Obj&) const = default; +}; + +template<> +struct std::hash +{ + static size_t operator()(Obj const& obj) + { return std::hash{}(obj.i); } +}; + +constexpr void +test_relops() +{ + std::indirect i1; + VERIFY( i1 == i1 ); + VERIFY( i1 <= i1 ); + VERIFY( i1 >= i1 ); + + std::indirect i2 = std::move(i1); // make i1 valueless + VERIFY( i1 == i1 ); + VERIFY( i2 == i2 ); + VERIFY( i2 != i1 ); + VERIFY( i1 < i2 ); + VERIFY( i2 >= i1 ); + + std::indirect i3 = std::move(i2); // make i2 valueless + VERIFY( i2 == i1 ); + VERIFY( i2 >= i1 ); + VERIFY( i2 <= i1 ); + VERIFY( i3 > i2 ); +} + +constexpr void +test_comp_with_t() +{ + std::indirect i1; + Obj o{2}; + VERIFY( i1 != o ); + VERIFY( i1 < o ); + + std::indirect i2(Obj{2}); + VERIFY( i2 == o ); + VERIFY( i2 <= o ); + VERIFY( o <= i2 ); + + std::indirect i3 = std::move(i2); // make i2 valueless + VERIFY( i2 != o ); + VERIFY( i2 < o ); +} + +void +test_hash() +{ + Obj o{5}; + std::indirect i(o); + VERIFY( std::hash>{}(i) + == std::hash{}(o) ); + + auto(std::move(i)); // make i valueless + (void)std::hash>{}(i); +} + +int main() +{ + test_relops(); + test_comp_with_t(); + test_hash(); + + static_assert([] { + test_relops(); + test_comp_with_t(); + return true; + }); +} -- cgit v1.1