• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // https://www.boost.org/LICENSE_1_0.txt)
9 
10 //  libs/uuid/test/test_string_generator.cpp  -------------------------------//
11 
12 #include <boost/uuid/uuid.hpp>
13 #include <boost/uuid/uuid_io.hpp>
14 #include <boost/uuid/string_generator.hpp>
15 #include <boost/detail/lightweight_test.hpp>
16 #include <boost/config.hpp>
17 #include <stdexcept>
18 #include <string>
19 
main(int,char * [])20 int main(int, char*[])
21 {
22     using namespace boost::uuids;
23 
24     uuid nil_uuid = {{0}};
25     BOOST_TEST_EQ(nil_uuid.is_nil(), true);
26 
27     string_generator gen;
28     uuid u = gen("00000000-0000-0000-0000-000000000000");
29     BOOST_TEST_EQ(u, nil_uuid);
30     BOOST_TEST_EQ(u.is_nil(), true);
31 
32     const uuid u_increasing = {{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }};
33     const uuid u_decreasing = {{ 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }};
34 
35     u = gen("0123456789abcdef0123456789ABCDEF");
36     BOOST_TEST_EQ(memcmp(&u, &u_increasing, sizeof(uuid)), 0);
37     BOOST_TEST_EQ(u, u_increasing);
38 
39     u = gen("{0123456789abcdef0123456789ABCDEF}");
40     BOOST_TEST_EQ(u, u_increasing);
41 
42     u = gen("{01234567-89AB-CDEF-0123-456789abcdef}");
43     BOOST_TEST_EQ(u, u_increasing);
44 
45     u = gen("01234567-89AB-CDEF-0123-456789abcdef");
46     BOOST_TEST_EQ(u, u_increasing);
47 
48     u = gen(std::string("fedcba98-7654-3210-fedc-ba9876543210"));
49     BOOST_TEST_EQ(u, u_decreasing);
50 
51 #ifndef BOOST_NO_STD_WSTRING
52     u = gen(L"fedcba98-7654-3210-fedc-ba9876543210");
53     BOOST_TEST_EQ(u, u_decreasing);
54 
55     u = gen(std::wstring(L"01234567-89ab-cdef-0123-456789abcdef"));
56     BOOST_TEST_EQ(u, u_increasing);
57 
58     u = gen(std::wstring(L"{01234567-89ab-cdef-0123-456789abcdef}"));
59     BOOST_TEST_EQ(u, u_increasing);
60 #endif //BOOST_NO_STD_WSTRING
61 
62     const char raw[36] = { '0', '1', '2', '3', '4', '5', '6', '7', '-',
63                            '8', '9', 'a', 'b', '-',
64                            'c', 'd', 'e', 'f', '-',
65                             0 , '1', '2', '3', '-',  // 0x00 character is intentional
66                            '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
67     BOOST_TEST_THROWS(gen(std::string(raw, 36)), std::runtime_error);
68 
69     BOOST_TEST_THROWS(gen("01234567-89ab-cdef-0123456789abcdef"), std::runtime_error);
70     BOOST_TEST_THROWS(gen("01234567-89ab-cdef0123-456789abcdef"), std::runtime_error);
71     BOOST_TEST_THROWS(gen("01234567-89abcdef-0123-456789abcdef"), std::runtime_error);
72     BOOST_TEST_THROWS(gen("0123456789ab-cdef-0123-456789abcdef"), std::runtime_error);
73 
74     BOOST_TEST_THROWS(gen("{01234567-89AB-CDEF-0123-456789abcdef)"), std::runtime_error);
75     BOOST_TEST_THROWS(gen("{01234567-89AB-CDEF-0123-456789abcdef"), std::runtime_error);
76     BOOST_TEST_THROWS(gen("01234567-89AB-CDEF-0123-456789abcdef}"), std::runtime_error);
77 #ifndef BOOST_NO_STD_WSTRING
78     BOOST_TEST_THROWS(gen(std::wstring(L"{01234567-89AB-CDEF-0123-456789abcdef)")), std::runtime_error);
79     BOOST_TEST_THROWS(gen(std::wstring(L"{01234567-89AB-CDEF-0123-456789abcdef")), std::runtime_error);
80     BOOST_TEST_THROWS(gen(std::wstring(L"01234567-89AB-CDEF-0123-456789abcdef}")), std::runtime_error);
81     BOOST_TEST_THROWS(gen(std::wstring(L"G1234567-89AB-CDEF-0123-456789abcdef}")), std::runtime_error);
82 #endif //BOOST_NO_STD_WSTRING
83 
84     BOOST_TEST_THROWS(gen("G1234567-89AB-CDEF-0123-456789abcdef"), std::runtime_error);
85     BOOST_TEST_THROWS(gen("Have a great big roast-beef sandwich!"), std::runtime_error);
86 
87     BOOST_TEST_THROWS(gen("83f8638b-8dca-4152-zzzz-2ca8b33039b4"), std::runtime_error);
88 
89     return boost::report_errors();
90 }
91