1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // REQUIRES: c++experimental
11 // UNSUPPORTED: c++98, c++03
12 
13 // <experimental/memory_resource>
14 
15 //-----------------------------------------------------------------------------
16 // TESTING memory_resource * get_default_resource() noexcept;
17 //         memory_resource * set_default_resource(memory_resource*) noexcept;
18 //
19 // Concerns:
20 //  A) 'get_default_resource()' returns a non-null memory_resource pointer.
21 //  B) 'get_default_resource()' returns the value set by the last call to
22 //     'set_default_resource(...)' and 'new_delete_resource()' if no call
23 //     to 'set_default_resource(...)' has occurred.
24 //  C) 'set_default_resource(...)' returns the previous value of the default
25 //     resource.
26 //  D) 'set_default_resource(T* p)' for a non-null 'p' sets the default resource
27 //     to be 'p'.
28 //  E) 'set_default_resource(null)' sets the default resource to
29 //     'new_delete_resource()'.
30 //  F) 'get_default_resource' and 'set_default_resource' are noexcept.
31 
32 
33 #include <experimental/memory_resource>
34 #include <cassert>
35 
36 #include "test_memory_resource.hpp"
37 
38 using namespace std::experimental::pmr;
39 
main()40 int main() {
41     TestResource R;
42     { // Test (A) and (B)
43         memory_resource* p = get_default_resource();
44         assert(p != nullptr);
45         assert(p == new_delete_resource());
46         assert(p == get_default_resource());
47     }
48     { // Test (C) and (D)
49         memory_resource *expect = &R;
50         memory_resource *old = set_default_resource(expect);
51         assert(old != nullptr);
52         assert(old == new_delete_resource());
53 
54         memory_resource *p = get_default_resource();
55         assert(p != nullptr);
56         assert(p == expect);
57         assert(p == get_default_resource());
58     }
59     { // Test (E)
60         memory_resource* old = set_default_resource(nullptr);
61         assert(old == &R);
62         memory_resource* p = get_default_resource();
63         assert(p != nullptr);
64         assert(p == new_delete_resource());
65         assert(p == get_default_resource());
66     }
67     { // Test (F)
68         static_assert(noexcept(get_default_resource()),
69                       "get_default_resource() must be noexcept");
70 
71         static_assert(noexcept(set_default_resource(nullptr)),
72                       "set_default_resource() must be noexcept");
73     }
74 }
75