|
2 | 2 |
|
3 | 3 | #include "corosig/testing/Signals.hpp" |
4 | 4 |
|
5 | | -#include <catch2/catch.hpp> |
| 5 | +#include <catch2/catch_all.hpp> |
6 | 6 | #include <cstddef> |
7 | 7 | #include <cstdint> |
8 | 8 |
|
9 | 9 | using namespace corosig; |
10 | 10 |
|
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}; |
15 | 14 |
|
16 | | - void *p1 = alloc.allocate(128); |
17 | | - COROSIG_REQUIRE(p1 != nullptr); |
| 15 | + void *p1 = alloc.allocate(128); |
| 16 | + COROSIG_REQUIRE(p1 != nullptr); |
18 | 17 |
|
19 | | - void *p2 = alloc.allocate(256); |
20 | | - COROSIG_REQUIRE(p2 != nullptr); |
| 18 | + void *p2 = alloc.allocate(256); |
| 19 | + COROSIG_REQUIRE(p2 != nullptr); |
21 | 20 |
|
22 | | - alloc.free(p1); |
23 | | - alloc.free(p2); |
24 | | - }); |
| 21 | + alloc.free(p1); |
| 22 | + alloc.free(p2); |
25 | 23 | } |
26 | 24 |
|
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}; |
31 | 28 |
|
32 | | - void *p = alloc.allocate(512); |
33 | | - COROSIG_REQUIRE(p == nullptr); |
34 | | - }); |
| 29 | + void *p = alloc.allocate(512); |
| 30 | + COROSIG_REQUIRE(p == nullptr); |
35 | 31 | } |
36 | 32 |
|
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}; |
41 | 36 |
|
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); |
44 | 39 |
|
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); |
49 | 43 | } |
50 | 44 |
|
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 | + } |
68 | 60 | } |
69 | 61 |
|
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}; |
74 | 65 |
|
75 | | - alloc.free(nullptr); |
76 | | - }); |
| 66 | + alloc.free(nullptr); |
77 | 67 | } |
78 | 68 |
|
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}; |
83 | 72 |
|
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); |
89 | 77 | } |
90 | 78 |
|
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}; |
95 | 82 |
|
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); |
99 | 86 |
|
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); |
104 | 90 | } |
105 | 91 |
|
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}; |
110 | 95 |
|
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); |
113 | 98 |
|
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); |
116 | 101 |
|
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); |
119 | 104 |
|
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); |
124 | 108 | } |
0 commit comments