]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/memory_resource/2.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / memory_resource / 2.cc
1 // { dg-options "-std=gnu++17" }
2 // { dg-do run { target c++17 } }
3 // { dg-skip-if "" { *-*-* } { -fno-aligned-new } }
4
5 // Copyright (C) 2018-2021 Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
21
22 #include <memory_resource>
23 #include <testsuite_allocator.h>
24
25 struct R : std::pmr::memory_resource {
26 void* do_allocate(std::size_t, std::size_t) override;
27 void do_deallocate(void*, std::size_t, std::size_t) override;
28 bool do_is_equal(const std::pmr::memory_resource&) const noexcept override;
29 };
30
31 bool called = false;
32
33 void* R::do_allocate(std::size_t bytes, std::size_t a)
34 {
35 called = true;
36 return ::operator new(bytes, std::align_val_t(a));
37 }
38
39 void R::do_deallocate(void* p, std::size_t bytes, std::size_t a)
40 {
41 called = true;
42 ::operator delete(p, bytes, std::align_val_t(a));
43 }
44
45 bool R::do_is_equal(const std::pmr::memory_resource& r) const noexcept
46 {
47 called = true;
48 return this == &r;
49 }
50
51 void
52 test01()
53 {
54 R res;
55 called = false;
56 auto p = res.allocate(1, 1);
57 VERIFY( called );
58 called = false;
59 res.deallocate(p, 1, 1);
60 VERIFY( called );
61 called = false;
62 VERIFY( res == res );
63 VERIFY( !called );
64 VERIFY( ! (res != res) );
65 VERIFY( !called );
66
67 struct X { int i = 0; };
68 struct MultipleInheritance : X, R { };
69 MultipleInheritance m;
70 VERIFY( m == m );
71 VERIFY( !called );
72 VERIFY( ! (m != m) );
73 VERIFY( !called );
74 VERIFY( m.is_equal(m) );
75 VERIFY( called );
76 called = false;
77 VERIFY( ! (m == res) );
78 VERIFY( called );
79 called = false;
80 VERIFY( m != res );
81 VERIFY( called );
82 called = false;
83 VERIFY( ! (res == m) );
84 VERIFY( called );
85 called = false;
86 VERIFY( res != m );
87 VERIFY( called );
88 called = false;
89 }
90
91 void
92 test02()
93 {
94 __gnu_test::memory_resource r1, r2;
95 VERIFY( r1 == r1 );
96 VERIFY( ! (r1 != r1) );
97 VERIFY( r1.is_equal(r1) );
98 VERIFY( r2 == r2 );
99 VERIFY( r2.is_equal(r2) );
100 VERIFY( ! (r1 == r2) );
101 VERIFY( r1 != r2 );
102 VERIFY( ! r1.is_equal(r2) );
103 VERIFY( ! (r2 == r1) );
104 VERIFY( r2 != r1 );
105 VERIFY( ! r2.is_equal(r1) );
106 }
107
108 int main()
109 {
110 test01();
111 test02();
112 }