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