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