File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 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}
You can’t perform that action at this time.
0 commit comments