-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvariant.test.cpp
More file actions
85 lines (64 loc) 路 2.34 KB
/
Copy pathvariant.test.cpp
File metadata and controls
85 lines (64 loc) 路 2.34 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <boost/ut.hpp>
#include <cr/channel.hpp>
#include <thread>
#include <numbers>
using namespace boost::ut;
using namespace boost::ut::literals;
using namespace std::chrono_literals;
template <class... Ts>
struct overloaded : Ts...
{
using Ts::operator()...;
};
// NOLINTNEXTLINE
suite<"variant"> variant_suite = []
{
auto [sender, receiver] = cr::channel<int, std::string, double>();
auto _t1 = [](decltype(receiver) receiver)
{
expect(*receiver.recv_as<int>() == 10);
expect(std::get<std::string>(*receiver.recv()) == "Some message!");
receiver.recv(
[]<typename T>(const T &what)
{
expect(std::is_same_v<std::decay_t<T>, double>);
if constexpr (std::is_same_v<T, double>)
{
expect(what == std::numbers::pi);
}
});
std::this_thread::sleep_for(std::chrono::seconds(2));
expect(*receiver.try_recv_as<int>() == 10);
expect(std::get<std::string>(receiver.try_recv()->value()) == "Some message!");
bool called = false;
receiver.try_recv(overloaded{[&](double what)
{
expect(what == std::numbers::pi);
called = true;
},
[](auto &&) {}});
expect(called);
expect(*receiver.try_recv_as<int>(10s) == 10);
expect(*receiver.try_recv_as<std::string>(10s) == "Some message!");
called = false;
receiver.try_recv(overloaded{[&](double what)
{
expect(what == std::numbers::pi);
called = true;
},
[](auto &&) {}},
10s);
expect(called);
};
std::jthread t1{_t1, std::move(receiver)};
sender.send(10);
sender.send("Some message!");
sender.send(std::numbers::pi);
sender.send(10);
sender.send("Some message!");
sender.send(std::numbers::pi);
std::this_thread::sleep_for(std::chrono::seconds(2));
sender.send(10);
sender.send("Some message!");
sender.send(std::numbers::pi);
};