mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 10:17:39 +02:00
Moved ancillary files to extras/
(fixes #1011)
This commit is contained in:
15
extras/tests/MsgPackSerializer/CMakeLists.txt
Normal file
15
extras/tests/MsgPackSerializer/CMakeLists.txt
Normal file
@ -0,0 +1,15 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2019
|
||||
# MIT License
|
||||
|
||||
add_executable(MsgPackSerializerTests
|
||||
destination_types.cpp
|
||||
measure.cpp
|
||||
misc.cpp
|
||||
serializeArray.cpp
|
||||
serializeObject.cpp
|
||||
serializeVariant.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(MsgPackSerializerTests catch)
|
||||
add_test(MsgPackSerializer MsgPackSerializerTests)
|
47
extras/tests/MsgPackSerializer/destination_types.cpp
Normal file
47
extras/tests/MsgPackSerializer/destination_types.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("serialize MsgPack to various destination types") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
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);
|
||||
}
|
||||
}
|
14
extras/tests/MsgPackSerializer/measure.cpp
Normal file
14
extras/tests/MsgPackSerializer/measure.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("measureMsgPack()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["hello"] = "world";
|
||||
|
||||
REQUIRE(measureMsgPack(doc) == 13);
|
||||
}
|
46
extras/tests/MsgPackSerializer/misc.cpp
Normal file
46
extras/tests/MsgPackSerializer/misc.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
#include <limits>
|
||||
|
||||
template <typename T>
|
||||
void check(T value, const std::string &expected) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
doc.to<JsonVariant>().set(value);
|
||||
char buffer[256] = "";
|
||||
size_t returnValue = serializeMsgPack(doc, buffer, sizeof(buffer));
|
||||
REQUIRE(expected == buffer);
|
||||
REQUIRE(expected.size() == returnValue);
|
||||
}
|
||||
|
||||
TEST_CASE("serializeMsgPack(MemberProxy)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
deserializeJson(doc, "{\"hello\":42}");
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
std::string result;
|
||||
|
||||
serializeMsgPack(obj["hello"], result);
|
||||
|
||||
REQUIRE(result == "*");
|
||||
}
|
||||
|
||||
TEST_CASE("serializeMsgPack(ElementProxy)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
deserializeJson(doc, "[42]");
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
std::string result;
|
||||
|
||||
serializeMsgPack(arr[0], result);
|
||||
|
||||
REQUIRE(result == "*");
|
||||
}
|
||||
|
||||
TEST_CASE("serializeMsgPack(JsonVariantSubscript)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
deserializeJson(doc, "[42]");
|
||||
JsonVariant var = doc.as<JsonVariant>();
|
||||
std::string result;
|
||||
|
||||
serializeMsgPack(var[0], result);
|
||||
|
||||
REQUIRE(result == "*");
|
||||
}
|
58
extras/tests/MsgPackSerializer/serializeArray.cpp
Normal file
58
extras/tests/MsgPackSerializer/serializeArray.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// 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);
|
||||
}
|
||||
|
||||
static void check(const JsonArray array, const std::string& expected) {
|
||||
check(array, expected.data(), expected.length());
|
||||
}
|
||||
|
||||
TEST_CASE("serialize MsgPack array") {
|
||||
DynamicJsonDocument doc(JSON_ARRAY_SIZE(65536));
|
||||
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");
|
||||
}
|
||||
|
||||
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, '\xc0'));
|
||||
}
|
||||
}
|
83
extras/tests/MsgPackSerializer/serializeObject.cpp
Normal file
83
extras/tests/MsgPackSerializer/serializeObject.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// 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(4096);
|
||||
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);
|
||||
// }
|
||||
|
||||
SECTION("serialized(const char*)") {
|
||||
object["hello"] = serialized("\xDB\x00\x01\x00\x00", 5);
|
||||
check(object, "\x81\xA5hello\xDB\x00\x01\x00\x00");
|
||||
}
|
||||
|
||||
SECTION("serialized(std::string)") {
|
||||
object["hello"] = serialized(std::string("\xDB\x00\x01\x00\x00", 5));
|
||||
check(object, "\x81\xA5hello\xDB\x00\x01\x00\x00");
|
||||
}
|
||||
}
|
138
extras/tests/MsgPackSerializer/serializeVariant.cpp
Normal file
138
extras/tests/MsgPackSerializer/serializeVariant.cpp
Normal file
@ -0,0 +1,138 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
template <typename T>
|
||||
void check(T value, const char* expected_data, size_t expected_len) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
variant.set(value);
|
||||
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 <typename T, size_t N>
|
||||
void check(T value, const char (&expected_data)[N]) {
|
||||
const size_t expected_len = N - 1;
|
||||
check(value, expected_data, expected_len);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void check(T value, const std::string& expected) {
|
||||
check(value, 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
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
SECTION("serialized(const char*)") {
|
||||
check(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF");
|
||||
check(serialized("\xDB\x00\x01\x00\x00", 5), "\xDB\x00\x01\x00\x00");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user