Home | History | Annotate | Download | only in forwardlist.ops
      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 // <forward_list>
     11 
     12 // void splice_after(const_iterator p, forward_list&& x, const_iterator i);
     13 
     14 #include <forward_list>
     15 #include <cassert>
     16 #include <iterator>
     17 
     18 #include "min_allocator.h"
     19 
     20 typedef int T;
     21 const T t1[] = {0, 1, 2, 3, 4, 5, 6, 7};
     22 const T t2[] = {10, 11, 12};
     23 const int size_t1 = std::end(t1) - std::begin(t1);
     24 const int size_t2 = std::end(t2) - std::begin(t2);
     25 
     26 template <class C>
     27 void
     28 testd(const C& c, int p, int f)
     29 {
     30     typename C::const_iterator i = c.begin();
     31     int n1 = 0;
     32     for (; n1 < p; ++n1, ++i)
     33         assert(*i == t1[n1]);
     34     for (int n2 = f; n2 < f+1; ++n2, ++i)
     35         assert(*i == t2[n2]);
     36     for (; n1 < size_t1; ++n1, ++i)
     37         assert(*i == t1[n1]);
     38     assert(distance(c.begin(), c.end()) == size_t1 + 1);
     39 }
     40 
     41 template <class C>
     42 void
     43 tests(const C& c, int p, int f)
     44 {
     45     typename C::const_iterator i = c.begin();
     46     int n = 0;
     47     int d = 1;
     48     if (p == f || p == f+1)
     49     {
     50         for (n = 0; n < size_t1; ++n, ++i)
     51             assert(*i == t1[n]);
     52     }
     53     else if (p < f)
     54     {
     55         for (n = 0; n < p; ++n, ++i)
     56             assert(*i == t1[n]);
     57         for (n = f; n < f+1; ++n, ++i)
     58             assert(*i == t1[n]);
     59         for (n = p; n < f; ++n, ++i)
     60             assert(*i == t1[n]);
     61         for (n = f+1; n < size_t1; ++n, ++i)
     62             assert(*i == t1[n]);
     63     }
     64     else // p > f+1
     65     {
     66         for (n = 0; n < f; ++n, ++i)
     67             assert(*i == t1[n]);
     68         for (n = f+1; n < p; ++n, ++i)
     69             assert(*i == t1[n]);
     70         for (n = f; n < f+1; ++n, ++i)
     71             assert(*i == t1[n]);
     72         for (n = p; n < size_t1; ++n, ++i)
     73             assert(*i == t1[n]);
     74     }
     75     assert(distance(c.begin(), c.end()) == size_t1);
     76 }
     77 
     78 int main()
     79 {
     80     {
     81     // splicing different containers
     82     typedef std::forward_list<T> C;
     83     for (int f = 0; f <= size_t2-1; ++f)
     84     {
     85         for (int p = 0; p <= size_t1; ++p)
     86         {
     87             C c1(std::begin(t1), std::end(t1));
     88             C c2(std::begin(t2), std::end(t2));
     89 
     90             c1.splice_after(next(c1.cbefore_begin(), p), std::move(c2),
     91                   next(c2.cbefore_begin(), f));
     92             testd(c1, p, f);
     93         }
     94     }
     95 
     96     // splicing within same container
     97     for (int f = 0; f <= size_t1-1; ++f)
     98     {
     99         for (int p = 0; p <= size_t1; ++p)
    100         {
    101             C c1(std::begin(t1), std::end(t1));
    102 
    103             c1.splice_after(next(c1.cbefore_begin(), p), std::move(c1),
    104                   next(c1.cbefore_begin(), f));
    105             tests(c1, p, f);
    106         }
    107     }
    108     }
    109 #if __cplusplus >= 201103L
    110     {
    111     // splicing different containers
    112     typedef std::forward_list<T, min_allocator<T>> C;
    113     for (int f = 0; f <= size_t2-1; ++f)
    114     {
    115         for (int p = 0; p <= size_t1; ++p)
    116         {
    117             C c1(std::begin(t1), std::end(t1));
    118             C c2(std::begin(t2), std::end(t2));
    119 
    120             c1.splice_after(next(c1.cbefore_begin(), p), std::move(c2),
    121                   next(c2.cbefore_begin(), f));
    122             testd(c1, p, f);
    123         }
    124     }
    125 
    126     // splicing within same container
    127     for (int f = 0; f <= size_t1-1; ++f)
    128     {
    129         for (int p = 0; p <= size_t1; ++p)
    130         {
    131             C c1(std::begin(t1), std::end(t1));
    132 
    133             c1.splice_after(next(c1.cbefore_begin(), p), std::move(c1),
    134                   next(c1.cbefore_begin(), f));
    135             tests(c1, p, f);
    136         }
    137     }
    138     }
    139 #endif
    140 }
    141