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