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: libcpp-has-no-threads
10 // UNSUPPORTED: c++03
11
12 // <future>
13
14 // class promise<R>
15
16 // void swap(promise& other);
17
18 // template <class R> void swap(promise<R>& x, promise<R>& y);
19
20 #include <future>
21 #include <cassert>
22
23 #include "test_macros.h"
24 #include "test_allocator.h"
25
main(int,char **)26 int main(int, char**)
27 {
28 assert(test_alloc_base::alloc_count == 0);
29 {
30 std::promise<int> p0(std::allocator_arg, test_allocator<int>());
31 std::promise<int> p(std::allocator_arg, test_allocator<int>());
32 assert(test_alloc_base::alloc_count == 2);
33 p.swap(p0);
34 assert(test_alloc_base::alloc_count == 2);
35 std::future<int> f = p.get_future();
36 assert(test_alloc_base::alloc_count == 2);
37 assert(f.valid());
38 f = p0.get_future();
39 assert(f.valid());
40 assert(test_alloc_base::alloc_count == 2);
41 }
42 assert(test_alloc_base::alloc_count == 0);
43 {
44 std::promise<int> p0(std::allocator_arg, test_allocator<int>());
45 std::promise<int> p(std::allocator_arg, test_allocator<int>());
46 assert(test_alloc_base::alloc_count == 2);
47 swap(p, p0);
48 assert(test_alloc_base::alloc_count == 2);
49 std::future<int> f = p.get_future();
50 assert(test_alloc_base::alloc_count == 2);
51 assert(f.valid());
52 f = p0.get_future();
53 assert(f.valid());
54 assert(test_alloc_base::alloc_count == 2);
55 }
56 assert(test_alloc_base::alloc_count == 0);
57 {
58 std::promise<int> p0(std::allocator_arg, test_allocator<int>());
59 std::promise<int> p;
60 assert(test_alloc_base::alloc_count == 1);
61 p.swap(p0);
62 assert(test_alloc_base::alloc_count == 1);
63 std::future<int> f = p.get_future();
64 assert(test_alloc_base::alloc_count == 1);
65 assert(f.valid());
66 f = p0.get_future();
67 assert(f.valid());
68 assert(test_alloc_base::alloc_count == 1);
69 }
70 assert(test_alloc_base::alloc_count == 0);
71 {
72 std::promise<int> p0(std::allocator_arg, test_allocator<int>());
73 std::promise<int> p;
74 assert(test_alloc_base::alloc_count == 1);
75 swap(p, p0);
76 assert(test_alloc_base::alloc_count == 1);
77 std::future<int> f = p.get_future();
78 assert(test_alloc_base::alloc_count == 1);
79 assert(f.valid());
80 f = p0.get_future();
81 assert(f.valid());
82 assert(test_alloc_base::alloc_count == 1);
83 }
84 assert(test_alloc_base::alloc_count == 0);
85
86 return 0;
87 }
88