Home | History | Annotate | Download | only in deque.modifiers
      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 // iterator insert (const_iterator p, const value_type& v);
     13 
     14 #include <deque>
     15 #include <cassert>
     16 #include <cstddef>
     17 
     18 #include "test_macros.h"
     19 #include "min_allocator.h"
     20 
     21 template <class C>
     22 C
     23 make(int size, int start = 0 )
     24 {
     25     const int b = 4096 / sizeof(int);
     26     int init = 0;
     27     if (start > 0)
     28     {
     29         init = (start+1) / b + ((start+1) % b != 0);
     30         init *= b;
     31         --init;
     32     }
     33     C c(init, 0);
     34     for (int i = 0; i < init-start; ++i)
     35         c.pop_back();
     36     for (int i = 0; i < size; ++i)
     37         c.push_back(i);
     38     for (int i = 0; i < start; ++i)
     39         c.pop_front();
     40     return c;
     41 }
     42 
     43 template <class C>
     44 void
     45 test(int P, C& c1, int x)
     46 {
     47     typedef typename C::const_iterator CI;
     48     std::size_t c1_osize = c1.size();
     49     CI i = c1.insert(c1.begin() + P, x);
     50     assert(i == c1.begin() + P);
     51     assert(c1.size() == c1_osize + 1);
     52     assert(static_cast<std::size_t>(distance(c1.begin(), c1.end())) == c1.size());
     53     i = c1.begin();
     54     for (int j = 0; j < P; ++j, ++i)
     55         assert(*i == j);
     56     assert(*i == x);
     57     ++i;
     58     for (int j = P; static_cast<std::size_t>(j) < c1_osize; ++j, ++i)
     59         assert(*i == j);
     60 }
     61 
     62 template <class C>
     63 void
     64 testN(int start, int N)
     65 {
     66     for (int i = 0; i <= 3; ++i)
     67     {
     68         if (0 <= i && i <= N)
     69         {
     70             C c1 = make<C>(N, start);
     71             test(i, c1, -10);
     72         }
     73     }
     74     for (int i = N/2-1; i <= N/2+1; ++i)
     75     {
     76         if (0 <= i && i <= N)
     77         {
     78             C c1 = make<C>(N, start);
     79             test(i, c1, -10);
     80         }
     81     }
     82     for (int i = N - 3; i <= N; ++i)
     83     {
     84         if (0 <= i && i <= N)
     85         {
     86             C c1 = make<C>(N, start);
     87             test(i, c1, -10);
     88         }
     89     }
     90 }
     91 
     92 template <class C>
     93 void
     94 self_reference_test()
     95 {
     96     typedef typename C::const_iterator CI;
     97     for (int i = 0; i < 20; ++i)
     98     {
     99         for (int j = 0; j < 20; ++j)
    100         {
    101             C c = make<C>(20);
    102             CI it = c.cbegin() + i;
    103             CI jt = c.cbegin() + j;
    104             c.insert(it, *jt);
    105             assert(c.size() == 21);
    106             assert(static_cast<std::size_t>(distance(c.begin(), c.end())) == c.size());
    107             it = c.cbegin();
    108             for (int k = 0; k < i; ++k, ++it)
    109                 assert(*it == k);
    110             assert(*it == j);
    111             ++it;
    112             for (int k = i; k < 20; ++k, ++it)
    113                 assert(*it == k);
    114         }
    115     }
    116 }
    117 
    118 int main()
    119 {
    120     {
    121     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
    122     const int N = sizeof(rng)/sizeof(rng[0]);
    123     for (int i = 0; i < N; ++i)
    124         for (int j = 0; j < N; ++j)
    125             testN<std::deque<int> >(rng[i], rng[j]);
    126     self_reference_test<std::deque<int> >();
    127     }
    128 #if TEST_STD_VER >= 11
    129     {
    130     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
    131     const int N = sizeof(rng)/sizeof(rng[0]);
    132     for (int i = 0; i < N; ++i)
    133         for (int j = 0; j < N; ++j)
    134             testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]);
    135     self_reference_test<std::deque<int, min_allocator<int>> >();
    136     }
    137 #endif
    138 }
    139