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