Home | History | Annotate | Download | only in vector
      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 // <vector>
     11 
     12 // iterator       begin();
     13 // iterator       end();
     14 // const_iterator begin()  const;
     15 // const_iterator end()    const;
     16 // const_iterator cbegin() const;
     17 // const_iterator cend()   const;
     18 
     19 #include <vector>
     20 #include <cassert>
     21 #include <iterator>
     22 
     23 #include "test_macros.h"
     24 #include "min_allocator.h"
     25 
     26 struct A
     27 {
     28     int first;
     29     int second;
     30 };
     31 
     32 int main()
     33 {
     34     {
     35         typedef int T;
     36         typedef std::vector<T> C;
     37         C c;
     38         C::iterator i = c.begin();
     39         C::iterator j = c.end();
     40         assert(std::distance(i, j) == 0);
     41         assert(i == j);
     42     }
     43     {
     44         typedef int T;
     45         typedef std::vector<T> C;
     46         const C c;
     47         C::const_iterator i = c.begin();
     48         C::const_iterator j = c.end();
     49         assert(std::distance(i, j) == 0);
     50         assert(i == j);
     51     }
     52     {
     53         typedef int T;
     54         typedef std::vector<T> C;
     55         C c;
     56         C::const_iterator i = c.cbegin();
     57         C::const_iterator j = c.cend();
     58         assert(std::distance(i, j) == 0);
     59         assert(i == j);
     60         assert(i == c.end());
     61     }
     62     {
     63         typedef int T;
     64         typedef std::vector<T> C;
     65         const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
     66         C c(std::begin(t), std::end(t));
     67         C::iterator i = c.begin();
     68         assert(*i == 0);
     69         ++i;
     70         assert(*i == 1);
     71         *i = 10;
     72         assert(*i == 10);
     73         assert(std::distance(c.begin(), c.end()) == 10);
     74     }
     75     {
     76         typedef int T;
     77         typedef std::vector<T> C;
     78         C::iterator i;
     79         C::const_iterator j;
     80     }
     81 #if TEST_STD_VER >= 11
     82     {
     83         typedef int T;
     84         typedef std::vector<T, min_allocator<T>> C;
     85         C c;
     86         C::iterator i = c.begin();
     87         C::iterator j = c.end();
     88         assert(std::distance(i, j) == 0);
     89         assert(i == j);
     90     }
     91     {
     92         typedef int T;
     93         typedef std::vector<T, min_allocator<T>> C;
     94         const C c;
     95         C::const_iterator i = c.begin();
     96         C::const_iterator j = c.end();
     97         assert(std::distance(i, j) == 0);
     98         assert(i == j);
     99     }
    100     {
    101         typedef int T;
    102         typedef std::vector<T, min_allocator<T>> C;
    103         C c;
    104         C::const_iterator i = c.cbegin();
    105         C::const_iterator j = c.cend();
    106         assert(std::distance(i, j) == 0);
    107         assert(i == j);
    108         assert(i == c.end());
    109     }
    110     {
    111         typedef int T;
    112         typedef std::vector<T, min_allocator<T>> C;
    113         const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    114         C c(std::begin(t), std::end(t));
    115         C::iterator i = c.begin();
    116         assert(*i == 0);
    117         ++i;
    118         assert(*i == 1);
    119         *i = 10;
    120         assert(*i == 10);
    121         assert(std::distance(c.begin(), c.end()) == 10);
    122     }
    123     {
    124         typedef int T;
    125         typedef std::vector<T, min_allocator<T>> C;
    126         C::iterator i;
    127         C::const_iterator j;
    128     }
    129     {
    130         typedef A T;
    131         typedef std::vector<T, min_allocator<T>> C;
    132         C c = {A{1, 2}};
    133         C::iterator i = c.begin();
    134         i->first = 3;
    135         C::const_iterator j = i;
    136         assert(j->first == 3);
    137     }
    138 #endif
    139 #if TEST_STD_VER > 11
    140     { // N3644 testing
    141         typedef std::vector<int> C;
    142         C::iterator ii1{}, ii2{};
    143         C::iterator ii4 = ii1;
    144         C::const_iterator cii{};
    145         assert ( ii1 == ii2 );
    146         assert ( ii1 == ii4 );
    147 
    148         assert (!(ii1 != ii2 ));
    149 
    150         assert ( (ii1 == cii ));
    151         assert ( (cii == ii1 ));
    152         assert (!(ii1 != cii ));
    153         assert (!(cii != ii1 ));
    154         assert (!(ii1 <  cii ));
    155         assert (!(cii <  ii1 ));
    156         assert ( (ii1 <= cii ));
    157         assert ( (cii <= ii1 ));
    158         assert (!(ii1 >  cii ));
    159         assert (!(cii >  ii1 ));
    160         assert ( (ii1 >= cii ));
    161         assert ( (cii >= ii1 ));
    162         assert (cii - ii1 == 0);
    163         assert (ii1 - cii == 0);
    164     }
    165 #endif
    166 }
    167