• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     __ _____ _____ _____
3  __|  |   __|     |   | |  JSON for Modern C++ (test suite)
4 |  |  |__   |  |  | | | |  version 3.7.3
5 |_____|_____|_____|_|___|  https://github.com/nlohmann/json
6 
7 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
8 SPDX-License-Identifier: MIT
9 Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
10 
11 Permission is hereby  granted, free of charge, to any  person obtaining a copy
12 of this software and associated  documentation files (the "Software"), to deal
13 in the Software  without restriction, including without  limitation the rights
14 to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
15 copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
16 furnished to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20 
21 THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
22 IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
23 FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
24 AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
25 LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28 */
29 
30 #include "doctest_compatibility.h"
31 
32 #include <nlohmann/json.hpp>
33 using nlohmann::json;
34 
35 TEST_CASE("pointer access")
36 {
37     // create a JSON value with different types
38     json json_types =
39     {
40         {"boolean", true},
41         {
42             "number", {
43                 {"integer", 42},
44                 {"unsigned", 42u},
45                 {"floating-point", 17.23}
46             }
47         },
48         {"string", "Hello, world!"},
49         {"array", {1, 2, 3, 4, 5}},
50         {"null", nullptr}
51     };
52 
53     SECTION("pointer access to object_t")
54     {
55         using test_type = json::object_t;
56         json value = {{"one", 1}, {"two", 2}};
57 
58         // check if pointers are returned correctly
59         test_type* p1 = value.get_ptr<test_type*>();
60         CHECK(p1 == value.get_ptr<test_type*>());
61         CHECK(*p1 == value.get<test_type>());
62 
63         const test_type* p2 = value.get_ptr<const test_type*>();
64         CHECK(p1 == value.get_ptr<const test_type*>());
65         CHECK(*p2 == value.get<test_type>());
66 
67         const test_type* const p3 = value.get_ptr<const test_type* const>();
68         CHECK(p1 == value.get_ptr<const test_type* const>());
69         CHECK(*p3 == value.get<test_type>());
70 
71         // check if null pointers are returned correctly
72         CHECK(value.get_ptr<json::object_t*>() != nullptr);
73         CHECK(value.get_ptr<json::array_t*>() == nullptr);
74         CHECK(value.get_ptr<json::string_t*>() == nullptr);
75         CHECK(value.get_ptr<json::boolean_t*>() == nullptr);
76         CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
77         CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
78         CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
79     }
80 
81     SECTION("pointer access to const object_t")
82     {
83         using test_type = const json::object_t;
84         const json value = {{"one", 1}, {"two", 2}};
85 
86         // check if pointers are returned correctly
87         test_type* p1 = value.get_ptr<test_type*>();
88         CHECK(p1 == value.get_ptr<test_type*>());
89         CHECK(*p1 == value.get<test_type>());
90 
91         const test_type* p2 = value.get_ptr<const test_type*>();
92         CHECK(p1 == value.get_ptr<const test_type*>());
93         CHECK(*p2 == value.get<test_type>());
94 
95         const test_type* const p3 = value.get_ptr<const test_type* const>();
96         CHECK(p1 == value.get_ptr<const test_type* const>());
97         CHECK(*p3 == value.get<test_type>());
98 
99         // check if null pointers are returned correctly
100         CHECK(value.get_ptr<const json::object_t*>() != nullptr);
101         CHECK(value.get_ptr<const json::array_t*>() == nullptr);
102         CHECK(value.get_ptr<const json::string_t*>() == nullptr);
103         CHECK(value.get_ptr<const json::boolean_t*>() == nullptr);
104         CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
105         CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
106         CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
107     }
108 
109     SECTION("pointer access to array_t")
110     {
111         using test_type = json::array_t;
112         json value = {1, 2, 3, 4};
113 
114         // check if pointers are returned correctly
115         test_type* p1 = value.get_ptr<test_type*>();
116         CHECK(p1 == value.get_ptr<test_type*>());
117         CHECK(*p1 == value.get<test_type>());
118 
119         const test_type* p2 = value.get_ptr<const test_type*>();
120         CHECK(p1 == value.get_ptr<const test_type*>());
121         CHECK(*p2 == value.get<test_type>());
122 
123         const test_type* const p3 = value.get_ptr<const test_type* const>();
124         CHECK(p1 == value.get_ptr<const test_type* const>());
125         CHECK(*p3 == value.get<test_type>());
126 
127         // check if null pointers are returned correctly
128         CHECK(value.get_ptr<json::object_t*>() == nullptr);
129         CHECK(value.get_ptr<json::array_t*>() != nullptr);
130         CHECK(value.get_ptr<json::string_t*>() == nullptr);
131         CHECK(value.get_ptr<json::boolean_t*>() == nullptr);
132         CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
133         CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
134         CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
135     }
136 
137     SECTION("pointer access to const array_t")
138     {
139         using test_type = const json::array_t;
140         const json value = {1, 2, 3, 4};
141 
142         // check if pointers are returned correctly
143         test_type* p1 = value.get_ptr<test_type*>();
144         CHECK(p1 == value.get_ptr<test_type*>());
145         CHECK(*p1 == value.get<test_type>());
146 
147         const test_type* p2 = value.get_ptr<const test_type*>();
148         CHECK(p1 == value.get_ptr<const test_type*>());
149         CHECK(*p2 == value.get<test_type>());
150 
151         const test_type* const p3 = value.get_ptr<const test_type* const>();
152         CHECK(p1 == value.get_ptr<const test_type* const>());
153         CHECK(*p3 == value.get<test_type>());
154 
155         // check if null pointers are returned correctly
156         CHECK(value.get_ptr<const json::object_t*>() == nullptr);
157         CHECK(value.get_ptr<const json::array_t*>() != nullptr);
158         CHECK(value.get_ptr<const json::string_t*>() == nullptr);
159         CHECK(value.get_ptr<const json::boolean_t*>() == nullptr);
160         CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
161         CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
162         CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
163     }
164 
165     SECTION("pointer access to string_t")
166     {
167         using test_type = json::string_t;
168         json value = "hello";
169 
170         // check if pointers are returned correctly
171         test_type* p1 = value.get_ptr<test_type*>();
172         CHECK(p1 == value.get_ptr<test_type*>());
173         CHECK(*p1 == value.get<test_type>());
174 
175         const test_type* p2 = value.get_ptr<const test_type*>();
176         CHECK(p1 == value.get_ptr<const test_type*>());
177         CHECK(*p2 == value.get<test_type>());
178 
179         const test_type* const p3 = value.get_ptr<const test_type* const>();
180         CHECK(p1 == value.get_ptr<const test_type* const>());
181         CHECK(*p3 == value.get<test_type>());
182 
183         // check if null pointers are returned correctly
184         CHECK(value.get_ptr<json::object_t*>() == nullptr);
185         CHECK(value.get_ptr<json::array_t*>() == nullptr);
186         CHECK(value.get_ptr<json::string_t*>() != nullptr);
187         CHECK(value.get_ptr<json::boolean_t*>() == nullptr);
188         CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
189         CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
190         CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
191     }
192 
193     SECTION("pointer access to const string_t")
194     {
195         using test_type = const json::string_t;
196         const json value = "hello";
197 
198         // check if pointers are returned correctly
199         test_type* p1 = value.get_ptr<test_type*>();
200         CHECK(p1 == value.get_ptr<test_type*>());
201         CHECK(*p1 == value.get<test_type>());
202 
203         const test_type* p2 = value.get_ptr<const test_type*>();
204         CHECK(p1 == value.get_ptr<const test_type*>());
205         CHECK(*p2 == value.get<test_type>());
206 
207         const test_type* const p3 = value.get_ptr<const test_type* const>();
208         CHECK(p1 == value.get_ptr<const test_type* const>());
209         CHECK(*p3 == value.get<test_type>());
210 
211         // check if null pointers are returned correctly
212         CHECK(value.get_ptr<const json::object_t*>() == nullptr);
213         CHECK(value.get_ptr<const json::array_t*>() == nullptr);
214         CHECK(value.get_ptr<const json::string_t*>() != nullptr);
215         CHECK(value.get_ptr<const json::boolean_t*>() == nullptr);
216         CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
217         CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
218         CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
219     }
220 
221     SECTION("pointer access to boolean_t")
222     {
223         using test_type = json::boolean_t;
224         json value = false;
225 
226         // check if pointers are returned correctly
227         test_type* p1 = value.get_ptr<test_type*>();
228         CHECK(p1 == value.get_ptr<test_type*>());
229         CHECK(*p1 == value.get<test_type>());
230 
231         const test_type* p2 = value.get_ptr<const test_type*>();
232         CHECK(p1 == value.get_ptr<const test_type*>());
233         CHECK(*p2 == value.get<test_type>());
234 
235         const test_type* const p3 = value.get_ptr<const test_type* const>();
236         CHECK(p1 == value.get_ptr<const test_type* const>());
237         CHECK(*p3 == value.get<test_type>());
238 
239         // check if null pointers are returned correctly
240         CHECK(value.get_ptr<json::object_t*>() == nullptr);
241         CHECK(value.get_ptr<json::array_t*>() == nullptr);
242         CHECK(value.get_ptr<json::string_t*>() == nullptr);
243         CHECK(value.get_ptr<json::boolean_t*>() != nullptr);
244         CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
245         CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
246         CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
247     }
248 
249     SECTION("pointer access to const boolean_t")
250     {
251         using test_type = const json::boolean_t;
252         const json value = false;
253 
254         // check if pointers are returned correctly
255         test_type* p1 = value.get_ptr<test_type*>();
256         CHECK(p1 == value.get_ptr<test_type*>());
257         //CHECK(*p1 == value.get<test_type>());
258 
259         const test_type* p2 = value.get_ptr<const test_type*>();
260         CHECK(p1 == value.get_ptr<const test_type*>());
261         CHECK(*p2 == value.get<test_type>());
262 
263         const test_type* const p3 = value.get_ptr<const test_type* const>();
264         CHECK(p1 == value.get_ptr<const test_type* const>());
265         CHECK(*p3 == value.get<test_type>());
266 
267         // check if null pointers are returned correctly
268         CHECK(value.get_ptr<const json::object_t*>() == nullptr);
269         CHECK(value.get_ptr<const json::array_t*>() == nullptr);
270         CHECK(value.get_ptr<const json::string_t*>() == nullptr);
271         CHECK(value.get_ptr<const json::boolean_t*>() != nullptr);
272         CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
273         CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
274         CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
275     }
276 
277     SECTION("pointer access to number_integer_t")
278     {
279         using test_type = json::number_integer_t;
280         json value = 23;
281 
282         // check if pointers are returned correctly
283         test_type* p1 = value.get_ptr<test_type*>();
284         CHECK(p1 == value.get_ptr<test_type*>());
285         CHECK(*p1 == value.get<test_type>());
286 
287         const test_type* p2 = value.get_ptr<const test_type*>();
288         CHECK(p1 == value.get_ptr<const test_type*>());
289         CHECK(*p2 == value.get<test_type>());
290 
291         const test_type* const p3 = value.get_ptr<const test_type* const>();
292         CHECK(p1 == value.get_ptr<const test_type* const>());
293         CHECK(*p3 == value.get<test_type>());
294 
295         // check if null pointers are returned correctly
296         CHECK(value.get_ptr<json::object_t*>() == nullptr);
297         CHECK(value.get_ptr<json::array_t*>() == nullptr);
298         CHECK(value.get_ptr<json::string_t*>() == nullptr);
299         CHECK(value.get_ptr<json::boolean_t*>() == nullptr);
300         CHECK(value.get_ptr<json::number_integer_t*>() != nullptr);
301         CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
302         CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
303     }
304 
305     SECTION("pointer access to const number_integer_t")
306     {
307         using test_type = const json::number_integer_t;
308         const json value = 23;
309 
310         // check if pointers are returned correctly
311         test_type* p1 = value.get_ptr<test_type*>();
312         CHECK(p1 == value.get_ptr<test_type*>());
313         CHECK(*p1 == value.get<test_type>());
314 
315         const test_type* p2 = value.get_ptr<const test_type*>();
316         CHECK(p1 == value.get_ptr<const test_type*>());
317         CHECK(*p2 == value.get<test_type>());
318 
319         const test_type* const p3 = value.get_ptr<const test_type* const>();
320         CHECK(p1 == value.get_ptr<const test_type* const>());
321         CHECK(*p3 == value.get<test_type>());
322 
323         // check if null pointers are returned correctly
324         CHECK(value.get_ptr<const json::object_t*>() == nullptr);
325         CHECK(value.get_ptr<const json::array_t*>() == nullptr);
326         CHECK(value.get_ptr<const json::string_t*>() == nullptr);
327         CHECK(value.get_ptr<const json::boolean_t*>() == nullptr);
328         CHECK(value.get_ptr<const json::number_integer_t*>() != nullptr);
329         CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
330         CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
331     }
332 
333     SECTION("pointer access to number_unsigned_t")
334     {
335         using test_type = json::number_unsigned_t;
336         json value = 23u;
337 
338         // check if pointers are returned correctly
339         test_type* p1 = value.get_ptr<test_type*>();
340         CHECK(p1 == value.get_ptr<test_type*>());
341         CHECK(*p1 == value.get<test_type>());
342 
343         const test_type* p2 = value.get_ptr<const test_type*>();
344         CHECK(p1 == value.get_ptr<const test_type*>());
345         CHECK(*p2 == value.get<test_type>());
346 
347         const test_type* const p3 = value.get_ptr<const test_type* const>();
348         CHECK(p1 == value.get_ptr<const test_type* const>());
349         CHECK(*p3 == value.get<test_type>());
350 
351         // check if null pointers are returned correctly
352         CHECK(value.get_ptr<json::object_t*>() == nullptr);
353         CHECK(value.get_ptr<json::array_t*>() == nullptr);
354         CHECK(value.get_ptr<json::string_t*>() == nullptr);
355         CHECK(value.get_ptr<json::boolean_t*>() == nullptr);
356         CHECK(value.get_ptr<json::number_integer_t*>() != nullptr);
357         CHECK(value.get_ptr<json::number_unsigned_t*>() != nullptr);
358         CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
359     }
360 
361     SECTION("pointer access to const number_unsigned_t")
362     {
363         using test_type = const json::number_unsigned_t;
364         const json value = 23u;
365 
366         // check if pointers are returned correctly
367         test_type* p1 = value.get_ptr<test_type*>();
368         CHECK(p1 == value.get_ptr<test_type*>());
369         CHECK(*p1 == value.get<test_type>());
370 
371         const test_type* p2 = value.get_ptr<const test_type*>();
372         CHECK(p1 == value.get_ptr<const test_type*>());
373         CHECK(*p2 == value.get<test_type>());
374 
375         const test_type* const p3 = value.get_ptr<const test_type* const>();
376         CHECK(p1 == value.get_ptr<const test_type* const>());
377         CHECK(*p3 == value.get<test_type>());
378 
379         // check if null pointers are returned correctly
380         CHECK(value.get_ptr<const json::object_t*>() == nullptr);
381         CHECK(value.get_ptr<const json::array_t*>() == nullptr);
382         CHECK(value.get_ptr<const json::string_t*>() == nullptr);
383         CHECK(value.get_ptr<const json::boolean_t*>() == nullptr);
384         CHECK(value.get_ptr<const json::number_integer_t*>() != nullptr);
385         CHECK(value.get_ptr<const json::number_unsigned_t*>() != nullptr);
386         CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
387     }
388 
389     SECTION("pointer access to number_float_t")
390     {
391         using test_type = json::number_float_t;
392         json value = 42.23;
393 
394         // check if pointers are returned correctly
395         test_type* p1 = value.get_ptr<test_type*>();
396         CHECK(p1 == value.get_ptr<test_type*>());
397         CHECK(*p1 == Approx(value.get<test_type>()));
398 
399         const test_type* p2 = value.get_ptr<const test_type*>();
400         CHECK(p1 == value.get_ptr<const test_type*>());
401         CHECK(*p2 == Approx(value.get<test_type>()));
402 
403         const test_type* const p3 = value.get_ptr<const test_type* const>();
404         CHECK(p1 == value.get_ptr<const test_type* const>());
405         CHECK(*p3 == Approx(value.get<test_type>()));
406 
407         // check if null pointers are returned correctly
408         CHECK(value.get_ptr<json::object_t*>() == nullptr);
409         CHECK(value.get_ptr<json::array_t*>() == nullptr);
410         CHECK(value.get_ptr<json::string_t*>() == nullptr);
411         CHECK(value.get_ptr<json::boolean_t*>() == nullptr);
412         CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
413         CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
414         CHECK(value.get_ptr<json::number_float_t*>() != nullptr);
415     }
416 
417     SECTION("pointer access to const number_float_t")
418     {
419         using test_type = const json::number_float_t;
420         const json value = 42.23;
421 
422         // check if pointers are returned correctly
423         test_type* p1 = value.get_ptr<test_type*>();
424         CHECK(p1 == value.get_ptr<test_type*>());
425         CHECK(*p1 == Approx(value.get<test_type>()));
426 
427         const test_type* p2 = value.get_ptr<const test_type*>();
428         CHECK(p1 == value.get_ptr<const test_type*>());
429         CHECK(*p2 == Approx(value.get<test_type>()));
430 
431         const test_type* const p3 = value.get_ptr<const test_type* const>();
432         CHECK(p1 == value.get_ptr<const test_type* const>());
433         CHECK(*p3 == Approx(value.get<test_type>()));
434 
435         // check if null pointers are returned correctly
436         CHECK(value.get_ptr<const json::object_t*>() == nullptr);
437         CHECK(value.get_ptr<const json::array_t*>() == nullptr);
438         CHECK(value.get_ptr<const json::string_t*>() == nullptr);
439         CHECK(value.get_ptr<const json::boolean_t*>() == nullptr);
440         CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
441         CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
442         CHECK(value.get_ptr<const json::number_float_t*>() != nullptr);
443     }
444 }
445