]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/value_ref.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / json / test / value_ref.cpp
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/json
8 //
9
10 // Test that header file is self-contained.
11 #include <boost/json/value_ref.hpp>
12
13 #include <boost/json/value.hpp>
14 #include <boost/json/serialize.hpp>
15
16 #include "test_suite.hpp"
17
18 BOOST_JSON_NS_BEGIN
19
20 //----------------------------------------------------------
21
22 // From the javadoc
23
24 class my_type
25 {
26 json::value jv_;
27
28 public:
29 my_type( std::initializer_list< json::value_ref > init )
30 : jv_(init)
31 {
32 }
33 };
34
35 //----------------------------------------------------------
36
37 class value_ref_test
38 {
39 public:
40 using init_list =
41 std::initializer_list<value_ref>;
42
43 void
44 testCtors()
45 {
46 // scalars
47 (void)value_ref((signed char)1);
48 (void)value_ref((short)1);
49 (void)value_ref((int)1);
50 (void)value_ref((long)1);
51 (void)value_ref((long long)1);
52 (void)value_ref((unsigned char)1);
53 (void)value_ref((unsigned short)1);
54 (void)value_ref((unsigned int)1);
55 (void)value_ref((unsigned long)1);
56 (void)value_ref((unsigned long long)1);
57 (void)value_ref((float)1.);
58 (void)value_ref((double)1.);
59 (void)value_ref(true);
60 (void)value_ref(nullptr);
61
62 // string_view, char const*
63 (void)(value_ref)string_view("test");
64 (void)(value_ref)("test");
65
66 // init-list
67 value_ref({1,2,3,4,5});
68
69 // value
70 (value_ref)value();
71 {
72 value jv;
73 (value_ref)jv;
74 }
75 {
76 value const jv;
77 (value_ref)jv;
78 }
79
80 // object
81 (value_ref)object();
82 {
83 object o;
84 (value_ref)o;
85 }
86 {
87 object const o{};
88 (value_ref)o;
89 }
90
91 // array
92 (value_ref)object();
93 {
94 array a;
95 (value_ref)a;
96 }
97 {
98 array const a{};
99 (value_ref)a;
100 }
101
102 // string
103 (value_ref)string();
104 {
105 string s;
106 (value_ref)s;
107 }
108 {
109 string const s{};
110 (value_ref)s;
111 }
112
113 // construct from a const rvalue
114 {
115 const value val;
116 (void)value_ref(std::move(val));
117 }
118 }
119
120 void
121 testInitList()
122 {
123 // string_view, char const*
124 (void)init_list{string_view("test")};
125 (void)init_list{"test"};
126
127 // scalars
128 (void)init_list{(short)1};
129 (void)init_list{(int)1};
130 (void)init_list{(long)1};
131 (void)init_list{(long long)1};
132 (void)init_list{(unsigned short)1};
133 (void)init_list{(unsigned int)1};
134 (void)init_list{(unsigned long)1};
135 (void)init_list{(unsigned long long)1};
136 (void)init_list{(float)1.};
137 (void)init_list{(double)1.};
138 (void)init_list{true};
139 (void)init_list{nullptr};
140
141 // bool
142 {
143 bool b = true;
144 auto const cb = b;
145 BOOST_TEST(value_ref::make_value(init_list{b}, {}).at(0).is_bool());
146 BOOST_TEST(value_ref::make_value(init_list{cb}, {}).at(0).is_bool());
147 }
148
149 // value
150 (void)init_list{value()};
151 {
152 value jv;
153 (void)init_list{jv};
154 }
155 {
156 value const jv{};
157 (void)init_list{jv};
158 }
159
160 // object
161 (void)init_list{object()};
162 {
163 object o;
164 (void)init_list{o};
165 }
166 {
167 object const o{};
168 (void)init_list{o};
169 }
170
171 // array
172 (void)init_list{object()};
173 {
174 array a;
175 (void)init_list{a};
176 }
177 {
178 array const a{};
179 (void)init_list{a};
180 }
181
182 // string
183 (void)init_list{string()};
184 {
185 string s;
186 (void)init_list{s};
187 }
188 {
189 string const s{};
190 (void)init_list{s};
191 }
192
193 // init_list
194 (void)init_list{{1,2,3,4,5}};
195 (void)init_list{{{1,2},{3,4,5}}};
196 (void)init_list{{1,2,{3,{4,5}}}};
197
198 {
199 init_list init =
200 { { "key", true } };
201 (void)init;
202 }
203 }
204
205 void
206 make_value(
207 init_list init,
208 string_view s)
209 {
210 auto const jv =
211 value_ref(init).make_value({});
212 auto const js = serialize(jv);
213 BOOST_TEST(js == s);
214 }
215
216 void
217 testMakeValue()
218 {
219 // scalars
220 make_value({(short)-1}, "[-1]");
221 make_value({(int)-2}, "[-2]");
222 make_value({(long)-3}, "[-3]");
223 make_value({(long long)-4}, "[-4]");
224 make_value({(unsigned short)1}, "[1]");
225 make_value({(unsigned int)2}, "[2]");
226 make_value({(unsigned long)3}, "[3]");
227 make_value({(unsigned long long)4}, "[4]");
228 //make_value({(float)1.}, "[1]");
229 //make_value({(double)1.}, "[1]");
230 make_value({true}, "[true]");
231 make_value({nullptr}, "[null]");
232
233 // string_view
234 make_value({string_view("5")}, "[\"5\"]");
235 make_value({"6"}, "[\"6\"]");
236
237 // value
238 {
239 make_value({value(1)}, "[1]");
240 value const v(1);
241 make_value({v}, "[1]");
242 make_value({value(2)}, "[2]");
243 }
244
245 // object
246 {
247 make_value({object({{"k1",1},{"k2",2}})}, "[{\"k1\":1,\"k2\":2}]");
248 object const obj({{"k1",1},{"k2",2}});
249 make_value({obj}, "[{\"k1\":1,\"k2\":2}]");
250 }
251
252 // array
253 {
254 make_value({array({1,2,3})}, "[[1,2,3]]");
255 array const arr({1,2,3});
256 make_value({arr}, "[[1,2,3]]");
257 }
258
259 // string
260 {
261 make_value({string("test")}, "[\"test\"]");
262 string const str("test");
263 make_value({str}, "[\"test\"]");
264 }
265
266 // init list with size != 2
267 {
268 make_value({{1,2,3}}, "[[1,2,3]]");
269 }
270
271 // `string` as key
272 {
273 make_value({
274 {string("k1"), 1},
275 {string("k2"), 2}},
276 "{\"k1\":1,\"k2\":2}");
277 }
278
279 // object/array conversion
280
281 make_value(
282 {1,2,{3,{4,5}}},
283 "[1,2,[3,[4,5]]]" );
284
285 make_value(
286 {{"k1",1}},
287 "{\"k1\":1}");
288
289 make_value(
290 {{"k1",1}, {"k2",2}},
291 "{\"k1\":1,\"k2\":2}");
292
293 make_value(
294 {{"k1",1}, {"k2",{{"k3",3}, {"k4",4}}}},
295 "{\"k1\":1,\"k2\":{\"k3\":3,\"k4\":4}}");
296
297 make_value({value(1)}, "[1]");
298 make_value({array({1,2,3,4})}, "[[1,2,3,4]]");
299 }
300
301 using kv_init_list =
302 std::initializer_list<
303 std::pair<string_view, value_ref>>;
304
305 void
306 make_object(
307 kv_init_list init,
308 string_view s)
309 {
310 auto const jv = value(object(init));
311 auto const js = serialize(jv);
312 BOOST_TEST(js == s);
313 }
314
315 void
316 testObjects()
317 {
318 make_object({
319 {"k1",1}
320 }, "{\"k1\":1}");
321 make_object({
322 {"k1",1}, {"k2",2}
323 }, "{\"k1\":1,\"k2\":2}");
324 make_object({
325 {"k1", {1,2}}, {"k2", {1,2,3}}
326 }, "{\"k1\":[1,2],\"k2\":[1,2,3]}");
327 make_object({
328 {"k1", {{"k2",2}, {"k3",3}}}
329 }, "{\"k1\":{\"k2\":2,\"k3\":3}}");
330 }
331
332 void
333 testMoveFrom()
334 {
335 {
336 string a = "abcdefghijklmnopqrstuvwxyz";
337 BOOST_TEST(!a.empty());
338 array b{std::move(a), string()};
339 BOOST_TEST(a.empty());
340 }
341 {
342 string a = "abcdefghijklmnopqrstuvwxyz";
343 BOOST_TEST(!a.empty());
344 array b{a, string()};
345 BOOST_TEST(!a.empty());
346 }
347 {
348 string const a = "abcdefghijklmnopqrstuvwxyz";
349 BOOST_TEST(!a.empty());
350 array b{a, string()};
351 BOOST_TEST(!a.empty());
352 }
353 {
354 array a{value()};
355 BOOST_TEST(a.begin() != a.end());
356 array b{std::move(a), array()};
357 BOOST_TEST(a.begin() == a.end());
358 }
359 {
360 array a{value()};
361 BOOST_TEST(a.begin() != a.end());
362 array b{a, array()};
363 BOOST_TEST(a.begin() != a.end());
364 }
365 {
366 const array a{value()};
367 BOOST_TEST(a.begin() != a.end());
368 array b{a, array()};
369 BOOST_TEST(a.begin() != a.end());
370 }
371 {
372 object a{{"a", 1}, {"b", 2}};
373 BOOST_TEST(a.capacity() > 0);
374 array b{std::move(a), object()};
375 BOOST_TEST(a.capacity() == 0);
376 }
377 {
378 object a{{"a", 1}, {"b", 2}};
379 BOOST_TEST(a.capacity() > 0);
380 array b{a, object()};
381 BOOST_TEST(a.capacity() > 0);
382 }
383 {
384 const object a{{"a", 1}, {"b", 2}};
385 BOOST_TEST(a.capacity() > 0);
386 array b{a, object()};
387 BOOST_TEST(a.capacity() > 0);
388 }
389 }
390
391 void
392 run()
393 {
394 testCtors();
395 testInitList();
396 testMakeValue();
397 testObjects();
398 testMoveFrom();
399 }
400 };
401
402 TEST_SUITE(value_ref_test, "boost.json.value_ref");
403
404 BOOST_JSON_NS_END