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 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
26int main(int, char**)
27{
28 test_allocator_statistics alloc_stats;
29 assert(alloc_stats.alloc_count == 0);
30 {
31 std::promise<int> p0(std::allocator_arg, test_allocator<int>(&alloc_stats));
32 std::promise<int> p(std::allocator_arg, test_allocator<int>(&alloc_stats));
33 assert(alloc_stats.alloc_count == 2);
34 p.swap(rhs&: p0);
35 assert(alloc_stats.alloc_count == 2);
36 std::future<int> f = p.get_future();
37 assert(alloc_stats.alloc_count == 2);
38 assert(f.valid());
39 f = p0.get_future();
40 assert(f.valid());
41 assert(alloc_stats.alloc_count == 2);
42 }
43 assert(alloc_stats.alloc_count == 0);
44 {
45 std::promise<int> p0(std::allocator_arg, test_allocator<int>(&alloc_stats));
46 std::promise<int> p(std::allocator_arg, test_allocator<int>(&alloc_stats));
47 assert(alloc_stats.alloc_count == 2);
48 swap(x&: p, y&: p0);
49 assert(alloc_stats.alloc_count == 2);
50 std::future<int> f = p.get_future();
51 assert(alloc_stats.alloc_count == 2);
52 assert(f.valid());
53 f = p0.get_future();
54 assert(f.valid());
55 assert(alloc_stats.alloc_count == 2);
56 }
57 assert(alloc_stats.alloc_count == 0);
58 {
59 std::promise<int> p0(std::allocator_arg, test_allocator<int>(&alloc_stats));
60 std::promise<int> p;
61 assert(alloc_stats.alloc_count == 1);
62 p.swap(rhs&: p0);
63 assert(alloc_stats.alloc_count == 1);
64 std::future<int> f = p.get_future();
65 assert(alloc_stats.alloc_count == 1);
66 assert(f.valid());
67 f = p0.get_future();
68 assert(f.valid());
69 assert(alloc_stats.alloc_count == 1);
70 }
71 assert(alloc_stats.alloc_count == 0);
72 {
73 std::promise<int> p0(std::allocator_arg, test_allocator<int>(&alloc_stats));
74 std::promise<int> p;
75 assert(alloc_stats.alloc_count == 1);
76 swap(x&: p, y&: p0);
77 assert(alloc_stats.alloc_count == 1);
78 std::future<int> f = p.get_future();
79 assert(alloc_stats.alloc_count == 1);
80 assert(f.valid());
81 f = p0.get_future();
82 assert(f.valid());
83 assert(alloc_stats.alloc_count == 1);
84 }
85 assert(alloc_stats.alloc_count == 0);
86
87 return 0;
88}
89

source code of libcxx/test/std/thread/futures/futures.promise/swap.pass.cpp