]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/uuid/test/test_random_generator.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / uuid / test / test_random_generator.cpp
1 // (C) Copyright Andy Tompkins 2010. Permission to copy, use, modify, sell and
2 // distribute this software is granted provided this copyright notice appears
3 // in all copies. This software is provided "as is" without express or implied
4 // warranty, and with no claim as to its suitability for any purpose.
5
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9
10 // libs/uuid/test/test_random_generator.cpp -------------------------------//
11
12 #include <boost/core/ignore_unused.hpp>
13 #include <boost/detail/lightweight_test.hpp>
14 #include <boost/predef/library/c/cloudabi.h>
15 #include <boost/random.hpp>
16 #include <boost/uuid/entropy_error.hpp>
17 #include <boost/uuid/random_generator.hpp>
18 #include <boost/uuid/uuid_io.hpp>
19 #include <boost/uuid/uuid.hpp>
20
21 template <typename RandomUuidGenerator>
22 void check_random_generator(RandomUuidGenerator& uuid_gen)
23 {
24 boost::uuids::uuid u1 = uuid_gen();
25 boost::uuids::uuid u2 = uuid_gen();
26
27 BOOST_TEST_NE(u1, u2);
28
29 // check variant
30 BOOST_TEST_EQ(u1.variant(), boost::uuids::uuid::variant_rfc_4122);
31
32 // version
33 BOOST_TEST_EQ(u1.version(), boost::uuids::uuid::version_random_number_based);
34 }
35
36 // This is the example block from the documentation - ensure it works!
37 void test_examples()
38 {
39 // Depending on the platform there may be a setup cost in
40 // initializing the generator so plan to reuse it if you can.
41 boost::uuids::random_generator gen;
42 boost::uuids::uuid id = gen();
43 boost::uuids::uuid id2 = gen();
44 #if !BOOST_LIB_C_CLOUDABI
45 std::cout << id << std::endl;
46 std::cout << id2 << std::endl;
47 #endif
48 BOOST_TEST_NE(id, id2);
49 boost::ignore_unused(id);
50 boost::ignore_unused(id2);
51
52 // You can still use a PseudoRandomNumberGenerator to create
53 // UUIDs, however this is not the preferred mechanism. For
54 // large numbers of UUIDs this may be more CPU efficient but
55 // it comes with all the perils of a PRNG. The test code
56 // in test_bench_random identifies the transition point for
57 // a given system.
58 boost::uuids::random_generator_mt19937 bulkgen;
59 for (size_t i = 0; i < 1000; ++i)
60 {
61 boost::uuids::uuid u = bulkgen();
62 // do something with u
63 boost::ignore_unused(u);
64 }
65 }
66
67 int main(int, char*[])
68 {
69 using namespace boost::uuids;
70
71 // default random number generator
72 random_generator uuid_gen1;
73 check_random_generator(uuid_gen1);
74
75 // specific random number generator
76 basic_random_generator<boost::rand48> uuid_gen2;
77 check_random_generator(uuid_gen2);
78
79 // pass by reference
80 boost::ecuyer1988 ecuyer1988_gen;
81 basic_random_generator<boost::ecuyer1988> uuid_gen3(ecuyer1988_gen);
82 check_random_generator(uuid_gen3);
83
84 // pass by pointer
85 boost::lagged_fibonacci607 lagged_fibonacci607_gen;
86 basic_random_generator<boost::lagged_fibonacci607> uuid_gen4(&lagged_fibonacci607_gen);
87 check_random_generator(uuid_gen4);
88
89 // there was a bug in basic_random_generator where it did not
90 // produce very random numbers. This checks for that bug.
91 random_generator_mt19937 bulkgen;
92 uuid u = bulkgen();
93 if ((u.data[4] == u.data[12]) &&
94 (u.data[5] == u.data[9] && u.data[5] == u.data[13]) &&
95 (u.data[7] == u.data[11] && u.data[7] == u.data[15]) &&
96 (u.data[10] == u.data[14]))
97 {
98 BOOST_ERROR("basic_random_generator is not producing random uuids");
99 }
100
101 // pseudo
102 check_random_generator(bulkgen);
103
104 // make sure default construction seeding is happening for PRNGs
105 random_generator_mt19937 gen1;
106 random_generator_mt19937 gen2;
107 BOOST_TEST_NE(gen1(), gen2());
108
109 // The example code snippet in the documentation
110 test_examples();
111
112 #if !BOOST_LIB_C_CLOUDABI
113 // dump 10 of them to cout for observation
114 for (size_t i = 0; i < 10; ++i)
115 {
116 std::cout << uuid_gen1() << std::endl;
117 }
118 #endif
119
120 return boost::report_errors();
121 }