forked from bblanchon/ArduinoJson
Added deserializeMsgPack()
(issue #358)
This commit is contained in:
@ -73,5 +73,6 @@ add_subdirectory(JsonSerializer)
|
||||
add_subdirectory(JsonVariant)
|
||||
add_subdirectory(JsonWriter)
|
||||
add_subdirectory(Misc)
|
||||
add_subdirectory(MsgPack)
|
||||
add_subdirectory(Polyfills)
|
||||
add_subdirectory(StaticJsonBuffer)
|
||||
|
@ -6,8 +6,8 @@
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
||||
#include <ArduinoJson/Serialization/DynamicStringBuilder.hpp>
|
||||
#include <ArduinoJson/Serialization/JsonWriter.hpp>
|
||||
#include <ArduinoJson/Json/Serialization/JsonWriter.hpp>
|
||||
#include <ArduinoJson/Print/DynamicStringBuilder.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
|
@ -4,8 +4,8 @@
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <ArduinoJson/Serialization/JsonWriter.hpp>
|
||||
#include <ArduinoJson/Serialization/StaticStringBuilder.hpp>
|
||||
#include <ArduinoJson/Json/Serialization/JsonWriter.hpp>
|
||||
#include <ArduinoJson/Print/StaticStringBuilder.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Serialization/FloatParts.hpp>
|
||||
#include <ArduinoJson/Json/Serialization/FloatParts.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
16
test/MsgPack/CMakeLists.txt
Normal file
16
test/MsgPack/CMakeLists.txt
Normal file
@ -0,0 +1,16 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(MsgPackTests
|
||||
deserializationErrors.cpp
|
||||
deserializeArray.cpp
|
||||
deserializeObject.cpp
|
||||
deserializeVariant.cpp
|
||||
deserializeStaticVariant.cpp
|
||||
doubleToFloat.cpp
|
||||
MsgPackError.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(MsgPackTests catch)
|
||||
add_test(MsgPack MsgPackTests)
|
40
test/MsgPack/MsgPackError.cpp
Normal file
40
test/MsgPack/MsgPackError.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
void testStringification(MsgPackError error, std::string expected) {
|
||||
REQUIRE(error.c_str() == expected);
|
||||
}
|
||||
|
||||
void testBoolification(MsgPackError error, bool expected) {
|
||||
CHECK(error == expected);
|
||||
}
|
||||
|
||||
#define TEST_STRINGIFICATION(symbol) \
|
||||
testStringification(MsgPackError::symbol, #symbol)
|
||||
|
||||
#define TEST_BOOLIFICATION(symbol, expected) \
|
||||
testBoolification(MsgPackError::symbol, expected)
|
||||
|
||||
TEST_CASE("MsgPackError") {
|
||||
SECTION("c_str()") {
|
||||
TEST_STRINGIFICATION(Ok);
|
||||
TEST_STRINGIFICATION(NotSupported);
|
||||
TEST_STRINGIFICATION(NoMemory);
|
||||
TEST_STRINGIFICATION(NotAnArray);
|
||||
TEST_STRINGIFICATION(NotAnObject);
|
||||
TEST_STRINGIFICATION(TooDeep);
|
||||
}
|
||||
|
||||
SECTION("as boolean") {
|
||||
TEST_BOOLIFICATION(Ok, false);
|
||||
TEST_BOOLIFICATION(NotSupported, true);
|
||||
TEST_BOOLIFICATION(NoMemory, true);
|
||||
TEST_BOOLIFICATION(NotAnArray, true);
|
||||
TEST_BOOLIFICATION(NotAnObject, true);
|
||||
TEST_BOOLIFICATION(TooDeep, true);
|
||||
}
|
||||
}
|
58
test/MsgPack/deserializationErrors.cpp
Normal file
58
test/MsgPack/deserializationErrors.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(const char* input, MsgPackError expected,
|
||||
uint8_t nestingLimit = 10) {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input, nestingLimit);
|
||||
|
||||
REQUIRE(error == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("Errors returned by deserializeMsgPack()") {
|
||||
SECTION("unsupported") {
|
||||
check("\xc4", MsgPackError::NotSupported); // bin 8
|
||||
check("\xc5", MsgPackError::NotSupported); // bin 16
|
||||
check("\xc6", MsgPackError::NotSupported); // bin 32
|
||||
check("\xc7", MsgPackError::NotSupported); // ext 8
|
||||
check("\xc8", MsgPackError::NotSupported); // ext 16
|
||||
check("\xc9", MsgPackError::NotSupported); // ext 32
|
||||
check("\xd4", MsgPackError::NotSupported); // fixext 1
|
||||
check("\xd5", MsgPackError::NotSupported); // fixext 2
|
||||
check("\xd6", MsgPackError::NotSupported); // fixext 4
|
||||
check("\xd7", MsgPackError::NotSupported); // fixext 8
|
||||
check("\xd8", MsgPackError::NotSupported); // fixext 16
|
||||
}
|
||||
|
||||
SECTION("unsupported in array") {
|
||||
check("\x91\xc4", MsgPackError::NotSupported);
|
||||
}
|
||||
|
||||
SECTION("unsupported in map") {
|
||||
check("\x81\xc4\x00\xA1H", MsgPackError::NotSupported);
|
||||
check("\x81\xA1H\xc4\x00", MsgPackError::NotSupported);
|
||||
}
|
||||
|
||||
SECTION("integer as key") {
|
||||
check("\x81\x01\xA1H", MsgPackError::NotSupported);
|
||||
}
|
||||
|
||||
SECTION("object too deep") {
|
||||
check("\x80", MsgPackError::TooDeep, 0); // {}
|
||||
check("\x80", MsgPackError::Ok, 1); // {}
|
||||
check("\x81\xA1H\x80", MsgPackError::TooDeep, 1); // {H:{}}
|
||||
check("\x81\xA1H\x80", MsgPackError::Ok, 2); // {H:{}}
|
||||
}
|
||||
|
||||
SECTION("array too deep") {
|
||||
check("\x90", MsgPackError::TooDeep, 0); // []
|
||||
check("\x90", MsgPackError::Ok, 1); // []
|
||||
check("\x91\x90", MsgPackError::TooDeep, 1); // [[]]
|
||||
check("\x91\x90", MsgPackError::Ok, 2); // [[]]
|
||||
}
|
||||
}
|
85
test/MsgPack/deserializeArray.cpp
Normal file
85
test/MsgPack/deserializeArray.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
DynamicJsonArray array;
|
||||
|
||||
SECTION("not an array") {
|
||||
const char* input = "\xA0";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::NotAnArray);
|
||||
}
|
||||
|
||||
SECTION("fixarray") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\x90";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two integers") {
|
||||
const char* input = "\x92\x01\x02";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
REQUIRE(array[0] == 1);
|
||||
REQUIRE(array[1] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("array 16") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDC\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two strings") {
|
||||
const char* input = "\xDC\x00\x02\xA5hello\xA5world";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::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";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::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";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
REQUIRE(array[0] == 0.0f);
|
||||
REQUIRE(array[1] == 3.14f);
|
||||
}
|
||||
}
|
||||
}
|
86
test/MsgPack/deserializeObject.cpp
Normal file
86
test/MsgPack/deserializeObject.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeMsgPack(JsonObject&)") {
|
||||
DynamicJsonObject object;
|
||||
|
||||
SECTION("not an object") {
|
||||
const char* input = "\xA0";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::NotAnObject);
|
||||
}
|
||||
|
||||
SECTION("fixmap") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\x80";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two integers") {
|
||||
const char* input = "\x82\xA3one\x01\xA3two\x02";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 2);
|
||||
REQUIRE(object["one"] == 1);
|
||||
REQUIRE(object["two"] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("map 16") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDE\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two strings") {
|
||||
const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 2);
|
||||
REQUIRE(object["H"] == "hello");
|
||||
REQUIRE(object["W"] == "world");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("map 32") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDF\x00\x00\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.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";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 2);
|
||||
REQUIRE(object["zero"] == 0.0f);
|
||||
REQUIRE(object["pi"] == 3.14f);
|
||||
}
|
||||
}
|
||||
}
|
131
test/MsgPack/deserializeStaticVariant.cpp
Normal file
131
test/MsgPack/deserializeStaticVariant.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
// 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, MsgPackError expected) {
|
||||
StaticJsonVariant<Capacity> variant;
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack(StaticJsonVariant&)") {
|
||||
SECTION("single values always fit") {
|
||||
check<0>("\xc0", MsgPackError::Ok); // nil
|
||||
check<0>("\xc2", MsgPackError::Ok); // false
|
||||
check<0>("\xc3", MsgPackError::Ok); // true
|
||||
check<0>("\xcc\x00", MsgPackError::Ok); // uint 8
|
||||
check<0>("\xcd\x30\x39", MsgPackError::Ok); // uint 16
|
||||
check<0>("\xCE\x12\x34\x56\x78", MsgPackError::Ok); // uint 32
|
||||
}
|
||||
|
||||
SECTION("fixstr") {
|
||||
check<0>("\xA0", MsgPackError::Ok);
|
||||
check<0>("\xA1H", MsgPackError::NoMemory);
|
||||
check<4>("\xA1H", MsgPackError::Ok);
|
||||
check<4>("\xA5Hello", MsgPackError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("str 8") {
|
||||
check<0>("\xD9\x00", MsgPackError::Ok);
|
||||
check<0>("\xD9\x01H", MsgPackError::NoMemory);
|
||||
check<4>("\xD9\x01H", MsgPackError::Ok);
|
||||
check<4>("\xD9\x05Hello", MsgPackError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("str 16") {
|
||||
check<0>("\xDA\x00\x00", MsgPackError::Ok);
|
||||
check<0>("\xDA\x00\x01H", MsgPackError::NoMemory);
|
||||
check<4>("\xDA\x00\x01H", MsgPackError::Ok);
|
||||
check<4>("\xDA\x00\x05Hello", MsgPackError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("str 32") {
|
||||
check<0>("\xDB\x00\x00\x00\x00", MsgPackError::Ok);
|
||||
check<0>("\xDB\x00\x00\x00\x01H", MsgPackError::NoMemory);
|
||||
check<4>("\xDB\x00\x00\x00\x01H", MsgPackError::Ok);
|
||||
check<4>("\xDB\x00\x00\x00\x05Hello", MsgPackError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("fixarray") {
|
||||
check<JSON_ARRAY_SIZE(0)>("\x90", MsgPackError::Ok); // []
|
||||
check<JSON_ARRAY_SIZE(0)>("\x91\x01", MsgPackError::NoMemory); // [1]
|
||||
check<JSON_ARRAY_SIZE(1)>("\x91\x01", MsgPackError::Ok); // [1]
|
||||
check<JSON_ARRAY_SIZE(1)>("\x92\x01\x02", MsgPackError::NoMemory); // [1,2]
|
||||
}
|
||||
|
||||
SECTION("array 16") {
|
||||
check<JSON_ARRAY_SIZE(0)>("\xDC\x00\x00", MsgPackError::Ok);
|
||||
check<JSON_ARRAY_SIZE(0)>("\xDC\x00\x01\x01", MsgPackError::NoMemory);
|
||||
check<JSON_ARRAY_SIZE(1)>("\xDC\x00\x01\x01", MsgPackError::Ok);
|
||||
check<JSON_ARRAY_SIZE(1)>("\xDC\x00\x02\x01\x02", MsgPackError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("array 32") {
|
||||
check<JSON_ARRAY_SIZE(0)>("\xDD\x00\x00\x00\x00", MsgPackError::Ok);
|
||||
check<JSON_ARRAY_SIZE(0)>("\xDD\x00\x00\x00\x01\x01",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_ARRAY_SIZE(1)>("\xDD\x00\x00\x00\x01\x01", MsgPackError::Ok);
|
||||
check<JSON_ARRAY_SIZE(1)>("\xDD\x00\x00\x00\x02\x01\x02",
|
||||
MsgPackError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("fixmap") {
|
||||
SECTION("{}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\x80", MsgPackError::Ok);
|
||||
}
|
||||
SECTION("{H:1}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\x81\xA1H\x01",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\x81\xA1H\x01", MsgPackError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\x82\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2*epsilon>("\x82\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::Ok);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("map 16") {
|
||||
SECTION("{}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x00", MsgPackError::Ok);
|
||||
}
|
||||
SECTION("{H:1}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x01\xA1H\x01",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDE\x00\x01\xA1H\x01", MsgPackError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDE\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2*epsilon>("\xDE\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::Ok);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("map 32") {
|
||||
SECTION("{}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\xDF\x00\x00\x00\x00", MsgPackError::Ok);
|
||||
}
|
||||
SECTION("{H:1}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\xDF\x00\x00\x00\x01\xA1H\x01",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDF\x00\x00\x00\x01\xA1H\x01",
|
||||
MsgPackError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2*epsilon>("\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::Ok);
|
||||
}
|
||||
}
|
||||
}
|
277
test/MsgPack/deserializeVariant.cpp
Normal file
277
test/MsgPack/deserializeVariant.cpp
Normal file
@ -0,0 +1,277 @@
|
||||
// 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) {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.is<T>());
|
||||
REQUIRE(variant.as<T>() == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack(JsonVariant&)") {
|
||||
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"));
|
||||
}
|
||||
|
||||
SECTION("fixarray") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\x90";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two integers") {
|
||||
const char* input = "\x92\x01\x02";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant[0] == 1);
|
||||
REQUIRE(variant[1] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("array 16") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDC\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two strings") {
|
||||
const char* input = "\xDC\x00\x02\xA5hello\xA5world";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant[0] == "hello");
|
||||
REQUIRE(variant[1] == "world");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("array 32") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDD\x00\x00\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two floats") {
|
||||
const char* input =
|
||||
"\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant[0] == 0.0f);
|
||||
REQUIRE(variant[1] == 3.14f);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("fixmap") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\x80";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two integers") {
|
||||
const char* input = "\x82\xA3one\x01\xA3two\x02";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant["one"] == 1);
|
||||
REQUIRE(variant["two"] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("map 16") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDE\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two strings") {
|
||||
const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant["H"] == "hello");
|
||||
REQUIRE(variant["W"] == "world");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("map 32") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDF\x00\x00\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.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";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant["zero"] == 0.0f);
|
||||
REQUIRE(variant["pi"] == 3.14f);
|
||||
}
|
||||
}
|
||||
}
|
25
test/MsgPack/doubleToFloat.cpp
Normal file
25
test/MsgPack/doubleToFloat.cpp
Normal 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);
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Polyfills/isFloat.hpp>
|
||||
#include <ArduinoJson/Text/isFloat.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
@ -2,7 +2,7 @@
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Polyfills/isInteger.hpp>
|
||||
#include <ArduinoJson/Text/isInteger.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
@ -2,7 +2,7 @@
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Polyfills/parseFloat.hpp>
|
||||
#include <ArduinoJson/Text/parseFloat.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
@ -3,7 +3,7 @@
|
||||
// MIT License
|
||||
|
||||
#include <stdint.h>
|
||||
#include <ArduinoJson/Polyfills/parseInteger.hpp>
|
||||
#include <ArduinoJson/Text/parseInteger.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
Reference in New Issue
Block a user