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 // template <class InputIterator>
     13 //   iterator insert (const_iterator p, InputIterator f, InputIterator l);
     14 
     15 #include <deque>
     16 #include <cassert>
     17 
     18 #include "test_iterators.h"
     19 #include "../../../MoveOnly.h"
     20 #include "../../../stack_allocator.h"
     21 
     22 std::deque<int>
     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     std::deque<int> 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 void
     44 test(int P, std::deque<int>& c1, const std::deque<int>& c2)
     45 {
     46     typedef std::deque<int> C;
     47     typedef C::iterator I;
     48     typedef C::const_iterator CI;
     49     typedef bidirectional_iterator<CI> BCI;
     50     std::size_t c1_osize = c1.size();
     51     CI i = c1.insert(c1.begin() + P, BCI(c2.begin()), BCI(c2.end()));
     52     assert(i == c1.begin() + P);
     53     assert(c1.size() == c1_osize + c2.size());
     54     assert(distance(c1.begin(), c1.end()) == c1.size());
     55     i = c1.begin();
     56     for (int j = 0; j < P; ++j, ++i)
     57         assert(*i == j);
     58     for (int j = 0; j < c2.size(); ++j, ++i)
     59         assert(*i == j);
     60     for (int j = P; j < c1_osize; ++j, ++i)
     61         assert(*i == j);
     62 }
     63 
     64 void
     65 testN(int start, int N, int M)
     66 {
     67     typedef std::deque<int> C;
     68     typedef C::iterator I;
     69     typedef C::const_iterator CI;
     70     for (int i = 0; i <= 3; ++i)
     71     {
     72         if (0 <= i && i <= N)
     73         {
     74             C c1 = make(N, start);
     75             C c2 = make(M);
     76             test(i, c1, c2);
     77         }
     78     }
     79     for (int i = M-1; i <= M+1; ++i)
     80     {
     81         if (0 <= i && i <= N)
     82         {
     83             C c1 = make(N, start);
     84             C c2 = make(M);
     85             test(i, c1, c2);
     86         }
     87     }
     88     for (int i = N/2-1; i <= N/2+1; ++i)
     89     {
     90         if (0 <= i && i <= N)
     91         {
     92             C c1 = make(N, start);
     93             C c2 = make(M);
     94             test(i, c1, c2);
     95         }
     96     }
     97     for (int i = N - M - 1; i <= N - M + 1; ++i)
     98     {
     99         if (0 <= i && i <= N)
    100         {
    101             C c1 = make(N, start);
    102             C c2 = make(M);
    103             test(i, c1, c2);
    104         }
    105     }
    106     for (int i = N - M - 1; i <= N - M + 1; ++i)
    107     {
    108         if (0 <= i && i <= N)
    109         {
    110             C c1 = make(N, start);
    111             C c2 = make(M);
    112             test(i, c1, c2);
    113         }
    114     }
    115     for (int i = N - 3; i <= N; ++i)
    116     {
    117         if (0 <= i && i <= N)
    118         {
    119             C c1 = make(N, start);
    120             C c2 = make(M);
    121             test(i, c1, c2);
    122         }
    123     }
    124 }
    125 
    126 void
    127 testI(int P, std::deque<int>& c1, const std::deque<int>& c2)
    128 {
    129     typedef std::deque<int> C;
    130     typedef C::iterator I;
    131     typedef C::const_iterator CI;
    132     typedef input_iterator<CI> ICI;
    133     std::size_t c1_osize = c1.size();
    134     CI i = c1.insert(c1.begin() + P, ICI(c2.begin()), ICI(c2.end()));
    135     assert(i == c1.begin() + P);
    136     assert(c1.size() == c1_osize + c2.size());
    137     assert(distance(c1.begin(), c1.end()) == c1.size());
    138     i = c1.begin();
    139     for (int j = 0; j < P; ++j, ++i)
    140         assert(*i == j);
    141     for (int j = 0; j < c2.size(); ++j, ++i)
    142         assert(*i == j);
    143     for (int j = P; j < c1_osize; ++j, ++i)
    144         assert(*i == j);
    145 }
    146 
    147 void
    148 testNI(int start, int N, int M)
    149 {
    150     typedef std::deque<int> C;
    151     typedef C::iterator I;
    152     typedef C::const_iterator CI;
    153     for (int i = 0; i <= 3; ++i)
    154     {
    155         if (0 <= i && i <= N)
    156         {
    157             C c1 = make(N, start);
    158             C c2 = make(M);
    159             testI(i, c1, c2);
    160         }
    161     }
    162     for (int i = M-1; i <= M+1; ++i)
    163     {
    164         if (0 <= i && i <= N)
    165         {
    166             C c1 = make(N, start);
    167             C c2 = make(M);
    168             testI(i, c1, c2);
    169         }
    170     }
    171     for (int i = N/2-1; i <= N/2+1; ++i)
    172     {
    173         if (0 <= i && i <= N)
    174         {
    175             C c1 = make(N, start);
    176             C c2 = make(M);
    177             testI(i, c1, c2);
    178         }
    179     }
    180     for (int i = N - M - 1; i <= N - M + 1; ++i)
    181     {
    182         if (0 <= i && i <= N)
    183         {
    184             C c1 = make(N, start);
    185             C c2 = make(M);
    186             testI(i, c1, c2);
    187         }
    188     }
    189     for (int i = N - 3; i <= N; ++i)
    190     {
    191         if (0 <= i && i <= N)
    192         {
    193             C c1 = make(N, start);
    194             C c2 = make(M);
    195             testI(i, c1, c2);
    196         }
    197     }
    198 }
    199 
    200 void
    201 test_move()
    202 {
    203 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    204     std::deque<MoveOnly, stack_allocator<MoveOnly, 2000> > c;
    205     typedef std::deque<MoveOnly>::const_iterator CI;
    206     {
    207         MoveOnly mo(0);
    208         typedef MoveOnly* I;
    209         c.insert(c.end(), std::move_iterator<I>(&mo), std::move_iterator<I>(&mo+1));
    210     }
    211     int j = 0;
    212     for (CI i = c.begin(); i != c.end(); ++i, ++j)
    213         assert(*i == MoveOnly(j));
    214     {
    215         MoveOnly mo(1);
    216         typedef input_iterator<MoveOnly*> I;
    217         c.insert(c.end(), std::move_iterator<I>(I(&mo)), std::move_iterator<I>(I(&mo+1)));
    218     }
    219     j = 0;
    220     for (CI i = c.begin(); i != c.end(); ++i, ++j)
    221         assert(*i == MoveOnly(j));
    222 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    223 }
    224 
    225 int main()
    226 {
    227     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
    228     const int N = sizeof(rng)/sizeof(rng[0]);
    229     for (int i = 0; i < N; ++i)
    230         for (int j = 0; j < N; ++j)
    231             for (int k = 0; k < N; ++k)
    232                 testN(rng[i], rng[j], rng[k]);
    233     testNI(1500, 2000, 1000);
    234     test_move();
    235 }
    236