Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 2 | // Distributed under MIT license, or public domain if desired and |
| 3 | // recognized in your jurisdiction. |
| 4 | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE |
| 5 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 6 | #if defined(__GNUC__) |
| 7 | #pragma GCC diagnostic push |
| 8 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 9 | #elif defined(_MSC_VER) |
| 10 | #pragma warning(disable : 4996) |
| 11 | #endif |
| 12 | |
| 13 | #include "fuzz.h" |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 14 | #include "jsontest.h" |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 15 | #include <algorithm> |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 16 | #include <cmath> |
| 17 | #include <cstring> |
| 18 | #include <functional> |
| 19 | #include <iomanip> |
| 20 | #include <iostream> |
| 21 | #include <iterator> |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 22 | #include <json/config.h> |
| 23 | #include <json/json.h> |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 24 | #include <limits> |
| 25 | #include <memory> |
| 26 | #include <sstream> |
| 27 | #include <string> |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 28 | #include <vector> |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 29 | |
| 30 | using CharReaderPtr = std::unique_ptr<Json::CharReader>; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 31 | |
| 32 | // Make numeric limits more convenient to talk about. |
| 33 | // Assumes int type in 32 bits. |
| 34 | #define kint32max Json::Value::maxInt |
| 35 | #define kint32min Json::Value::minInt |
| 36 | #define kuint32max Json::Value::maxUInt |
| 37 | #define kint64max Json::Value::maxInt64 |
| 38 | #define kint64min Json::Value::minInt64 |
| 39 | #define kuint64max Json::Value::maxUInt64 |
| 40 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 41 | // static const double kdint64max = double(kint64max); |
| 42 | // static const float kfint64max = float(kint64max); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 43 | static const float kfint32max = float(kint32max); |
| 44 | static const float kfuint32max = float(kuint32max); |
| 45 | |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 46 | // ////////////////////////////////////////////////////////////////// |
| 47 | // ////////////////////////////////////////////////////////////////// |
| 48 | // Json Library test cases |
| 49 | // ////////////////////////////////////////////////////////////////// |
| 50 | // ////////////////////////////////////////////////////////////////// |
| 51 | |
| 52 | #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 53 | static inline double uint64ToDouble(Json::UInt64 value) { |
| 54 | return static_cast<double>(value); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 55 | } |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 56 | #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) |
| 57 | static inline double uint64ToDouble(Json::UInt64 value) { |
| 58 | return static_cast<double>(Json::Int64(value / 2)) * 2.0 + |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 59 | static_cast<double>(Json::Int64(value & 1)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 60 | } |
| 61 | #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) |
| 62 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 63 | // local_ is the collection for the testcases in this code file. |
| 64 | static std::deque<JsonTest::TestCaseFactory> local_; |
| 65 | #define JSONTEST_FIXTURE_LOCAL(FixtureType, name) \ |
| 66 | JSONTEST_FIXTURE_V2(FixtureType, name, local_) |
| 67 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 68 | struct ValueTest : JsonTest::TestCase { |
| 69 | Json::Value null_; |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 70 | Json::Value emptyArray_{Json::arrayValue}; |
| 71 | Json::Value emptyObject_{Json::objectValue}; |
| 72 | Json::Value integer_{123456789}; |
| 73 | Json::Value unsignedInteger_{34567890}; |
| 74 | Json::Value smallUnsignedInteger_{Json::Value::UInt(Json::Value::maxInt)}; |
| 75 | Json::Value real_{1234.56789}; |
| 76 | Json::Value float_{0.00390625f}; |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 77 | Json::Value array1_; |
| 78 | Json::Value object1_; |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 79 | Json::Value emptyString_{""}; |
| 80 | Json::Value string1_{"a"}; |
| 81 | Json::Value string_{"sometext with space"}; |
| 82 | Json::Value true_{true}; |
| 83 | Json::Value false_{false}; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 84 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 85 | ValueTest() { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 86 | array1_.append(1234); |
| 87 | object1_["id"] = 1234; |
| 88 | } |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 89 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 90 | struct IsCheck { |
| 91 | /// Initialize all checks to \c false by default. |
| 92 | IsCheck(); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 93 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 94 | bool isObject_{false}; |
| 95 | bool isArray_{false}; |
| 96 | bool isBool_{false}; |
| 97 | bool isString_{false}; |
| 98 | bool isNull_{false}; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 99 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 100 | bool isInt_{false}; |
| 101 | bool isInt64_{false}; |
| 102 | bool isUInt_{false}; |
| 103 | bool isUInt64_{false}; |
| 104 | bool isIntegral_{false}; |
| 105 | bool isDouble_{false}; |
| 106 | bool isNumeric_{false}; |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 107 | }; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 108 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 109 | void checkConstMemberCount(const Json::Value& value, |
| 110 | unsigned int expectedCount); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 111 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 112 | void checkMemberCount(Json::Value& value, unsigned int expectedCount); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 113 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 114 | void checkIs(const Json::Value& value, const IsCheck& check); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 115 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 116 | void checkIsLess(const Json::Value& x, const Json::Value& y); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 117 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 118 | void checkIsEqual(const Json::Value& x, const Json::Value& y); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 119 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 120 | /// Normalize the representation of floating-point number by stripped leading |
| 121 | /// 0 in exponent. |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 122 | static Json::String normalizeFloatingPointStr(const Json::String& s); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 123 | }; |
| 124 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 125 | Json::String ValueTest::normalizeFloatingPointStr(const Json::String& s) { |
| 126 | auto index = s.find_last_of("eE"); |
| 127 | if (index == s.npos) |
| 128 | return s; |
| 129 | std::size_t signWidth = (s[index + 1] == '+' || s[index + 1] == '-') ? 1 : 0; |
| 130 | auto exponentStartIndex = index + 1 + signWidth; |
| 131 | Json::String normalized = s.substr(0, exponentStartIndex); |
| 132 | auto indexDigit = s.find_first_not_of('0', exponentStartIndex); |
| 133 | Json::String exponent = "0"; |
| 134 | if (indexDigit != s.npos) { // nonzero exponent |
| 135 | exponent = s.substr(indexDigit); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 136 | } |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 137 | return normalized + exponent; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 140 | JSONTEST_FIXTURE_LOCAL(ValueTest, checkNormalizeFloatingPointStr) { |
| 141 | struct TestData { |
| 142 | std::string in; |
| 143 | std::string out; |
| 144 | } const testData[] = { |
| 145 | {"0.0", "0.0"}, |
| 146 | {"0e0", "0e0"}, |
| 147 | {"1234.0", "1234.0"}, |
| 148 | {"1234.0e0", "1234.0e0"}, |
| 149 | {"1234.0e-1", "1234.0e-1"}, |
| 150 | {"1234.0e+0", "1234.0e+0"}, |
| 151 | {"1234.0e+001", "1234.0e+1"}, |
| 152 | {"1234e-1", "1234e-1"}, |
| 153 | {"1234e+000", "1234e+0"}, |
| 154 | {"1234e+001", "1234e+1"}, |
| 155 | {"1234e10", "1234e10"}, |
| 156 | {"1234e010", "1234e10"}, |
| 157 | {"1234e+010", "1234e+10"}, |
| 158 | {"1234e-010", "1234e-10"}, |
| 159 | {"1234e+100", "1234e+100"}, |
| 160 | {"1234e-100", "1234e-100"}, |
| 161 | }; |
| 162 | for (const auto& td : testData) { |
| 163 | JSONTEST_ASSERT_STRING_EQUAL(normalizeFloatingPointStr(td.in), td.out); |
| 164 | } |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 167 | JSONTEST_FIXTURE_LOCAL(ValueTest, memberCount) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 168 | JSONTEST_ASSERT_PRED(checkMemberCount(emptyArray_, 0)); |
| 169 | JSONTEST_ASSERT_PRED(checkMemberCount(emptyObject_, 0)); |
| 170 | JSONTEST_ASSERT_PRED(checkMemberCount(array1_, 1)); |
| 171 | JSONTEST_ASSERT_PRED(checkMemberCount(object1_, 1)); |
| 172 | JSONTEST_ASSERT_PRED(checkMemberCount(null_, 0)); |
| 173 | JSONTEST_ASSERT_PRED(checkMemberCount(integer_, 0)); |
| 174 | JSONTEST_ASSERT_PRED(checkMemberCount(unsignedInteger_, 0)); |
| 175 | JSONTEST_ASSERT_PRED(checkMemberCount(smallUnsignedInteger_, 0)); |
| 176 | JSONTEST_ASSERT_PRED(checkMemberCount(real_, 0)); |
| 177 | JSONTEST_ASSERT_PRED(checkMemberCount(emptyString_, 0)); |
| 178 | JSONTEST_ASSERT_PRED(checkMemberCount(string_, 0)); |
| 179 | JSONTEST_ASSERT_PRED(checkMemberCount(true_, 0)); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 180 | JSONTEST_ASSERT_PRED(checkMemberCount(false_, 0)); |
| 181 | JSONTEST_ASSERT_PRED(checkMemberCount(string1_, 0)); |
| 182 | JSONTEST_ASSERT_PRED(checkMemberCount(float_, 0)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 185 | JSONTEST_FIXTURE_LOCAL(ValueTest, objects) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 186 | // Types |
| 187 | IsCheck checks; |
| 188 | checks.isObject_ = true; |
| 189 | JSONTEST_ASSERT_PRED(checkIs(emptyObject_, checks)); |
| 190 | JSONTEST_ASSERT_PRED(checkIs(object1_, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 191 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 192 | JSONTEST_ASSERT_EQUAL(Json::objectValue, emptyObject_.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 193 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 194 | // Empty object okay |
| 195 | JSONTEST_ASSERT(emptyObject_.isConvertibleTo(Json::nullValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 196 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 197 | // Non-empty object not okay |
| 198 | JSONTEST_ASSERT(!object1_.isConvertibleTo(Json::nullValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 199 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 200 | // Always okay |
| 201 | JSONTEST_ASSERT(emptyObject_.isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 202 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 203 | // Never okay |
| 204 | JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::arrayValue)); |
| 205 | JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::intValue)); |
| 206 | JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::uintValue)); |
| 207 | JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::realValue)); |
| 208 | JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::booleanValue)); |
| 209 | JSONTEST_ASSERT(!emptyObject_.isConvertibleTo(Json::stringValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 210 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 211 | // Access through const reference |
| 212 | const Json::Value& constObject = object1_; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 213 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 214 | JSONTEST_ASSERT_EQUAL(Json::Value(1234), constObject["id"]); |
| 215 | JSONTEST_ASSERT_EQUAL(Json::Value(), constObject["unknown id"]); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 216 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 217 | // Access through find() |
| 218 | const char idKey[] = "id"; |
| 219 | const Json::Value* foundId = object1_.find(idKey, idKey + strlen(idKey)); |
| 220 | JSONTEST_ASSERT(foundId != nullptr); |
| 221 | JSONTEST_ASSERT_EQUAL(Json::Value(1234), *foundId); |
| 222 | |
| 223 | const char unknownIdKey[] = "unknown id"; |
| 224 | const Json::Value* foundUnknownId = |
| 225 | object1_.find(unknownIdKey, unknownIdKey + strlen(unknownIdKey)); |
| 226 | JSONTEST_ASSERT_EQUAL(nullptr, foundUnknownId); |
| 227 | |
| 228 | // Access through demand() |
| 229 | const char yetAnotherIdKey[] = "yet another id"; |
| 230 | const Json::Value* foundYetAnotherId = |
| 231 | object1_.find(yetAnotherIdKey, yetAnotherIdKey + strlen(yetAnotherIdKey)); |
| 232 | JSONTEST_ASSERT_EQUAL(nullptr, foundYetAnotherId); |
| 233 | Json::Value* demandedYetAnotherId = object1_.demand( |
| 234 | yetAnotherIdKey, yetAnotherIdKey + strlen(yetAnotherIdKey)); |
| 235 | JSONTEST_ASSERT(demandedYetAnotherId != nullptr); |
| 236 | *demandedYetAnotherId = "baz"; |
| 237 | |
| 238 | JSONTEST_ASSERT_EQUAL(Json::Value("baz"), object1_["yet another id"]); |
| 239 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 240 | // Access through non-const reference |
| 241 | JSONTEST_ASSERT_EQUAL(Json::Value(1234), object1_["id"]); |
| 242 | JSONTEST_ASSERT_EQUAL(Json::Value(), object1_["unknown id"]); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 243 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 244 | object1_["some other id"] = "foo"; |
| 245 | JSONTEST_ASSERT_EQUAL(Json::Value("foo"), object1_["some other id"]); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 246 | JSONTEST_ASSERT_EQUAL(Json::Value("foo"), object1_["some other id"]); |
| 247 | |
| 248 | // Remove. |
| 249 | Json::Value got; |
| 250 | bool did; |
| 251 | did = object1_.removeMember("some other id", &got); |
| 252 | JSONTEST_ASSERT_EQUAL(Json::Value("foo"), got); |
| 253 | JSONTEST_ASSERT_EQUAL(true, did); |
| 254 | got = Json::Value("bar"); |
| 255 | did = object1_.removeMember("some other id", &got); |
| 256 | JSONTEST_ASSERT_EQUAL(Json::Value("bar"), got); |
| 257 | JSONTEST_ASSERT_EQUAL(false, did); |
| 258 | |
| 259 | object1_["some other id"] = "foo"; |
| 260 | Json::Value* gotPtr = nullptr; |
| 261 | did = object1_.removeMember("some other id", gotPtr); |
| 262 | JSONTEST_ASSERT_EQUAL(nullptr, gotPtr); |
| 263 | JSONTEST_ASSERT_EQUAL(true, did); |
| 264 | |
| 265 | // Using other removeMember interfaces, the test idea is the same as above. |
| 266 | object1_["some other id"] = "foo"; |
| 267 | const Json::String key("some other id"); |
| 268 | did = object1_.removeMember(key, &got); |
| 269 | JSONTEST_ASSERT_EQUAL(Json::Value("foo"), got); |
| 270 | JSONTEST_ASSERT_EQUAL(true, did); |
| 271 | got = Json::Value("bar"); |
| 272 | did = object1_.removeMember(key, &got); |
| 273 | JSONTEST_ASSERT_EQUAL(Json::Value("bar"), got); |
| 274 | JSONTEST_ASSERT_EQUAL(false, did); |
| 275 | |
| 276 | object1_["some other id"] = "foo"; |
| 277 | object1_.removeMember(key); |
| 278 | JSONTEST_ASSERT_EQUAL(Json::nullValue, object1_[key]); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 281 | JSONTEST_FIXTURE_LOCAL(ValueTest, arrays) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 282 | const unsigned int index0 = 0; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 283 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 284 | // Types |
| 285 | IsCheck checks; |
| 286 | checks.isArray_ = true; |
| 287 | JSONTEST_ASSERT_PRED(checkIs(emptyArray_, checks)); |
| 288 | JSONTEST_ASSERT_PRED(checkIs(array1_, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 289 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 290 | JSONTEST_ASSERT_EQUAL(Json::arrayValue, array1_.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 291 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 292 | // Empty array okay |
| 293 | JSONTEST_ASSERT(emptyArray_.isConvertibleTo(Json::nullValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 294 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 295 | // Non-empty array not okay |
| 296 | JSONTEST_ASSERT(!array1_.isConvertibleTo(Json::nullValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 297 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 298 | // Always okay |
| 299 | JSONTEST_ASSERT(emptyArray_.isConvertibleTo(Json::arrayValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 300 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 301 | // Never okay |
| 302 | JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::objectValue)); |
| 303 | JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::intValue)); |
| 304 | JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::uintValue)); |
| 305 | JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::realValue)); |
| 306 | JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::booleanValue)); |
| 307 | JSONTEST_ASSERT(!emptyArray_.isConvertibleTo(Json::stringValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 308 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 309 | // Access through const reference |
| 310 | const Json::Value& constArray = array1_; |
| 311 | JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray[index0]); |
| 312 | JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray[0]); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 313 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 314 | // Access through non-const reference |
| 315 | JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_[index0]); |
| 316 | JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_[0]); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 317 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 318 | array1_[2] = Json::Value(17); |
| 319 | JSONTEST_ASSERT_EQUAL(Json::Value(), array1_[1]); |
| 320 | JSONTEST_ASSERT_EQUAL(Json::Value(17), array1_[2]); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 321 | Json::Value got; |
| 322 | JSONTEST_ASSERT_EQUAL(true, array1_.removeIndex(2, &got)); |
| 323 | JSONTEST_ASSERT_EQUAL(Json::Value(17), got); |
| 324 | JSONTEST_ASSERT_EQUAL(false, array1_.removeIndex(2, &got)); // gone now |
| 325 | } |
| 326 | JSONTEST_FIXTURE_LOCAL(ValueTest, resizeArray) { |
| 327 | Json::Value array; |
| 328 | { |
| 329 | for (Json::ArrayIndex i = 0; i < 10; i++) |
| 330 | array[i] = i; |
| 331 | JSONTEST_ASSERT_EQUAL(array.size(), 10); |
| 332 | // The length set is greater than the length of the array. |
| 333 | array.resize(15); |
| 334 | JSONTEST_ASSERT_EQUAL(array.size(), 15); |
| 335 | |
| 336 | // The length set is less than the length of the array. |
| 337 | array.resize(5); |
| 338 | JSONTEST_ASSERT_EQUAL(array.size(), 5); |
| 339 | |
| 340 | // The length of the array is set to 0. |
| 341 | array.resize(0); |
| 342 | JSONTEST_ASSERT_EQUAL(array.size(), 0); |
| 343 | } |
| 344 | { |
| 345 | for (Json::ArrayIndex i = 0; i < 10; i++) |
| 346 | array[i] = i; |
| 347 | JSONTEST_ASSERT_EQUAL(array.size(), 10); |
| 348 | array.clear(); |
| 349 | JSONTEST_ASSERT_EQUAL(array.size(), 0); |
| 350 | } |
| 351 | } |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 352 | |
| 353 | JSONTEST_FIXTURE_LOCAL(ValueTest, resizePopulatesAllMissingElements) { |
| 354 | Json::ArrayIndex n = 10; |
| 355 | Json::Value v; |
| 356 | v.resize(n); |
| 357 | JSONTEST_ASSERT_EQUAL(n, v.size()); |
| 358 | JSONTEST_ASSERT_EQUAL(n, std::distance(v.begin(), v.end())); |
| 359 | for (const Json::Value& e : v) |
| 360 | JSONTEST_ASSERT_EQUAL(e, Json::Value{}); |
| 361 | } |
| 362 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 363 | JSONTEST_FIXTURE_LOCAL(ValueTest, getArrayValue) { |
| 364 | Json::Value array; |
| 365 | for (Json::ArrayIndex i = 0; i < 5; i++) |
| 366 | array[i] = i; |
| 367 | |
| 368 | JSONTEST_ASSERT_EQUAL(array.size(), 5); |
| 369 | const Json::Value defaultValue(10); |
| 370 | Json::ArrayIndex index = 0; |
| 371 | for (; index <= 4; index++) |
| 372 | JSONTEST_ASSERT_EQUAL(index, array.get(index, defaultValue).asInt()); |
| 373 | |
| 374 | index = 4; |
| 375 | JSONTEST_ASSERT_EQUAL(array.isValidIndex(index), true); |
| 376 | index = 5; |
| 377 | JSONTEST_ASSERT_EQUAL(array.isValidIndex(index), false); |
| 378 | JSONTEST_ASSERT_EQUAL(defaultValue, array.get(index, defaultValue)); |
| 379 | JSONTEST_ASSERT_EQUAL(array.isValidIndex(index), false); |
| 380 | } |
| 381 | JSONTEST_FIXTURE_LOCAL(ValueTest, arrayIssue252) { |
| 382 | int count = 5; |
| 383 | Json::Value root; |
| 384 | Json::Value item; |
| 385 | root["array"] = Json::Value::nullSingleton(); |
| 386 | for (int i = 0; i < count; i++) { |
| 387 | item["a"] = i; |
| 388 | item["b"] = i; |
| 389 | root["array"][i] = item; |
| 390 | } |
| 391 | // JSONTEST_ASSERT_EQUAL(5, root["array"].size()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 392 | } |
| 393 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 394 | JSONTEST_FIXTURE_LOCAL(ValueTest, arrayInsertAtRandomIndex) { |
| 395 | Json::Value array; |
| 396 | const Json::Value str0("index2"); |
| 397 | const Json::Value str1("index3"); |
| 398 | array.append("index0"); // append rvalue |
| 399 | array.append("index1"); |
| 400 | array.append(str0); // append lvalue |
| 401 | |
| 402 | std::vector<Json::Value*> vec; // storage value address for checking |
| 403 | for (Json::ArrayIndex i = 0; i < 3; i++) { |
| 404 | vec.push_back(&array[i]); |
| 405 | } |
| 406 | JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); // check append |
| 407 | JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); |
| 408 | JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); |
| 409 | |
| 410 | // insert lvalue at the head |
| 411 | JSONTEST_ASSERT(array.insert(0, str1)); |
| 412 | JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); |
| 413 | JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); |
| 414 | JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]); |
| 415 | JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[3]); |
| 416 | // checking address |
| 417 | for (Json::ArrayIndex i = 0; i < 3; i++) { |
| 418 | JSONTEST_ASSERT_EQUAL(vec[i], &array[i]); |
| 419 | } |
| 420 | vec.push_back(&array[3]); |
| 421 | // insert rvalue at middle |
| 422 | JSONTEST_ASSERT(array.insert(2, "index4")); |
| 423 | JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); |
| 424 | JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); |
| 425 | JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]); |
| 426 | JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]); |
| 427 | JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]); |
| 428 | // checking address |
| 429 | for (Json::ArrayIndex i = 0; i < 4; i++) { |
| 430 | JSONTEST_ASSERT_EQUAL(vec[i], &array[i]); |
| 431 | } |
| 432 | vec.push_back(&array[4]); |
| 433 | // insert rvalue at the tail |
| 434 | JSONTEST_ASSERT(array.insert(5, "index5")); |
| 435 | JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); |
| 436 | JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); |
| 437 | JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]); |
| 438 | JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]); |
| 439 | JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]); |
| 440 | JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]); |
| 441 | // checking address |
| 442 | for (Json::ArrayIndex i = 0; i < 5; i++) { |
| 443 | JSONTEST_ASSERT_EQUAL(vec[i], &array[i]); |
| 444 | } |
| 445 | vec.push_back(&array[5]); |
| 446 | // beyond max array size, it should not be allowed to insert into its tail |
| 447 | JSONTEST_ASSERT(!array.insert(10, "index10")); |
| 448 | } |
| 449 | |
| 450 | JSONTEST_FIXTURE_LOCAL(ValueTest, null) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 451 | JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 452 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 453 | IsCheck checks; |
| 454 | checks.isNull_ = true; |
| 455 | JSONTEST_ASSERT_PRED(checkIs(null_, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 456 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 457 | JSONTEST_ASSERT(null_.isConvertibleTo(Json::nullValue)); |
| 458 | JSONTEST_ASSERT(null_.isConvertibleTo(Json::intValue)); |
| 459 | JSONTEST_ASSERT(null_.isConvertibleTo(Json::uintValue)); |
| 460 | JSONTEST_ASSERT(null_.isConvertibleTo(Json::realValue)); |
| 461 | JSONTEST_ASSERT(null_.isConvertibleTo(Json::booleanValue)); |
| 462 | JSONTEST_ASSERT(null_.isConvertibleTo(Json::stringValue)); |
| 463 | JSONTEST_ASSERT(null_.isConvertibleTo(Json::arrayValue)); |
| 464 | JSONTEST_ASSERT(null_.isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 465 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 466 | JSONTEST_ASSERT_EQUAL(Json::Int(0), null_.asInt()); |
| 467 | JSONTEST_ASSERT_EQUAL(Json::LargestInt(0), null_.asLargestInt()); |
| 468 | JSONTEST_ASSERT_EQUAL(Json::UInt(0), null_.asUInt()); |
| 469 | JSONTEST_ASSERT_EQUAL(Json::LargestUInt(0), null_.asLargestUInt()); |
| 470 | JSONTEST_ASSERT_EQUAL(0.0, null_.asDouble()); |
| 471 | JSONTEST_ASSERT_EQUAL(0.0, null_.asFloat()); |
| 472 | JSONTEST_ASSERT_STRING_EQUAL("", null_.asString()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 473 | |
| 474 | JSONTEST_ASSERT_EQUAL(Json::Value::nullSingleton(), null_); |
| 475 | |
| 476 | // Test using a Value in a boolean context (false iff null) |
| 477 | JSONTEST_ASSERT_EQUAL(null_, false); |
| 478 | JSONTEST_ASSERT_EQUAL(object1_, true); |
| 479 | JSONTEST_ASSERT_EQUAL(!null_, true); |
| 480 | JSONTEST_ASSERT_EQUAL(!object1_, false); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 481 | } |
| 482 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 483 | JSONTEST_FIXTURE_LOCAL(ValueTest, strings) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 484 | JSONTEST_ASSERT_EQUAL(Json::stringValue, string1_.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 485 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 486 | IsCheck checks; |
| 487 | checks.isString_ = true; |
| 488 | JSONTEST_ASSERT_PRED(checkIs(emptyString_, checks)); |
| 489 | JSONTEST_ASSERT_PRED(checkIs(string_, checks)); |
| 490 | JSONTEST_ASSERT_PRED(checkIs(string1_, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 491 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 492 | // Empty string okay |
| 493 | JSONTEST_ASSERT(emptyString_.isConvertibleTo(Json::nullValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 494 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 495 | // Non-empty string not okay |
| 496 | JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::nullValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 497 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 498 | // Always okay |
| 499 | JSONTEST_ASSERT(string1_.isConvertibleTo(Json::stringValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 500 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 501 | // Never okay |
| 502 | JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::objectValue)); |
| 503 | JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::arrayValue)); |
| 504 | JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::intValue)); |
| 505 | JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::uintValue)); |
| 506 | JSONTEST_ASSERT(!string1_.isConvertibleTo(Json::realValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 507 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 508 | JSONTEST_ASSERT_STRING_EQUAL("a", string1_.asString()); |
| 509 | JSONTEST_ASSERT_STRING_EQUAL("a", string1_.asCString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 510 | } |
| 511 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 512 | JSONTEST_FIXTURE_LOCAL(ValueTest, bools) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 513 | JSONTEST_ASSERT_EQUAL(Json::booleanValue, false_.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 514 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 515 | IsCheck checks; |
| 516 | checks.isBool_ = true; |
| 517 | JSONTEST_ASSERT_PRED(checkIs(false_, checks)); |
| 518 | JSONTEST_ASSERT_PRED(checkIs(true_, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 519 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 520 | // False okay |
| 521 | JSONTEST_ASSERT(false_.isConvertibleTo(Json::nullValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 522 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 523 | // True not okay |
| 524 | JSONTEST_ASSERT(!true_.isConvertibleTo(Json::nullValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 525 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 526 | // Always okay |
| 527 | JSONTEST_ASSERT(true_.isConvertibleTo(Json::intValue)); |
| 528 | JSONTEST_ASSERT(true_.isConvertibleTo(Json::uintValue)); |
| 529 | JSONTEST_ASSERT(true_.isConvertibleTo(Json::realValue)); |
| 530 | JSONTEST_ASSERT(true_.isConvertibleTo(Json::booleanValue)); |
| 531 | JSONTEST_ASSERT(true_.isConvertibleTo(Json::stringValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 532 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 533 | // Never okay |
| 534 | JSONTEST_ASSERT(!true_.isConvertibleTo(Json::arrayValue)); |
| 535 | JSONTEST_ASSERT(!true_.isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 536 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 537 | JSONTEST_ASSERT_EQUAL(true, true_.asBool()); |
| 538 | JSONTEST_ASSERT_EQUAL(1, true_.asInt()); |
| 539 | JSONTEST_ASSERT_EQUAL(1, true_.asLargestInt()); |
| 540 | JSONTEST_ASSERT_EQUAL(1, true_.asUInt()); |
| 541 | JSONTEST_ASSERT_EQUAL(1, true_.asLargestUInt()); |
| 542 | JSONTEST_ASSERT_EQUAL(1.0, true_.asDouble()); |
| 543 | JSONTEST_ASSERT_EQUAL(1.0, true_.asFloat()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 544 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 545 | JSONTEST_ASSERT_EQUAL(false, false_.asBool()); |
| 546 | JSONTEST_ASSERT_EQUAL(0, false_.asInt()); |
| 547 | JSONTEST_ASSERT_EQUAL(0, false_.asLargestInt()); |
| 548 | JSONTEST_ASSERT_EQUAL(0, false_.asUInt()); |
| 549 | JSONTEST_ASSERT_EQUAL(0, false_.asLargestUInt()); |
| 550 | JSONTEST_ASSERT_EQUAL(0.0, false_.asDouble()); |
| 551 | JSONTEST_ASSERT_EQUAL(0.0, false_.asFloat()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 552 | } |
| 553 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 554 | JSONTEST_FIXTURE_LOCAL(ValueTest, integers) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 555 | IsCheck checks; |
| 556 | Json::Value val; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 557 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 558 | // Conversions that don't depend on the value. |
| 559 | JSONTEST_ASSERT(Json::Value(17).isConvertibleTo(Json::realValue)); |
| 560 | JSONTEST_ASSERT(Json::Value(17).isConvertibleTo(Json::stringValue)); |
| 561 | JSONTEST_ASSERT(Json::Value(17).isConvertibleTo(Json::booleanValue)); |
| 562 | JSONTEST_ASSERT(!Json::Value(17).isConvertibleTo(Json::arrayValue)); |
| 563 | JSONTEST_ASSERT(!Json::Value(17).isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 564 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 565 | JSONTEST_ASSERT(Json::Value(17U).isConvertibleTo(Json::realValue)); |
| 566 | JSONTEST_ASSERT(Json::Value(17U).isConvertibleTo(Json::stringValue)); |
| 567 | JSONTEST_ASSERT(Json::Value(17U).isConvertibleTo(Json::booleanValue)); |
| 568 | JSONTEST_ASSERT(!Json::Value(17U).isConvertibleTo(Json::arrayValue)); |
| 569 | JSONTEST_ASSERT(!Json::Value(17U).isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 570 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 571 | JSONTEST_ASSERT(Json::Value(17.0).isConvertibleTo(Json::realValue)); |
| 572 | JSONTEST_ASSERT(Json::Value(17.0).isConvertibleTo(Json::stringValue)); |
| 573 | JSONTEST_ASSERT(Json::Value(17.0).isConvertibleTo(Json::booleanValue)); |
| 574 | JSONTEST_ASSERT(!Json::Value(17.0).isConvertibleTo(Json::arrayValue)); |
| 575 | JSONTEST_ASSERT(!Json::Value(17.0).isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 576 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 577 | // Default int |
| 578 | val = Json::Value(Json::intValue); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 579 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 580 | JSONTEST_ASSERT_EQUAL(Json::intValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 581 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 582 | checks = IsCheck(); |
| 583 | checks.isInt_ = true; |
| 584 | checks.isInt64_ = true; |
| 585 | checks.isUInt_ = true; |
| 586 | checks.isUInt64_ = true; |
| 587 | checks.isIntegral_ = true; |
| 588 | checks.isDouble_ = true; |
| 589 | checks.isNumeric_ = true; |
| 590 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 591 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 592 | JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue)); |
| 593 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 594 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 595 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 596 | JSONTEST_ASSERT_EQUAL(0, val.asInt()); |
| 597 | JSONTEST_ASSERT_EQUAL(0, val.asLargestInt()); |
| 598 | JSONTEST_ASSERT_EQUAL(0, val.asUInt()); |
| 599 | JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt()); |
| 600 | JSONTEST_ASSERT_EQUAL(0.0, val.asDouble()); |
| 601 | JSONTEST_ASSERT_EQUAL(0.0, val.asFloat()); |
| 602 | JSONTEST_ASSERT_EQUAL(false, val.asBool()); |
| 603 | JSONTEST_ASSERT_STRING_EQUAL("0", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 604 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 605 | // Default uint |
| 606 | val = Json::Value(Json::uintValue); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 607 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 608 | JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 609 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 610 | checks = IsCheck(); |
| 611 | checks.isInt_ = true; |
| 612 | checks.isInt64_ = true; |
| 613 | checks.isUInt_ = true; |
| 614 | checks.isUInt64_ = true; |
| 615 | checks.isIntegral_ = true; |
| 616 | checks.isDouble_ = true; |
| 617 | checks.isNumeric_ = true; |
| 618 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 619 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 620 | JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue)); |
| 621 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 622 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 623 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 624 | JSONTEST_ASSERT_EQUAL(0, val.asInt()); |
| 625 | JSONTEST_ASSERT_EQUAL(0, val.asLargestInt()); |
| 626 | JSONTEST_ASSERT_EQUAL(0, val.asUInt()); |
| 627 | JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt()); |
| 628 | JSONTEST_ASSERT_EQUAL(0.0, val.asDouble()); |
| 629 | JSONTEST_ASSERT_EQUAL(0.0, val.asFloat()); |
| 630 | JSONTEST_ASSERT_EQUAL(false, val.asBool()); |
| 631 | JSONTEST_ASSERT_STRING_EQUAL("0", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 632 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 633 | // Default real |
| 634 | val = Json::Value(Json::realValue); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 635 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 636 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 637 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 638 | JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue)); |
| 639 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 640 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 641 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 642 | checks = IsCheck(); |
| 643 | checks.isInt_ = true; |
| 644 | checks.isInt64_ = true; |
| 645 | checks.isUInt_ = true; |
| 646 | checks.isUInt64_ = true; |
| 647 | checks.isIntegral_ = true; |
| 648 | checks.isDouble_ = true; |
| 649 | checks.isNumeric_ = true; |
| 650 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 651 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 652 | JSONTEST_ASSERT_EQUAL(0, val.asInt()); |
| 653 | JSONTEST_ASSERT_EQUAL(0, val.asLargestInt()); |
| 654 | JSONTEST_ASSERT_EQUAL(0, val.asUInt()); |
| 655 | JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt()); |
| 656 | JSONTEST_ASSERT_EQUAL(0.0, val.asDouble()); |
| 657 | JSONTEST_ASSERT_EQUAL(0.0, val.asFloat()); |
| 658 | JSONTEST_ASSERT_EQUAL(false, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 659 | JSONTEST_ASSERT_STRING_EQUAL("0.0", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 660 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 661 | // Zero (signed constructor arg) |
| 662 | val = Json::Value(0); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 663 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 664 | JSONTEST_ASSERT_EQUAL(Json::intValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 665 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 666 | checks = IsCheck(); |
| 667 | checks.isInt_ = true; |
| 668 | checks.isInt64_ = true; |
| 669 | checks.isUInt_ = true; |
| 670 | checks.isUInt64_ = true; |
| 671 | checks.isIntegral_ = true; |
| 672 | checks.isDouble_ = true; |
| 673 | checks.isNumeric_ = true; |
| 674 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 675 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 676 | JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue)); |
| 677 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 678 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 679 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 680 | JSONTEST_ASSERT_EQUAL(0, val.asInt()); |
| 681 | JSONTEST_ASSERT_EQUAL(0, val.asLargestInt()); |
| 682 | JSONTEST_ASSERT_EQUAL(0, val.asUInt()); |
| 683 | JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt()); |
| 684 | JSONTEST_ASSERT_EQUAL(0.0, val.asDouble()); |
| 685 | JSONTEST_ASSERT_EQUAL(0.0, val.asFloat()); |
| 686 | JSONTEST_ASSERT_EQUAL(false, val.asBool()); |
| 687 | JSONTEST_ASSERT_STRING_EQUAL("0", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 688 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 689 | // Zero (unsigned constructor arg) |
| 690 | val = Json::Value(0u); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 691 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 692 | JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 693 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 694 | checks = IsCheck(); |
| 695 | checks.isInt_ = true; |
| 696 | checks.isInt64_ = true; |
| 697 | checks.isUInt_ = true; |
| 698 | checks.isUInt64_ = true; |
| 699 | checks.isIntegral_ = true; |
| 700 | checks.isDouble_ = true; |
| 701 | checks.isNumeric_ = true; |
| 702 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 703 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 704 | JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue)); |
| 705 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 706 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 707 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 708 | JSONTEST_ASSERT_EQUAL(0, val.asInt()); |
| 709 | JSONTEST_ASSERT_EQUAL(0, val.asLargestInt()); |
| 710 | JSONTEST_ASSERT_EQUAL(0, val.asUInt()); |
| 711 | JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt()); |
| 712 | JSONTEST_ASSERT_EQUAL(0.0, val.asDouble()); |
| 713 | JSONTEST_ASSERT_EQUAL(0.0, val.asFloat()); |
| 714 | JSONTEST_ASSERT_EQUAL(false, val.asBool()); |
| 715 | JSONTEST_ASSERT_STRING_EQUAL("0", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 716 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 717 | // Zero (floating-point constructor arg) |
| 718 | val = Json::Value(0.0); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 719 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 720 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 721 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 722 | checks = IsCheck(); |
| 723 | checks.isInt_ = true; |
| 724 | checks.isInt64_ = true; |
| 725 | checks.isUInt_ = true; |
| 726 | checks.isUInt64_ = true; |
| 727 | checks.isIntegral_ = true; |
| 728 | checks.isDouble_ = true; |
| 729 | checks.isNumeric_ = true; |
| 730 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 731 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 732 | JSONTEST_ASSERT(val.isConvertibleTo(Json::nullValue)); |
| 733 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 734 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 735 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 736 | JSONTEST_ASSERT_EQUAL(0, val.asInt()); |
| 737 | JSONTEST_ASSERT_EQUAL(0, val.asLargestInt()); |
| 738 | JSONTEST_ASSERT_EQUAL(0, val.asUInt()); |
| 739 | JSONTEST_ASSERT_EQUAL(0, val.asLargestUInt()); |
| 740 | JSONTEST_ASSERT_EQUAL(0.0, val.asDouble()); |
| 741 | JSONTEST_ASSERT_EQUAL(0.0, val.asFloat()); |
| 742 | JSONTEST_ASSERT_EQUAL(false, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 743 | JSONTEST_ASSERT_STRING_EQUAL("0.0", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 744 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 745 | // 2^20 (signed constructor arg) |
| 746 | val = Json::Value(1 << 20); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 747 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 748 | JSONTEST_ASSERT_EQUAL(Json::intValue, val.type()); |
| 749 | checks = IsCheck(); |
| 750 | checks.isInt_ = true; |
| 751 | checks.isInt64_ = true; |
| 752 | checks.isUInt_ = true; |
| 753 | checks.isUInt64_ = true; |
| 754 | checks.isIntegral_ = true; |
| 755 | checks.isDouble_ = true; |
| 756 | checks.isNumeric_ = true; |
| 757 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 758 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 759 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 760 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 761 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 762 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 763 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asInt()); |
| 764 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestInt()); |
| 765 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asUInt()); |
| 766 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestUInt()); |
| 767 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asDouble()); |
| 768 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asFloat()); |
| 769 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 770 | JSONTEST_ASSERT_STRING_EQUAL("1048576", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 771 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 772 | // 2^20 (unsigned constructor arg) |
| 773 | val = Json::Value(Json::UInt(1 << 20)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 774 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 775 | JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 776 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 777 | checks = IsCheck(); |
| 778 | checks.isInt_ = true; |
| 779 | checks.isInt64_ = true; |
| 780 | checks.isUInt_ = true; |
| 781 | checks.isUInt64_ = true; |
| 782 | checks.isIntegral_ = true; |
| 783 | checks.isDouble_ = true; |
| 784 | checks.isNumeric_ = true; |
| 785 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 786 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 787 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 788 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 789 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 790 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 791 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asInt()); |
| 792 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestInt()); |
| 793 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asUInt()); |
| 794 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestUInt()); |
| 795 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asDouble()); |
| 796 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asFloat()); |
| 797 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 798 | JSONTEST_ASSERT_STRING_EQUAL("1048576", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 799 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 800 | // 2^20 (floating-point constructor arg) |
| 801 | val = Json::Value((1 << 20) / 1.0); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 802 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 803 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 804 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 805 | checks = IsCheck(); |
| 806 | checks.isInt_ = true; |
| 807 | checks.isInt64_ = true; |
| 808 | checks.isUInt_ = true; |
| 809 | checks.isUInt64_ = true; |
| 810 | checks.isIntegral_ = true; |
| 811 | checks.isDouble_ = true; |
| 812 | checks.isNumeric_ = true; |
| 813 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 814 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 815 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 816 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 817 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 818 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 819 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asInt()); |
| 820 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestInt()); |
| 821 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asUInt()); |
| 822 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asLargestUInt()); |
| 823 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asDouble()); |
| 824 | JSONTEST_ASSERT_EQUAL((1 << 20), val.asFloat()); |
| 825 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 826 | JSONTEST_ASSERT_STRING_EQUAL( |
| 827 | "1048576.0", |
| 828 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 829 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 830 | // -2^20 |
| 831 | val = Json::Value(-(1 << 20)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 832 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 833 | JSONTEST_ASSERT_EQUAL(Json::intValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 834 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 835 | checks = IsCheck(); |
| 836 | checks.isInt_ = true; |
| 837 | checks.isInt64_ = true; |
| 838 | checks.isIntegral_ = true; |
| 839 | checks.isDouble_ = true; |
| 840 | checks.isNumeric_ = true; |
| 841 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 842 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 843 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 844 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 845 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 846 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 847 | JSONTEST_ASSERT_EQUAL(-(1 << 20), val.asInt()); |
| 848 | JSONTEST_ASSERT_EQUAL(-(1 << 20), val.asLargestInt()); |
| 849 | JSONTEST_ASSERT_EQUAL(-(1 << 20), val.asDouble()); |
| 850 | JSONTEST_ASSERT_EQUAL(-(1 << 20), val.asFloat()); |
| 851 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 852 | JSONTEST_ASSERT_STRING_EQUAL("-1048576", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 853 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 854 | // int32 max |
| 855 | val = Json::Value(kint32max); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 856 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 857 | JSONTEST_ASSERT_EQUAL(Json::intValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 858 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 859 | checks = IsCheck(); |
| 860 | checks.isInt_ = true; |
| 861 | checks.isInt64_ = true; |
| 862 | checks.isUInt_ = true; |
| 863 | checks.isUInt64_ = true; |
| 864 | checks.isIntegral_ = true; |
| 865 | checks.isDouble_ = true; |
| 866 | checks.isNumeric_ = true; |
| 867 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 868 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 869 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 870 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 871 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 872 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 873 | JSONTEST_ASSERT_EQUAL(kint32max, val.asInt()); |
| 874 | JSONTEST_ASSERT_EQUAL(kint32max, val.asLargestInt()); |
| 875 | JSONTEST_ASSERT_EQUAL(kint32max, val.asUInt()); |
| 876 | JSONTEST_ASSERT_EQUAL(kint32max, val.asLargestUInt()); |
| 877 | JSONTEST_ASSERT_EQUAL(kint32max, val.asDouble()); |
| 878 | JSONTEST_ASSERT_EQUAL(kfint32max, val.asFloat()); |
| 879 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 880 | JSONTEST_ASSERT_STRING_EQUAL("2147483647", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 881 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 882 | // int32 min |
| 883 | val = Json::Value(kint32min); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 884 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 885 | JSONTEST_ASSERT_EQUAL(Json::intValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 886 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 887 | checks = IsCheck(); |
| 888 | checks.isInt_ = true; |
| 889 | checks.isInt64_ = true; |
| 890 | checks.isIntegral_ = true; |
| 891 | checks.isDouble_ = true; |
| 892 | checks.isNumeric_ = true; |
| 893 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 894 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 895 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 896 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 897 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 898 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 899 | JSONTEST_ASSERT_EQUAL(kint32min, val.asInt()); |
| 900 | JSONTEST_ASSERT_EQUAL(kint32min, val.asLargestInt()); |
| 901 | JSONTEST_ASSERT_EQUAL(kint32min, val.asDouble()); |
| 902 | JSONTEST_ASSERT_EQUAL(kint32min, val.asFloat()); |
| 903 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 904 | JSONTEST_ASSERT_STRING_EQUAL("-2147483648", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 905 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 906 | // uint32 max |
| 907 | val = Json::Value(kuint32max); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 908 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 909 | JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 910 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 911 | checks = IsCheck(); |
| 912 | checks.isInt64_ = true; |
| 913 | checks.isUInt_ = true; |
| 914 | checks.isUInt64_ = true; |
| 915 | checks.isIntegral_ = true; |
| 916 | checks.isDouble_ = true; |
| 917 | checks.isNumeric_ = true; |
| 918 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 919 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 920 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 921 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 922 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 923 | |
| 924 | #ifndef JSON_NO_INT64 |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 925 | JSONTEST_ASSERT_EQUAL(kuint32max, val.asLargestInt()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 926 | #endif |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 927 | JSONTEST_ASSERT_EQUAL(kuint32max, val.asUInt()); |
| 928 | JSONTEST_ASSERT_EQUAL(kuint32max, val.asLargestUInt()); |
| 929 | JSONTEST_ASSERT_EQUAL(kuint32max, val.asDouble()); |
| 930 | JSONTEST_ASSERT_EQUAL(kfuint32max, val.asFloat()); |
| 931 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 932 | JSONTEST_ASSERT_STRING_EQUAL("4294967295", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 933 | |
| 934 | #ifdef JSON_NO_INT64 |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 935 | // int64 max |
| 936 | val = Json::Value(double(kint64max)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 937 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 938 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 939 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 940 | checks = IsCheck(); |
| 941 | checks.isDouble_ = true; |
| 942 | checks.isNumeric_ = true; |
| 943 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 944 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 945 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 946 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 947 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 948 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 949 | JSONTEST_ASSERT_EQUAL(double(kint64max), val.asDouble()); |
| 950 | JSONTEST_ASSERT_EQUAL(float(kint64max), val.asFloat()); |
| 951 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 952 | JSONTEST_ASSERT_STRING_EQUAL("9.22337e+18", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 953 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 954 | // int64 min |
| 955 | val = Json::Value(double(kint64min)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 956 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 957 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 958 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 959 | checks = IsCheck(); |
| 960 | checks.isDouble_ = true; |
| 961 | checks.isNumeric_ = true; |
| 962 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 963 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 964 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 965 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 966 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 967 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 968 | JSONTEST_ASSERT_EQUAL(double(kint64min), val.asDouble()); |
| 969 | JSONTEST_ASSERT_EQUAL(float(kint64min), val.asFloat()); |
| 970 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 971 | JSONTEST_ASSERT_STRING_EQUAL("-9.22337e+18", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 972 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 973 | // uint64 max |
| 974 | val = Json::Value(double(kuint64max)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 975 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 976 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 977 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 978 | checks = IsCheck(); |
| 979 | checks.isDouble_ = true; |
| 980 | checks.isNumeric_ = true; |
| 981 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 982 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 983 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 984 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 985 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 986 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 987 | JSONTEST_ASSERT_EQUAL(double(kuint64max), val.asDouble()); |
| 988 | JSONTEST_ASSERT_EQUAL(float(kuint64max), val.asFloat()); |
| 989 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 990 | JSONTEST_ASSERT_STRING_EQUAL("1.84467e+19", val.asString()); |
| 991 | #else // ifdef JSON_NO_INT64 |
| 992 | // 2^40 (signed constructor arg) |
| 993 | val = Json::Value(Json::Int64(1) << 40); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 994 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 995 | JSONTEST_ASSERT_EQUAL(Json::intValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 996 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 997 | checks = IsCheck(); |
| 998 | checks.isInt64_ = true; |
| 999 | checks.isUInt64_ = true; |
| 1000 | checks.isIntegral_ = true; |
| 1001 | checks.isDouble_ = true; |
| 1002 | checks.isNumeric_ = true; |
| 1003 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1004 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1005 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1006 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1007 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1008 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1009 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asInt64()); |
| 1010 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestInt()); |
| 1011 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asUInt64()); |
| 1012 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestUInt()); |
| 1013 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asDouble()); |
| 1014 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asFloat()); |
| 1015 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 1016 | JSONTEST_ASSERT_STRING_EQUAL("1099511627776", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1017 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1018 | // 2^40 (unsigned constructor arg) |
| 1019 | val = Json::Value(Json::UInt64(1) << 40); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1020 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1021 | JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1022 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1023 | checks = IsCheck(); |
| 1024 | checks.isInt64_ = true; |
| 1025 | checks.isUInt64_ = true; |
| 1026 | checks.isIntegral_ = true; |
| 1027 | checks.isDouble_ = true; |
| 1028 | checks.isNumeric_ = true; |
| 1029 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1030 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1031 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1032 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1033 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1034 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1035 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asInt64()); |
| 1036 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestInt()); |
| 1037 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asUInt64()); |
| 1038 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestUInt()); |
| 1039 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asDouble()); |
| 1040 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asFloat()); |
| 1041 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 1042 | JSONTEST_ASSERT_STRING_EQUAL("1099511627776", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1043 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1044 | // 2^40 (floating-point constructor arg) |
| 1045 | val = Json::Value((Json::Int64(1) << 40) / 1.0); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1046 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1047 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1048 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1049 | checks = IsCheck(); |
| 1050 | checks.isInt64_ = true; |
| 1051 | checks.isUInt64_ = true; |
| 1052 | checks.isIntegral_ = true; |
| 1053 | checks.isDouble_ = true; |
| 1054 | checks.isNumeric_ = true; |
| 1055 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1056 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1057 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1058 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1059 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1060 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1061 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asInt64()); |
| 1062 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestInt()); |
| 1063 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asUInt64()); |
| 1064 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asLargestUInt()); |
| 1065 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asDouble()); |
| 1066 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asFloat()); |
| 1067 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1068 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1069 | "1099511627776.0", |
| 1070 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1071 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1072 | // -2^40 |
| 1073 | val = Json::Value(-(Json::Int64(1) << 40)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1074 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1075 | JSONTEST_ASSERT_EQUAL(Json::intValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1076 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1077 | checks = IsCheck(); |
| 1078 | checks.isInt64_ = true; |
| 1079 | checks.isIntegral_ = true; |
| 1080 | checks.isDouble_ = true; |
| 1081 | checks.isNumeric_ = true; |
| 1082 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1083 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1084 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1085 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1086 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1087 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1088 | JSONTEST_ASSERT_EQUAL(-(Json::Int64(1) << 40), val.asInt64()); |
| 1089 | JSONTEST_ASSERT_EQUAL(-(Json::Int64(1) << 40), val.asLargestInt()); |
| 1090 | JSONTEST_ASSERT_EQUAL(-(Json::Int64(1) << 40), val.asDouble()); |
| 1091 | JSONTEST_ASSERT_EQUAL(-(Json::Int64(1) << 40), val.asFloat()); |
| 1092 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 1093 | JSONTEST_ASSERT_STRING_EQUAL("-1099511627776", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1094 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1095 | // int64 max |
| 1096 | val = Json::Value(Json::Int64(kint64max)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1097 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1098 | JSONTEST_ASSERT_EQUAL(Json::intValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1099 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1100 | checks = IsCheck(); |
| 1101 | checks.isInt64_ = true; |
| 1102 | checks.isUInt64_ = true; |
| 1103 | checks.isIntegral_ = true; |
| 1104 | checks.isDouble_ = true; |
| 1105 | checks.isNumeric_ = true; |
| 1106 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1107 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1108 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1109 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1110 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1111 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1112 | JSONTEST_ASSERT_EQUAL(kint64max, val.asInt64()); |
| 1113 | JSONTEST_ASSERT_EQUAL(kint64max, val.asLargestInt()); |
| 1114 | JSONTEST_ASSERT_EQUAL(kint64max, val.asUInt64()); |
| 1115 | JSONTEST_ASSERT_EQUAL(kint64max, val.asLargestUInt()); |
| 1116 | JSONTEST_ASSERT_EQUAL(double(kint64max), val.asDouble()); |
| 1117 | JSONTEST_ASSERT_EQUAL(float(kint64max), val.asFloat()); |
| 1118 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 1119 | JSONTEST_ASSERT_STRING_EQUAL("9223372036854775807", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1120 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1121 | // int64 max (floating point constructor). Note that kint64max is not exactly |
| 1122 | // representable as a double, and will be rounded up to be higher. |
| 1123 | val = Json::Value(double(kint64max)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1124 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1125 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1126 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1127 | checks = IsCheck(); |
| 1128 | checks.isUInt64_ = true; |
| 1129 | checks.isIntegral_ = true; |
| 1130 | checks.isDouble_ = true; |
| 1131 | checks.isNumeric_ = true; |
| 1132 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1133 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1134 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1135 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1136 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1137 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1138 | JSONTEST_ASSERT_EQUAL(Json::UInt64(1) << 63, val.asUInt64()); |
| 1139 | JSONTEST_ASSERT_EQUAL(Json::UInt64(1) << 63, val.asLargestUInt()); |
| 1140 | JSONTEST_ASSERT_EQUAL(uint64ToDouble(Json::UInt64(1) << 63), val.asDouble()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1141 | JSONTEST_ASSERT_EQUAL(float(Json::UInt64(1) << 63), val.asFloat()); |
| 1142 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1143 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1144 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1145 | "9.2233720368547758e+18", |
| 1146 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1147 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1148 | // int64 min |
| 1149 | val = Json::Value(Json::Int64(kint64min)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1150 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1151 | JSONTEST_ASSERT_EQUAL(Json::intValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1152 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1153 | checks = IsCheck(); |
| 1154 | checks.isInt64_ = true; |
| 1155 | checks.isIntegral_ = true; |
| 1156 | checks.isDouble_ = true; |
| 1157 | checks.isNumeric_ = true; |
| 1158 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1159 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1160 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1161 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1162 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1163 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1164 | JSONTEST_ASSERT_EQUAL(kint64min, val.asInt64()); |
| 1165 | JSONTEST_ASSERT_EQUAL(kint64min, val.asLargestInt()); |
| 1166 | JSONTEST_ASSERT_EQUAL(double(kint64min), val.asDouble()); |
| 1167 | JSONTEST_ASSERT_EQUAL(float(kint64min), val.asFloat()); |
| 1168 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 1169 | JSONTEST_ASSERT_STRING_EQUAL("-9223372036854775808", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1170 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1171 | // int64 min (floating point constructor). Note that kint64min *is* exactly |
| 1172 | // representable as a double. |
| 1173 | val = Json::Value(double(kint64min)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1174 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1175 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1176 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1177 | checks = IsCheck(); |
| 1178 | checks.isInt64_ = true; |
| 1179 | checks.isIntegral_ = true; |
| 1180 | checks.isDouble_ = true; |
| 1181 | checks.isNumeric_ = true; |
| 1182 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1183 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1184 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1185 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1186 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1187 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1188 | JSONTEST_ASSERT_EQUAL(kint64min, val.asInt64()); |
| 1189 | JSONTEST_ASSERT_EQUAL(kint64min, val.asLargestInt()); |
| 1190 | JSONTEST_ASSERT_EQUAL(-9223372036854775808.0, val.asDouble()); |
| 1191 | JSONTEST_ASSERT_EQUAL(-9223372036854775808.0, val.asFloat()); |
| 1192 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1193 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1194 | "-9.2233720368547758e+18", |
| 1195 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1196 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1197 | // 10^19 |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1198 | const auto ten_to_19 = static_cast<Json::UInt64>(1e19); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1199 | val = Json::Value(Json::UInt64(ten_to_19)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1200 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1201 | JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1202 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1203 | checks = IsCheck(); |
| 1204 | checks.isUInt64_ = true; |
| 1205 | checks.isIntegral_ = true; |
| 1206 | checks.isDouble_ = true; |
| 1207 | checks.isNumeric_ = true; |
| 1208 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1209 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1210 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1211 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1212 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1213 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1214 | JSONTEST_ASSERT_EQUAL(ten_to_19, val.asUInt64()); |
| 1215 | JSONTEST_ASSERT_EQUAL(ten_to_19, val.asLargestUInt()); |
| 1216 | JSONTEST_ASSERT_EQUAL(uint64ToDouble(ten_to_19), val.asDouble()); |
| 1217 | JSONTEST_ASSERT_EQUAL(float(uint64ToDouble(ten_to_19)), val.asFloat()); |
| 1218 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 1219 | JSONTEST_ASSERT_STRING_EQUAL("10000000000000000000", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1220 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1221 | // 10^19 (double constructor). Note that 10^19 is not exactly representable |
| 1222 | // as a double. |
| 1223 | val = Json::Value(uint64ToDouble(ten_to_19)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1224 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1225 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1226 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1227 | checks = IsCheck(); |
| 1228 | checks.isUInt64_ = true; |
| 1229 | checks.isIntegral_ = true; |
| 1230 | checks.isDouble_ = true; |
| 1231 | checks.isNumeric_ = true; |
| 1232 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1233 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1234 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1235 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1236 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1237 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1238 | JSONTEST_ASSERT_EQUAL(1e19, val.asDouble()); |
| 1239 | JSONTEST_ASSERT_EQUAL(1e19, val.asFloat()); |
| 1240 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1241 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1242 | "1e+19", |
| 1243 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1244 | |
| 1245 | // uint64 max |
| 1246 | val = Json::Value(Json::UInt64(kuint64max)); |
| 1247 | |
| 1248 | JSONTEST_ASSERT_EQUAL(Json::uintValue, val.type()); |
| 1249 | |
| 1250 | checks = IsCheck(); |
| 1251 | checks.isUInt64_ = true; |
| 1252 | checks.isIntegral_ = true; |
| 1253 | checks.isDouble_ = true; |
| 1254 | checks.isNumeric_ = true; |
| 1255 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
| 1256 | |
| 1257 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1258 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1259 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
| 1260 | |
| 1261 | JSONTEST_ASSERT_EQUAL(kuint64max, val.asUInt64()); |
| 1262 | JSONTEST_ASSERT_EQUAL(kuint64max, val.asLargestUInt()); |
| 1263 | JSONTEST_ASSERT_EQUAL(uint64ToDouble(kuint64max), val.asDouble()); |
| 1264 | JSONTEST_ASSERT_EQUAL(float(uint64ToDouble(kuint64max)), val.asFloat()); |
| 1265 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 1266 | JSONTEST_ASSERT_STRING_EQUAL("18446744073709551615", val.asString()); |
| 1267 | |
| 1268 | // uint64 max (floating point constructor). Note that kuint64max is not |
| 1269 | // exactly representable as a double, and will be rounded up to be higher. |
| 1270 | val = Json::Value(uint64ToDouble(kuint64max)); |
| 1271 | |
| 1272 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
| 1273 | |
| 1274 | checks = IsCheck(); |
| 1275 | checks.isDouble_ = true; |
| 1276 | checks.isNumeric_ = true; |
| 1277 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
| 1278 | |
| 1279 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1280 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1281 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
| 1282 | |
| 1283 | JSONTEST_ASSERT_EQUAL(18446744073709551616.0, val.asDouble()); |
| 1284 | JSONTEST_ASSERT_EQUAL(18446744073709551616.0, val.asFloat()); |
| 1285 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1286 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1287 | "1.8446744073709552e+19", |
| 1288 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1289 | #endif |
| 1290 | } |
| 1291 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1292 | JSONTEST_FIXTURE_LOCAL(ValueTest, nonIntegers) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1293 | IsCheck checks; |
| 1294 | Json::Value val; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1295 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1296 | // Small positive number |
| 1297 | val = Json::Value(1.5); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1298 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1299 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1300 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1301 | checks = IsCheck(); |
| 1302 | checks.isDouble_ = true; |
| 1303 | checks.isNumeric_ = true; |
| 1304 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1305 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1306 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 1307 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
| 1308 | JSONTEST_ASSERT(val.isConvertibleTo(Json::realValue)); |
| 1309 | JSONTEST_ASSERT(val.isConvertibleTo(Json::booleanValue)); |
| 1310 | JSONTEST_ASSERT(val.isConvertibleTo(Json::stringValue)); |
| 1311 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1312 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::arrayValue)); |
| 1313 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1314 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1315 | JSONTEST_ASSERT_EQUAL(1.5, val.asDouble()); |
| 1316 | JSONTEST_ASSERT_EQUAL(1.5, val.asFloat()); |
| 1317 | JSONTEST_ASSERT_EQUAL(1, val.asInt()); |
| 1318 | JSONTEST_ASSERT_EQUAL(1, val.asLargestInt()); |
| 1319 | JSONTEST_ASSERT_EQUAL(1, val.asUInt()); |
| 1320 | JSONTEST_ASSERT_EQUAL(1, val.asLargestUInt()); |
| 1321 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 1322 | JSONTEST_ASSERT_EQUAL("1.5", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1323 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1324 | // Small negative number |
| 1325 | val = Json::Value(-1.5); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1326 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1327 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1328 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1329 | checks = IsCheck(); |
| 1330 | checks.isDouble_ = true; |
| 1331 | checks.isNumeric_ = true; |
| 1332 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1333 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1334 | JSONTEST_ASSERT(val.isConvertibleTo(Json::intValue)); |
| 1335 | JSONTEST_ASSERT(val.isConvertibleTo(Json::realValue)); |
| 1336 | JSONTEST_ASSERT(val.isConvertibleTo(Json::booleanValue)); |
| 1337 | JSONTEST_ASSERT(val.isConvertibleTo(Json::stringValue)); |
| 1338 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1339 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
| 1340 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::arrayValue)); |
| 1341 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1342 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1343 | JSONTEST_ASSERT_EQUAL(-1.5, val.asDouble()); |
| 1344 | JSONTEST_ASSERT_EQUAL(-1.5, val.asFloat()); |
| 1345 | JSONTEST_ASSERT_EQUAL(-1, val.asInt()); |
| 1346 | JSONTEST_ASSERT_EQUAL(-1, val.asLargestInt()); |
| 1347 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
| 1348 | JSONTEST_ASSERT_EQUAL("-1.5", val.asString()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1349 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1350 | // A bit over int32 max |
| 1351 | val = Json::Value(kint32max + 0.5); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1352 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1353 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1354 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1355 | checks = IsCheck(); |
| 1356 | checks.isDouble_ = true; |
| 1357 | checks.isNumeric_ = true; |
| 1358 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1359 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1360 | JSONTEST_ASSERT(val.isConvertibleTo(Json::uintValue)); |
| 1361 | JSONTEST_ASSERT(val.isConvertibleTo(Json::realValue)); |
| 1362 | JSONTEST_ASSERT(val.isConvertibleTo(Json::booleanValue)); |
| 1363 | JSONTEST_ASSERT(val.isConvertibleTo(Json::stringValue)); |
| 1364 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1365 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1366 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::arrayValue)); |
| 1367 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1368 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1369 | JSONTEST_ASSERT_EQUAL(2147483647.5, val.asDouble()); |
| 1370 | JSONTEST_ASSERT_EQUAL(float(2147483647.5), val.asFloat()); |
| 1371 | JSONTEST_ASSERT_EQUAL(2147483647U, val.asUInt()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1372 | #ifdef JSON_HAS_INT64 |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1373 | JSONTEST_ASSERT_EQUAL(2147483647L, val.asLargestInt()); |
| 1374 | JSONTEST_ASSERT_EQUAL(2147483647U, val.asLargestUInt()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1375 | #endif |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1376 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1377 | JSONTEST_ASSERT_EQUAL( |
| 1378 | "2147483647.5", |
| 1379 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1380 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1381 | // A bit under int32 min |
| 1382 | val = Json::Value(kint32min - 0.5); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1383 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1384 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1385 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1386 | checks = IsCheck(); |
| 1387 | checks.isDouble_ = true; |
| 1388 | checks.isNumeric_ = true; |
| 1389 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1390 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1391 | JSONTEST_ASSERT(val.isConvertibleTo(Json::realValue)); |
| 1392 | JSONTEST_ASSERT(val.isConvertibleTo(Json::booleanValue)); |
| 1393 | JSONTEST_ASSERT(val.isConvertibleTo(Json::stringValue)); |
| 1394 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1395 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1396 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
| 1397 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::arrayValue)); |
| 1398 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1399 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1400 | JSONTEST_ASSERT_EQUAL(-2147483648.5, val.asDouble()); |
| 1401 | JSONTEST_ASSERT_EQUAL(float(-2147483648.5), val.asFloat()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1402 | #ifdef JSON_HAS_INT64 |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1403 | JSONTEST_ASSERT_EQUAL(-(Json::Int64(1) << 31), val.asLargestInt()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1404 | #endif |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1405 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1406 | JSONTEST_ASSERT_EQUAL( |
| 1407 | "-2147483648.5", |
| 1408 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1409 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1410 | // A bit over uint32 max |
| 1411 | val = Json::Value(kuint32max + 0.5); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1412 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1413 | JSONTEST_ASSERT_EQUAL(Json::realValue, val.type()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1414 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1415 | checks = IsCheck(); |
| 1416 | checks.isDouble_ = true; |
| 1417 | checks.isNumeric_ = true; |
| 1418 | JSONTEST_ASSERT_PRED(checkIs(val, checks)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1419 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1420 | JSONTEST_ASSERT(val.isConvertibleTo(Json::realValue)); |
| 1421 | JSONTEST_ASSERT(val.isConvertibleTo(Json::booleanValue)); |
| 1422 | JSONTEST_ASSERT(val.isConvertibleTo(Json::stringValue)); |
| 1423 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::nullValue)); |
| 1424 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::intValue)); |
| 1425 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::uintValue)); |
| 1426 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::arrayValue)); |
| 1427 | JSONTEST_ASSERT(!val.isConvertibleTo(Json::objectValue)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1428 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1429 | JSONTEST_ASSERT_EQUAL(4294967295.5, val.asDouble()); |
| 1430 | JSONTEST_ASSERT_EQUAL(float(4294967295.5), val.asFloat()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1431 | #ifdef JSON_HAS_INT64 |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1432 | JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 32) - 1, val.asLargestInt()); |
| 1433 | JSONTEST_ASSERT_EQUAL((Json::UInt64(1) << 32) - Json::UInt64(1), |
| 1434 | val.asLargestUInt()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1435 | #endif |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1436 | JSONTEST_ASSERT_EQUAL(true, val.asBool()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1437 | JSONTEST_ASSERT_EQUAL( |
| 1438 | "4294967295.5", |
| 1439 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1440 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1441 | val = Json::Value(1.2345678901234); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1442 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1443 | "1.2345678901234001", |
| 1444 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1445 | |
| 1446 | // A 16-digit floating point number. |
| 1447 | val = Json::Value(2199023255552000.0f); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1448 | JSONTEST_ASSERT_EQUAL(float(2199023255552000.0f), val.asFloat()); |
| 1449 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1450 | "2199023255552000.0", |
| 1451 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1452 | |
| 1453 | // A very large floating point number. |
| 1454 | val = Json::Value(3.402823466385289e38); |
| 1455 | JSONTEST_ASSERT_EQUAL(float(3.402823466385289e38), val.asFloat()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1456 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1457 | "3.402823466385289e+38", |
| 1458 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1459 | |
| 1460 | // An even larger floating point number. |
| 1461 | val = Json::Value(1.2345678e300); |
| 1462 | JSONTEST_ASSERT_EQUAL(double(1.2345678e300), val.asDouble()); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1463 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1464 | "1.2345678e+300", |
| 1465 | normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1466 | } |
| 1467 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1468 | void ValueTest::checkConstMemberCount(const Json::Value& value, |
| 1469 | unsigned int expectedCount) { |
| 1470 | unsigned int count = 0; |
| 1471 | Json::Value::const_iterator itEnd = value.end(); |
| 1472 | for (Json::Value::const_iterator it = value.begin(); it != itEnd; ++it) { |
| 1473 | ++count; |
| 1474 | } |
| 1475 | JSONTEST_ASSERT_EQUAL(expectedCount, count) << "Json::Value::const_iterator"; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1476 | } |
| 1477 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1478 | void ValueTest::checkMemberCount(Json::Value& value, |
| 1479 | unsigned int expectedCount) { |
| 1480 | JSONTEST_ASSERT_EQUAL(expectedCount, value.size()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1481 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1482 | unsigned int count = 0; |
| 1483 | Json::Value::iterator itEnd = value.end(); |
| 1484 | for (Json::Value::iterator it = value.begin(); it != itEnd; ++it) { |
| 1485 | ++count; |
| 1486 | } |
| 1487 | JSONTEST_ASSERT_EQUAL(expectedCount, count) << "Json::Value::iterator"; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1488 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1489 | JSONTEST_ASSERT_PRED(checkConstMemberCount(value, expectedCount)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1490 | } |
| 1491 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1492 | ValueTest::IsCheck::IsCheck() = default; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1493 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1494 | void ValueTest::checkIs(const Json::Value& value, const IsCheck& check) { |
| 1495 | JSONTEST_ASSERT_EQUAL(check.isObject_, value.isObject()); |
| 1496 | JSONTEST_ASSERT_EQUAL(check.isArray_, value.isArray()); |
| 1497 | JSONTEST_ASSERT_EQUAL(check.isBool_, value.isBool()); |
| 1498 | JSONTEST_ASSERT_EQUAL(check.isDouble_, value.isDouble()); |
| 1499 | JSONTEST_ASSERT_EQUAL(check.isInt_, value.isInt()); |
| 1500 | JSONTEST_ASSERT_EQUAL(check.isUInt_, value.isUInt()); |
| 1501 | JSONTEST_ASSERT_EQUAL(check.isIntegral_, value.isIntegral()); |
| 1502 | JSONTEST_ASSERT_EQUAL(check.isNumeric_, value.isNumeric()); |
| 1503 | JSONTEST_ASSERT_EQUAL(check.isString_, value.isString()); |
| 1504 | JSONTEST_ASSERT_EQUAL(check.isNull_, value.isNull()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1505 | |
| 1506 | #ifdef JSON_HAS_INT64 |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1507 | JSONTEST_ASSERT_EQUAL(check.isInt64_, value.isInt64()); |
| 1508 | JSONTEST_ASSERT_EQUAL(check.isUInt64_, value.isUInt64()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1509 | #else |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1510 | JSONTEST_ASSERT_EQUAL(false, value.isInt64()); |
| 1511 | JSONTEST_ASSERT_EQUAL(false, value.isUInt64()); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1512 | #endif |
| 1513 | } |
| 1514 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1515 | JSONTEST_FIXTURE_LOCAL(ValueTest, compareNull) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1516 | JSONTEST_ASSERT_PRED(checkIsEqual(Json::Value(), Json::Value())); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1517 | JSONTEST_ASSERT_PRED( |
| 1518 | checkIsEqual(Json::Value::nullSingleton(), Json::Value())); |
| 1519 | JSONTEST_ASSERT_PRED( |
| 1520 | checkIsEqual(Json::Value::nullSingleton(), Json::Value::nullSingleton())); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1521 | } |
| 1522 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1523 | JSONTEST_FIXTURE_LOCAL(ValueTest, compareInt) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1524 | JSONTEST_ASSERT_PRED(checkIsLess(0, 10)); |
| 1525 | JSONTEST_ASSERT_PRED(checkIsEqual(10, 10)); |
| 1526 | JSONTEST_ASSERT_PRED(checkIsEqual(-10, -10)); |
| 1527 | JSONTEST_ASSERT_PRED(checkIsLess(-10, 0)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1528 | } |
| 1529 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1530 | JSONTEST_FIXTURE_LOCAL(ValueTest, compareUInt) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1531 | JSONTEST_ASSERT_PRED(checkIsLess(0u, 10u)); |
| 1532 | JSONTEST_ASSERT_PRED(checkIsLess(0u, Json::Value::maxUInt)); |
| 1533 | JSONTEST_ASSERT_PRED(checkIsEqual(10u, 10u)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1534 | } |
| 1535 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1536 | JSONTEST_FIXTURE_LOCAL(ValueTest, compareDouble) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1537 | JSONTEST_ASSERT_PRED(checkIsLess(0.0, 10.0)); |
| 1538 | JSONTEST_ASSERT_PRED(checkIsEqual(10.0, 10.0)); |
| 1539 | JSONTEST_ASSERT_PRED(checkIsEqual(-10.0, -10.0)); |
| 1540 | JSONTEST_ASSERT_PRED(checkIsLess(-10.0, 0.0)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1541 | } |
| 1542 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1543 | JSONTEST_FIXTURE_LOCAL(ValueTest, compareString) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1544 | JSONTEST_ASSERT_PRED(checkIsLess("", " ")); |
| 1545 | JSONTEST_ASSERT_PRED(checkIsLess("", "a")); |
| 1546 | JSONTEST_ASSERT_PRED(checkIsLess("abcd", "zyui")); |
| 1547 | JSONTEST_ASSERT_PRED(checkIsLess("abc", "abcd")); |
| 1548 | JSONTEST_ASSERT_PRED(checkIsEqual("abcd", "abcd")); |
| 1549 | JSONTEST_ASSERT_PRED(checkIsEqual(" ", " ")); |
| 1550 | JSONTEST_ASSERT_PRED(checkIsLess("ABCD", "abcd")); |
| 1551 | JSONTEST_ASSERT_PRED(checkIsEqual("ABCD", "ABCD")); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1552 | } |
| 1553 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1554 | JSONTEST_FIXTURE_LOCAL(ValueTest, compareBoolean) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1555 | JSONTEST_ASSERT_PRED(checkIsLess(false, true)); |
| 1556 | JSONTEST_ASSERT_PRED(checkIsEqual(false, false)); |
| 1557 | JSONTEST_ASSERT_PRED(checkIsEqual(true, true)); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1558 | } |
| 1559 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1560 | JSONTEST_FIXTURE_LOCAL(ValueTest, compareArray) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1561 | // array compare size then content |
| 1562 | Json::Value emptyArray(Json::arrayValue); |
| 1563 | Json::Value l1aArray; |
| 1564 | l1aArray.append(0); |
| 1565 | Json::Value l1bArray; |
| 1566 | l1bArray.append(10); |
| 1567 | Json::Value l2aArray; |
| 1568 | l2aArray.append(0); |
| 1569 | l2aArray.append(0); |
| 1570 | Json::Value l2bArray; |
| 1571 | l2bArray.append(0); |
| 1572 | l2bArray.append(10); |
| 1573 | JSONTEST_ASSERT_PRED(checkIsLess(emptyArray, l1aArray)); |
| 1574 | JSONTEST_ASSERT_PRED(checkIsLess(emptyArray, l2aArray)); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1575 | JSONTEST_ASSERT_PRED(checkIsLess(l1aArray, l1bArray)); |
| 1576 | JSONTEST_ASSERT_PRED(checkIsLess(l1bArray, l2aArray)); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1577 | JSONTEST_ASSERT_PRED(checkIsLess(l2aArray, l2bArray)); |
| 1578 | JSONTEST_ASSERT_PRED(checkIsEqual(emptyArray, Json::Value(emptyArray))); |
| 1579 | JSONTEST_ASSERT_PRED(checkIsEqual(l1aArray, Json::Value(l1aArray))); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1580 | JSONTEST_ASSERT_PRED(checkIsEqual(l1bArray, Json::Value(l1bArray))); |
| 1581 | JSONTEST_ASSERT_PRED(checkIsEqual(l2aArray, Json::Value(l2aArray))); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1582 | JSONTEST_ASSERT_PRED(checkIsEqual(l2bArray, Json::Value(l2bArray))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1583 | } |
| 1584 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1585 | JSONTEST_FIXTURE_LOCAL(ValueTest, compareObject) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1586 | // object compare size then content |
| 1587 | Json::Value emptyObject(Json::objectValue); |
| 1588 | Json::Value l1aObject; |
| 1589 | l1aObject["key1"] = 0; |
| 1590 | Json::Value l1bObject; |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1591 | l1bObject["key1"] = 10; |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1592 | Json::Value l2aObject; |
| 1593 | l2aObject["key1"] = 0; |
| 1594 | l2aObject["key2"] = 0; |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1595 | Json::Value l2bObject; |
| 1596 | l2bObject["key1"] = 10; |
| 1597 | l2bObject["key2"] = 0; |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1598 | JSONTEST_ASSERT_PRED(checkIsLess(emptyObject, l1aObject)); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1599 | JSONTEST_ASSERT_PRED(checkIsLess(l1aObject, l1bObject)); |
| 1600 | JSONTEST_ASSERT_PRED(checkIsLess(l1bObject, l2aObject)); |
| 1601 | JSONTEST_ASSERT_PRED(checkIsLess(l2aObject, l2bObject)); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1602 | JSONTEST_ASSERT_PRED(checkIsEqual(emptyObject, Json::Value(emptyObject))); |
| 1603 | JSONTEST_ASSERT_PRED(checkIsEqual(l1aObject, Json::Value(l1aObject))); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1604 | JSONTEST_ASSERT_PRED(checkIsEqual(l1bObject, Json::Value(l1bObject))); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1605 | JSONTEST_ASSERT_PRED(checkIsEqual(l2aObject, Json::Value(l2aObject))); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1606 | JSONTEST_ASSERT_PRED(checkIsEqual(l2bObject, Json::Value(l2bObject))); |
| 1607 | { |
| 1608 | Json::Value aObject; |
| 1609 | aObject["a"] = 10; |
| 1610 | Json::Value bObject; |
| 1611 | bObject["b"] = 0; |
| 1612 | Json::Value cObject; |
| 1613 | cObject["c"] = 20; |
| 1614 | cObject["f"] = 15; |
| 1615 | Json::Value dObject; |
| 1616 | dObject["d"] = -2; |
| 1617 | dObject["e"] = 10; |
| 1618 | JSONTEST_ASSERT_PRED(checkIsLess(aObject, bObject)); |
| 1619 | JSONTEST_ASSERT_PRED(checkIsLess(bObject, cObject)); |
| 1620 | JSONTEST_ASSERT_PRED(checkIsLess(cObject, dObject)); |
| 1621 | JSONTEST_ASSERT_PRED(checkIsEqual(aObject, Json::Value(aObject))); |
| 1622 | JSONTEST_ASSERT_PRED(checkIsEqual(bObject, Json::Value(bObject))); |
| 1623 | JSONTEST_ASSERT_PRED(checkIsEqual(cObject, Json::Value(cObject))); |
| 1624 | JSONTEST_ASSERT_PRED(checkIsEqual(dObject, Json::Value(dObject))); |
| 1625 | } |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1626 | } |
| 1627 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1628 | JSONTEST_FIXTURE_LOCAL(ValueTest, compareType) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1629 | // object of different type are ordered according to their type |
| 1630 | JSONTEST_ASSERT_PRED(checkIsLess(Json::Value(), Json::Value(1))); |
| 1631 | JSONTEST_ASSERT_PRED(checkIsLess(Json::Value(1), Json::Value(1u))); |
| 1632 | JSONTEST_ASSERT_PRED(checkIsLess(Json::Value(1u), Json::Value(1.0))); |
| 1633 | JSONTEST_ASSERT_PRED(checkIsLess(Json::Value(1.0), Json::Value("a"))); |
| 1634 | JSONTEST_ASSERT_PRED(checkIsLess(Json::Value("a"), Json::Value(true))); |
| 1635 | JSONTEST_ASSERT_PRED( |
| 1636 | checkIsLess(Json::Value(true), Json::Value(Json::arrayValue))); |
| 1637 | JSONTEST_ASSERT_PRED(checkIsLess(Json::Value(Json::arrayValue), |
| 1638 | Json::Value(Json::objectValue))); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1639 | } |
| 1640 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1641 | JSONTEST_FIXTURE_LOCAL(ValueTest, CopyObject) { |
| 1642 | Json::Value arrayVal; |
| 1643 | arrayVal.append("val1"); |
| 1644 | arrayVal.append("val2"); |
| 1645 | arrayVal.append("val3"); |
| 1646 | Json::Value stringVal("string value"); |
| 1647 | Json::Value copy1, copy2; |
| 1648 | { |
| 1649 | Json::Value arrayCopy, stringCopy; |
| 1650 | arrayCopy.copy(arrayVal); |
| 1651 | stringCopy.copy(stringVal); |
| 1652 | JSONTEST_ASSERT_PRED(checkIsEqual(arrayCopy, arrayVal)); |
| 1653 | JSONTEST_ASSERT_PRED(checkIsEqual(stringCopy, stringVal)); |
| 1654 | arrayCopy.append("val4"); |
| 1655 | JSONTEST_ASSERT(arrayCopy.size() == 4); |
| 1656 | arrayVal.append("new4"); |
| 1657 | arrayVal.append("new5"); |
| 1658 | JSONTEST_ASSERT(arrayVal.size() == 5); |
| 1659 | JSONTEST_ASSERT(!(arrayCopy == arrayVal)); |
| 1660 | stringCopy = "another string"; |
| 1661 | JSONTEST_ASSERT(!(stringCopy == stringVal)); |
| 1662 | copy1.copy(arrayCopy); |
| 1663 | copy2.copy(stringCopy); |
| 1664 | } |
| 1665 | JSONTEST_ASSERT(arrayVal.size() == 5); |
| 1666 | JSONTEST_ASSERT(stringVal == "string value"); |
| 1667 | JSONTEST_ASSERT(copy1.size() == 4); |
| 1668 | JSONTEST_ASSERT(copy2 == "another string"); |
| 1669 | copy1.copy(stringVal); |
| 1670 | JSONTEST_ASSERT(copy1 == "string value"); |
| 1671 | copy2.copy(arrayVal); |
| 1672 | JSONTEST_ASSERT(copy2.size() == 5); |
| 1673 | { |
| 1674 | Json::Value srcObject, objectCopy, otherObject; |
| 1675 | srcObject["key0"] = 10; |
| 1676 | objectCopy.copy(srcObject); |
| 1677 | JSONTEST_ASSERT(srcObject["key0"] == 10); |
| 1678 | JSONTEST_ASSERT(objectCopy["key0"] == 10); |
| 1679 | JSONTEST_ASSERT(srcObject.getMemberNames().size() == 1); |
| 1680 | JSONTEST_ASSERT(objectCopy.getMemberNames().size() == 1); |
| 1681 | otherObject["key1"] = 15; |
| 1682 | otherObject["key2"] = 16; |
| 1683 | JSONTEST_ASSERT(otherObject.getMemberNames().size() == 2); |
| 1684 | objectCopy.copy(otherObject); |
| 1685 | JSONTEST_ASSERT(objectCopy["key1"] == 15); |
| 1686 | JSONTEST_ASSERT(objectCopy["key2"] == 16); |
| 1687 | JSONTEST_ASSERT(objectCopy.getMemberNames().size() == 2); |
| 1688 | otherObject["key1"] = 20; |
| 1689 | JSONTEST_ASSERT(objectCopy["key1"] == 15); |
| 1690 | } |
| 1691 | } |
| 1692 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1693 | void ValueTest::checkIsLess(const Json::Value& x, const Json::Value& y) { |
| 1694 | JSONTEST_ASSERT(x < y); |
| 1695 | JSONTEST_ASSERT(y > x); |
| 1696 | JSONTEST_ASSERT(x <= y); |
| 1697 | JSONTEST_ASSERT(y >= x); |
| 1698 | JSONTEST_ASSERT(!(x == y)); |
| 1699 | JSONTEST_ASSERT(!(y == x)); |
| 1700 | JSONTEST_ASSERT(!(x >= y)); |
| 1701 | JSONTEST_ASSERT(!(y <= x)); |
| 1702 | JSONTEST_ASSERT(!(x > y)); |
| 1703 | JSONTEST_ASSERT(!(y < x)); |
| 1704 | JSONTEST_ASSERT(x.compare(y) < 0); |
| 1705 | JSONTEST_ASSERT(y.compare(x) >= 0); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1706 | } |
| 1707 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1708 | void ValueTest::checkIsEqual(const Json::Value& x, const Json::Value& y) { |
| 1709 | JSONTEST_ASSERT(x == y); |
| 1710 | JSONTEST_ASSERT(y == x); |
| 1711 | JSONTEST_ASSERT(x <= y); |
| 1712 | JSONTEST_ASSERT(y <= x); |
| 1713 | JSONTEST_ASSERT(x >= y); |
| 1714 | JSONTEST_ASSERT(y >= x); |
| 1715 | JSONTEST_ASSERT(!(x < y)); |
| 1716 | JSONTEST_ASSERT(!(y < x)); |
| 1717 | JSONTEST_ASSERT(!(x > y)); |
| 1718 | JSONTEST_ASSERT(!(y > x)); |
| 1719 | JSONTEST_ASSERT(x.compare(y) == 0); |
| 1720 | JSONTEST_ASSERT(y.compare(x) == 0); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1721 | } |
| 1722 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1723 | JSONTEST_FIXTURE_LOCAL(ValueTest, typeChecksThrowExceptions) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1724 | #if JSON_USE_EXCEPTION |
| 1725 | |
| 1726 | Json::Value intVal(1); |
| 1727 | Json::Value strVal("Test"); |
| 1728 | Json::Value objVal(Json::objectValue); |
| 1729 | Json::Value arrVal(Json::arrayValue); |
| 1730 | |
| 1731 | JSONTEST_ASSERT_THROWS(intVal["test"]); |
| 1732 | JSONTEST_ASSERT_THROWS(strVal["test"]); |
| 1733 | JSONTEST_ASSERT_THROWS(arrVal["test"]); |
| 1734 | |
| 1735 | JSONTEST_ASSERT_THROWS(intVal.removeMember("test")); |
| 1736 | JSONTEST_ASSERT_THROWS(strVal.removeMember("test")); |
| 1737 | JSONTEST_ASSERT_THROWS(arrVal.removeMember("test")); |
| 1738 | |
| 1739 | JSONTEST_ASSERT_THROWS(intVal.getMemberNames()); |
| 1740 | JSONTEST_ASSERT_THROWS(strVal.getMemberNames()); |
| 1741 | JSONTEST_ASSERT_THROWS(arrVal.getMemberNames()); |
| 1742 | |
| 1743 | JSONTEST_ASSERT_THROWS(intVal[0]); |
| 1744 | JSONTEST_ASSERT_THROWS(objVal[0]); |
| 1745 | JSONTEST_ASSERT_THROWS(strVal[0]); |
| 1746 | |
| 1747 | JSONTEST_ASSERT_THROWS(intVal.clear()); |
| 1748 | |
| 1749 | JSONTEST_ASSERT_THROWS(intVal.resize(1)); |
| 1750 | JSONTEST_ASSERT_THROWS(strVal.resize(1)); |
| 1751 | JSONTEST_ASSERT_THROWS(objVal.resize(1)); |
| 1752 | |
| 1753 | JSONTEST_ASSERT_THROWS(intVal.asCString()); |
| 1754 | |
| 1755 | JSONTEST_ASSERT_THROWS(objVal.asString()); |
| 1756 | JSONTEST_ASSERT_THROWS(arrVal.asString()); |
| 1757 | |
| 1758 | JSONTEST_ASSERT_THROWS(strVal.asInt()); |
| 1759 | JSONTEST_ASSERT_THROWS(objVal.asInt()); |
| 1760 | JSONTEST_ASSERT_THROWS(arrVal.asInt()); |
| 1761 | |
| 1762 | JSONTEST_ASSERT_THROWS(strVal.asUInt()); |
| 1763 | JSONTEST_ASSERT_THROWS(objVal.asUInt()); |
| 1764 | JSONTEST_ASSERT_THROWS(arrVal.asUInt()); |
| 1765 | |
| 1766 | JSONTEST_ASSERT_THROWS(strVal.asInt64()); |
| 1767 | JSONTEST_ASSERT_THROWS(objVal.asInt64()); |
| 1768 | JSONTEST_ASSERT_THROWS(arrVal.asInt64()); |
| 1769 | |
| 1770 | JSONTEST_ASSERT_THROWS(strVal.asUInt64()); |
| 1771 | JSONTEST_ASSERT_THROWS(objVal.asUInt64()); |
| 1772 | JSONTEST_ASSERT_THROWS(arrVal.asUInt64()); |
| 1773 | |
| 1774 | JSONTEST_ASSERT_THROWS(strVal.asDouble()); |
| 1775 | JSONTEST_ASSERT_THROWS(objVal.asDouble()); |
| 1776 | JSONTEST_ASSERT_THROWS(arrVal.asDouble()); |
| 1777 | |
| 1778 | JSONTEST_ASSERT_THROWS(strVal.asFloat()); |
| 1779 | JSONTEST_ASSERT_THROWS(objVal.asFloat()); |
| 1780 | JSONTEST_ASSERT_THROWS(arrVal.asFloat()); |
| 1781 | |
| 1782 | JSONTEST_ASSERT_THROWS(strVal.asBool()); |
| 1783 | JSONTEST_ASSERT_THROWS(objVal.asBool()); |
| 1784 | JSONTEST_ASSERT_THROWS(arrVal.asBool()); |
| 1785 | |
| 1786 | #endif |
| 1787 | } |
| 1788 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1789 | JSONTEST_FIXTURE_LOCAL(ValueTest, offsetAccessors) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1790 | Json::Value x; |
| 1791 | JSONTEST_ASSERT(x.getOffsetStart() == 0); |
| 1792 | JSONTEST_ASSERT(x.getOffsetLimit() == 0); |
| 1793 | x.setOffsetStart(10); |
| 1794 | x.setOffsetLimit(20); |
| 1795 | JSONTEST_ASSERT(x.getOffsetStart() == 10); |
| 1796 | JSONTEST_ASSERT(x.getOffsetLimit() == 20); |
| 1797 | Json::Value y(x); |
| 1798 | JSONTEST_ASSERT(y.getOffsetStart() == 10); |
| 1799 | JSONTEST_ASSERT(y.getOffsetLimit() == 20); |
| 1800 | Json::Value z; |
| 1801 | z.swap(y); |
| 1802 | JSONTEST_ASSERT(z.getOffsetStart() == 10); |
| 1803 | JSONTEST_ASSERT(z.getOffsetLimit() == 20); |
| 1804 | JSONTEST_ASSERT(y.getOffsetStart() == 0); |
| 1805 | JSONTEST_ASSERT(y.getOffsetLimit() == 0); |
| 1806 | } |
| 1807 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1808 | JSONTEST_FIXTURE_LOCAL(ValueTest, StaticString) { |
| 1809 | char mutant[] = "hello"; |
| 1810 | Json::StaticString ss(mutant); |
| 1811 | Json::String regular(mutant); |
| 1812 | mutant[1] = 'a'; |
| 1813 | JSONTEST_ASSERT_STRING_EQUAL("hallo", ss.c_str()); |
| 1814 | JSONTEST_ASSERT_STRING_EQUAL("hello", regular.c_str()); |
| 1815 | { |
| 1816 | Json::Value root; |
| 1817 | root["top"] = ss; |
| 1818 | JSONTEST_ASSERT_STRING_EQUAL("hallo", root["top"].asString()); |
| 1819 | mutant[1] = 'u'; |
| 1820 | JSONTEST_ASSERT_STRING_EQUAL("hullo", root["top"].asString()); |
| 1821 | } |
| 1822 | { |
| 1823 | Json::Value root; |
| 1824 | root["top"] = regular; |
| 1825 | JSONTEST_ASSERT_STRING_EQUAL("hello", root["top"].asString()); |
| 1826 | mutant[1] = 'u'; |
| 1827 | JSONTEST_ASSERT_STRING_EQUAL("hello", root["top"].asString()); |
| 1828 | } |
| 1829 | } |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 1830 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1831 | JSONTEST_FIXTURE_LOCAL(ValueTest, WideString) { |
| 1832 | // https://github.com/open-source-parsers/jsoncpp/issues/756 |
| 1833 | const std::string uni = u8"\u5f0f\uff0c\u8fdb"; // "式,进" |
| 1834 | std::string styled; |
| 1835 | { |
| 1836 | Json::Value v; |
| 1837 | v["abc"] = uni; |
| 1838 | styled = v.toStyledString(); |
| 1839 | } |
| 1840 | Json::Value root; |
| 1841 | { |
| 1842 | JSONCPP_STRING errs; |
| 1843 | std::istringstream iss(styled); |
| 1844 | bool ok = parseFromStream(Json::CharReaderBuilder(), iss, &root, &errs); |
| 1845 | JSONTEST_ASSERT(ok); |
| 1846 | if (!ok) { |
| 1847 | std::cerr << "errs: " << errs << std::endl; |
| 1848 | } |
| 1849 | } |
| 1850 | JSONTEST_ASSERT_STRING_EQUAL(root["abc"].asString(), uni); |
| 1851 | } |
| 1852 | |
| 1853 | JSONTEST_FIXTURE_LOCAL(ValueTest, CommentBefore) { |
| 1854 | Json::Value val; // fill val |
| 1855 | val.setComment(Json::String("// this comment should appear before"), |
| 1856 | Json::commentBefore); |
| 1857 | Json::StreamWriterBuilder wbuilder; |
| 1858 | wbuilder.settings_["commentStyle"] = "All"; |
| 1859 | { |
| 1860 | char const expected[] = "// this comment should appear before\nnull"; |
| 1861 | Json::String result = Json::writeString(wbuilder, val); |
| 1862 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 1863 | Json::String res2 = val.toStyledString(); |
| 1864 | Json::String exp2 = "\n"; |
| 1865 | exp2 += expected; |
| 1866 | exp2 += "\n"; |
| 1867 | JSONTEST_ASSERT_STRING_EQUAL(exp2, res2); |
| 1868 | } |
| 1869 | Json::Value other = "hello"; |
| 1870 | val.swapPayload(other); |
| 1871 | { |
| 1872 | char const expected[] = "// this comment should appear before\n\"hello\""; |
| 1873 | Json::String result = Json::writeString(wbuilder, val); |
| 1874 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 1875 | Json::String res2 = val.toStyledString(); |
| 1876 | Json::String exp2 = "\n"; |
| 1877 | exp2 += expected; |
| 1878 | exp2 += "\n"; |
| 1879 | JSONTEST_ASSERT_STRING_EQUAL(exp2, res2); |
| 1880 | JSONTEST_ASSERT_STRING_EQUAL("null\n", other.toStyledString()); |
| 1881 | } |
| 1882 | val = "hello"; |
| 1883 | // val.setComment("// this comment should appear before", |
| 1884 | // Json::CommentPlacement::commentBefore); Assignment over-writes comments. |
| 1885 | { |
| 1886 | char const expected[] = "\"hello\""; |
| 1887 | Json::String result = Json::writeString(wbuilder, val); |
| 1888 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 1889 | Json::String res2 = val.toStyledString(); |
| 1890 | Json::String exp2; |
| 1891 | exp2 += expected; |
| 1892 | exp2 += "\n"; |
| 1893 | JSONTEST_ASSERT_STRING_EQUAL(exp2, res2); |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | JSONTEST_FIXTURE_LOCAL(ValueTest, zeroes) { |
| 1898 | char const cstr[] = "h\0i"; |
| 1899 | Json::String binary(cstr, sizeof(cstr)); // include trailing 0 |
| 1900 | JSONTEST_ASSERT_EQUAL(4U, binary.length()); |
| 1901 | Json::StreamWriterBuilder b; |
| 1902 | { |
| 1903 | Json::Value root; |
| 1904 | root = binary; |
| 1905 | JSONTEST_ASSERT_STRING_EQUAL(binary, root.asString()); |
| 1906 | } |
| 1907 | { |
| 1908 | char const top[] = "top"; |
| 1909 | Json::Value root; |
| 1910 | root[top] = binary; |
| 1911 | JSONTEST_ASSERT_STRING_EQUAL(binary, root[top].asString()); |
| 1912 | Json::Value removed; |
| 1913 | bool did; |
| 1914 | did = root.removeMember(top, top + sizeof(top) - 1U, &removed); |
| 1915 | JSONTEST_ASSERT(did); |
| 1916 | JSONTEST_ASSERT_STRING_EQUAL(binary, removed.asString()); |
| 1917 | did = root.removeMember(top, top + sizeof(top) - 1U, &removed); |
| 1918 | JSONTEST_ASSERT(!did); |
| 1919 | JSONTEST_ASSERT_STRING_EQUAL(binary, removed.asString()); // still |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | JSONTEST_FIXTURE_LOCAL(ValueTest, zeroesInKeys) { |
| 1924 | char const cstr[] = "h\0i"; |
| 1925 | Json::String binary(cstr, sizeof(cstr)); // include trailing 0 |
| 1926 | JSONTEST_ASSERT_EQUAL(4U, binary.length()); |
| 1927 | { |
| 1928 | Json::Value root; |
| 1929 | root[binary] = "there"; |
| 1930 | JSONTEST_ASSERT_STRING_EQUAL("there", root[binary].asString()); |
| 1931 | JSONTEST_ASSERT(!root.isMember("h")); |
| 1932 | JSONTEST_ASSERT(root.isMember(binary)); |
| 1933 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1934 | "there", root.get(binary, Json::Value::nullSingleton()).asString()); |
| 1935 | Json::Value removed; |
| 1936 | bool did; |
| 1937 | did = root.removeMember(binary.data(), binary.data() + binary.length(), |
| 1938 | &removed); |
| 1939 | JSONTEST_ASSERT(did); |
| 1940 | JSONTEST_ASSERT_STRING_EQUAL("there", removed.asString()); |
| 1941 | did = root.removeMember(binary.data(), binary.data() + binary.length(), |
| 1942 | &removed); |
| 1943 | JSONTEST_ASSERT(!did); |
| 1944 | JSONTEST_ASSERT_STRING_EQUAL("there", removed.asString()); // still |
| 1945 | JSONTEST_ASSERT(!root.isMember(binary)); |
| 1946 | JSONTEST_ASSERT_STRING_EQUAL( |
| 1947 | "", root.get(binary, Json::Value::nullSingleton()).asString()); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | JSONTEST_FIXTURE_LOCAL(ValueTest, specialFloats) { |
| 1952 | Json::StreamWriterBuilder b; |
| 1953 | b.settings_["useSpecialFloats"] = true; |
| 1954 | |
| 1955 | Json::Value v = std::numeric_limits<double>::quiet_NaN(); |
| 1956 | Json::String expected = "NaN"; |
| 1957 | Json::String result = Json::writeString(b, v); |
| 1958 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 1959 | |
| 1960 | v = std::numeric_limits<double>::infinity(); |
| 1961 | expected = "Infinity"; |
| 1962 | result = Json::writeString(b, v); |
| 1963 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 1964 | |
| 1965 | v = -std::numeric_limits<double>::infinity(); |
| 1966 | expected = "-Infinity"; |
| 1967 | result = Json::writeString(b, v); |
| 1968 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 1969 | } |
| 1970 | |
| 1971 | JSONTEST_FIXTURE_LOCAL(ValueTest, precision) { |
| 1972 | Json::StreamWriterBuilder b; |
| 1973 | b.settings_["precision"] = 5; |
| 1974 | |
| 1975 | Json::Value v = 100.0 / 3; |
| 1976 | Json::String expected = "33.333"; |
| 1977 | Json::String result = Json::writeString(b, v); |
| 1978 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 1979 | |
| 1980 | v = 0.25000000; |
| 1981 | expected = "0.25"; |
| 1982 | result = Json::writeString(b, v); |
| 1983 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 1984 | |
| 1985 | v = 0.2563456; |
| 1986 | expected = "0.25635"; |
| 1987 | result = Json::writeString(b, v); |
| 1988 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 1989 | |
| 1990 | b.settings_["precision"] = 1; |
| 1991 | expected = "0.3"; |
| 1992 | result = Json::writeString(b, v); |
| 1993 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 1994 | |
| 1995 | b.settings_["precision"] = 17; |
| 1996 | v = 1234857476305.256345694873740545068; |
| 1997 | expected = "1234857476305.2563"; |
| 1998 | result = Json::writeString(b, v); |
| 1999 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2000 | |
| 2001 | b.settings_["precision"] = 24; |
| 2002 | v = 0.256345694873740545068; |
| 2003 | expected = "0.25634569487374054"; |
| 2004 | result = Json::writeString(b, v); |
| 2005 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2006 | |
| 2007 | b.settings_["precision"] = 5; |
| 2008 | b.settings_["precisionType"] = "decimal"; |
| 2009 | v = 0.256345694873740545068; |
| 2010 | expected = "0.25635"; |
| 2011 | result = Json::writeString(b, v); |
| 2012 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2013 | |
| 2014 | b.settings_["precision"] = 1; |
| 2015 | b.settings_["precisionType"] = "decimal"; |
| 2016 | v = 0.256345694873740545068; |
| 2017 | expected = "0.3"; |
| 2018 | result = Json::writeString(b, v); |
| 2019 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2020 | |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 2021 | b.settings_["precision"] = 0; |
| 2022 | b.settings_["precisionType"] = "decimal"; |
| 2023 | v = 123.56345694873740545068; |
| 2024 | expected = "124"; |
| 2025 | result = Json::writeString(b, v); |
| 2026 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2027 | |
| 2028 | b.settings_["precision"] = 1; |
| 2029 | b.settings_["precisionType"] = "decimal"; |
| 2030 | v = 1230.001; |
| 2031 | expected = "1230.0"; |
| 2032 | result = Json::writeString(b, v); |
| 2033 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2034 | |
| 2035 | b.settings_["precision"] = 0; |
| 2036 | b.settings_["precisionType"] = "decimal"; |
| 2037 | v = 1230.001; |
| 2038 | expected = "1230"; |
| 2039 | result = Json::writeString(b, v); |
| 2040 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2041 | |
| 2042 | b.settings_["precision"] = 0; |
| 2043 | b.settings_["precisionType"] = "decimal"; |
| 2044 | v = 1231.5; |
| 2045 | expected = "1232"; |
| 2046 | result = Json::writeString(b, v); |
| 2047 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2048 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 2049 | b.settings_["precision"] = 10; |
| 2050 | b.settings_["precisionType"] = "decimal"; |
| 2051 | v = 0.23300000; |
| 2052 | expected = "0.233"; |
| 2053 | result = Json::writeString(b, v); |
| 2054 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2055 | } |
| 2056 | JSONTEST_FIXTURE_LOCAL(ValueTest, searchValueByPath) { |
| 2057 | Json::Value root, subroot; |
| 2058 | root["property1"][0] = 0; |
| 2059 | root["property1"][1] = 1; |
| 2060 | subroot["object"] = "object"; |
| 2061 | root["property2"] = subroot; |
| 2062 | |
| 2063 | const Json::Value defaultValue("error"); |
| 2064 | Json::FastWriter writer; |
| 2065 | |
| 2066 | { |
| 2067 | const Json::String expected("{" |
| 2068 | "\"property1\":[0,1]," |
| 2069 | "\"property2\":{\"object\":\"object\"}" |
| 2070 | "}\n"); |
| 2071 | Json::String outcome = writer.write(root); |
| 2072 | JSONTEST_ASSERT_STRING_EQUAL(expected, outcome); |
| 2073 | |
| 2074 | // Array member exists. |
| 2075 | const Json::Path path1(".property1.[%]", 1); |
| 2076 | Json::Value result = path1.resolve(root); |
| 2077 | JSONTEST_ASSERT_EQUAL(Json::Value(1), result); |
| 2078 | result = path1.resolve(root, defaultValue); |
| 2079 | JSONTEST_ASSERT_EQUAL(Json::Value(1), result); |
| 2080 | |
| 2081 | // Array member does not exist. |
| 2082 | const Json::Path path2(".property1.[2]"); |
| 2083 | result = path2.resolve(root); |
| 2084 | JSONTEST_ASSERT_EQUAL(Json::nullValue, result); |
| 2085 | result = path2.resolve(root, defaultValue); |
| 2086 | JSONTEST_ASSERT_EQUAL(defaultValue, result); |
| 2087 | |
| 2088 | // Access array path form error |
| 2089 | const Json::Path path3(".property1.0"); |
| 2090 | result = path3.resolve(root); |
| 2091 | JSONTEST_ASSERT_EQUAL(Json::nullValue, result); |
| 2092 | result = path3.resolve(root, defaultValue); |
| 2093 | JSONTEST_ASSERT_EQUAL(defaultValue, result); |
| 2094 | |
| 2095 | // Object member exists. |
| 2096 | const Json::Path path4(".property2.%", "object"); |
| 2097 | result = path4.resolve(root); |
| 2098 | JSONTEST_ASSERT_EQUAL(Json::Value("object"), result); |
| 2099 | result = path4.resolve(root, defaultValue); |
| 2100 | JSONTEST_ASSERT_EQUAL(Json::Value("object"), result); |
| 2101 | |
| 2102 | // Object member does not exist. |
| 2103 | const Json::Path path5(".property2.hello"); |
| 2104 | result = path5.resolve(root); |
| 2105 | JSONTEST_ASSERT_EQUAL(Json::nullValue, result); |
| 2106 | result = path5.resolve(root, defaultValue); |
| 2107 | JSONTEST_ASSERT_EQUAL(defaultValue, result); |
| 2108 | |
| 2109 | // Access object path form error |
| 2110 | const Json::Path path6(".property2.[0]"); |
| 2111 | result = path5.resolve(root); |
| 2112 | JSONTEST_ASSERT_EQUAL(Json::nullValue, result); |
| 2113 | result = path6.resolve(root, defaultValue); |
| 2114 | JSONTEST_ASSERT_EQUAL(defaultValue, result); |
| 2115 | |
| 2116 | // resolve will not change the value |
| 2117 | outcome = writer.write(root); |
| 2118 | JSONTEST_ASSERT_STRING_EQUAL(expected, outcome); |
| 2119 | } |
| 2120 | { |
| 2121 | const Json::String expected("{" |
| 2122 | "\"property1\":[0,1,null]," |
| 2123 | "\"property2\":{" |
| 2124 | "\"hello\":null," |
| 2125 | "\"object\":\"object\"}}\n"); |
| 2126 | Json::Path path1(".property1.[%]", 2); |
| 2127 | Json::Value& value1 = path1.make(root); |
| 2128 | JSONTEST_ASSERT_EQUAL(Json::nullValue, value1); |
| 2129 | |
| 2130 | Json::Path path2(".property2.%", "hello"); |
| 2131 | Json::Value& value2 = path2.make(root); |
| 2132 | JSONTEST_ASSERT_EQUAL(Json::nullValue, value2); |
| 2133 | |
| 2134 | // make will change the value |
| 2135 | const Json::String outcome = writer.write(root); |
| 2136 | JSONTEST_ASSERT_STRING_EQUAL(expected, outcome); |
| 2137 | } |
| 2138 | } |
| 2139 | struct FastWriterTest : JsonTest::TestCase {}; |
| 2140 | |
| 2141 | JSONTEST_FIXTURE_LOCAL(FastWriterTest, dropNullPlaceholders) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 2142 | Json::FastWriter writer; |
| 2143 | Json::Value nullValue; |
| 2144 | JSONTEST_ASSERT(writer.write(nullValue) == "null\n"); |
| 2145 | |
| 2146 | writer.dropNullPlaceholders(); |
| 2147 | JSONTEST_ASSERT(writer.write(nullValue) == "\n"); |
| 2148 | } |
| 2149 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 2150 | JSONTEST_FIXTURE_LOCAL(FastWriterTest, enableYAMLCompatibility) { |
| 2151 | Json::FastWriter writer; |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 2152 | Json::Value root; |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 2153 | root["hello"] = "world"; |
| 2154 | |
| 2155 | JSONTEST_ASSERT(writer.write(root) == "{\"hello\":\"world\"}\n"); |
| 2156 | |
| 2157 | writer.enableYAMLCompatibility(); |
| 2158 | JSONTEST_ASSERT(writer.write(root) == "{\"hello\": \"world\"}\n"); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 2159 | } |
| 2160 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 2161 | JSONTEST_FIXTURE_LOCAL(FastWriterTest, omitEndingLineFeed) { |
| 2162 | Json::FastWriter writer; |
| 2163 | Json::Value nullValue; |
| 2164 | |
| 2165 | JSONTEST_ASSERT(writer.write(nullValue) == "null\n"); |
| 2166 | |
| 2167 | writer.omitEndingLineFeed(); |
| 2168 | JSONTEST_ASSERT(writer.write(nullValue) == "null"); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 2169 | } |
| 2170 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 2171 | JSONTEST_FIXTURE_LOCAL(FastWriterTest, writeNumericValue) { |
| 2172 | Json::FastWriter writer; |
| 2173 | const Json::String expected("{" |
| 2174 | "\"emptyValue\":null," |
| 2175 | "\"false\":false," |
| 2176 | "\"null\":\"null\"," |
| 2177 | "\"number\":-6200000000000000.0," |
| 2178 | "\"real\":1.256," |
| 2179 | "\"uintValue\":17" |
| 2180 | "}\n"); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 2181 | Json::Value root; |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 2182 | root["emptyValue"] = Json::nullValue; |
| 2183 | root["false"] = false; |
| 2184 | root["null"] = "null"; |
| 2185 | root["number"] = -6.2e+15; |
| 2186 | root["real"] = 1.256; |
| 2187 | root["uintValue"] = Json::Value(17U); |
| 2188 | |
| 2189 | const Json::String result = writer.write(root); |
| 2190 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2191 | } |
| 2192 | |
| 2193 | JSONTEST_FIXTURE_LOCAL(FastWriterTest, writeArrays) { |
| 2194 | Json::FastWriter writer; |
| 2195 | const Json::String expected("{" |
| 2196 | "\"property1\":[\"value1\",\"value2\"]," |
| 2197 | "\"property2\":[]" |
| 2198 | "}\n"); |
| 2199 | Json::Value root; |
| 2200 | root["property1"][0] = "value1"; |
| 2201 | root["property1"][1] = "value2"; |
| 2202 | root["property2"] = Json::arrayValue; |
| 2203 | |
| 2204 | const Json::String result = writer.write(root); |
| 2205 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2206 | } |
| 2207 | |
| 2208 | JSONTEST_FIXTURE_LOCAL(FastWriterTest, writeNestedObjects) { |
| 2209 | Json::FastWriter writer; |
| 2210 | const Json::String expected("{" |
| 2211 | "\"object1\":{" |
| 2212 | "\"bool\":true," |
| 2213 | "\"nested\":123" |
| 2214 | "}," |
| 2215 | "\"object2\":{}" |
| 2216 | "}\n"); |
| 2217 | Json::Value root, child; |
| 2218 | child["nested"] = 123; |
| 2219 | child["bool"] = true; |
| 2220 | root["object1"] = child; |
| 2221 | root["object2"] = Json::objectValue; |
| 2222 | |
| 2223 | const Json::String result = writer.write(root); |
| 2224 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2225 | } |
| 2226 | |
| 2227 | struct StyledWriterTest : JsonTest::TestCase {}; |
| 2228 | |
| 2229 | JSONTEST_FIXTURE_LOCAL(StyledWriterTest, writeNumericValue) { |
| 2230 | Json::StyledWriter writer; |
| 2231 | const Json::String expected("{\n" |
| 2232 | " \"emptyValue\" : null,\n" |
| 2233 | " \"false\" : false,\n" |
| 2234 | " \"null\" : \"null\",\n" |
| 2235 | " \"number\" : -6200000000000000.0,\n" |
| 2236 | " \"real\" : 1.256,\n" |
| 2237 | " \"uintValue\" : 17\n" |
| 2238 | "}\n"); |
| 2239 | Json::Value root; |
| 2240 | root["emptyValue"] = Json::nullValue; |
| 2241 | root["false"] = false; |
| 2242 | root["null"] = "null"; |
| 2243 | root["number"] = -6.2e+15; |
| 2244 | root["real"] = 1.256; |
| 2245 | root["uintValue"] = Json::Value(17U); |
| 2246 | |
| 2247 | const Json::String result = writer.write(root); |
| 2248 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2249 | } |
| 2250 | |
| 2251 | JSONTEST_FIXTURE_LOCAL(StyledWriterTest, writeArrays) { |
| 2252 | Json::StyledWriter writer; |
| 2253 | const Json::String expected("{\n" |
| 2254 | " \"property1\" : [ \"value1\", \"value2\" ],\n" |
| 2255 | " \"property2\" : []\n" |
| 2256 | "}\n"); |
| 2257 | Json::Value root; |
| 2258 | root["property1"][0] = "value1"; |
| 2259 | root["property1"][1] = "value2"; |
| 2260 | root["property2"] = Json::arrayValue; |
| 2261 | |
| 2262 | const Json::String result = writer.write(root); |
| 2263 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2264 | } |
| 2265 | |
| 2266 | JSONTEST_FIXTURE_LOCAL(StyledWriterTest, writeNestedObjects) { |
| 2267 | Json::StyledWriter writer; |
| 2268 | const Json::String expected("{\n" |
| 2269 | " \"object1\" : {\n" |
| 2270 | " \"bool\" : true,\n" |
| 2271 | " \"nested\" : 123\n" |
| 2272 | " },\n" |
| 2273 | " \"object2\" : {}\n" |
| 2274 | "}\n"); |
| 2275 | Json::Value root, child; |
| 2276 | child["nested"] = 123; |
| 2277 | child["bool"] = true; |
| 2278 | root["object1"] = child; |
| 2279 | root["object2"] = Json::objectValue; |
| 2280 | |
| 2281 | const Json::String result = writer.write(root); |
| 2282 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2283 | } |
| 2284 | |
| 2285 | JSONTEST_FIXTURE_LOCAL(StyledWriterTest, multiLineArray) { |
| 2286 | Json::StyledWriter writer; |
| 2287 | { |
| 2288 | // Array member has more than 20 print effect rendering lines |
| 2289 | const Json::String expected("[\n " |
| 2290 | "0,\n 1,\n 2,\n " |
| 2291 | "3,\n 4,\n 5,\n " |
| 2292 | "6,\n 7,\n 8,\n " |
| 2293 | "9,\n 10,\n 11,\n " |
| 2294 | "12,\n 13,\n 14,\n " |
| 2295 | "15,\n 16,\n 17,\n " |
| 2296 | "18,\n 19,\n 20\n]\n"); |
| 2297 | Json::Value root; |
| 2298 | for (Json::ArrayIndex i = 0; i < 21; i++) |
| 2299 | root[i] = i; |
| 2300 | const Json::String result = writer.write(root); |
| 2301 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2302 | } |
| 2303 | { |
| 2304 | // Array members do not exceed 21 print effects to render a single line |
| 2305 | const Json::String expected("[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]\n"); |
| 2306 | Json::Value root; |
| 2307 | for (Json::ArrayIndex i = 0; i < 10; i++) |
| 2308 | root[i] = i; |
| 2309 | const Json::String result = writer.write(root); |
| 2310 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | JSONTEST_FIXTURE_LOCAL(StyledWriterTest, writeValueWithComment) { |
| 2315 | Json::StyledWriter writer; |
| 2316 | { |
| 2317 | const Json::String expected("\n//commentBeforeValue\n\"hello\"\n"); |
| 2318 | Json::Value root = "hello"; |
| 2319 | root.setComment(Json::String("//commentBeforeValue"), Json::commentBefore); |
| 2320 | const Json::String result = writer.write(root); |
| 2321 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2322 | } |
| 2323 | { |
| 2324 | const Json::String expected("\"hello\" //commentAfterValueOnSameLine\n"); |
| 2325 | Json::Value root = "hello"; |
| 2326 | root.setComment(Json::String("//commentAfterValueOnSameLine"), |
| 2327 | Json::commentAfterOnSameLine); |
| 2328 | const Json::String result = writer.write(root); |
| 2329 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2330 | } |
| 2331 | { |
| 2332 | const Json::String expected("\"hello\"\n//commentAfter\n\n"); |
| 2333 | Json::Value root = "hello"; |
| 2334 | root.setComment(Json::String("//commentAfter"), Json::commentAfter); |
| 2335 | const Json::String result = writer.write(root); |
| 2336 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | struct StyledStreamWriterTest : JsonTest::TestCase {}; |
| 2341 | |
| 2342 | JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest, writeNumericValue) { |
| 2343 | Json::StyledStreamWriter writer; |
| 2344 | const Json::String expected("{\n" |
| 2345 | "\t\"emptyValue\" : null,\n" |
| 2346 | "\t\"false\" : false,\n" |
| 2347 | "\t\"null\" : \"null\",\n" |
| 2348 | "\t\"number\" : -6200000000000000.0,\n" |
| 2349 | "\t\"real\" : 1.256,\n" |
| 2350 | "\t\"uintValue\" : 17\n" |
| 2351 | "}\n"); |
| 2352 | |
| 2353 | Json::Value root; |
| 2354 | root["emptyValue"] = Json::nullValue; |
| 2355 | root["false"] = false; |
| 2356 | root["null"] = "null"; |
| 2357 | root["number"] = -6.2e+15; // big float number |
| 2358 | root["real"] = 1.256; // float number |
| 2359 | root["uintValue"] = Json::Value(17U); |
| 2360 | |
| 2361 | Json::OStringStream sout; |
| 2362 | writer.write(sout, root); |
| 2363 | const Json::String result = sout.str(); |
| 2364 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2365 | } |
| 2366 | |
| 2367 | JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest, writeArrays) { |
| 2368 | Json::StyledStreamWriter writer; |
| 2369 | const Json::String expected("{\n" |
| 2370 | "\t\"property1\" : [ \"value1\", \"value2\" ],\n" |
| 2371 | "\t\"property2\" : []\n" |
| 2372 | "}\n"); |
| 2373 | Json::Value root; |
| 2374 | root["property1"][0] = "value1"; |
| 2375 | root["property1"][1] = "value2"; |
| 2376 | root["property2"] = Json::arrayValue; |
| 2377 | |
| 2378 | Json::OStringStream sout; |
| 2379 | writer.write(sout, root); |
| 2380 | const Json::String result = sout.str(); |
| 2381 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2382 | } |
| 2383 | |
| 2384 | JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest, writeNestedObjects) { |
| 2385 | Json::StyledStreamWriter writer; |
| 2386 | const Json::String expected("{\n" |
| 2387 | "\t\"object1\" : \n" |
| 2388 | "\t" |
| 2389 | "{\n" |
| 2390 | "\t\t\"bool\" : true,\n" |
| 2391 | "\t\t\"nested\" : 123\n" |
| 2392 | "\t},\n" |
| 2393 | "\t\"object2\" : {}\n" |
| 2394 | "}\n"); |
| 2395 | Json::Value root, child; |
| 2396 | child["nested"] = 123; |
| 2397 | child["bool"] = true; |
| 2398 | root["object1"] = child; |
| 2399 | root["object2"] = Json::objectValue; |
| 2400 | |
| 2401 | Json::OStringStream sout; |
| 2402 | writer.write(sout, root); |
| 2403 | const Json::String result = sout.str(); |
| 2404 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2405 | } |
| 2406 | |
| 2407 | JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest, multiLineArray) { |
| 2408 | { |
| 2409 | // Array member has more than 20 print effect rendering lines |
| 2410 | const Json::String expected("[\n\t0," |
| 2411 | "\n\t1," |
| 2412 | "\n\t2," |
| 2413 | "\n\t3," |
| 2414 | "\n\t4," |
| 2415 | "\n\t5," |
| 2416 | "\n\t6," |
| 2417 | "\n\t7," |
| 2418 | "\n\t8," |
| 2419 | "\n\t9," |
| 2420 | "\n\t10," |
| 2421 | "\n\t11," |
| 2422 | "\n\t12," |
| 2423 | "\n\t13," |
| 2424 | "\n\t14," |
| 2425 | "\n\t15," |
| 2426 | "\n\t16," |
| 2427 | "\n\t17," |
| 2428 | "\n\t18," |
| 2429 | "\n\t19," |
| 2430 | "\n\t20\n]\n"); |
| 2431 | Json::StyledStreamWriter writer; |
| 2432 | Json::Value root; |
| 2433 | for (Json::ArrayIndex i = 0; i < 21; i++) |
| 2434 | root[i] = i; |
| 2435 | Json::OStringStream sout; |
| 2436 | writer.write(sout, root); |
| 2437 | const Json::String result = sout.str(); |
| 2438 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2439 | } |
| 2440 | { |
| 2441 | Json::StyledStreamWriter writer; |
| 2442 | // Array members do not exceed 21 print effects to render a single line |
| 2443 | const Json::String expected("[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]\n"); |
| 2444 | Json::Value root; |
| 2445 | for (Json::ArrayIndex i = 0; i < 10; i++) |
| 2446 | root[i] = i; |
| 2447 | Json::OStringStream sout; |
| 2448 | writer.write(sout, root); |
| 2449 | const Json::String result = sout.str(); |
| 2450 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | JSONTEST_FIXTURE_LOCAL(StyledStreamWriterTest, writeValueWithComment) { |
| 2455 | Json::StyledStreamWriter writer("\t"); |
| 2456 | { |
| 2457 | const Json::String expected("//commentBeforeValue\n\"hello\"\n"); |
| 2458 | Json::Value root = "hello"; |
| 2459 | Json::OStringStream sout; |
| 2460 | root.setComment(Json::String("//commentBeforeValue"), Json::commentBefore); |
| 2461 | writer.write(sout, root); |
| 2462 | const Json::String result = sout.str(); |
| 2463 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2464 | } |
| 2465 | { |
| 2466 | const Json::String expected("\"hello\" //commentAfterValueOnSameLine\n"); |
| 2467 | Json::Value root = "hello"; |
| 2468 | Json::OStringStream sout; |
| 2469 | root.setComment(Json::String("//commentAfterValueOnSameLine"), |
| 2470 | Json::commentAfterOnSameLine); |
| 2471 | writer.write(sout, root); |
| 2472 | const Json::String result = sout.str(); |
| 2473 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2474 | } |
| 2475 | { |
| 2476 | const Json::String expected("\"hello\"\n//commentAfter\n"); |
| 2477 | Json::Value root = "hello"; |
| 2478 | Json::OStringStream sout; |
| 2479 | root.setComment(Json::String("//commentAfter"), Json::commentAfter); |
| 2480 | writer.write(sout, root); |
| 2481 | const Json::String result = sout.str(); |
| 2482 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | struct StreamWriterTest : JsonTest::TestCase {}; |
| 2487 | |
| 2488 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, writeNumericValue) { |
| 2489 | Json::StreamWriterBuilder writer; |
| 2490 | const Json::String expected("{\n" |
| 2491 | "\t\"emptyValue\" : null,\n" |
| 2492 | "\t\"false\" : false,\n" |
| 2493 | "\t\"null\" : \"null\",\n" |
| 2494 | "\t\"number\" : -6200000000000000.0,\n" |
| 2495 | "\t\"real\" : 1.256,\n" |
| 2496 | "\t\"uintValue\" : 17\n" |
| 2497 | "}"); |
| 2498 | Json::Value root; |
| 2499 | root["emptyValue"] = Json::nullValue; |
| 2500 | root["false"] = false; |
| 2501 | root["null"] = "null"; |
| 2502 | root["number"] = -6.2e+15; |
| 2503 | root["real"] = 1.256; |
| 2504 | root["uintValue"] = Json::Value(17U); |
| 2505 | |
| 2506 | const Json::String result = Json::writeString(writer, root); |
| 2507 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2508 | } |
| 2509 | |
| 2510 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, writeArrays) { |
| 2511 | Json::StreamWriterBuilder writer; |
| 2512 | const Json::String expected("{\n" |
| 2513 | "\t\"property1\" : \n" |
| 2514 | "\t[\n" |
| 2515 | "\t\t\"value1\",\n" |
| 2516 | "\t\t\"value2\"\n" |
| 2517 | "\t],\n" |
| 2518 | "\t\"property2\" : []\n" |
| 2519 | "}"); |
| 2520 | |
| 2521 | Json::Value root; |
| 2522 | root["property1"][0] = "value1"; |
| 2523 | root["property1"][1] = "value2"; |
| 2524 | root["property2"] = Json::arrayValue; |
| 2525 | |
| 2526 | const Json::String result = Json::writeString(writer, root); |
| 2527 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2528 | } |
| 2529 | |
| 2530 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, writeNestedObjects) { |
| 2531 | Json::StreamWriterBuilder writer; |
| 2532 | const Json::String expected("{\n" |
| 2533 | "\t\"object1\" : \n" |
| 2534 | "\t{\n" |
| 2535 | "\t\t\"bool\" : true,\n" |
| 2536 | "\t\t\"nested\" : 123\n" |
| 2537 | "\t},\n" |
| 2538 | "\t\"object2\" : {}\n" |
| 2539 | "}"); |
| 2540 | |
| 2541 | Json::Value root, child; |
| 2542 | child["nested"] = 123; |
| 2543 | child["bool"] = true; |
| 2544 | root["object1"] = child; |
| 2545 | root["object2"] = Json::objectValue; |
| 2546 | |
| 2547 | const Json::String result = Json::writeString(writer, root); |
| 2548 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2549 | } |
| 2550 | |
| 2551 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, multiLineArray) { |
| 2552 | Json::StreamWriterBuilder wb; |
| 2553 | wb.settings_["commentStyle"] = "None"; |
| 2554 | { |
| 2555 | // When wb.settings_["commentStyle"] = "None", the effect of |
| 2556 | // printing multiple lines will be displayed when there are |
| 2557 | // more than 20 array members. |
| 2558 | const Json::String expected("[\n\t0," |
| 2559 | "\n\t1," |
| 2560 | "\n\t2," |
| 2561 | "\n\t3," |
| 2562 | "\n\t4," |
| 2563 | "\n\t5," |
| 2564 | "\n\t6," |
| 2565 | "\n\t7," |
| 2566 | "\n\t8," |
| 2567 | "\n\t9," |
| 2568 | "\n\t10," |
| 2569 | "\n\t11," |
| 2570 | "\n\t12," |
| 2571 | "\n\t13," |
| 2572 | "\n\t14," |
| 2573 | "\n\t15," |
| 2574 | "\n\t16," |
| 2575 | "\n\t17," |
| 2576 | "\n\t18," |
| 2577 | "\n\t19," |
| 2578 | "\n\t20\n]"); |
| 2579 | Json::Value root; |
| 2580 | for (Json::ArrayIndex i = 0; i < 21; i++) |
| 2581 | root[i] = i; |
| 2582 | const Json::String result = Json::writeString(wb, root); |
| 2583 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2584 | } |
| 2585 | { |
| 2586 | // Array members do not exceed 21 print effects to render a single line |
| 2587 | const Json::String expected("[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]"); |
| 2588 | Json::Value root; |
| 2589 | for (Json::ArrayIndex i = 0; i < 10; i++) |
| 2590 | root[i] = i; |
| 2591 | const Json::String result = Json::writeString(wb, root); |
| 2592 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, dropNullPlaceholders) { |
| 2597 | Json::StreamWriterBuilder b; |
| 2598 | Json::Value nullValue; |
| 2599 | b.settings_["dropNullPlaceholders"] = false; |
| 2600 | JSONTEST_ASSERT(Json::writeString(b, nullValue) == "null"); |
| 2601 | b.settings_["dropNullPlaceholders"] = true; |
| 2602 | JSONTEST_ASSERT(Json::writeString(b, nullValue).empty()); |
| 2603 | } |
| 2604 | |
| 2605 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, enableYAMLCompatibility) { |
| 2606 | Json::StreamWriterBuilder b; |
| 2607 | Json::Value root; |
| 2608 | root["hello"] = "world"; |
| 2609 | |
| 2610 | b.settings_["indentation"] = ""; |
| 2611 | JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\":\"world\"}"); |
| 2612 | |
| 2613 | b.settings_["enableYAMLCompatibility"] = true; |
| 2614 | JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\": \"world\"}"); |
| 2615 | |
| 2616 | b.settings_["enableYAMLCompatibility"] = false; |
| 2617 | JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\":\"world\"}"); |
| 2618 | } |
| 2619 | |
| 2620 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, indentation) { |
| 2621 | Json::StreamWriterBuilder b; |
| 2622 | Json::Value root; |
| 2623 | root["hello"] = "world"; |
| 2624 | |
| 2625 | b.settings_["indentation"] = ""; |
| 2626 | JSONTEST_ASSERT(Json::writeString(b, root) == "{\"hello\":\"world\"}"); |
| 2627 | |
| 2628 | b.settings_["indentation"] = "\t"; |
| 2629 | JSONTEST_ASSERT(Json::writeString(b, root) == |
| 2630 | "{\n\t\"hello\" : \"world\"\n}"); |
| 2631 | } |
| 2632 | |
| 2633 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, writeZeroes) { |
| 2634 | Json::String binary("hi", 3); // include trailing 0 |
| 2635 | JSONTEST_ASSERT_EQUAL(3, binary.length()); |
| 2636 | Json::String expected(R"("hi\u0000")"); // unicoded zero |
| 2637 | Json::StreamWriterBuilder b; |
| 2638 | { |
| 2639 | Json::Value root; |
| 2640 | root = binary; |
| 2641 | JSONTEST_ASSERT_STRING_EQUAL(binary, root.asString()); |
| 2642 | Json::String out = Json::writeString(b, root); |
| 2643 | JSONTEST_ASSERT_EQUAL(expected.size(), out.size()); |
| 2644 | JSONTEST_ASSERT_STRING_EQUAL(expected, out); |
| 2645 | } |
| 2646 | { |
| 2647 | Json::Value root; |
| 2648 | root["top"] = binary; |
| 2649 | JSONTEST_ASSERT_STRING_EQUAL(binary, root["top"].asString()); |
| 2650 | Json::String out = Json::writeString(b, root["top"]); |
| 2651 | JSONTEST_ASSERT_STRING_EQUAL(expected, out); |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, unicode) { |
| 2656 | // Create a Json value containing UTF-8 string with some chars that need |
| 2657 | // escape (tab,newline). |
| 2658 | Json::Value root; |
| 2659 | root["test"] = "\t\n\xF0\x91\xA2\xA1\x3D\xC4\xB3\xF0\x9B\x84\x9B\xEF\xBD\xA7"; |
| 2660 | |
| 2661 | Json::StreamWriterBuilder b; |
| 2662 | |
| 2663 | // Default settings - should be unicode escaped. |
| 2664 | JSONTEST_ASSERT(Json::writeString(b, root) == |
| 2665 | "{\n\t\"test\" : " |
| 2666 | "\"\\t\\n\\ud806\\udca1=\\u0133\\ud82c\\udd1b\\uff67\"\n}"); |
| 2667 | |
| 2668 | b.settings_["emitUTF8"] = true; |
| 2669 | |
| 2670 | // Should not be unicode escaped. |
| 2671 | JSONTEST_ASSERT( |
| 2672 | Json::writeString(b, root) == |
| 2673 | "{\n\t\"test\" : " |
| 2674 | "\"\\t\\n\xF0\x91\xA2\xA1=\xC4\xB3\xF0\x9B\x84\x9B\xEF\xBD\xA7\"\n}"); |
| 2675 | |
| 2676 | b.settings_["emitUTF8"] = false; |
| 2677 | |
| 2678 | // Should be unicode escaped. |
| 2679 | JSONTEST_ASSERT(Json::writeString(b, root) == |
| 2680 | "{\n\t\"test\" : " |
| 2681 | "\"\\t\\n\\ud806\\udca1=\\u0133\\ud82c\\udd1b\\uff67\"\n}"); |
| 2682 | } |
| 2683 | |
| 2684 | // Control chars should be escaped regardless of UTF-8 input encoding. |
| 2685 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, escapeControlCharacters) { |
| 2686 | auto uEscape = [](unsigned ch) { |
| 2687 | static const char h[] = "0123456789abcdef"; |
| 2688 | std::string r = "\\u"; |
| 2689 | r += h[(ch >> (3 * 4)) & 0xf]; |
| 2690 | r += h[(ch >> (2 * 4)) & 0xf]; |
| 2691 | r += h[(ch >> (1 * 4)) & 0xf]; |
| 2692 | r += h[(ch >> (0 * 4)) & 0xf]; |
| 2693 | return r; |
| 2694 | }; |
| 2695 | auto shortEscape = [](unsigned ch) -> const char* { |
| 2696 | switch (ch) { |
| 2697 | case '\"': |
| 2698 | return "\\\""; |
| 2699 | case '\\': |
| 2700 | return "\\\\"; |
| 2701 | case '\b': |
| 2702 | return "\\b"; |
| 2703 | case '\f': |
| 2704 | return "\\f"; |
| 2705 | case '\n': |
| 2706 | return "\\n"; |
| 2707 | case '\r': |
| 2708 | return "\\r"; |
| 2709 | case '\t': |
| 2710 | return "\\t"; |
| 2711 | default: |
| 2712 | return nullptr; |
| 2713 | } |
| 2714 | }; |
| 2715 | |
| 2716 | Json::StreamWriterBuilder b; |
| 2717 | |
| 2718 | for (bool emitUTF8 : {true, false}) { |
| 2719 | b.settings_["emitUTF8"] = emitUTF8; |
| 2720 | |
| 2721 | for (unsigned i = 0; i != 0x100; ++i) { |
| 2722 | if (!emitUTF8 && i >= 0x80) |
| 2723 | break; // The algorithm would try to parse UTF-8, so stop here. |
| 2724 | |
| 2725 | std::string raw({static_cast<char>(i)}); |
| 2726 | std::string esc = raw; |
| 2727 | if (i < 0x20) |
| 2728 | esc = uEscape(i); |
| 2729 | if (const char* shEsc = shortEscape(i)) |
| 2730 | esc = shEsc; |
| 2731 | |
| 2732 | // std::cout << "emit=" << emitUTF8 << ", i=" << std::hex << i << std::dec |
| 2733 | // << std::endl; |
| 2734 | |
| 2735 | Json::Value root; |
| 2736 | root["test"] = raw; |
| 2737 | JSONTEST_ASSERT_STRING_EQUAL( |
| 2738 | std::string("{\n\t\"test\" : \"").append(esc).append("\"\n}"), |
| 2739 | Json::writeString(b, root)) |
| 2740 | << ", emit=" << emitUTF8 << ", i=" << i << ", raw=\"" << raw << "\"" |
| 2741 | << ", esc=\"" << esc << "\""; |
| 2742 | } |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | #ifdef _WIN32 |
| 2747 | JSONTEST_FIXTURE_LOCAL(StreamWriterTest, escapeTabCharacterWindows) { |
| 2748 | // Get the current locale before changing it |
| 2749 | std::string currentLocale = setlocale(LC_ALL, NULL); |
| 2750 | setlocale(LC_ALL, "English_United States.1252"); |
| 2751 | |
| 2752 | Json::Value root; |
| 2753 | root["test"] = "\tTabTesting\t"; |
| 2754 | |
| 2755 | Json::StreamWriterBuilder b; |
| 2756 | |
| 2757 | JSONTEST_ASSERT(Json::writeString(b, root) == "{\n\t\"test\" : " |
| 2758 | "\"\\tTabTesting\\t\"\n}"); |
| 2759 | |
| 2760 | b.settings_["emitUTF8"] = true; |
| 2761 | JSONTEST_ASSERT(Json::writeString(b, root) == "{\n\t\"test\" : " |
| 2762 | "\"\\tTabTesting\\t\"\n}"); |
| 2763 | |
| 2764 | b.settings_["emitUTF8"] = false; |
| 2765 | JSONTEST_ASSERT(Json::writeString(b, root) == "{\n\t\"test\" : " |
| 2766 | "\"\\tTabTesting\\t\"\n}"); |
| 2767 | |
| 2768 | // Restore the locale |
| 2769 | if (!currentLocale.empty()) |
| 2770 | setlocale(LC_ALL, currentLocale.c_str()); |
| 2771 | } |
| 2772 | #endif |
| 2773 | |
| 2774 | struct ReaderTest : JsonTest::TestCase { |
| 2775 | void setStrictMode() { |
| 2776 | reader = std::unique_ptr<Json::Reader>( |
| 2777 | new Json::Reader(Json::Features{}.strictMode())); |
| 2778 | } |
| 2779 | |
| 2780 | void setFeatures(Json::Features& features) { |
| 2781 | reader = std::unique_ptr<Json::Reader>(new Json::Reader(features)); |
| 2782 | } |
| 2783 | |
| 2784 | void checkStructuredErrors( |
| 2785 | const std::vector<Json::Reader::StructuredError>& actual, |
| 2786 | const std::vector<Json::Reader::StructuredError>& expected) { |
| 2787 | JSONTEST_ASSERT_EQUAL(expected.size(), actual.size()); |
| 2788 | for (size_t i = 0; i < actual.size(); ++i) { |
| 2789 | const auto& a = actual[i]; |
| 2790 | const auto& e = expected[i]; |
| 2791 | JSONTEST_ASSERT_EQUAL(e.offset_start, a.offset_start) << i; |
| 2792 | JSONTEST_ASSERT_EQUAL(e.offset_limit, a.offset_limit) << i; |
| 2793 | JSONTEST_ASSERT_EQUAL(e.message, a.message) << i; |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | template <typename Input> void checkParse(Input&& input) { |
| 2798 | JSONTEST_ASSERT(reader->parse(input, root)); |
| 2799 | } |
| 2800 | |
| 2801 | template <typename Input> |
| 2802 | void |
| 2803 | checkParse(Input&& input, |
| 2804 | const std::vector<Json::Reader::StructuredError>& structured) { |
| 2805 | JSONTEST_ASSERT(!reader->parse(input, root)); |
| 2806 | checkStructuredErrors(reader->getStructuredErrors(), structured); |
| 2807 | } |
| 2808 | |
| 2809 | template <typename Input> |
| 2810 | void checkParse(Input&& input, |
| 2811 | const std::vector<Json::Reader::StructuredError>& structured, |
| 2812 | const std::string& formatted) { |
| 2813 | checkParse(input, structured); |
| 2814 | JSONTEST_ASSERT_EQUAL(formatted, reader->getFormattedErrorMessages()); |
| 2815 | } |
| 2816 | |
| 2817 | std::unique_ptr<Json::Reader> reader{new Json::Reader()}; |
| 2818 | Json::Value root; |
| 2819 | }; |
| 2820 | |
| 2821 | JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithNoErrors) { |
| 2822 | checkParse(R"({ "property" : "value" })"); |
| 2823 | } |
| 2824 | |
| 2825 | JSONTEST_FIXTURE_LOCAL(ReaderTest, parseObject) { |
| 2826 | checkParse(R"({"property"})", |
| 2827 | {{11, 12, "Missing ':' after object member name"}}, |
| 2828 | "* Line 1, Column 12\n Missing ':' after object member name\n"); |
| 2829 | checkParse( |
| 2830 | R"({"property" : "value" )", |
| 2831 | {{22, 22, "Missing ',' or '}' in object declaration"}}, |
| 2832 | "* Line 1, Column 23\n Missing ',' or '}' in object declaration\n"); |
| 2833 | checkParse(R"({"property" : "value", )", |
| 2834 | {{23, 23, "Missing '}' or object member name"}}, |
| 2835 | "* Line 1, Column 24\n Missing '}' or object member name\n"); |
| 2836 | } |
| 2837 | |
| 2838 | JSONTEST_FIXTURE_LOCAL(ReaderTest, parseArray) { |
| 2839 | checkParse( |
| 2840 | R"([ "value" )", {{10, 10, "Missing ',' or ']' in array declaration"}}, |
| 2841 | "* Line 1, Column 11\n Missing ',' or ']' in array declaration\n"); |
| 2842 | checkParse( |
| 2843 | R"([ "value1" "value2" ] )", |
| 2844 | {{11, 19, "Missing ',' or ']' in array declaration"}}, |
| 2845 | "* Line 1, Column 12\n Missing ',' or ']' in array declaration\n"); |
| 2846 | } |
| 2847 | |
| 2848 | JSONTEST_FIXTURE_LOCAL(ReaderTest, parseString) { |
| 2849 | checkParse(R"([ "\u8a2a" ])"); |
| 2850 | checkParse( |
| 2851 | R"([ "\ud801" ])", |
| 2852 | {{2, 10, |
| 2853 | "additional six characters expected to parse unicode surrogate " |
| 2854 | "pair."}}, |
| 2855 | "* Line 1, Column 3\n" |
| 2856 | " additional six characters expected to parse unicode surrogate pair.\n" |
| 2857 | "See Line 1, Column 10 for detail.\n"); |
| 2858 | checkParse(R"([ "\ud801\d1234" ])", |
| 2859 | {{2, 16, |
| 2860 | "expecting another \\u token to begin the " |
| 2861 | "second half of a unicode surrogate pair"}}, |
| 2862 | "* Line 1, Column 3\n" |
| 2863 | " expecting another \\u token to begin the " |
| 2864 | "second half of a unicode surrogate pair\n" |
| 2865 | "See Line 1, Column 12 for detail.\n"); |
| 2866 | checkParse(R"([ "\ua3t@" ])", |
| 2867 | {{2, 10, |
| 2868 | "Bad unicode escape sequence in string: " |
| 2869 | "hexadecimal digit expected."}}, |
| 2870 | "* Line 1, Column 3\n" |
| 2871 | " Bad unicode escape sequence in string: " |
| 2872 | "hexadecimal digit expected.\n" |
| 2873 | "See Line 1, Column 9 for detail.\n"); |
| 2874 | checkParse( |
| 2875 | R"([ "\ua3t" ])", |
| 2876 | {{2, 9, "Bad unicode escape sequence in string: four digits expected."}}, |
| 2877 | "* Line 1, Column 3\n" |
| 2878 | " Bad unicode escape sequence in string: four digits expected.\n" |
| 2879 | "See Line 1, Column 6 for detail.\n"); |
| 2880 | } |
| 2881 | |
| 2882 | JSONTEST_FIXTURE_LOCAL(ReaderTest, parseComment) { |
| 2883 | checkParse( |
| 2884 | R"({ /*commentBeforeValue*/ "property" : "value" }//commentAfterValue)" |
| 2885 | "\n"); |
| 2886 | checkParse(" true //comment1\n//comment2\r//comment3\r\n"); |
| 2887 | } |
| 2888 | |
| 2889 | JSONTEST_FIXTURE_LOCAL(ReaderTest, streamParseWithNoErrors) { |
| 2890 | std::string styled = R"({ "property" : "value" })"; |
| 2891 | std::istringstream iss(styled); |
| 2892 | checkParse(iss); |
| 2893 | } |
| 2894 | |
| 2895 | JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithNoErrorsTestingOffsets) { |
| 2896 | checkParse(R"({)" |
| 2897 | R"( "property" : ["value", "value2"],)" |
| 2898 | R"( "obj" : { "nested" : -6.2e+15, "bool" : true},)" |
| 2899 | R"( "null" : null,)" |
| 2900 | R"( "false" : false)" |
| 2901 | R"( })"); |
| 2902 | auto checkOffsets = [&](const Json::Value& v, int start, int limit) { |
| 2903 | JSONTEST_ASSERT_EQUAL(start, v.getOffsetStart()); |
| 2904 | JSONTEST_ASSERT_EQUAL(limit, v.getOffsetLimit()); |
| 2905 | }; |
| 2906 | checkOffsets(root, 0, 115); |
| 2907 | checkOffsets(root["property"], 15, 34); |
| 2908 | checkOffsets(root["property"][0], 16, 23); |
| 2909 | checkOffsets(root["property"][1], 25, 33); |
| 2910 | checkOffsets(root["obj"], 44, 81); |
| 2911 | checkOffsets(root["obj"]["nested"], 57, 65); |
| 2912 | checkOffsets(root["obj"]["bool"], 76, 80); |
| 2913 | checkOffsets(root["null"], 92, 96); |
| 2914 | checkOffsets(root["false"], 108, 113); |
| 2915 | } |
| 2916 | |
| 2917 | JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithOneError) { |
| 2918 | checkParse(R"({ "property" :: "value" })", |
| 2919 | {{14, 15, "Syntax error: value, object or array expected."}}, |
| 2920 | "* Line 1, Column 15\n Syntax error: value, object or array " |
| 2921 | "expected.\n"); |
| 2922 | checkParse("s", {{0, 1, "Syntax error: value, object or array expected."}}, |
| 2923 | "* Line 1, Column 1\n Syntax error: value, object or array " |
| 2924 | "expected.\n"); |
| 2925 | } |
| 2926 | |
| 2927 | JSONTEST_FIXTURE_LOCAL(ReaderTest, parseSpecialFloat) { |
| 2928 | checkParse(R"({ "a" : Infi })", |
| 2929 | {{8, 9, "Syntax error: value, object or array expected."}}, |
| 2930 | "* Line 1, Column 9\n Syntax error: value, object or array " |
| 2931 | "expected.\n"); |
| 2932 | checkParse(R"({ "a" : Infiniaa })", |
| 2933 | {{8, 9, "Syntax error: value, object or array expected."}}, |
| 2934 | "* Line 1, Column 9\n Syntax error: value, object or array " |
| 2935 | "expected.\n"); |
| 2936 | } |
| 2937 | |
| 2938 | JSONTEST_FIXTURE_LOCAL(ReaderTest, strictModeParseNumber) { |
| 2939 | setStrictMode(); |
| 2940 | checkParse( |
| 2941 | "123", |
| 2942 | {{0, 3, |
| 2943 | "A valid JSON document must be either an array or an object value."}}, |
| 2944 | "* Line 1, Column 1\n" |
| 2945 | " A valid JSON document must be either an array or an object value.\n"); |
| 2946 | } |
| 2947 | |
| 2948 | JSONTEST_FIXTURE_LOCAL(ReaderTest, parseChineseWithOneError) { |
| 2949 | checkParse(R"({ "pr)" |
| 2950 | u8"\u4f50\u85e4" // 佐藤 |
| 2951 | R"(erty" :: "value" })", |
| 2952 | {{18, 19, "Syntax error: value, object or array expected."}}, |
| 2953 | "* Line 1, Column 19\n Syntax error: value, object or array " |
| 2954 | "expected.\n"); |
| 2955 | } |
| 2956 | |
| 2957 | JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithDetailError) { |
| 2958 | checkParse(R"({ "property" : "v\alue" })", |
| 2959 | {{15, 23, "Bad escape sequence in string"}}, |
| 2960 | "* Line 1, Column 16\n" |
| 2961 | " Bad escape sequence in string\n" |
| 2962 | "See Line 1, Column 20 for detail.\n"); |
| 2963 | } |
| 2964 | |
| 2965 | JSONTEST_FIXTURE_LOCAL(ReaderTest, pushErrorTest) { |
| 2966 | checkParse(R"({ "AUTHOR" : 123 })"); |
| 2967 | if (!root["AUTHOR"].isString()) { |
| 2968 | JSONTEST_ASSERT( |
| 2969 | reader->pushError(root["AUTHOR"], "AUTHOR must be a string")); |
| 2970 | } |
| 2971 | JSONTEST_ASSERT_STRING_EQUAL(reader->getFormattedErrorMessages(), |
| 2972 | "* Line 1, Column 14\n" |
| 2973 | " AUTHOR must be a string\n"); |
| 2974 | |
| 2975 | checkParse(R"({ "AUTHOR" : 123 })"); |
| 2976 | if (!root["AUTHOR"].isString()) { |
| 2977 | JSONTEST_ASSERT(reader->pushError(root["AUTHOR"], "AUTHOR must be a string", |
| 2978 | root["AUTHOR"])); |
| 2979 | } |
| 2980 | JSONTEST_ASSERT_STRING_EQUAL(reader->getFormattedErrorMessages(), |
| 2981 | "* Line 1, Column 14\n" |
| 2982 | " AUTHOR must be a string\n" |
| 2983 | "See Line 1, Column 14 for detail.\n"); |
| 2984 | } |
| 2985 | |
| 2986 | JSONTEST_FIXTURE_LOCAL(ReaderTest, allowNumericKeysTest) { |
| 2987 | Json::Features features; |
| 2988 | features.allowNumericKeys_ = true; |
| 2989 | setFeatures(features); |
| 2990 | checkParse(R"({ 123 : "abc" })"); |
| 2991 | } |
| 2992 | |
| 2993 | struct CharReaderTest : JsonTest::TestCase {}; |
| 2994 | |
| 2995 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithNoErrors) { |
| 2996 | Json::CharReaderBuilder b; |
| 2997 | CharReaderPtr reader(b.newCharReader()); |
| 2998 | Json::String errs; |
| 2999 | Json::Value root; |
| 3000 | char const doc[] = R"({ "property" : "value" })"; |
| 3001 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3002 | JSONTEST_ASSERT(ok); |
| 3003 | JSONTEST_ASSERT(errs.empty()); |
| 3004 | } |
| 3005 | |
| 3006 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithNoErrorsTestingOffsets) { |
| 3007 | Json::CharReaderBuilder b; |
| 3008 | CharReaderPtr reader(b.newCharReader()); |
| 3009 | Json::String errs; |
| 3010 | Json::Value root; |
| 3011 | char const doc[] = "{ \"property\" : [\"value\", \"value2\"], \"obj\" : " |
| 3012 | "{ \"nested\" : -6.2e+15, \"num\" : +123, \"bool\" : " |
| 3013 | "true}, \"null\" : null, \"false\" : false }"; |
| 3014 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3015 | JSONTEST_ASSERT(ok); |
| 3016 | JSONTEST_ASSERT(errs.empty()); |
| 3017 | } |
| 3018 | |
| 3019 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseNumber) { |
| 3020 | Json::CharReaderBuilder b; |
| 3021 | CharReaderPtr reader(b.newCharReader()); |
| 3022 | Json::String errs; |
| 3023 | Json::Value root; |
| 3024 | { |
| 3025 | // if intvalue > threshold, treat the number as a double. |
| 3026 | // 21 digits |
| 3027 | char const doc[] = "[111111111111111111111]"; |
| 3028 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3029 | JSONTEST_ASSERT(ok); |
| 3030 | JSONTEST_ASSERT(errs.empty()); |
| 3031 | JSONTEST_ASSERT_EQUAL(1.1111111111111111e+020, root[0]); |
| 3032 | } |
| 3033 | } |
| 3034 | |
| 3035 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseString) { |
| 3036 | Json::CharReaderBuilder b; |
| 3037 | CharReaderPtr reader(b.newCharReader()); |
| 3038 | Json::Value root; |
| 3039 | Json::String errs; |
| 3040 | { |
| 3041 | char const doc[] = "[\"\"]"; |
| 3042 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3043 | JSONTEST_ASSERT(ok); |
| 3044 | JSONTEST_ASSERT(errs.empty()); |
| 3045 | JSONTEST_ASSERT_EQUAL("", root[0]); |
| 3046 | } |
| 3047 | { |
| 3048 | char const doc[] = R"(["\u8A2a"])"; |
| 3049 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3050 | JSONTEST_ASSERT(ok); |
| 3051 | JSONTEST_ASSERT(errs.empty()); |
| 3052 | JSONTEST_ASSERT_EQUAL(u8"\u8A2a", root[0].asString()); // "訪" |
| 3053 | } |
| 3054 | { |
| 3055 | char const doc[] = R"([ "\uD801" ])"; |
| 3056 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3057 | JSONTEST_ASSERT(!ok); |
| 3058 | JSONTEST_ASSERT(errs == "* Line 1, Column 3\n" |
| 3059 | " additional six characters expected to " |
| 3060 | "parse unicode surrogate pair.\n" |
| 3061 | "See Line 1, Column 10 for detail.\n"); |
| 3062 | } |
| 3063 | { |
| 3064 | char const doc[] = R"([ "\uD801\d1234" ])"; |
| 3065 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3066 | JSONTEST_ASSERT(!ok); |
| 3067 | JSONTEST_ASSERT(errs == "* Line 1, Column 3\n" |
| 3068 | " expecting another \\u token to begin the " |
| 3069 | "second half of a unicode surrogate pair\n" |
| 3070 | "See Line 1, Column 12 for detail.\n"); |
| 3071 | } |
| 3072 | { |
| 3073 | char const doc[] = R"([ "\ua3t@" ])"; |
| 3074 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3075 | JSONTEST_ASSERT(!ok); |
| 3076 | JSONTEST_ASSERT(errs == "* Line 1, Column 3\n" |
| 3077 | " Bad unicode escape sequence in string: " |
| 3078 | "hexadecimal digit expected.\n" |
| 3079 | "See Line 1, Column 9 for detail.\n"); |
| 3080 | } |
| 3081 | { |
| 3082 | char const doc[] = R"([ "\ua3t" ])"; |
| 3083 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3084 | JSONTEST_ASSERT(!ok); |
| 3085 | JSONTEST_ASSERT( |
| 3086 | errs == |
| 3087 | "* Line 1, Column 3\n" |
| 3088 | " Bad unicode escape sequence in string: four digits expected.\n" |
| 3089 | "See Line 1, Column 6 for detail.\n"); |
| 3090 | } |
| 3091 | { |
| 3092 | b.settings_["allowSingleQuotes"] = true; |
| 3093 | CharReaderPtr charreader(b.newCharReader()); |
| 3094 | char const doc[] = R"({'a': 'x\ty', "b":'x\\y'})"; |
| 3095 | bool ok = charreader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3096 | JSONTEST_ASSERT(ok); |
| 3097 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3098 | JSONTEST_ASSERT_EQUAL(2u, root.size()); |
| 3099 | JSONTEST_ASSERT_STRING_EQUAL("x\ty", root["a"].asString()); |
| 3100 | JSONTEST_ASSERT_STRING_EQUAL("x\\y", root["b"].asString()); |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseComment) { |
| 3105 | Json::CharReaderBuilder b; |
| 3106 | CharReaderPtr reader(b.newCharReader()); |
| 3107 | Json::Value root; |
| 3108 | Json::String errs; |
| 3109 | { |
| 3110 | char const doc[] = "//comment1\n { //comment2\n \"property\" :" |
| 3111 | " \"value\" //comment3\n } //comment4\n"; |
| 3112 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3113 | JSONTEST_ASSERT(ok); |
| 3114 | JSONTEST_ASSERT(errs.empty()); |
| 3115 | JSONTEST_ASSERT_EQUAL("value", root["property"]); |
| 3116 | } |
| 3117 | { |
| 3118 | char const doc[] = "{ \"property\" //comment\n : \"value\" }"; |
| 3119 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3120 | JSONTEST_ASSERT(!ok); |
| 3121 | JSONTEST_ASSERT(errs == "* Line 1, Column 14\n" |
| 3122 | " Missing ':' after object member name\n"); |
| 3123 | } |
| 3124 | { |
| 3125 | char const doc[] = "//comment1\n [ //comment2\n \"value\" //comment3\n," |
| 3126 | " //comment4\n true //comment5\n ] //comment6\n"; |
| 3127 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3128 | JSONTEST_ASSERT(ok); |
| 3129 | JSONTEST_ASSERT(errs.empty()); |
| 3130 | JSONTEST_ASSERT_EQUAL("value", root[0]); |
| 3131 | JSONTEST_ASSERT_EQUAL(true, root[1]); |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseObjectWithErrors) { |
| 3136 | Json::CharReaderBuilder b; |
| 3137 | CharReaderPtr reader(b.newCharReader()); |
| 3138 | Json::Value root; |
| 3139 | Json::String errs; |
| 3140 | { |
| 3141 | char const doc[] = R"({ "property" : "value" )"; |
| 3142 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3143 | JSONTEST_ASSERT(!ok); |
| 3144 | JSONTEST_ASSERT(errs == "* Line 1, Column 24\n" |
| 3145 | " Missing ',' or '}' in object declaration\n"); |
| 3146 | JSONTEST_ASSERT_EQUAL("value", root["property"]); |
| 3147 | } |
| 3148 | { |
| 3149 | char const doc[] = R"({ "property" : "value" ,)"; |
| 3150 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3151 | JSONTEST_ASSERT(!ok); |
| 3152 | JSONTEST_ASSERT(errs == "* Line 1, Column 25\n" |
| 3153 | " Missing '}' or object member name\n"); |
| 3154 | JSONTEST_ASSERT_EQUAL("value", root["property"]); |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseArrayWithErrors) { |
| 3159 | Json::CharReaderBuilder b; |
| 3160 | CharReaderPtr reader(b.newCharReader()); |
| 3161 | Json::Value root; |
| 3162 | Json::String errs; |
| 3163 | { |
| 3164 | char const doc[] = "[ \"value\" "; |
| 3165 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3166 | JSONTEST_ASSERT(!ok); |
| 3167 | JSONTEST_ASSERT(errs == "* Line 1, Column 11\n" |
| 3168 | " Missing ',' or ']' in array declaration\n"); |
| 3169 | JSONTEST_ASSERT_EQUAL("value", root[0]); |
| 3170 | } |
| 3171 | { |
| 3172 | char const doc[] = R"([ "value1" "value2" ])"; |
| 3173 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3174 | JSONTEST_ASSERT(!ok); |
| 3175 | JSONTEST_ASSERT(errs == "* Line 1, Column 12\n" |
| 3176 | " Missing ',' or ']' in array declaration\n"); |
| 3177 | JSONTEST_ASSERT_EQUAL("value1", root[0]); |
| 3178 | } |
| 3179 | } |
| 3180 | |
| 3181 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithOneError) { |
| 3182 | Json::CharReaderBuilder b; |
| 3183 | CharReaderPtr reader(b.newCharReader()); |
| 3184 | Json::String errs; |
| 3185 | Json::Value root; |
| 3186 | char const doc[] = R"({ "property" :: "value" })"; |
| 3187 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3188 | JSONTEST_ASSERT(!ok); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3189 | JSONTEST_ASSERT(errs == |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3190 | "* Line 1, Column 15\n Syntax error: value, object or array " |
| 3191 | "expected.\n"); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3192 | } |
| 3193 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3194 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseChineseWithOneError) { |
| 3195 | Json::CharReaderBuilder b; |
| 3196 | CharReaderPtr reader(b.newCharReader()); |
| 3197 | Json::String errs; |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3198 | Json::Value root; |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3199 | char const doc[] = "{ \"pr佐藤erty\" :: \"value\" }"; |
| 3200 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3201 | JSONTEST_ASSERT(!ok); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3202 | JSONTEST_ASSERT(errs == |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3203 | "* Line 1, Column 19\n Syntax error: value, object or array " |
| 3204 | "expected.\n"); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3205 | } |
| 3206 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3207 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithDetailError) { |
| 3208 | Json::CharReaderBuilder b; |
| 3209 | CharReaderPtr reader(b.newCharReader()); |
| 3210 | Json::String errs; |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3211 | Json::Value root; |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3212 | char const doc[] = R"({ "property" : "v\alue" })"; |
| 3213 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3214 | JSONTEST_ASSERT(!ok); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3215 | JSONTEST_ASSERT(errs == |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3216 | "* Line 1, Column 16\n Bad escape sequence in string\nSee " |
| 3217 | "Line 1, Column 20 for detail.\n"); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithStackLimit) { |
| 3221 | Json::CharReaderBuilder b; |
| 3222 | Json::Value root; |
| 3223 | char const doc[] = R"({ "property" : "value" })"; |
| 3224 | { |
| 3225 | b.settings_["stackLimit"] = 2; |
| 3226 | CharReaderPtr reader(b.newCharReader()); |
| 3227 | Json::String errs; |
| 3228 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3229 | JSONTEST_ASSERT(ok); |
| 3230 | JSONTEST_ASSERT(errs.empty()); |
| 3231 | JSONTEST_ASSERT_EQUAL("value", root["property"]); |
| 3232 | } |
| 3233 | { |
| 3234 | b.settings_["stackLimit"] = 1; |
| 3235 | CharReaderPtr reader(b.newCharReader()); |
| 3236 | Json::String errs; |
| 3237 | JSONTEST_ASSERT_THROWS( |
| 3238 | reader->parse(doc, doc + std::strlen(doc), &root, &errs)); |
| 3239 | } |
| 3240 | } |
| 3241 | |
| 3242 | JSONTEST_FIXTURE_LOCAL(CharReaderTest, testOperator) { |
| 3243 | const std::string styled = R"({ "property" : "value" })"; |
| 3244 | std::istringstream iss(styled); |
| 3245 | Json::Value root; |
| 3246 | iss >> root; |
| 3247 | JSONTEST_ASSERT_EQUAL("value", root["property"]); |
| 3248 | } |
| 3249 | |
| 3250 | struct CharReaderStrictModeTest : JsonTest::TestCase {}; |
| 3251 | |
| 3252 | JSONTEST_FIXTURE_LOCAL(CharReaderStrictModeTest, dupKeys) { |
| 3253 | Json::CharReaderBuilder b; |
| 3254 | Json::Value root; |
| 3255 | char const doc[] = |
| 3256 | R"({ "property" : "value", "key" : "val1", "key" : "val2" })"; |
| 3257 | { |
| 3258 | b.strictMode(&b.settings_); |
| 3259 | CharReaderPtr reader(b.newCharReader()); |
| 3260 | Json::String errs; |
| 3261 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3262 | JSONTEST_ASSERT(!ok); |
| 3263 | JSONTEST_ASSERT_STRING_EQUAL("* Line 1, Column 41\n" |
| 3264 | " Duplicate key: 'key'\n", |
| 3265 | errs); |
| 3266 | JSONTEST_ASSERT_EQUAL("val1", root["key"]); // so far |
| 3267 | } |
| 3268 | } |
| 3269 | struct CharReaderFailIfExtraTest : JsonTest::TestCase {}; |
| 3270 | |
| 3271 | JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, issue164) { |
| 3272 | // This is interpreted as a string value followed by a colon. |
| 3273 | Json::CharReaderBuilder b; |
| 3274 | Json::Value root; |
| 3275 | char const doc[] = R"( "property" : "value" })"; |
| 3276 | { |
| 3277 | b.settings_["failIfExtra"] = false; |
| 3278 | CharReaderPtr reader(b.newCharReader()); |
| 3279 | Json::String errs; |
| 3280 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3281 | JSONTEST_ASSERT(ok); |
| 3282 | JSONTEST_ASSERT(errs.empty()); |
| 3283 | JSONTEST_ASSERT_EQUAL("property", root); |
| 3284 | } |
| 3285 | { |
| 3286 | b.settings_["failIfExtra"] = true; |
| 3287 | CharReaderPtr reader(b.newCharReader()); |
| 3288 | Json::String errs; |
| 3289 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3290 | JSONTEST_ASSERT(!ok); |
| 3291 | JSONTEST_ASSERT_STRING_EQUAL("* Line 1, Column 13\n" |
| 3292 | " Extra non-whitespace after JSON value.\n", |
| 3293 | errs); |
| 3294 | JSONTEST_ASSERT_EQUAL("property", root); |
| 3295 | } |
| 3296 | { |
| 3297 | b.strictMode(&b.settings_); |
| 3298 | CharReaderPtr reader(b.newCharReader()); |
| 3299 | Json::String errs; |
| 3300 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3301 | JSONTEST_ASSERT(!ok); |
| 3302 | JSONTEST_ASSERT_STRING_EQUAL("* Line 1, Column 13\n" |
| 3303 | " Extra non-whitespace after JSON value.\n", |
| 3304 | errs); |
| 3305 | JSONTEST_ASSERT_EQUAL("property", root); |
| 3306 | } |
| 3307 | { |
| 3308 | b.strictMode(&b.settings_); |
| 3309 | b.settings_["failIfExtra"] = false; |
| 3310 | CharReaderPtr reader(b.newCharReader()); |
| 3311 | Json::String errs; |
| 3312 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3313 | JSONTEST_ASSERT(!ok); |
| 3314 | JSONTEST_ASSERT_STRING_EQUAL( |
| 3315 | "* Line 1, Column 1\n" |
| 3316 | " A valid JSON document must be either an array or an object value.\n", |
| 3317 | errs); |
| 3318 | JSONTEST_ASSERT_EQUAL("property", root); |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, issue107) { |
| 3323 | // This is interpreted as an int value followed by a colon. |
| 3324 | Json::CharReaderBuilder b; |
| 3325 | Json::Value root; |
| 3326 | char const doc[] = "1:2:3"; |
| 3327 | b.settings_["failIfExtra"] = true; |
| 3328 | CharReaderPtr reader(b.newCharReader()); |
| 3329 | Json::String errs; |
| 3330 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3331 | JSONTEST_ASSERT(!ok); |
| 3332 | JSONTEST_ASSERT_STRING_EQUAL("* Line 1, Column 2\n" |
| 3333 | " Extra non-whitespace after JSON value.\n", |
| 3334 | errs); |
| 3335 | JSONTEST_ASSERT_EQUAL(1, root.asInt()); |
| 3336 | } |
| 3337 | JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, commentAfterObject) { |
| 3338 | Json::CharReaderBuilder b; |
| 3339 | Json::Value root; |
| 3340 | { |
| 3341 | char const doc[] = "{ \"property\" : \"value\" } //trailing\n//comment\n"; |
| 3342 | b.settings_["failIfExtra"] = true; |
| 3343 | CharReaderPtr reader(b.newCharReader()); |
| 3344 | Json::String errs; |
| 3345 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3346 | JSONTEST_ASSERT(ok); |
| 3347 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3348 | JSONTEST_ASSERT_EQUAL("value", root["property"]); |
| 3349 | } |
| 3350 | } |
| 3351 | JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, commentAfterArray) { |
| 3352 | Json::CharReaderBuilder b; |
| 3353 | Json::Value root; |
| 3354 | char const doc[] = "[ \"property\" , \"value\" ] //trailing\n//comment\n"; |
| 3355 | b.settings_["failIfExtra"] = true; |
| 3356 | CharReaderPtr reader(b.newCharReader()); |
| 3357 | Json::String errs; |
| 3358 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3359 | JSONTEST_ASSERT(ok); |
| 3360 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3361 | JSONTEST_ASSERT_EQUAL("value", root[1u]); |
| 3362 | } |
| 3363 | JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, commentAfterBool) { |
| 3364 | Json::CharReaderBuilder b; |
| 3365 | Json::Value root; |
| 3366 | char const doc[] = " true /*trailing\ncomment*/"; |
| 3367 | b.settings_["failIfExtra"] = true; |
| 3368 | CharReaderPtr reader(b.newCharReader()); |
| 3369 | Json::String errs; |
| 3370 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3371 | JSONTEST_ASSERT(ok); |
| 3372 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3373 | JSONTEST_ASSERT_EQUAL(true, root.asBool()); |
| 3374 | } |
| 3375 | |
| 3376 | JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, parseComment) { |
| 3377 | Json::CharReaderBuilder b; |
| 3378 | b.settings_["failIfExtra"] = true; |
| 3379 | CharReaderPtr reader(b.newCharReader()); |
| 3380 | Json::Value root; |
| 3381 | Json::String errs; |
| 3382 | { |
| 3383 | char const doc[] = " true //comment1\n//comment2\r//comment3\r\n"; |
| 3384 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3385 | JSONTEST_ASSERT(ok); |
| 3386 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3387 | JSONTEST_ASSERT_EQUAL(true, root.asBool()); |
| 3388 | } |
| 3389 | { |
| 3390 | char const doc[] = " true //com\rment"; |
| 3391 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3392 | JSONTEST_ASSERT(!ok); |
| 3393 | JSONTEST_ASSERT_STRING_EQUAL("* Line 2, Column 1\n" |
| 3394 | " Extra non-whitespace after JSON value.\n", |
| 3395 | errs); |
| 3396 | JSONTEST_ASSERT_EQUAL(true, root.asBool()); |
| 3397 | } |
| 3398 | { |
| 3399 | char const doc[] = " true //com\nment"; |
| 3400 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3401 | JSONTEST_ASSERT(!ok); |
| 3402 | JSONTEST_ASSERT_STRING_EQUAL("* Line 2, Column 1\n" |
| 3403 | " Extra non-whitespace after JSON value.\n", |
| 3404 | errs); |
| 3405 | JSONTEST_ASSERT_EQUAL(true, root.asBool()); |
| 3406 | } |
| 3407 | } |
| 3408 | |
| 3409 | struct CharReaderAllowDropNullTest : JsonTest::TestCase { |
| 3410 | using Value = Json::Value; |
| 3411 | using ValueCheck = std::function<void(const Value&)>; |
| 3412 | |
| 3413 | Value nullValue = Value{Json::nullValue}; |
| 3414 | Value emptyArray = Value{Json::arrayValue}; |
| 3415 | |
| 3416 | ValueCheck checkEq(const Value& v) { |
| 3417 | return [=](const Value& root) { JSONTEST_ASSERT_EQUAL(root, v); }; |
| 3418 | } |
| 3419 | |
| 3420 | static ValueCheck objGetAnd(std::string idx, ValueCheck f) { |
| 3421 | return [=](const Value& root) { f(root.get(idx, true)); }; |
| 3422 | } |
| 3423 | |
| 3424 | static ValueCheck arrGetAnd(int idx, ValueCheck f) { |
| 3425 | return [=](const Value& root) { f(root[idx]); }; |
| 3426 | } |
| 3427 | }; |
| 3428 | |
| 3429 | JSONTEST_FIXTURE_LOCAL(CharReaderAllowDropNullTest, issue178) { |
| 3430 | struct TestSpec { |
| 3431 | int line; |
| 3432 | std::string doc; |
| 3433 | size_t rootSize; |
| 3434 | ValueCheck onRoot; |
| 3435 | }; |
| 3436 | const TestSpec specs[] = { |
| 3437 | {__LINE__, R"({"a":,"b":true})", 2, objGetAnd("a", checkEq(nullValue))}, |
| 3438 | {__LINE__, R"({"a":,"b":true})", 2, objGetAnd("a", checkEq(nullValue))}, |
| 3439 | {__LINE__, R"({"a":})", 1, objGetAnd("a", checkEq(nullValue))}, |
| 3440 | {__LINE__, "[]", 0, checkEq(emptyArray)}, |
| 3441 | {__LINE__, "[null]", 1, nullptr}, |
| 3442 | {__LINE__, "[,]", 2, nullptr}, |
| 3443 | {__LINE__, "[,,,]", 4, nullptr}, |
| 3444 | {__LINE__, "[null,]", 2, nullptr}, |
| 3445 | {__LINE__, "[,null]", 2, nullptr}, |
| 3446 | {__LINE__, "[,,]", 3, nullptr}, |
| 3447 | {__LINE__, "[null,,]", 3, nullptr}, |
| 3448 | {__LINE__, "[,null,]", 3, nullptr}, |
| 3449 | {__LINE__, "[,,null]", 3, nullptr}, |
| 3450 | {__LINE__, "[[],,,]", 4, arrGetAnd(0, checkEq(emptyArray))}, |
| 3451 | {__LINE__, "[,[],,]", 4, arrGetAnd(1, checkEq(emptyArray))}, |
| 3452 | {__LINE__, "[,,,[]]", 4, arrGetAnd(3, checkEq(emptyArray))}, |
| 3453 | }; |
| 3454 | for (const auto& spec : specs) { |
| 3455 | Json::CharReaderBuilder b; |
| 3456 | b.settings_["allowDroppedNullPlaceholders"] = true; |
| 3457 | std::unique_ptr<Json::CharReader> reader(b.newCharReader()); |
| 3458 | |
| 3459 | Json::Value root; |
| 3460 | Json::String errs; |
| 3461 | bool ok = reader->parse(spec.doc.data(), spec.doc.data() + spec.doc.size(), |
| 3462 | &root, &errs); |
| 3463 | JSONTEST_ASSERT(ok); |
| 3464 | JSONTEST_ASSERT_STRING_EQUAL(errs, ""); |
| 3465 | if (spec.onRoot) { |
| 3466 | spec.onRoot(root); |
| 3467 | } |
| 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | struct CharReaderAllowNumericKeysTest : JsonTest::TestCase {}; |
| 3472 | |
| 3473 | JSONTEST_FIXTURE_LOCAL(CharReaderAllowNumericKeysTest, allowNumericKeys) { |
| 3474 | Json::CharReaderBuilder b; |
| 3475 | b.settings_["allowNumericKeys"] = true; |
| 3476 | Json::Value root; |
| 3477 | Json::String errs; |
| 3478 | CharReaderPtr reader(b.newCharReader()); |
| 3479 | char const doc[] = "{15:true,-16:true,12.01:true}"; |
| 3480 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3481 | JSONTEST_ASSERT(ok); |
| 3482 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3483 | JSONTEST_ASSERT_EQUAL(3u, root.size()); |
| 3484 | JSONTEST_ASSERT_EQUAL(true, root.get("15", false)); |
| 3485 | JSONTEST_ASSERT_EQUAL(true, root.get("-16", false)); |
| 3486 | JSONTEST_ASSERT_EQUAL(true, root.get("12.01", false)); |
| 3487 | } |
| 3488 | |
| 3489 | struct CharReaderAllowSingleQuotesTest : JsonTest::TestCase {}; |
| 3490 | |
| 3491 | JSONTEST_FIXTURE_LOCAL(CharReaderAllowSingleQuotesTest, issue182) { |
| 3492 | Json::CharReaderBuilder b; |
| 3493 | b.settings_["allowSingleQuotes"] = true; |
| 3494 | Json::Value root; |
| 3495 | Json::String errs; |
| 3496 | CharReaderPtr reader(b.newCharReader()); |
| 3497 | { |
| 3498 | char const doc[] = "{'a':true,\"b\":true}"; |
| 3499 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3500 | JSONTEST_ASSERT(ok); |
| 3501 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3502 | JSONTEST_ASSERT_EQUAL(2u, root.size()); |
| 3503 | JSONTEST_ASSERT_EQUAL(true, root.get("a", false)); |
| 3504 | JSONTEST_ASSERT_EQUAL(true, root.get("b", false)); |
| 3505 | } |
| 3506 | { |
| 3507 | char const doc[] = "{'a': 'x', \"b\":'y'}"; |
| 3508 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3509 | JSONTEST_ASSERT(ok); |
| 3510 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3511 | JSONTEST_ASSERT_EQUAL(2u, root.size()); |
| 3512 | JSONTEST_ASSERT_STRING_EQUAL("x", root["a"].asString()); |
| 3513 | JSONTEST_ASSERT_STRING_EQUAL("y", root["b"].asString()); |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | struct CharReaderAllowZeroesTest : JsonTest::TestCase {}; |
| 3518 | |
| 3519 | JSONTEST_FIXTURE_LOCAL(CharReaderAllowZeroesTest, issue176) { |
| 3520 | Json::CharReaderBuilder b; |
| 3521 | b.settings_["allowSingleQuotes"] = true; |
| 3522 | Json::Value root; |
| 3523 | Json::String errs; |
| 3524 | CharReaderPtr reader(b.newCharReader()); |
| 3525 | { |
| 3526 | char const doc[] = "{'a':true,\"b\":true}"; |
| 3527 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3528 | JSONTEST_ASSERT(ok); |
| 3529 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3530 | JSONTEST_ASSERT_EQUAL(2u, root.size()); |
| 3531 | JSONTEST_ASSERT_EQUAL(true, root.get("a", false)); |
| 3532 | JSONTEST_ASSERT_EQUAL(true, root.get("b", false)); |
| 3533 | } |
| 3534 | { |
| 3535 | char const doc[] = "{'a': 'x', \"b\":'y'}"; |
| 3536 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3537 | JSONTEST_ASSERT(ok); |
| 3538 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3539 | JSONTEST_ASSERT_EQUAL(2u, root.size()); |
| 3540 | JSONTEST_ASSERT_STRING_EQUAL("x", root["a"].asString()); |
| 3541 | JSONTEST_ASSERT_STRING_EQUAL("y", root["b"].asString()); |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | struct CharReaderAllowSpecialFloatsTest : JsonTest::TestCase {}; |
| 3546 | |
| 3547 | JSONTEST_FIXTURE_LOCAL(CharReaderAllowSpecialFloatsTest, specialFloat) { |
| 3548 | Json::CharReaderBuilder b; |
| 3549 | CharReaderPtr reader(b.newCharReader()); |
| 3550 | Json::Value root; |
| 3551 | Json::String errs; |
| 3552 | { |
| 3553 | char const doc[] = "{\"a\": NaN}"; |
| 3554 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3555 | JSONTEST_ASSERT(!ok); |
| 3556 | JSONTEST_ASSERT_STRING_EQUAL( |
| 3557 | "* Line 1, Column 7\n" |
| 3558 | " Syntax error: value, object or array expected.\n", |
| 3559 | errs); |
| 3560 | } |
| 3561 | { |
| 3562 | char const doc[] = "{\"a\": Infinity}"; |
| 3563 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3564 | JSONTEST_ASSERT(!ok); |
| 3565 | JSONTEST_ASSERT_STRING_EQUAL( |
| 3566 | "* Line 1, Column 7\n" |
| 3567 | " Syntax error: value, object or array expected.\n", |
| 3568 | errs); |
| 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | JSONTEST_FIXTURE_LOCAL(CharReaderAllowSpecialFloatsTest, issue209) { |
| 3573 | Json::CharReaderBuilder b; |
| 3574 | b.settings_["allowSpecialFloats"] = true; |
| 3575 | Json::Value root; |
| 3576 | Json::String errs; |
| 3577 | CharReaderPtr reader(b.newCharReader()); |
| 3578 | { |
| 3579 | char const doc[] = R"({"a":NaN,"b":Infinity,"c":-Infinity,"d":+Infinity})"; |
| 3580 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3581 | JSONTEST_ASSERT(ok); |
| 3582 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3583 | JSONTEST_ASSERT_EQUAL(4u, root.size()); |
| 3584 | double n = root["a"].asDouble(); |
| 3585 | JSONTEST_ASSERT(std::isnan(n)); |
| 3586 | JSONTEST_ASSERT_EQUAL(std::numeric_limits<double>::infinity(), |
| 3587 | root.get("b", 0.0)); |
| 3588 | JSONTEST_ASSERT_EQUAL(-std::numeric_limits<double>::infinity(), |
| 3589 | root.get("c", 0.0)); |
| 3590 | JSONTEST_ASSERT_EQUAL(std::numeric_limits<double>::infinity(), |
| 3591 | root.get("d", 0.0)); |
| 3592 | } |
| 3593 | |
| 3594 | struct TestData { |
| 3595 | int line; |
| 3596 | bool ok; |
| 3597 | Json::String in; |
| 3598 | }; |
| 3599 | const TestData test_data[] = { |
| 3600 | {__LINE__, true, "{\"a\":9}"}, // |
| 3601 | {__LINE__, false, "{\"a\":0Infinity}"}, // |
| 3602 | {__LINE__, false, "{\"a\":1Infinity}"}, // |
| 3603 | {__LINE__, false, "{\"a\":9Infinity}"}, // |
| 3604 | {__LINE__, false, "{\"a\":0nfinity}"}, // |
| 3605 | {__LINE__, false, "{\"a\":1nfinity}"}, // |
| 3606 | {__LINE__, false, "{\"a\":9nfinity}"}, // |
| 3607 | {__LINE__, false, "{\"a\":nfinity}"}, // |
| 3608 | {__LINE__, false, "{\"a\":.nfinity}"}, // |
| 3609 | {__LINE__, false, "{\"a\":9nfinity}"}, // |
| 3610 | {__LINE__, false, "{\"a\":-nfinity}"}, // |
| 3611 | {__LINE__, true, "{\"a\":Infinity}"}, // |
| 3612 | {__LINE__, false, "{\"a\":.Infinity}"}, // |
| 3613 | {__LINE__, false, "{\"a\":_Infinity}"}, // |
| 3614 | {__LINE__, false, "{\"a\":_nfinity}"}, // |
| 3615 | {__LINE__, true, "{\"a\":-Infinity}"}, // |
| 3616 | {__LINE__, true, "{\"a\":+Infinity}"} // |
| 3617 | }; |
| 3618 | for (const auto& td : test_data) { |
| 3619 | bool ok = reader->parse(&*td.in.begin(), &*td.in.begin() + td.in.size(), |
| 3620 | &root, &errs); |
| 3621 | JSONTEST_ASSERT(td.ok == ok) << "line:" << td.line << "\n" |
| 3622 | << " expected: {" |
| 3623 | << "ok:" << td.ok << ", in:\'" << td.in << "\'" |
| 3624 | << "}\n" |
| 3625 | << " actual: {" |
| 3626 | << "ok:" << ok << "}\n"; |
| 3627 | } |
| 3628 | |
| 3629 | { |
| 3630 | char const doc[] = R"({"posInf": +Infinity, "NegInf": -Infinity})"; |
| 3631 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3632 | JSONTEST_ASSERT(ok); |
| 3633 | JSONTEST_ASSERT_STRING_EQUAL("", errs); |
| 3634 | JSONTEST_ASSERT_EQUAL(2u, root.size()); |
| 3635 | JSONTEST_ASSERT_EQUAL(std::numeric_limits<double>::infinity(), |
| 3636 | root["posInf"].asDouble()); |
| 3637 | JSONTEST_ASSERT_EQUAL(-std::numeric_limits<double>::infinity(), |
| 3638 | root["NegInf"].asDouble()); |
| 3639 | } |
| 3640 | } |
| 3641 | |
| 3642 | struct EscapeSequenceTest : JsonTest::TestCase {}; |
| 3643 | |
| 3644 | JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest, readerParseEscapeSequence) { |
| 3645 | Json::Reader reader; |
| 3646 | Json::Value root; |
| 3647 | bool ok = reader.parse("[\"\\\"\",\"\\/\",\"\\\\\",\"\\b\"," |
| 3648 | "\"\\f\",\"\\n\",\"\\r\",\"\\t\"," |
| 3649 | "\"\\u0278\",\"\\ud852\\udf62\"]\n", |
| 3650 | root); |
| 3651 | JSONTEST_ASSERT(ok); |
| 3652 | JSONTEST_ASSERT(reader.getFormattedErrorMessages().empty()); |
| 3653 | JSONTEST_ASSERT(reader.getStructuredErrors().empty()); |
| 3654 | } |
| 3655 | |
| 3656 | JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest, charReaderParseEscapeSequence) { |
| 3657 | Json::CharReaderBuilder b; |
| 3658 | CharReaderPtr reader(b.newCharReader()); |
| 3659 | Json::Value root; |
| 3660 | Json::String errs; |
| 3661 | char const doc[] = "[\"\\\"\",\"\\/\",\"\\\\\",\"\\b\"," |
| 3662 | "\"\\f\",\"\\n\",\"\\r\",\"\\t\"," |
| 3663 | "\"\\u0278\",\"\\ud852\\udf62\"]"; |
| 3664 | bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); |
| 3665 | JSONTEST_ASSERT(ok); |
| 3666 | JSONTEST_ASSERT(errs.empty()); |
| 3667 | } |
| 3668 | |
| 3669 | JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest, writeEscapeSequence) { |
| 3670 | Json::FastWriter writer; |
| 3671 | const Json::String expected("[\"\\\"\",\"\\\\\",\"\\b\"," |
| 3672 | "\"\\f\",\"\\n\",\"\\r\",\"\\t\"," |
| 3673 | "\"\\u0278\",\"\\ud852\\udf62\"]\n"); |
| 3674 | Json::Value root; |
| 3675 | root[0] = "\""; |
| 3676 | root[1] = "\\"; |
| 3677 | root[2] = "\b"; |
| 3678 | root[3] = "\f"; |
| 3679 | root[4] = "\n"; |
| 3680 | root[5] = "\r"; |
| 3681 | root[6] = "\t"; |
| 3682 | root[7] = "ɸ"; |
| 3683 | root[8] = "𤭢"; |
| 3684 | const Json::String result = writer.write(root); |
| 3685 | JSONTEST_ASSERT_STRING_EQUAL(expected, result); |
| 3686 | } |
| 3687 | |
| 3688 | struct BuilderTest : JsonTest::TestCase {}; |
| 3689 | |
| 3690 | JSONTEST_FIXTURE_LOCAL(BuilderTest, settings) { |
| 3691 | { |
| 3692 | Json::Value errs; |
| 3693 | Json::CharReaderBuilder rb; |
| 3694 | JSONTEST_ASSERT_EQUAL(false, rb.settings_.isMember("foo")); |
| 3695 | JSONTEST_ASSERT_EQUAL(true, rb.validate(&errs)); |
| 3696 | rb["foo"] = "bar"; |
| 3697 | JSONTEST_ASSERT_EQUAL(true, rb.settings_.isMember("foo")); |
| 3698 | JSONTEST_ASSERT_EQUAL(false, rb.validate(&errs)); |
| 3699 | } |
| 3700 | { |
| 3701 | Json::Value errs; |
| 3702 | Json::StreamWriterBuilder wb; |
| 3703 | JSONTEST_ASSERT_EQUAL(false, wb.settings_.isMember("foo")); |
| 3704 | JSONTEST_ASSERT_EQUAL(true, wb.validate(&errs)); |
| 3705 | wb["foo"] = "bar"; |
| 3706 | JSONTEST_ASSERT_EQUAL(true, wb.settings_.isMember("foo")); |
| 3707 | JSONTEST_ASSERT_EQUAL(false, wb.validate(&errs)); |
| 3708 | } |
| 3709 | } |
| 3710 | |
| 3711 | struct BomTest : JsonTest::TestCase {}; |
| 3712 | |
| 3713 | JSONTEST_FIXTURE_LOCAL(BomTest, skipBom) { |
| 3714 | const std::string with_bom = "\xEF\xBB\xBF{\"key\" : \"value\"}"; |
| 3715 | Json::Value root; |
| 3716 | JSONCPP_STRING errs; |
| 3717 | std::istringstream iss(with_bom); |
| 3718 | bool ok = parseFromStream(Json::CharReaderBuilder(), iss, &root, &errs); |
| 3719 | // The default behavior is to skip the BOM, so we can parse it normally. |
| 3720 | JSONTEST_ASSERT(ok); |
| 3721 | JSONTEST_ASSERT(errs.empty()); |
| 3722 | JSONTEST_ASSERT_STRING_EQUAL(root["key"].asString(), "value"); |
| 3723 | } |
| 3724 | JSONTEST_FIXTURE_LOCAL(BomTest, notSkipBom) { |
| 3725 | const std::string with_bom = "\xEF\xBB\xBF{\"key\" : \"value\"}"; |
| 3726 | Json::Value root; |
| 3727 | JSONCPP_STRING errs; |
| 3728 | std::istringstream iss(with_bom); |
| 3729 | Json::CharReaderBuilder b; |
| 3730 | b.settings_["skipBom"] = false; |
| 3731 | bool ok = parseFromStream(b, iss, &root, &errs); |
| 3732 | // Detect the BOM, and failed on it. |
| 3733 | JSONTEST_ASSERT(!ok); |
| 3734 | JSONTEST_ASSERT(!errs.empty()); |
| 3735 | } |
| 3736 | |
| 3737 | struct IteratorTest : JsonTest::TestCase {}; |
| 3738 | |
| 3739 | JSONTEST_FIXTURE_LOCAL(IteratorTest, convert) { |
| 3740 | Json::Value j; |
| 3741 | const Json::Value& cj = j; |
| 3742 | auto it = j.begin(); |
| 3743 | Json::Value::const_iterator cit; |
| 3744 | cit = it; |
| 3745 | JSONTEST_ASSERT(cit == cj.begin()); |
| 3746 | } |
| 3747 | |
| 3748 | JSONTEST_FIXTURE_LOCAL(IteratorTest, decrement) { |
| 3749 | Json::Value json; |
| 3750 | json["k1"] = "a"; |
| 3751 | json["k2"] = "b"; |
| 3752 | std::vector<std::string> values; |
| 3753 | for (auto it = json.end(); it != json.begin();) { |
| 3754 | --it; |
| 3755 | values.push_back(it->asString()); |
| 3756 | } |
| 3757 | JSONTEST_ASSERT((values == std::vector<std::string>{"b", "a"})); |
| 3758 | } |
| 3759 | |
| 3760 | JSONTEST_FIXTURE_LOCAL(IteratorTest, reverseIterator) { |
| 3761 | Json::Value json; |
| 3762 | json["k1"] = "a"; |
| 3763 | json["k2"] = "b"; |
| 3764 | std::vector<std::string> values; |
| 3765 | using Iter = decltype(json.begin()); |
| 3766 | auto re = std::reverse_iterator<Iter>(json.begin()); |
| 3767 | for (auto it = std::reverse_iterator<Iter>(json.end()); it != re; ++it) { |
| 3768 | values.push_back(it->asString()); |
| 3769 | } |
| 3770 | JSONTEST_ASSERT((values == std::vector<std::string>{"b", "a"})); |
| 3771 | } |
| 3772 | |
| 3773 | JSONTEST_FIXTURE_LOCAL(IteratorTest, distance) { |
| 3774 | { |
| 3775 | Json::Value json; |
| 3776 | json["k1"] = "a"; |
| 3777 | json["k2"] = "b"; |
| 3778 | int i = 0; |
| 3779 | auto it = json.begin(); |
| 3780 | for (;; ++it, ++i) { |
| 3781 | auto dist = it - json.begin(); |
| 3782 | JSONTEST_ASSERT_EQUAL(i, dist); |
| 3783 | if (it == json.end()) |
| 3784 | break; |
| 3785 | } |
| 3786 | } |
| 3787 | { |
| 3788 | Json::Value empty; |
| 3789 | JSONTEST_ASSERT_EQUAL(0, empty.end() - empty.end()); |
| 3790 | JSONTEST_ASSERT_EQUAL(0, empty.end() - empty.begin()); |
| 3791 | } |
| 3792 | } |
| 3793 | |
| 3794 | JSONTEST_FIXTURE_LOCAL(IteratorTest, nullValues) { |
| 3795 | { |
| 3796 | Json::Value json; |
| 3797 | auto end = json.end(); |
| 3798 | auto endCopy = end; |
| 3799 | JSONTEST_ASSERT(endCopy == end); |
| 3800 | endCopy = end; |
| 3801 | JSONTEST_ASSERT(endCopy == end); |
| 3802 | } |
| 3803 | { |
| 3804 | // Same test, now with const Value. |
| 3805 | const Json::Value json; |
| 3806 | auto end = json.end(); |
| 3807 | auto endCopy = end; |
| 3808 | JSONTEST_ASSERT(endCopy == end); |
| 3809 | endCopy = end; |
| 3810 | JSONTEST_ASSERT(endCopy == end); |
| 3811 | } |
| 3812 | } |
| 3813 | |
| 3814 | JSONTEST_FIXTURE_LOCAL(IteratorTest, staticStringKey) { |
| 3815 | Json::Value json; |
| 3816 | json[Json::StaticString("k1")] = "a"; |
| 3817 | JSONTEST_ASSERT_EQUAL(Json::Value("k1"), json.begin().key()); |
| 3818 | } |
| 3819 | |
| 3820 | JSONTEST_FIXTURE_LOCAL(IteratorTest, names) { |
| 3821 | Json::Value json; |
| 3822 | json["k1"] = "a"; |
| 3823 | json["k2"] = "b"; |
| 3824 | Json::ValueIterator it = json.begin(); |
| 3825 | JSONTEST_ASSERT(it != json.end()); |
| 3826 | JSONTEST_ASSERT_EQUAL(Json::Value("k1"), it.key()); |
| 3827 | JSONTEST_ASSERT_STRING_EQUAL("k1", it.name()); |
| 3828 | JSONTEST_ASSERT_STRING_EQUAL("k1", it.memberName()); |
| 3829 | JSONTEST_ASSERT_EQUAL(-1, it.index()); |
| 3830 | ++it; |
| 3831 | JSONTEST_ASSERT(it != json.end()); |
| 3832 | JSONTEST_ASSERT_EQUAL(Json::Value("k2"), it.key()); |
| 3833 | JSONTEST_ASSERT_STRING_EQUAL("k2", it.name()); |
| 3834 | JSONTEST_ASSERT_STRING_EQUAL("k2", it.memberName()); |
| 3835 | JSONTEST_ASSERT_EQUAL(-1, it.index()); |
| 3836 | ++it; |
| 3837 | JSONTEST_ASSERT(it == json.end()); |
| 3838 | } |
| 3839 | |
| 3840 | JSONTEST_FIXTURE_LOCAL(IteratorTest, indexes) { |
| 3841 | Json::Value json; |
| 3842 | json[0] = "a"; |
| 3843 | json[1] = "b"; |
| 3844 | Json::ValueIterator it = json.begin(); |
| 3845 | JSONTEST_ASSERT(it != json.end()); |
| 3846 | JSONTEST_ASSERT_EQUAL(Json::Value(Json::ArrayIndex(0)), it.key()); |
| 3847 | JSONTEST_ASSERT_STRING_EQUAL("", it.name()); |
| 3848 | JSONTEST_ASSERT_EQUAL(0, it.index()); |
| 3849 | ++it; |
| 3850 | JSONTEST_ASSERT(it != json.end()); |
| 3851 | JSONTEST_ASSERT_EQUAL(Json::Value(Json::ArrayIndex(1)), it.key()); |
| 3852 | JSONTEST_ASSERT_STRING_EQUAL("", it.name()); |
| 3853 | JSONTEST_ASSERT_EQUAL(1, it.index()); |
| 3854 | ++it; |
| 3855 | JSONTEST_ASSERT(it == json.end()); |
| 3856 | } |
| 3857 | |
| 3858 | JSONTEST_FIXTURE_LOCAL(IteratorTest, constness) { |
| 3859 | Json::Value const v; |
| 3860 | JSONTEST_ASSERT_THROWS( |
| 3861 | Json::Value::iterator it(v.begin())); // Compile, but throw. |
| 3862 | |
| 3863 | Json::Value value; |
| 3864 | |
| 3865 | for (int i = 9; i < 12; ++i) { |
| 3866 | Json::OStringStream out; |
| 3867 | out << std::setw(2) << i; |
| 3868 | Json::String str = out.str(); |
| 3869 | value[str] = str; |
| 3870 | } |
| 3871 | |
| 3872 | Json::OStringStream out; |
| 3873 | // in old code, this will get a compile error |
| 3874 | Json::Value::const_iterator iter = value.begin(); |
| 3875 | for (; iter != value.end(); ++iter) { |
| 3876 | out << *iter << ','; |
| 3877 | } |
| 3878 | Json::String expected = R"(" 9","10","11",)"; |
| 3879 | JSONTEST_ASSERT_STRING_EQUAL(expected, out.str()); |
| 3880 | } |
| 3881 | |
| 3882 | struct RValueTest : JsonTest::TestCase {}; |
| 3883 | |
| 3884 | JSONTEST_FIXTURE_LOCAL(RValueTest, moveConstruction) { |
| 3885 | Json::Value json; |
| 3886 | json["key"] = "value"; |
| 3887 | Json::Value moved = std::move(json); |
| 3888 | JSONTEST_ASSERT(moved != json); // Possibly not nullValue; definitely not |
| 3889 | // equal. |
| 3890 | JSONTEST_ASSERT_EQUAL(Json::objectValue, moved.type()); |
| 3891 | JSONTEST_ASSERT_EQUAL(Json::stringValue, moved["key"].type()); |
| 3892 | } |
| 3893 | |
| 3894 | struct FuzzTest : JsonTest::TestCase {}; |
| 3895 | |
| 3896 | // Build and run the fuzz test without any fuzzer, so that it's guaranteed not |
| 3897 | // go out of date, even if it's never run as an actual fuzz test. |
| 3898 | JSONTEST_FIXTURE_LOCAL(FuzzTest, fuzzDoesntCrash) { |
| 3899 | const std::string example = "{}"; |
| 3900 | JSONTEST_ASSERT_EQUAL( |
| 3901 | 0, |
| 3902 | LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(example.c_str()), |
| 3903 | example.size())); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3904 | } |
| 3905 | |
| 3906 | int main(int argc, const char* argv[]) { |
| 3907 | JsonTest::Runner runner; |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3908 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3909 | for (auto& local : local_) { |
| 3910 | runner.add(local); |
| 3911 | } |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 3912 | |
| 3913 | return runner.runCommandLine(argc, argv); |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 3914 | } |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3915 | |
| 3916 | struct MemberTemplateAs : JsonTest::TestCase { |
| 3917 | template <typename T, typename F> |
| 3918 | JsonTest::TestResult& EqEval(T v, F f) const { |
| 3919 | const Json::Value j = v; |
| 3920 | return JSONTEST_ASSERT_EQUAL(j.as<T>(), f(j)); |
| 3921 | } |
| 3922 | }; |
| 3923 | |
| 3924 | JSONTEST_FIXTURE_LOCAL(MemberTemplateAs, BehavesSameAsNamedAs) { |
| 3925 | const Json::Value jstr = "hello world"; |
| 3926 | JSONTEST_ASSERT_STRING_EQUAL(jstr.as<const char*>(), jstr.asCString()); |
| 3927 | JSONTEST_ASSERT_STRING_EQUAL(jstr.as<Json::String>(), jstr.asString()); |
| 3928 | EqEval(Json::Int(64), [](const Json::Value& j) { return j.asInt(); }); |
| 3929 | EqEval(Json::UInt(64), [](const Json::Value& j) { return j.asUInt(); }); |
| 3930 | #if defined(JSON_HAS_INT64) |
| 3931 | EqEval(Json::Int64(64), [](const Json::Value& j) { return j.asInt64(); }); |
| 3932 | EqEval(Json::UInt64(64), [](const Json::Value& j) { return j.asUInt64(); }); |
| 3933 | #endif // if defined(JSON_HAS_INT64) |
| 3934 | EqEval(Json::LargestInt(64), |
| 3935 | [](const Json::Value& j) { return j.asLargestInt(); }); |
| 3936 | EqEval(Json::LargestUInt(64), |
| 3937 | [](const Json::Value& j) { return j.asLargestUInt(); }); |
| 3938 | |
| 3939 | EqEval(69.69f, [](const Json::Value& j) { return j.asFloat(); }); |
| 3940 | EqEval(69.69, [](const Json::Value& j) { return j.asDouble(); }); |
| 3941 | EqEval(false, [](const Json::Value& j) { return j.asBool(); }); |
| 3942 | EqEval(true, [](const Json::Value& j) { return j.asBool(); }); |
| 3943 | } |
| 3944 | |
| 3945 | class MemberTemplateIs : public JsonTest::TestCase {}; |
| 3946 | |
| 3947 | JSONTEST_FIXTURE_LOCAL(MemberTemplateIs, BehavesSameAsNamedIs) { |
| 3948 | const Json::Value values[] = {true, 142, 40.63, "hello world"}; |
| 3949 | for (const Json::Value& j : values) { |
| 3950 | JSONTEST_ASSERT_EQUAL(j.is<bool>(), j.isBool()); |
| 3951 | JSONTEST_ASSERT_EQUAL(j.is<Json::Int>(), j.isInt()); |
| 3952 | JSONTEST_ASSERT_EQUAL(j.is<Json::Int64>(), j.isInt64()); |
| 3953 | JSONTEST_ASSERT_EQUAL(j.is<Json::UInt>(), j.isUInt()); |
| 3954 | JSONTEST_ASSERT_EQUAL(j.is<Json::UInt64>(), j.isUInt64()); |
| 3955 | JSONTEST_ASSERT_EQUAL(j.is<double>(), j.isDouble()); |
| 3956 | JSONTEST_ASSERT_EQUAL(j.is<Json::String>(), j.isString()); |
| 3957 | } |
| 3958 | } |
| 3959 | |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 3960 | class VersionTest : public JsonTest::TestCase {}; |
| 3961 | |
| 3962 | JSONTEST_FIXTURE_LOCAL(VersionTest, VersionNumbersMatch) { |
| 3963 | std::ostringstream vstr; |
| 3964 | vstr << JSONCPP_VERSION_MAJOR << '.' << JSONCPP_VERSION_MINOR << '.' |
| 3965 | << JSONCPP_VERSION_PATCH; |
| 3966 | JSONTEST_ASSERT_EQUAL(vstr.str(), std::string(JSONCPP_VERSION_STRING)); |
| 3967 | } |
| 3968 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 3969 | #if defined(__GNUC__) |
| 3970 | #pragma GCC diagnostic pop |
| 3971 | #endif |