// ArduinoJson - https://arduinojson.org // Copyright © 2014-2023, Benoit BLANCHON // MIT License #include #include template static void check(const char* input, U expected) { DynamicJsonDocument doc(4096); DeserializationError error = deserializeMsgPack(doc, input); REQUIRE(error == DeserializationError::Ok); REQUIRE(doc.is()); REQUIRE(doc.as() == expected); } #if ARDUINOJSON_USE_LONG_LONG == 0 static void checkNotSupported(const char* input) { DynamicJsonDocument doc(4096); DeserializationError error = deserializeMsgPack(doc, input); REQUIRE(error == DeserializationError::Ok); REQUIRE(doc.isNull()); } #endif static void checkIsNull(const char* input) { DynamicJsonDocument doc(4096); DeserializationError error = deserializeMsgPack(doc, input); REQUIRE(error == DeserializationError::Ok); REQUIRE(doc.as().isNull()); } TEST_CASE("deserialize MsgPack value") { SECTION("nil") { checkIsNull("\xc0"); } SECTION("bool") { check("\xc2", false); check("\xc3", true); } SECTION("positive fixint") { check("\x00", 0); check("\x7F", 127); } SECTION("negative fixint") { check("\xe0", -32); check("\xff", -1); } SECTION("uint 8") { check("\xcc\x00", 0); check("\xcc\xff", 255); } SECTION("uint 16") { check("\xcd\x00\x00", 0); check("\xcd\xFF\xFF", 65535); check("\xcd\x30\x39", 12345); } SECTION("uint 32") { check("\xCE\x00\x00\x00\x00", 0x00000000U); check("\xCE\xFF\xFF\xFF\xFF", 0xFFFFFFFFU); check("\xCE\x12\x34\x56\x78", 0x12345678U); } SECTION("uint 64") { #if ARDUINOJSON_USE_LONG_LONG check("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 0U); check("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0xFFFFFFFFFFFFFFFFU); check("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0", 0x123456789ABCDEF0U); #else checkNotSupported("\xCF\x00\x00\x00\x00\x00\x00\x00\x00"); checkNotSupported("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); checkNotSupported("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0"); #endif } SECTION("int 8") { check("\xd0\x00", 0); check("\xd0\xff", -1); } SECTION("int 16") { check("\xD1\x00\x00", 0); check("\xD1\xFF\xFF", -1); check("\xD1\xCF\xC7", -12345); } SECTION("int 32") { check("\xD2\x00\x00\x00\x00", 0); check("\xD2\xFF\xFF\xFF\xFF", -1); check("\xD2\xB6\x69\xFD\x2E", -1234567890); } SECTION("int 64") { #if ARDUINOJSON_USE_LONG_LONG check("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", int64_t(0U)); check("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", int64_t(0xFFFFFFFFFFFFFFFFU)); check("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0", int64_t(0x123456789ABCDEF0)); #else checkNotSupported("\xD3\x00\x00\x00\x00\x00\x00\x00\x00"); checkNotSupported("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); checkNotSupported("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0"); #endif } SECTION("float 32") { check("\xCA\x00\x00\x00\x00", 0.0f); check("\xCA\x40\x48\xF5\xC3", 3.14f); } SECTION("float 64") { check("\xCB\x00\x00\x00\x00\x00\x00\x00\x00", 0.0); check("\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F", 3.1415); } SECTION("fixstr") { check("\xA0", std::string("")); check("\xABhello world", std::string("hello world")); check("\xBFhello world hello world hello !", std::string("hello world hello world hello !")); } SECTION("str 8") { check("\xd9\x05hello", std::string("hello")); } SECTION("str 16") { check("\xda\x00\x05hello", std::string("hello")); } SECTION("str 32") { check("\xdb\x00\x00\x00\x05hello", std::string("hello")); } }