xref: /llvm-project/libcxx/test/std/thread/futures/futures.promise/move_assign.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& operator=(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::allocator_arg, test_allocator<int>(&alloc_stats));
31         assert(alloc_stats.alloc_count == 2);
32         p = std::move(p0);
33         assert(alloc_stats.alloc_count == 1);
34         std::future<int> f = p.get_future();
35         assert(alloc_stats.alloc_count == 1);
36         assert(f.valid());
37 #ifndef TEST_HAS_NO_EXCEPTIONS
38         try
39         {
40             f = p0.get_future();
41             assert(false);
42         }
43         catch (const std::future_error& e)
44         {
45             assert(e.code() == make_error_code(std::future_errc::no_state));
46         }
47 #endif
48         assert(alloc_stats.alloc_count == 1);
49     }
50     assert(alloc_stats.alloc_count == 0);
51     {
52         std::promise<int&> p0(std::allocator_arg, test_allocator<int>(&alloc_stats));
53         std::promise<int&> p(std::allocator_arg, test_allocator<int>(&alloc_stats));
54         assert(alloc_stats.alloc_count == 2);
55         p = std::move(p0);
56         assert(alloc_stats.alloc_count == 1);
57         std::future<int&> f = p.get_future();
58         assert(alloc_stats.alloc_count == 1);
59         assert(f.valid());
60 #ifndef TEST_HAS_NO_EXCEPTIONS
61         try
62         {
63             f = p0.get_future();
64             assert(false);
65         }
66         catch (const std::future_error& e)
67         {
68             assert(e.code() == make_error_code(std::future_errc::no_state));
69         }
70 #endif
71         assert(alloc_stats.alloc_count == 1);
72     }
73     assert(alloc_stats.alloc_count == 0);
74     {
75         std::promise<void> p0(std::allocator_arg, test_allocator<void>(&alloc_stats));
76         std::promise<void> p(std::allocator_arg, test_allocator<void>(&alloc_stats));
77         assert(alloc_stats.alloc_count == 2);
78         p = std::move(p0);
79         assert(alloc_stats.alloc_count == 1);
80         std::future<void> f = p.get_future();
81         assert(alloc_stats.alloc_count == 1);
82         assert(f.valid());
83 #ifndef TEST_HAS_NO_EXCEPTIONS
84         try
85         {
86             f = p0.get_future();
87             assert(false);
88         }
89         catch (const std::future_error& e)
90         {
91             assert(e.code() == make_error_code(std::future_errc::no_state));
92         }
93 #endif
94         assert(alloc_stats.alloc_count == 1);
95     }
96     assert(alloc_stats.alloc_count == 0);
97 
98   return 0;
99 }
100