Adds RigtorpSPSC test case
[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   static void SetUpTestCase() {
18     cds_test::config const &cfg = get_config("SequentialMisc");
19     GetConfigExpected(RigtorpSPSCQueuePassCount, 10000);
20     GetConfigExpected(RigtorpSPSCQueueEnqueueStride, 1024);
21     GetConfigExpected(RigtorpSPSCQueueCapacity, 2048);
22     if (s_nRigtorpSPSCQueueCapacity <= s_nRigtorpSPSCQueueEnqueueStride) {
23       s_nRigtorpSPSCQueueCapacity = 2 * s_nRigtorpSPSCQueueEnqueueStride;
24     }
25   }
26
27   void test() {
28     rigtorp::SPSCQueue<size_t> q(s_nRigtorpSPSCQueueCapacity);
29     size_t nNo = 0;
30     size_t push_sum = 0;
31     size_t pop_sum = 0;
32
33     while (nNo < s_nRigtorpSPSCQueuePassCount) {
34       size_t curr_push_count = std::min(s_nRigtorpSPSCQueuePassCount - nNo,
35                                         s_nRigtorpSPSCQueueEnqueueStride);
36       for (size_t i = 0; i < curr_push_count; i++) {
37         q.push(nNo);
38         push_sum += nNo;
39         ++nNo;
40       }
41
42       size_t* res = nullptr;
43       while ((res = q.front())) {
44         pop_sum += *res;
45         q.pop();
46       }
47       EXPECT_EQ(pop_sum, push_sum);
48       push_sum = 0;
49       pop_sum = 0;
50     }
51   }
52 };
53
54 size_t rigtorpSPSCQueueTest::s_nRigtorpSPSCQueuePassCount;
55 size_t rigtorpSPSCQueueTest::s_nRigtorpSPSCQueueEnqueueStride;
56 size_t rigtorpSPSCQueueTest::s_nRigtorpSPSCQueueCapacity;
57
58 TEST_F(rigtorpSPSCQueueTest, PushPop) {
59   test();
60 }
61
62 } // namespace