Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / thread / futures / futures.shared_future / get.pass.cpp
blob62da170a90c422c4f7b1fd5492a1cce90fe8ebf5
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
12 // <future>
14 // class shared_future<R>
16 // const R& shared_future::get();
17 // R& shared_future<R&>::get();
18 // void shared_future<void>::get();
20 #include <future>
21 #include <cassert>
23 #include "make_test_thread.h"
24 #include "test_macros.h"
26 void func1(std::promise<int> p)
28 std::this_thread::sleep_for(std::chrono::milliseconds(500));
29 p.set_value(3);
32 void func2(std::promise<int> p)
34 std::this_thread::sleep_for(std::chrono::milliseconds(500));
35 p.set_exception(std::make_exception_ptr(3));
38 int j = 0;
40 void func3(std::promise<int&> p)
42 std::this_thread::sleep_for(std::chrono::milliseconds(500));
43 j = 5;
44 p.set_value(j);
47 void func4(std::promise<int&> p)
49 std::this_thread::sleep_for(std::chrono::milliseconds(500));
50 p.set_exception(std::make_exception_ptr(3.5));
53 void func5(std::promise<void> p)
55 std::this_thread::sleep_for(std::chrono::milliseconds(500));
56 p.set_value();
59 void func6(std::promise<void> p)
61 std::this_thread::sleep_for(std::chrono::milliseconds(500));
62 p.set_exception(std::make_exception_ptr('c'));
65 int main(int, char**)
68 typedef int T;
70 std::promise<T> p;
71 std::shared_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());
77 #ifndef TEST_HAS_NO_EXCEPTIONS
79 std::promise<T> p;
80 std::shared_future<T> f = p.get_future();
81 support::make_test_thread(func2, std::move(p)).detach();
82 try
84 assert(f.valid());
85 assert(f.get() == 3);
86 assert(false);
88 catch (int i)
90 assert(i == 3);
92 assert(f.valid());
94 #endif
97 typedef int& T;
99 std::promise<T> p;
100 std::shared_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());
106 #ifndef TEST_HAS_NO_EXCEPTIONS
108 std::promise<T> p;
109 std::shared_future<T> f = p.get_future();
110 support::make_test_thread(func4, std::move(p)).detach();
113 assert(f.valid());
114 assert(f.get() == 3);
115 assert(false);
117 catch (double i)
119 assert(i == 3.5);
121 assert(f.valid());
123 #endif
126 typedef void T;
128 std::promise<T> p;
129 std::shared_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());
135 #ifndef TEST_HAS_NO_EXCEPTIONS
137 std::promise<T> p;
138 std::shared_future<T> f = p.get_future();
139 support::make_test_thread(func6, std::move(p)).detach();
142 assert(f.valid());
143 f.get();
144 assert(false);
146 catch (char i)
148 assert(i == 'c');
150 assert(f.valid());
152 #endif
155 return 0;