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
10 
11 // Aligned allocation is required by std::experimental::pmr, but it was not provided
12 // before macosx10.13 and as a result we get linker errors when deploying to older than
13 // macosx10.13.
14 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}}
15 
16 // <experimental/memory_resource>
17 
18 // memory_resource * null_memory_resource()
19 
20 #include <experimental/memory_resource>
21 #include <new>
22 #include <type_traits>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 #include "count_new.h"
27 
28 namespace ex = std::experimental::pmr;
29 
30 struct assert_on_compare : public ex::memory_resource
31 {
32 protected:
do_allocateassert_on_compare33     void * do_allocate(size_t, size_t) override
34     { assert(false); return nullptr; }
35 
do_deallocateassert_on_compare36     void do_deallocate(void *, size_t, size_t) override
37     { assert(false); }
38 
do_is_equalassert_on_compare39     bool do_is_equal(ex::memory_resource const &) const noexcept override
40     { assert(false); return false; }
41 };
42 
test_return()43 void test_return()
44 {
45     {
46         static_assert(std::is_same<
47             decltype(ex::null_memory_resource()), ex::memory_resource*
48           >::value, "");
49     }
50     // Test that the returned value is not null
51     {
52         assert(ex::null_memory_resource());
53     }
54     // Test the same value is returned by repeated calls.
55     {
56         assert(ex::null_memory_resource() == ex::null_memory_resource());
57     }
58 }
59 
test_equality()60 void test_equality()
61 {
62     // Same object
63     {
64         ex::memory_resource & r1 = *ex::null_memory_resource();
65         ex::memory_resource & r2 = *ex::null_memory_resource();
66         // check both calls returned the same object
67         assert(&r1 == &r2);
68         // check for proper equality semantics
69         assert(r1 == r2);
70         assert(r2 == r1);
71         assert(!(r1 != r2));
72         assert(!(r2 != r1));
73         // check the is_equal method
74         assert(r1.is_equal(r2));
75         assert(r2.is_equal(r1));
76     }
77     // Different types
78     {
79         ex::memory_resource & r1 = *ex::null_memory_resource();
80         assert_on_compare c;
81         ex::memory_resource & r2 = c;
82         assert(r1 != r2);
83         assert(!(r1 == r2));
84         assert(!r1.is_equal(r2));
85     }
86 }
87 
test_allocate()88 void test_allocate()
89 {
90 #ifndef TEST_HAS_NO_EXCEPTIONS
91     DisableAllocationGuard g; // null_memory_resource shouldn't allocate.
92     try {
93         ex::null_memory_resource()->allocate(1);
94         assert(false);
95     } catch (std::bad_alloc const &) {
96        // do nothing
97     } catch (...) {
98         assert(false);
99     }
100 #endif
101 }
102 
test_deallocate()103 void test_deallocate()
104 {
105     globalMemCounter.reset();
106 
107     int x = 42;
108     ex::null_memory_resource()->deallocate(nullptr, 0);
109     ex::null_memory_resource()->deallocate(&x, 0);
110 
111     assert(globalMemCounter.checkDeleteCalledEq(0));
112     assert(globalMemCounter.checkDeleteArrayCalledEq(0));
113 }
114 
main(int,char **)115 int main(int, char**)
116 {
117     test_return();
118     test_equality();
119     test_allocate();
120     test_deallocate();
121 
122   return 0;
123 }
124