Add missing dg-require-cstdint directives to tests
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / monotonic_buffer_resource / deallocate.cc
blobdb835935079bf8a7cc1627aff7ca2ed7450d4e8e
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 struct resource : __gnu_test::memory_resource
27 int allocate_calls = 0;
28 int deallocate_calls = 0;
30 void*
31 do_allocate(std::size_t bytes, std::size_t align) override
33 ++allocate_calls;
34 return __gnu_test::memory_resource::do_allocate(bytes, align);
37 void
38 do_deallocate(void* p, std::size_t bytes, std::size_t align) override
40 ++deallocate_calls;
41 __gnu_test::memory_resource::do_deallocate(p, bytes, align);
45 void
46 test01()
48 resource r;
50 // test that it's possible to deallocate after each of the constructors
52 std::pmr::monotonic_buffer_resource mr(&r);
53 auto p = mr.allocate(1024);
54 VERIFY( p != nullptr );
55 const std::uintptr_t pi = reinterpret_cast<std::uintptr_t>(p);
56 mr.deallocate(p, 1024);
57 VERIFY( r.deallocate_calls == 0 );
58 auto q = mr.allocate(1024);
59 VERIFY( q != nullptr );
60 VERIFY( pi != reinterpret_cast<std::uintptr_t>(q) );
61 mr.deallocate(q, 1024);
62 VERIFY( r.deallocate_calls == 0 );
64 VERIFY( r.deallocate_calls == r.allocate_calls );
65 VERIFY( r.number_of_active_allocations() == 0 );
67 r.deallocate_calls = r.allocate_calls = 0;
68 std::pmr::monotonic_buffer_resource mr(128, &r);
69 auto p = mr.allocate(64);
70 VERIFY( p != nullptr );
71 const std::uintptr_t pi = reinterpret_cast<std::uintptr_t>(p);
72 mr.deallocate(p, 64);
73 auto q = mr.allocate(1024);
74 VERIFY( q != nullptr );
75 VERIFY( p != q );
76 VERIFY( pi != reinterpret_cast<std::uintptr_t>(q) );
77 mr.deallocate(q, 1024);
78 VERIFY( r.deallocate_calls == 0 );
80 VERIFY( r.number_of_active_allocations() == 0 );
82 r.deallocate_calls = r.allocate_calls = 0;
83 unsigned char buf[64];
84 std::pmr::monotonic_buffer_resource mr((void*)buf, sizeof(buf), &r);
85 auto p = mr.allocate(64);
86 VERIFY( p != nullptr );
87 const std::uintptr_t pi = reinterpret_cast<std::uintptr_t>(p);
88 mr.deallocate(p, 64);
89 auto q = mr.allocate(1024);
90 VERIFY( q != nullptr );
91 VERIFY( p != q );
92 VERIFY( pi != reinterpret_cast<std::uintptr_t>(q) );
93 mr.deallocate(q, 1024);
94 VERIFY( r.deallocate_calls == 0 );
96 VERIFY( r.deallocate_calls == r.allocate_calls );
97 VERIFY( r.number_of_active_allocations() == 0 );
101 main()
103 test01();