xref: /llvm-project/libcxx/test/std/thread/futures/futures.promise/move_ctor.pass.cpp (revision a7f9895cc18995549c7facb96e72718da282a864)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: no-threads
10 // UNSUPPORTED: c++03
11 
12 // <future>
13 
14 // class promise<R>
15 
16 // promise(promise&& rhs);
17 
18 #include <future>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "test_allocator.h"
23 
main(int,char **)24 int main(int, char**)
25 {
26     test_allocator_statistics alloc_stats;
27     assert(alloc_stats.alloc_count == 0);
28     {
29         std::promise<int> p0(std::allocator_arg, test_allocator<int>(&alloc_stats));
30         std::promise<int> p(std::move(p0));
31         assert(alloc_stats.alloc_count == 1);
32         std::future<int> f = p.get_future();
33         assert(alloc_stats.alloc_count == 1);
34         assert(f.valid());
35 #ifndef TEST_HAS_NO_EXCEPTIONS
36         try
37         {
38             f = p0.get_future();
39             assert(false);
40         }
41         catch (const std::future_error& e)
42         {
43             assert(e.code() == make_error_code(std::future_errc::no_state));
44         }
45         assert(alloc_stats.alloc_count == 1);
46 #endif
47     }
48     assert(alloc_stats.alloc_count == 0);
49     {
50         std::promise<int&> p0(std::allocator_arg, test_allocator<int>(&alloc_stats));
51         std::promise<int&> p(std::move(p0));
52         assert(alloc_stats.alloc_count == 1);
53         std::future<int&> f = p.get_future();
54         assert(alloc_stats.alloc_count == 1);
55         assert(f.valid());
56 #ifndef TEST_HAS_NO_EXCEPTIONS
57         try
58         {
59             f = p0.get_future();
60             assert(false);
61         }
62         catch (const std::future_error& e)
63         {
64             assert(e.code() == make_error_code(std::future_errc::no_state));
65         }
66         assert(alloc_stats.alloc_count == 1);
67 #endif
68     }
69     assert(alloc_stats.alloc_count == 0);
70     {
71         std::promise<void> p0(std::allocator_arg, test_allocator<void>(&alloc_stats));
72         std::promise<void> p(std::move(p0));
73         assert(alloc_stats.alloc_count == 1);
74         std::future<void> f = p.get_future();
75         assert(alloc_stats.alloc_count == 1);
76         assert(f.valid());
77 #ifndef TEST_HAS_NO_EXCEPTIONS
78         try
79         {
80             f = p0.get_future();
81             assert(false);
82         }
83         catch (const std::future_error& e)
84         {
85             assert(e.code() == make_error_code(std::future_errc::no_state));
86         }
87         assert(alloc_stats.alloc_count == 1);
88 #endif
89     }
90     assert(alloc_stats.alloc_count == 0);
91 
92   return 0;
93 }
94