Upstream version 8.36.169.0
[platform/framework/web/crosswalk.git] / src / third_party / libc++ / trunk / test / thread / futures / futures.tas / futures.task.members / ctor_func_alloc.pass.cpp
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 // <future>
11
12 // class packaged_task<R(ArgTypes...)>
13
14 // template <class F, class Allocator>
15 //     explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
16
17 #include <future>
18 #include <cassert>
19
20 #include "../../test_allocator.h"
21
22 class A
23 {
24     long data_;
25
26 public:
27     static int n_moves;
28     static int n_copies;
29
30     explicit A(long i) : data_(i) {}
31     A(A&& a) : data_(a.data_) {++n_moves; a.data_ = -1;}
32     A(const A& a) : data_(a.data_) {++n_copies;}
33
34     long operator()(long i, long j) const {return data_ + i + j;}
35 };
36
37 int A::n_moves = 0;
38 int A::n_copies = 0;
39
40 int func(int i) { return i; }
41
42 int main()
43 {
44     {
45         std::packaged_task<double(int, char)> p(std::allocator_arg,
46                                                 test_allocator<A>(), A(5));
47         assert(test_alloc_base::count > 0);
48         assert(p.valid());
49         std::future<double> f = p.get_future();
50         p(3, 'a');
51         assert(f.get() == 105.0);
52         assert(A::n_copies == 0);
53         assert(A::n_moves > 0);
54     }
55     assert(test_alloc_base::count == 0);
56     A::n_copies = 0;
57     A::n_moves  = 0;
58     {
59         A a(5);
60         std::packaged_task<double(int, char)> p(std::allocator_arg,
61                                                 test_allocator<A>(), a);
62         assert(test_alloc_base::count > 0);
63         assert(p.valid());
64         std::future<double> f = p.get_future();
65         p(3, 'a');
66         assert(f.get() == 105.0);
67         assert(A::n_copies > 0);
68         assert(A::n_moves > 0);
69     }
70     assert(test_alloc_base::count == 0);
71     A::n_copies = 0;
72     A::n_moves  = 0;
73     {
74         A a(5);
75         std::packaged_task<int(int)> p(std::allocator_arg, test_allocator<A>(), &func);
76         assert(test_alloc_base::count > 0);
77         assert(p.valid());
78         std::future<int> f = p.get_future();
79         p(4);
80         assert(f.get() == 4);
81     }
82     assert(test_alloc_base::count == 0);
83     A::n_copies = 0;
84     A::n_moves  = 0;
85     {
86         A a(5);
87         std::packaged_task<int(int)> p(std::allocator_arg, test_allocator<A>(), func);
88         assert(test_alloc_base::count > 0);
89         assert(p.valid());
90         std::future<int> f = p.get_future();
91         p(4);
92         assert(f.get() == 4);
93     }
94     assert(test_alloc_base::count == 0);
95 }