xref: /minix3/external/bsd/libc++/dist/libcxx/test/thread/futures/futures.promise/swap.pass.cpp (revision 4684ddb6aab0b36791c8099bc705d6140b3d05d0)
1*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
2*4684ddb6SLionel Sambuc //
3*4684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
4*4684ddb6SLionel Sambuc //
5*4684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*4684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*4684ddb6SLionel Sambuc //
8*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
9*4684ddb6SLionel Sambuc 
10*4684ddb6SLionel Sambuc // <future>
11*4684ddb6SLionel Sambuc 
12*4684ddb6SLionel Sambuc // class promise<R>
13*4684ddb6SLionel Sambuc 
14*4684ddb6SLionel Sambuc // void swap(promise& other);
15*4684ddb6SLionel Sambuc 
16*4684ddb6SLionel Sambuc // template <class R> void swap(promise<R>& x, promise<R>& y);
17*4684ddb6SLionel Sambuc 
18*4684ddb6SLionel Sambuc #include <future>
19*4684ddb6SLionel Sambuc #include <cassert>
20*4684ddb6SLionel Sambuc 
21*4684ddb6SLionel Sambuc #include "../test_allocator.h"
22*4684ddb6SLionel Sambuc 
main()23*4684ddb6SLionel Sambuc int main()
24*4684ddb6SLionel Sambuc {
25*4684ddb6SLionel Sambuc     assert(test_alloc_base::count == 0);
26*4684ddb6SLionel Sambuc     {
27*4684ddb6SLionel Sambuc         std::promise<int> p0(std::allocator_arg, test_allocator<int>());
28*4684ddb6SLionel Sambuc         std::promise<int> p(std::allocator_arg, test_allocator<int>());
29*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 2);
30*4684ddb6SLionel Sambuc         p.swap(p0);
31*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 2);
32*4684ddb6SLionel Sambuc         std::future<int> f = p.get_future();
33*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 2);
34*4684ddb6SLionel Sambuc         assert(f.valid());
35*4684ddb6SLionel Sambuc         f = p0.get_future();
36*4684ddb6SLionel Sambuc         assert(f.valid());
37*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 2);
38*4684ddb6SLionel Sambuc     }
39*4684ddb6SLionel Sambuc     assert(test_alloc_base::count == 0);
40*4684ddb6SLionel Sambuc     {
41*4684ddb6SLionel Sambuc         std::promise<int> p0(std::allocator_arg, test_allocator<int>());
42*4684ddb6SLionel Sambuc         std::promise<int> p(std::allocator_arg, test_allocator<int>());
43*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 2);
44*4684ddb6SLionel Sambuc         swap(p, p0);
45*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 2);
46*4684ddb6SLionel Sambuc         std::future<int> f = p.get_future();
47*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 2);
48*4684ddb6SLionel Sambuc         assert(f.valid());
49*4684ddb6SLionel Sambuc         f = p0.get_future();
50*4684ddb6SLionel Sambuc         assert(f.valid());
51*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 2);
52*4684ddb6SLionel Sambuc     }
53*4684ddb6SLionel Sambuc     assert(test_alloc_base::count == 0);
54*4684ddb6SLionel Sambuc     {
55*4684ddb6SLionel Sambuc         std::promise<int> p0(std::allocator_arg, test_allocator<int>());
56*4684ddb6SLionel Sambuc         std::promise<int> p;
57*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 1);
58*4684ddb6SLionel Sambuc         p.swap(p0);
59*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 1);
60*4684ddb6SLionel Sambuc         std::future<int> f = p.get_future();
61*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 1);
62*4684ddb6SLionel Sambuc         assert(f.valid());
63*4684ddb6SLionel Sambuc         f = p0.get_future();
64*4684ddb6SLionel Sambuc         assert(f.valid());
65*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 1);
66*4684ddb6SLionel Sambuc     }
67*4684ddb6SLionel Sambuc     assert(test_alloc_base::count == 0);
68*4684ddb6SLionel Sambuc     {
69*4684ddb6SLionel Sambuc         std::promise<int> p0(std::allocator_arg, test_allocator<int>());
70*4684ddb6SLionel Sambuc         std::promise<int> p;
71*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 1);
72*4684ddb6SLionel Sambuc         swap(p, p0);
73*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 1);
74*4684ddb6SLionel Sambuc         std::future<int> f = p.get_future();
75*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 1);
76*4684ddb6SLionel Sambuc         assert(f.valid());
77*4684ddb6SLionel Sambuc         f = p0.get_future();
78*4684ddb6SLionel Sambuc         assert(f.valid());
79*4684ddb6SLionel Sambuc         assert(test_alloc_base::count == 1);
80*4684ddb6SLionel Sambuc     }
81*4684ddb6SLionel Sambuc     assert(test_alloc_base::count == 0);
82*4684ddb6SLionel Sambuc }
83