1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
10 // TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed
11 // UNSUPPORTED: availability-pmr-missing
15 // memory_resource *null_memory_resource()
17 #include <memory_resource>
19 #include <cstddef> // size_t
21 #include <type_traits>
23 #include "count_new.h"
24 #include "test_macros.h"
26 struct assert_on_compare
: public std::pmr::memory_resource
{
27 void* do_allocate(std::size_t, size_t) override
{
32 void do_deallocate(void*, std::size_t, size_t) override
{ assert(false); }
34 bool do_is_equal(const std::pmr::memory_resource
&) const noexcept override
{
41 { ASSERT_SAME_TYPE(decltype(std::pmr::null_memory_resource()), std::pmr::memory_resource
*); }
42 // Test that the returned value is not null
43 { assert(std::pmr::null_memory_resource()); }
44 // Test the same value is returned by repeated calls.
45 { assert(std::pmr::null_memory_resource() == std::pmr::null_memory_resource()); }
48 void test_equality() {
51 std::pmr::memory_resource
& r1
= *std::pmr::null_memory_resource();
52 std::pmr::memory_resource
& r2
= *std::pmr::null_memory_resource();
53 // check both calls returned the same object
55 // check for proper equality semantics
60 // check the is_equal method
61 assert(r1
.is_equal(r2
));
62 assert(r2
.is_equal(r1
));
66 std::pmr::memory_resource
& r1
= *std::pmr::null_memory_resource();
68 std::pmr::memory_resource
& r2
= c
;
71 assert(!r1
.is_equal(r2
));
75 void test_allocate() {
76 #ifndef TEST_HAS_NO_EXCEPTIONS
77 DisableAllocationGuard g
; // null_memory_resource shouldn't allocate.
79 (void)std::pmr::null_memory_resource()->allocate(1);
81 } catch (std::bad_alloc
const&) {
89 void test_deallocate() {
90 globalMemCounter
.reset();
93 std::pmr::null_memory_resource()->deallocate(&x
, 0);
95 assert(globalMemCounter
.checkDeleteCalledEq(0));
96 assert(globalMemCounter
.checkDeleteArrayCalledEq(0));
99 int main(int, char**) {