2e0cf5d723e2575c7c20c36355a19235e98563b0
[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 main()
41 {
42     {
43         std::packaged_task<double(int, char)> p(std::allocator_arg,
44                                                 test_allocator<A>(), A(5));
45         assert(test_alloc_base::count > 0);
46         assert(p.valid());
47         std::future<double> f = p.get_future();
48         p(3, 'a');
49         assert(f.get() == 105.0);
50         assert(A::n_copies == 0);
51         assert(A::n_moves > 0);
52     }
53     assert(test_alloc_base::count == 0);
54     A::n_copies = 0;
55     A::n_copies = 0;
56     {
57         A a(5);
58         std::packaged_task<double(int, char)> p(std::allocator_arg,
59                                                 test_allocator<A>(), a);
60         assert(test_alloc_base::count > 0);
61         assert(p.valid());
62         std::future<double> f = p.get_future();
63         p(3, 'a');
64         assert(f.get() == 105.0);
65         assert(A::n_copies > 0);
66         assert(A::n_moves > 0);
67     }
68     assert(test_alloc_base::count == 0);
69 }