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();
17
18#include <future>
19#include <cassert>
20
21#include "test_macros.h"
22
23int main(int, char**)
24{
25 {
26 typedef int T;
27 std::future<T> f;
28 {
29 std::promise<T> p;
30 f = p.get_future();
31 p.set_value(3);
32 }
33 assert(f.get() == 3);
34 }
35#ifndef TEST_HAS_NO_EXCEPTIONS
36 {
37 typedef int T;
38 std::future<T> f;
39 {
40 std::promise<T> p;
41 f = p.get_future();
42 }
43 try
44 {
45 T i = f.get();
46 ((void)i); // Prevent unused warning
47 assert(false);
48 }
49 catch (const std::future_error& e)
50 {
51 assert(e.code() == make_error_code(std::future_errc::broken_promise));
52 }
53 }
54#endif
55
56 {
57 typedef int& T;
58 int i = 4;
59 std::future<T> f;
60 {
61 std::promise<T> p;
62 f = p.get_future();
63 p.set_value(i);
64 }
65 assert(&f.get() == &i);
66 }
67#ifndef TEST_HAS_NO_EXCEPTIONS
68 {
69 typedef int& T;
70 std::future<T> f;
71 {
72 std::promise<T> p;
73 f = p.get_future();
74 }
75 try
76 {
77 T i = f.get();
78 ((void)i); // Prevent unused warning
79 assert(false);
80 }
81 catch (const std::future_error& e)
82 {
83 assert(e.code() == make_error_code(std::future_errc::broken_promise));
84 }
85 }
86#endif
87
88 {
89 typedef void T;
90 std::future<T> f;
91 {
92 std::promise<T> p;
93 f = p.get_future();
94 p.set_value();
95 }
96 f.get();
97 assert(true);
98 }
99#ifndef TEST_HAS_NO_EXCEPTIONS
100 {
101 typedef void T;
102 std::future<T> f;
103 {
104 std::promise<T> p;
105 f = p.get_future();
106 }
107 try
108 {
109 f.get();
110 assert(false);
111 }
112 catch (const std::future_error& e)
113 {
114 // LWG 2056 changed the values of future_errc, so if we're using new
115 // headers with an old library the error codes won't line up.
116 //
117 // Note that this particular check only applies to promise<void>
118 // since the other specializations happen to be implemented in the
119 // header rather than the library.
120 assert(
121 e.code() == make_error_code(std::future_errc::broken_promise) ||
122 e.code() == std::error_code(0, std::future_category()));
123 }
124 }
125#endif
126
127 return 0;
128}
129

source code of libcxx/test/std/thread/futures/futures.promise/dtor.pass.cpp