• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  (C) Copyright Andy Tompkins 2009. 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 // https://www.boost.org/LICENSE_1_0.txt)
9 
10 //  libs/uuid/test/test_generators.cpp  -------------------------------//
11 
12 #include <boost/uuid/uuid.hpp>
13 #include <boost/uuid/uuid_generators.hpp>
14 #include <boost/uuid/uuid_io.hpp>
15 
16 #include <boost/test/included/test_exec_monitor.hpp>
17 #include <boost/test/test_tools.hpp>
18 
19 #include <boost/random.hpp>
20 
21 template <typename RandomUuidGenerator>
check_random_generator(RandomUuidGenerator & uuid_gen)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_CHECK_NE(u1, u2);
28 
29     // check variant
30     BOOST_CHECK_EQUAL(u1.variant(), boost::uuids::uuid::variant_rfc_4122);
31 //    BOOST_CHECK_EQUAL( *(u1.begin()+8) & 0xC0, 0x80);
32     // version
33     BOOST_CHECK_EQUAL( *(u1.begin()+6) & 0xF0, 0x40);
34 }
35 
test_main(int,char * [])36 int test_main(int, char*[])
37 {
38     using namespace boost::uuids;
39     using boost::test_tools::output_test_stream;
40 
41     { // test nil generator
42         uuid u1 = nil_generator()();
43         uuid u2 = {{0}};
44         BOOST_CHECK_EQUAL(u1, u2);
45 
46         uuid u3 = nil_uuid();
47         BOOST_CHECK_EQUAL(u3, u2);
48     }
49 
50     { // test string_generator
51         string_generator gen;
52         uuid u = gen("00000000-0000-0000-0000-000000000000");
53         BOOST_CHECK_EQUAL(u, nil_uuid());
54         BOOST_CHECK_EQUAL(u.is_nil(), true);
55 
56         const uuid u_increasing = {{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }};
57         const uuid u_decreasing = {{ 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }};
58 
59         u = gen("0123456789abcdef0123456789ABCDEF");
60         BOOST_CHECK_EQUAL(u, u_increasing);
61 
62         u = gen("{0123456789abcdef0123456789ABCDEF}");
63         BOOST_CHECK_EQUAL(u, u_increasing);
64 
65         u = gen("{01234567-89AB-CDEF-0123-456789abcdef}");
66         BOOST_CHECK_EQUAL(u, u_increasing);
67 
68         u = gen("01234567-89AB-CDEF-0123-456789abcdef");
69         BOOST_CHECK_EQUAL(u, u_increasing);
70 
71         u = gen(std::string("fedcba98-7654-3210-fedc-ba9876543210"));
72         BOOST_CHECK_EQUAL(u, u_decreasing);
73 
74 #ifndef BOOST_NO_STD_WSTRING
75         u = gen(L"fedcba98-7654-3210-fedc-ba9876543210");
76         BOOST_CHECK_EQUAL(u, u_decreasing);
77 
78         u = gen(std::wstring(L"01234567-89ab-cdef-0123-456789abcdef"));
79         BOOST_CHECK_EQUAL(u, u_increasing);
80 #endif //BOOST_NO_STD_WSTRING
81     }
82 
83     { // test name_generator
84         uuid dns_namespace_uuid = {{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}};
85         uuid correct = {{0x21, 0xf7, 0xf8, 0xde, 0x80, 0x51, 0x5b, 0x89, 0x86, 0x80, 0x01, 0x95, 0xef, 0x79, 0x8b, 0x6a}};
86         uuid wcorrect = {{0xb9, 0x50, 0x66, 0x13, 0x2c, 0x04, 0x51, 0x2d, 0xb8, 0xfe, 0xbf, 0x8d, 0x0b, 0xa1, 0xb2, 0x71}};
87 
88         name_generator gen(dns_namespace_uuid);
89 
90         uuid u = gen("www.widgets.com");
91         BOOST_CHECK_EQUAL(u, correct);
92         BOOST_CHECK_EQUAL(u.variant(), boost::uuids::uuid::variant_rfc_4122);
93 
94         u = gen(L"www.widgets.com");
95         BOOST_CHECK_EQUAL(u, wcorrect);
96         BOOST_CHECK_EQUAL(u.variant(), boost::uuids::uuid::variant_rfc_4122);
97 
98         u = gen(std::string("www.widgets.com"));
99         BOOST_CHECK_EQUAL(u, correct);
100         BOOST_CHECK_EQUAL(u.variant(), boost::uuids::uuid::variant_rfc_4122);
101 
102         u = gen(std::wstring(L"www.widgets.com"));
103         BOOST_CHECK_EQUAL(u, wcorrect);
104         BOOST_CHECK_EQUAL(u.variant(), boost::uuids::uuid::variant_rfc_4122);
105 
106         char name[] = "www.widgets.com";
107         u = gen(name, 15);
108         BOOST_CHECK_EQUAL(u, correct);
109         BOOST_CHECK_EQUAL(u.variant(), boost::uuids::uuid::variant_rfc_4122);
110     }
111 
112     { // test random_generator
113         // default random number generator
114         random_generator uuid_gen1;
115         check_random_generator(uuid_gen1);
116 
117         // specific random number generator
118         basic_random_generator<boost::rand48> uuid_gen2;
119         check_random_generator(uuid_gen2);
120 
121         // pass by reference
122         boost::ecuyer1988 ecuyer1988_gen;
123         basic_random_generator<boost::ecuyer1988> uuid_gen3(ecuyer1988_gen);
124         check_random_generator(uuid_gen3);
125 
126         // pass by pointer
127         boost::lagged_fibonacci607 lagged_fibonacci607_gen;
128         basic_random_generator<boost::lagged_fibonacci607> uuid_gen4(&lagged_fibonacci607_gen);
129         check_random_generator(uuid_gen4);
130 
131         // random device
132         //basic_random_generator<boost::random_device> uuid_gen5;
133         //check_random_generator(uuid_gen5);
134     }
135 
136     return 0;
137 }
138