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) 2009-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 that promise::set_exception throws when required. 26 27 #include <future> 28 #include <testsuite_hooks.h> 29 30 // Check for promise_already_satisfied error conditions. 31 test01()32void test01() 33 { 34 bool test = false; 35 36 std::promise<int> p1; 37 std::future<int> f1 = p1.get_future(); 38 39 p1.set_exception(std::make_exception_ptr(0)); 40 41 try 42 { 43 p1.set_exception(std::make_exception_ptr(1)); 44 VERIFY( false ); 45 } 46 catch (std::future_error& e) 47 { 48 VERIFY(e.code() == 49 std::make_error_code(std::future_errc::promise_already_satisfied)); 50 test = true; 51 } 52 53 try 54 { 55 f1.get(); 56 test = false; 57 } 58 catch(int i) 59 { 60 VERIFY( i == 0 ); 61 } 62 63 VERIFY( test ); 64 } 65 test02()66void test02() 67 { 68 bool test = false; 69 70 std::promise<int> p1; 71 std::future<int> f1 = p1.get_future(); 72 73 p1.set_value(2); 74 75 try 76 { 77 p1.set_exception(std::make_exception_ptr(0)); 78 VERIFY( false ); 79 } 80 catch (std::future_error& e) 81 { 82 VERIFY(e.code() == 83 std::make_error_code(std::future_errc::promise_already_satisfied)); 84 test = true; 85 } 86 87 VERIFY( test ); 88 } 89 test03()90void test03() 91 { 92 bool test = false; 93 94 std::promise<int&> p1; 95 std::future<int&> f1 = p1.get_future(); 96 97 p1.set_exception(std::make_exception_ptr(0)); 98 99 try 100 { 101 p1.set_exception(std::make_exception_ptr(1)); 102 VERIFY( false ); 103 } 104 catch (std::future_error& e) 105 { 106 VERIFY(e.code() == 107 std::make_error_code(std::future_errc::promise_already_satisfied)); 108 test = true; 109 } 110 111 try 112 { 113 f1.get(); 114 test = false; 115 } 116 catch(int i) 117 { 118 VERIFY( i == 0 ); 119 } 120 121 VERIFY( test ); 122 } 123 test04()124void test04() 125 { 126 bool test = false; 127 128 std::promise<int&> p1; 129 std::future<int&> f1 = p1.get_future(); 130 131 int i = 2; 132 p1.set_value(i); 133 134 try 135 { 136 p1.set_exception(std::make_exception_ptr(0)); 137 VERIFY( false ); 138 } 139 catch (std::future_error& e) 140 { 141 VERIFY(e.code() == 142 std::make_error_code(std::future_errc::promise_already_satisfied)); 143 test = true; 144 } 145 146 VERIFY( test ); 147 } 148 test05()149void test05() 150 { 151 bool test = false; 152 153 std::promise<void> p1; 154 std::future<void> f1 = p1.get_future(); 155 156 p1.set_exception(std::make_exception_ptr(0)); 157 158 try 159 { 160 p1.set_exception(std::make_exception_ptr(1)); 161 VERIFY( false ); 162 } 163 catch (std::future_error& e) 164 { 165 VERIFY(e.code() == 166 std::make_error_code(std::future_errc::promise_already_satisfied)); 167 test = true; 168 } 169 170 try 171 { 172 f1.get(); 173 test = false; 174 } 175 catch(int i) 176 { 177 VERIFY( i == 0 ); 178 } 179 180 VERIFY( test ); 181 } 182 test06()183void test06() 184 { 185 bool test = false; 186 187 std::promise<void> p1; 188 std::future<void> f1 = p1.get_future(); 189 190 p1.set_value(); 191 192 try 193 { 194 p1.set_exception(std::make_exception_ptr(0)); 195 VERIFY( false ); 196 } 197 catch (std::future_error& e) 198 { 199 VERIFY(e.code() == 200 std::make_error_code(std::future_errc::promise_already_satisfied)); 201 test = true; 202 } 203 204 VERIFY( test ); 205 } 206 207 // Check for no_state error condition (PR libstdc++/80316) 208 test07()209void test07() 210 { 211 using namespace std; 212 213 promise<int> p1; 214 promise<int> p2(std::move(p1)); 215 try 216 { 217 p1.set_exception(std::make_exception_ptr(1)); 218 VERIFY( false ); 219 } 220 catch (std::future_error& e) 221 { 222 VERIFY(e.code() == make_error_code(future_errc::no_state)); 223 } 224 } 225 test08()226void test08() 227 { 228 using namespace std; 229 230 promise<int&> p1; 231 promise<int&> p2(std::move(p1)); 232 try 233 { 234 int i = 0; 235 p1.set_exception(std::make_exception_ptr(1)); 236 VERIFY( false ); 237 } 238 catch (std::future_error& e) 239 { 240 VERIFY(e.code() == make_error_code(future_errc::no_state)); 241 } 242 } 243 test09()244void test09() 245 { 246 using namespace std; 247 248 promise<void> p1; 249 promise<void> p2(std::move(p1)); 250 try 251 { 252 p1.set_exception(std::make_exception_ptr(1)); 253 VERIFY( false ); 254 } 255 catch (std::future_error& e) 256 { 257 VERIFY(e.code() == make_error_code(future_errc::no_state)); 258 } 259 } 260 main()261int main() 262 { 263 test01(); 264 test02(); 265 test03(); 266 test04(); 267 test05(); 268 test06(); 269 test07(); 270 test08(); 271 test09(); 272 return 0; 273 } 274