Merged branch 'master' of https://github.com/Nemo1369/libcds
[libcds.git] / test / unit / set / test_set_data.h
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, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef CDSUNIT_SET_TEST_SET_DATA_H
32 #define CDSUNIT_SET_TEST_SET_DATA_H
33
34 #include <cds_test/check_size.h>
35 #include <cds_test/fixture.h>
36
37 #include <cds/opt/hash.h>
38
39 // forward declaration
40 namespace cds { namespace container {}}
41
42 namespace cds_test {
43     namespace co = cds::opt;
44
45     class container_set_data : public fixture
46     {
47     public:
48         static size_t const kSize = 1000;
49
50         struct stat
51         {
52             unsigned int nFindCount;
53             unsigned int nUpdateNewCount;
54             unsigned int nUpdateCount;
55             mutable unsigned int nEraseCount;
56
57             stat()
58             {
59                 clear_stat();
60             }
61
62             void clear_stat()
63             {
64                 memset( this, 0, sizeof( *this ));
65             }
66         };
67
68         struct other_item {
69             int nKey;
70
71             explicit other_item( int k )
72                 : nKey( k )
73             {}
74
75             int key() const
76             {
77                 return nKey;
78             }
79         };
80
81         struct int_item: public stat
82         {
83             int nKey;
84             int nVal;
85             std::string strVal;
86
87             int_item()
88                 : nKey( 0 )
89                 , nVal( 0 )
90             {}
91
92             explicit int_item( int k )
93                 : nKey( k )
94                 , nVal( k * 2 )
95             {}
96
97             template <typename Q>
98             explicit int_item( Q const& src )
99                 : nKey( src.key())
100                 , nVal( 0 )
101             {}
102
103             int_item( int_item const& src )
104                 : nKey( src.nKey )
105                 , nVal( src.nVal )
106                 , strVal( src.strVal )
107             {}
108
109             int_item( int_item&& src )
110                 : nKey( src.nKey )
111                 , nVal( src.nVal )
112                 , strVal( std::move( src.strVal ))
113             {}
114
115             int_item( int k, std::string&& s )
116                 : nKey( k )
117                 , nVal( k * 2 )
118                 , strVal( std::move( s ))
119             {}
120
121             explicit int_item( other_item const& s )
122                 : nKey( s.key())
123                 , nVal( s.key() * 2 )
124             {}
125
126             int key() const
127             {
128                 return nKey;
129             }
130         };
131
132         struct hash_int {
133             size_t operator()( int i ) const
134             {
135                 return co::v::hash<int>()(i);
136             }
137             template <typename Item>
138             size_t operator()( const Item& i ) const
139             {
140                 return (*this)(i.key());
141             }
142         };
143
144         struct simple_item_counter {
145             size_t  m_nCount;
146
147             simple_item_counter()
148                 : m_nCount( 0 )
149             {}
150
151             size_t operator ++()
152             {
153                 return ++m_nCount;
154             }
155
156             size_t operator --()
157             {
158                 return --m_nCount;
159             }
160
161             void reset()
162             {
163                 m_nCount = 0;
164             }
165
166             operator size_t() const
167             {
168                 return m_nCount;
169             }
170
171         };
172
173         struct less
174         {
175             bool operator ()( int_item const& v1, int_item const& v2 ) const
176             {
177                 return v1.key() < v2.key();
178             }
179
180             template <typename Q>
181             bool operator ()( int_item const& v1, const Q& v2 ) const
182             {
183                 return v1.key() < v2;
184             }
185
186             template <typename Q>
187             bool operator ()( const Q& v1, int_item const& v2 ) const
188             {
189                 return v1 < v2.key();
190             }
191         };
192
193         struct cmp {
194             int operator ()( int_item const& v1, int_item const& v2 ) const
195             {
196                 if ( v1.key() < v2.key())
197                     return -1;
198                 return v1.key() > v2.key() ? 1 : 0;
199             }
200
201             template <typename T>
202             int operator ()( T const& v1, int v2 ) const
203             {
204                 if ( v1.key() < v2 )
205                     return -1;
206                 return v1.key() > v2 ? 1 : 0;
207             }
208
209             template <typename T>
210             int operator ()( int v1, T const& v2 ) const
211             {
212                 if ( v1 < v2.key())
213                     return -1;
214                 return v1 > v2.key() ? 1 : 0;
215             }
216         };
217
218         struct other_less {
219             template <typename Q, typename T>
220             bool operator()( Q const& lhs, T const& rhs ) const
221             {
222                 return lhs.key() < rhs.key();
223             }
224         };
225     };
226
227 } // namespace cds_test
228
229 #endif // CDSUNIT_SET_TEST_SET_DATA_H