Home | History | Annotate | Download | only in futures.promise
      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 
     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_allocator.h"
     24 
     25 int main()
     26 {
     27     assert(test_alloc_base::count == 0);
     28     {
     29         std::promise<int> p0(std::allocator_arg, test_allocator<int>());
     30         std::promise<int> p(std::allocator_arg, test_allocator<int>());
     31         assert(test_alloc_base::count == 2);
     32         p.swap(p0);
     33         assert(test_alloc_base::count == 2);
     34         std::future<int> f = p.get_future();
     35         assert(test_alloc_base::count == 2);
     36         assert(f.valid());
     37         f = p0.get_future();
     38         assert(f.valid());
     39         assert(test_alloc_base::count == 2);
     40     }
     41     assert(test_alloc_base::count == 0);
     42     {
     43         std::promise<int> p0(std::allocator_arg, test_allocator<int>());
     44         std::promise<int> p(std::allocator_arg, test_allocator<int>());
     45         assert(test_alloc_base::count == 2);
     46         swap(p, p0);
     47         assert(test_alloc_base::count == 2);
     48         std::future<int> f = p.get_future();
     49         assert(test_alloc_base::count == 2);
     50         assert(f.valid());
     51         f = p0.get_future();
     52         assert(f.valid());
     53         assert(test_alloc_base::count == 2);
     54     }
     55     assert(test_alloc_base::count == 0);
     56     {
     57         std::promise<int> p0(std::allocator_arg, test_allocator<int>());
     58         std::promise<int> p;
     59         assert(test_alloc_base::count == 1);
     60         p.swap(p0);
     61         assert(test_alloc_base::count == 1);
     62         std::future<int> f = p.get_future();
     63         assert(test_alloc_base::count == 1);
     64         assert(f.valid());
     65         f = p0.get_future();
     66         assert(f.valid());
     67         assert(test_alloc_base::count == 1);
     68     }
     69     assert(test_alloc_base::count == 0);
     70     {
     71         std::promise<int> p0(std::allocator_arg, test_allocator<int>());
     72         std::promise<int> p;
     73         assert(test_alloc_base::count == 1);
     74         swap(p, p0);
     75         assert(test_alloc_base::count == 1);
     76         std::future<int> f = p.get_future();
     77         assert(test_alloc_base::count == 1);
     78         assert(f.valid());
     79         f = p0.get_future();
     80         assert(f.valid());
     81         assert(test_alloc_base::count == 1);
     82     }
     83     assert(test_alloc_base::count == 0);
     84 }
     85