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_io.cpp -------------------------------//
11
12 #include <boost/uuid/uuid.hpp>
13 #include <boost/uuid/uuid_io.hpp>
14 #include <boost/detail/lightweight_test.hpp>
15 #include <boost/predef/library/c/cloudabi.h>
16 #include <string>
17 #include <sstream>
18 #include <iomanip>
19
20 #if !BOOST_LIB_C_CLOUDABI
21 #include <boost/lexical_cast.hpp>
22 #endif
23
24 using namespace boost::uuids;
25
uufail(const std::string & in)26 void uufail(const std::string& in)
27 {
28 uuid out;
29 std::stringstream ss;
30 ss << in;
31 ss >> out;
32 BOOST_TEST_EQ(true, ss.fail());
33 }
34
35 #ifndef BOOST_NO_STD_WSTRING
uufail(const std::wstring & in)36 void uufail(const std::wstring& in)
37 {
38 uuid out;
39 std::wstringstream ss;
40 ss << in;
41 ss >> out;
42 BOOST_TEST_EQ(true, ss.fail());
43 }
44 #endif
45
uuroundtrip(const uuid & in,const std::string & expect)46 void uuroundtrip(const uuid& in, const std::string& expect)
47 {
48 std::stringstream ss;
49 ss << in;
50 BOOST_TEST_EQ(false, ss.fail());
51 BOOST_TEST_EQ(expect, ss.str());
52
53 uuid out;
54 std::stringstream ss2;
55 ss2 << expect;
56 ss2 >> out;
57 BOOST_TEST_EQ(false, ss2.fail());
58 BOOST_TEST_EQ(in, out);
59 }
60
61 #ifndef BOOST_NO_STD_WSTRING
uuroundtrip(const uuid & in,const std::wstring & expect)62 void uuroundtrip(const uuid& in, const std::wstring& expect)
63 {
64 std::wstringstream ss;
65 ss << in;
66 BOOST_TEST_EQ(false, ss.fail());
67 BOOST_TEST (expect == ss.str());
68
69 uuid out;
70 std::wstringstream ss2;
71 ss2 << expect;
72 ss2 >> out;
73 BOOST_TEST_EQ(false, ss2.fail());
74 BOOST_TEST (in == out);
75 }
76 #endif
77
main(int,char * [])78 int main(int, char*[])
79 {
80 const uuid u1 = {{0}};
81 const uuid u2 = {{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}};
82 const uuid u3 = {{0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef}};
83
84 uuroundtrip(u1, "00000000-0000-0000-0000-000000000000");
85 uuroundtrip(u2, "00010203-0405-0607-0809-0a0b0c0d0e0f");
86 uuroundtrip(u3, "12345678-90ab-cdef-1234-567890abcdef");
87
88 std::stringstream ss5;
89 ss5 << 'a' << std::right << std::setfill('*') << std::setw(40) << u1 << 'a';
90 BOOST_TEST_EQ(ss5.str(), "a****00000000-0000-0000-0000-000000000000a");
91
92 std::stringstream ss6;
93 ss6 << std::left << std::setfill('*') << std::setw(45) << u1;
94 BOOST_TEST_EQ(ss6.str(), "00000000-0000-0000-0000-000000000000*********");
95
96 std::stringstream ss7;
97 ss7 << std::left << std::setfill('*') << std::setw(45) << u2;
98 BOOST_TEST_EQ(ss7.str(), "00010203-0405-0607-0809-0a0b0c0d0e0f*********");
99
100 #ifndef BOOST_NO_STD_WSTRING
101 uuroundtrip(u1, L"00000000-0000-0000-0000-000000000000");
102 uuroundtrip(u2, L"00010203-0405-0607-0809-0a0b0c0d0e0f");
103 uuroundtrip(u3, L"12345678-90ab-cdef-1234-567890abcdef");
104 #endif
105
106 const char raw[36] = { '0', '1', '2', '3', '4', '5', '6', '7', '-',
107 '8', '9', 'a', 'b', '-',
108 'c', 'd', 'e', 'f', '-',
109 0 , '1', '2', '3', '-', // 0x00 character is intentional
110 '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
111
112 uufail(std::string(raw, 36));
113 uufail("01234567-89ab-cdef-0123456789abcdef");
114 uufail("01234567-89ab-cdef0123-456789abcdef");
115 uufail("01234567-89abcdef-0123-456789abcdef");
116 uufail("0123456789ab-cdef-0123-456789abcdef");
117 uufail("{12345678-90ab-cdef-1234-567890abcdef}"); // stream is not as flexible as string
118 uufail("G1234567-89AB-CDEF-0123-456789abcdef");
119 uufail("01234567-89AB-CDEF-0123-456789abcdeg");
120
121 #ifndef BOOST_NO_STD_WSTRING
122 uufail(L"01234567-89ab-cdef-");
123 uufail(L"01234567-89ab-cdef-0123456789abcdef");
124 uufail(L"01234567-89ab-cdef0123-456789abcdef");
125 uufail(L"01234567-89abcdef-0123-456789abcdef");
126 uufail(L"0123456789ab-cdef-0123-456789abcdef");
127 uufail(L"{12345678-90ab-cdef-1234-567890abcdef}"); // stream is not as flexible as string
128 uufail(L"G1234567-89AB-CDEF-0123-456789abcdef");
129 uufail(L"01234567-89AB-CDEF-0123-456789abcdeg");
130 #endif
131
132 #if !BOOST_LIB_C_CLOUDABI
133 // lexical_cast depends on sprintf which is not available in cloudlibc
134 BOOST_TEST(boost::lexical_cast<std::string>(u1) == std::string("00000000-0000-0000-0000-000000000000"));
135 BOOST_TEST(boost::lexical_cast<uuid>("00000000-0000-0000-0000-000000000000") == u1);
136
137 BOOST_TEST(boost::lexical_cast<std::string>(u3) == std::string("12345678-90ab-cdef-1234-567890abcdef"));
138 BOOST_TEST(boost::lexical_cast<uuid>("12345678-90ab-cdef-1234-567890abcdef") == u3);
139
140 #ifndef BOOST_NO_STD_WSTRING
141 BOOST_TEST(boost::lexical_cast<std::wstring>(u1) == std::wstring(L"00000000-0000-0000-0000-000000000000"));
142 BOOST_TEST(boost::lexical_cast<uuid>(L"00000000-0000-0000-0000-000000000000") == u1);
143
144 BOOST_TEST(boost::lexical_cast<std::wstring>(u3) == std::wstring(L"12345678-90ab-cdef-1234-567890abcdef"));
145 BOOST_TEST(boost::lexical_cast<uuid>(L"12345678-90ab-cdef-1234-567890abcdef") == u3);
146 #endif
147
148 #endif
149
150 BOOST_TEST(to_string(u1) == std::string("00000000-0000-0000-0000-000000000000"));
151 BOOST_TEST(to_string(u3) == std::string("12345678-90ab-cdef-1234-567890abcdef"));
152
153 #ifndef BOOST_NO_STD_WSTRING
154 BOOST_TEST(to_wstring(u1) == std::wstring(L"00000000-0000-0000-0000-000000000000"));
155 BOOST_TEST(to_wstring(u3) == std::wstring(L"12345678-90ab-cdef-1234-567890abcdef"));
156 #endif
157
158 return boost::report_errors();
159 }
160
161