File: test_seed_seq.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (130 lines) | stat: -rw-r--r-- 4,341 bytes parent folder | download | duplicates (16)
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
/* boost test_seed_seq.cpp
 *
 * Copyright Steven Watanabe 2010
 * 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/seed_seq.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/config.hpp>
#include <vector>

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

using boost::assign::list_of;

BOOST_AUTO_TEST_CASE(test_seed_seq) {
    boost::uint32_t expected_param[4] = { 2, 3, 4, 0xdeadbeaf };
    boost::uint32_t param[4] = { 2, 3, 4, 0xdeadbeaf };
    boost::uint32_t store32[10];
    boost::uint64_t store64[10];
    boost::uint32_t expected[10] = {
        3155793538u,
        2047427591u,
        2886057794u,
        280666868u,
        2184015838u,
        4035763234u,
        808987374u,
        3177165994u,
        2993445429u,
        3110180644u
    };
    std::fill_n(&store32[0], 10, 0);
    std::fill_n(&store64[0], 10, 0);
    boost::random::seed_seq seq;
    seq.generate(&store32[0], &store32[0] + 10);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &store32[0], &store32[0] + 10, &expected[0], &expected[0] + 10);
    seq.generate(&store64[0], &store64[0] + 10);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &store64[0], &store64[0] + 10, &expected[0], &expected[0] + 10);
    BOOST_CHECK_EQUAL(seq.size(), 0u);
    seq.param(&param[0]);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &param[0], &param[0] + 4, &expected_param[0], &expected_param[0] + 4);

    boost::uint32_t expected_r[10] = {
        2681148375u,
        3302224839u,
        249244011u,
        1549723892u,
        3429166360u,
        2812310274u,
        3902694127u,
        1014283089u,
        1122383019u,
        494552679u
    };

    std::vector<int> data = list_of(2)(3)(4);
    
    std::fill_n(&store32[0], 10, 0);
    std::fill_n(&store64[0], 10, 0);
    std::fill_n(&param[0], 3, 0);
    boost::random::seed_seq seq_r(data);
    seq_r.generate(&store32[0], &store32[0] + 10);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &store32[0], &store32[0] + 10, &expected_r[0], &expected_r[0] + 10);
    seq_r.generate(&store64[0], &store64[0] + 10);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &store64[0], &store64[0] + 10, &expected_r[0], &expected_r[0] + 10);
    BOOST_CHECK_EQUAL(seq_r.size(), 3u);
    seq_r.param(&param[0]);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &param[0], &param[0] + 4, &expected_param[0], &expected_param[0] + 4);
    
    std::fill_n(&store32[0], 10, 0);
    std::fill_n(&store64[0], 10, 0);
    std::fill_n(&param[0], 3, 0);
    boost::random::seed_seq seq_it(data.begin(), data.end());
    seq_it.generate(&store32[0], &store32[0] + 10);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &store32[0], &store32[0] + 10, &expected_r[0], &expected_r[0] + 10);
    seq_it.generate(&store64[0], &store64[0] + 10);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &store64[0], &store64[0] + 10, &expected_r[0], &expected_r[0] + 10);
    BOOST_CHECK_EQUAL(seq_it.size(), 3u);
    seq_it.param(&param[0]);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &param[0], &param[0] + 4, &expected_param[0], &expected_param[0] + 4);
    
#ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
    std::fill_n(&store32[0], 10, 0);
    std::fill_n(&store64[0], 10, 0);
    std::fill_n(&param[0], 3, 0);
    boost::random::seed_seq seq_il = {2, 3, 4};
    seq_il.generate(&store32[0], &store32[0] + 10);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &store32[0], &store32[0] + 10, &expected_r[0], &expected_r[0] + 10);
    seq_il.generate(&store64[0], &store64[0] + 10);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &store64[0], &store64[0] + 10, &expected_r[0], &expected_r[0] + 10);
    BOOST_CHECK_EQUAL(seq_il.size(), 3u);
    seq_il.param(&param[0]);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &param[0], &param[0] + 4, &expected_param[0], &expected_param[0] + 4);
#endif
}

BOOST_AUTO_TEST_CASE(test_seed_seq_short_output) {
    boost::uint32_t store32[2];

    boost::uint32_t expected_short[2] = {
        4149590228u,
        3175758659u
    };
    
    std::vector<int> data = list_of(2)(3)(4)(5);

    boost::random::seed_seq seq(data);

    seq.generate(&store32[0], &store32[0] + 2);
    BOOST_CHECK_EQUAL_COLLECTIONS(
        &store32[0], &store32[0] + 2, &expected_short[0], &expected_short[0] + 2);
}