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