Skip to content

Commit e193b75

Browse files
committed
tests: Check CircularBuffer threading
1 parent 2a69fb0 commit e193b75

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

test/TestCircularBuffer.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <catch2/catch_test_macros.hpp>
2+
#include <thread>
23

34
#include "bridge/CircularBuffer.hpp"
45

@@ -43,4 +44,42 @@ TEST_CASE("Peek/Skip", "[CircularBuffer]") {
4344
REQUIRE(buffer.Peek(data, 1) == 1); // [34]
4445
REQUIRE(buffer.BytesAvailable() == 2);
4546
REQUIRE(std::string(data, 1) == "3");
47+
}
48+
49+
void consumer(int n, CircularBuffer& buf, int& sum1) {
50+
char k;
51+
int i = 0;
52+
while (i != n) {
53+
if (!buf.Pop(&k, 1)) continue;
54+
sum1 += k;
55+
i++;
56+
}
57+
}
58+
59+
void threading(int size) {
60+
CircularBuffer buf(size);
61+
const int n = 1000000;
62+
63+
int sum0 = 0, sum1 = 0;
64+
char v = 1;
65+
std::thread t { [&]() { consumer(n, buf, sum1); } };
66+
int i = 0;
67+
while (i != n) {
68+
if (!buf.Push(&v, 1)) continue;
69+
sum0 += v;
70+
v = 3 + 2 * v;
71+
i++;
72+
}
73+
t.join();
74+
REQUIRE(sum0 == sum1);
75+
}
76+
77+
TEST_CASE("Threading8192", "[CircularBuffer]") {
78+
threading(8192);
79+
}
80+
TEST_CASE("Threading4", "[CircularBuffer]") {
81+
threading(4);
82+
}
83+
TEST_CASE("Threading1", "[CircularBuffer]") {
84+
threading(1);
4685
}

0 commit comments

Comments
 (0)