File indexing completed on 2025-01-31 10:02:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef BOOST_TEST_DATA_MONOMORPHIC_GENERATORS_RANDOM_HPP_101512GER
0013 #define BOOST_TEST_DATA_MONOMORPHIC_GENERATORS_RANDOM_HPP_101512GER
0014
0015
0016 #include <boost/test/data/config.hpp>
0017
0018
0019
0020
0021 #if !defined(BOOST_TEST_NO_RANDOM_DATASET_AVAILABLE) || defined(BOOST_TEST_DOXYGEN_DOC__)
0022
0023 #include <boost/test/data/monomorphic/generate.hpp>
0024 #include <boost/test/data/monomorphic/generators/keywords.hpp>
0025
0026
0027 #include <random>
0028
0029 #include <boost/test/detail/suppress_warnings.hpp>
0030
0031
0032
0033 namespace boost {
0034 namespace unit_test {
0035 namespace data {
0036
0037 namespace {
0038 nfp::keyword<struct seed_t> seed;
0039 nfp::keyword<struct distribution_t> distribution;
0040 nfp::keyword<struct engine_t> engine;
0041 }
0042
0043 namespace monomorphic {
0044
0045 namespace ds_detail {
0046 template<typename SampleType>
0047 struct default_distribution {
0048 typedef typename mpl::if_<std::is_integral<SampleType>,
0049 std::uniform_int_distribution<SampleType>,
0050 std::uniform_real_distribution<SampleType>>::type type;
0051 };
0052
0053 }
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 template<typename SampleType = double,
0065 typename DistributionType = typename ds_detail::default_distribution<SampleType>::type,
0066 typename EngineType = std::default_random_engine>
0067 class random_t {
0068 public:
0069 typedef SampleType sample;
0070 typedef DistributionType distr_type;
0071 typedef EngineType engine_type;
0072
0073 random_t()
0074 : m_distribution()
0075 , m_engine( std::random_device()() )
0076 {}
0077 explicit random_t( distr_type&& d )
0078 : m_distribution( std::forward<distr_type>(d) )
0079 , m_engine( std::random_device()() ){}
0080 random_t( engine_type&& e, distr_type&& d )
0081 : m_distribution( std::forward<distr_type>(d) )
0082 , m_engine( std::forward<engine_type>(e) ){}
0083
0084
0085 data::size_t capacity() const { return BOOST_TEST_DS_INFINITE_SIZE; }
0086 SampleType next()
0087 {
0088 return m_distribution( m_engine );
0089 }
0090 void reset() {}
0091
0092
0093 template<typename SeedType>
0094 void seed( SeedType&& seed ) { m_engine.seed( std::forward<SeedType>( seed ) ); }
0095
0096 private:
0097
0098 DistributionType m_distribution;
0099 EngineType m_engine;
0100 };
0101
0102
0103
0104 }
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 inline monomorphic::generated_by< monomorphic::random_t<>> random()
0131 {
0132 return monomorphic::generated_by<monomorphic::random_t<>>( monomorphic::random_t<>() );
0133 }
0134
0135
0136
0137
0138 template<typename SampleType>
0139 inline monomorphic::generated_by< monomorphic::random_t<SampleType>>
0140 random( SampleType begin, SampleType end )
0141 {
0142 typedef monomorphic::random_t<SampleType> Gen;
0143 typedef typename Gen::distr_type distr_type;
0144 return monomorphic::generated_by<Gen>( Gen( distr_type(begin,end) ) );
0145 }
0146
0147
0148
0149 namespace ds_detail {
0150 template<typename Params>
0151 struct random_gen_type {
0152 typedef typename nfp::param_type<Params,decltype(distribution),std::uniform_real_distribution<>>::type distr_type;
0153 typedef typename nfp::param_type<Params,decltype(engine),std::default_random_engine>::type engine_type;
0154 typedef typename distr_type::result_type sample_type;
0155
0156 typedef monomorphic::random_t<sample_type,distr_type,engine_type> type;
0157 };
0158
0159 }
0160
0161
0162
0163 template<typename Params>
0164 inline monomorphic::generated_by<typename ds_detail::random_gen_type<Params>::type>
0165 random( Params const& params )
0166 {
0167 typedef typename ds_detail::random_gen_type<Params>::type Gen;
0168 typedef typename Gen::distr_type distr_type;
0169 typedef typename Gen::engine_type engine_type;
0170
0171 std::random_device rd;
0172 engine_type E;
0173
0174 if( params.has(engine) )
0175 E = params[engine];
0176
0177 distr_type D;
0178 if( params.has(distribution) )
0179 D = params[distribution];
0180
0181 Gen G( std::move(E), std::move(D) );
0182
0183 if( params.has(seed) )
0184 G.seed( params[seed] );
0185
0186 return monomorphic::generated_by<Gen>( std::move(G) );
0187 }
0188
0189 }
0190 }
0191 }
0192
0193 #include <boost/test/detail/enable_warnings.hpp>
0194
0195 #endif
0196
0197
0198 #endif