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/uuid/uuid.hpp>
13 #include <boost/uuid/random_generator.hpp>
14 #include <boost/uuid/uuid_io.hpp>
15 #include <boost/detail/lightweight_test.hpp>
16 #include <boost/random.hpp>
18 template <typename RandomUuidGenerator
>
19 void check_random_generator(RandomUuidGenerator
& uuid_gen
)
21 boost::uuids::uuid u1
= uuid_gen();
22 boost::uuids::uuid u2
= uuid_gen();
24 BOOST_TEST_NE(u1
, u2
);
27 BOOST_TEST_EQ(u1
.variant(), boost::uuids::uuid::variant_rfc_4122
);
30 BOOST_TEST_EQ(u1
.version(), boost::uuids::uuid::version_random_number_based
);
33 int main(int, char*[])
35 using namespace boost::uuids
;
37 // default random number generator
38 random_generator uuid_gen1
;
39 check_random_generator(uuid_gen1
);
41 // specific random number generator
42 basic_random_generator
<boost::rand48
> uuid_gen2
;
43 check_random_generator(uuid_gen2
);
46 boost::ecuyer1988 ecuyer1988_gen
;
47 basic_random_generator
<boost::ecuyer1988
> uuid_gen3(ecuyer1988_gen
);
48 check_random_generator(uuid_gen3
);
51 boost::lagged_fibonacci607 lagged_fibonacci607_gen
;
52 basic_random_generator
<boost::lagged_fibonacci607
> uuid_gen4(&lagged_fibonacci607_gen
);
53 check_random_generator(uuid_gen4
);
56 //basic_random_generator<boost::random_device> uuid_gen5;
57 //check_random_generator(uuid_gen5);
59 // there was a bug in basic_random_generator where it did not
60 // produce very random numbers. This checks for that bug.
61 uuid u
= random_generator()();
62 if ( (u
.data
[4] == u
.data
[12]) &&
63 (u
.data
[5] == u
.data
[9] && u
.data
[5] == u
.data
[13]) &&
64 (u
.data
[7] == u
.data
[11] && u
.data
[7] == u
.data
[15]) &&
65 (u
.data
[10] == u
.data
[14]) )
67 BOOST_ERROR("basic_random_generator is not producing random uuids");
70 return boost::report_errors();