// ArduinoJson - https://arduinojson.org // Copyright © 2014-2024, Benoit BLANCHON // MIT License #define ARDUINOJSON_USE_LONG_LONG 0 #define ARDUINOJSON_ENABLE_NAN 1 #define ARDUINOJSON_ENABLE_INFINITY 1 #include #include #include namespace my { using ArduinoJson::detail::isinf; using ArduinoJson::detail::isnan; } // namespace my TEST_CASE("deserialize an integer") { JsonDocument doc; SECTION("Integer") { SECTION("0") { DeserializationError err = deserializeJson(doc, "0"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == true); REQUIRE(doc.as() == 0); REQUIRE(doc.as() == "0"); // issue #808 } SECTION("Negative") { DeserializationError err = deserializeJson(doc, "-42"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); REQUIRE_FALSE(doc.is()); REQUIRE(doc.as() == -42); } #if LONG_MAX == 2147483647 SECTION("LONG_MAX") { DeserializationError err = deserializeJson(doc, "2147483647"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == true); REQUIRE(doc.as() == LONG_MAX); } SECTION("LONG_MAX + 1") { DeserializationError err = deserializeJson(doc, "2147483648"); CAPTURE(LONG_MIN); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == false); REQUIRE(doc.is() == true); } #endif #if LONG_MIN == -2147483648 SECTION("LONG_MIN") { DeserializationError err = deserializeJson(doc, "-2147483648"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == true); REQUIRE(doc.as() == LONG_MIN); } SECTION("LONG_MIN - 1") { DeserializationError err = deserializeJson(doc, "-2147483649"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == false); REQUIRE(doc.is() == true); } #endif #if ULONG_MAX == 4294967295 SECTION("ULONG_MAX") { DeserializationError err = deserializeJson(doc, "4294967295"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == true); REQUIRE(doc.as() == ULONG_MAX); REQUIRE(doc.is() == false); } SECTION("ULONG_MAX + 1") { DeserializationError err = deserializeJson(doc, "4294967296"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == false); REQUIRE(doc.is() == true); } #endif } SECTION("Floats") { SECTION("Double") { DeserializationError err = deserializeJson(doc, "-1.23e+4"); REQUIRE(err == DeserializationError::Ok); REQUIRE_FALSE(doc.is()); REQUIRE(doc.is()); REQUIRE(doc.as() == Approx(-1.23e+4)); } SECTION("NaN") { DeserializationError err = deserializeJson(doc, "NaN"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == true); REQUIRE(my::isnan(doc.as())); } SECTION("Infinity") { DeserializationError err = deserializeJson(doc, "Infinity"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == true); REQUIRE(my::isinf(doc.as())); } SECTION("+Infinity") { DeserializationError err = deserializeJson(doc, "+Infinity"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == true); REQUIRE(my::isinf(doc.as())); } SECTION("-Infinity") { DeserializationError err = deserializeJson(doc, "-Infinity"); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is() == true); REQUIRE(my::isinf(doc.as())); } } }