libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

move.pass.cpp (2932B)


      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 // UNSUPPORTED: c++98, c++03
     11 
     12 // <vector>
     13 
     14 // vector(vector&& c);
     15 
     16 #include <vector>
     17 #include <cassert>
     18 #include "test_macros.h"
     19 #include "test_allocator.h"
     20 #include "min_allocator.h"
     21 
     22 int main()
     23 {
     24     {
     25         std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5));
     26         std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5));
     27         for (int i = 1; i <= 3; ++i)
     28         {
     29             l.push_back(true);
     30             lo.push_back(true);
     31         }
     32         std::vector<bool, test_allocator<bool> > l2 = std::move(l);
     33         assert(l2 == lo);
     34         assert(l.empty());
     35         assert(l2.get_allocator() == lo.get_allocator());
     36     }
     37     {
     38         std::vector<bool, other_allocator<bool> > l(other_allocator<bool>(5));
     39         std::vector<bool, other_allocator<bool> > lo(other_allocator<bool>(5));
     40         for (int i = 1; i <= 3; ++i)
     41         {
     42             l.push_back(true);
     43             lo.push_back(true);
     44         }
     45         std::vector<bool, other_allocator<bool> > l2 = std::move(l);
     46         assert(l2 == lo);
     47         assert(l.empty());
     48         assert(l2.get_allocator() == lo.get_allocator());
     49     }
     50     {
     51         std::vector<bool, min_allocator<bool> > l(min_allocator<bool>{});
     52         std::vector<bool, min_allocator<bool> > lo(min_allocator<bool>{});
     53         for (int i = 1; i <= 3; ++i)
     54         {
     55             l.push_back(true);
     56             lo.push_back(true);
     57         }
     58         std::vector<bool, min_allocator<bool> > l2 = std::move(l);
     59         assert(l2 == lo);
     60         assert(l.empty());
     61         assert(l2.get_allocator() == lo.get_allocator());
     62     }
     63     {
     64       test_alloc_base::clear();
     65       using Vect = std::vector<bool, test_allocator<bool> >;
     66       using AllocT = Vect::allocator_type;
     67       Vect v(test_allocator<bool>(42, 101));
     68       assert(test_alloc_base::count == 1);
     69       {
     70         const AllocT& a = v.get_allocator();
     71         assert(test_alloc_base::count == 2);
     72         assert(a.get_data() == 42);
     73         assert(a.get_id() == 101);
     74       }
     75       assert(test_alloc_base::count == 1);
     76       test_alloc_base::clear_ctor_counters();
     77 
     78       Vect v2 = std::move(v);
     79       assert(test_alloc_base::count == 2);
     80       assert(test_alloc_base::copied == 0);
     81       assert(test_alloc_base::moved == 1);
     82       {
     83         const AllocT& a = v.get_allocator();
     84         assert(a.get_id() == test_alloc_base::moved_value);
     85         assert(a.get_data() == test_alloc_base::moved_value);
     86       }
     87       {
     88         const AllocT& a = v2.get_allocator();
     89         assert(a.get_id() == 101);
     90         assert(a.get_data() == 42);
     91       }
     92     }
     93 }