Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // UNSUPPORTED: libcpp-has-no-threads |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 11 | |
| 12 | // <future> |
| 13 | |
| 14 | // class packaged_task<R(ArgTypes...)> |
| 15 | |
| 16 | // template <class F, class Allocator> |
| 17 | // explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f); |
| 18 | |
| 19 | #include <future> |
| 20 | #include <cassert> |
| 21 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 22 | #include "../../test_allocator.h" |
Eric Fiselier | 4d2413c | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 23 | #include "min_allocator.h" |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 24 | |
| 25 | class A |
| 26 | { |
| 27 | long data_; |
| 28 | |
| 29 | public: |
| 30 | static int n_moves; |
| 31 | static int n_copies; |
| 32 | |
| 33 | explicit A(long i) : data_(i) {} |
| 34 | A(A&& a) : data_(a.data_) {++n_moves; a.data_ = -1;} |
| 35 | A(const A& a) : data_(a.data_) {++n_copies;} |
| 36 | |
| 37 | long operator()(long i, long j) const {return data_ + i + j;} |
| 38 | }; |
| 39 | |
| 40 | int A::n_moves = 0; |
| 41 | int A::n_copies = 0; |
| 42 | |
Marshall Clow | f1264e7 | 2014-04-07 13:32:26 +0000 | [diff] [blame] | 43 | int func(int i) { return i; } |
| 44 | |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 45 | int main() |
| 46 | { |
| 47 | { |
| 48 | std::packaged_task<double(int, char)> p(std::allocator_arg, |
| 49 | test_allocator<A>(), A(5)); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 50 | assert(test_alloc_base::count > 0); |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 51 | assert(p.valid()); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 52 | std::future<double> f = p.get_future(); |
| 53 | p(3, 'a'); |
| 54 | assert(f.get() == 105.0); |
| 55 | assert(A::n_copies == 0); |
| 56 | assert(A::n_moves > 0); |
| 57 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 58 | assert(test_alloc_base::count == 0); |
Howard Hinnant | 97ecd64 | 2011-05-17 19:12:55 +0000 | [diff] [blame] | 59 | A::n_copies = 0; |
Marshall Clow | f1264e7 | 2014-04-07 13:32:26 +0000 | [diff] [blame] | 60 | A::n_moves = 0; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 61 | { |
| 62 | A a(5); |
| 63 | std::packaged_task<double(int, char)> p(std::allocator_arg, |
| 64 | test_allocator<A>(), a); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 65 | assert(test_alloc_base::count > 0); |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 66 | assert(p.valid()); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 67 | std::future<double> f = p.get_future(); |
| 68 | p(3, 'a'); |
| 69 | assert(f.get() == 105.0); |
| 70 | assert(A::n_copies > 0); |
| 71 | assert(A::n_moves > 0); |
| 72 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 73 | assert(test_alloc_base::count == 0); |
Marshall Clow | f1264e7 | 2014-04-07 13:32:26 +0000 | [diff] [blame] | 74 | A::n_copies = 0; |
| 75 | A::n_moves = 0; |
| 76 | { |
| 77 | A a(5); |
| 78 | std::packaged_task<int(int)> p(std::allocator_arg, test_allocator<A>(), &func); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 79 | assert(test_alloc_base::count > 0); |
Marshall Clow | f1264e7 | 2014-04-07 13:32:26 +0000 | [diff] [blame] | 80 | assert(p.valid()); |
| 81 | std::future<int> f = p.get_future(); |
| 82 | p(4); |
| 83 | assert(f.get() == 4); |
| 84 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 85 | assert(test_alloc_base::count == 0); |
Marshall Clow | f1264e7 | 2014-04-07 13:32:26 +0000 | [diff] [blame] | 86 | A::n_copies = 0; |
| 87 | A::n_moves = 0; |
| 88 | { |
| 89 | A a(5); |
| 90 | std::packaged_task<int(int)> p(std::allocator_arg, test_allocator<A>(), func); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 91 | assert(test_alloc_base::count > 0); |
Marshall Clow | f1264e7 | 2014-04-07 13:32:26 +0000 | [diff] [blame] | 92 | assert(p.valid()); |
| 93 | std::future<int> f = p.get_future(); |
| 94 | p(4); |
| 95 | assert(f.get() == 4); |
| 96 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 97 | assert(test_alloc_base::count == 0); |
Eric Fiselier | 4d2413c | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 98 | A::n_copies = 0; |
| 99 | A::n_moves = 0; |
| 100 | { |
| 101 | std::packaged_task<double(int, char)> p(std::allocator_arg, |
| 102 | bare_allocator<void>(), A(5)); |
| 103 | assert(p.valid()); |
| 104 | std::future<double> f = p.get_future(); |
| 105 | p(3, 'a'); |
| 106 | assert(f.get() == 105.0); |
| 107 | assert(A::n_copies == 0); |
| 108 | assert(A::n_moves > 0); |
| 109 | } |
| 110 | A::n_copies = 0; |
| 111 | A::n_moves = 0; |
| 112 | { |
| 113 | std::packaged_task<double(int, char)> p(std::allocator_arg, |
| 114 | min_allocator<void>(), A(5)); |
| 115 | assert(p.valid()); |
| 116 | std::future<double> f = p.get_future(); |
| 117 | p(3, 'a'); |
| 118 | assert(f.get() == 105.0); |
| 119 | assert(A::n_copies == 0); |
| 120 | assert(A::n_moves > 0); |
| 121 | } |
| 122 | A::n_copies = 0; |
| 123 | A::n_moves = 0; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 124 | } |