• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     __ _____ _____ _____
3  __|  |   __|     |   | |  JSON for Modern C++ (test suite)
4 |  |  |__   |  |  | | | |  version 3.9.1
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 #define private public
33 #include <nlohmann/json.hpp>
34 using nlohmann::json;
35 #undef private
36 
37 TEST_CASE("const_iterator class")
38 {
39     SECTION("construction")
40     {
41         SECTION("constructor")
42         {
43             SECTION("null")
44             {
45                 json j(json::value_t::null);
46                 json::const_iterator it(&j);
47             }
48 
49             SECTION("object")
50             {
51                 json j(json::value_t::object);
52                 json::const_iterator it(&j);
53             }
54 
55             SECTION("array")
56             {
57                 json j(json::value_t::array);
58                 json::const_iterator it(&j);
59             }
60         }
61 
62         SECTION("copy assignment")
63         {
64             json j(json::value_t::null);
65             json::const_iterator it(&j);
66             json::const_iterator it2(&j);
67             it2 = it;
68         }
69 
70         SECTION("copy constructor from non-const iterator")
71         {
72             SECTION("create from uninitialized iterator")
73             {
74                 const json::iterator it {};
75                 json::const_iterator cit(it);
76             }
77 
78             SECTION("create from initialized iterator")
79             {
80                 json j;
81                 const json::iterator it = j.begin();
82                 json::const_iterator cit(it);
83             }
84         }
85     }
86 
87     SECTION("initialization")
88     {
89         SECTION("set_begin")
90         {
91             SECTION("null")
92             {
93                 json j(json::value_t::null);
94                 json::const_iterator it(&j);
95                 it.set_begin();
96                 CHECK((it == j.cbegin()));
97             }
98 
99             SECTION("object")
100             {
101                 json j(json::value_t::object);
102                 json::const_iterator it(&j);
103                 it.set_begin();
104                 CHECK((it == j.cbegin()));
105             }
106 
107             SECTION("array")
108             {
109                 json j(json::value_t::array);
110                 json::const_iterator it(&j);
111                 it.set_begin();
112                 CHECK((it == j.cbegin()));
113             }
114         }
115 
116         SECTION("set_end")
117         {
118             SECTION("null")
119             {
120                 json j(json::value_t::null);
121                 json::const_iterator it(&j);
122                 it.set_end();
123                 CHECK((it == j.cend()));
124             }
125 
126             SECTION("object")
127             {
128                 json j(json::value_t::object);
129                 json::const_iterator it(&j);
130                 it.set_end();
131                 CHECK((it == j.cend()));
132             }
133 
134             SECTION("array")
135             {
136                 json j(json::value_t::array);
137                 json::const_iterator it(&j);
138                 it.set_end();
139                 CHECK((it == j.cend()));
140             }
141         }
142     }
143 
144     SECTION("element access")
145     {
146         SECTION("operator*")
147         {
148             SECTION("null")
149             {
150                 json j(json::value_t::null);
151                 json::const_iterator it = j.cbegin();
152                 CHECK_THROWS_AS(*it, json::invalid_iterator&);
153                 CHECK_THROWS_WITH(*it, "[json.exception.invalid_iterator.214] cannot get value");
154             }
155 
156             SECTION("number")
157             {
158                 json j(17);
159                 json::const_iterator it = j.cbegin();
160                 CHECK(*it == json(17));
161                 it = j.cend();
162                 CHECK_THROWS_AS(*it, json::invalid_iterator&);
163                 CHECK_THROWS_WITH(*it, "[json.exception.invalid_iterator.214] cannot get value");
164             }
165 
166             SECTION("object")
167             {
168                 json j({{"foo", "bar"}});
169                 json::const_iterator it = j.cbegin();
170                 CHECK(*it == json("bar"));
171             }
172 
173             SECTION("array")
174             {
175                 json j({1, 2, 3, 4});
176                 json::const_iterator it = j.cbegin();
177                 CHECK(*it == json(1));
178             }
179         }
180 
181         SECTION("operator->")
182         {
183             SECTION("null")
184             {
185                 json j(json::value_t::null);
186                 json::const_iterator it = j.cbegin();
187                 CHECK_THROWS_AS(std::string(it->type_name()), json::invalid_iterator&);
188                 CHECK_THROWS_WITH(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value");
189             }
190 
191             SECTION("number")
192             {
193                 json j(17);
194                 json::const_iterator it = j.cbegin();
195                 CHECK(std::string(it->type_name()) == "number");
196                 it = j.cend();
197                 CHECK_THROWS_AS(std::string(it->type_name()), json::invalid_iterator&);
198                 CHECK_THROWS_WITH(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value");
199             }
200 
201             SECTION("object")
202             {
203                 json j({{"foo", "bar"}});
204                 json::const_iterator it = j.cbegin();
205                 CHECK(std::string(it->type_name()) == "string");
206             }
207 
208             SECTION("array")
209             {
210                 json j({1, 2, 3, 4});
211                 json::const_iterator it = j.cbegin();
212                 CHECK(std::string(it->type_name()) == "number");
213             }
214         }
215     }
216 
217     SECTION("increment/decrement")
218     {
219         SECTION("post-increment")
220         {
221             SECTION("null")
222             {
223                 json j(json::value_t::null);
224                 json::const_iterator it = j.cbegin();
225                 CHECK((it.m_it.primitive_iterator.m_it == 1));
226                 it++;
227                 CHECK((it.m_it.primitive_iterator.m_it != 0 && it.m_it.primitive_iterator.m_it != 1));
228             }
229 
230             SECTION("number")
231             {
232                 json j(17);
233                 json::const_iterator it = j.cbegin();
234                 CHECK((it.m_it.primitive_iterator.m_it == 0));
235                 it++;
236                 CHECK((it.m_it.primitive_iterator.m_it == 1));
237                 it++;
238                 CHECK((it.m_it.primitive_iterator.m_it != 0 && it.m_it.primitive_iterator.m_it != 1));
239             }
240 
241             SECTION("object")
242             {
243                 json j({{"foo", "bar"}});
244                 json::const_iterator it = j.cbegin();
245                 CHECK((it.m_it.object_iterator == it.m_object->m_value.object->begin()));
246                 it++;
247                 CHECK((it.m_it.object_iterator == it.m_object->m_value.object->end()));
248             }
249 
250             SECTION("array")
251             {
252                 json j({1, 2, 3, 4});
253                 json::const_iterator it = j.cbegin();
254                 CHECK((it.m_it.array_iterator == it.m_object->m_value.array->begin()));
255                 it++;
256                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
257                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
258                 it++;
259                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
260                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
261                 it++;
262                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
263                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
264                 it++;
265                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
266                 CHECK((it.m_it.array_iterator == it.m_object->m_value.array->end()));
267             }
268         }
269 
270         SECTION("pre-increment")
271         {
272             SECTION("null")
273             {
274                 json j(json::value_t::null);
275                 json::const_iterator it = j.cbegin();
276                 CHECK((it.m_it.primitive_iterator.m_it == 1));
277                 ++it;
278                 CHECK((it.m_it.primitive_iterator.m_it != 0 && it.m_it.primitive_iterator.m_it != 1));
279             }
280 
281             SECTION("number")
282             {
283                 json j(17);
284                 json::const_iterator it = j.cbegin();
285                 CHECK((it.m_it.primitive_iterator.m_it == 0));
286                 ++it;
287                 CHECK((it.m_it.primitive_iterator.m_it == 1));
288                 ++it;
289                 CHECK((it.m_it.primitive_iterator.m_it != 0 && it.m_it.primitive_iterator.m_it != 1));
290             }
291 
292             SECTION("object")
293             {
294                 json j({{"foo", "bar"}});
295                 json::const_iterator it = j.cbegin();
296                 CHECK((it.m_it.object_iterator == it.m_object->m_value.object->begin()));
297                 ++it;
298                 CHECK((it.m_it.object_iterator == it.m_object->m_value.object->end()));
299             }
300 
301             SECTION("array")
302             {
303                 json j({1, 2, 3, 4});
304                 json::const_iterator it = j.cbegin();
305                 CHECK((it.m_it.array_iterator == it.m_object->m_value.array->begin()));
306                 ++it;
307                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
308                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
309                 ++it;
310                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
311                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
312                 ++it;
313                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
314                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
315                 ++it;
316                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
317                 CHECK((it.m_it.array_iterator == it.m_object->m_value.array->end()));
318             }
319         }
320 
321         SECTION("post-decrement")
322         {
323             SECTION("null")
324             {
325                 json j(json::value_t::null);
326                 json::const_iterator it = j.cend();
327                 CHECK((it.m_it.primitive_iterator.m_it == 1));
328             }
329 
330             SECTION("number")
331             {
332                 json j(17);
333                 json::const_iterator it = j.cend();
334                 CHECK((it.m_it.primitive_iterator.m_it == 1));
335                 it--;
336                 CHECK((it.m_it.primitive_iterator.m_it == 0));
337                 it--;
338                 CHECK((it.m_it.primitive_iterator.m_it != 0 && it.m_it.primitive_iterator.m_it != 1));
339             }
340 
341             SECTION("object")
342             {
343                 json j({{"foo", "bar"}});
344                 json::const_iterator it = j.cend();
345                 CHECK((it.m_it.object_iterator == it.m_object->m_value.object->end()));
346                 it--;
347                 CHECK((it.m_it.object_iterator == it.m_object->m_value.object->begin()));
348             }
349 
350             SECTION("array")
351             {
352                 json j({1, 2, 3, 4});
353                 json::const_iterator it = j.cend();
354                 CHECK((it.m_it.array_iterator == it.m_object->m_value.array->end()));
355                 it--;
356                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
357                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
358                 it--;
359                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
360                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
361                 it--;
362                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
363                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
364                 it--;
365                 CHECK((it.m_it.array_iterator == it.m_object->m_value.array->begin()));
366                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
367             }
368         }
369 
370         SECTION("pre-decrement")
371         {
372             SECTION("null")
373             {
374                 json j(json::value_t::null);
375                 json::const_iterator it = j.cend();
376                 CHECK((it.m_it.primitive_iterator.m_it == 1));
377             }
378 
379             SECTION("number")
380             {
381                 json j(17);
382                 json::const_iterator it = j.cend();
383                 CHECK((it.m_it.primitive_iterator.m_it == 1));
384                 --it;
385                 CHECK((it.m_it.primitive_iterator.m_it == 0));
386                 --it;
387                 CHECK((it.m_it.primitive_iterator.m_it != 0 && it.m_it.primitive_iterator.m_it != 1));
388             }
389 
390             SECTION("object")
391             {
392                 json j({{"foo", "bar"}});
393                 json::const_iterator it = j.cend();
394                 CHECK((it.m_it.object_iterator == it.m_object->m_value.object->end()));
395                 --it;
396                 CHECK((it.m_it.object_iterator == it.m_object->m_value.object->begin()));
397             }
398 
399             SECTION("array")
400             {
401                 json j({1, 2, 3, 4});
402                 json::const_iterator it = j.cend();
403                 CHECK((it.m_it.array_iterator == it.m_object->m_value.array->end()));
404                 --it;
405                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
406                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
407                 --it;
408                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
409                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
410                 --it;
411                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->begin()));
412                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
413                 --it;
414                 CHECK((it.m_it.array_iterator == it.m_object->m_value.array->begin()));
415                 CHECK((it.m_it.array_iterator != it.m_object->m_value.array->end()));
416             }
417         }
418     }
419 }
420