Skip to content

Commit 3f970b2

Browse files
committed
tests [excluded]
1 parent d2686db commit 3f970b2

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

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 <gtest/gtest.h>
2+
#include <optional>
3+
#include <thread>
4+
#include <chrono>
5+
#include "utils/channel.hpp"
6+
7+
using namespace std::chrono_literals;
8+
9+
TEST(ChannelTest, SendAndReceiveValue) {
10+
auto [recv, send] = Channel<int>::create_channel<int>();
11+
12+
send.set(42);
13+
auto value = recv.wait();
14+
15+
ASSERT_TRUE(value.has_value());
16+
EXPECT_EQ(value.value(), 42);
17+
}
18+
19+
TEST(ChannelTest, SendLValue) {
20+
auto [recv, send] = Channel<int>::create_channel<int>();
21+
22+
int x = 123;
23+
send.set(x);
24+
auto value = recv.wait();
25+
26+
ASSERT_TRUE(value.has_value());
27+
EXPECT_EQ(value.value(), 123);
28+
}
29+
30+
TEST(ChannelTest, SenderDestructionNotifiesReceiver) {
31+
std::optional<Channel<int>::Receiver> recv;
32+
std::optional<Channel<int>::Sender> send;
33+
34+
std::tie(recv, send) = Channel<int>::create_channel<int>();
35+
36+
std::optional<int> result;
37+
38+
std::thread t([&]() {
39+
result = recv->wait();
40+
});
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)