Home | History | Annotate | Download | only in deque.capacity
      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 shrink_to_fit();
     13 
     14 #include <deque>
     15 #include <cassert>
     16 
     17 #include "test_macros.h"
     18 #include "min_allocator.h"
     19 
     20 template <class C>
     21 C
     22 make(int size, int start = 0 )
     23 {
     24     const int b = 4096 / sizeof(int);
     25     int init = 0;
     26     if (start > 0)
     27     {
     28         init = (start+1) / b + ((start+1) % b != 0);
     29         init *= b;
     30         --init;
     31     }
     32     C c(init, 0);
     33     for (int i = 0; i < init-start; ++i)
     34         c.pop_back();
     35     for (int i = 0; i < size; ++i)
     36         c.push_back(i);
     37     for (int i = 0; i < start; ++i)
     38         c.pop_front();
     39     return c;
     40 }
     41 
     42 template <class C>
     43 void
     44 test(C& c1)
     45 {
     46     C s = c1;
     47     c1.shrink_to_fit();
     48     assert(c1 == s);
     49 }
     50 
     51 template <class C>
     52 void
     53 testN(int start, int N)
     54 {
     55     C c1 = make<C>(N, start);
     56     test(c1);
     57 }
     58 
     59 int main()
     60 {
     61     {
     62     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
     63     const int N = sizeof(rng)/sizeof(rng[0]);
     64     for (int i = 0; i < N; ++i)
     65         for (int j = 0; j < N; ++j)
     66             testN<std::deque<int> >(rng[i], rng[j]);
     67     }
     68 #if TEST_STD_VER >= 11
     69     {
     70     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
     71     const int N = sizeof(rng)/sizeof(rng[0]);
     72     for (int i = 0; i < N; ++i)
     73         for (int j = 0; j < N; ++j)
     74             testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]);
     75     }
     76 #endif
     77 }
     78