libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

set_value_const.pass.cpp (1451B)


      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 promise::set_value(const R& r);
     18 
     19 #include <future>
     20 #include <cassert>
     21 
     22 #include "test_macros.h"
     23 
     24 struct A
     25 {
     26     A() {}
     27     A(const A&) {
     28         TEST_THROW(10);
     29     }
     30 };
     31 
     32 int main()
     33 {
     34     {
     35         typedef int T;
     36         T i = 3;
     37         std::promise<T> p;
     38         std::future<T> f = p.get_future();
     39         p.set_value(i);
     40         ++i;
     41         assert(f.get() == 3);
     42 #ifndef TEST_HAS_NO_EXCEPTIONS
     43         --i;
     44         try
     45         {
     46             p.set_value(i);
     47             assert(false);
     48         }
     49         catch (const std::future_error& e)
     50         {
     51             assert(e.code() == make_error_code(std::future_errc::promise_already_satisfied));
     52         }
     53 #endif
     54     }
     55     {
     56         typedef A T;
     57         T i;
     58         std::promise<T> p;
     59         std::future<T> f = p.get_future();
     60 #ifndef TEST_HAS_NO_EXCEPTIONS
     61         try
     62         {
     63             p.set_value(i);
     64             assert(false);
     65         }
     66         catch (int j)
     67         {
     68             assert(j == 10);
     69         }
     70 #endif
     71     }
     72 }