Updated copyright
[libcds.git] / test / unit / map / test_map_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_MAP_TEST_MAP_DATA_H
32 #define CDSUNIT_MAP_TEST_MAP_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 namespace cds_test {
40
41     class map_fixture: public fixture
42     {
43     public:
44         struct key_type {
45             int nKey;
46
47             explicit key_type( int n )
48                 : nKey( n )
49             {}
50
51             explicit key_type( std::string const& str )
52                 : nKey( std::stoi( str ))
53             {}
54
55             key_type( key_type const& s )
56                 : nKey( s.nKey )
57             {}
58         };
59
60         struct value_type {
61             int nVal;
62             std::string strVal;
63
64             value_type()
65                 : nVal( 0 )
66             {}
67
68             explicit value_type( int n )
69                 : nVal( n )
70                 , strVal( std::to_string( n ))
71             {}
72
73             explicit value_type( std::string const& str )
74                 : nVal( std::stoi( str ))
75                 , strVal( str )
76             {}
77
78             explicit value_type( std::string&& str )
79                 : nVal( std::stoi( str ))
80                 , strVal( std::move( str ))
81             {}
82
83             value_type( int n, std::string const& str )
84                 : nVal( n )
85                 , strVal( str )
86             {}
87
88             value_type( int n, std::string&& str )
89                 : nVal( n )
90                 , strVal( std::move( str ))
91             {}
92
93             value_type( value_type&& v )
94                 : nVal( v.nVal )
95                 , strVal( std::move( v.strVal ))
96             {}
97
98             value_type( value_type const& v )
99                 : nVal( v.nVal )
100                 , strVal( v.strVal )
101             {}
102
103             value_type& operator=( value_type const& v )
104             {
105                 if ( this != &v ) {
106                     nVal = v.nVal;
107                     strVal = v.strVal;
108                 }
109                 return *this;
110             }
111
112             value_type& operator=( value_type&& v )
113             {
114                 if ( this != &v ) {
115                     nVal = v.nVal;
116                     strVal = std::move( v.strVal );
117                 }
118                 return *this;
119             }
120
121             value_type& operator=( int i )
122             {
123                 nVal = i;
124                 strVal = std::to_string( i );
125                 return *this;
126             }
127
128             value_type& operator=( std::string const& s )
129             {
130                 nVal = std::stoi( s );
131                 strVal = s;
132                 return *this;
133             }
134         };
135
136         typedef std::pair<key_type const, value_type> pair_type;
137
138         struct less
139         {
140             bool operator ()( key_type const& v1, key_type const& v2 ) const
141             {
142                 return v1.nKey < v2.nKey;
143             }
144
145             bool operator ()( key_type const& v1, int v2 ) const
146             {
147                 return v1.nKey < v2;
148             }
149
150             bool operator ()( int v1, key_type const& v2 ) const
151             {
152                 return v1 < v2.nKey;
153             }
154
155             bool operator ()( key_type const& v1, std::string const& v2 ) const
156             {
157                 return v1.nKey < std::stoi(v2 );
158             }
159
160             bool operator ()( std::string const& v1, key_type const& v2 ) const
161             {
162                 return std::stoi( v1 ) < v2.nKey;
163             }
164         };
165
166         struct cmp {
167             int operator ()( key_type const& v1, key_type const& v2 ) const
168             {
169                 if ( v1.nKey < v2.nKey )
170                     return -1;
171                 return v1.nKey > v2.nKey ? 1 : 0;
172             }
173
174             int operator ()( key_type const& v1, int v2 ) const
175             {
176                 if ( v1.nKey < v2 )
177                     return -1;
178                 return v1.nKey > v2 ? 1 : 0;
179             }
180
181             int operator ()( int v1, key_type const& v2 ) const
182             {
183                 if ( v1 < v2.nKey )
184                     return -1;
185                 return v1 > v2.nKey ? 1 : 0;
186             }
187
188             int operator ()( key_type const& v1, std::string const& v2 ) const
189             {
190                 int n2 = std::stoi( v2 );
191                 if ( v1.nKey < n2 )
192                     return -1;
193                 return v1.nKey > n2 ? 1 : 0;
194             }
195
196             int operator ()( std::string const& v1, key_type const& v2 ) const
197             {
198                 int n1 = std::stoi( v1 );
199                 if ( n1 < v2.nKey )
200                     return -1;
201                 return n1 > v2.nKey ? 1 : 0;
202             }
203         };
204
205         struct hash1 {
206             size_t operator()( int i ) const
207             {
208                 return cds::opt::v::hash<int>()( i );
209             }
210
211             size_t operator()( std::string const& str ) const
212             {
213                 return cds::opt::v::hash<int>()( std::stoi( str ));
214             }
215
216             template <typename T>
217             size_t operator()( T const& i ) const
218             {
219                 return cds::opt::v::hash<int>()(i.nKey);
220             }
221         };
222
223         struct other_item {
224             int nKey;
225
226             other_item( int key )
227                 : nKey( key )
228             {}
229         };
230
231         struct other_less
232         {
233             bool operator ()( key_type const& v1, other_item const& v2 ) const
234             {
235                 return v1.nKey < v2.nKey;
236             }
237             bool operator ()( other_item const& v1, key_type const& v2 ) const
238             {
239                 return v1.nKey < v2.nKey;
240             }
241         };
242     };
243 } // namespace cds_test
244
245 #endif //