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 | |
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 |
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 | |
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 |
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 | |
78 | int main(int, char*[]) |
79 | { |
80 | const uuid u1 = {.data: {0}}; |
81 | const uuid u2 = {.data: {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}}; |
82 | const uuid u3 = {.data: {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef}}; |
83 | |
84 | uuroundtrip(in: u1, expect: "00000000-0000-0000-0000-000000000000" ); |
85 | uuroundtrip(in: u2, expect: "00010203-0405-0607-0809-0a0b0c0d0e0f" ); |
86 | uuroundtrip(in: u3, expect: "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(in: u1, expect: L"00000000-0000-0000-0000-000000000000" ); |
102 | uuroundtrip(in: u2, expect: L"00010203-0405-0607-0809-0a0b0c0d0e0f" ); |
103 | uuroundtrip(in: u3, expect: 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(in: std::string(raw, 36)); |
113 | uufail(in: "01234567-89ab-cdef-0123456789abcdef" ); |
114 | uufail(in: "01234567-89ab-cdef0123-456789abcdef" ); |
115 | uufail(in: "01234567-89abcdef-0123-456789abcdef" ); |
116 | uufail(in: "0123456789ab-cdef-0123-456789abcdef" ); |
117 | uufail(in: "{12345678-90ab-cdef-1234-567890abcdef}" ); // stream is not as flexible as string |
118 | uufail(in: "G1234567-89AB-CDEF-0123-456789abcdef" ); |
119 | uufail(in: "01234567-89AB-CDEF-0123-456789abcdeg" ); |
120 | |
121 | #ifndef BOOST_NO_STD_WSTRING |
122 | uufail(in: L"01234567-89ab-cdef-" ); |
123 | uufail(in: L"01234567-89ab-cdef-0123456789abcdef" ); |
124 | uufail(in: L"01234567-89ab-cdef0123-456789abcdef" ); |
125 | uufail(in: L"01234567-89abcdef-0123-456789abcdef" ); |
126 | uufail(in: L"0123456789ab-cdef-0123-456789abcdef" ); |
127 | uufail(in: L"{12345678-90ab-cdef-1234-567890abcdef}" ); // stream is not as flexible as string |
128 | uufail(in: L"G1234567-89AB-CDEF-0123-456789abcdef" ); |
129 | uufail(in: 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 | { |
151 | char buf[36] = {}; |
152 | BOOST_TEST(to_chars(u3, &buf[0], &buf[36])); |
153 | BOOST_TEST(std::string(buf, 36) == std::string("12345678-90ab-cdef-1234-567890abcdef" )); |
154 | } |
155 | { |
156 | char buf[37] = {}; // will be null-terminated |
157 | BOOST_TEST(to_chars(u3, &buf[0], &buf[37])); |
158 | BOOST_TEST(std::string(buf) == std::string("12345678-90ab-cdef-1234-567890abcdef" )); |
159 | } |
160 | { |
161 | char buf[35] = {}; |
162 | BOOST_TEST(!to_chars(u3, &buf[0], &buf[35])); |
163 | } |
164 | { |
165 | char buf[36] = {}; |
166 | char* ret = to_chars(u: u3, out: &buf[0]); |
167 | BOOST_TEST(ret == &buf[36]); |
168 | BOOST_TEST(std::string(buf, 36) == std::string("12345678-90ab-cdef-1234-567890abcdef" )); |
169 | } |
170 | { |
171 | char buf[36] = {}; |
172 | char* ret = to_chars(u: u3, out: buf); // decay |
173 | BOOST_TEST(ret == &buf[36]); |
174 | BOOST_TEST(std::string(buf, 36) == std::string("12345678-90ab-cdef-1234-567890abcdef" )); |
175 | } |
176 | { |
177 | char buf[37] = {}; // will be null-terminated |
178 | char* ret = to_chars(u: u3, out: buf); |
179 | BOOST_TEST(ret == &buf[36]); |
180 | BOOST_TEST(std::string(buf) == std::string("12345678-90ab-cdef-1234-567890abcdef" )); |
181 | } |
182 | |
183 | BOOST_TEST(to_string(u1) == std::string("00000000-0000-0000-0000-000000000000" )); |
184 | BOOST_TEST(to_string(u3) == std::string("12345678-90ab-cdef-1234-567890abcdef" )); |
185 | |
186 | #ifndef BOOST_NO_STD_WSTRING |
187 | BOOST_TEST(to_wstring(u1) == std::wstring(L"00000000-0000-0000-0000-000000000000" )); |
188 | BOOST_TEST(to_wstring(u3) == std::wstring(L"12345678-90ab-cdef-1234-567890abcdef" )); |
189 | #endif |
190 | |
191 | return boost::report_errors(); |
192 | } |
193 | |
194 | |