xref: /llvm-project/libcxx/test/std/thread/futures/futures.promise/get_future.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 // future<R> get_future();
17 
18 #include <future>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 
main(int,char **)23 int main(int, char**)
24 {
25     {
26         std::promise<double> p;
27         std::future<double> f = p.get_future();
28         p.set_value(105.5);
29         assert(f.get() == 105.5);
30     }
31 #ifndef TEST_HAS_NO_EXCEPTIONS
32     {
33         std::promise<double> p;
34         std::future<double> f = p.get_future();
35         try
36         {
37             f = p.get_future();
38             assert(false);
39         }
40         catch (const std::future_error& e)
41         {
42             assert(e.code() ==  make_error_code(std::future_errc::future_already_retrieved));
43         }
44     }
45     {
46         std::promise<double> p;
47         std::promise<double> p0 = std::move(p);
48         try
49         {
50             std::future<double> f = p.get_future();
51             assert(false);
52         }
53         catch (const std::future_error& e)
54         {
55             assert(e.code() ==  make_error_code(std::future_errc::no_state));
56         }
57     }
58 #endif
59 
60   return 0;
61 }
62