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>
17 #include <boost/random/random_device.hpp>
19 template <typename RandomUuidGenerator
>
20 void check_random_generator(RandomUuidGenerator
& uuid_gen
)
22 boost::uuids::uuid u1
= uuid_gen();
23 boost::uuids::uuid u2
= uuid_gen();
25 BOOST_TEST_NE(u1
, u2
);
28 BOOST_TEST_EQ(u1
.variant(), boost::uuids::uuid::variant_rfc_4122
);
31 BOOST_TEST_EQ(u1
.version(), boost::uuids::uuid::version_random_number_based
);
34 int main(int, char*[])
36 using namespace boost::uuids
;
38 // default random number generator
39 random_generator uuid_gen1
;
40 check_random_generator(uuid_gen1
);
42 // specific random number generator
43 basic_random_generator
<boost::rand48
> uuid_gen2
;
44 check_random_generator(uuid_gen2
);
47 boost::ecuyer1988 ecuyer1988_gen
;
48 basic_random_generator
<boost::ecuyer1988
> uuid_gen3(ecuyer1988_gen
);
49 check_random_generator(uuid_gen3
);
52 boost::lagged_fibonacci607 lagged_fibonacci607_gen
;
53 basic_random_generator
<boost::lagged_fibonacci607
> uuid_gen4(&lagged_fibonacci607_gen
);
54 check_random_generator(uuid_gen4
);
57 basic_random_generator
<boost::random::random_device
> uuid_gen5
;
58 check_random_generator(uuid_gen5
);
60 // there was a bug in basic_random_generator where it did not
61 // produce very random numbers. This checks for that bug.
62 uuid u
= random_generator()();
63 if ( (u
.data
[4] == u
.data
[12]) &&
64 (u
.data
[5] == u
.data
[9] && u
.data
[5] == u
.data
[13]) &&
65 (u
.data
[7] == u
.data
[11] && u
.data
[7] == u
.data
[15]) &&
66 (u
.data
[10] == u
.data
[14]) )
68 BOOST_ERROR("basic_random_generator is not producing random uuids");
71 return boost::report_errors();