Added copyright and license
[libcds.git] / tests / test-hdr / set / hdr_refinable_hashset_hashset_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 "set/hdr_striped_set.h"
32 #include <cds/container/striped_set/std_hash_set.h>
33 #include <cds/container/striped_set.h>
34 #include <cds/sync/spinlock.h>
35
36 namespace set {
37
38     namespace {
39         struct equal_item
40         {
41             template <typename T>
42             bool operator ()( T const& i1, T const& i2) const
43             {
44                 return i1.key() == i2.key();
45             }
46         };
47
48         typedef std::unordered_set< StripedSetHdrTest::item, StripedSetHdrTest::hash_int, equal_item > set_t;
49
50         struct my_copy_policy {
51             typedef set_t::iterator iterator;
52
53             void operator()( set_t& set, iterator itWhat )
54             {
55                 set.insert( std::make_pair(itWhat->key(), itWhat->val()) );
56             }
57         };
58     }
59
60     void StripedSetHdrTest::Refinable_hashset()
61     {
62         CPPUNIT_MESSAGE( "cmp");
63         typedef cc::StripedSet< set_t
64             , co::hash< hash_int >
65             , co::compare< cmp<item> >
66             ,co::mutex_policy< cc::striped_set::refinable<> >
67         >   set_cmp;
68         test_striped< set_cmp >();
69
70         CPPUNIT_MESSAGE( "less");
71         typedef cc::StripedSet< set_t
72             ,co::mutex_policy< cc::striped_set::refinable<> >
73             , co::hash< hash_int >
74             , co::less< less<item> >
75         >   set_less;
76         test_striped< set_less >();
77
78         CPPUNIT_MESSAGE( "cmpmix");
79         typedef cc::StripedSet< set_t
80             ,co::mutex_policy< cc::striped_set::refinable<> >
81             , co::hash< hash_int >
82             , co::compare< cmp<item> >
83             , co::less< less<item> >
84         >   set_cmpmix;
85         test_striped< set_cmpmix >();
86
87         // Spinlock as lock policy
88         CPPUNIT_MESSAGE( "spinlock");
89         typedef cc::StripedSet< set_t
90             , co::mutex_policy< cc::striped_set::refinable<cds::sync::reentrant_spin> >
91             , co::hash< hash_int >
92             , co::less< less<item> >
93         >   set_spin;
94         test_striped< set_spin >();
95
96         // Resizing policy
97         CPPUNIT_MESSAGE( "load_factor_resizing<0>(1024)");
98         {
99             typedef cc::StripedSet< set_t
100                 ,co::mutex_policy< cc::striped_set::refinable<> >
101                 , co::hash< hash_int >
102                 , co::less< less<item> >
103                 , co::resizing_policy< cc::striped_set::load_factor_resizing<0> >
104             >   set_less_resizing_lf;
105             set_less_resizing_lf s(30, cc::striped_set::load_factor_resizing<0>(1024));
106             test_striped_with(s);
107         }
108
109         CPPUNIT_MESSAGE( "load_factor_resizing<256>");
110         typedef cc::StripedSet< set_t
111             ,co::mutex_policy< cc::striped_set::refinable<> >
112             , co::hash< hash_int >
113             , co::less< less<item> >
114             , co::resizing_policy< cc::striped_set::load_factor_resizing<256> >
115         >   set_less_resizing_lf16;
116         test_striped< set_less_resizing_lf16 >();
117
118         CPPUNIT_MESSAGE( "single_bucket_size_threshold<0>(1024)");
119         {
120             typedef cc::StripedSet< set_t
121                 ,co::mutex_policy< cc::striped_set::refinable<> >
122                 , co::hash< hash_int >
123                 , co::less< less<item> >
124                 , co::resizing_policy< cc::striped_set::single_bucket_size_threshold<0> >
125             >   set_less_resizing_sbt;
126             set_less_resizing_sbt s(30, cc::striped_set::single_bucket_size_threshold<0>(1024) );
127             test_striped_with(s);
128         }
129
130         CPPUNIT_MESSAGE( "single_bucket_size_threshold<256>");
131         typedef cc::StripedSet< set_t
132             ,co::mutex_policy< cc::striped_set::refinable<> >
133             , co::hash< hash_int >
134             , co::less< less<item> >
135             , co::resizing_policy< cc::striped_set::single_bucket_size_threshold<256> >
136         >   set_less_resizing_sbt16;
137         test_striped< set_less_resizing_sbt16 >();
138
139         // Copy policy
140         CPPUNIT_MESSAGE( "load_factor_resizing<256>, copy_item");
141         typedef cc::StripedSet< set_t
142             ,co::mutex_policy< cc::striped_set::refinable<> >
143             , co::hash< hash_int >
144             , co::compare< cmp<item> >
145             , co::resizing_policy< cc::striped_set::load_factor_resizing<256> >
146             , co::copy_policy< cc::striped_set::copy_item >
147         >   set_copy_item;
148         test_striped< set_copy_item >();
149
150         CPPUNIT_MESSAGE( "load_factor_resizing<256>, swap_item");
151         typedef cc::StripedSet< set_t
152             ,co::mutex_policy< cc::striped_set::refinable<> >
153             , co::hash< hash_int >
154             , co::compare< cmp<item> >
155             , co::resizing_policy< cc::striped_set::load_factor_resizing<256> >
156             , co::copy_policy< cc::striped_set::swap_item >
157         >   set_swap_item;
158         test_striped< set_swap_item >();
159
160         CPPUNIT_MESSAGE( "load_factor_resizing<256>, move_item");
161         typedef cc::StripedSet< set_t
162             ,co::mutex_policy< cc::striped_set::refinable<> >
163             , co::hash< hash_int >
164             , co::compare< cmp<item> >
165             , co::resizing_policy< cc::striped_set::load_factor_resizing<256> >
166             , co::copy_policy< cc::striped_set::move_item >
167         >   set_move_item;
168         test_striped< set_move_item >();
169
170         CPPUNIT_MESSAGE( "load_factor_resizing<256>, special copy_item");
171         typedef cc::StripedSet< set_t
172             ,co::mutex_policy< cc::striped_set::refinable<> >
173             , co::hash< hash_int >
174             , co::compare< cmp<item> >
175             , co::resizing_policy< cc::striped_set::load_factor_resizing<256> >
176             , co::copy_policy< my_copy_policy >
177         >   set_special_copy_item;
178         test_striped< set_special_copy_item >();
179     }
180
181 }   // namespace set