1 #include "catch.hpp" 2 3 #include <osmium/thread/util.hpp> 4 5 #include <future> 6 #include <stdexcept> 7 #include <type_traits> 8 9 TEST_CASE("check_for_exception") { 10 std::promise<int> p; 11 auto f = p.get_future(); 12 13 SECTION("not ready") { 14 osmium::thread::check_for_exception(f); 15 } 16 SECTION("ready") { 17 p.set_value(3); 18 osmium::thread::check_for_exception(f); 19 } 20 SECTION("no shared state") { 21 p.set_value(3); 22 REQUIRE(f.get() == 3); 23 osmium::thread::check_for_exception(f); 24 } 25 } 26 27 TEST_CASE("check_for_exception with exception") { 28 std::promise<int> p; 29 auto f = p.get_future(); 30 31 try { 32 throw std::runtime_error{"TEST"}; 33 } catch (...) { 34 p.set_exception(std::current_exception()); 35 } 36 37 REQUIRE_THROWS_AS(osmium::thread::check_for_exception(f), std::runtime_error); 38 } 39 40 static_assert(std::is_nothrow_move_constructible<osmium::thread::thread_handler>::value, "thread_handler must have noexcept move constructor"); 41 42 TEST_CASE("empty thread_handler") { 43 osmium::thread::thread_handler th; 44 } 45 46 int foo; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) 47 test_func(int value)48void test_func(int value) { 49 foo = value; 50 } 51 52 TEST_CASE("valid thread_handler") { 53 foo = 22; 54 test_func(17); 55 REQUIRE(foo == 17); 56 { 57 osmium::thread::thread_handler th{test_func, 5}; 58 } 59 REQUIRE(foo == 5); 60 } 61 62