//---------------------------------------------------------------------------// // Copyright (c) 2014 Roshan // // Distributed under the Boost Software License, Version 1.0 // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt // // See http://boostorg.github.com/compute for more information. //---------------------------------------------------------------------------// #define BOOST_TEST_MODULE TestDiscreteDistribution #include #include #include #include #include #include #include #include #include #include "context_setup.hpp" BOOST_AUTO_TEST_CASE(discrete_distribution_doctest) { using boost::compute::uint_; using boost::compute::lambda::_1; boost::compute::vector vec(100, context); //! [generate] // initialize the default random engine boost::compute::default_random_engine engine(queue); // initialize weights int weights[] = {2, 2}; // setup the discrete distribution to produce integers 0 and 1 // with equal weights boost::compute::discrete_distribution distribution(weights, weights+2); // generate the random values and store them to 'vec' distribution.generate(vec.begin(), vec.end(), engine, queue); // ! [generate] BOOST_CHECK_EQUAL( boost::compute::count_if( vec.begin(), vec.end(), _1 > 1, queue ), size_t(0) ); } BOOST_AUTO_TEST_CASE(discrete_distribution) { using boost::compute::uint_; using boost::compute::lambda::_1; size_t size = 100; boost::compute::vector vec(size, context); // initialize the default random engine boost::compute::default_random_engine engine(queue); // initialize weights int weights[] = {10, 40, 40, 10}; // setup the discrete distribution boost::compute::discrete_distribution distribution( weights, weights + 4 ); std::vector p = distribution.probabilities(); BOOST_CHECK_CLOSE(p[0], double(0.1), 0.001); BOOST_CHECK_CLOSE(p[1], double(0.4), 0.001); BOOST_CHECK_CLOSE(p[2], double(0.4), 0.001); BOOST_CHECK_CLOSE(p[3], double(0.1), 0.001); BOOST_CHECK_EQUAL((distribution.min)(), uint_(0)); BOOST_CHECK_EQUAL((distribution.max)(), uint_(3)); // generate the random values and store them to 'vec' distribution.generate(vec.begin(), vec.end(), engine, queue); BOOST_CHECK_EQUAL( boost::compute::count_if( vec.begin(), vec.end(), _1 < 4, queue ), size ); } BOOST_AUTO_TEST_CASE(discrete_distribution_default_ctor) { using boost::compute::uint_; using boost::compute::lambda::_1; size_t size = 100; boost::compute::vector vec(size, context); // initialize the default random engine boost::compute::default_random_engine engine(queue); // call default constructor boost::compute::discrete_distribution distribution; std::vector p = distribution.probabilities(); BOOST_CHECK_CLOSE(p[0], double(1), 0.001); // generate the random values and store them to 'vec' distribution.generate(vec.begin(), vec.end(), engine, queue); BOOST_CHECK_EQUAL( boost::compute::count_if( vec.begin(), vec.end(), _1 == 0, queue ), size ); } BOOST_AUTO_TEST_CASE(discrete_distribution_one_weight) { using boost::compute::uint_; using boost::compute::lambda::_1; size_t size = 100; boost::compute::vector vec(size, context); // initialize the default random engine boost::compute::default_random_engine engine(queue); std::vector weights(1, 1); // call default constructor boost::compute::discrete_distribution distribution( weights.begin(), weights.end() ); std::vector p = distribution.probabilities(); BOOST_CHECK_CLOSE(p[0], double(1), 0.001); BOOST_CHECK_EQUAL((distribution.min)(), uint_(0)); BOOST_CHECK_EQUAL((distribution.max)(), uint_(0)); // generate the random values and store them to 'vec' distribution.generate(vec.begin(), vec.end(), engine, queue); BOOST_CHECK_EQUAL( boost::compute::count_if( vec.begin(), vec.end(), _1 == 0, queue ), size ); } BOOST_AUTO_TEST_CASE(discrete_distribution_empty_weights) { using boost::compute::uint_; using boost::compute::lambda::_1; size_t size = 100; boost::compute::vector vec(size, context); // initialize the default random engine boost::compute::default_random_engine engine(queue); std::vector weights; // weights.begin() == weights.end() boost::compute::discrete_distribution distribution( weights.begin(), weights.end() ); std::vector p = distribution.probabilities(); BOOST_CHECK_CLOSE(p[0], double(1), 0.001); BOOST_CHECK_EQUAL((distribution.min)(), uint_(0)); BOOST_CHECK_EQUAL((distribution.max)(), uint_(0)); // generate the random values and store them to 'vec' distribution.generate(vec.begin(), vec.end(), engine, queue); BOOST_CHECK_EQUAL( boost::compute::count_if( vec.begin(), vec.end(), _1 == 0, queue ), size ); } BOOST_AUTO_TEST_CASE(discrete_distribution_uchar) { using boost::compute::uchar_; using boost::compute::uint_; using boost::compute::lambda::_1; size_t size = 100; boost::compute::vector uchar_vec(size, context); boost::compute::vector uint_vec(size, context); // initialize the default random engine boost::compute::default_random_engine engine(queue); // initialize weights std::vector weights(258, 0); weights[257] = 1; // setup the discrete distribution boost::compute::discrete_distribution distribution( weights.begin(), weights.end() ); BOOST_CHECK_EQUAL((distribution.min)(), uchar_(0)); BOOST_CHECK_EQUAL((distribution.max)(), uchar_(255)); // generate the random uchar_ values to the uchar_ vector distribution.generate(uchar_vec.begin(), uchar_vec.end(), engine, queue); BOOST_CHECK_EQUAL( boost::compute::count_if( uchar_vec.begin(), uchar_vec.end(), _1 == uchar_(1), queue ), size ); // generate the random uchar_ values to the uint_ vector distribution.generate(uint_vec.begin(), uint_vec.end(), engine, queue); BOOST_CHECK_EQUAL( boost::compute::count_if( uint_vec.begin(), uint_vec.end(), _1 == uint_(1), queue ), size ); } BOOST_AUTO_TEST_SUITE_END()