Modifies more sequential test cases for sets
[libcds.git] / test / stress / sequential / sequential_stack.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice,
13    this
14     list of conditions and the following disclaimer.
15
16     * Redistributions in binary form must reproduce the above copyright notice,
17     this list of conditions and the following disclaimer in the documentation
18     and/or other materials provided with the distribution.
19
20     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    ARE
24     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29    LIABILITY,
30     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31    USE
32     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "../misc/common.h"
36 #include "../stack/stack_type.h"
37
38 namespace {
39
40 #define CDSSTRESS_SequentialTreiberStack_F(test_fixture, type_name)            \
41   TEST_F(test_fixture, type_name) {                                            \
42     typedef stack::Types<value_type>::type_name stack_type;                    \
43     stack_type stack;                                                          \
44     test(stack);                                                               \
45   }
46
47 #define CDSSTRESS_SequentialEliminationStack_F(test_fixture, type_name)        \
48   TEST_F(test_fixture, type_name) {                                            \
49     typedef stack::Types<value_type>::type_name stack_type;                    \
50     stack_type stack(s_nSequentialEliminationSize);                            \
51     test(stack);                                                               \
52   }
53
54 #define CDSSTRESS_SequentialTreiberStack(test_fixture)                         \
55   CDSSTRESS_SequentialTreiberStack_F(test_fixture, Treiber_HP);                \
56   CDSSTRESS_SequentialTreiberStack_F(test_fixture, Treiber_DHP);               \
57
58 #define CDSSTRESS_SequentialEliminationStack(test_fixture)                     \
59   CDSSTRESS_SequentialEliminationStack_F(test_fixture, Elimination_HP);        \
60   CDSSTRESS_SequentialEliminationStack_F(test_fixture, Elimination_HP_dyn);    \
61   CDSSTRESS_SequentialEliminationStack_F(test_fixture, Elimination_DHP);       \
62   CDSSTRESS_SequentialEliminationStack_F(test_fixture, Elimination_DHP_dyn)
63
64 static size_t s_nSequentialStackPushCount = 100000;
65 static size_t s_nSequentialEliminationSize = 4;
66
67 class sequential_stack : public cds_test::stress_fixture {
68 protected:
69   struct value_type {
70     size_t nNo;
71     size_t nThread;
72
73     value_type() : nNo(0), nThread(0) {}
74     value_type(size_t n) : nNo(n), nThread(0) {}
75   };
76
77 protected:
78   static void SetUpTestCase() {
79     cds_test::config const &cfg = get_config("SequentialStack");
80     GetConfig(SequentialStackPushCount);
81     GetConfig(SequentialEliminationSize);
82   }
83
84   template <typename Stack> void test(Stack &stack) {
85     size_t push_error_cnt = 0;
86     size_t pop_sum = 0;
87     value_type v;
88     size_t m_nStartItem = 0;
89     size_t m_nEndItem = s_nSequentialStackPushCount;
90     for (v.nNo = m_nStartItem; v.nNo < m_nEndItem; ++v.nNo) {
91       if (!stack.push(v))
92         ++push_error_cnt;
93     }
94
95     while (stack.pop(v)) {
96       pop_sum += v.nNo;
97     }
98
99     if (push_error_cnt) {
100       std::cout << "Sequential stack push error count: " << push_error_cnt
101                 << "\n";
102     }
103     size_t supposed_sum =
104         (m_nStartItem + m_nEndItem - 1) * (m_nEndItem - m_nStartItem) / 2;
105     if (pop_sum != supposed_sum) {
106       std::cout << "Sequential stack pop sum: " << pop_sum
107                 << " != " << supposed_sum << "\n";
108     }
109   }
110 };
111
112 CDSSTRESS_SequentialTreiberStack(sequential_stack)
113     CDSSTRESS_SequentialEliminationStack(sequential_stack)
114
115 } // namespace