test_data.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_PQUEUE_TEST_DATA_H
  6. #define CDSUNIT_PQUEUE_TEST_DATA_H
  7. #include <cds_test/fixture.h>
  8. #include <memory>
  9. namespace cds_test {
  10. class PQueueTest : public ::cds_test::fixture
  11. {
  12. public:
  13. typedef int key_type;
  14. struct value_type {
  15. key_type k;
  16. int v;
  17. value_type()
  18. {}
  19. value_type( value_type const& kv )
  20. : k( kv.k )
  21. , v( kv.v )
  22. {}
  23. value_type( key_type key )
  24. : k( key )
  25. , v( key )
  26. {}
  27. value_type( key_type key, int val )
  28. : k( key )
  29. , v( val )
  30. {}
  31. value_type( std::pair<key_type, int> const& p )
  32. : k( p.first )
  33. , v( p.second )
  34. {}
  35. };
  36. struct compare {
  37. int operator()( value_type k1, value_type k2 ) const
  38. {
  39. return k1.k - k2.k;
  40. }
  41. };
  42. struct less {
  43. bool operator()( value_type k1, value_type k2 ) const
  44. {
  45. return k1.k < k2.k;
  46. }
  47. };
  48. enum {
  49. c_nMinValue = -123,
  50. c_nCapacity = 1024
  51. };
  52. protected:
  53. template <typename T>
  54. class data_array
  55. {
  56. std::unique_ptr<T[]> pFirst;
  57. T * pLast;
  58. public:
  59. data_array( size_t nSize )
  60. : pFirst( new T[nSize] )
  61. , pLast( pFirst.get() + nSize )
  62. {
  63. key_type i = c_nMinValue;
  64. for ( T * p = pFirst.get(); p != pLast; ++p, ++i )
  65. p->k = p->v = i;
  66. shuffle( pFirst.get(), pLast );
  67. }
  68. T * begin() { return pFirst.get(); }
  69. T * end() { return pLast; }
  70. size_t size() const
  71. {
  72. return pLast - pFirst;
  73. }
  74. };
  75. };
  76. } // namespace cds_test
  77. namespace std {
  78. template<>
  79. struct less<cds_test::PQueueTest::value_type>
  80. {
  81. bool operator()( cds_test::PQueueTest::value_type const& v1, cds_test::PQueueTest::value_type const& v2 ) const
  82. {
  83. return cds_test::PQueueTest::less()(v1, v2);
  84. }
  85. };
  86. }
  87. #endif // CDSUNIT_PQUEUE_FCPQUEUE_H