Skip to content

Commit 932edfb

Browse files
author
Dmitry Malakhov
committed
Move tests to catch2 and add SIGNAL_TEST_CASE for convienience
1 parent 6cd54d1 commit 932edfb

File tree

15 files changed

+325
-368
lines changed

15 files changed

+325
-368
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Checks: >
1313
-readability-make-member-function-const,
1414
-readability-avoid-return-with-void-value,
1515
-readability-named-parameter,
16+
-readability-magic-numbers,
1617
1718
1819
# Turn all the warnings from the checks above into errors.

example/SendLogsOnCrash.cpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,33 @@ sighandler(int) noexcept {
6464
co_return success();
6565
}
6666

67+
void run_tcp_server(std::string &out) {
68+
int srv_fd = ::socket(AF_INET, SOCK_STREAM, 0);
69+
assert(srv_fd >= 0);
70+
71+
sockaddr_in addr{};
72+
addr.sin_family = AF_INET;
73+
addr.sin_port = ::htons(8080);
74+
addr.sin_addr.s_addr = ::htonl(INADDR_LOOPBACK);
75+
76+
int opt = 1;
77+
setsockopt(srv_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
78+
::bind(srv_fd, (sockaddr *)&addr, sizeof(addr)); // NOLINT
79+
::listen(srv_fd, 1);
80+
81+
int client = ::accept(srv_fd, nullptr, nullptr);
82+
char buf[1024]; // NOLINT
83+
while (true) {
84+
ssize_t n = ::read(client, buf, sizeof(buf));
85+
if (n <= 0) {
86+
break;
87+
}
88+
out += std::string_view{buf, size_t(n)};
89+
}
90+
::close(client);
91+
::close(srv_fd);
92+
}
93+
6794
} // namespace
6895

6996
int main() {
@@ -74,32 +101,7 @@ int main() {
74101
}
75102

76103
std::string remote_server_data;
77-
auto remote_server_thread = std::jthread([&] {
78-
int srv_fd = ::socket(AF_INET, SOCK_STREAM, 0);
79-
assert(srv_fd >= 0);
80-
81-
sockaddr_in addr{};
82-
addr.sin_family = AF_INET;
83-
addr.sin_port = ::htons(8080);
84-
addr.sin_addr.s_addr = ::htonl(INADDR_LOOPBACK);
85-
86-
int opt = 1;
87-
setsockopt(srv_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
88-
::bind(srv_fd, (sockaddr *)&addr, sizeof(addr)); // NOLINT
89-
::listen(srv_fd, 1);
90-
91-
int client = ::accept(srv_fd, nullptr, nullptr);
92-
char buf[1024]; // NOLINT
93-
while (true) {
94-
ssize_t n = ::read(client, buf, sizeof(buf));
95-
if (n <= 0) {
96-
break;
97-
}
98-
remote_server_data += std::string_view{buf, size_t(n)};
99-
}
100-
::close(client);
101-
::close(srv_fd);
102-
});
104+
auto remote_server_thread = std::jthread(run_tcp_server, std::ref(remote_server_data));
103105

104106
logs_buffer.emplace_back("Log message 1\n");
105107
logs_buffer.emplace_back("Log message 2\n");
@@ -131,5 +133,6 @@ int main() {
131133
return 0;
132134
} catch (const std::exception &e) {
133135
std::cout << e.what() << '\n';
136+
return 1;
134137
}
135138
}

test/cases/Alloc.cpp

Lines changed: 70 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,123 +2,107 @@
22

33
#include "corosig/testing/Signals.hpp"
44

5-
#include <catch2/catch.hpp>
5+
#include <catch2/catch_all.hpp>
66
#include <cstddef>
77
#include <cstdint>
88

99
using namespace corosig;
1010

11-
TEST_CASE("Allocation and freeing within capacity") {
12-
test_in_sighandler([] {
13-
Alloc::Memory<1024> mem;
14-
Alloc alloc{mem};
11+
COROSIG_SIGHANDLER_TEST_CASE("Allocation and freeing within capacity") {
12+
Alloc::Memory<1024> mem;
13+
Alloc alloc{mem};
1514

16-
void *p1 = alloc.allocate(128);
17-
COROSIG_REQUIRE(p1 != nullptr);
15+
void *p1 = alloc.allocate(128);
16+
COROSIG_REQUIRE(p1 != nullptr);
1817

19-
void *p2 = alloc.allocate(256);
20-
COROSIG_REQUIRE(p2 != nullptr);
18+
void *p2 = alloc.allocate(256);
19+
COROSIG_REQUIRE(p2 != nullptr);
2120

22-
alloc.free(p1);
23-
alloc.free(p2);
24-
});
21+
alloc.free(p1);
22+
alloc.free(p2);
2523
}
2624

27-
TEST_CASE("Allocation exceeds capacity") {
28-
test_in_sighandler([] {
29-
Alloc::Memory<256> mem;
30-
Alloc alloc{mem};
25+
COROSIG_SIGHANDLER_TEST_CASE("Allocation exceeds capacity") {
26+
Alloc::Memory<256> mem;
27+
Alloc alloc{mem};
3128

32-
void *p = alloc.allocate(512);
33-
COROSIG_REQUIRE(p == nullptr);
34-
});
29+
void *p = alloc.allocate(512);
30+
COROSIG_REQUIRE(p == nullptr);
3531
}
3632

37-
TEST_CASE("Alignment handling") {
38-
test_in_sighandler([] {
39-
Alloc::Memory<1024> mem;
40-
Alloc alloc{mem};
33+
COROSIG_SIGHANDLER_TEST_CASE("Alignment handling") {
34+
Alloc::Memory<1024> mem;
35+
Alloc alloc{mem};
4136

42-
constexpr size_t align = alignof(std::max_align_t);
43-
void *p = alloc.allocate(64, align);
37+
constexpr size_t align = alignof(std::max_align_t);
38+
void *p = alloc.allocate(64, align);
4439

45-
COROSIG_REQUIRE(p != nullptr);
46-
COROSIG_REQUIRE(reinterpret_cast<std::uintptr_t>(p) % align == 0);
47-
alloc.free(p);
48-
});
40+
COROSIG_REQUIRE(p != nullptr);
41+
COROSIG_REQUIRE(reinterpret_cast<std::uintptr_t>(p) % align == 0);
42+
alloc.free(p);
4943
}
5044

51-
TEST_CASE("Multiple allocations until exhaustion") {
52-
test_in_sighandler([] {
53-
Alloc::Memory<128> mem;
54-
Alloc alloc{mem};
55-
56-
void *blocks[10] = {};
57-
size_t count = 0;
58-
while (void *p = alloc.allocate(16)) {
59-
blocks[count++] = p;
60-
}
61-
COROSIG_REQUIRE(count > 0);
62-
COROSIG_REQUIRE(count * 16 <= 128);
63-
64-
for (size_t i = 0; i < count; ++i) {
65-
alloc.free(blocks[i]);
66-
}
67-
});
45+
COROSIG_SIGHANDLER_TEST_CASE("Multiple allocations until exhaustion") {
46+
Alloc::Memory<128> mem;
47+
Alloc alloc{mem};
48+
49+
std::array<void *, 10> blocks = {};
50+
size_t count = 0;
51+
while (void *p = alloc.allocate(16)) {
52+
blocks[count++] = p;
53+
}
54+
COROSIG_REQUIRE(count > 0);
55+
COROSIG_REQUIRE(count * 16 <= 128);
56+
57+
for (size_t i = 0; i < count; ++i) {
58+
alloc.free(blocks[i]);
59+
}
6860
}
6961

70-
TEST_CASE("Freeing nullptr should be safe") {
71-
test_in_sighandler([] {
72-
Alloc::Memory<128> mem;
73-
Alloc alloc{mem};
62+
COROSIG_SIGHANDLER_TEST_CASE("Freeing nullptr should be safe") {
63+
Alloc::Memory<128> mem;
64+
Alloc alloc{mem};
7465

75-
alloc.free(nullptr);
76-
});
66+
alloc.free(nullptr);
7767
}
7868

79-
TEST_CASE("Zero-size allocation should return non-null or null consistently") {
80-
test_in_sighandler([] {
81-
Alloc::Memory<128> mem;
82-
Alloc alloc{mem};
69+
COROSIG_SIGHANDLER_TEST_CASE("Zero-size allocation should return non-null or null consistently") {
70+
Alloc::Memory<128> mem;
71+
Alloc alloc{mem};
8372

84-
void *p = alloc.allocate(0);
85-
// Depending on implementation: could return nullptr or a valid pointer.
86-
// Just ensure it doesn't crash.
87-
alloc.free(p);
88-
});
73+
void *p = alloc.allocate(0);
74+
// Depending on implementation: could return nullptr or a valid pointer.
75+
// Just ensure it doesn't crash.
76+
alloc.free(p);
8977
}
9078

91-
TEST_CASE("Reallocation after freeing") {
92-
test_in_sighandler([] {
93-
Alloc::Memory<128> mem;
94-
Alloc alloc{mem};
79+
COROSIG_SIGHANDLER_TEST_CASE("Reallocation after freeing") {
80+
Alloc::Memory<128> mem;
81+
Alloc alloc{mem};
9582

96-
void *p1 = alloc.allocate(64);
97-
COROSIG_REQUIRE(p1 != nullptr);
98-
alloc.free(p1);
83+
void *p1 = alloc.allocate(64);
84+
COROSIG_REQUIRE(p1 != nullptr);
85+
alloc.free(p1);
9986

100-
void *p2 = alloc.allocate(64);
101-
COROSIG_REQUIRE(p2 != nullptr);
102-
alloc.free(p2);
103-
});
87+
void *p2 = alloc.allocate(64);
88+
COROSIG_REQUIRE(p2 != nullptr);
89+
alloc.free(p2);
10490
}
10591

106-
TEST_CASE("Stress test with varied sizes and alignments") {
107-
test_in_sighandler([] {
108-
Alloc::Memory<512> mem;
109-
Alloc alloc{mem};
92+
COROSIG_SIGHANDLER_TEST_CASE("Stress test with varied sizes and alignments") {
93+
Alloc::Memory<512> mem;
94+
Alloc alloc{mem};
11095

111-
void *a = alloc.allocate(32, alignof(int));
112-
COROSIG_REQUIRE(a != nullptr);
96+
void *a = alloc.allocate(32, alignof(int));
97+
COROSIG_REQUIRE(a != nullptr);
11398

114-
void *b = alloc.allocate(64, alignof(double));
115-
COROSIG_REQUIRE(b != nullptr);
99+
void *b = alloc.allocate(64, alignof(double));
100+
COROSIG_REQUIRE(b != nullptr);
116101

117-
void *c = alloc.allocate(128, alignof(std::max_align_t));
118-
COROSIG_REQUIRE(c != nullptr);
102+
void *c = alloc.allocate(128, alignof(std::max_align_t));
103+
COROSIG_REQUIRE(c != nullptr);
119104

120-
alloc.free(a);
121-
alloc.free(b);
122-
alloc.free(c);
123-
});
105+
alloc.free(a);
106+
alloc.free(b);
107+
alloc.free(c);
124108
}

test/cases/Coro.cpp

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,52 @@
66
#include "corosig/testing/Signals.hpp"
77
#include "corosig/util/SetDefaultOnMove.hpp"
88

9-
#include <catch2/catch.hpp>
9+
#include <catch2/catch_all.hpp>
1010
#include <cmath>
1111
#include <csignal>
1212

1313
using namespace corosig;
1414

15-
TEST_CASE("Coroutine executed synchronously") {
16-
test_in_sighandler([] {
17-
auto foo = []() -> Fut<int> { co_return 20; };
18-
auto res = foo().block_on();
19-
COROSIG_REQUIRE(res);
20-
COROSIG_REQUIRE(res.value() == 20);
21-
});
15+
COROSIG_SIGHANDLER_TEST_CASE("Coroutine executed synchronously") {
16+
auto foo = []() -> Fut<int> { co_return 20; };
17+
auto res = foo().block_on();
18+
COROSIG_REQUIRE(res);
19+
COROSIG_REQUIRE(res.value() == 20);
2220
}
2321

24-
TEST_CASE("Fut move constructor transfers ownership") {
25-
test_in_sighandler([] {
26-
auto foo = []() -> Fut<> { co_return success(); };
22+
COROSIG_SIGHANDLER_TEST_CASE("Fut move constructor transfers ownership") {
23+
auto foo = []() -> Fut<> { co_return success(); };
2724

28-
Fut<> fut1 = foo();
29-
COROSIG_REQUIRE(fut1.has_value());
30-
Fut<> fut2{std::move(fut1)};
31-
COROSIG_REQUIRE(fut2.has_value());
32-
});
25+
Fut<> fut1 = foo();
26+
COROSIG_REQUIRE(fut1.has_value());
27+
Fut<> fut2{std::move(fut1)};
28+
COROSIG_REQUIRE(fut2.has_value());
3329
}
3430

35-
TEST_CASE("Fut move constructor with nontrivial move") {
36-
test_in_sighandler([] {
37-
using MoveType = SetDefaultOnMove<double, double(2 << 10)>;
31+
COROSIG_SIGHANDLER_TEST_CASE("Fut move constructor with nontrivial move") {
32+
using MoveType = SetDefaultOnMove<double, double(2 << 10)>;
3833

39-
auto foo = []() -> Fut<MoveType> { co_return success(MoveType{123.0}); };
34+
auto foo = []() -> Fut<MoveType> { co_return success(MoveType{123.0}); };
4035

41-
Fut<MoveType> fut1 = foo();
42-
COROSIG_REQUIRE(fut1.has_value());
36+
Fut<MoveType> fut1 = foo();
37+
COROSIG_REQUIRE(fut1.has_value());
4338

44-
Fut<MoveType> fut2{std::move(fut1)};
45-
COROSIG_REQUIRE(fut2.has_value());
46-
});
39+
Fut<MoveType> fut2{std::move(fut1)};
40+
COROSIG_REQUIRE(fut2.has_value());
4741
}
4842

49-
TEST_CASE("Reactor allocation error") {
43+
COROSIG_SIGHANDLER_TEST_CASE("Fut::block_on returns error when reactor fails") {
44+
Result res = Fut<>::promise_type::get_return_object_on_allocation_failure().block_on();
45+
COROSIG_REQUIRE(!res.has_value());
46+
COROSIG_REQUIRE(res.has_error());
47+
COROSIG_REQUIRE(res.assume_error().holds<AllocationError>());
5048
}
5149

52-
TEST_CASE("Fut::block_on returns error when reactor fails") {
53-
test_in_sighandler([] {
54-
Result res = Fut<>::promise_type::get_return_object_on_allocation_failure().block_on();
55-
COROSIG_REQUIRE(!res.has_value());
56-
COROSIG_REQUIRE(res.has_error());
57-
COROSIG_REQUIRE(res.assume_error().holds<AllocationError>());
58-
});
59-
}
60-
61-
TEST_CASE("Fut can be co_awaited inside a coroutine") {
62-
test_in_sighandler([] {
63-
constexpr static auto foo = []() -> Fut<int> { co_return 99; };
64-
constexpr static auto bar = []() -> Fut<int> { co_return 123 + (co_await foo()).value(); };
50+
COROSIG_SIGHANDLER_TEST_CASE("Fut can be co_awaited inside a coroutine") {
51+
constexpr static auto foo = []() -> Fut<int> { co_return 99; };
52+
constexpr static auto bar = []() -> Fut<int> { co_return 123 + (co_await foo()).value(); };
6553

66-
auto result = bar().block_on();
67-
REQUIRE(result.has_value());
68-
REQUIRE(result.assume_value() == 123 + 99);
69-
});
54+
auto result = bar().block_on();
55+
REQUIRE(result.has_value());
56+
REQUIRE(result.assume_value() == 123 + 99);
7057
}

test/cases/ErrorTypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "corosig/ErrorTypes.hpp"
22

3-
#include "catch2/catch.hpp"
3+
#include <catch2/catch_all.hpp>
44

55
using namespace corosig;
66

0 commit comments

Comments
 (0)