1 // (C) Copyright Gennadiy Rozental 2011-2015.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 // See http://www.boost.org/libs/test for the library home page.
7 //
8 //! @file
9 //! @brief tests random dataset
10 // ***************************************************************************
11
12 // Boost.Test
13 #include <boost/test/unit_test.hpp>
14 #include <boost/test/data/monomorphic/generators/random.hpp>
15 #include <boost/test/data/monomorphic/zip.hpp>
16 #include <boost/test/data/monomorphic/array.hpp>
17 #include <boost/test/data/monomorphic/grid.hpp>
18 #include <boost/test/data/for_each_sample.hpp>
19 namespace data=boost::unit_test::data;
20
21 #include "datasets-test.hpp"
22
23 //____________________________________________________________________________//
24
BOOST_AUTO_TEST_CASE(test_default)25 BOOST_AUTO_TEST_CASE( test_default )
26 {
27 BOOST_TEST( data::random().size() == data::BOOST_TEST_DS_INFINITE_SIZE );
28
29 auto ds = data::random();
30
31 BOOST_CHECK_THROW( data::for_each_sample( ds, [](double ) {}), std::logic_error );
32
33 invocation_count ic;
34
35 ic.m_value = 0;
36 data::for_each_sample( ds, ic, 10 );
37 BOOST_TEST( ic.m_value == 10 );
38
39 ic.m_value = 0;
40 int arr[] = {1,2,3,4,5};
41 data::for_each_sample( ds^arr, ic );
42 BOOST_TEST( ic.m_value == 5 );
43
44 BOOST_CHECK_THROW( (ds * arr).size(), std::logic_error );
45 BOOST_CHECK_THROW( (arr * ds).size(), std::logic_error );
46 }
47
48 //____________________________________________________________________________//
49
BOOST_AUTO_TEST_CASE(test_uniform_range)50 BOOST_AUTO_TEST_CASE( test_uniform_range )
51 {
52 auto ds1 = data::random(1,5);
53
54 data::for_each_sample( ds1, [](int s) {
55 BOOST_TEST(s>=1);
56 BOOST_TEST(s<=5);
57 }, 10);
58
59 auto ds2 = data::random(1.,2.);
60
61 data::for_each_sample( ds2, [](double s) {
62 BOOST_TEST(s>=1.);
63 BOOST_TEST(s<=2.);
64 }, 100);
65 }
66
67 //____________________________________________________________________________//
68
BOOST_AUTO_TEST_CASE(test_parameterized_init)69 BOOST_AUTO_TEST_CASE( test_parameterized_init )
70 {
71 auto ds1 = data::random(data::distribution = std::normal_distribution<>(5.,2));
72 typedef decltype(ds1) DS1;
73
74 BOOST_TEST(( std::is_same<DS1::generator_type::distr_type,
75 std::normal_distribution<>>::value ));
76 BOOST_TEST(( std::is_same<DS1::generator_type::sample,double>::value ));
77 BOOST_TEST(( std::is_same<DS1::generator_type::engine_type,
78 std::default_random_engine>::value ));
79
80 auto ds2 = data::random(data::distribution = std::discrete_distribution<>());
81 typedef decltype(ds2) DS2;
82
83 BOOST_TEST(( std::is_same<DS2::generator_type::distr_type,
84 std::discrete_distribution<>>::value ));
85 BOOST_TEST(( std::is_same<DS2::generator_type::sample,int>::value ));
86 BOOST_TEST(( std::is_same<DS2::generator_type::engine_type,
87 std::default_random_engine>::value ));
88
89 auto ds3 = data::random(data::engine = std::minstd_rand());
90 typedef decltype(ds3) DS3;
91
92 BOOST_TEST(( std::is_same<DS3::generator_type::distr_type,
93 std::uniform_real_distribution<>>::value ));
94 BOOST_TEST(( std::is_same<DS3::generator_type::sample,double>::value ));
95 BOOST_TEST(( std::is_same<DS3::generator_type::engine_type,
96 std::minstd_rand>::value ));
97
98 auto ds4 = data::random(data::seed = 100UL);
99 typedef decltype(ds4) DS4;
100
101 BOOST_TEST(( std::is_same<DS4::generator_type::distr_type,
102 std::uniform_real_distribution<>>::value ));
103 BOOST_TEST(( std::is_same<DS4::generator_type::sample,double>::value ));
104 BOOST_TEST(( std::is_same<DS4::generator_type::engine_type,
105 std::default_random_engine>::value ));
106
107 auto ds5 = data::random(data::seed = 100UL);
108
109 std::list<double> vals;
110 data::for_each_sample( ds4, [&vals](double s) {
111 vals.push_back( s );
112 }, 10);
113 data::for_each_sample( ds5, [&vals](double s) {
114 BOOST_TEST( vals.front() == s );
115 vals.pop_front();
116 }, 10);
117
118 auto ds6 = data::random(( data::engine = std::minstd_rand(),
119 data::distribution = std::normal_distribution<>(5.,2),
120 data::seed = 20UL ));
121 typedef decltype(ds6) DS6;
122
123 BOOST_TEST(( std::is_same<DS6::generator_type::distr_type,
124 std::normal_distribution<>>::value ));
125 BOOST_TEST(( std::is_same<DS6::generator_type::sample,double>::value ));
126 BOOST_TEST(( std::is_same<DS6::generator_type::engine_type,
127 std::minstd_rand>::value ));
128 }
129
130 //____________________________________________________________________________//
131
132 // EOF
133
134