allocate.pass.cpp (2474B)
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 // <experimental/memory_resource> 11 12 // UNSUPPORTED: c++98, c++03 13 14 //------------------------------------------------------------------------------ 15 // TESTING void * memory_resource::allocate(size_t, size_t = max_align) 16 // 17 // Concerns: 18 // A) 'memory_resource' contains a member 'allocate' with the required 19 // signature, including the default alignment parameter. 20 // B) The return type of 'allocate' is 'void*'. 21 // C) 'allocate' is not marked as 'noexcept'. 22 // D) Invoking 'allocate' invokes 'do_allocate' with the same arguments. 23 // E) If 'do_allocate' throws then 'allocate' propagates that exception. 24 25 #include <experimental/memory_resource> 26 #include <type_traits> 27 #include <cstddef> 28 #include <cassert> 29 30 #include "test_macros.h" 31 #include "test_memory_resource.hpp" 32 33 using std::experimental::pmr::memory_resource; 34 35 int main() 36 { 37 TestResource R(42); 38 auto& P = R.getController(); 39 memory_resource& M = R; 40 { 41 static_assert( 42 std::is_same<decltype(M.allocate(0, 0)), void*>::value 43 , "Must be void*" 44 ); 45 static_assert( 46 std::is_same<decltype(M.allocate(0)), void*>::value 47 , "Must be void*" 48 ); 49 } 50 { 51 static_assert( 52 ! noexcept(M.allocate(0, 0)) 53 , "Must not be noexcept." 54 ); 55 static_assert( 56 ! noexcept(M.allocate(0)) 57 , "Must not be noexcept." 58 ); 59 } 60 { 61 int s = 42; 62 int a = 64; 63 void* p = M.allocate(s, a); 64 assert(P.alloc_count == 1); 65 assert(P.checkAlloc(p, s, a)); 66 67 s = 128; 68 a = MaxAlignV; 69 p = M.allocate(s); 70 assert(P.alloc_count == 2); 71 assert(P.checkAlloc(p, s, a)); 72 } 73 #ifndef TEST_HAS_NO_EXCEPTIONS 74 { 75 TestResource R2; 76 auto& P2 = R2.getController(); 77 P2.throw_on_alloc = true; 78 memory_resource& M2 = R2; 79 try { 80 M2.allocate(42); 81 assert(false); 82 } catch (TestException const&) { 83 // do nothing. 84 } catch (...) { 85 assert(false); 86 } 87 } 88 #endif 89 }