test_map_data.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright (c) 2006-2018 Maxim Khizhinsky
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef CDSUNIT_MAP_TEST_MAP_DATA_H
  6. #define CDSUNIT_MAP_TEST_MAP_DATA_H
  7. #include <cds_test/check_size.h>
  8. #include <cds_test/fixture.h>
  9. #include <cds/opt/hash.h>
  10. namespace cds_test {
  11. class map_fixture: public fixture
  12. {
  13. public:
  14. struct key_type {
  15. int nKey;
  16. explicit key_type( int n )
  17. : nKey( n )
  18. {}
  19. explicit key_type( std::string const& str )
  20. : nKey( std::stoi( str ))
  21. {}
  22. key_type( key_type const& s )
  23. : nKey( s.nKey )
  24. {}
  25. };
  26. struct value_type {
  27. int nVal;
  28. std::string strVal;
  29. value_type()
  30. : nVal( 0 )
  31. {}
  32. explicit value_type( int n )
  33. : nVal( n )
  34. , strVal( std::to_string( n ))
  35. {}
  36. explicit value_type( std::string const& str )
  37. : nVal( std::stoi( str ))
  38. , strVal( str )
  39. {}
  40. explicit value_type( std::string&& str )
  41. : nVal( std::stoi( str ))
  42. , strVal( std::move( str ))
  43. {}
  44. value_type( int n, std::string const& str )
  45. : nVal( n )
  46. , strVal( str )
  47. {}
  48. value_type( int n, std::string&& str )
  49. : nVal( n )
  50. , strVal( std::move( str ))
  51. {}
  52. value_type( value_type&& v )
  53. : nVal( v.nVal )
  54. , strVal( std::move( v.strVal ))
  55. {}
  56. value_type( value_type const& v )
  57. : nVal( v.nVal )
  58. , strVal( v.strVal )
  59. {}
  60. value_type& operator=( value_type const& v )
  61. {
  62. if ( this != &v ) {
  63. nVal = v.nVal;
  64. strVal = v.strVal;
  65. }
  66. return *this;
  67. }
  68. value_type& operator=( value_type&& v )
  69. {
  70. if ( this != &v ) {
  71. nVal = v.nVal;
  72. strVal = std::move( v.strVal );
  73. }
  74. return *this;
  75. }
  76. value_type& operator=( int i )
  77. {
  78. nVal = i;
  79. strVal = std::to_string( i );
  80. return *this;
  81. }
  82. value_type& operator=( std::string const& s )
  83. {
  84. nVal = std::stoi( s );
  85. strVal = s;
  86. return *this;
  87. }
  88. };
  89. typedef std::pair<key_type const, value_type> pair_type;
  90. struct less
  91. {
  92. bool operator ()( key_type const& v1, key_type const& v2 ) const
  93. {
  94. return v1.nKey < v2.nKey;
  95. }
  96. bool operator ()( key_type const& v1, int v2 ) const
  97. {
  98. return v1.nKey < v2;
  99. }
  100. bool operator ()( int v1, key_type const& v2 ) const
  101. {
  102. return v1 < v2.nKey;
  103. }
  104. bool operator ()( key_type const& v1, std::string const& v2 ) const
  105. {
  106. return v1.nKey < std::stoi(v2 );
  107. }
  108. bool operator ()( std::string const& v1, key_type const& v2 ) const
  109. {
  110. return std::stoi( v1 ) < v2.nKey;
  111. }
  112. };
  113. struct cmp {
  114. int operator ()( key_type const& v1, key_type const& v2 ) const
  115. {
  116. if ( v1.nKey < v2.nKey )
  117. return -1;
  118. return v1.nKey > v2.nKey ? 1 : 0;
  119. }
  120. int operator ()( key_type const& v1, int v2 ) const
  121. {
  122. if ( v1.nKey < v2 )
  123. return -1;
  124. return v1.nKey > v2 ? 1 : 0;
  125. }
  126. int operator ()( int v1, key_type const& v2 ) const
  127. {
  128. if ( v1 < v2.nKey )
  129. return -1;
  130. return v1 > v2.nKey ? 1 : 0;
  131. }
  132. int operator ()( key_type const& v1, std::string const& v2 ) const
  133. {
  134. int n2 = std::stoi( v2 );
  135. if ( v1.nKey < n2 )
  136. return -1;
  137. return v1.nKey > n2 ? 1 : 0;
  138. }
  139. int operator ()( std::string const& v1, key_type const& v2 ) const
  140. {
  141. int n1 = std::stoi( v1 );
  142. if ( n1 < v2.nKey )
  143. return -1;
  144. return n1 > v2.nKey ? 1 : 0;
  145. }
  146. };
  147. struct hash1 {
  148. size_t operator()( int i ) const
  149. {
  150. return cds::opt::v::hash<int>()( i );
  151. }
  152. size_t operator()( std::string const& str ) const
  153. {
  154. return cds::opt::v::hash<int>()( std::stoi( str ));
  155. }
  156. template <typename T>
  157. size_t operator()( T const& i ) const
  158. {
  159. return cds::opt::v::hash<int>()(i.nKey);
  160. }
  161. };
  162. struct other_item {
  163. int nKey;
  164. other_item( int key )
  165. : nKey( key )
  166. {}
  167. };
  168. struct other_less
  169. {
  170. bool operator ()( key_type const& v1, other_item const& v2 ) const
  171. {
  172. return v1.nKey < v2.nKey;
  173. }
  174. bool operator ()( other_item const& v1, key_type const& v2 ) const
  175. {
  176. return v1.nKey < v2.nKey;
  177. }
  178. };
  179. };
  180. } // namespace cds_test
  181. #endif //