]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory_resource/resource_adaptor.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / memory_resource / resource_adaptor.cc
1 // { dg-do run { target c++14 } }
2
3 // Copyright (C) 2016-2018 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 #include <experimental/memory_resource>
21 #include <testsuite_hooks.h>
22 #include <testsuite_allocator.h>
23
24 using std::experimental::pmr::memory_resource;
25 using std::experimental::pmr::resource_adaptor;
26
27 template<typename T>
28 struct Allocator : __gnu_test::SimpleAllocator<T>
29 {
30 Allocator(int) { } // not default constructible
31
32 template<typename U>
33 Allocator(const Allocator<U>&) { }
34 };
35
36 template<typename T>
37 bool aligned(void* p)
38 {
39 return (reinterpret_cast<std::uintptr_t>(p) % alignof(T)) == 0;
40 }
41
42 // resource_adaptor
43 void
44 test05()
45 {
46 using std::max_align_t;
47 using std::uintptr_t;
48 void* p = nullptr;
49
50 Allocator<int> a1(1), a2(2); // minimal interface allocators
51 resource_adaptor<decltype(a1)> r1(a1), r2(a2);
52 VERIFY( r1 == r1 );
53 VERIFY( r1 == r2 );
54 p = r1.allocate(1);
55 VERIFY( aligned<max_align_t>(p) );
56 r1.deallocate(p, 1);
57 p = r1.allocate(1, alignof(short));
58 VERIFY( aligned<short>(p) );
59 r1.deallocate(p, 1, alignof(short));
60 p = r1.allocate(1, alignof(long));
61 VERIFY( aligned<long>(p) );
62 r1.deallocate(p, 1, alignof(long));
63
64 __gnu_test::uneq_allocator<double> a3(3), a4(4); // non-equal allocators
65 resource_adaptor<decltype(a3)> r3(a3), r4(a4);
66 VERIFY( r3 == r3 );
67 VERIFY( r4 == r4 );
68 VERIFY( r3 != r4 );
69 p = r3.allocate(1);
70 VERIFY( aligned<max_align_t>(p) );
71 r3.deallocate(p, 1);
72 p = r3.allocate(1, alignof(short));
73 VERIFY( aligned<short>(p) );
74 r3.deallocate(p, 1, alignof(short));
75 p = r3.allocate(1, alignof(long));
76 VERIFY( aligned<long>(p) );
77 r3.deallocate(p, 1, alignof(long));
78
79 // TODO test with an allocator that doesn't use new or malloc, so
80 // returns pointers that are not suitably aligned for any type.
81 }
82
83 int main()
84 {
85 test05();
86 }