libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

allocate_hint.pass.cpp (2101B)


      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 // <memory>
     11 
     12 // template <class Alloc>
     13 // struct allocator_traits
     14 // {
     15 //     static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
     16 //     ...
     17 // };
     18 
     19 #include <memory>
     20 #include <cstdint>
     21 #include <cassert>
     22 
     23 #include "test_macros.h"
     24 #include "incomplete_type_helper.h"
     25 
     26 template <class T>
     27 struct A
     28 {
     29     typedef T value_type;
     30 
     31     value_type* allocate(std::size_t n)
     32     {
     33         assert(n == 10);
     34         return reinterpret_cast<value_type*>(static_cast<std::uintptr_t>(0xDEADBEEF));
     35     }
     36 };
     37 
     38 template <class T>
     39 struct B
     40 {
     41     typedef T value_type;
     42 
     43     value_type* allocate(std::size_t n)
     44     {
     45         assert(n == 12);
     46         return reinterpret_cast<value_type*>(static_cast<std::uintptr_t>(0xEEADBEEF));
     47     }
     48     value_type* allocate(std::size_t n, const void* p)
     49     {
     50         assert(n == 11);
     51         assert(p == 0);
     52         return reinterpret_cast<value_type*>(static_cast<std::uintptr_t>(0xFEADBEEF));
     53     }
     54 };
     55 
     56 
     57 int main()
     58 {
     59 #if TEST_STD_VER >= 11
     60   {
     61     A<int> a;
     62     assert(std::allocator_traits<A<int> >::allocate(a, 10, nullptr) == reinterpret_cast<int*>(static_cast<std::uintptr_t>(0xDEADBEEF)));
     63   }
     64   {
     65     typedef IncompleteHolder* VT;
     66     typedef A<VT> Alloc;
     67     Alloc a;
     68     assert(std::allocator_traits<Alloc >::allocate(a, 10, nullptr) == reinterpret_cast<VT*>(static_cast<std::uintptr_t>(0xDEADBEEF)));
     69   }
     70 #endif
     71   {
     72     B<int> b;
     73     assert(std::allocator_traits<B<int> >::allocate(b, 11, nullptr) == reinterpret_cast<int*>(static_cast<std::uintptr_t>(0xFEADBEEF)));
     74   }
     75   {
     76     typedef IncompleteHolder* VT;
     77     typedef B<VT> Alloc;
     78     Alloc b;
     79     assert(std::allocator_traits<Alloc >::allocate(b, 11, nullptr) == reinterpret_cast<VT*>(static_cast<std::uintptr_t>(0xFEADBEEF)));
     80   }
     81 }