Reduced Unicode conversion code size (-122 bytes on AVR)

This commit is contained in:
Benoit Blanchon
2020-01-09 15:39:45 +01:00
parent 91b808381e
commit 5ec062cc71
5 changed files with 115 additions and 37 deletions

View File

@ -7,10 +7,9 @@
#include <catch.hpp>
TEST_CASE("Invalid JSON input") {
const char* testCases[] = {
"'\\u'", "'\\u000g'", "'\\u000'", "'\\u000G'", "'\\ud83d\\ud83d'",
"'\\udda4'", "'\\ud83d_'", "'\\u000/'", "\\x1234", "6a9",
"1,", "2]", "3}"};
const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'", "'\\u000G'",
"'\\u000/'", "\\x1234", "6a9", "1,",
"2]", "3}"};
const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
DynamicJsonDocument doc(4096);
@ -23,7 +22,14 @@ TEST_CASE("Invalid JSON input") {
}
TEST_CASE("Invalid JSON input that should pass") {
const char* testCases[] = {"nulL", "tru3", "fals3"};
const char* testCases[] = {
"nulL",
"tru3",
"fals3",
"'\\ud83d'", // leading surrogate without a trailing surrogate
"'\\udda4'", // trailing surrogate without a leading surrogate
"'\\ud83d\\ud83d'", // two leading surrogates
};
const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
DynamicJsonDocument doc(4096);

View File

@ -10,6 +10,7 @@ add_executable(MiscTests
StringWriter.cpp
TypeTraits.cpp
unsigned_char.cpp
Utf8.cpp
version.cpp
)

View File

@ -0,0 +1,59 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
using namespace ARDUINOJSON_NAMESPACE;
static void testCodepoint(uint32_t codepoint, std::string expected) {
char buffer[4096];
MemoryPool pool(buffer, 4096);
StringBuilder str(&pool);
CAPTURE(codepoint);
Utf8::encodeCodepoint(codepoint, str);
REQUIRE(str.complete() == expected);
}
TEST_CASE("Utf8::encodeCodepoint()") {
SECTION("U+0000") {
testCodepoint(0x0000, "");
}
SECTION("U+0001") {
testCodepoint(0x0001, "\x01");
}
SECTION("U+007F") {
testCodepoint(0x007F, "\x7f");
}
SECTION("U+0080") {
testCodepoint(0x0080, "\xc2\x80");
}
SECTION("U+07FF") {
testCodepoint(0x07FF, "\xdf\xbf");
}
SECTION("U+0800") {
testCodepoint(0x0800, "\xe0\xa0\x80");
}
SECTION("U+FFFF") {
testCodepoint(0xFFFF, "\xef\xbf\xbf");
}
SECTION("U+10000") {
testCodepoint(0x10000, "\xf0\x90\x80\x80");
}
SECTION("U+10FFFF") {
testCodepoint(0x10FFFF, "\xf4\x8f\xbf\xbf");
}
}