Add missing dg-require-cstdint directives to tests
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / monotonic_buffer_resource / allocate.cc
blob412150944cd9a52cc84a4eeec99e377a283a4edb
1 // Copyright (C) 2018 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-cstdint "" }
22 #include <memory_resource>
23 #include <testsuite_allocator.h>
25 void
26 test01()
28 __gnu_test::memory_resource r;
30 // test that it's possible to allocate after each of the constructors
32 std::pmr::monotonic_buffer_resource mr(&r);
33 auto p = mr.allocate(1024);
34 VERIFY( p != nullptr );
35 auto q = mr.allocate(1024);
36 VERIFY( q != nullptr );
37 VERIFY( p != q );
39 VERIFY( r.number_of_active_allocations() == 0 );
41 std::pmr::monotonic_buffer_resource mr(128, &r);
42 auto p = mr.allocate(1024);
43 VERIFY( p != nullptr );
44 auto q = mr.allocate(1024);
45 VERIFY( q != nullptr );
46 VERIFY( p != q );
48 VERIFY( r.number_of_active_allocations() == 0 );
50 unsigned char buf[64];
51 std::pmr::monotonic_buffer_resource mr((void*)buf, sizeof(buf), &r);
52 auto p = mr.allocate(1024);
53 VERIFY( p != nullptr );
54 auto q = mr.allocate(1024);
55 VERIFY( q != nullptr );
56 VERIFY( p != q );
58 VERIFY( r.number_of_active_allocations() == 0 );
60 std::pmr::monotonic_buffer_resource mr;
61 auto p = mr.allocate(1024);
62 VERIFY( p != nullptr );
63 auto q = mr.allocate(1024);
64 VERIFY( q != nullptr );
65 VERIFY( p != q );
68 std::pmr::monotonic_buffer_resource mr(64);
69 auto p = mr.allocate(1024);
70 VERIFY( p != nullptr );
71 auto q = mr.allocate(1024);
72 VERIFY( q != nullptr );
73 VERIFY( p != q );
76 unsigned char buf[64];
77 std::pmr::monotonic_buffer_resource mr((void*)buf, sizeof(buf));
78 auto p = mr.allocate(1024);
79 VERIFY( p != nullptr );
80 auto q = mr.allocate(1024);
81 VERIFY( q != nullptr );
82 VERIFY( p != q );
86 void
87 test02()
89 unsigned char buf[64];
90 std::pmr::monotonic_buffer_resource mr(buf, sizeof(buf));
92 auto p = mr.allocate(0);
93 VERIFY( p != nullptr );
94 auto q = mr.allocate(0);
95 VERIFY( q != nullptr );
96 VERIFY( p != q );
98 p = mr.allocate(0, 1);
99 VERIFY( p != nullptr );
100 q = mr.allocate(0, 1);
101 VERIFY( q != nullptr );
102 VERIFY( p != q );
105 void
106 test03()
108 #if __cpp_exceptions
110 std::pmr::monotonic_buffer_resource mr(std::pmr::null_memory_resource());
111 bool caught = false;
114 (void) mr.allocate(1, 1);
116 catch (const std::bad_alloc&)
118 caught = true;
120 VERIFY( caught );
123 unsigned char buf[16];
124 std::pmr::monotonic_buffer_resource mr(buf, sizeof(buf),
125 std::pmr::null_memory_resource());
126 (void) mr.allocate(16, 1);
127 bool caught = false;
130 (void) mr.allocate(1, 1);
132 catch (const std::bad_alloc&)
134 caught = true;
136 VERIFY( caught );
138 #endif
141 void
142 test04()
144 auto buf = new unsigned char[512];
145 std::pmr::monotonic_buffer_resource mr(buf, 512,
146 std::pmr::null_memory_resource());
147 std::size_t prev_size = 1;
148 void* prev_ptr = mr.allocate(prev_size, 1);
149 for (int i = 0; i < 9; ++i)
151 std::size_t size = 1 << i;
152 void* ptr = mr.allocate(size, 1);
153 VERIFY( ((char*)ptr - (char*)prev_ptr) == prev_size );
154 prev_ptr = ptr;
155 prev_size = size;
159 void
160 test05()
162 // test that returned pointer is correctly aligned
163 auto is_aligned = [](void* p, size_t alignment) -> bool {
164 return (reinterpret_cast<std::uintptr_t>(p) % alignment) == 0;
167 auto buf = new unsigned char[2048];
168 std::pmr::monotonic_buffer_resource mr(buf+1, 2047);
169 for (int i = 0; i < 9; ++i)
171 auto p = mr.allocate(1, 1 << i);
172 VERIFY( is_aligned(p, 1 << i) );
173 // Make next available byte misaligned:
174 (void) mr.allocate(1 << i, 1);
178 void
179 test06()
181 // check for geometric progression in buffer sizes from upstream
183 struct resource : __gnu_test::memory_resource
185 bool allocated = false;
186 std::size_t last_size = 0;
188 void*
189 do_allocate(size_t bytes, size_t align) override
191 allocated = true;
192 last_size = bytes;
193 return __gnu_test::memory_resource::do_allocate(bytes, align);
197 resource r;
198 std::pmr::monotonic_buffer_resource mr(32, &r);
199 std::size_t last_size = 0;
201 for (int i = 0; i < 100; ++i)
203 (void) mr.allocate(16);
204 if (r.allocated)
206 VERIFY(r.last_size >= last_size);
207 last_size = r.last_size;
208 r.allocated = false;
214 main()
216 test01();
217 test02();
218 test03();
219 test04();
220 test05();
221 test06();