1 /* test_uniform_on_sphere_distribution.cpp 2 * 3 * Copyright Steven Watanabe 2011 4 * Distributed under the Boost Software License, Version 1.0. (See 5 * accompanying file LICENSE_1_0.txt or copy at 6 * http://www.boost.org/LICENSE_1_0.txt) 7 * 8 * $Id$ 9 * 10 */ 11 12 #include <boost/random/uniform_on_sphere.hpp> 13 #include <boost/assign/list_of.hpp> 14 15 #include <limits> 16 17 #define BOOST_RANDOM_DISTRIBUTION boost::random::uniform_on_sphere<> 18 #define BOOST_RANDOM_ARG1 dim 19 #define BOOST_RANDOM_ARG1_DEFAULT 2 20 #define BOOST_RANDOM_ARG1_VALUE 3 21 22 std::vector<double> min0 = boost::assign::list_of(-1.0)(0.0); 23 std::vector<double> max0 = boost::assign::list_of(1.0)(0.0); 24 std::vector<double> min1 = boost::assign::list_of(-1.0)(0.0)(0.0); 25 std::vector<double> max1 = boost::assign::list_of(1.0)(0.0)(0.0); 26 27 #define BOOST_RANDOM_DIST0_MIN min0 28 #define BOOST_RANDOM_DIST0_MAX max0 29 #define BOOST_RANDOM_DIST1_MIN min1 30 #define BOOST_RANDOM_DIST1_MAX max1 31 32 #define BOOST_RANDOM_TEST1_PARAMS (0) 33 #define BOOST_RANDOM_TEST1_MIN std::vector<double>() 34 #define BOOST_RANDOM_TEST1_MAX std::vector<double>() 35 #define BOOST_RANDOM_TEST2_PARAMS 36 #define BOOST_RANDOM_TEST2_MIN min0 37 #define BOOST_RANDOM_TEST2_MAX max0 38 39 #include <boost/test/test_tools.hpp> 40 41 BOOST_TEST_DONT_PRINT_LOG_VALUE( std::vector<double> ) 42 43 #include "test_distribution.ipp" 44 45 #include <boost/math/special_functions/fpclassify.hpp> 46 47 struct generate_zeros { 48 public: generate_zerosgenerate_zeros49 generate_zeros() : i(0) {} 50 typedef unsigned result_type; 51 static unsigned (min)() { return 0u; } 52 static unsigned (max)() { return boost::random::minstd_rand0::max(); } operator ()generate_zeros53 unsigned operator()() { 54 static unsigned data[] = { 0, 0, 0, 0, 0, 0 }; 55 if(i < 6) { 56 return data[i++]; 57 } else { 58 return gen(); 59 } 60 } 61 private: 62 int i; 63 boost::random::minstd_rand0 gen; 64 }; 65 BOOST_AUTO_TEST_CASE(test_zeros)66BOOST_AUTO_TEST_CASE(test_zeros) { 67 generate_zeros gen; 68 boost::random::uniform_on_sphere<> dist(2); 69 std::vector<double> val = dist(gen); 70 BOOST_CHECK(!(boost::math::isnan)(val[0])); 71 } 72 BOOST_AUTO_TEST_CASE(test_valid_output)73BOOST_AUTO_TEST_CASE(test_valid_output) { 74 boost::random::minstd_rand0 gen; 75 for(int n = 0; n < 10; ++n) { 76 boost::random::uniform_on_sphere<> dist(n); 77 std::vector<double> result = dist(gen); 78 BOOST_TEST(result.size() == static_cast<std::size_t>(n)); 79 if(n > 0) { 80 double sum_sq = 0; 81 for(std::size_t j = 0; j < result.size(); ++j) { 82 sum_sq += result[j] * result[j]; 83 } 84 BOOST_CHECK_CLOSE_FRACTION(sum_sq, 1.0, 1e-5); 85 } 86 } 87 } 88