• 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 #include <nlohmann/json.hpp>
33 using nlohmann::json;
34 
35 TEST_CASE("other constructors and destructor")
36 {
37     SECTION("copy constructor")
38     {
39         SECTION("object")
40         {
41             json j {{"foo", 1}, {"bar", false}};
42             json k(j);
43             CHECK(j == k);
44         }
45 
46         SECTION("array")
47         {
48             json j {"foo", 1, 42.23, false};
49             json k(j);
50             CHECK(j == k);
51         }
52 
53         SECTION("null")
54         {
55             json j(nullptr);
56             json k(j);
57             CHECK(j == k);
58         }
59 
60         SECTION("boolean")
61         {
62             json j(true);
63             json k(j);
64             CHECK(j == k);
65         }
66 
67         SECTION("string")
68         {
69             json j("Hello world");
70             json k(j);
71             CHECK(j == k);
72         }
73 
74         SECTION("number (integer)")
75         {
76             json j(42);
77             json k(j);
78             CHECK(j == k);
79         }
80 
81         SECTION("number (unsigned)")
82         {
83             json j(42u);
84             json k(j);
85             CHECK(j == k);
86         }
87 
88         SECTION("number (floating-point)")
89         {
90             json j(42.23);
91             json k(j);
92             CHECK(j == k);
93         }
94 
95         SECTION("binary")
96         {
97             json j = json::binary({1, 2, 3});
98             json k(j);
99             CHECK(j == k);
100         }
101     }
102 
103     SECTION("move constructor")
104     {
105         json j {{"foo", "bar"}, {"baz", {1, 2, 3, 4}}, {"a", 42u}, {"b", 42.23}, {"c", nullptr}};
106         CHECK(j.type() == json::value_t::object);
107         json k(std::move(j));
108         CHECK(k.type() == json::value_t::object);
109         CHECK(j.type() == json::value_t::null);
110     }
111 
112     SECTION("copy assignment")
113     {
114         SECTION("object")
115         {
116             json j {{"foo", 1}, {"bar", false}};
117             json k;
118             k = j;
119             CHECK(j == k);
120         }
121 
122         SECTION("array")
123         {
124             json j {"foo", 1, 42.23, false};
125             json k;
126             k = j;
127             CHECK(j == k);
128         }
129 
130         SECTION("null")
131         {
132             json j(nullptr);
133             json k;
134             k = j;
135             CHECK(j == k);
136         }
137 
138         SECTION("boolean")
139         {
140             json j(true);
141             json k;
142             k = j;
143             CHECK(j == k);
144         }
145 
146         SECTION("string")
147         {
148             json j("Hello world");
149             json k;
150             k = j;
151             CHECK(j == k);
152         }
153 
154         SECTION("number (integer)")
155         {
156             json j(42);
157             json k;
158             k = j;
159             CHECK(j == k);
160         }
161 
162         SECTION("number (unsigned)")
163         {
164             json j(42u);
165             json k;
166             k = j;
167             CHECK(j == k);
168         }
169 
170         SECTION("number (floating-point)")
171         {
172             json j(42.23);
173             json k;
174             k = j;
175             CHECK(j == k);
176         }
177 
178         SECTION("binary")
179         {
180             json j = json::binary({1, 2, 3});
181             json k;
182             k = j;
183             CHECK(j == k);
184         }
185     }
186 
187     SECTION("destructor")
188     {
189         SECTION("object")
190         {
191             auto j = new json {{"foo", 1}, {"bar", false}};
192             delete j;
193         }
194 
195         SECTION("array")
196         {
197             auto j = new json {"foo", 1, 1u, false, 23.42};
198             delete j;
199         }
200 
201         SECTION("string")
202         {
203             auto j = new json("Hello world");
204             delete j;
205         }
206     }
207 }
208