1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // UNSUPPORTED: libcpp-has-no-threads 11 // UNSUPPORTED: c++98, c++03 12 13 // <future> 14 15 // class promise<R> 16 17 // void swap(promise& other); 18 19 // template <class R> void swap(promise<R>& x, promise<R>& y); 20 21 #include <future> 22 #include <cassert> 23 24 #include "test_allocator.h" 25 26 int main() 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