arduino/libraries/ArduinoJson/test/JsonArray/add.cpp

139 lines
3.4 KiB
C++
Raw Permalink Normal View History

2018-11-03 20:21:33 +00:00
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonArray::add()") {
DynamicJsonDocument doc;
2018-11-13 14:50:48 +00:00
JsonArray array = doc.to<JsonArray>();
2018-11-03 20:21:33 +00:00
SECTION("int") {
2018-11-13 14:50:48 +00:00
array.add(123);
REQUIRE(123 == array[0].as<int>());
REQUIRE(array[0].is<int>());
REQUIRE(array[0].is<double>());
2018-11-03 20:21:33 +00:00
}
SECTION("double") {
2018-11-13 14:50:48 +00:00
array.add(123.45);
REQUIRE(123.45 == array[0].as<double>());
REQUIRE(array[0].is<double>());
REQUIRE_FALSE(array[0].is<bool>());
2018-11-03 20:21:33 +00:00
}
SECTION("bool") {
2018-11-13 14:50:48 +00:00
array.add(true);
REQUIRE(true == array[0].as<bool>());
REQUIRE(array[0].is<bool>());
REQUIRE_FALSE(array[0].is<int>());
2018-11-03 20:21:33 +00:00
}
SECTION("const char*") {
const char* str = "hello";
2018-11-13 14:50:48 +00:00
array.add(str);
REQUIRE(str == array[0].as<std::string>());
REQUIRE(array[0].is<const char*>());
REQUIRE_FALSE(array[0].is<int>());
2018-11-03 20:21:33 +00:00
}
2018-11-13 14:50:48 +00:00
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("vla") {
int i = 16;
char vla[i];
strcpy(vla, "world");
array.add(vla);
REQUIRE(std::string("world") == array[0]);
}
#endif
2018-11-03 20:21:33 +00:00
SECTION("nested array") {
DynamicJsonDocument doc2;
JsonArray arr = doc2.to<JsonArray>();
2018-11-13 14:50:48 +00:00
array.add(arr);
2018-11-03 20:21:33 +00:00
2018-11-13 14:50:48 +00:00
REQUIRE(arr == array[0].as<JsonArray>());
REQUIRE(array[0].is<JsonArray>());
REQUIRE_FALSE(array[0].is<int>());
2018-11-03 20:21:33 +00:00
}
SECTION("nested object") {
DynamicJsonDocument doc2;
JsonObject obj = doc2.to<JsonObject>();
2018-11-13 14:50:48 +00:00
array.add(obj);
2018-11-03 20:21:33 +00:00
2018-11-13 14:50:48 +00:00
REQUIRE(obj == array[0].as<JsonObject>());
REQUIRE(array[0].is<JsonObject>());
REQUIRE_FALSE(array[0].is<int>());
2018-11-03 20:21:33 +00:00
}
SECTION("array subscript") {
const char* str = "hello";
DynamicJsonDocument doc2;
JsonArray arr = doc2.to<JsonArray>();
arr.add(str);
2018-11-13 14:50:48 +00:00
array.add(arr[0]);
2018-11-03 20:21:33 +00:00
2018-11-13 14:50:48 +00:00
REQUIRE(str == array[0]);
2018-11-03 20:21:33 +00:00
}
SECTION("object subscript") {
const char* str = "hello";
DynamicJsonDocument doc2;
JsonObject obj = doc2.to<JsonObject>();
obj["x"] = str;
2018-11-13 14:50:48 +00:00
array.add(obj["x"]);
2018-11-03 20:21:33 +00:00
2018-11-13 14:50:48 +00:00
REQUIRE(str == array[0]);
2018-11-03 20:21:33 +00:00
}
SECTION("should not duplicate const char*") {
2018-11-13 14:50:48 +00:00
array.add("world");
2018-11-03 20:21:33 +00:00
const size_t expectedSize = JSON_ARRAY_SIZE(1);
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate char*") {
2018-11-13 14:50:48 +00:00
array.add(const_cast<char*>("world"));
2018-11-03 20:21:33 +00:00
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate std::string") {
2018-11-13 14:50:48 +00:00
array.add(std::string("world"));
2018-11-03 20:21:33 +00:00
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should not duplicate serialized(const char*)") {
2018-11-13 14:50:48 +00:00
array.add(serialized("{}"));
2018-11-03 20:21:33 +00:00
const size_t expectedSize = JSON_ARRAY_SIZE(1);
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate serialized(char*)") {
2018-11-13 14:50:48 +00:00
array.add(serialized(const_cast<char*>("{}")));
2018-11-03 20:21:33 +00:00
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 2;
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate serialized(std::string)") {
2018-11-13 14:50:48 +00:00
array.add(serialized(std::string("{}")));
2018-11-03 20:21:33 +00:00
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 2;
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate serialized(std::string)") {
2018-11-13 14:50:48 +00:00
array.add(serialized(std::string("\0XX", 3)));
2018-11-03 20:21:33 +00:00
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 3;
REQUIRE(expectedSize == doc.memoryUsage());
}
}