Home | History | Annotate | Download | only in deque.cons
      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 //   void assign(InputIterator f, InputIterator l);
     14 
     15 #include <deque>
     16 #include <cassert>
     17 #include <cstddef>
     18 
     19 #include "test_macros.h"
     20 #include "test_iterators.h"
     21 #include "min_allocator.h"
     22 #if TEST_STD_VER >= 11
     23 #include "emplace_constructible.h"
     24 #endif
     25 
     26 template <class C>
     27 C
     28 make(int size, int start = 0 )
     29 {
     30     const int b = 4096 / sizeof(int);
     31     int init = 0;
     32     if (start > 0)
     33     {
     34         init = (start+1) / b + ((start+1) % b != 0);
     35         init *= b;
     36         --init;
     37     }
     38     C c(init, 0);
     39     for (int i = 0; i < init-start; ++i)
     40         c.pop_back();
     41     for (int i = 0; i < size; ++i)
     42         c.push_back(i);
     43     for (int i = 0; i < start; ++i)
     44         c.pop_front();
     45     return c;
     46 }
     47 
     48 template <class C>
     49 void
     50 test(C& c1, const C& c2)
     51 {
     52     c1.assign(c2.begin(), c2.end());
     53     assert(static_cast<std::size_t>(distance(c1.begin(), c1.end())) == c1.size());
     54     assert(c1 == c2);
     55 }
     56 
     57 template <class C>
     58 void
     59 testN(int start, int N, int M)
     60 {
     61     C c1 = make<C>(N, start);
     62     C c2 = make<C>(M);
     63     test(c1, c2);
     64 }
     65 
     66 template <class C>
     67 void
     68 testI(C& c1, const C& c2)
     69 {
     70     typedef typename C::const_iterator CI;
     71     typedef input_iterator<CI> ICI;
     72     c1.assign(ICI(c2.begin()), ICI(c2.end()));
     73     assert(static_cast<std::size_t>(distance(c1.begin(), c1.end())) == c1.size());
     74     assert(c1 == c2);
     75 }
     76 
     77 template <class C>
     78 void
     79 testNI(int start, int N, int M)
     80 {
     81     C c1 = make<C>(N, start);
     82     C c2 = make<C>(M);
     83     testI(c1, c2);
     84 }
     85 
     86 void basic_test()
     87 {
     88     {
     89     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
     90     const int N = sizeof(rng)/sizeof(rng[0]);
     91     for (int i = 0; i < N; ++i)
     92         for (int j = 0; j < N; ++j)
     93             for (int k = 0; k < N; ++k)
     94                 testN<std::deque<int> >(rng[i], rng[j], rng[k]);
     95     testNI<std::deque<int> >(1500, 2000, 1000);
     96     }
     97 #if TEST_STD_VER >= 11
     98     {
     99     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
    100     const int N = sizeof(rng)/sizeof(rng[0]);
    101     for (int i = 0; i < N; ++i)
    102         for (int j = 0; j < N; ++j)
    103             for (int k = 0; k < N; ++k)
    104                 testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j], rng[k]);
    105     testNI<std::deque<int, min_allocator<int>> >(1500, 2000, 1000);
    106     }
    107 #endif
    108 }
    109 
    110 void test_emplacable_concept() {
    111 #if TEST_STD_VER >= 11
    112   int arr1[] = {42};
    113   int arr2[] = {1, 101, 42};
    114   {
    115     using T = EmplaceConstructibleMoveableAndAssignable<int>;
    116     using It = random_access_iterator<int*>;
    117     {
    118       std::deque<T> v;
    119       v.assign(It(arr1), It(std::end(arr1)));
    120       assert(v[0].value == 42);
    121     }
    122     {
    123       std::deque<T> v;
    124       v.assign(It(arr2), It(std::end(arr2)));
    125       assert(v[0].value == 1);
    126       assert(v[1].value == 101);
    127       assert(v[2].value == 42);
    128     }
    129   }
    130   {
    131     using T = EmplaceConstructibleMoveableAndAssignable<int>;
    132     using It = input_iterator<int*>;
    133     {
    134       std::deque<T> v;
    135       v.assign(It(arr1), It(std::end(arr1)));
    136       assert(v[0].copied == 0);
    137       assert(v[0].value == 42);
    138     }
    139     {
    140       std::deque<T> v;
    141       v.assign(It(arr2), It(std::end(arr2)));
    142       //assert(v[0].copied == 0);
    143       assert(v[0].value == 1);
    144       //assert(v[1].copied == 0);
    145       assert(v[1].value == 101);
    146       assert(v[2].copied == 0);
    147       assert(v[2].value == 42);
    148     }
    149   }
    150 #endif
    151 }
    152 
    153 int main() {
    154   basic_test();
    155   test_emplacable_concept();
    156 }
    157