blob: bd8c121a4e866ba618a94be99dad94f95b82c9a7 [file] [log] [blame]
Eric Fiselier257fd692016-05-07 01:04:55 +00001//===----------------------------------------------------------------------===//
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// UNSUPPORTED: c++98, c++03
11
12// <experimental/memory_resource>
13
14// memory_resource * new_delete_resource()
15
16#include <experimental/memory_resource>
17#include <type_traits>
18#include <cassert>
19
20#include "count_new.hpp"
21
22namespace ex = std::experimental::pmr;
23
24struct assert_on_compare : public ex::memory_resource
25{
26protected:
27 virtual void * do_allocate(size_t, size_t)
28 { assert(false); }
29
30 virtual void do_deallocate(void *, size_t, size_t)
31 { assert(false); }
32
33 virtual bool do_is_equal(ex::memory_resource const &) const noexcept
34 { assert(false); }
35};
36
37void test_return()
38{
39 {
40 static_assert(std::is_same<
41 decltype(ex::new_delete_resource()), ex::memory_resource*
42 >::value, "");
43 }
44 // assert not null
45 {
46 assert(ex::new_delete_resource());
47 }
48 // assert same return value
49 {
50 assert(ex::new_delete_resource() == ex::new_delete_resource());
51 }
52}
53
54void test_equality()
55{
56 // Same object
57 {
58 ex::memory_resource & r1 = *ex::new_delete_resource();
59 ex::memory_resource & r2 = *ex::new_delete_resource();
60 // check both calls returned the same object
61 assert(&r1 == &r2);
62 // check for proper equality semantics
63 assert(r1 == r2);
64 assert(r2 == r1);
65 assert(!(r1 != r2));
66 assert(!(r2 != r1));
67 }
68 // Different types
69 {
70 ex::memory_resource & r1 = *ex::new_delete_resource();
71 assert_on_compare c;
72 ex::memory_resource & r2 = c;
73 assert(r1 != r2);
74 assert(!(r1 == r2));
75 }
76}
77
78void test_allocate_deallocate()
79{
80 ex::memory_resource & r1 = *ex::new_delete_resource();
81
82 globalMemCounter.reset();
83
84 void *ret = r1.allocate(50);
85 assert(ret);
86 assert(globalMemCounter.checkOutstandingNewEq(1));
87 assert(globalMemCounter.checkLastNewSizeEq(50));
88
89 r1.deallocate(ret, 1);
90 assert(globalMemCounter.checkOutstandingNewEq(0));
91 assert(globalMemCounter.checkDeleteCalledEq(1));
92
93}
94
95int main()
96{
97 static_assert(noexcept(ex::new_delete_resource()), "Must be noexcept");
98 test_return();
99 test_equality();
100 test_allocate_deallocate();
101}