resize_size.pass.cpp (2286B)
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 // <deque> 11 12 // void resize(size_type n); 13 14 #include <deque> 15 #include <algorithm> 16 #include <iterator> 17 #include <cassert> 18 #include <cstddef> 19 20 #include "test_macros.h" 21 #include "min_allocator.h" 22 23 template <class C> 24 C 25 make(int size, int start = 0 ) 26 { 27 const int b = 4096 / sizeof(int); 28 int init = 0; 29 if (start > 0) 30 { 31 init = (start+1) / b + ((start+1) % b != 0); 32 init *= b; 33 --init; 34 } 35 C c(init, 0); 36 for (int i = 0; i < init-start; ++i) 37 c.pop_back(); 38 for (int i = 0; i < size; ++i) 39 c.push_back(i); 40 for (int i = 0; i < start; ++i) 41 c.pop_front(); 42 return c; 43 } 44 45 template <class C> 46 void 47 test(C& c1, int size) 48 { 49 typedef typename C::const_iterator CI; 50 typename C::size_type c1_osize = c1.size(); 51 c1.resize(size); 52 assert(c1.size() == static_cast<std::size_t>(size)); 53 assert(static_cast<std::size_t>(distance(c1.begin(), c1.end())) == c1.size()); 54 CI i = c1.begin(); 55 for (int j = 0; static_cast<std::size_t>(j) < std::min(c1_osize, c1.size()); ++j, ++i) 56 assert(*i == j); 57 for (std::size_t j = c1_osize; j < c1.size(); ++j, ++i) 58 assert(*i == 0); 59 } 60 61 template <class C> 62 void 63 testN(int start, int N, int M) 64 { 65 C c1 = make<C>(N, start); 66 test(c1, M); 67 } 68 69 int main() 70 { 71 { 72 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; 73 const int N = sizeof(rng)/sizeof(rng[0]); 74 for (int i = 0; i < N; ++i) 75 for (int j = 0; j < N; ++j) 76 for (int k = 0; k < N; ++k) 77 testN<std::deque<int> >(rng[i], rng[j], rng[k]); 78 } 79 #if TEST_STD_VER >= 11 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 for (int k = 0; k < N; ++k) 86 testN<std::deque<int, min_allocator<int>>>(rng[i], rng[j], rng[k]); 87 } 88 #endif 89 }