Added copyright and license
[libcds.git] / tests / test-hdr / map / hdr_refinable_hashmap_hashmap_std.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-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 #include "map/hdr_striped_map.h"
32 #include <cds/container/striped_map/std_hash_map.h>
33 #include <cds/container/striped_map.h>
34 #include <cds/sync/spinlock.h>
35
36 namespace map {
37
38     namespace {
39         typedef std::unordered_map< StripedMapHdrTest::key_type, StripedMapHdrTest::value_type > map_t;
40
41         struct my_copy_policy {
42             typedef map_t::iterator iterator;
43
44             void operator()( map_t& m, iterator /*itInsert*/, iterator itWhat )
45             {
46                 m.insert( std::make_pair(itWhat->first, itWhat->second ) );
47             }
48         };
49     }
50
51     void StripedMapHdrTest::Refinable_hashmap()
52     {
53         CPPUNIT_MESSAGE( "cmp");
54         typedef cc::StripedMap< map_t
55             ,co::mutex_policy< cc::striped_set::refinable<> >
56             , co::hash< hash_int >
57             , co::compare< cmp >
58         >   map_cmp;
59         test_striped< map_cmp >();
60
61         CPPUNIT_MESSAGE( "less");
62         typedef cc::StripedMap< map_t
63             ,co::mutex_policy< cc::striped_set::refinable<> >
64             , co::hash< hash_int >
65             , co::less< less >
66         >   map_less;
67         test_striped< map_less >();
68
69         CPPUNIT_MESSAGE( "cmpmix");
70         typedef cc::StripedMap< map_t
71             ,co::mutex_policy< cc::striped_set::refinable<> >
72             , co::hash< hash_int >
73             , co::compare< cmp >
74             , co::less< less >
75         >   map_cmpmix;
76         test_striped< map_cmpmix >();
77
78         // Spinlock as lock policy
79         CPPUNIT_MESSAGE( "spinlock");
80         typedef cc::StripedMap< map_t
81             , co::mutex_policy< cc::striped_set::refinable<cds::sync::spin> >
82             , co::hash< hash_int >
83             , co::less< less >
84         >   map_spin;
85         test_striped< map_spin >();
86
87         // Resizing policy
88         CPPUNIT_MESSAGE( "load_factor_resizing<0>(1024)");
89         {
90             typedef cc::StripedMap< map_t
91                 ,co::mutex_policy< cc::striped_set::refinable<> >
92                 , co::hash< hash_int >
93                 , co::less< less >
94                 , co::resizing_policy< cc::striped_set::load_factor_resizing<0> >
95             >   map_less_resizing_lf;
96             map_less_resizing_lf m(30, cc::striped_set::load_factor_resizing<0>(1024));
97             test_striped_with(m);
98         }
99
100         CPPUNIT_MESSAGE( "load_factor_resizing<256>");
101         typedef cc::StripedMap< map_t
102             ,co::mutex_policy< cc::striped_set::refinable<> >
103             , co::hash< hash_int >
104             , co::less< less >
105             , co::resizing_policy< cc::striped_set::load_factor_resizing<256> >
106         >   map_less_resizing_lf16;
107         test_striped< map_less_resizing_lf16 >();
108
109         CPPUNIT_MESSAGE( "single_bucket_size_threshold<0>(1024)");
110         {
111             typedef cc::StripedMap< map_t
112                 ,co::mutex_policy< cc::striped_set::refinable<> >
113                 , co::hash< hash_int >
114                 , co::compare< cmp >
115                 , co::resizing_policy< cc::striped_set::single_bucket_size_threshold<0> >
116             >   map_less_resizing_sbt;
117             map_less_resizing_sbt m(30, cc::striped_set::single_bucket_size_threshold<0>(1024));
118             test_striped_with(m);
119         }
120
121         CPPUNIT_MESSAGE( "single_bucket_size_threshold<256>");
122         typedef cc::StripedMap< map_t
123             ,co::mutex_policy< cc::striped_set::refinable<> >
124             , co::hash< hash_int >
125             , co::less< less >
126             , co::resizing_policy< cc::striped_set::single_bucket_size_threshold<256> >
127         >   map_less_resizing_sbt16;
128         test_striped< map_less_resizing_sbt16 >();
129
130         // Copy policy
131         CPPUNIT_MESSAGE( "load_factor_resizing<256>, copy_item");
132         typedef cc::StripedMap< map_t
133             ,co::mutex_policy< cc::striped_set::refinable<> >
134             , co::hash< hash_int >
135             , co::less< less >
136             , co::resizing_policy< cc::striped_set::load_factor_resizing<256> >
137             , co::copy_policy< cc::striped_set::copy_item >
138         >   set_copy_item;
139         test_striped< set_copy_item >();
140
141         CPPUNIT_MESSAGE( "load_factor_resizing<256>, swap_item");
142         typedef cc::StripedMap< map_t
143             ,co::mutex_policy< cc::striped_set::refinable<> >
144             , co::hash< hash_int >
145             , co::less< less >
146             , co::resizing_policy< cc::striped_set::load_factor_resizing<256> >
147             , co::copy_policy< cc::striped_set::swap_item >
148         >   set_swap_item;
149         test_striped< set_swap_item >();
150
151         CPPUNIT_MESSAGE( "load_factor_resizing<256>, move_item");
152         typedef cc::StripedMap< map_t
153             ,co::mutex_policy< cc::striped_set::refinable<> >
154             , co::hash< hash_int >
155             , co::less< less >
156             , co::resizing_policy< cc::striped_set::load_factor_resizing<256> >
157             , co::copy_policy< cc::striped_set::move_item >
158         >   set_move_item;
159         test_striped< set_move_item >();
160
161         CPPUNIT_MESSAGE( "load_factor_resizing<256>, special copy_policy");
162         typedef cc::StripedMap< map_t
163             ,co::mutex_policy< cc::striped_set::refinable<> >
164             , co::hash< hash_int >
165             , co::less< less >
166             , co::resizing_policy< cc::striped_set::load_factor_resizing<256> >
167             , co::copy_policy< my_copy_policy >
168         >   set_special_copy_item;
169         test_striped< set_special_copy_item >();
170     }
171
172 }   // namespace map