File: test_util.cpp

package info (click to toggle)
libosmium 2.22.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,544 kB
  • sloc: cpp: 52,804; sh: 148; makefile: 19
file content (66 lines) | stat: -rw-r--r-- 1,461 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "catch.hpp"

#include <osmium/thread/util.hpp>

#include <exception>
#include <future>
#include <stdexcept>
#include <type_traits>

TEST_CASE("check_for_exception") {
    std::promise<int> p;
    auto f = p.get_future();

    SECTION("not ready") {
        osmium::thread::check_for_exception(f);
    }
    SECTION("ready") {
        p.set_value(3);
        osmium::thread::check_for_exception(f);
    }
    SECTION("no shared state") {
        p.set_value(3);
        REQUIRE(f.get() == 3);
        osmium::thread::check_for_exception(f);
    }
}

TEST_CASE("check_for_exception with exception") {
    std::promise<int> p;
    auto f = p.get_future();

    try {
        throw std::runtime_error{"TEST"};
    } catch (...) {
        p.set_exception(std::current_exception());
    }

    REQUIRE_THROWS_AS(osmium::thread::check_for_exception(f), std::runtime_error);
}

static_assert(std::is_nothrow_move_constructible<osmium::thread::thread_handler>::value, "thread_handler must have noexcept move constructor");

TEST_CASE("empty thread_handler") {
    const osmium::thread::thread_handler th;
}

namespace {

int foo; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

void test_func(int value) {
    foo = value;
}

} // anonymous namespace

TEST_CASE("valid thread_handler") {
    foo = 22;
    test_func(17);
    REQUIRE(foo == 17);
    {
        const osmium::thread::thread_handler th{test_func, 5};
    }
    REQUIRE(foo == 5);
}