1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: c++03 10 11 // <deque> 12 13 // template <class... Args> reference emplace_front(Args&&... args); 14 // return type is 'reference' in C++17; 'void' before 15 16 #include <deque> 17 #include <cstddef> 18 #include <cassert> 19 20 #include "test_macros.h" 21 #include "../../../Emplaceable.h" 22 #include "min_allocator.h" 23 #include "test_allocator.h" 24 25 template <class C> 26 C 27 make(int size, int start = 0 ) 28 { 29 const int b = 4096 / sizeof(int); 30 int init = 0; 31 if (start > 0) 32 { 33 init = (start+1) / b + ((start+1) % b != 0); 34 init *= b; 35 --init; 36 } 37 C c(init); 38 for (int i = 0; i < init-start; ++i) 39 c.pop_back(); 40 for (int i = 0; i < size; ++i) 41 c.push_back(Emplaceable()); 42 for (int i = 0; i < start; ++i) 43 c.pop_front(); 44 return c; 45 } 46 47 template <class C> 48 void 49 test(C& c1) 50 { 51 typedef typename C::iterator I; 52 std::size_t c1_osize = c1.size(); 53 #if TEST_STD_VER > 14 54 typedef typename C::reference Ref; 55 Ref res_ref = c1.emplace_front(Emplaceable(1, 2.5)); 56 #else 57 c1.emplace_front(Emplaceable(1, 2.5)); 58 #endif 59 assert(c1.size() == c1_osize + 1); 60 assert(distance(c1.begin(), c1.end()) 61 == static_cast<std::ptrdiff_t>(c1.size())); 62 I i = c1.begin(); 63 assert(*i == Emplaceable(1, 2.5)); 64 #if TEST_STD_VER > 14 65 assert(&res_ref == &(*i)); 66 #endif 67 } 68 69 template <class C> 70 void 71 testN(int start, int N) 72 { 73 C c1 = make<C>(N, start); 74 test(c1); 75 } 76 77 78 int main(int, char**) 79 { 80 { 81 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; 82 const int N = sizeof(rng)/sizeof(rng[0]); 83 for (int i = 0; i < N; ++i) 84 for (int j = 0; j < N; ++j) 85 testN<std::deque<Emplaceable> >(rng[i], rng[j]); 86 } 87 { 88 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; 89 const int N = sizeof(rng)/sizeof(rng[0]); 90 for (int i = 0; i < N; ++i) 91 for (int j = 0; j < N; ++j) 92 testN<std::deque<Emplaceable, min_allocator<Emplaceable>> >(rng[i], rng[j]); 93 } 94 { 95 std::deque<Tag_X, TaggingAllocator<Tag_X>> c; 96 c.emplace_front(); 97 assert(c.size() == 1); 98 c.emplace_front(1, 2, 3); 99 assert(c.size() == 2); 100 c.emplace_front(); 101 assert(c.size() == 3); 102 c.emplace_front(1, 2, 3); 103 assert(c.size() == 4); 104 } 105 106 return 0; 107 } 108