1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
//---------------------------------------------------------------------------//
// Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
//
// 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 <boost/test/unit_test.hpp>
#include <vector>
#include <boost/compute/system.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/algorithm/count_if.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/random/default_random_engine.hpp>
#include <boost/compute/random/discrete_distribution.hpp>
#include <boost/compute/lambda.hpp>
#include "context_setup.hpp"
BOOST_AUTO_TEST_CASE(discrete_distribution_doctest)
{
using boost::compute::uint_;
using boost::compute::lambda::_1;
boost::compute::vector<uint_> 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<uint_> 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<uint_> 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<uint_> distribution(
weights, weights + 4
);
std::vector<double> 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<uint_> vec(size, context);
// initialize the default random engine
boost::compute::default_random_engine engine(queue);
// call default constructor
boost::compute::discrete_distribution<uint_> distribution;
std::vector<double> 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<uint_> vec(size, context);
// initialize the default random engine
boost::compute::default_random_engine engine(queue);
std::vector<int> weights(1, 1);
// call default constructor
boost::compute::discrete_distribution<uint_> distribution(
weights.begin(), weights.end()
);
std::vector<double> 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<uint_> vec(size, context);
// initialize the default random engine
boost::compute::default_random_engine engine(queue);
std::vector<int> weights;
// weights.begin() == weights.end()
boost::compute::discrete_distribution<uint_> distribution(
weights.begin(), weights.end()
);
std::vector<double> 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_> uchar_vec(size, context);
boost::compute::vector<uint_> uint_vec(size, context);
// initialize the default random engine
boost::compute::default_random_engine engine(queue);
// initialize weights
std::vector<int> weights(258, 0);
weights[257] = 1;
// setup the discrete distribution
boost::compute::discrete_distribution<uchar_> 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()
|