Skip to content

Commit 0325594

Browse files
committed
tests [excluded]
1 parent d2686db commit 0325594

3 files changed

Lines changed: 89 additions & 1 deletion

File tree

tests/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44
# SPDX-License-Identifier: Apache-2.0
55
#
66

7-
message(STATUS "There are no tests yet")
7+
include_directories(
8+
${CMAKE_CURRENT_SOURCE_DIR}
9+
${PROJECT_SOURCE_DIR}/src
10+
)
11+
12+
# add_subdirectory(utils)

tests/utils/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# Copyright Quadrivium LLC
3+
# All Rights Reserved
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
# addtest(utils_test
8+
# channel.cpp
9+
# )
10+
11+
# target_link_libraries(utils_test
12+
13+
# )

tests/utils/channel.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "utils/channel.hpp"
2+
3+
#include <gtest/gtest.h>
4+
5+
#include <chrono>
6+
#include <optional>
7+
#include <thread>
8+
9+
using namespace std::chrono_literals;
10+
11+
TEST(ChannelTest, SendAndReceiveValue) {
12+
auto [recv, send] = Channel<int>::create_channel<int>();
13+
14+
send.set(42);
15+
auto value = recv.wait();
16+
17+
ASSERT_TRUE(value.has_value());
18+
EXPECT_EQ(value.value(), 42);
19+
}
20+
21+
TEST(ChannelTest, SendLValue) {
22+
auto [recv, send] = Channel<int>::create_channel<int>();
23+
24+
int x = 123;
25+
send.set(x);
26+
auto value = recv.wait();
27+
28+
ASSERT_TRUE(value.has_value());
29+
EXPECT_EQ(value.value(), 123);
30+
}
31+
32+
TEST(ChannelTest, SenderDestructionNotifiesReceiver) {
33+
std::optional<Channel<int>::Receiver> recv;
34+
std::optional<Channel<int>::Sender> send;
35+
36+
std::tie(recv, send) = Channel<int>::create_channel<int>();
37+
38+
std::optional<int> result;
39+
40+
std::thread t([&]() { result = recv->wait(); });
41+
42+
std::this_thread::sleep_for(50ms);
43+
send.reset();
44+
45+
t.join();
46+
47+
EXPECT_FALSE(result.has_value());
48+
}
49+
50+
TEST(ChannelTest, MultipleSendKeepsOneValue) {
51+
auto [recv, send] = Channel<int>::create_channel<int>();
52+
53+
send.set(1);
54+
send.set(2);
55+
56+
auto value = recv.wait();
57+
ASSERT_TRUE(value.has_value());
58+
EXPECT_TRUE(value.value() == 1 || value.value() == 2);
59+
}
60+
61+
TEST(ChannelTest, ReceiverDestructionUnregistersSender) {
62+
std::optional<Channel<int>::Receiver> recv;
63+
std::optional<Channel<int>::Sender> send;
64+
65+
std::tie(recv, send) = Channel<int>::create_channel<int>();
66+
67+
recv.reset();
68+
69+
EXPECT_NO_THROW(send->set(999));
70+
}

0 commit comments

Comments
 (0)