Added serializeMsgPack() and measureMsgPack() (closes #358)

This commit is contained in:
Benoit Blanchon
2018-05-29 08:31:17 +02:00
parent 58cb793c96
commit fc2e3a4ab3
59 changed files with 975 additions and 391 deletions

View File

@ -0,0 +1,19 @@
# ArduinoJson - arduinojson.org
# Copyright Benoit Blanchon 2014-2018
# MIT License
add_executable(MsgPackDeserializerTests
deserializeArray.cpp
deserializeObject.cpp
deserializeStaticVariant.cpp
deserializeVariant.cpp
doubleToFloat.cpp
incompleteInput.cpp
nestingLimit.cpp
notSupported.cpp
std_string.cpp
std_istream.cpp
)
target_link_libraries(MsgPackDeserializerTests catch)
add_test(MsgPackDeserializer MsgPackDeserializerTests)

View File

@ -0,0 +1,83 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("deserialize MsgPack array") {
DynamicJsonDocument doc;
SECTION("fixarray") {
SECTION("empty") {
const char* input = "\x90";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 0);
}
SECTION("two integers") {
const char* input = "\x92\x01\x02";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 2);
REQUIRE(array[0] == 1);
REQUIRE(array[1] == 2);
}
}
SECTION("array 16") {
SECTION("empty") {
const char* input = "\xDC\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 0);
}
SECTION("two strings") {
const char* input = "\xDC\x00\x02\xA5hello\xA5world";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 2);
REQUIRE(array[0] == "hello");
REQUIRE(array[1] == "world");
}
}
SECTION("array 32") {
SECTION("empty") {
const char* input = "\xDD\x00\x00\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 0);
}
SECTION("two floats") {
const char* input =
"\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 2);
REQUIRE(array[0] == 0.0f);
REQUIRE(array[1] == 3.14f);
}
}
}

View File

@ -0,0 +1,90 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("deserialize MsgPack object") {
DynamicJsonDocument doc;
SECTION("fixmap") {
SECTION("empty") {
const char* input = "\x80";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0);
}
SECTION("two integers") {
const char* input = "\x82\xA3one\x01\xA3two\x02";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["one"] == 1);
REQUIRE(obj["two"] == 2);
}
}
SECTION("map 16") {
SECTION("empty") {
const char* input = "\xDE\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0);
}
SECTION("two strings") {
const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["H"] == "hello");
REQUIRE(obj["W"] == "world");
}
}
SECTION("map 32") {
SECTION("empty") {
const char* input = "\xDF\x00\x00\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0);
}
SECTION("two floats") {
const char* input =
"\xDF\x00\x00\x00\x02\xA4zero\xCA\x00\x00\x00\x00\xA2pi\xCA\x40\x48"
"\xF5\xC3";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["zero"] == 0.0f);
REQUIRE(obj["pi"] == 3.14f);
}
}
}

View File

@ -0,0 +1,140 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
static const size_t epsilon = sizeof(void*);
template <size_t Capacity>
static void check(const char* input, DeserializationError expected) {
StaticJsonDocument<Capacity> variant;
DeserializationError error = deserializeMsgPack(variant, input);
REQUIRE(error == expected);
}
TEST_CASE("deserializeMsgPack(StaticJsonDocument&)") {
SECTION("single values always fit") {
check<0>("\xc0", DeserializationError::Ok); // nil
check<0>("\xc2", DeserializationError::Ok); // false
check<0>("\xc3", DeserializationError::Ok); // true
check<0>("\xcc\x00", DeserializationError::Ok); // uint 8
check<0>("\xcd\x30\x39", DeserializationError::Ok); // uint 16
check<0>("\xCE\x12\x34\x56\x78", DeserializationError::Ok); // uint 32
}
SECTION("fixstr") {
check<0>("\xA0", DeserializationError::Ok);
check<0>("\xA1H", DeserializationError::NoMemory);
check<4>("\xA1H", DeserializationError::Ok);
check<4>("\xA5Hello", DeserializationError::NoMemory);
}
SECTION("str 8") {
check<0>("\xD9\x00", DeserializationError::Ok);
check<0>("\xD9\x01H", DeserializationError::NoMemory);
check<4>("\xD9\x01H", DeserializationError::Ok);
check<4>("\xD9\x05Hello", DeserializationError::NoMemory);
}
SECTION("str 16") {
check<0>("\xDA\x00\x00", DeserializationError::Ok);
check<0>("\xDA\x00\x01H", DeserializationError::NoMemory);
check<4>("\xDA\x00\x01H", DeserializationError::Ok);
check<4>("\xDA\x00\x05Hello", DeserializationError::NoMemory);
}
SECTION("str 32") {
check<0>("\xDB\x00\x00\x00\x00", DeserializationError::Ok);
check<0>("\xDB\x00\x00\x00\x01H", DeserializationError::NoMemory);
check<4>("\xDB\x00\x00\x00\x01H", DeserializationError::Ok);
check<4>("\xDB\x00\x00\x00\x05Hello", DeserializationError::NoMemory);
}
SECTION("fixarray") {
check<JSON_ARRAY_SIZE(0)>("\x90", DeserializationError::Ok); // []
check<JSON_ARRAY_SIZE(0)>("\x91\x01",
DeserializationError::NoMemory); // [1]
check<JSON_ARRAY_SIZE(1)>("\x91\x01", DeserializationError::Ok); // [1]
check<JSON_ARRAY_SIZE(1)>("\x92\x01\x02",
DeserializationError::NoMemory); // [1,2]
}
SECTION("array 16") {
check<JSON_ARRAY_SIZE(0)>("\xDC\x00\x00", DeserializationError::Ok);
check<JSON_ARRAY_SIZE(0)>("\xDC\x00\x01\x01",
DeserializationError::NoMemory);
check<JSON_ARRAY_SIZE(1)>("\xDC\x00\x01\x01", DeserializationError::Ok);
check<JSON_ARRAY_SIZE(1)>("\xDC\x00\x02\x01\x02",
DeserializationError::NoMemory);
}
SECTION("array 32") {
check<JSON_ARRAY_SIZE(0)>("\xDD\x00\x00\x00\x00", DeserializationError::Ok);
check<JSON_ARRAY_SIZE(0)>("\xDD\x00\x00\x00\x01\x01",
DeserializationError::NoMemory);
check<JSON_ARRAY_SIZE(1)>("\xDD\x00\x00\x00\x01\x01",
DeserializationError::Ok);
check<JSON_ARRAY_SIZE(1)>("\xDD\x00\x00\x00\x02\x01\x02",
DeserializationError::NoMemory);
}
SECTION("fixmap") {
SECTION("{}") {
check<JSON_OBJECT_SIZE(0)>("\x80", DeserializationError::Ok);
}
SECTION("{H:1}") {
check<JSON_OBJECT_SIZE(0)>("\x81\xA1H\x01",
DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(1) + epsilon>("\x81\xA1H\x01",
DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
check<JSON_OBJECT_SIZE(1) + epsilon>("\x82\xA1H\x01\xA1W\x02",
DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(2) + 2 * epsilon>("\x82\xA1H\x01\xA1W\x02",
DeserializationError::Ok);
}
}
SECTION("map 16") {
SECTION("{}") {
check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x00", DeserializationError::Ok);
}
SECTION("{H:1}") {
check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x01\xA1H\x01",
DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDE\x00\x01\xA1H\x01",
DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDE\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(2) + 2 * epsilon>("\xDE\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::Ok);
}
}
SECTION("map 32") {
SECTION("{}") {
check<JSON_OBJECT_SIZE(0)>("\xDF\x00\x00\x00\x00",
DeserializationError::Ok);
}
SECTION("{H:1}") {
check<JSON_OBJECT_SIZE(0)>("\xDF\x00\x00\x00\x01\xA1H\x01",
DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDF\x00\x00\x00\x01\xA1H\x01",
DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
check<JSON_OBJECT_SIZE(1) + epsilon>(
"\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(2) + 2 * epsilon>(
"\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::Ok);
}
}
}

View File

@ -0,0 +1,130 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
template <typename T, typename U>
static void check(const char* input, U expected) {
DynamicJsonDocument variant;
DeserializationError error = deserializeMsgPack(variant, input);
REQUIRE(error == DeserializationError::Ok);
REQUIRE(variant.is<T>());
REQUIRE(variant.as<T>() == expected);
}
TEST_CASE("deserialize MsgPack value") {
SECTION("nil") {
const char* nil = 0; // ArduinoJson uses a string for null
check<const char*>("\xc0", nil);
}
SECTION("bool") {
check<bool>("\xc2", false);
check<bool>("\xc3", true);
}
SECTION("positive fixint") {
check<int>("\x00", 0);
check<int>("\x7F", 127);
}
SECTION("negative fixint") {
check<int>("\xe0", -32);
check<int>("\xff", -1);
}
SECTION("uint 8") {
check<int>("\xcc\x00", 0);
check<int>("\xcc\xff", 255);
}
SECTION("uint 16") {
check<int>("\xcd\x00\x00", 0);
check<int>("\xcd\xFF\xFF", 65535);
check<int>("\xcd\x30\x39", 12345);
}
SECTION("uint 32") {
check<uint32_t>("\xCE\x00\x00\x00\x00", 0x00000000U);
check<uint32_t>("\xCE\xFF\xFF\xFF\xFF", 0xFFFFFFFFU);
check<uint32_t>("\xCE\x12\x34\x56\x78", 0x12345678U);
}
SECTION("uint 64") {
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
check<uint64_t>("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
check<uint64_t>("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
0xFFFFFFFFFFFFFFFFU);
check<uint64_t>("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
0x123456789ABCDEF0U);
#else
check<uint32_t>("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
check<uint32_t>("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0xFFFFFFFF);
check<uint32_t>("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0", 0x9ABCDEF0);
#endif
}
SECTION("int 8") {
check<int>("\xd0\x00", 0);
check<int>("\xd0\xff", -1);
}
SECTION("int 16") {
check<int>("\xD1\x00\x00", 0);
check<int>("\xD1\xFF\xFF", -1);
check<int>("\xD1\xCF\xC7", -12345);
}
SECTION("int 32") {
check<int>("\xD2\x00\x00\x00\x00", 0);
check<int>("\xD2\xFF\xFF\xFF\xFF", -1);
check<int>("\xD2\xB6\x69\xFD\x2E", -1234567890);
}
SECTION("int 64") {
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
check<uint64_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
check<uint64_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
0xFFFFFFFFFFFFFFFFU);
check<uint64_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
0x123456789ABCDEF0U);
#else
check<uint32_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
check<uint32_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0xFFFFFFFF);
check<uint32_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0", 0x9ABCDEF0);
#endif
}
SECTION("float 32") {
check<double>("\xCA\x00\x00\x00\x00", 0.0f);
check<double>("\xCA\x40\x48\xF5\xC3", 3.14f);
}
SECTION("float 64") {
check<double>("\xCB\x00\x00\x00\x00\x00\x00\x00\x00", 0.0);
check<double>("\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F", 3.1415);
}
SECTION("fixstr") {
check<const char*>("\xA0", std::string(""));
check<const char*>("\xABhello world", std::string("hello world"));
check<const char*>("\xBFhello world hello world hello !",
std::string("hello world hello world hello !"));
}
SECTION("str 8") {
check<const char*>("\xd9\x05hello", std::string("hello"));
}
SECTION("str 16") {
check<const char*>("\xda\x00\x05hello", std::string("hello"));
}
SECTION("str 32") {
check<const char*>("\xdb\x00\x00\x00\x05hello", std::string("hello"));
}
}

View File

@ -0,0 +1,25 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ArduinoJson::Internals;
template <typename T>
static void check(const char* input, T expected) {
T actual;
uint8_t* f = reinterpret_cast<uint8_t*>(&actual);
const uint8_t* d = reinterpret_cast<const uint8_t*>(input);
doubleToFloat(d, f);
fixEndianess(actual);
CHECK(actual == expected);
}
TEST_CASE("Internals::doubleToFloat()") {
check("\x40\x09\x21\xCA\xC0\x83\x12\x6F", 3.1415f);
check("\x00\x00\x00\x00\x00\x00\x00\x00", 0.0f);
check("\x80\x00\x00\x00\x00\x00\x00\x00", -0.0f);
check("\xC0\x5E\xDC\xCC\xCC\xCC\xCC\xCD", -123.45f);
}

View File

@ -0,0 +1,106 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
DeserializationError deserialize(const char* input, size_t len) {
DynamicJsonDocument doc;
return deserializeMsgPack(doc, input, len);
}
void checkAllSizes(const char* input, size_t len) {
REQUIRE(deserialize(input, len) == DeserializationError::Ok);
while (--len) {
REQUIRE(deserialize(input, len) == DeserializationError::IncompleteInput);
}
}
TEST_CASE("deserializeMsgPack() returns IncompleteInput") {
SECTION("empty input") {
checkAllSizes("\x00", 1);
}
SECTION("fixarray") {
checkAllSizes("\x91\x01", 2);
}
SECTION("array 16") {
checkAllSizes("\xDC\x00\x01\x01", 4);
}
SECTION("array 32") {
checkAllSizes("\xDD\x00\x00\x00\x01\x01", 6);
}
SECTION("fixmap") {
checkAllSizes("\x81\xA3one\x01", 6);
}
SECTION("map 16") {
checkAllSizes("\xDE\x00\x01\xA3one\x01", 8);
}
SECTION("map 32") {
checkAllSizes("\xDF\x00\x00\x00\x01\xA3one\x01", 10);
}
SECTION("uint 8") {
checkAllSizes("\xcc\x01", 2);
}
SECTION("uint 16") {
checkAllSizes("\xcd\x00\x01", 3);
}
SECTION("uint 32") {
checkAllSizes("\xCE\x00\x00\x00\x01", 5);
}
SECTION("uint 64") {
checkAllSizes("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 9);
}
SECTION("int 8") {
checkAllSizes("\xD0\x01", 2);
}
SECTION("int 16") {
checkAllSizes("\xD1\x00\x01", 3);
}
SECTION("int 32") {
checkAllSizes("\xD2\x00\x00\x00\x01", 5);
}
SECTION("int 64") {
checkAllSizes("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 9);
}
SECTION("float 32") {
checkAllSizes("\xCA\x40\x48\xF5\xC3", 5);
}
SECTION("float 64") {
checkAllSizes("\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F", 9);
}
SECTION("fixstr") {
checkAllSizes("\xABhello world", 12);
}
SECTION("str 8") {
checkAllSizes("\xd9\x05hello", 7);
}
SECTION("str 16") {
checkAllSizes("\xda\x00\x05hello", 8);
}
SECTION("str 32") {
checkAllSizes("\xdb\x00\x00\x00\x05hello", 10);
}
}

View File

@ -0,0 +1,32 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
static void check(const char* input, DeserializationError expected,
uint8_t limit) {
DynamicJsonDocument doc;
doc.nestingLimit = limit;
DeserializationError error = deserializeMsgPack(doc, input);
REQUIRE(error == expected);
}
TEST_CASE("Errors returned by deserializeMsgPack()") {
SECTION("object too deep") {
check("\x80", DeserializationError::TooDeep, 0); // {}
check("\x80", DeserializationError::Ok, 1); // {}
check("\x81\xA1H\x80", DeserializationError::TooDeep, 1); // {H:{}}
check("\x81\xA1H\x80", DeserializationError::Ok, 2); // {H:{}}
}
SECTION("array too deep") {
check("\x90", DeserializationError::TooDeep, 0); // []
check("\x90", DeserializationError::Ok, 1); // []
check("\x91\x90", DeserializationError::TooDeep, 1); // [[]]
check("\x91\x90", DeserializationError::Ok, 2); // [[]]
}
}

View File

@ -0,0 +1,73 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
static void checkNotSupported(const char* input) {
DynamicJsonDocument doc;
DeserializationError error = deserializeMsgPack(doc, input);
REQUIRE(error == DeserializationError::NotSupported);
}
TEST_CASE("deserializeMsgPack() return NotSupported") {
SECTION("bin 8") {
checkNotSupported("\xc4");
}
SECTION("bin 16") {
checkNotSupported("\xc5");
}
SECTION("bin 32") {
checkNotSupported("\xc6");
}
SECTION("ext 8") {
checkNotSupported("\xc7");
}
SECTION("ext 16") {
checkNotSupported("\xc8");
}
SECTION("ext 32") {
checkNotSupported("\xc9");
}
SECTION("fixext 1") {
checkNotSupported("\xd4");
}
SECTION("fixext 2") {
checkNotSupported("\xd5");
}
SECTION("fixext 4") {
checkNotSupported("\xd6");
}
SECTION("fixext 8") {
checkNotSupported("\xd7");
}
SECTION("fixext 16") {
checkNotSupported("\xd8");
}
SECTION("unsupported in array") {
checkNotSupported("\x91\xc4");
}
SECTION("unsupported in map") {
checkNotSupported("\x81\xc4\x00\xA1H");
checkNotSupported("\x81\xA1H\xc4\x00");
}
SECTION("integer as key") {
checkNotSupported("\x81\x01\xA1H");
}
}

View File

@ -0,0 +1,29 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("deserializeMsgPack(std::istream&)") {
DynamicJsonDocument doc;
SECTION("should accept a zero in input") {
std::istringstream input(std::string("\x92\x00\x02", 3));
DeserializationError err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
JsonArray& arr = doc.as<JsonArray>();
REQUIRE(arr[0] == 0);
REQUIRE(arr[1] == 2);
}
SECTION("should detect incomplete input") {
std::istringstream input("\x92\x00\x02");
DeserializationError err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::IncompleteInput);
}
}

View File

@ -0,0 +1,46 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("deserializeMsgPack(const std::string&)") {
DynamicJsonDocument doc;
SECTION("should accept const string") {
const std::string input("\x92\x01\x02");
DeserializationError err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("should accept temporary string") {
DeserializationError err =
deserializeMsgPack(doc, std::string("\x92\x01\x02"));
REQUIRE(err == DeserializationError::Ok);
}
SECTION("should duplicate content") {
std::string input("\x91\xA5hello");
DeserializationError err = deserializeMsgPack(doc, input);
input[2] = 'X'; // alter the string tomake sure we made a copy
JsonArray& array = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(std::string("hello") == array[0]);
}
SECTION("should accept a zero in input") {
DeserializationError err =
deserializeMsgPack(doc, std::string("\x92\x00\x02", 3));
REQUIRE(err == DeserializationError::Ok);
JsonArray& arr = doc.as<JsonArray>();
REQUIRE(arr[0] == 0);
REQUIRE(arr[1] == 2);
}
}