| 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 future<R> |
| 15 | |
| 16 | // R future::get(); |
| 17 | // R& future<R&>::get(); |
| 18 | // void future<void>::get(); |
| 19 | |
| 20 | #include <future> |
| 21 | #include <cassert> |
| 22 | |
| 23 | #include "make_test_thread.h" |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | void func1(std::promise<int> p) |
| 27 | { |
| 28 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 29 | p.set_value(3); |
| 30 | } |
| 31 | |
| 32 | void func2(std::promise<int> p) |
| 33 | { |
| 34 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 35 | p.set_exception(std::make_exception_ptr(ex: 3)); |
| 36 | } |
| 37 | |
| 38 | int j = 0; |
| 39 | |
| 40 | void func3(std::promise<int&> p) |
| 41 | { |
| 42 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 43 | j = 5; |
| 44 | p.set_value(j); |
| 45 | } |
| 46 | |
| 47 | void func4(std::promise<int&> p) |
| 48 | { |
| 49 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 50 | p.set_exception(std::make_exception_ptr(ex: 3.5)); |
| 51 | } |
| 52 | |
| 53 | void func5(std::promise<void> p) |
| 54 | { |
| 55 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 56 | p.set_value(); |
| 57 | } |
| 58 | |
| 59 | void func6(std::promise<void> p) |
| 60 | { |
| 61 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 62 | p.set_exception(std::make_exception_ptr(ex: 'c')); |
| 63 | } |
| 64 | |
| 65 | int main(int, char**) |
| 66 | { |
| 67 | { |
| 68 | typedef int T; |
| 69 | { |
| 70 | std::promise<T> p; |
| 71 | std::future<T> f = p.get_future(); |
| 72 | support::make_test_thread(func1, std::move(p)).detach(); |
| 73 | assert(f.valid()); |
| 74 | assert(f.get() == 3); |
| 75 | assert(!f.valid()); |
| 76 | } |
| 77 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 78 | { |
| 79 | std::promise<T> p; |
| 80 | std::future<T> f = p.get_future(); |
| 81 | support::make_test_thread(func2, std::move(p)).detach(); |
| 82 | try |
| 83 | { |
| 84 | assert(f.valid()); |
| 85 | assert(f.get() == 3); |
| 86 | assert(false); |
| 87 | } |
| 88 | catch (int i) |
| 89 | { |
| 90 | assert(i == 3); |
| 91 | } |
| 92 | assert(!f.valid()); |
| 93 | } |
| 94 | #endif |
| 95 | } |
| 96 | { |
| 97 | typedef int& T; |
| 98 | { |
| 99 | std::promise<T> p; |
| 100 | std::future<T> f = p.get_future(); |
| 101 | support::make_test_thread(func3, std::move(p)).detach(); |
| 102 | assert(f.valid()); |
| 103 | assert(f.get() == 5); |
| 104 | assert(!f.valid()); |
| 105 | } |
| 106 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 107 | { |
| 108 | std::promise<T> p; |
| 109 | std::future<T> f = p.get_future(); |
| 110 | support::make_test_thread(func4, std::move(p)).detach(); |
| 111 | try |
| 112 | { |
| 113 | assert(f.valid()); |
| 114 | assert(f.get() == 3); |
| 115 | assert(false); |
| 116 | } |
| 117 | catch (double i) |
| 118 | { |
| 119 | assert(i == 3.5); |
| 120 | } |
| 121 | assert(!f.valid()); |
| 122 | } |
| 123 | #endif |
| 124 | } |
| 125 | { |
| 126 | typedef void T; |
| 127 | { |
| 128 | std::promise<T> p; |
| 129 | std::future<T> f = p.get_future(); |
| 130 | support::make_test_thread(func5, std::move(p)).detach(); |
| 131 | assert(f.valid()); |
| 132 | f.get(); |
| 133 | assert(!f.valid()); |
| 134 | } |
| 135 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 136 | { |
| 137 | std::promise<T> p; |
| 138 | std::future<T> f = p.get_future(); |
| 139 | support::make_test_thread(func6, std::move(p)).detach(); |
| 140 | try |
| 141 | { |
| 142 | assert(f.valid()); |
| 143 | f.get(); |
| 144 | assert(false); |
| 145 | } |
| 146 | catch (char i) |
| 147 | { |
| 148 | assert(i == 'c'); |
| 149 | } |
| 150 | assert(!f.valid()); |
| 151 | } |
| 152 | #endif |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 |
