summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2022-04-25 14:24:48 +0100
committerJonathan Wakely <jwakely@redhat.com>2022-04-25 16:16:33 +0100
commit2fbdcf5e58c83a671b4d00f72b7ef91c462b6fc8 (patch)
tree430396d99d87c717c2caf14e2385278e90c78f31 /libstdc++-v3/testsuite
parenta5cee0480c10bafa8ed65d49e5cedca23d98d7b7 (diff)
libstdc++: Implement constexpr std::unique_ptr for C++23 (P2273R3)
libstdc++-v3/ChangeLog: * include/bits/ptr_traits.h (__cpp_lib_constexpr_memory): Define conditionally. * include/bits/unique_ptr.h (__cpp_lib_constexpr_memory): Define for C++23. (default_delete, default_delete<T[]>, __uniq_ptr_impl) (unique_ptr, unique_ptr<T[], D>): Add constexpr to all member functions. * include/std/version (__cpp_lib_constexpr_memory): Define new value for C++23. * testsuite/20_util/unique_ptr/assign/constexpr.cc: New test. * testsuite/20_util/unique_ptr/comparison/constexpr.cc: New test. * testsuite/20_util/unique_ptr/cons/constexpr_c++20.cc: New test. * testsuite/20_util/unique_ptr/creation/constexpr.cc: New test. * testsuite/20_util/unique_ptr/modifiers/constexpr.cc: New test. * testsuite/20_util/unique_ptr/specialized_algorithms/constexpr.cc: New test.
Diffstat (limited to 'libstdc++-v3/testsuite')
-rw-r--r--libstdc++-v3/testsuite/20_util/unique_ptr/assign/constexpr.cc48
-rw-r--r--libstdc++-v3/testsuite/20_util/unique_ptr/comparison/constexpr.cc73
-rw-r--r--libstdc++-v3/testsuite/20_util/unique_ptr/cons/constexpr_c++20.cc85
-rw-r--r--libstdc++-v3/testsuite/20_util/unique_ptr/creation/constexpr.cc34
-rw-r--r--libstdc++-v3/testsuite/20_util/unique_ptr/modifiers/constexpr.cc68
-rw-r--r--libstdc++-v3/testsuite/20_util/unique_ptr/specialized_algorithms/constexpr.cc46
6 files changed, 354 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/assign/constexpr.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/assign/constexpr.cc
new file mode 100644
index 00000000000..fb4acbda2d5
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/assign/constexpr.cc
@@ -0,0 +1,48 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+constexpr bool
+test_move()
+{
+ std::unique_ptr<int> p1(new int(2));
+ std::unique_ptr<int> p2;
+ p2 = std::move(p1);
+ VERIFY( *p2 == 2 );
+ std::unique_ptr<int[]> a1(new int[]{0, 1, 2});
+ std::unique_ptr<int[]> a2;
+ a2 = std::move(a1);
+ VERIFY( a2[2] == 2 );
+
+ return true;
+}
+static_assert( test_move() );
+
+constexpr bool
+test_convert()
+{
+ std::unique_ptr<int> p1(new int(2));
+ std::unique_ptr<const int> p2;
+ p2 = std::move(p1);
+ VERIFY( *p2 == 2 );
+ std::unique_ptr<int[]> a1(new int[]{0, 1, 2});
+ std::unique_ptr<const int[]> a2;
+ a2 = std::move(a1);
+ VERIFY( a2[2] == 2 );
+
+ return true;
+}
+static_assert( test_convert() );
+
+constexpr bool
+test_null()
+{
+ std::unique_ptr<int> p(new int(2));
+ p = nullptr;
+ VERIFY( !p );
+ p = nullptr;
+ return true;
+}
+static_assert( test_null() );
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/comparison/constexpr.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/comparison/constexpr.cc
new file mode 100644
index 00000000000..83e4f0826a8
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/comparison/constexpr.cc
@@ -0,0 +1,73 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+constexpr bool
+test_eq()
+{
+ std::unique_ptr<int> p1, p2;
+ VERIFY( p1 == p2 );
+ p1.reset(new int(1));
+ VERIFY( p1 == p1 );
+ VERIFY( p1 != p2 );
+ struct null_deleter { constexpr void operator()(const void*) const { } };
+ std::unique_ptr<const int[], null_deleter> p3(p1.get());
+ VERIFY( p3 == p3 );
+ VERIFY( p1 == p3 );
+ VERIFY( p3 != p2 );
+
+ return true;
+}
+static_assert( test_eq() );
+
+constexpr bool
+test_rel()
+{
+ std::unique_ptr<int> p1, p2;
+ VERIFY( !(p1 < p2) );
+ VERIFY( !(p1 > p2) );
+ VERIFY( p1 <= p2 );
+ VERIFY( p1 >= p2 );
+ p1.reset(new int(1));
+ VERIFY( p1 <= p1 );
+ VERIFY( p1 >= p1 );
+ VERIFY( p1 > p2 );
+ VERIFY( p2 < p1 );
+ VERIFY( p2 <= p1 );
+ VERIFY( p1 >= p2 );
+ struct null_deleter { constexpr void operator()(const void*) const { } };
+ std::unique_ptr<const int[], null_deleter> p3(p1.get());
+ VERIFY( p3 <= p3 );
+ VERIFY( p3 >= p3 );
+ VERIFY( p1 <= p3 );
+ VERIFY( p3 > p2 );
+ VERIFY( p3 >= p2 );
+ VERIFY( p2 < p3 );
+ VERIFY( p2 <= p3 );
+
+ return true;
+}
+static_assert( test_rel() );
+
+constexpr bool
+test_3way()
+{
+ std::unique_ptr<int> p1, p2;
+ VERIFY( (p1 <=> p1) == 0 );
+ VERIFY( (p1 <=> p2) == 0 );
+ p1.reset(new int(1));
+ VERIFY( (p1 <=> p1) == 0 );
+ VERIFY( (p1 <=> p2) > 0 );
+ VERIFY( (p2 <=> p1) < 0 );
+ struct null_deleter { constexpr void operator()(const void*) const { } };
+ std::unique_ptr<const int[], null_deleter> p3(p1.get());
+ VERIFY( (p3 <=> p3) == 0 );
+ VERIFY( (p1 <=> p3) == 0 );
+ VERIFY( (p3 <=> p2) > 0 );
+ VERIFY( (p2 <=> p3) < 0 );
+
+ return true;
+}
+static_assert( test_3way() );
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/cons/constexpr_c++20.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/cons/constexpr_c++20.cc
new file mode 100644
index 00000000000..243d80aaba5
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/cons/constexpr_c++20.cc
@@ -0,0 +1,85 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <memory>
+
+#ifndef __cpp_lib_constexpr_memory
+# error "Feature test macro for constexpr unique_ptr is missing in <memory>"
+#elif __cpp_lib_constexpr_memory < 202202L
+# error "Feature test macro for constexpr unique_ptr has wrong value in <memory>"
+#endif
+
+#include <testsuite_hooks.h>
+
+constexpr bool
+test_default()
+{
+ std::unique_ptr<int> p;
+ std::unique_ptr<int> np(nullptr);
+ VERIFY( p == np );
+
+ std::unique_ptr<int[]> a;
+ std::unique_ptr<int[]> na(nullptr);
+ VERIFY( a == na );
+
+ return true;
+}
+static_assert( test_default() );
+
+constexpr bool
+test_ptr()
+{
+ std::unique_ptr<int> p(new int(2));
+ VERIFY( *p == 2 );
+ std::unique_ptr<int[]> a(new int[]{0, 1, 2});
+ VERIFY( a[2] == 2 );
+
+ return true;
+}
+static_assert( test_ptr() );
+
+constexpr bool
+test_del()
+{
+ const std::default_delete<int> pd;
+ std::unique_ptr<int> p1(new int(1), pd);
+ VERIFY( *p1 == 1 );
+ std::unique_ptr<int> p2(new int(2), std::default_delete<int>{});
+ VERIFY( *p2 == 2 );
+ const std::default_delete<int[]> ad;
+ std::unique_ptr<int[]> a1(new int[]{3, 4}, ad);
+ VERIFY( a1[0] == 3 );
+ std::unique_ptr<int[]> a2(new int[]{5, 6}, std::default_delete<int[]>{});
+ VERIFY( a2[1] == 6 );
+
+ return true;
+}
+static_assert( test_del() );
+
+constexpr bool
+test_move()
+{
+ std::unique_ptr<int> p1(new int(2));
+ std::unique_ptr<int> p2 = std::move(p1);
+ VERIFY( *p2 == 2 );
+ std::unique_ptr<int[]> a1(new int[]{0, 1, 2});
+ std::unique_ptr<int[]> a2 = std::move(a1);
+ VERIFY( a2[2] == 2 );
+
+ return true;
+}
+static_assert( test_move() );
+
+constexpr bool
+test_convert()
+{
+ std::unique_ptr<int> p1(new int(2));
+ std::unique_ptr<const int> p2 = std::move(p1);
+ VERIFY( *p2 == 2 );
+ std::unique_ptr<int[]> a1(new int[]{0, 1, 2});
+ std::unique_ptr<const int[]> a2 = std::move(a1);
+ VERIFY( a2[2] == 2 );
+
+ return true;
+}
+static_assert( test_convert() );
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/creation/constexpr.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/constexpr.cc
new file mode 100644
index 00000000000..90d11198578
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/constexpr.cc
@@ -0,0 +1,34 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+constexpr bool
+test_creation_single()
+{
+ std::unique_ptr<int> p = std::make_unique<int>(1);
+ VERIFY( *p == 1 );
+ p = std::make_unique_for_overwrite<int>();
+ *p = 2;
+ VERIFY( *p == 2 );
+
+ return true;
+}
+static_assert( test_creation_single() );
+
+constexpr bool
+test_creation_array()
+{
+ std::unique_ptr<int[]> a = std::make_unique<int[]>(2);
+ VERIFY( a[0] == 0 );
+ VERIFY( a[1] == 0 );
+ a = std::make_unique_for_overwrite<int[]>(2);
+ a[0] = 1;
+ a[1] = 2;
+ VERIFY( a[0] == 1 );
+ VERIFY( a[1] == 2 );
+
+ return true;
+}
+static_assert( test_creation_array() );
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/modifiers/constexpr.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/modifiers/constexpr.cc
new file mode 100644
index 00000000000..81908fdc081
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/modifiers/constexpr.cc
@@ -0,0 +1,68 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+constexpr bool
+test_release()
+{
+ std::unique_ptr<int> p1;
+ int* r = p1.release();
+ VERIFY( !r );
+ VERIFY( !p1 );
+
+ std::unique_ptr<int> p2(new int(2));
+ r = p2.release();
+ VERIFY( r );
+ VERIFY( !p2 );
+ delete r;
+
+ std::unique_ptr<int[]> a1;
+ r = a1.release();
+ VERIFY( !r );
+ VERIFY( !a1 );
+
+ std::unique_ptr<int[]> a2(new int[2]{});
+ r = a2.release();
+ VERIFY( r );
+ VERIFY( !a2 );
+ delete[] r;
+
+ return true;
+}
+static_assert( test_release() );
+
+constexpr bool
+test_reset()
+{
+ std::unique_ptr<int> p1;
+ p1.reset();
+ VERIFY( !p1 );
+ p1.reset(nullptr);
+ VERIFY( !p1 );
+ p1.reset(new int(2));
+ VERIFY( *p1 == 2 );
+ p1.reset(new int(3));
+ VERIFY( *p1 == 3 );
+ p1.reset(nullptr);
+ VERIFY( !p1 );
+
+ std::unique_ptr<int[]> a1;
+ a1.reset();
+ VERIFY( !a1 );
+ a1.reset(nullptr);
+ VERIFY( !a1 );
+ a1.reset(new int[]{2,3});
+ VERIFY( a1[0] == 2 );
+ a1.reset(new int[]{4,5,6});
+ VERIFY( a1[1] == 5 );
+ a1.reset(nullptr);
+ VERIFY( !a1 );
+
+ std::unique_ptr<const int[]> a2;
+ a2.reset(new int[2]{});
+
+ return true;
+}
+static_assert( test_reset() );
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/specialized_algorithms/constexpr.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/specialized_algorithms/constexpr.cc
new file mode 100644
index 00000000000..91a0165d212
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/specialized_algorithms/constexpr.cc
@@ -0,0 +1,46 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+constexpr bool
+test_swap_single()
+{
+ std::unique_ptr<int> p1;
+ swap(p1, p1);
+ VERIFY( !p1 );
+ std::unique_ptr<int> p2;
+ swap(p1, p2);
+ VERIFY( !p1 && !p2 );
+ std::unique_ptr<int> p3(new int(3));
+ swap(p3, p3);
+ VERIFY( *p3 == 3 );
+ swap(p1, p3);
+ VERIFY( *p1 == 3 );
+ std::unique_ptr<int> p4(new int(4));
+ swap(p4, p1);
+ VERIFY( *p4 == 3 );
+ VERIFY( *p1 == 4 );
+
+ return true;
+}
+static_assert( test_swap_single() );
+
+constexpr bool
+test_swap_array()
+{
+ std::unique_ptr<int[]> a1;
+ std::unique_ptr<int[]> a2;
+ swap(a1, a2);
+ VERIFY( !a1 && !a2 );
+ std::unique_ptr<int[]> a3(new int[]{3});
+ swap(a1, a3);
+ VERIFY( a1[0] == 3 );
+ std::unique_ptr<int[]> a4(new int[]{4, 5});
+ swap(a1, a4);
+ VERIFY( a1[1] == 5 );
+
+ return true;
+}
+static_assert( test_swap_array() );