1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
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
12
13 // <memory_resource>
14
15 // memory_resource *null_memory_resource()
16
17 #include <memory_resource>
18 #include <cassert>
19 #include <cstddef> // size_t
20 #include <new>
21 #include <type_traits>
22
23 #include "count_new.h"
24 #include "test_macros.h"
25
26 struct assert_on_compare : public std::pmr::memory_resource {
do_allocateassert_on_compare27 void* do_allocate(std::size_t, size_t) override {
28 assert(false);
29 return nullptr;
30 }
31
do_deallocateassert_on_compare32 void do_deallocate(void*, std::size_t, size_t) override { assert(false); }
33
do_is_equalassert_on_compare34 bool do_is_equal(const std::pmr::memory_resource&) const noexcept override {
35 assert(false);
36 return true;
37 }
38 };
39
test_return()40 void test_return() {
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()); }
46 }
47
test_equality()48 void test_equality() {
49 // Same object
50 {
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
54 assert(&r1 == &r2);
55 // check for proper equality semantics
56 assert(r1 == r2);
57 assert(r2 == r1);
58 assert(!(r1 != r2));
59 assert(!(r2 != r1));
60 // check the is_equal method
61 assert(r1.is_equal(r2));
62 assert(r2.is_equal(r1));
63 }
64 // Different types
65 {
66 std::pmr::memory_resource& r1 = *std::pmr::null_memory_resource();
67 assert_on_compare c;
68 std::pmr::memory_resource& r2 = c;
69 assert(r1 != r2);
70 assert(!(r1 == r2));
71 assert(!r1.is_equal(r2));
72 }
73 }
74
test_allocate()75 void test_allocate() {
76 #ifndef TEST_HAS_NO_EXCEPTIONS
77 DisableAllocationGuard g; // null_memory_resource shouldn't allocate.
78 try {
79 (void)std::pmr::null_memory_resource()->allocate(1);
80 assert(false);
81 } catch (std::bad_alloc const&) {
82 // do nothing
83 } catch (...) {
84 assert(false);
85 }
86 #endif
87 }
88
test_deallocate()89 void test_deallocate() {
90 globalMemCounter.reset();
91
92 int x = 42;
93 std::pmr::null_memory_resource()->deallocate(&x, 0);
94
95 assert(globalMemCounter.checkDeleteCalledEq(0));
96 assert(globalMemCounter.checkDeleteArrayCalledEq(0));
97 }
98
main(int,char **)99 int main(int, char**) {
100 test_return();
101 test_equality();
102 test_allocate();
103 test_deallocate();
104
105 return 0;
106 }
107