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("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 96 SECTION("move constructor") 97 { 98 json j {{"foo", "bar"}, {"baz", {1, 2, 3, 4}}, {"a", 42u}, {"b", 42.23}, {"c", nullptr}}; 99 CHECK(j.type() == json::value_t::object); 100 json k(std::move(j)); 101 CHECK(k.type() == json::value_t::object); 102 CHECK(j.type() == json::value_t::null); 103 } 104 105 SECTION("copy assignment") 106 { 107 SECTION("object") 108 { 109 json j {{"foo", 1}, {"bar", false}}; 110 json k; 111 k = j; 112 CHECK(j == k); 113 } 114 115 SECTION("array") 116 { 117 json j {"foo", 1, 42.23, false}; 118 json k; 119 k = j; 120 CHECK(j == k); 121 } 122 123 SECTION("null") 124 { 125 json j(nullptr); 126 json k; 127 k = j; 128 CHECK(j == k); 129 } 130 131 SECTION("boolean") 132 { 133 json j(true); 134 json k; 135 k = j; 136 CHECK(j == k); 137 } 138 139 SECTION("string") 140 { 141 json j("Hello world"); 142 json k; 143 k = j; 144 CHECK(j == k); 145 } 146 147 SECTION("number (integer)") 148 { 149 json j(42); 150 json k; 151 k = j; 152 CHECK(j == k); 153 } 154 155 SECTION("number (unsigned)") 156 { 157 json j(42u); 158 json k; 159 k = j; 160 CHECK(j == k); 161 } 162 163 SECTION("number (floating-point)") 164 { 165 json j(42.23); 166 json k; 167 k = j; 168 CHECK(j == k); 169 } 170 } 171 172 SECTION("destructor") 173 { 174 SECTION("object") 175 { 176 auto j = new json {{"foo", 1}, {"bar", false}}; 177 delete j; 178 } 179 180 SECTION("array") 181 { 182 auto j = new json {"foo", 1, 1u, false, 23.42}; 183 delete j; 184 } 185 186 SECTION("string") 187 { 188 auto j = new json("Hello world"); 189 delete j; 190 } 191 } 192 } 193