Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / thread / futures / futures.promise / swap.pass.cpp
blobefb32a58f58f3f89116a5a4295f8673f2679e34e
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 promise<R>
16 // void swap(promise& other);
18 // template <class R> void swap(promise<R>& x, promise<R>& y);
20 #include <future>
21 #include <cassert>
23 #include "test_macros.h"
24 #include "test_allocator.h"
26 int main(int, char**)
28 test_allocator_statistics alloc_stats;
29 assert(alloc_stats.alloc_count == 0);
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(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);
43 assert(alloc_stats.alloc_count == 0);
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(p, 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);
57 assert(alloc_stats.alloc_count == 0);
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(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);
71 assert(alloc_stats.alloc_count == 0);
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(p, 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);
85 assert(alloc_stats.alloc_count == 0);
87 return 0;