Refactors sequential misc test cases
[libcds.git] / test / stress / sequential / sequential-misc / rigtorp_spsc_driver.cpp
1 #include "common.h"
2 #include <cds/misc/RigtorpSPSCQueue.h>
3 #include <cds_test/stress_test.h>
4 #include <ctime>
5 #include <iostream>
6
7 using namespace std;
8
9 namespace {
10
11 class rigtorpSPSCQueueTest : public cds_test::stress_fixture {
12 protected:
13   static size_t s_nRigtorpSPSCQueuePassCount;
14   static size_t s_nRigtorpSPSCQueueEnqueueStride;
15   static size_t s_nRigtorpSPSCQueueCapacity;
16
17   rigtorp::SPSCQueue<size_t>* q;
18
19   static void SetUpTestCase() {
20     cds_test::config const &cfg = get_config("SequentialMisc");
21     GetConfigExpected(RigtorpSPSCQueuePassCount, 10000);
22     GetConfigExpected(RigtorpSPSCQueueEnqueueStride, 1024);
23     GetConfigExpected(RigtorpSPSCQueueCapacity, 2048);
24     if (s_nRigtorpSPSCQueueCapacity <= s_nRigtorpSPSCQueueEnqueueStride) {
25       s_nRigtorpSPSCQueueCapacity = 2 * s_nRigtorpSPSCQueueEnqueueStride;
26     }
27   }
28
29   void test() {
30     std::unique_ptr<rigtorp::SPSCQueue<size_t>> q(
31         new rigtorp::SPSCQueue<size_t>(s_nRigtorpSPSCQueueCapacity));
32     size_t nNo = 0;
33     size_t push_sum = 0;
34     size_t pop_sum = 0;
35
36     while (nNo < s_nRigtorpSPSCQueuePassCount) {
37       size_t curr_push_count = std::min(s_nRigtorpSPSCQueuePassCount - nNo,
38                                         s_nRigtorpSPSCQueueEnqueueStride);
39       for (size_t i = 0; i < curr_push_count; i++) {
40         q->push(nNo);
41         push_sum += nNo;
42         ++nNo;
43       }
44
45       size_t* res = nullptr;
46       while ((res = q->front())) {
47         pop_sum += *res;
48         q->pop();
49       }
50       EXPECT_EQ(pop_sum, push_sum);
51       push_sum = 0;
52       pop_sum = 0;
53     }
54   }
55 };
56
57 size_t rigtorpSPSCQueueTest::s_nRigtorpSPSCQueuePassCount;
58 size_t rigtorpSPSCQueueTest::s_nRigtorpSPSCQueueEnqueueStride;
59 size_t rigtorpSPSCQueueTest::s_nRigtorpSPSCQueueCapacity;
60
61 TEST_F(rigtorpSPSCQueueTest, PushPop) {
62   test();
63 }
64
65 } // namespace