xref: /llvm-project/libcxx/test/std/utilities/utility/mem.res/mem.res.global/new_delete_resource.pass.cpp (revision b8efe370497a6cfe83185ab7e57124ed5f5b125f)
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 *new_delete_resource()
16 
17 #include <memory_resource>
18 #include <cassert>
19 #include <type_traits>
20 
21 #include "count_new.h"
22 #include "test_macros.h"
23 
24 class assert_on_compare : public std::pmr::memory_resource {
do_allocate(std::size_t,size_t)25   void* do_allocate(std::size_t, size_t) override {
26     assert(false);
27     return nullptr;
28   }
29 
do_deallocate(void *,std::size_t,size_t)30   void do_deallocate(void*, std::size_t, size_t) override { assert(false); }
31 
do_is_equal(const std::pmr::memory_resource &) const32   bool do_is_equal(const std::pmr::memory_resource&) const noexcept override {
33     assert(false);
34     return true;
35   }
36 };
37 
test_return()38 void test_return() {
39   { ASSERT_SAME_TYPE(decltype(std::pmr::new_delete_resource()), std::pmr::memory_resource*); }
40   // assert not null
41   { assert(std::pmr::new_delete_resource()); }
42   // assert same return value
43   { assert(std::pmr::new_delete_resource() == std::pmr::new_delete_resource()); }
44 }
45 
test_equality()46 void test_equality() {
47   // Same object
48   {
49     std::pmr::memory_resource& r1 = *std::pmr::new_delete_resource();
50     std::pmr::memory_resource& r2 = *std::pmr::new_delete_resource();
51     // check both calls returned the same object
52     assert(&r1 == &r2);
53     // check for proper equality semantics
54     assert(r1 == r2);
55     assert(r2 == r1);
56     assert(!(r1 != r2));
57     assert(!(r2 != r1));
58   }
59   // Different types
60   {
61     std::pmr::memory_resource& r1 = *std::pmr::new_delete_resource();
62     assert_on_compare c;
63     std::pmr::memory_resource& r2 = c;
64     assert(r1 != r2);
65     assert(!(r1 == r2));
66   }
67 }
68 
test_allocate_deallocate()69 void test_allocate_deallocate() {
70   std::pmr::memory_resource& r1 = *std::pmr::new_delete_resource();
71 
72   globalMemCounter.reset();
73 
74   void* ret = r1.allocate(50);
75   assert(ret);
76   ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkOutstandingNewEq(1));
77   ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkLastNewSizeEq(50));
78 
79   r1.deallocate(ret, 50);
80   assert(globalMemCounter.checkOutstandingNewEq(0));
81   ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkDeleteCalledEq(1));
82 }
83 
main(int,char **)84 int main(int, char**) {
85   ASSERT_NOEXCEPT(std::pmr::new_delete_resource());
86   test_return();
87   test_equality();
88   test_allocate_deallocate();
89 
90   return 0;
91 }
92