aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/experimental/type_erased_allocator
diff options
context:
space:
mode:
Diffstat (limited to 'libstdc++-v3/testsuite/experimental/type_erased_allocator')
-rw-r--r--libstdc++-v3/testsuite/experimental/type_erased_allocator/1.cc147
-rw-r--r--libstdc++-v3/testsuite/experimental/type_erased_allocator/1_neg.cc37
-rw-r--r--libstdc++-v3/testsuite/experimental/type_erased_allocator/2.cc202
-rw-r--r--libstdc++-v3/testsuite/experimental/type_erased_allocator/uses_allocator.cc22
4 files changed, 408 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/experimental/type_erased_allocator/1.cc b/libstdc++-v3/testsuite/experimental/type_erased_allocator/1.cc
new file mode 100644
index 00000000000..9545edff54d
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/type_erased_allocator/1.cc
@@ -0,0 +1,147 @@
+// { dg-options "-std=gnu++14" }
+
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <memory>
+#include <experimental/memory_resource>
+#include <vector>
+#include <bits/uses_allocator.h>
+#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
+
+using std::experimental::pmr::polymorphic_allocator;
+using std::experimental::pmr::memory_resource;
+using std::experimental::pmr::new_delete_resource;
+using std::experimental::pmr::get_default_resource;
+using std::experimental::pmr::set_default_resource;
+
+struct A
+{
+ A() { ++ctor_count; }
+ ~A() { ++dtor_count; }
+ static int ctor_count;
+ static int dtor_count;
+};
+int A::ctor_count = 0;
+int A::dtor_count = 0;
+
+struct CountedResource : public memory_resource
+{
+public:
+ CountedResource() = default;
+ ~ CountedResource() = default;
+
+ static size_t get_alloc_count() { return alloc_count; }
+ static size_t get_dalloc_count() { return dalloc_count; }
+
+ static size_t alloc_count;
+ static size_t dalloc_count;
+protected:
+ void* do_allocate(size_t bytes, size_t alignment)
+ {
+ alloc_count += bytes;
+ if (auto ptr = std::malloc(bytes)) {
+ return ptr;
+ }
+ throw std::bad_alloc();
+ }
+
+ void do_deallocate(void *p, size_t bytes, size_t alignment)
+ {
+ dalloc_count += bytes;
+ free(p);
+ }
+
+ bool do_is_equal(const memory_resource& __other) const noexcept
+ { return this == &__other; }
+};
+ size_t CountedResource::alloc_count = 0;
+ size_t CountedResource::dalloc_count = 0;
+
+void clear()
+{
+ CountedResource::alloc_count = 0;
+ CountedResource::dalloc_count = 0;
+ A::ctor_count = 0;
+ A::dtor_count = 0;
+}
+
+// memory resource
+void test01()
+{
+ memory_resource* r = new_delete_resource();
+ VERIFY(get_default_resource() == r);
+ void *p = get_default_resource()->allocate(5);
+ VERIFY(p);
+ get_default_resource()->deallocate(p, 5);
+
+ clear();
+ CountedResource* cr = new CountedResource();
+ set_default_resource(cr);
+ VERIFY(get_default_resource() == cr);
+ void *pc = get_default_resource()->allocate(5);
+ VERIFY(pc);
+ get_default_resource()->deallocate(pc, 5);
+ VERIFY(CountedResource::get_alloc_count() == 5);
+ VERIFY(CountedResource::get_dalloc_count() == 5);
+}
+
+// polymorphic_allocator
+void test02()
+{
+ clear();
+ {
+ CountedResource cr;
+ polymorphic_allocator<A> pa(&cr);
+ std::vector<A, polymorphic_allocator<A>> v(5, A(), pa);
+ }
+ VERIFY(A::ctor_count == 1);
+ VERIFY(A::dtor_count == 6);
+ VERIFY(CountedResource::get_alloc_count() == 5);
+ VERIFY(CountedResource::get_dalloc_count() == 5);
+}
+
+void test03() {
+ clear();
+ CountedResource cr;
+ polymorphic_allocator<A> pa(&cr);
+ A* p = pa.allocate(1);
+ pa.construct(p);
+ pa.destroy(p);
+ pa.deallocate(p, 1);
+ VERIFY(A::ctor_count == 1);
+ VERIFY(A::dtor_count == 1);
+ VERIFY(CountedResource::get_alloc_count() == 1);
+ VERIFY(CountedResource::get_dalloc_count() == 1);
+}
+
+void test04() {
+ polymorphic_allocator<A> pa1(get_default_resource());
+ polymorphic_allocator<A> pa2(get_default_resource());
+ VERIFY(pa1 == pa2);
+ polymorphic_allocator<A> pa3 = pa2.select_on_container_copy_construction();
+ VERIFY(pa1 == pa3);
+}
+
+int main() {
+ test01();
+ test02();
+ test03();
+ test04();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/experimental/type_erased_allocator/1_neg.cc b/libstdc++-v3/testsuite/experimental/type_erased_allocator/1_neg.cc
new file mode 100644
index 00000000000..b85e0ea469f
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/type_erased_allocator/1_neg.cc
@@ -0,0 +1,37 @@
+// { dg-do run { xfail *-*-* } }
+// { dg-options "-std=gnu++14" }
+
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <experimental/memory_resource>
+#include <bits/uses_allocator.h>
+#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
+
+using std::experimental::pmr::polymorphic_allocator;
+using std::experimental::pmr::null_memory_resource;
+using std::experimental::pmr::memory_resource;
+
+void test01() {
+ memory_resource* r = null_memory_resource();
+ auto p = r->allocate(1);
+}
+
+int main() {
+ test01();
+}
diff --git a/libstdc++-v3/testsuite/experimental/type_erased_allocator/2.cc b/libstdc++-v3/testsuite/experimental/type_erased_allocator/2.cc
new file mode 100644
index 00000000000..014c357e5fa
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/type_erased_allocator/2.cc
@@ -0,0 +1,202 @@
+// { dg-options "-std=gnu++14" }
+
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <memory>
+#include <experimental/memory_resource>
+#include <experimental/utility>
+#include <bits/uses_allocator.h>
+#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
+
+using std::experimental::pmr::polymorphic_allocator;
+using std::experimental::pmr::memory_resource;
+using std::experimental::pmr::new_delete_resource;
+using std::experimental::pmr::get_default_resource;
+using std::experimental::pmr::set_default_resource;
+using std::allocator_arg_t;
+
+enum CtorType { Default, Copy, Move, Other, Tuple, Piecewise_Default, Piecewise_Copy};
+
+// type that takes a memory_resource before other ctor args
+struct A
+{
+ using allocator_type = std::experimental::erased_type;
+
+ CtorType type;
+ memory_resource* alloc = nullptr;
+
+ A() : type(Default) { }
+ A(allocator_arg_t, memory_resource* a) : type(Default), alloc(a) { }
+ A(const A&) : type(Copy) { }
+ A(allocator_arg_t, memory_resource* a, const A&) : type(Copy), alloc(a) { }
+ A(A&&) : type (Move) { }
+ A(allocator_arg_t, memory_resource* a, A&&) : type (Move), alloc(a) { }
+ A(int) : type(Other) { }
+ A(allocator_arg_t, memory_resource* a, int) : type(Other), alloc(a) { }
+};
+
+// type that takes a memory_resource after other ctor args
+struct B
+{
+ using allocator_type = std::experimental::erased_type;
+
+ CtorType type;
+ memory_resource* alloc = nullptr;
+
+ B() : type(Default) { }
+ B(memory_resource* a) : type(Default), alloc(a) { }
+ B(const B&) : type(Copy) { }
+ B(const B&, memory_resource* a) : type(Copy), alloc(a) { }
+ B(B&&) : type (Move) { }
+ B(B&&, memory_resource* a) : type(Move), alloc(a) { }
+ B(int) : type(Other) { }
+ B(int, memory_resource* a) : type(Other), alloc(a) { }
+};
+
+// type that takes no memory_resource
+struct C
+{
+ CtorType type;
+ C() : type(Default) { }
+ C(const C&) : type(Copy) { }
+ C(C&&) : type(Move) { }
+ C(int) : type(Other) { }
+};
+
+// test construct for type that
+// uses memory_resource* as allocator
+template<typename A>
+void test_uses_alloc() {
+ polymorphic_allocator<A> pa;
+ A* p = pa.allocate(1);
+ A a;
+
+ pa.construct(p);
+ VERIFY(p->alloc == get_default_resource());
+ VERIFY(p->type == Default);
+ pa.destroy(p);
+
+ pa.construct(p, a);
+ VERIFY(p->type == Copy);
+ pa.destroy(p);
+
+ pa.construct(p, A());
+ VERIFY(p->type == Move);
+ pa.destroy(p);
+
+ pa.construct(p, 1);
+ VERIFY(p->type == Other);
+ pa.destroy(p);
+
+ pa.deallocate(p, 1);
+}
+
+// test construct for type that not using allocator
+template <typename C>
+void test_non_alloc() {
+ polymorphic_allocator<C> pa;
+ C* p = pa.allocate(1);
+ C b;
+
+ pa.construct(p);
+ VERIFY(p->type == Default);
+ pa.destroy(p);
+
+ pa.construct(p, b);
+ VERIFY(p->type == Copy);
+ pa.destroy(p);
+
+ pa.construct(p, C());
+ VERIFY(p->type == Move);
+ pa.destroy(p);
+
+ pa.construct(p, 1);
+ VERIFY(p->type == Other);
+ pa.destroy(p);
+
+ pa.deallocate(p, 1);
+}
+
+// test piecewise_construct
+template <typename A, typename B>
+void test_pair() {
+ polymorphic_allocator<std::pair<A, B>> pa;
+ std::pair<A, B>* p = pa.allocate(1);
+ std::tuple<> t;
+
+ // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>)
+ pa.construct(p, std::piecewise_construct, t, t);
+ VERIFY(p->first.type == Default);
+ VERIFY(p->second.type == Default);
+ pa.destroy(p);
+
+ // construct(pair<T1, T2>* __p)
+ pa.construct(p);
+ VERIFY(p->first.type == Default);
+ VERIFY(p->second.type == Default);
+ pa.destroy(p);
+
+ // construct(pair<T1, T2>* p, U&& x, V&& y)
+ A a; B b;
+ pa.construct(p, a, b);
+ VERIFY(p->first.type == Copy);
+ VERIFY(p->second.type == Copy);
+ pa.destroy(p);
+
+ pa.construct(p, A(), B());
+ VERIFY(p->first.type == Move);
+ VERIFY(p->second.type == Move);
+ auto pp = *p;
+ pa.destroy(p);
+
+ // construct(pair<T1, T2>* p, const pair<U, V>& x)
+ pa.construct(p, pp);
+ VERIFY(p->first.type == Copy);
+ VERIFY(p->second.type == Copy);
+ pa.destroy(p);
+
+ // construct(pair<T1, T2>* p, pair<U, V>&& x)
+ pa.construct(p, std::move(pp));
+ VERIFY(p->first.type == Move);
+ VERIFY(p->second.type == Move);
+ pa.destroy(p);
+ pa.deallocate(p, 1);
+}
+
+void test01() {
+ test_uses_alloc<A>();
+ test_uses_alloc<B>();
+ test_non_alloc<C>();
+}
+
+void test02() {
+ test_pair<A, A>();
+ test_pair<A, B>();
+ test_pair<A, C>();
+ test_pair<B, B>();
+ test_pair<B, A>();
+ test_pair<B, C>();
+ test_pair<C, C>();
+}
+
+
+int main() {
+ test01();
+ test02();
+}
diff --git a/libstdc++-v3/testsuite/experimental/type_erased_allocator/uses_allocator.cc b/libstdc++-v3/testsuite/experimental/type_erased_allocator/uses_allocator.cc
new file mode 100644
index 00000000000..fc8acf16dcd
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/type_erased_allocator/uses_allocator.cc
@@ -0,0 +1,22 @@
+#include <bits/uses_allocator.h>
+#include <vector>
+#include <experimental/utility>
+#include <memory>
+
+using std::vector;
+using std::allocator;
+using std::uses_allocator;
+
+struct A {
+ using allocator_type = std::experimental::erased_type;
+};
+
+void test01() {
+ static_assert(uses_allocator<vector<int>, allocator<int>>());
+ static_assert(uses_allocator<A, allocator<A>>());
+}
+
+int main() {
+ test01();
+ return 0;
+}