Added tests for FlashStringAdapter

This commit is contained in:
Benoit Blanchon
2020-02-16 17:56:47 +01:00
parent e6ddfc7afb
commit 85499be855
15 changed files with 86 additions and 18 deletions

View File

@ -2,9 +2,14 @@
// Copyright Benoit Blanchon 2014-2020
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "custom_string.hpp"
#include "progmem_emulation.hpp"
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
#include <ArduinoJson/Strings/FlashStringAdapter.hpp>
#include <ArduinoJson/Strings/StlStringAdapter.hpp>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
@ -17,6 +22,8 @@ TEST_CASE("ConstRamStringAdapter") {
REQUIRE(adapter.equals(NULL));
REQUIRE_FALSE(adapter.equals("charlie"));
REQUIRE(adapter.size() == 0);
}
SECTION("non-null") {
@ -29,6 +36,36 @@ TEST_CASE("ConstRamStringAdapter") {
REQUIRE(adapter.equals("bravo"));
REQUIRE_FALSE(adapter.equals("charlie"));
REQUIRE(adapter.size() == 5);
}
}
TEST_CASE("FlashStringAdapter") {
SECTION("null") {
FlashStringAdapter adapter(NULL);
REQUIRE(adapter.compare("bravo") < 0);
REQUIRE(adapter.compare(NULL) == 0);
REQUIRE(adapter.equals(NULL));
REQUIRE_FALSE(adapter.equals("charlie"));
REQUIRE(adapter.size() == 0);
}
SECTION("non-null") {
FlashStringAdapter adapter = adaptString(F("bravo"));
REQUIRE(adapter.compare(NULL) > 0);
REQUIRE(adapter.compare("alpha") > 0);
REQUIRE(adapter.compare("bravo") == 0);
REQUIRE(adapter.compare("charlie") < 0);
REQUIRE(adapter.equals("bravo"));
REQUIRE_FALSE(adapter.equals("charlie"));
REQUIRE(adapter.size() == 5);
}
}
@ -43,6 +80,8 @@ TEST_CASE("std::string") {
REQUIRE(adapter.equals("bravo"));
REQUIRE_FALSE(adapter.equals("charlie"));
REQUIRE(adapter.size() == 5);
}
TEST_CASE("custom_string") {
@ -56,6 +95,8 @@ TEST_CASE("custom_string") {
REQUIRE(adapter.equals("bravo"));
REQUIRE_FALSE(adapter.equals("charlie"));
REQUIRE(adapter.size() == 5);
}
TEST_CASE("IsString<T>") {
@ -70,4 +111,8 @@ TEST_CASE("IsString<T>") {
SECTION("custom_string") {
REQUIRE(IsString<custom_string>::value == true);
}
SECTION("const __FlashStringHelper*") {
REQUIRE(IsString<const __FlashStringHelper*>::value == true);
}
}

View File

@ -6,8 +6,6 @@
#include <string>
using namespace ARDUINOJSON_NAMESPACE;
struct custom_char_traits : std::char_traits<char> {};
struct custom_allocator : std::allocator<char> {};
typedef std::basic_string<char, custom_char_traits, custom_allocator>

View File

@ -5,6 +5,7 @@
#pragma once
#include <ArduinoJson/Namespace.hpp>
#include <ArduinoJson/Polyfills/assert.hpp>
namespace ARDUINOJSON_NAMESPACE {
// Wraps a const char* so that the our functions are picked only if the
@ -18,6 +19,7 @@ struct pgm_p {
#ifndef strlen_P
inline size_t strlen_P(ARDUINOJSON_NAMESPACE::pgm_p s) {
const char* p = s.address;
ARDUINOJSON_ASSERT(p != NULL);
while (pgm_read_byte(p)) p++;
return size_t(p - s.address);
}
@ -27,6 +29,8 @@ inline size_t strlen_P(ARDUINOJSON_NAMESPACE::pgm_p s) {
inline int strncmp_P(const char* a, ARDUINOJSON_NAMESPACE::pgm_p b, size_t n) {
const char* s1 = a;
const char* s2 = b.address;
ARDUINOJSON_ASSERT(s1 != NULL);
ARDUINOJSON_ASSERT(s2 != NULL);
while (n-- > 0) {
char c1 = *s1++;
char c2 = static_cast<char>(pgm_read_byte(s2++));
@ -45,6 +49,8 @@ inline int strncmp_P(const char* a, ARDUINOJSON_NAMESPACE::pgm_p b, size_t n) {
inline int strcmp_P(const char* a, ARDUINOJSON_NAMESPACE::pgm_p b) {
const char* s1 = a;
const char* s2 = b.address;
ARDUINOJSON_ASSERT(s1 != NULL);
ARDUINOJSON_ASSERT(s2 != NULL);
for (;;) {
char c1 = *s1++;
char c2 = static_cast<char>(pgm_read_byte(s2++));
@ -62,6 +68,8 @@ inline int strcmp_P(const char* a, ARDUINOJSON_NAMESPACE::pgm_p b) {
inline void* memcpy_P(void* dst, ARDUINOJSON_NAMESPACE::pgm_p src, size_t n) {
uint8_t* d = reinterpret_cast<uint8_t*>(dst);
const char* s = src.address;
ARDUINOJSON_ASSERT(d != NULL);
ARDUINOJSON_ASSERT(s != NULL);
while (n-- > 0) {
*d++ = pgm_read_byte(s++);
}

View File

@ -6,6 +6,8 @@
#include <ArduinoJson/Namespace.hpp>
#include <stdint.h> // int8_t
namespace ARDUINOJSON_NAMESPACE {
inline int8_t safe_strcmp(const char* a, const char* b) {

View File

@ -7,6 +7,7 @@
#include <WString.h>
#include <ArduinoJson/Polyfills/safe_strcmp.hpp>
#include <ArduinoJson/Strings/IsString.hpp>
#include <ArduinoJson/Strings/StoragePolicy.hpp>
namespace ARDUINOJSON_NAMESPACE {

View File

@ -8,6 +8,7 @@
#include <string.h> // strcmp
#include <ArduinoJson/Polyfills/safe_strcmp.hpp>
#include <ArduinoJson/Strings/IsString.hpp>
#include <ArduinoJson/Strings/StoragePolicy.hpp>
namespace ARDUINOJSON_NAMESPACE {

View File

@ -4,7 +4,9 @@
#pragma once
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Polyfills/pgmspace.hpp>
#include <ArduinoJson/Strings/IsString.hpp>
#include <ArduinoJson/Strings/StoragePolicy.hpp>
namespace ARDUINOJSON_NAMESPACE {

View File

@ -0,0 +1,18 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#pragma once
#include <ArduinoJson/Polyfills/type_traits.hpp>
namespace ARDUINOJSON_NAMESPACE {
template <typename>
struct IsString : false_type {};
template <typename T>
struct IsString<const T> : IsString<T> {};
template <typename T>
struct IsString<T&> : IsString<T> {};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -4,7 +4,9 @@
#pragma once
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
#include <ArduinoJson/Strings/IsString.hpp>
#include <ArduinoJson/Strings/StoragePolicy.hpp>
namespace ARDUINOJSON_NAMESPACE {

View File

@ -5,6 +5,7 @@
#pragma once
#include <ArduinoJson/Namespace.hpp>
#include <ArduinoJson/Strings/IsString.hpp>
#include <ArduinoJson/Strings/StoragePolicy.hpp>
namespace ARDUINOJSON_NAMESPACE {

View File

@ -5,6 +5,7 @@
#pragma once
#include <ArduinoJson/Namespace.hpp>
#include <ArduinoJson/Strings/IsString.hpp>
#include <ArduinoJson/Strings/StoragePolicy.hpp>
#include <string.h> // strcmp

View File

@ -4,7 +4,9 @@
#pragma once
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Namespace.hpp>
#include <ArduinoJson/Strings/IsString.hpp>
#include <ArduinoJson/Strings/StoragePolicy.hpp>
#include <string>

View File

@ -5,6 +5,7 @@
#pragma once
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
#include <ArduinoJson/Strings/IsString.hpp>
#include <ArduinoJson/Strings/StoragePolicy.hpp>
namespace ARDUINOJSON_NAMESPACE {

View File

@ -4,20 +4,6 @@
#pragma once
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Polyfills/type_traits.hpp>
namespace ARDUINOJSON_NAMESPACE {
template <typename>
struct IsString : false_type {};
template <typename T>
struct IsString<const T> : IsString<T> {};
template <typename T>
struct IsString<T&> : IsString<T> {};
} // namespace ARDUINOJSON_NAMESPACE
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
#include <ArduinoJson/Strings/RamStringAdapter.hpp>
#include <ArduinoJson/Strings/SizedRamStringAdapter.hpp>