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,14 @@
# ArduinoJson - arduinojson.org
# Copyright Benoit Blanchon 2014-2018
# MIT License
add_executable(MsgPackSerializerTests
destination_types.cpp
measure.cpp
serializeArray.cpp
serializeObject.cpp
serializeVariant.cpp
)
target_link_libraries(MsgPackSerializerTests catch)
add_test(MsgPackSerializer MsgPackSerializerTests)

View File

@ -0,0 +1,47 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("serialize MsgPack to various destination types") {
DynamicJsonDocument doc;
JsonObject &object = doc.to<JsonObject>();
object["hello"] = "world";
const char *expected_result = "\x81\xA5hello\xA5world";
const size_t expected_length = 13;
SECTION("std::string") {
std::string result;
size_t len = serializeMsgPack(object, result);
REQUIRE(expected_result == result);
REQUIRE(expected_length == len);
}
/* SECTION("std::vector<char>") {
std::vector<char> result;
size_t len = serializeMsgPack(object, result);
REQUIRE(std::vector<char>(expected_result, expected_result + 13) ==
result);
REQUIRE(expected_length == len);
} */
SECTION("char[]") {
char result[64];
size_t len = serializeMsgPack(object, result);
REQUIRE(std::string(expected_result) == result);
REQUIRE(expected_length == len);
}
SECTION("char*") {
char result[64];
size_t len = serializeMsgPack(object, result, 64);
REQUIRE(std::string(expected_result) == result);
REQUIRE(expected_length == len);
}
}

View File

@ -0,0 +1,14 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("measureMsgPack()") {
DynamicJsonDocument doc;
JsonObject &object = doc.to<JsonObject>();
object["hello"] = "world";
REQUIRE(measureMsgPack(doc) == 13);
}

View File

@ -0,0 +1,60 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
static void check(const JsonArray& array, const char* expected_data,
size_t expected_len) {
std::string expected(expected_data, expected_data + expected_len);
std::string actual;
size_t len = serializeMsgPack(array, actual);
CAPTURE(array);
REQUIRE(len == expected_len);
REQUIRE(actual == expected);
}
template <size_t N>
static void check(const JsonArray& array, const char (&expected_data)[N]) {
const size_t expected_len = N - 1;
check(array, expected_data, expected_len);
}
// TODO: this function is used by the commented test
// static void check(const JsonArray& array, const std::string& expected) {
// check(array, expected.data(), expected.length());
// }
TEST_CASE("serialize MsgPack array") {
DynamicJsonDocument doc;
JsonArray& array = doc.to<JsonArray>();
SECTION("empty") {
check(array, "\x90");
}
SECTION("fixarray") {
array.add("hello");
array.add("world");
check(array, "\x92\xA5hello\xA5world");
}
SECTION("array 16") {
for (int i = 0; i < 16; i++) array.add(i);
check(array,
"\xDC\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D"
"\x0E\x0F");
}
// TODO: this test is too slow
// SECTION("array 32") {
// const char* nil = 0;
// for (int i = 0; i < 65536; i++) array.add(nil);
//
// check(array,
// std::string("\xDD\x00\x01\x00\x00", 5) + std::string(65536, 0xC0));
// }
}

View File

@ -0,0 +1,73 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <stdio.h>
#include <catch.hpp>
static void check(const JsonObject& object, const char* expected_data,
size_t expected_len) {
std::string expected(expected_data, expected_data + expected_len);
std::string actual;
size_t len = serializeMsgPack(object, actual);
CAPTURE(object);
REQUIRE(len == expected_len);
REQUIRE(actual == expected);
}
template <size_t N>
static void check(const JsonObject& object, const char (&expected_data)[N]) {
const size_t expected_len = N - 1;
check(object, expected_data, expected_len);
}
// TODO: used by the commented test
// static void check(const JsonObject& object, const std::string& expected) {
// check(object, expected.data(), expected.length());
//}
TEST_CASE("serialize MsgPack object") {
DynamicJsonDocument doc;
JsonObject& object = doc.to<JsonObject>();
SECTION("empty") {
check(object, "\x80");
}
SECTION("fixmap") {
object["hello"] = "world";
check(object, "\x81\xA5hello\xA5world");
}
SECTION("map 16") {
for (int i = 0; i < 16; ++i) {
char key[16];
sprintf(key, "i%X", i);
object[key] = i;
}
check(object,
"\xDE\x00\x10\xA2i0\x00\xA2i1\x01\xA2i2\x02\xA2i3\x03\xA2i4\x04\xA2i5"
"\x05\xA2i6\x06\xA2i7\x07\xA2i8\x08\xA2i9\x09\xA2iA\x0A\xA2iB\x0B\xA2"
"iC\x0C\xA2iD\x0D\xA2iE\x0E\xA2iF\x0F");
}
// TODO: improve performance and uncomment
// SECTION("map 32") {
// std::string expected("\xDF\x00\x01\x00\x00", 5);
//
// for (int i = 0; i < 65536; ++i) {
// char kv[16];
// sprintf(kv, "%04x", i);
// object[kv] = kv;
// expected += '\xA4';
// expected += kv;
// expected += '\xA4';
// expected += kv;
// }
//
// check(object, expected);
// }
}

View File

@ -0,0 +1,129 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
void check(JsonVariant variant, const char* expected_data,
size_t expected_len) {
std::string expected(expected_data, expected_data + expected_len);
std::string actual;
size_t len = serializeMsgPack(variant, actual);
CAPTURE(variant);
REQUIRE(len == expected_len);
REQUIRE(actual == expected);
}
template <size_t N>
void check(JsonVariant variant, const char (&expected_data)[N]) {
const size_t expected_len = N - 1;
check(variant, expected_data, expected_len);
}
void check(JsonVariant variant, const std::string& expected) {
check(variant, expected.data(), expected.length());
}
TEST_CASE("serialize MsgPack value") {
SECTION("undefined") {
check(JsonVariant(), "\xC0"); // we represent undefined as nil
}
SECTION("nil") {
const char* nil = 0; // ArduinoJson uses a string for null
check(nil, "\xC0");
}
SECTION("bool") {
check(false, "\xC2");
check(true, "\xC3");
}
SECTION("positive fixint") {
check(0, "\x00");
check(127, "\x7F");
}
SECTION("uint 8") {
check(128, "\xCC\x80");
check(255, "\xCC\xFF");
}
SECTION("uint 16") {
check(256, "\xCD\x01\x00");
check(0xFFFF, "\xCD\xFF\xFF");
}
SECTION("uint 32") {
check(0x00010000U, "\xCE\x00\x01\x00\x00");
check(0x12345678U, "\xCE\x12\x34\x56\x78");
check(0xFFFFFFFFU, "\xCE\xFF\xFF\xFF\xFF");
}
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
SECTION("uint 64") {
check(0x0001000000000000U, "\xCF\x00\x01\x00\x00\x00\x00\x00\x00");
check(0x123456789ABCDEF0U, "\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
check(0xFFFFFFFFFFFFFFFFU, "\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
}
#endif
SECTION("negative fixint") {
check(-1, "\xFF");
check(-32, "\xE0");
}
SECTION("int 8") {
check(-33, "\xD0\xDF");
check(-128, "\xD0\x80");
}
SECTION("int 16") {
check(-129, "\xD1\xFF\x7F");
check(-32768, "\xD1\x80\x00");
}
SECTION("int 32") {
check(-32769, "\xD2\xFF\xFF\x7F\xFF");
check(-2147483647 - 1, "\xD2\x80\x00\x00\x00");
}
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
SECTION("int 64") {
check(int64_t(0xFEDCBA9876543210), "\xD3\xFE\xDC\xBA\x98\x76\x54\x32\x10");
}
#endif
SECTION("float 32") {
check(1.25, "\xCA\x3F\xA0\x00\x00");
}
SECTION("float 64") {
check(3.1415, "\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F");
}
SECTION("fixstr") {
check("", "\xA0");
check("hello world hello world hello !",
"\xBFhello world hello world hello !");
}
SECTION("str 8") {
check("hello world hello world hello !!",
"\xD9\x20hello world hello world hello !!");
}
SECTION("str 16") {
std::string shortest(256, '?');
check(shortest.c_str(), std::string("\xDA\x01\x00", 3) + shortest);
std::string longest(65535, '?');
check(longest.c_str(), std::string("\xDA\xFF\xFF", 3) + longest);
}
SECTION("str 32") {
std::string shortest(65536, '?');
check(shortest.c_str(), std::string("\xDB\x00\x01\x00\x00", 5) + shortest);
}
}