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();
     18 
     19 #include <future>
     20 #include <cassert>
     21 
     22 #include "test_macros.h"
     23 
     24 int main()
     25 {
     26     {
     27         typedef int T;
     28         std::future<T> f;
     29         {
     30             std::promise<T> p;
     31             f = p.get_future();
     32             p.set_value(3);
     33         }
     34         assert(f.get() == 3);
     35     }
     36 #ifndef TEST_HAS_NO_EXCEPTIONS
     37     {
     38         typedef int T;
     39         std::future<T> f;
     40         {
     41             std::promise<T> p;
     42             f = p.get_future();
     43         }
     44         try
     45         {
     46             T i = f.get();
     47             ((void)i); // Prevent unused warning
     48             assert(false);
     49         }
     50         catch (const std::future_error& e)
     51         {
     52             assert(e.code() == make_error_code(std::future_errc::broken_promise));
     53         }
     54     }
     55 #endif
     56 
     57     {
     58         typedef int& T;
     59         int i = 4;
     60         std::future<T> f;
     61         {
     62             std::promise<T> p;
     63             f = p.get_future();
     64             p.set_value(i);
     65         }
     66         assert(&f.get() == &i);
     67     }
     68 #ifndef TEST_HAS_NO_EXCEPTIONS
     69     {
     70         typedef int& T;
     71         std::future<T> f;
     72         {
     73             std::promise<T> p;
     74             f = p.get_future();
     75         }
     76         try
     77         {
     78             T i = f.get();
     79             ((void)i); // Prevent unused warning
     80             assert(false);
     81         }
     82         catch (const std::future_error& e)
     83         {
     84             assert(e.code() == make_error_code(std::future_errc::broken_promise));
     85         }
     86     }
     87 #endif
     88 
     89     {
     90         typedef void T;
     91         std::future<T> f;
     92         {
     93             std::promise<T> p;
     94             f = p.get_future();
     95             p.set_value();
     96         }
     97         f.get();
     98         assert(true);
     99     }
    100 #ifndef TEST_HAS_NO_EXCEPTIONS
    101     {
    102         typedef void T;
    103         std::future<T> f;
    104         {
    105             std::promise<T> p;
    106             f = p.get_future();
    107         }
    108         try
    109         {
    110             f.get();
    111             assert(false);
    112         }
    113         catch (const std::future_error& e)
    114         {
    115             // LWG 2056 changed the values of future_errc, so if we're using new
    116             // headers with an old library the error codes won't line up.
    117             //
    118             // Note that this particular check only applies to promise<void>
    119             // since the other specializations happen to be implemented in the
    120             // header rather than the library.
    121             assert(
    122                 e.code() == make_error_code(std::future_errc::broken_promise) ||
    123                 e.code() == std::error_code(0, std::future_category()));
    124         }
    125     }
    126 #endif
    127 }
    128