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 * new_delete_resource()
19 
20 #include <experimental/memory_resource>
21 #include <type_traits>
22 #include <cassert>
23 
24 #include "count_new.h"
25 
26 #include "test_macros.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::new_delete_resource()), ex::memory_resource*
48           >::value, "");
49     }
50     // assert not null
51     {
52         assert(ex::new_delete_resource());
53     }
54     // assert same return value
55     {
56         assert(ex::new_delete_resource() == ex::new_delete_resource());
57     }
58 }
59 
test_equality()60 void test_equality()
61 {
62     // Same object
63     {
64         ex::memory_resource & r1 = *ex::new_delete_resource();
65         ex::memory_resource & r2 = *ex::new_delete_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     }
74     // Different types
75     {
76         ex::memory_resource & r1 = *ex::new_delete_resource();
77         assert_on_compare c;
78         ex::memory_resource & r2 = c;
79         assert(r1 != r2);
80         assert(!(r1 == r2));
81     }
82 }
83 
test_allocate_deallocate()84 void test_allocate_deallocate()
85 {
86     ex::memory_resource & r1 = *ex::new_delete_resource();
87 
88     globalMemCounter.reset();
89 
90     void *ret = r1.allocate(50);
91     assert(ret);
92     assert(globalMemCounter.checkOutstandingNewEq(1));
93     assert(globalMemCounter.checkLastNewSizeEq(50));
94 
95     r1.deallocate(ret, 1);
96     assert(globalMemCounter.checkOutstandingNewEq(0));
97     assert(globalMemCounter.checkDeleteCalledEq(1));
98 
99 }
100 
main(int,char **)101 int main(int, char**)
102 {
103     static_assert(noexcept(ex::new_delete_resource()), "Must be noexcept");
104     test_return();
105     test_equality();
106     test_allocate_deallocate();
107 
108   return 0;
109 }
110