forked from bblanchon/ArduinoJson
Change string copy policy: only string literal are stored by pointer
This commit is contained in:
1 parent
5f8e3c0f0f
commit
594dc707cb
27 files changed
+456
-197
No files matched your search
@@ -17,31 +17,112 @@ TEST_CASE("JsonArray::add(T)") {
|
||||
|
||||
SECTION("int") {
|
||||
array.add(123);
|
||||
|
||||
REQUIRE(123 == array[0].as<int>());
|
||||
REQUIRE(array[0].is<int>());
|
||||
REQUIRE(array[0].is<double>());
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("double") {
|
||||
array.add(123.45);
|
||||
|
||||
REQUIRE(123.45 == array[0].as<double>());
|
||||
REQUIRE(array[0].is<double>());
|
||||
REQUIRE_FALSE(array[0].is<bool>());
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
array.add(true);
|
||||
REQUIRE(true == array[0].as<bool>());
|
||||
|
||||
REQUIRE(array[0].as<bool>() == true);
|
||||
REQUIRE(array[0].is<bool>());
|
||||
REQUIRE_FALSE(array[0].is<int>());
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("string literal") {
|
||||
array.add("hello");
|
||||
|
||||
REQUIRE(array[0].as<std::string>() == "hello");
|
||||
REQUIRE(array[0].is<const char*>());
|
||||
REQUIRE(array[0].is<int>() == false);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
array.add("hello"_s);
|
||||
|
||||
REQUIRE(array[0].as<std::string>() == "hello");
|
||||
REQUIRE(array[0].is<const char*>() == true);
|
||||
REQUIRE(array[0].is<int>() == false);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* str = "hello";
|
||||
array.add(str);
|
||||
REQUIRE(str == array[0].as<std::string>());
|
||||
REQUIRE(array[0].is<const char*>());
|
||||
REQUIRE_FALSE(array[0].is<int>());
|
||||
|
||||
REQUIRE(array[0].as<std::string>() == "hello");
|
||||
REQUIRE(array[0].as<const char*>() != str);
|
||||
REQUIRE(array[0].is<const char*>() == true);
|
||||
REQUIRE(array[0].is<int>() == false);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("serialized(const char*)") {
|
||||
array.add(serialized("{}"));
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[{}]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("{}")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("serialized(char*)") {
|
||||
array.add(serialized(const_cast<char*>("{}")));
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[{}]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("{}")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("serialized(std::string)") {
|
||||
array.add(serialized("{}"_s));
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[{}]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("{}")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("serialized(std::string)") {
|
||||
array.add(serialized("\0XX"_s));
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\0XX]"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString(" XX")),
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
@@ -52,7 +133,12 @@ TEST_CASE("JsonArray::add(T)") {
|
||||
|
||||
array.add(vla);
|
||||
|
||||
REQUIRE("world"_s == array[0]);
|
||||
strcpy(vla, "hello");
|
||||
REQUIRE(array[0] == "world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -99,61 +185,6 @@ TEST_CASE("JsonArray::add(T)") {
|
||||
|
||||
REQUIRE(str == array[0]);
|
||||
}
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
array.add("world");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
array.add(const_cast<char*>("world"));
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
array.add("world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(const char*)") {
|
||||
array.add(serialized("{}"));
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("{}")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(char*)") {
|
||||
array.add(serialized(const_cast<char*>("{}")));
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("{}")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(std::string)") {
|
||||
array.add(serialized("{}"_s));
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("{}")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(std::string)") {
|
||||
array.add(serialized("\0XX"_s));
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString(" XX")),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonArray::add<T>()") {
|
||||
|
||||
@@ -59,15 +59,56 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
REQUIRE(false == array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* str = "hello";
|
||||
SECTION("string literal") {
|
||||
array[0] = "hello";
|
||||
|
||||
array[0] = str;
|
||||
REQUIRE(str == array[0].as<const char*>());
|
||||
REQUIRE(array[0].as<std::string>() == "hello");
|
||||
REQUIRE(true == array[0].is<const char*>());
|
||||
REQUIRE(false == array[0].is<int>());
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* str = "hello";
|
||||
array[0] = str;
|
||||
|
||||
REQUIRE(array[0].as<std::string>() == "hello");
|
||||
REQUIRE(true == array[0].is<const char*>());
|
||||
REQUIRE(false == array[0].is<int>());
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
array[0] = "hello"_s;
|
||||
|
||||
REQUIRE(array[0].as<std::string>() == "hello");
|
||||
REQUIRE(true == array[0].is<const char*>());
|
||||
REQUIRE(false == array[0].is<int>());
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("VLA") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
array.add("hello");
|
||||
array[0] = vla;
|
||||
|
||||
REQUIRE(array[0] == "world"_s);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("nested array") {
|
||||
JsonDocument doc2;
|
||||
JsonArray arr2 = doc2.to<JsonArray>();
|
||||
@@ -114,58 +155,11 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
REQUIRE(str == array[0]);
|
||||
}
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
array[0] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
array[0] = const_cast<char*>("world");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
array[0] = "world"_s;
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("array[0].to<JsonObject>()") {
|
||||
JsonObject obj = array[0].to<JsonObject>();
|
||||
REQUIRE(obj.isNull() == false);
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("set(VLA)") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
array.add("hello");
|
||||
array[0].set(vla);
|
||||
|
||||
REQUIRE("world"_s == array[0]);
|
||||
}
|
||||
|
||||
SECTION("operator=(VLA)") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
array.add("hello");
|
||||
array[0] = vla;
|
||||
|
||||
REQUIRE("world"_s == array[0]);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("Use a JsonVariant as index") {
|
||||
array[0] = 1;
|
||||
array[1] = 2;
|
||||
|
||||
@@ -5,37 +5,60 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ElementProxy = ArduinoJson::detail::ElementProxy<JsonDocument&>;
|
||||
|
||||
TEST_CASE("ElementProxy::add()") {
|
||||
JsonDocument doc;
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
doc.add<JsonVariant>();
|
||||
const ElementProxy& ep = doc[0];
|
||||
|
||||
SECTION("add(int)") {
|
||||
SECTION("integer") {
|
||||
ep.add(42);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[[42]]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("add(const char*)") {
|
||||
SECTION("string literal") {
|
||||
ep.add("world");
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("add(char[])") {
|
||||
SECTION("const char*") {
|
||||
const char* s = "world";
|
||||
ep.add(s);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("char[]") {
|
||||
char s[] = "world";
|
||||
ep.add(s);
|
||||
strcpy(s, "!!!!!");
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("set(vla)") {
|
||||
SECTION("VLA") {
|
||||
size_t i = 8;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
@@ -43,6 +66,10 @@ TEST_CASE("ElementProxy::add()") {
|
||||
ep.add(vla);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -15,23 +15,53 @@ using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
|
||||
TEST_CASE("MemberProxy::add()") {
|
||||
JsonDocument doc;
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
const auto& mp = doc["hello"];
|
||||
|
||||
SECTION("add(int)") {
|
||||
SECTION("integer") {
|
||||
mp.add(42);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("add(const char*)") {
|
||||
SECTION("string literal") {
|
||||
mp.add("world");
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* temp = "world";
|
||||
mp.add(temp);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("char[]") {
|
||||
char temp[] = "world";
|
||||
mp.add(temp);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("add(vla)") {
|
||||
SECTION("VLA") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
@@ -39,6 +69,10 @@ TEST_CASE("MemberProxy::add()") {
|
||||
mp.add(vla);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ TEST_CASE("JsonDocument::add(T)") {
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
SECTION("string literal") {
|
||||
doc.add("hello");
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
||||
@@ -35,6 +35,17 @@ TEST_CASE("JsonDocument::add(T)") {
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* value = "hello";
|
||||
doc.add(value);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
doc.add("example"_s);
|
||||
doc.add("example"_s);
|
||||
|
||||
@@ -20,11 +20,21 @@ TEST_CASE("JsonDocument::remove()") {
|
||||
REQUIRE(doc.as<std::string>() == "[1,3]");
|
||||
}
|
||||
|
||||
SECTION("string literal") {
|
||||
doc["a"] = 1;
|
||||
doc["a\0b"_s] = 2;
|
||||
doc["b"] = 3;
|
||||
|
||||
doc.remove("a\0b");
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"a\":1,\"b\":3}");
|
||||
}
|
||||
|
||||
SECTION("remove(const char *)") {
|
||||
doc["a"] = 1;
|
||||
doc["b"] = 2;
|
||||
|
||||
doc.remove("a");
|
||||
doc.remove(static_cast<const char*>("a"));
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"b\":2}");
|
||||
}
|
||||
|
||||
@@ -11,6 +11,21 @@ TEST_CASE("JsonDocument::set()") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("nullptr") {
|
||||
doc.set(nullptr);
|
||||
|
||||
REQUIRE(doc.isNull());
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("integer&") {
|
||||
int toto = 42;
|
||||
doc.set(toto);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "42");
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("integer") {
|
||||
doc.set(42);
|
||||
|
||||
@@ -18,13 +33,23 @@ TEST_CASE("JsonDocument::set()") {
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
SECTION("string literal") {
|
||||
doc.set("example");
|
||||
|
||||
REQUIRE(doc.as<const char*>() == "example"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* value = "example";
|
||||
doc.set(value);
|
||||
|
||||
REQUIRE(doc.as<const char*>() == "example"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("example")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
doc.set("example"_s);
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonDocument::operator[]") {
|
||||
@@ -16,8 +17,16 @@ TEST_CASE("JsonDocument::operator[]") {
|
||||
doc["abc\0d"_s] = "ABCD";
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* key = "abc";
|
||||
REQUIRE(doc[key] == "ABC");
|
||||
REQUIRE(cdoc[key] == "ABC");
|
||||
}
|
||||
|
||||
SECTION("string literal") {
|
||||
REQUIRE(doc["abc"] == "ABC");
|
||||
REQUIRE(cdoc["abc"] == "ABC");
|
||||
REQUIRE(doc["abc\0d"] == "ABCD");
|
||||
REQUIRE(cdoc["abc\0d"] == "ABCD");
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
@@ -94,3 +103,65 @@ TEST_CASE("JsonDocument automatically promotes to array") {
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[null,null,2]");
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument::operator[] key storage") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("string literal") {
|
||||
doc["hello"] = 0;
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* key = "hello";
|
||||
doc[key] = 0;
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("char[]") {
|
||||
char key[] = "hello";
|
||||
doc[key] = 0;
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
doc["hello"_s] = 0;
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
|
||||
!defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
|
||||
SECTION("VLA") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
doc[vla] = 0;
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -17,6 +17,15 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
JsonDocument doc(&spy);
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
|
||||
SECTION("string literal") {
|
||||
bool result = variant.set("hello\0world");
|
||||
|
||||
REQUIRE(result == true);
|
||||
CHECK(variant ==
|
||||
"hello"_s); // linked string cannot contain '\0' at the moment
|
||||
CHECK(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
char str[16];
|
||||
|
||||
@@ -25,8 +34,10 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
strcpy(str, "world");
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(variant == "world"); // stores by pointer
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
REQUIRE(variant == "hello"); // stores by copy
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("(const char*)0") {
|
||||
@@ -34,6 +45,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(variant.isNull());
|
||||
REQUIRE(variant.as<const char*>() == nullptr);
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
@@ -105,16 +117,14 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
#endif
|
||||
|
||||
SECTION("std::string") {
|
||||
std::string str;
|
||||
|
||||
str = "hello";
|
||||
std::string str = "hello\0world"_s;
|
||||
bool result = variant.set(str);
|
||||
str.replace(0, 5, "world");
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(variant == "hello"); // stores by copy
|
||||
REQUIRE(variant == "hello\0world"_s); // stores by copy
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("hello?world")),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -7,14 +7,8 @@
|
||||
#include <catch.hpp>
|
||||
#include <limits>
|
||||
|
||||
template <typename T>
|
||||
void checkValue(T expected) {
|
||||
JsonDocument doc;
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
|
||||
variant.set(expected);
|
||||
REQUIRE(expected == variant.as<T>());
|
||||
}
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
template <typename T>
|
||||
void checkReference(T& expected) {
|
||||
@@ -39,27 +33,29 @@ void checkNumericType() {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariant set()/get()") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
SECTION("SizeOfJsonInteger") {
|
||||
REQUIRE(8 == sizeof(JsonInteger));
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("Null") {
|
||||
checkValue<const char*>(NULL);
|
||||
}
|
||||
SECTION("const char*") {
|
||||
checkValue<const char*>("hello");
|
||||
}
|
||||
SECTION("std::string") {
|
||||
checkValue<std::string>("hello");
|
||||
}
|
||||
// /!\ Most test were moved to `JsonVariant/set.cpp`
|
||||
// TODO: move the remaining tests too
|
||||
|
||||
SECTION("False") {
|
||||
checkValue<bool>(false);
|
||||
variant.set(false);
|
||||
REQUIRE(variant.as<bool>() == false);
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("True") {
|
||||
checkValue<bool>(true);
|
||||
variant.set(true);
|
||||
REQUIRE(variant.as<bool>() == true);
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("Double") {
|
||||
@@ -129,10 +125,12 @@ TEST_CASE("JsonVariant set()/get()") {
|
||||
#endif
|
||||
|
||||
SECTION("CanStoreObject") {
|
||||
JsonDocument doc;
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
JsonDocument doc2;
|
||||
JsonObject object = doc2.to<JsonObject>();
|
||||
|
||||
checkValue<JsonObject>(object);
|
||||
variant.set(object);
|
||||
REQUIRE(variant.is<JsonObject>());
|
||||
REQUIRE(variant.as<JsonObject>() == object);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,13 +54,22 @@ TEST_CASE("JsonVariantConst::operator[]") {
|
||||
object["abc"_s] = "ABC";
|
||||
object["abc\0d"_s] = "ABCD";
|
||||
|
||||
SECTION("supports const char*") {
|
||||
SECTION("string literal") {
|
||||
REQUIRE(var["ab"] == "AB"_s);
|
||||
REQUIRE(var["abc"] == "ABC"_s);
|
||||
REQUIRE(var["abc\0d"] == "ABCD"_s);
|
||||
REQUIRE(var["def"].isNull());
|
||||
REQUIRE(var[0].isNull());
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
REQUIRE(var[static_cast<const char*>("ab")] == "AB"_s);
|
||||
REQUIRE(var[static_cast<const char*>("abc")] == "ABC"_s);
|
||||
REQUIRE(var[static_cast<const char*>("abc\0d")] == "ABC"_s);
|
||||
REQUIRE(var[static_cast<const char*>("def")].isNull());
|
||||
REQUIRE(var[static_cast<const char*>(0)].isNull());
|
||||
}
|
||||
|
||||
SECTION("supports std::string") {
|
||||
REQUIRE(var["ab"_s] == "AB"_s);
|
||||
REQUIRE(var["abc"_s] == "ABC"_s);
|
||||
|
||||
@@ -16,20 +16,29 @@ using ArduinoJson::JsonString;
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("adaptString()") {
|
||||
SECTION("string literal") {
|
||||
auto s = adaptString("bravo\0alpha");
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 11);
|
||||
CHECK(s.isLinked() == true);
|
||||
}
|
||||
|
||||
SECTION("null const char*") {
|
||||
auto s = adaptString(static_cast<const char*>(0));
|
||||
|
||||
CHECK(s.isNull() == true);
|
||||
CHECK(s.size() == 0);
|
||||
CHECK(s.isLinked() == true);
|
||||
}
|
||||
|
||||
SECTION("non-null const char*") {
|
||||
auto s = adaptString("bravo");
|
||||
const char* p = "bravo";
|
||||
auto s = adaptString(p);
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isLinked() == true);
|
||||
CHECK(s.isLinked() == false);
|
||||
CHECK(s.data() == p);
|
||||
}
|
||||
|
||||
SECTION("null const char* + size") {
|
||||
|
||||
@@ -139,7 +139,8 @@ TEST_CASE("serialize MsgPack value") {
|
||||
|
||||
SECTION("str 32") {
|
||||
std::string shortest(65536, '?');
|
||||
checkVariant(shortest.c_str(), "\xDB\x00\x01\x00\x00"_s + shortest);
|
||||
checkVariant(JsonString(shortest.c_str(), true), // force store by pointer
|
||||
"\xDB\x00\x01\x00\x00"_s + shortest);
|
||||
}
|
||||
|
||||
SECTION("serialized(const char*)") {
|
||||
|
||||
Reference in new issue
Block a user