forked from bblanchon/ArduinoJson
Added tests for Flash strings (closes #1070)
This commit is contained in:
@ -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)
|
||||
|
53
test/MixedConfiguration/enable_progmem_1.cpp
Normal file
53
test/MixedConfiguration/enable_progmem_1.cpp
Normal 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"));
|
||||
}
|
||||
}
|
41
test/MixedConfiguration/progmem_emulation.hpp
Normal file
41
test/MixedConfiguration/progmem_emulation.hpp
Normal 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)));
|
||||
}
|
Reference in New Issue
Block a user