Moved ancillary files to extras/ (fixes #1011)

This commit is contained in:
Benoit Blanchon
2019-09-03 15:11:05 +02:00
parent ed18e77655
commit b47ac27ac6
226 changed files with 19 additions and 54 deletions

View File

@ -0,0 +1,38 @@
# ArduinoJson - arduinojson.org
# Copyright Benoit Blanchon 2014-2019
# MIT License
add_executable(MiscTests
conflicts.cpp
FloatParts.cpp
StreamReader.cpp
StringAdapters.cpp
StringWriter.cpp
TypeTraits.cpp
unsigned_char.cpp
version.cpp
)
target_link_libraries(MiscTests catch)
add_test(Misc MiscTests)
add_executable(Issue978
Issue978.cpp
)
set_target_properties(Issue978
PROPERTIES
EXCLUDE_FROM_ALL TRUE
EXCLUDE_FROM_DEFAULT_BUILD TRUE
)
add_test(
NAME
Issue978
COMMAND
${CMAKE_COMMAND} --build . --target Issue978 --config $<CONFIGURATION>
WORKING_DIRECTORY
${CMAKE_BINARY_DIR}
)
set_tests_properties(Issue978
PROPERTIES
WILL_FAIL TRUE)

View File

@ -0,0 +1,44 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson/Numbers/FloatParts.hpp>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("FloatParts<double>") {
SECTION("1.7976931348623157E+308") {
FloatParts<double> parts(1.7976931348623157E+308);
REQUIRE(parts.integral == 1);
REQUIRE(parts.decimal == 797693135);
REQUIRE(parts.decimalPlaces == 9);
REQUIRE(parts.exponent == 308);
}
SECTION("4.94065645841247e-324") {
FloatParts<double> parts(4.94065645841247e-324);
REQUIRE(parts.integral == 4);
REQUIRE(parts.decimal == 940656458);
REQUIRE(parts.decimalPlaces == 9);
REQUIRE(parts.exponent == -324);
}
}
TEST_CASE("FloatParts<float>") {
SECTION("3.4E+38") {
FloatParts<float> parts(3.4E+38f);
REQUIRE(parts.integral == 3);
REQUIRE(parts.decimal == 4);
REQUIRE(parts.decimalPlaces == 1);
REQUIRE(parts.exponent == 38);
}
SECTION("1.17549435e38") {
FloatParts<float> parts(1.17549435e-38f);
REQUIRE(parts.integral == 1);
REQUIRE(parts.decimal == 175494);
REQUIRE(parts.decimalPlaces == 6);
REQUIRE(parts.exponent == -38);
}
}

View File

@ -0,0 +1,13 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
struct Stream {};
int main() {
Stream* stream = 0;
DynamicJsonDocument doc(1024);
deserializeJson(doc, stream);
}

View File

@ -0,0 +1,33 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("StdStreamReader") {
std::istringstream src("\x01\xFF");
StdStreamReader reader(src);
REQUIRE(reader.read() == 0x01);
REQUIRE(reader.read() == 0xFF);
REQUIRE(reader.read() == -1);
}
TEST_CASE("SafeCharPointerReader") {
SafeCharPointerReader reader("\x01\xFF", 2);
REQUIRE(reader.read() == 0x01);
REQUIRE(reader.read() == 0xFF);
REQUIRE(reader.read() == -1);
}
TEST_CASE("UnsafeCharPointerReader") {
UnsafeCharPointerReader reader("\x01\xFF");
REQUIRE(reader.read() == 0x01);
REQUIRE(reader.read() == 0xFF);
REQUIRE(reader.read() == 0);
}

View File

@ -0,0 +1,73 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "custom_string.hpp"
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("ConstRamStringAdapter") {
SECTION("null") {
ConstRamStringAdapter adapter(NULL);
REQUIRE(adapter.compare("bravo") < 0);
REQUIRE(adapter.compare(NULL) == 0);
REQUIRE(adapter.equals(NULL));
REQUIRE_FALSE(adapter.equals("charlie"));
}
SECTION("non-null") {
ConstRamStringAdapter adapter("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"));
}
}
TEST_CASE("std::string") {
std::string str("bravo");
StlStringAdapter<std::string> adapter = adaptString(str);
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"));
}
TEST_CASE("custom_string") {
custom_string str("bravo");
StlStringAdapter<custom_string> adapter = adaptString(str);
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"));
}
TEST_CASE("IsString<T>") {
SECTION("std::string") {
REQUIRE(IsString<std::string>::value == true);
}
SECTION("basic_string<wchar_t>") {
REQUIRE(IsString<std::basic_string<wchar_t> >::value == false);
}
SECTION("custom_string") {
REQUIRE(IsString<custom_string>::value == true);
}
}

View File

@ -0,0 +1,78 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "custom_string.hpp"
using namespace ARDUINOJSON_NAMESPACE;
template <typename StringWriter>
static size_t print(StringWriter& sb, const char* s) {
return sb.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
}
template <typename StringWriter, typename String>
void common_tests(StringWriter& sb, const String& output) {
SECTION("InitialState") {
REQUIRE(std::string("") == output);
}
SECTION("EmptyString") {
REQUIRE(0 == print(sb, ""));
REQUIRE(std::string("") == output);
}
SECTION("OneString") {
REQUIRE(4 == print(sb, "ABCD"));
REQUIRE(std::string("ABCD") == output);
}
SECTION("TwoStrings") {
REQUIRE(4 == print(sb, "ABCD"));
REQUIRE(4 == print(sb, "EFGH"));
REQUIRE(std::string("ABCDEFGH") == output);
}
}
TEST_CASE("StaticStringWriter") {
char output[20];
StaticStringWriter sb(output, sizeof(output));
common_tests(sb, static_cast<const char*>(output));
SECTION("OverCapacity") {
REQUIRE(19 == print(sb, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
REQUIRE(0 == print(sb, "ABC"));
REQUIRE(std::string("ABCDEFGHIJKLMNOPQRS") == output);
}
}
TEST_CASE("DynamicStringWriter<std::string>") {
std::string output;
DynamicStringWriter<std::string> sb(output);
common_tests(sb, output);
}
TEST_CASE("DynamicStringWriter<custom_string>") {
custom_string output;
DynamicStringWriter<custom_string> sb(output);
REQUIRE(4 == print(sb, "ABCD"));
REQUIRE("ABCD" == output);
}
TEST_CASE("IsWriteableString") {
SECTION("std::string") {
REQUIRE(IsWriteableString<std::string>::value == true);
}
SECTION("custom_string") {
REQUIRE(IsWriteableString<custom_string>::value == true);
}
SECTION("basic_string<wchar_t>") {
REQUIRE(IsWriteableString<std::basic_string<wchar_t> >::value == false);
}
}

View File

@ -0,0 +1,65 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("Polyfills/type_traits") {
SECTION("is_base_of") {
REQUIRE_FALSE(
static_cast<bool>(is_base_of<std::istream, std::ostringstream>::value));
REQUIRE(
static_cast<bool>(is_base_of<std::istream, std::istringstream>::value));
}
SECTION("is_array") {
REQUIRE_FALSE((is_array<const char*>::value));
REQUIRE((is_array<const char[]>::value));
REQUIRE((is_array<const char[10]>::value));
}
SECTION("is_const") {
CHECK(is_const<char>::value == false);
CHECK(is_const<const char>::value == true);
}
SECTION("is_signed") {
CHECK(is_signed<char>::value == true);
CHECK(is_signed<signed char>::value == true);
CHECK(is_signed<signed int>::value == true);
CHECK(is_signed<signed short>::value == true);
CHECK(is_signed<signed long>::value == true);
CHECK(is_signed<float>::value == true);
CHECK(is_signed<double>::value == true);
CHECK(is_signed<bool>::value == false);
}
SECTION("is_unsigned") {
CHECK(is_unsigned<unsigned char>::value == true);
CHECK(is_unsigned<unsigned int>::value == true);
CHECK(is_unsigned<unsigned short>::value == true);
CHECK(is_unsigned<unsigned long>::value == true);
CHECK(is_unsigned<bool>::value == true);
CHECK(is_unsigned<char>::value == false);
CHECK(is_unsigned<float>::value == false);
CHECK(is_unsigned<double>::value == false);
}
SECTION("IsVisitable") {
CHECK(IsVisitable<DeserializationError>::value == false);
CHECK(IsVisitable<JsonPair>::value == false);
CHECK(IsVisitable<VariantRef>::value == true);
CHECK(IsVisitable<VariantConstRef>::value == true);
CHECK(IsVisitable<ArrayRef>::value == true);
CHECK(IsVisitable<ElementProxy<ArrayRef> >::value == true);
CHECK(IsVisitable<ArrayConstRef>::value == true);
CHECK(IsVisitable<ObjectRef>::value == true);
CHECK((IsVisitable<MemberProxy<ObjectRef, const char*> >::value == true));
CHECK(IsVisitable<ObjectConstRef>::value == true);
CHECK(IsVisitable<DynamicJsonDocument>::value == true);
CHECK(IsVisitable<StaticJsonDocument<10> >::value == true);
}
}

View File

@ -0,0 +1,47 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
// Include any header that might use the conflicting macros
#include <cmath>
#include <string>
#include <iostream>
// All cores
#define bit()
#define constrain()
#define DEFAULT
#define DISABLED
#define HIGH
#define INPUT
#define LOW
#define max()
#define min()
#define OUTPUT
#define round()
#define sq()
#define word()
#define bitRead()
#define bitSet()
#define bitClear()
#define bitWrite()
#define interrupts()
#define lowByte()
#define highByte()
#define DEC
#define HEX
#define OCT
#define BIN
#define cbi()
#define sbi()
// ESP8266
#define _max()
#define _min()
// issue #839
#define BLOCKSIZE
#define CAPACITY
// catch.hpp mutes several warnings, this file also allows to detect them
#include "ArduinoJson.h"

View File

@ -0,0 +1,14 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#pragma once
#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>
custom_string;

View File

@ -0,0 +1,207 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#if defined(__clang__)
#define CONFLICTS_WITH_BUILTIN_OPERATOR
#endif
TEST_CASE("unsigned char[]") {
SECTION("deserializeJson()") {
unsigned char input[] = "{\"a\":42}";
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("deserializeMsgPack()") {
unsigned char input[] = "\xDE\x00\x01\xA5Hello\xA5world";
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
DeserializationError err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("JsonVariant") {
DynamicJsonDocument doc(4096);
SECTION("set") {
unsigned char value[] = "42";
JsonVariant variant = doc.to<JsonVariant>();
variant.set(value);
REQUIRE(42 == variant.as<int>());
}
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
SECTION("operator[]") {
unsigned char key[] = "hello";
deserializeJson(doc, "{\"hello\":\"world\"}");
JsonVariant variant = doc.as<JsonVariant>();
REQUIRE(std::string("world") == variant[key]);
}
#endif
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
SECTION("operator[] const") {
unsigned char key[] = "hello";
deserializeJson(doc, "{\"hello\":\"world\"}");
const JsonVariant variant = doc.as<JsonVariant>();
REQUIRE(std::string("world") == variant[key]);
}
#endif
SECTION("operator==") {
unsigned char comparand[] = "hello";
JsonVariant variant = doc.to<JsonVariant>();
variant.set("hello");
REQUIRE(comparand == variant);
REQUIRE(variant == comparand);
REQUIRE_FALSE(comparand != variant);
REQUIRE_FALSE(variant != comparand);
}
SECTION("operator!=") {
unsigned char comparand[] = "hello";
JsonVariant variant = doc.to<JsonVariant>();
variant.set("world");
REQUIRE(comparand != variant);
REQUIRE(variant != comparand);
REQUIRE_FALSE(comparand == variant);
REQUIRE_FALSE(variant == comparand);
}
}
SECTION("JsonObject") {
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
SECTION("operator[]") {
unsigned char key[] = "hello";
DynamicJsonDocument doc(4096);
JsonObject obj = doc.to<JsonObject>();
obj[key] = "world";
REQUIRE(std::string("world") == obj["hello"]);
}
SECTION("JsonObject::operator[] const") {
unsigned char key[] = "hello";
DynamicJsonDocument doc(4096);
deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("world") == obj[key]);
}
#endif
SECTION("containsKey()") {
unsigned char key[] = "hello";
DynamicJsonDocument doc(4096);
deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(true == obj.containsKey(key));
}
SECTION("remove()") {
unsigned char key[] = "hello";
DynamicJsonDocument doc(4096);
deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject obj = doc.as<JsonObject>();
obj.remove(key);
REQUIRE(0 == obj.size());
}
SECTION("createNestedArray()") {
unsigned char key[] = "hello";
DynamicJsonDocument doc(4096);
JsonObject obj = doc.to<JsonObject>();
obj.createNestedArray(key);
}
SECTION("createNestedObject()") {
unsigned char key[] = "hello";
DynamicJsonDocument doc(4096);
JsonObject obj = doc.to<JsonObject>();
obj.createNestedObject(key);
}
}
SECTION("MemberProxy") {
SECTION("operator=") { // issue #416
unsigned char value[] = "world";
DynamicJsonDocument doc(4096);
JsonObject obj = doc.to<JsonObject>();
obj["hello"] = value;
REQUIRE(std::string("world") == obj["hello"]);
}
SECTION("set()") {
unsigned char value[] = "world";
DynamicJsonDocument doc(4096);
JsonObject obj = doc.to<JsonObject>();
obj["hello"].set(value);
REQUIRE(std::string("world") == obj["hello"]);
}
}
SECTION("JsonArray") {
SECTION("add()") {
unsigned char value[] = "world";
DynamicJsonDocument doc(4096);
JsonArray arr = doc.to<JsonArray>();
arr.add(value);
REQUIRE(std::string("world") == arr[0]);
}
}
SECTION("ElementProxy") {
SECTION("set()") {
unsigned char value[] = "world";
DynamicJsonDocument doc(4096);
JsonArray arr = doc.to<JsonArray>();
arr.add("hello");
arr[0].set(value);
REQUIRE(std::string("world") == arr[0]);
}
SECTION("operator=") {
unsigned char value[] = "world";
DynamicJsonDocument doc(4096);
JsonArray arr = doc.to<JsonArray>();
arr.add("hello");
arr[0] = value;
REQUIRE(std::string("world") == arr[0]);
}
}
}

View File

@ -0,0 +1,18 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson/version.hpp>
#include <catch.hpp>
#include <sstream>
using Catch::Matchers::StartsWith;
TEST_CASE("ARDUINOJSON_VERSION") {
std::stringstream version;
version << ARDUINOJSON_VERSION_MAJOR << "." << ARDUINOJSON_VERSION_MINOR
<< "." << ARDUINOJSON_VERSION_REVISION;
REQUIRE_THAT(ARDUINOJSON_VERSION, StartsWith(version.str()));
}