Home | History | Annotate | Download | only in vector.special
      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 // template <class T, class Alloc>
     13 //   void swap(vector<T,Alloc>& x, vector<T,Alloc>& y);
     14 
     15 #include <vector>
     16 #include <iterator>
     17 #include <cassert>
     18 #include "test_allocator.h"
     19 #include "min_allocator.h"
     20 #include "asan_testing.h"
     21 
     22 int main()
     23 {
     24     {
     25         int a1[] = {1, 3, 7, 9, 10};
     26         int a2[] = {0, 2, 4, 5, 6, 8, 11};
     27         std::vector<int> c1(a1, a1+sizeof(a1)/sizeof(a1[0]));
     28         std::vector<int> c2(a2, a2+sizeof(a2)/sizeof(a2[0]));
     29         assert(is_contiguous_container_asan_correct(c1));
     30         assert(is_contiguous_container_asan_correct(c2));
     31         swap(c1, c2);
     32         assert(c1 == std::vector<int>(a2, a2+sizeof(a2)/sizeof(a2[0])));
     33         assert(c2 == std::vector<int>(a1, a1+sizeof(a1)/sizeof(a1[0])));
     34         assert(is_contiguous_container_asan_correct(c1));
     35         assert(is_contiguous_container_asan_correct(c2));
     36     }
     37     {
     38         int a1[] = {1, 3, 7, 9, 10};
     39         int a2[] = {0, 2, 4, 5, 6, 8, 11};
     40         std::vector<int> c1(a1, a1);
     41         std::vector<int> c2(a2, a2+sizeof(a2)/sizeof(a2[0]));
     42         assert(is_contiguous_container_asan_correct(c1));
     43         assert(is_contiguous_container_asan_correct(c2));
     44         swap(c1, c2);
     45         assert(c1 == std::vector<int>(a2, a2+sizeof(a2)/sizeof(a2[0])));
     46         assert(c2.empty());
     47         assert(distance(c2.begin(), c2.end()) == 0);
     48         assert(is_contiguous_container_asan_correct(c1));
     49         assert(is_contiguous_container_asan_correct(c2));
     50     }
     51     {
     52         int a1[] = {1, 3, 7, 9, 10};
     53         int a2[] = {0, 2, 4, 5, 6, 8, 11};
     54         std::vector<int> c1(a1, a1+sizeof(a1)/sizeof(a1[0]));
     55         std::vector<int> c2(a2, a2);
     56         assert(is_contiguous_container_asan_correct(c1));
     57         assert(is_contiguous_container_asan_correct(c2));
     58         swap(c1, c2);
     59         assert(c1.empty());
     60         assert(distance(c1.begin(), c1.end()) == 0);
     61         assert(c2 == std::vector<int>(a1, a1+sizeof(a1)/sizeof(a1[0])));
     62         assert(is_contiguous_container_asan_correct(c1));
     63         assert(is_contiguous_container_asan_correct(c2));
     64     }
     65     {
     66         int a1[] = {1, 3, 7, 9, 10};
     67         int a2[] = {0, 2, 4, 5, 6, 8, 11};
     68         std::vector<int> c1(a1, a1);
     69         std::vector<int> c2(a2, a2);
     70         assert(is_contiguous_container_asan_correct(c1));
     71         assert(is_contiguous_container_asan_correct(c2));
     72         swap(c1, c2);
     73         assert(c1.empty());
     74         assert(distance(c1.begin(), c1.end()) == 0);
     75         assert(c2.empty());
     76         assert(distance(c2.begin(), c2.end()) == 0);
     77         assert(is_contiguous_container_asan_correct(c1));
     78         assert(is_contiguous_container_asan_correct(c2));
     79     }
     80     {
     81         int a1[] = {1, 3, 7, 9, 10};
     82         int a2[] = {0, 2, 4, 5, 6, 8, 11};
     83         typedef test_allocator<int> A;
     84         std::vector<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1, 1));
     85         std::vector<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(1, 2));
     86         swap(c1, c2);
     87         assert((c1 == std::vector<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
     88         assert(c1.get_allocator().get_id() == 1);
     89         assert((c2 == std::vector<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
     90         assert(c2.get_allocator().get_id() == 2);
     91     }
     92     {
     93         int a1[] = {1, 3, 7, 9, 10};
     94         int a2[] = {0, 2, 4, 5, 6, 8, 11};
     95         typedef other_allocator<int> A;
     96         std::vector<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1));
     97         std::vector<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2));
     98         assert(is_contiguous_container_asan_correct(c1));
     99         assert(is_contiguous_container_asan_correct(c2));
    100         swap(c1, c2);
    101         assert((c1 == std::vector<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
    102         assert(c1.get_allocator() == A(2));
    103         assert((c2 == std::vector<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
    104         assert(c2.get_allocator() == A(1));
    105         assert(is_contiguous_container_asan_correct(c1));
    106         assert(is_contiguous_container_asan_correct(c2));
    107     }
    108 #if TEST_STD_VER >= 11
    109     {
    110         int a1[] = {1, 3, 7, 9, 10};
    111         int a2[] = {0, 2, 4, 5, 6, 8, 11};
    112         std::vector<int, min_allocator<int>> c1(a1, a1+sizeof(a1)/sizeof(a1[0]));
    113         std::vector<int, min_allocator<int>> c2(a2, a2+sizeof(a2)/sizeof(a2[0]));
    114         assert(is_contiguous_container_asan_correct(c1));
    115         assert(is_contiguous_container_asan_correct(c2));
    116         swap(c1, c2);
    117         assert((c1 == std::vector<int, min_allocator<int>>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
    118         assert((c2 == std::vector<int, min_allocator<int>>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
    119         assert(is_contiguous_container_asan_correct(c1));
    120         assert(is_contiguous_container_asan_correct(c2));
    121     }
    122     {
    123         int a1[] = {1, 3, 7, 9, 10};
    124         int a2[] = {0, 2, 4, 5, 6, 8, 11};
    125         std::vector<int, min_allocator<int>> c1(a1, a1);
    126         std::vector<int, min_allocator<int>> c2(a2, a2+sizeof(a2)/sizeof(a2[0]));
    127         assert(is_contiguous_container_asan_correct(c1));
    128         assert(is_contiguous_container_asan_correct(c2));
    129         swap(c1, c2);
    130         assert((c1 == std::vector<int, min_allocator<int>>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
    131         assert(c2.empty());
    132         assert(distance(c2.begin(), c2.end()) == 0);
    133         assert(is_contiguous_container_asan_correct(c1));
    134         assert(is_contiguous_container_asan_correct(c2));
    135     }
    136     {
    137         int a1[] = {1, 3, 7, 9, 10};
    138         int a2[] = {0, 2, 4, 5, 6, 8, 11};
    139         std::vector<int, min_allocator<int>> c1(a1, a1+sizeof(a1)/sizeof(a1[0]));
    140         std::vector<int, min_allocator<int>> c2(a2, a2);
    141         assert(is_contiguous_container_asan_correct(c1));
    142         assert(is_contiguous_container_asan_correct(c2));
    143         swap(c1, c2);
    144         assert(c1.empty());
    145         assert(distance(c1.begin(), c1.end()) == 0);
    146         assert((c2 == std::vector<int, min_allocator<int>>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
    147         assert(is_contiguous_container_asan_correct(c1));
    148         assert(is_contiguous_container_asan_correct(c2));
    149     }
    150     {
    151         int a1[] = {1, 3, 7, 9, 10};
    152         int a2[] = {0, 2, 4, 5, 6, 8, 11};
    153         std::vector<int, min_allocator<int>> c1(a1, a1);
    154         std::vector<int, min_allocator<int>> c2(a2, a2);
    155         assert(is_contiguous_container_asan_correct(c1));
    156         assert(is_contiguous_container_asan_correct(c2));
    157         swap(c1, c2);
    158         assert(c1.empty());
    159         assert(distance(c1.begin(), c1.end()) == 0);
    160         assert(c2.empty());
    161         assert(distance(c2.begin(), c2.end()) == 0);
    162         assert(is_contiguous_container_asan_correct(c1));
    163         assert(is_contiguous_container_asan_correct(c2));
    164     }
    165     {
    166         int a1[] = {1, 3, 7, 9, 10};
    167         int a2[] = {0, 2, 4, 5, 6, 8, 11};
    168         typedef min_allocator<int> A;
    169         std::vector<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A());
    170         std::vector<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A());
    171         assert(is_contiguous_container_asan_correct(c1));
    172         assert(is_contiguous_container_asan_correct(c2));
    173         swap(c1, c2);
    174         assert((c1 == std::vector<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
    175         assert(c1.get_allocator() == A());
    176         assert((c2 == std::vector<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
    177         assert(c2.get_allocator() == A());
    178         assert(is_contiguous_container_asan_correct(c1));
    179         assert(is_contiguous_container_asan_correct(c2));
    180     }
    181 #endif
    182 }
    183