Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / random / test / test_seed_seq.cpp
1 /* boost test_seed_seq.cpp
2  *
3  * Copyright Steven Watanabe 2010
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: test_seed_seq.cpp 79771 2012-07-27 18:15:55Z jewillco $
9  */
10
11 #include <boost/random/seed_seq.hpp>
12 #include <boost/assign/list_of.hpp>
13 #include <boost/config.hpp>
14 #include <vector>
15
16 #define BOOST_TEST_MAIN
17 #include <boost/test/unit_test.hpp>
18
19 using boost::assign::list_of;
20
21 BOOST_AUTO_TEST_CASE(test_seed_seq) {
22     boost::uint32_t expected_param[4] = { 2, 3, 4, 0xdeadbeaf };
23     boost::uint32_t param[4] = { 2, 3, 4, 0xdeadbeaf };
24     boost::uint32_t store32[10];
25     boost::uint64_t store64[10];
26     boost::uint32_t expected[10] = {
27         3155793538u,
28         2047427591u,
29         2886057794u,
30         280666868u,
31         2184015838u,
32         4035763234u,
33         808987374u,
34         3177165994u,
35         2993445429u,
36         3110180644u
37     };
38     std::fill_n(&store32[0], 10, 0);
39     std::fill_n(&store64[0], 10, 0);
40     boost::random::seed_seq seq;
41     seq.generate(&store32[0], &store32[0] + 10);
42     BOOST_CHECK_EQUAL_COLLECTIONS(
43         &store32[0], &store32[0] + 10, &expected[0], &expected[0] + 10);
44     seq.generate(&store64[0], &store64[0] + 10);
45     BOOST_CHECK_EQUAL_COLLECTIONS(
46         &store64[0], &store64[0] + 10, &expected[0], &expected[0] + 10);
47     BOOST_CHECK_EQUAL(seq.size(), 0u);
48     seq.param(&param[0]);
49     BOOST_CHECK_EQUAL_COLLECTIONS(
50         &param[0], &param[0] + 4, &expected_param[0], &expected_param[0] + 4);
51
52     boost::uint32_t expected_r[10] = {
53         2681148375u,
54         3302224839u,
55         249244011u,
56         1549723892u,
57         3429166360u,
58         2812310274u,
59         3902694127u,
60         1014283089u,
61         1122383019u,
62         494552679u
63     };
64
65     std::vector<int> data = list_of(2)(3)(4);
66     
67     std::fill_n(&store32[0], 10, 0);
68     std::fill_n(&store64[0], 10, 0);
69     std::fill_n(&param[0], 3, 0);
70     boost::random::seed_seq seq_r(data);
71     seq_r.generate(&store32[0], &store32[0] + 10);
72     BOOST_CHECK_EQUAL_COLLECTIONS(
73         &store32[0], &store32[0] + 10, &expected_r[0], &expected_r[0] + 10);
74     seq_r.generate(&store64[0], &store64[0] + 10);
75     BOOST_CHECK_EQUAL_COLLECTIONS(
76         &store64[0], &store64[0] + 10, &expected_r[0], &expected_r[0] + 10);
77     BOOST_CHECK_EQUAL(seq_r.size(), 3u);
78     seq_r.param(&param[0]);
79     BOOST_CHECK_EQUAL_COLLECTIONS(
80         &param[0], &param[0] + 4, &expected_param[0], &expected_param[0] + 4);
81     
82     std::fill_n(&store32[0], 10, 0);
83     std::fill_n(&store64[0], 10, 0);
84     std::fill_n(&param[0], 3, 0);
85     boost::random::seed_seq seq_it(data.begin(), data.end());
86     seq_it.generate(&store32[0], &store32[0] + 10);
87     BOOST_CHECK_EQUAL_COLLECTIONS(
88         &store32[0], &store32[0] + 10, &expected_r[0], &expected_r[0] + 10);
89     seq_it.generate(&store64[0], &store64[0] + 10);
90     BOOST_CHECK_EQUAL_COLLECTIONS(
91         &store64[0], &store64[0] + 10, &expected_r[0], &expected_r[0] + 10);
92     BOOST_CHECK_EQUAL(seq_it.size(), 3u);
93     seq_it.param(&param[0]);
94     BOOST_CHECK_EQUAL_COLLECTIONS(
95         &param[0], &param[0] + 4, &expected_param[0], &expected_param[0] + 4);
96     
97 #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
98     std::fill_n(&store32[0], 10, 0);
99     std::fill_n(&store64[0], 10, 0);
100     std::fill_n(&param[0], 3, 0);
101     boost::random::seed_seq seq_il = {2, 3, 4};
102     seq_il.generate(&store32[0], &store32[0] + 10);
103     BOOST_CHECK_EQUAL_COLLECTIONS(
104         &store32[0], &store32[0] + 10, &expected_r[0], &expected_r[0] + 10);
105     seq_il.generate(&store64[0], &store64[0] + 10);
106     BOOST_CHECK_EQUAL_COLLECTIONS(
107         &store64[0], &store64[0] + 10, &expected_r[0], &expected_r[0] + 10);
108     BOOST_CHECK_EQUAL(seq_il.size(), 3u);
109     seq_il.param(&param[0]);
110     BOOST_CHECK_EQUAL_COLLECTIONS(
111         &param[0], &param[0] + 4, &expected_param[0], &expected_param[0] + 4);
112 #endif
113 }