]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/promise/members/at_thread_exit2.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / promise / members / at_thread_exit2.cc
1 // { dg-do run }
2 // { dg-options "-pthread" }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-effective-target pthread }
5 // { dg-require-cstdint "" }
6 // { dg-require-gthreads "" }
7
8 // Copyright (C) 2014-2018 Free Software Foundation, Inc.
9 //
10 // This file is part of the GNU ISO C++ Library. This library is free
11 // software; you can redistribute it and/or modify it under the
12 // terms of the GNU General Public License as published by the
13 // Free Software Foundation; either version 3, or (at your option)
14 // any later version.
15
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20
21 // You should have received a copy of the GNU General Public License along
22 // with this library; see the file COPYING3. If not see
23 // <http://www.gnu.org/licenses/>.
24
25 // Test set_value_at_thread_exit error conditions
26
27 #include <future>
28 #include <testsuite_hooks.h>
29
30 void test01()
31 {
32 std::promise<int> p1;
33 p1.set_value(1);
34 try
35 {
36 p1.set_value_at_thread_exit(2);
37 VERIFY( false );
38 }
39 catch (std::future_error& e)
40 {
41 VERIFY( e.code() == std::future_errc::promise_already_satisfied );
42 }
43 try
44 {
45 p1.set_exception_at_thread_exit(std::make_exception_ptr(3));
46 VERIFY( false );
47 }
48 catch (std::future_error& e)
49 {
50 VERIFY( e.code() == std::future_errc::promise_already_satisfied );
51 }
52
53 std::promise<int> p2(std::move(p1));
54 try
55 {
56 p1.set_value_at_thread_exit(2);
57 VERIFY( false );
58 }
59 catch (std::future_error& e)
60 {
61 VERIFY( e.code() == std::future_errc::no_state );
62 }
63 try
64 {
65 p1.set_exception_at_thread_exit(std::make_exception_ptr(3));
66 VERIFY( false );
67 }
68 catch (std::future_error& e)
69 {
70 VERIFY( e.code() == std::future_errc::no_state );
71 }
72 }
73
74 void test02()
75 {
76 std::promise<int&> p1;
77 int i = 1;
78 p1.set_value(i);
79 try
80 {
81 p1.set_value_at_thread_exit(i);
82 VERIFY( false );
83 }
84 catch (std::future_error& e)
85 {
86 VERIFY( e.code() == std::future_errc::promise_already_satisfied );
87 }
88 try
89 {
90 p1.set_exception_at_thread_exit(std::make_exception_ptr(3));
91 VERIFY( false );
92 }
93 catch (std::future_error& e)
94 {
95 VERIFY( e.code() == std::future_errc::promise_already_satisfied );
96 }
97
98 std::promise<int&> p2(std::move(p1));
99 try
100 {
101 int i = 0;
102 p1.set_value_at_thread_exit(i);
103 VERIFY( false );
104 }
105 catch (std::future_error& e)
106 {
107 VERIFY( e.code() == std::future_errc::no_state );
108 }
109 try
110 {
111 p1.set_exception_at_thread_exit(std::make_exception_ptr(3));
112 VERIFY( false );
113 }
114 catch (std::future_error& e)
115 {
116 VERIFY( e.code() == std::future_errc::no_state );
117 }
118 }
119
120 void test03()
121 {
122 std::promise<void> p1;
123 int i = 0;
124 p1.set_value();
125 try {
126 p1.set_value_at_thread_exit();
127 VERIFY( false );
128 }
129 catch (std::future_error& e)
130 {
131 VERIFY( e.code() == std::future_errc::promise_already_satisfied );
132 }
133 try
134 {
135 p1.set_exception_at_thread_exit(std::make_exception_ptr(3));
136 VERIFY( false );
137 }
138 catch (std::future_error& e)
139 {
140 VERIFY( e.code() == std::future_errc::promise_already_satisfied );
141 }
142
143 std::promise<void> p2(std::move(p1));
144 try {
145 p1.set_value_at_thread_exit();
146 VERIFY( false );
147 }
148 catch (std::future_error& e)
149 {
150 VERIFY( e.code() == std::future_errc::no_state );
151 }
152 try
153 {
154 p1.set_exception_at_thread_exit(std::make_exception_ptr(3));
155 VERIFY( false );
156 }
157 catch (std::future_error& e)
158 {
159 VERIFY( e.code() == std::future_errc::no_state );
160 }
161 }
162
163 int main()
164 {
165 test01();
166 test02();
167 test03();
168 }