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