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 // UNSUPPORTED: c++98, c++03
     12 
     13 // <future>
     14 
     15 // class promise<R>
     16 
     17 // promise(promise&& rhs);
     18 
     19 #include <future>
     20 #include <cassert>
     21 
     22 #include "test_macros.h"
     23 #include "test_allocator.h"
     24 
     25 int main()
     26 {
     27     assert(test_alloc_base::alloc_count == 0);
     28     {
     29         std::promise<int> p0(std::allocator_arg, test_allocator<int>());
     30         std::promise<int> p(std::move(p0));
     31         assert(test_alloc_base::alloc_count == 1);
     32         std::future<int> f = p.get_future();
     33         assert(test_alloc_base::alloc_count == 1);
     34         assert(f.valid());
     35 #ifndef TEST_HAS_NO_EXCEPTIONS
     36         try
     37         {
     38             f = p0.get_future();
     39             assert(false);
     40         }
     41         catch (const std::future_error& e)
     42         {
     43             assert(e.code() == make_error_code(std::future_errc::no_state));
     44         }
     45         assert(test_alloc_base::alloc_count == 1);
     46 #endif
     47     }
     48     assert(test_alloc_base::alloc_count == 0);
     49     {
     50         std::promise<int&> p0(std::allocator_arg, test_allocator<int>());
     51         std::promise<int&> p(std::move(p0));
     52         assert(test_alloc_base::alloc_count == 1);
     53         std::future<int&> f = p.get_future();
     54         assert(test_alloc_base::alloc_count == 1);
     55         assert(f.valid());
     56 #ifndef TEST_HAS_NO_EXCEPTIONS
     57         try
     58         {
     59             f = p0.get_future();
     60             assert(false);
     61         }
     62         catch (const std::future_error& e)
     63         {
     64             assert(e.code() == make_error_code(std::future_errc::no_state));
     65         }
     66         assert(test_alloc_base::alloc_count == 1);
     67 #endif
     68     }
     69     assert(test_alloc_base::alloc_count == 0);
     70     {
     71         std::promise<void> p0(std::allocator_arg, test_allocator<void>());
     72         std::promise<void> p(std::move(p0));
     73         assert(test_alloc_base::alloc_count == 1);
     74         std::future<void> f = p.get_future();
     75         assert(test_alloc_base::alloc_count == 1);
     76         assert(f.valid());
     77 #ifndef TEST_HAS_NO_EXCEPTIONS
     78         try
     79         {
     80             f = p0.get_future();
     81             assert(false);
     82         }
     83         catch (const std::future_error& e)
     84         {
     85             assert(e.code() == make_error_code(std::future_errc::no_state));
     86         }
     87         assert(test_alloc_base::alloc_count == 1);
     88 #endif
     89     }
     90     assert(test_alloc_base::alloc_count == 0);
     91 }
     92