libcxx

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

ctor_func_alloc.pass.cpp (3692B)


      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: libcpp-has-no-threads
     11 // UNSUPPORTED: c++98, c++03
     12 // REQUIRES: c++11 || c++14
     13 // packaged_task allocator support was removed in C++17 (LWG 2921)
     14 
     15 // <future>
     16 
     17 // class packaged_task<R(ArgTypes...)>
     18 
     19 // template <class F, class Allocator>
     20 //     explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
     21 
     22 #include <future>
     23 #include <cassert>
     24 
     25 #include "test_allocator.h"
     26 #include "min_allocator.h"
     27 
     28 class A
     29 {
     30     long data_;
     31 
     32 public:
     33     static int n_moves;
     34     static int n_copies;
     35 
     36     explicit A(long i) : data_(i) {}
     37     A(A&& a) : data_(a.data_) {++n_moves; a.data_ = -1;}
     38     A(const A& a) : data_(a.data_) {++n_copies;}
     39 
     40     long operator()(long i, long j) const {return data_ + i + j;}
     41 };
     42 
     43 int A::n_moves = 0;
     44 int A::n_copies = 0;
     45 
     46 int func(int i) { return i; }
     47 
     48 int main()
     49 {
     50     {
     51         std::packaged_task<double(int, char)> p(std::allocator_arg,
     52                                                 test_allocator<A>(), A(5));
     53         assert(test_alloc_base::alloc_count > 0);
     54         assert(p.valid());
     55         std::future<double> f = p.get_future();
     56         p(3, 'a');
     57         assert(f.get() == 105.0);
     58         assert(A::n_copies == 0);
     59         assert(A::n_moves > 0);
     60     }
     61     assert(test_alloc_base::alloc_count == 0);
     62     A::n_copies = 0;
     63     A::n_moves  = 0;
     64     {
     65         A a(5);
     66         std::packaged_task<double(int, char)> p(std::allocator_arg,
     67                                                 test_allocator<A>(), a);
     68         assert(test_alloc_base::alloc_count > 0);
     69         assert(p.valid());
     70         std::future<double> f = p.get_future();
     71         p(3, 'a');
     72         assert(f.get() == 105.0);
     73         assert(A::n_copies > 0);
     74         assert(A::n_moves >= 0);
     75     }
     76     assert(test_alloc_base::alloc_count == 0);
     77     A::n_copies = 0;
     78     A::n_moves  = 0;
     79     {
     80         A a(5);
     81         std::packaged_task<int(int)> p(std::allocator_arg, test_allocator<A>(), &func);
     82         assert(test_alloc_base::alloc_count > 0);
     83         assert(p.valid());
     84         std::future<int> f = p.get_future();
     85         p(4);
     86         assert(f.get() == 4);
     87     }
     88     assert(test_alloc_base::alloc_count == 0);
     89     A::n_copies = 0;
     90     A::n_moves  = 0;
     91     {
     92         A a(5);
     93         std::packaged_task<int(int)> p(std::allocator_arg, test_allocator<A>(), func);
     94         assert(test_alloc_base::alloc_count > 0);
     95         assert(p.valid());
     96         std::future<int> f = p.get_future();
     97         p(4);
     98         assert(f.get() == 4);
     99     }
    100     assert(test_alloc_base::alloc_count == 0);
    101     A::n_copies = 0;
    102     A::n_moves  = 0;
    103     {
    104         std::packaged_task<double(int, char)> p(std::allocator_arg,
    105                                                 bare_allocator<void>(), A(5));
    106         assert(p.valid());
    107         std::future<double> f = p.get_future();
    108         p(3, 'a');
    109         assert(f.get() == 105.0);
    110         assert(A::n_copies == 0);
    111         assert(A::n_moves > 0);
    112     }
    113     A::n_copies = 0;
    114     A::n_moves  = 0;
    115     {
    116         std::packaged_task<double(int, char)> p(std::allocator_arg,
    117                                                 min_allocator<void>(), A(5));
    118         assert(p.valid());
    119         std::future<double> f = p.get_future();
    120         p(3, 'a');
    121         assert(f.get() == 105.0);
    122         assert(A::n_copies == 0);
    123         assert(A::n_moves > 0);
    124     }
    125     A::n_copies = 0;
    126     A::n_moves  = 0;
    127 }