File: test_uniform_on_sphere_distribution.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (87 lines) | stat: -rw-r--r-- 2,562 bytes parent folder | download | duplicates (18)
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
/* test_uniform_on_sphere_distribution.cpp
 *
 * Copyright Steven Watanabe 2011
 * 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)
 *
 * $Id$
 *
 */

#include <boost/random/uniform_on_sphere.hpp>
#include <boost/assign/list_of.hpp>

#include <limits>

#define BOOST_RANDOM_DISTRIBUTION boost::random::uniform_on_sphere<>
#define BOOST_RANDOM_ARG1 dim
#define BOOST_RANDOM_ARG1_DEFAULT 2
#define BOOST_RANDOM_ARG1_VALUE 3

std::vector<double> min0 = boost::assign::list_of(-1.0)(0.0);
std::vector<double> max0 = boost::assign::list_of(1.0)(0.0);
std::vector<double> min1 = boost::assign::list_of(-1.0)(0.0)(0.0);
std::vector<double> max1 = boost::assign::list_of(1.0)(0.0)(0.0);

#define BOOST_RANDOM_DIST0_MIN min0
#define BOOST_RANDOM_DIST0_MAX max0
#define BOOST_RANDOM_DIST1_MIN min1
#define BOOST_RANDOM_DIST1_MAX max1

#define BOOST_RANDOM_TEST1_PARAMS (0)
#define BOOST_RANDOM_TEST1_MIN std::vector<double>()
#define BOOST_RANDOM_TEST1_MAX std::vector<double>()
#define BOOST_RANDOM_TEST2_PARAMS
#define BOOST_RANDOM_TEST2_MIN min0
#define BOOST_RANDOM_TEST2_MAX max0

#include <boost/test/test_tools.hpp>

BOOST_TEST_DONT_PRINT_LOG_VALUE( std::vector<double> )

#include "test_distribution.ipp"

#include <boost/math/special_functions/fpclassify.hpp>

struct generate_zeros {
public:
    generate_zeros() : i(0) {}
    typedef unsigned result_type;
    static unsigned (min)() { return 0u; }
    static unsigned (max)() { return boost::random::minstd_rand0::max(); }
    unsigned operator()() {
        static unsigned data[] = { 0, 0, 0, 0, 0, 0 };
        if(i < 6) {
            return data[i++];
        } else {
            return gen();
        }
    }
private:
    int i;
    boost::random::minstd_rand0 gen;
};

BOOST_AUTO_TEST_CASE(test_zeros) {
    generate_zeros gen;
    boost::random::uniform_on_sphere<> dist(2);
    std::vector<double> val = dist(gen);
    BOOST_CHECK(!(boost::math::isnan)(val[0]));
}

BOOST_AUTO_TEST_CASE(test_valid_output) {
    boost::random::minstd_rand0 gen;
    for(int n = 0; n < 10; ++n) {
        boost::random::uniform_on_sphere<> dist(n);
        std::vector<double> result = dist(gen);
        BOOST_TEST(result.size() == static_cast<std::size_t>(n));
        if(n > 0) {
            double sum_sq = 0;
            for(std::size_t j = 0; j < result.size(); ++j) {
                sum_sq += result[j] * result[j];
            }
            BOOST_CHECK_CLOSE_FRACTION(sum_sq, 1.0, 1e-5);
        }
    }
}