Dan Albert | 287553d | 2017-02-16 10:47:51 -0800 | [diff] [blame] | 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 | // UNSUPPORTED: c++98, c++03 |
| 11 | |
| 12 | // <experimental/memory_resource> |
| 13 | |
| 14 | // template <class T> class polymorphic_allocator |
| 15 | |
| 16 | // template <class P1, class P2, class U1, class U2> |
| 17 | // void polymorphic_allocator<T>::construct(pair<P1, P2>*, pair<U1, U2> const&) |
| 18 | |
| 19 | #include <experimental/memory_resource> |
| 20 | #include <type_traits> |
| 21 | #include <utility> |
| 22 | #include <tuple> |
| 23 | #include <cassert> |
| 24 | #include <cstdlib> |
| 25 | #include "uses_alloc_types.hpp" |
| 26 | |
| 27 | namespace ex = std::experimental::pmr; |
| 28 | |
| 29 | |
| 30 | template <class UA1, class UA2, class TT, class UU> |
| 31 | bool doTest(UsesAllocatorType TExpect, UsesAllocatorType UExpect, |
| 32 | std::pair<TT, UU> const& p) |
| 33 | { |
| 34 | using P = std::pair<UA1, UA2>; |
| 35 | TestResource R; |
| 36 | ex::memory_resource * M = &R; |
| 37 | ex::polymorphic_allocator<P> A(M); |
| 38 | P * ptr = (P*)std::malloc(sizeof(P)); |
| 39 | P * ptr2 = (P*)std::malloc(sizeof(P)); |
| 40 | |
| 41 | // UNDER TEST // |
| 42 | A.construct(ptr, p); |
| 43 | |
| 44 | A.construct(ptr2, std::piecewise_construct, |
| 45 | std::forward_as_tuple(p.first), |
| 46 | std::forward_as_tuple(p.second)); |
| 47 | // ------- // |
| 48 | |
| 49 | bool tres = checkConstruct<decltype((p.first))>(ptr->first, TExpect, M) && |
| 50 | checkConstructionEquiv(ptr->first, ptr2->first); |
| 51 | |
| 52 | bool ures = checkConstruct<decltype((p.second))>(ptr->second, UExpect, M) && |
| 53 | checkConstructionEquiv(ptr->second, ptr2->second); |
| 54 | |
| 55 | A.destroy(ptr); |
| 56 | std::free(ptr); |
| 57 | A.destroy(ptr2); |
| 58 | std::free(ptr2); |
| 59 | return tres && ures; |
| 60 | |
| 61 | } |
| 62 | |
| 63 | template <class Alloc, class TT, class UU> |
| 64 | void test_pmr_uses_allocator(std::pair<TT, UU> const& p) |
| 65 | { |
| 66 | { |
| 67 | using T = NotUsesAllocator<Alloc, 1>; |
| 68 | using U = NotUsesAllocator<Alloc, 1>; |
| 69 | assert((doTest<T, U>(UA_None, UA_None, p))); |
| 70 | } |
| 71 | { |
| 72 | using T = UsesAllocatorV1<Alloc, 1>; |
| 73 | using U = UsesAllocatorV2<Alloc, 1>; |
| 74 | assert((doTest<T, U>(UA_AllocArg, UA_AllocLast, p))); |
| 75 | } |
| 76 | { |
| 77 | using T = UsesAllocatorV2<Alloc, 1>; |
| 78 | using U = UsesAllocatorV3<Alloc, 1>; |
| 79 | assert((doTest<T, U>(UA_AllocLast, UA_AllocArg, p))); |
| 80 | } |
| 81 | { |
| 82 | using T = UsesAllocatorV3<Alloc, 1>; |
| 83 | using U = NotUsesAllocator<Alloc, 1>; |
| 84 | assert((doTest<T, U>(UA_AllocArg, UA_None, p))); |
| 85 | } |
| 86 | } |
| 87 | template <class Tp> |
| 88 | struct Print; |
| 89 | |
| 90 | int main() |
| 91 | { |
| 92 | using ERT = std::experimental::erased_type; |
| 93 | using PMR = ex::memory_resource*; |
| 94 | using PMA = ex::polymorphic_allocator<char>; |
| 95 | { |
| 96 | int x = 42; |
| 97 | int y = 42; |
| 98 | const std::pair<int, int&> p(x, y); |
| 99 | test_pmr_uses_allocator<ERT>(p); |
| 100 | test_pmr_uses_allocator<PMR>(p); |
| 101 | test_pmr_uses_allocator<PMA>(p); |
| 102 | } |
| 103 | { |
| 104 | int x = 42; |
| 105 | int y = 42; |
| 106 | const std::pair<int&, int&&> p(x, std::move(y)); |
| 107 | test_pmr_uses_allocator<ERT>(p); |
| 108 | test_pmr_uses_allocator<PMR>(p); |
| 109 | test_pmr_uses_allocator<PMA>(p); |
| 110 | } |
| 111 | } |