Migrated map-insfind-int stress test to gtest
[libcds.git] / tests / unit / map2 / map_type.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-2016
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_TYPE_H
32 #define CDSUNIT_MAP_TYPE_H
33
34 #include <cds/urcu/general_instant.h>
35 #include <cds/urcu/general_buffered.h>
36 #include <cds/urcu/general_threaded.h>
37 #include <cds/urcu/signal_buffered.h>
38 #include <cds/urcu/signal_threaded.h>
39
40 #include <cds/sync/spinlock.h>
41 #include <cds/opt/hash.h>
42 #include <boost/functional/hash/hash.hpp>
43
44 #include "cppunit/cppunit_mini.h"
45
46 namespace map2 {
47     namespace cc = cds::container;
48     namespace co = cds::opt;
49
50     typedef cds::urcu::gc< cds::urcu::general_instant<> >   rcu_gpi;
51     typedef cds::urcu::gc< cds::urcu::general_buffered<> >  rcu_gpb;
52     typedef cds::urcu::gc< cds::urcu::general_threaded<> >  rcu_gpt;
53 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
54     typedef cds::urcu::gc< cds::urcu::signal_buffered<> >  rcu_shb;
55     typedef cds::urcu::gc< cds::urcu::signal_threaded<> >  rcu_sht;
56 #endif
57
58     template <typename Key>
59     struct cmp {
60         int operator ()(Key const& k1, Key const& k2) const
61         {
62             if ( std::less<Key>( k1, k2 ) )
63                 return -1;
64             return std::less<Key>( k2, k1 ) ? 1 : 0;
65         }
66     };
67
68 #define CDSUNIT_INT_COMPARE(t)  template <> struct cmp<t> { int operator()( t k1, t k2 ){ return (int)(k1 - k2); } }
69     CDSUNIT_INT_COMPARE(char);
70     CDSUNIT_INT_COMPARE(unsigned char);
71     CDSUNIT_INT_COMPARE(int);
72     CDSUNIT_INT_COMPARE(unsigned int);
73     CDSUNIT_INT_COMPARE(long);
74     CDSUNIT_INT_COMPARE(unsigned long);
75     CDSUNIT_INT_COMPARE(long long);
76     CDSUNIT_INT_COMPARE(unsigned long long);
77 #undef CDSUNIT_INT_COMPARE
78
79     template <>
80     struct cmp<std::string>
81     {
82         int operator()(std::string const& s1, std::string const& s2)
83         {
84             return s1.compare( s2 );
85         }
86         int operator()(std::string const& s1, char const * s2)
87         {
88             return s1.compare( s2 );
89         }
90         int operator()(char const * s1, std::string const& s2)
91         {
92             return -s2.compare( s1 );
93         }
94     };
95
96     // forward
97     template <typename ImplSelector, typename Key, typename Value>
98     struct map_type;
99
100     template <typename Key, typename Value>
101     struct map_type_base
102     {
103         typedef co::v::hash<Key>    key_hash;
104         typedef std::less<Key>      less;
105         typedef cmp<Key>            compare;
106
107         struct equal_to {
108             bool operator()( Key const& k1, Key const& k2 ) const
109             {
110                 return compare()( k1, k2 ) == 0;
111             }
112         };
113
114         struct hash: public key_hash
115         {
116             size_t operator()( Key const& k ) const
117             {
118                 return key_hash::operator()( k );
119             }
120             template <typename Q>
121             size_t operator()( Q const& k ) const
122             {
123                 return key_hash::operator()( k );
124             }
125         };
126
127         struct hash2: public key_hash
128         {
129             size_t operator()( Key const& k ) const
130             {
131                 size_t seed = ~key_hash::operator ()( k );
132                 boost::hash_combine( seed, k );
133                 return seed;
134             }
135             template <typename Q>
136             size_t operator()( Q const& k ) const
137             {
138                 size_t seed = ~key_hash::operator()( k );
139                 boost::hash_combine( seed, k );
140                 return seed;
141             }
142         };
143     };
144
145     template <typename Map>
146     static inline void print_stat( Map const& /*m*/ )
147     {}
148
149     template <typename Map>
150     static inline void check_before_cleanup( Map& /*m*/ )
151     {}
152
153     template <typename Map>
154     static inline void additional_cleanup( Map& /*m*/ )
155     {}
156
157     template <typename Map>
158     static inline void additional_check( Map& /*m*/ )
159     {}
160
161 }   // namespace map2
162
163 #endif // ifndef CDSUNIT_MAP_TYPE_H