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