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.
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)
10 // libs/uuid/test/test_random_generator.cpp -------------------------------//
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>
21 template <typename RandomUuidGenerator
>
22 void check_random_generator(RandomUuidGenerator
& uuid_gen
)
24 boost::uuids::uuid u1
= uuid_gen();
25 boost::uuids::uuid u2
= uuid_gen();
27 BOOST_TEST_NE(u1
, u2
);
30 BOOST_TEST_EQ(u1
.variant(), boost::uuids::uuid::variant_rfc_4122
);
33 BOOST_TEST_EQ(u1
.version(), boost::uuids::uuid::version_random_number_based
);
36 // This is the example block from the documentation - ensure it works!
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
;
48 BOOST_TEST_NE(id
, id2
);
49 boost::ignore_unused(id
);
50 boost::ignore_unused(id2
);
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
58 boost::uuids::random_generator_mt19937 bulkgen
;
59 for (size_t i
= 0; i
< 1000; ++i
)
61 boost::uuids::uuid u
= bulkgen();
62 // do something with u
63 boost::ignore_unused(u
);
67 int main(int, char*[])
69 using namespace boost::uuids
;
71 // default random number generator
72 random_generator uuid_gen1
;
73 check_random_generator(uuid_gen1
);
75 // specific random number generator
76 basic_random_generator
<boost::rand48
> uuid_gen2
;
77 check_random_generator(uuid_gen2
);
80 boost::ecuyer1988 ecuyer1988_gen
;
81 basic_random_generator
<boost::ecuyer1988
> uuid_gen3(ecuyer1988_gen
);
82 check_random_generator(uuid_gen3
);
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
);
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
;
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]))
98 BOOST_ERROR("basic_random_generator is not producing random uuids");
102 check_random_generator(bulkgen
);
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());
109 // The example code snippet in the documentation
112 #if !BOOST_LIB_C_CLOUDABI
113 // dump 10 of them to cout for observation
114 for (size_t i
= 0; i
< 10; ++i
)
116 std::cout
<< uuid_gen1() << std::endl
;
120 return boost::report_errors();