Added tests for Flash strings (closes #1070)

This commit is contained in:
Benoit Blanchon
2019-08-11 10:47:19 +02:00
parent 63d7d87080
commit 0d339300f9
6 changed files with 104 additions and 9 deletions

View File

@ -17,6 +17,7 @@ add_executable(MixedConfigurationTests
use_double_1.cpp
use_long_long_0.cpp
use_long_long_1.cpp
enable_progmem_1.cpp
)
target_link_libraries(MixedConfigurationTests catch)

View File

@ -0,0 +1,53 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include "progmem_emulation.hpp"
#define ARDUINOJSON_ENABLE_PROGMEM 1
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("Flash strings") {
DynamicJsonDocument doc(2048);
SECTION("deserializeJson()") {
DeserializationError err = deserializeJson(doc, F("{'hello':'world'}"));
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc["hello"] == "world");
}
SECTION("JsonDocument::operator[]") {
doc[F("hello")] = F("world");
REQUIRE(doc["hello"] == "world");
}
SECTION("JsonDocument::add()") {
doc.add(F("world"));
REQUIRE(doc[0] == "world");
}
SECTION("JsonVariant::set()") {
JsonVariant var = doc.to<JsonVariant>();
var.set(F("world"));
REQUIRE(var == "world");
}
SECTION("MemberProxy::operator==") {
doc["hello"] = "world";
REQUIRE(doc["hello"] == F("world"));
}
SECTION("ElementProxy::operator==") {
doc.add("world");
REQUIRE(doc[0] == F("world"));
}
}

View File

@ -0,0 +1,41 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <stdint.h> // uint8_t
#include <string.h> // strcmp, strlen...
class __FlashStringHelper;
typedef char prog_char;
typedef void prog_void;
inline const void* convertPtrToFlash(const void* s) {
return reinterpret_cast<const char*>(s) + 42;
}
inline const void* convertFlashToPtr(const void* s) {
return reinterpret_cast<const char*>(s) - 42;
}
#define F(X) reinterpret_cast<const __FlashStringHelper*>(convertPtrToFlash(X))
inline uint8_t pgm_read_byte_near(const void* p) {
return *reinterpret_cast<const uint8_t*>(convertFlashToPtr(p));
}
inline void* memcpy_P(void* a, const prog_void* b, size_t n) {
return memcpy(a, convertFlashToPtr(b), n);
}
inline int strcmp_P(const char* a, const prog_char* b) {
return strcmp(a, reinterpret_cast<const char*>(convertFlashToPtr(b)));
}
inline int strncmp_P(const char* a, const prog_char* b, size_t n) {
return strncmp(a, reinterpret_cast<const char*>(convertFlashToPtr(b)), n);
}
inline size_t strlen_P(const prog_char* s) {
return strlen(reinterpret_cast<const char*>(convertFlashToPtr(s)));
}