14684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
24684ddb6SLionel Sambuc //
34684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
44684ddb6SLionel Sambuc //
54684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
64684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
74684ddb6SLionel Sambuc //
84684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
94684ddb6SLionel Sambuc 
104684ddb6SLionel Sambuc // <deque>
114684ddb6SLionel Sambuc 
124684ddb6SLionel Sambuc // template <class InputIterator>
134684ddb6SLionel Sambuc //   iterator insert (const_iterator p, InputIterator f, InputIterator l);
144684ddb6SLionel Sambuc 
154684ddb6SLionel Sambuc #include <deque>
164684ddb6SLionel Sambuc #include <cassert>
174684ddb6SLionel Sambuc 
184684ddb6SLionel Sambuc #include "test_iterators.h"
194684ddb6SLionel Sambuc #include "../../../MoveOnly.h"
204684ddb6SLionel Sambuc #include "../../../stack_allocator.h"
21*0a6a1f1dSLionel Sambuc #include "min_allocator.h"
224684ddb6SLionel Sambuc 
234684ddb6SLionel Sambuc template <class C>
244684ddb6SLionel Sambuc C
make(int size,int start=0)254684ddb6SLionel Sambuc make(int size, int start = 0 )
264684ddb6SLionel Sambuc {
274684ddb6SLionel Sambuc     const int b = 4096 / sizeof(int);
284684ddb6SLionel Sambuc     int init = 0;
294684ddb6SLionel Sambuc     if (start > 0)
304684ddb6SLionel Sambuc     {
314684ddb6SLionel Sambuc         init = (start+1) / b + ((start+1) % b != 0);
324684ddb6SLionel Sambuc         init *= b;
334684ddb6SLionel Sambuc         --init;
344684ddb6SLionel Sambuc     }
354684ddb6SLionel Sambuc     C c(init, 0);
364684ddb6SLionel Sambuc     for (int i = 0; i < init-start; ++i)
374684ddb6SLionel Sambuc         c.pop_back();
384684ddb6SLionel Sambuc     for (int i = 0; i < size; ++i)
394684ddb6SLionel Sambuc         c.push_back(i);
404684ddb6SLionel Sambuc     for (int i = 0; i < start; ++i)
414684ddb6SLionel Sambuc         c.pop_front();
424684ddb6SLionel Sambuc     return c;
434684ddb6SLionel Sambuc };
444684ddb6SLionel Sambuc 
454684ddb6SLionel Sambuc template <class C>
464684ddb6SLionel Sambuc void
test(int P,C & c1,const C & c2)474684ddb6SLionel Sambuc test(int P, C& c1, const C& c2)
484684ddb6SLionel Sambuc {
494684ddb6SLionel Sambuc     typedef typename C::iterator I;
504684ddb6SLionel Sambuc     typedef typename C::const_iterator CI;
514684ddb6SLionel Sambuc     typedef bidirectional_iterator<CI> BCI;
524684ddb6SLionel Sambuc     std::size_t c1_osize = c1.size();
534684ddb6SLionel Sambuc     CI i = c1.insert(c1.begin() + P, BCI(c2.begin()), BCI(c2.end()));
544684ddb6SLionel Sambuc     assert(i == c1.begin() + P);
554684ddb6SLionel Sambuc     assert(c1.size() == c1_osize + c2.size());
564684ddb6SLionel Sambuc     assert(distance(c1.begin(), c1.end()) == c1.size());
574684ddb6SLionel Sambuc     i = c1.begin();
584684ddb6SLionel Sambuc     for (int j = 0; j < P; ++j, ++i)
594684ddb6SLionel Sambuc         assert(*i == j);
604684ddb6SLionel Sambuc     for (int j = 0; j < c2.size(); ++j, ++i)
614684ddb6SLionel Sambuc         assert(*i == j);
624684ddb6SLionel Sambuc     for (int j = P; j < c1_osize; ++j, ++i)
634684ddb6SLionel Sambuc         assert(*i == j);
644684ddb6SLionel Sambuc }
654684ddb6SLionel Sambuc 
664684ddb6SLionel Sambuc template <class C>
674684ddb6SLionel Sambuc void
testN(int start,int N,int M)684684ddb6SLionel Sambuc testN(int start, int N, int M)
694684ddb6SLionel Sambuc {
704684ddb6SLionel Sambuc     typedef typename C::iterator I;
714684ddb6SLionel Sambuc     typedef typename C::const_iterator CI;
724684ddb6SLionel Sambuc     for (int i = 0; i <= 3; ++i)
734684ddb6SLionel Sambuc     {
744684ddb6SLionel Sambuc         if (0 <= i && i <= N)
754684ddb6SLionel Sambuc         {
764684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
774684ddb6SLionel Sambuc             C c2 = make<C>(M);
784684ddb6SLionel Sambuc             test(i, c1, c2);
794684ddb6SLionel Sambuc         }
804684ddb6SLionel Sambuc     }
814684ddb6SLionel Sambuc     for (int i = M-1; i <= M+1; ++i)
824684ddb6SLionel Sambuc     {
834684ddb6SLionel Sambuc         if (0 <= i && i <= N)
844684ddb6SLionel Sambuc         {
854684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
864684ddb6SLionel Sambuc             C c2 = make<C>(M);
874684ddb6SLionel Sambuc             test(i, c1, c2);
884684ddb6SLionel Sambuc         }
894684ddb6SLionel Sambuc     }
904684ddb6SLionel Sambuc     for (int i = N/2-1; i <= N/2+1; ++i)
914684ddb6SLionel Sambuc     {
924684ddb6SLionel Sambuc         if (0 <= i && i <= N)
934684ddb6SLionel Sambuc         {
944684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
954684ddb6SLionel Sambuc             C c2 = make<C>(M);
964684ddb6SLionel Sambuc             test(i, c1, c2);
974684ddb6SLionel Sambuc         }
984684ddb6SLionel Sambuc     }
994684ddb6SLionel Sambuc     for (int i = N - M - 1; i <= N - M + 1; ++i)
1004684ddb6SLionel Sambuc     {
1014684ddb6SLionel Sambuc         if (0 <= i && i <= N)
1024684ddb6SLionel Sambuc         {
1034684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
1044684ddb6SLionel Sambuc             C c2 = make<C>(M);
1054684ddb6SLionel Sambuc             test(i, c1, c2);
1064684ddb6SLionel Sambuc         }
1074684ddb6SLionel Sambuc     }
1084684ddb6SLionel Sambuc     for (int i = N - M - 1; i <= N - M + 1; ++i)
1094684ddb6SLionel Sambuc     {
1104684ddb6SLionel Sambuc         if (0 <= i && i <= N)
1114684ddb6SLionel Sambuc         {
1124684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
1134684ddb6SLionel Sambuc             C c2 = make<C>(M);
1144684ddb6SLionel Sambuc             test(i, c1, c2);
1154684ddb6SLionel Sambuc         }
1164684ddb6SLionel Sambuc     }
1174684ddb6SLionel Sambuc     for (int i = N - 3; i <= N; ++i)
1184684ddb6SLionel Sambuc     {
1194684ddb6SLionel Sambuc         if (0 <= i && i <= N)
1204684ddb6SLionel Sambuc         {
1214684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
1224684ddb6SLionel Sambuc             C c2 = make<C>(M);
1234684ddb6SLionel Sambuc             test(i, c1, c2);
1244684ddb6SLionel Sambuc         }
1254684ddb6SLionel Sambuc     }
1264684ddb6SLionel Sambuc }
1274684ddb6SLionel Sambuc 
1284684ddb6SLionel Sambuc template <class C>
1294684ddb6SLionel Sambuc void
testI(int P,C & c1,const C & c2)1304684ddb6SLionel Sambuc testI(int P, C& c1, const C& c2)
1314684ddb6SLionel Sambuc {
1324684ddb6SLionel Sambuc     typedef typename C::iterator I;
1334684ddb6SLionel Sambuc     typedef typename C::const_iterator CI;
1344684ddb6SLionel Sambuc     typedef input_iterator<CI> ICI;
1354684ddb6SLionel Sambuc     std::size_t c1_osize = c1.size();
1364684ddb6SLionel Sambuc     CI i = c1.insert(c1.begin() + P, ICI(c2.begin()), ICI(c2.end()));
1374684ddb6SLionel Sambuc     assert(i == c1.begin() + P);
1384684ddb6SLionel Sambuc     assert(c1.size() == c1_osize + c2.size());
1394684ddb6SLionel Sambuc     assert(distance(c1.begin(), c1.end()) == c1.size());
1404684ddb6SLionel Sambuc     i = c1.begin();
1414684ddb6SLionel Sambuc     for (int j = 0; j < P; ++j, ++i)
1424684ddb6SLionel Sambuc         assert(*i == j);
1434684ddb6SLionel Sambuc     for (int j = 0; j < c2.size(); ++j, ++i)
1444684ddb6SLionel Sambuc         assert(*i == j);
1454684ddb6SLionel Sambuc     for (int j = P; j < c1_osize; ++j, ++i)
1464684ddb6SLionel Sambuc         assert(*i == j);
1474684ddb6SLionel Sambuc }
1484684ddb6SLionel Sambuc 
1494684ddb6SLionel Sambuc template <class C>
1504684ddb6SLionel Sambuc void
testNI(int start,int N,int M)1514684ddb6SLionel Sambuc testNI(int start, int N, int M)
1524684ddb6SLionel Sambuc {
1534684ddb6SLionel Sambuc     typedef typename C::iterator I;
1544684ddb6SLionel Sambuc     typedef typename C::const_iterator CI;
1554684ddb6SLionel Sambuc     for (int i = 0; i <= 3; ++i)
1564684ddb6SLionel Sambuc     {
1574684ddb6SLionel Sambuc         if (0 <= i && i <= N)
1584684ddb6SLionel Sambuc         {
1594684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
1604684ddb6SLionel Sambuc             C c2 = make<C>(M);
1614684ddb6SLionel Sambuc             testI(i, c1, c2);
1624684ddb6SLionel Sambuc         }
1634684ddb6SLionel Sambuc     }
1644684ddb6SLionel Sambuc     for (int i = M-1; i <= M+1; ++i)
1654684ddb6SLionel Sambuc     {
1664684ddb6SLionel Sambuc         if (0 <= i && i <= N)
1674684ddb6SLionel Sambuc         {
1684684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
1694684ddb6SLionel Sambuc             C c2 = make<C>(M);
1704684ddb6SLionel Sambuc             testI(i, c1, c2);
1714684ddb6SLionel Sambuc         }
1724684ddb6SLionel Sambuc     }
1734684ddb6SLionel Sambuc     for (int i = N/2-1; i <= N/2+1; ++i)
1744684ddb6SLionel Sambuc     {
1754684ddb6SLionel Sambuc         if (0 <= i && i <= N)
1764684ddb6SLionel Sambuc         {
1774684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
1784684ddb6SLionel Sambuc             C c2 = make<C>(M);
1794684ddb6SLionel Sambuc             testI(i, c1, c2);
1804684ddb6SLionel Sambuc         }
1814684ddb6SLionel Sambuc     }
1824684ddb6SLionel Sambuc     for (int i = N - M - 1; i <= N - M + 1; ++i)
1834684ddb6SLionel Sambuc     {
1844684ddb6SLionel Sambuc         if (0 <= i && i <= N)
1854684ddb6SLionel Sambuc         {
1864684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
1874684ddb6SLionel Sambuc             C c2 = make<C>(M);
1884684ddb6SLionel Sambuc             testI(i, c1, c2);
1894684ddb6SLionel Sambuc         }
1904684ddb6SLionel Sambuc     }
1914684ddb6SLionel Sambuc     for (int i = N - 3; i <= N; ++i)
1924684ddb6SLionel Sambuc     {
1934684ddb6SLionel Sambuc         if (0 <= i && i <= N)
1944684ddb6SLionel Sambuc         {
1954684ddb6SLionel Sambuc             C c1 = make<C>(N, start);
1964684ddb6SLionel Sambuc             C c2 = make<C>(M);
1974684ddb6SLionel Sambuc             testI(i, c1, c2);
1984684ddb6SLionel Sambuc         }
1994684ddb6SLionel Sambuc     }
2004684ddb6SLionel Sambuc }
2014684ddb6SLionel Sambuc 
2024684ddb6SLionel Sambuc template <class C>
2034684ddb6SLionel Sambuc void
test_move()2044684ddb6SLionel Sambuc test_move()
2054684ddb6SLionel Sambuc {
2064684ddb6SLionel Sambuc #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2074684ddb6SLionel Sambuc     C c;
2084684ddb6SLionel Sambuc     typedef typename C::const_iterator CI;
2094684ddb6SLionel Sambuc     {
2104684ddb6SLionel Sambuc         MoveOnly mo(0);
2114684ddb6SLionel Sambuc         typedef MoveOnly* I;
2124684ddb6SLionel Sambuc         c.insert(c.end(), std::move_iterator<I>(&mo), std::move_iterator<I>(&mo+1));
2134684ddb6SLionel Sambuc     }
2144684ddb6SLionel Sambuc     int j = 0;
2154684ddb6SLionel Sambuc     for (CI i = c.begin(); i != c.end(); ++i, ++j)
2164684ddb6SLionel Sambuc         assert(*i == MoveOnly(j));
2174684ddb6SLionel Sambuc     {
2184684ddb6SLionel Sambuc         MoveOnly mo(1);
2194684ddb6SLionel Sambuc         typedef input_iterator<MoveOnly*> I;
2204684ddb6SLionel Sambuc         c.insert(c.end(), std::move_iterator<I>(I(&mo)), std::move_iterator<I>(I(&mo+1)));
2214684ddb6SLionel Sambuc     }
2224684ddb6SLionel Sambuc     j = 0;
2234684ddb6SLionel Sambuc     for (CI i = c.begin(); i != c.end(); ++i, ++j)
2244684ddb6SLionel Sambuc         assert(*i == MoveOnly(j));
2254684ddb6SLionel Sambuc #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2264684ddb6SLionel Sambuc }
2274684ddb6SLionel Sambuc 
main()2284684ddb6SLionel Sambuc int main()
2294684ddb6SLionel Sambuc {
2304684ddb6SLionel Sambuc     {
2314684ddb6SLionel Sambuc     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
2324684ddb6SLionel Sambuc     const int N = sizeof(rng)/sizeof(rng[0]);
2334684ddb6SLionel Sambuc     for (int i = 0; i < N; ++i)
2344684ddb6SLionel Sambuc         for (int j = 0; j < N; ++j)
2354684ddb6SLionel Sambuc             for (int k = 0; k < N; ++k)
2364684ddb6SLionel Sambuc                 testN<std::deque<int> >(rng[i], rng[j], rng[k]);
2374684ddb6SLionel Sambuc     testNI<std::deque<int> >(1500, 2000, 1000);
2384684ddb6SLionel Sambuc #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2394684ddb6SLionel Sambuc     test_move<std::deque<MoveOnly, stack_allocator<MoveOnly, 2000> > >();
2404684ddb6SLionel Sambuc #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2414684ddb6SLionel Sambuc     }
2424684ddb6SLionel Sambuc #if __cplusplus >= 201103L
2434684ddb6SLionel Sambuc     {
2444684ddb6SLionel Sambuc     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
2454684ddb6SLionel Sambuc     const int N = sizeof(rng)/sizeof(rng[0]);
2464684ddb6SLionel Sambuc     for (int i = 0; i < N; ++i)
2474684ddb6SLionel Sambuc         for (int j = 0; j < N; ++j)
2484684ddb6SLionel Sambuc             for (int k = 0; k < N; ++k)
2494684ddb6SLionel Sambuc                 testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j], rng[k]);
2504684ddb6SLionel Sambuc     testNI<std::deque<int> >(1500, 2000, 1000);
2514684ddb6SLionel Sambuc     test_move<std::deque<MoveOnly, min_allocator<MoveOnly> > >();
2524684ddb6SLionel Sambuc     }
2534684ddb6SLionel Sambuc #endif
2544684ddb6SLionel Sambuc }
255