Eric Fiselier | 257fd69 | 2016-05-07 01:04:55 +0000 | [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 | // T* polymorphic_allocator<T>::allocate(size_t n) |
| 17 | |
| 18 | #include <experimental/memory_resource> |
| 19 | #include <limits> |
| 20 | #include <memory> |
| 21 | #include <exception> |
| 22 | #include <type_traits> |
| 23 | #include <cassert> |
| 24 | |
| 25 | #include "test_macros.h" |
| 26 | #include "test_memory_resource.hpp" |
| 27 | |
| 28 | namespace ex = std::experimental::pmr; |
| 29 | |
| 30 | template <size_t S, size_t Align> |
| 31 | void testForSizeAndAlign() { |
| 32 | using T = typename std::aligned_storage<S, Align>::type; |
| 33 | TestResource R; |
| 34 | ex::polymorphic_allocator<T> a(&R); |
| 35 | |
| 36 | for (int N = 1; N <= 5; ++N) { |
| 37 | auto ret = a.allocate(N); |
| 38 | assert(R.checkAlloc(ret, N * sizeof(T), alignof(T))); |
| 39 | |
| 40 | a.deallocate(ret, N); |
| 41 | R.reset(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 46 | template <size_t S> |
| 47 | void testAllocForSizeThrows() { |
| 48 | using T = typename std::aligned_storage<S>::type; |
| 49 | using Alloc = ex::polymorphic_allocator<T>; |
| 50 | using Traits = std::allocator_traits<Alloc>; |
| 51 | NullResource R; |
| 52 | Alloc a(&R); |
| 53 | |
| 54 | // Test that allocating exactly the max size does not throw. |
| 55 | size_t maxSize = Traits::max_size(a); |
| 56 | try { |
| 57 | a.allocate(maxSize); |
| 58 | } catch (...) { |
| 59 | assert(false); |
| 60 | } |
| 61 | |
| 62 | size_t sizeTypeMax = std::numeric_limits<std::size_t>::max(); |
| 63 | if (maxSize != sizeTypeMax) |
| 64 | { |
| 65 | // Test that allocating size_t(~0) throws bad alloc. |
| 66 | try { |
| 67 | a.allocate(sizeTypeMax); |
| 68 | assert(false); |
| 69 | } catch (std::exception const&) { |
| 70 | } |
| 71 | |
| 72 | // Test that allocating even one more than the max size does throw. |
| 73 | size_t overSize = maxSize + 1; |
| 74 | try { |
| 75 | a.allocate(overSize); |
| 76 | assert(false); |
| 77 | } catch (std::exception const&) { |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | #endif // TEST_HAS_NO_EXCEPTIONS |
| 82 | |
| 83 | int main() |
| 84 | { |
| 85 | { |
| 86 | ex::polymorphic_allocator<int> a; |
| 87 | static_assert(std::is_same<decltype(a.allocate(0)), int*>::value, ""); |
| 88 | static_assert(!noexcept(a.allocate(0)), ""); |
| 89 | } |
| 90 | { |
| 91 | constexpr std::size_t MA = alignof(std::max_align_t); |
| 92 | testForSizeAndAlign<1, 1>(); |
| 93 | testForSizeAndAlign<1, 2>(); |
| 94 | testForSizeAndAlign<1, MA>(); |
| 95 | testForSizeAndAlign<2, 2>(); |
| 96 | testForSizeAndAlign<73, alignof(void*)>(); |
| 97 | testForSizeAndAlign<73, MA>(); |
| 98 | testForSizeAndAlign<13, MA>(); |
| 99 | } |
| 100 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 101 | { |
| 102 | testAllocForSizeThrows<1>(); |
| 103 | testAllocForSizeThrows<2>(); |
| 104 | testAllocForSizeThrows<4>(); |
| 105 | testAllocForSizeThrows<8>(); |
| 106 | testAllocForSizeThrows<16>(); |
| 107 | testAllocForSizeThrows<73>(); |
| 108 | testAllocForSizeThrows<13>(); |
| 109 | } |
| 110 | #endif |
| 111 | } |