From ba5cdab6197bced6120e0e2a8d711cabf7b36e9b Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 15 Jun 2021 18:52:18 +0200 Subject: [PATCH] Test: extracted executable Cpp11Tests --- CMakeLists.txt | 2 +- extras/CompileOptions.cmake | 6 --- extras/tests/CMakeLists.txt | 4 ++ extras/tests/Cpp11/CMakeLists.txt | 32 +++++++++++++++ .../cpp11.cpp => Cpp11/issue1120.cpp} | 36 ----------------- extras/tests/Cpp11/nullptr.cpp | 39 +++++++++++++++++++ .../use_long_long_0.cpp | 0 .../use_long_long_1.cpp | 0 .../tests/MixedConfiguration/CMakeLists.txt | 8 +--- .../MixedConfiguration/enable_infinity_0.cpp | 2 +- .../MixedConfiguration/enable_infinity_1.cpp | 3 +- .../tests/MixedConfiguration/enable_nan_0.cpp | 2 +- .../tests/MixedConfiguration/enable_nan_1.cpp | 2 +- src/ArduinoJson/Configuration.hpp | 10 ++++- 14 files changed, 90 insertions(+), 56 deletions(-) create mode 100644 extras/tests/Cpp11/CMakeLists.txt rename extras/tests/{MixedConfiguration/cpp11.cpp => Cpp11/issue1120.cpp} (68%) create mode 100644 extras/tests/Cpp11/nullptr.cpp rename extras/tests/{MixedConfiguration => Cpp11}/use_long_long_0.cpp (100%) rename extras/tests/{MixedConfiguration => Cpp11}/use_long_long_1.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index a42759e1..d488c4ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ # Copyright Benoit Blanchon 2014-2021 # MIT License -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.3) project(ArduinoJson VERSION 6.18.0) diff --git a/extras/CompileOptions.cmake b/extras/CompileOptions.cmake index 356c4077..3421ba06 100644 --- a/extras/CompileOptions.cmake +++ b/extras/CompileOptions.cmake @@ -23,12 +23,6 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)") -Wundef ) - if(NOT MINGW) - add_compile_options( - -std=c++98 - ) - endif() - if(${COVERAGE}) set(CMAKE_CXX_FLAGS "-fprofile-arcs -ftest-coverage") endif() diff --git a/extras/tests/CMakeLists.txt b/extras/tests/CMakeLists.txt index 20ce7e83..6bd2eaf4 100644 --- a/extras/tests/CMakeLists.txt +++ b/extras/tests/CMakeLists.txt @@ -2,11 +2,15 @@ # Copyright Benoit Blanchon 2014-2021 # MIT License +set(CMAKE_CXX_STANDARD 98) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + add_subdirectory(catch) link_libraries(ArduinoJson catch) include_directories(Helpers) +add_subdirectory(Cpp11) add_subdirectory(FailingBuilds) add_subdirectory(IntegrationTests) add_subdirectory(JsonArray) diff --git a/extras/tests/Cpp11/CMakeLists.txt b/extras/tests/Cpp11/CMakeLists.txt new file mode 100644 index 00000000..bae9fa91 --- /dev/null +++ b/extras/tests/Cpp11/CMakeLists.txt @@ -0,0 +1,32 @@ +# ArduinoJson - https://arduinojson.org +# Copyright Benoit Blanchon 2014-2021 +# MIT License + +if("cxx_nullptr" IN_LIST CMAKE_CXX_COMPILE_FEATURES) + list(APPEND SOURCES nullptr.cpp) + add_definitions(-DARDUINOJSON_HAS_NULLPTR=1) +endif() + +if("cxx_auto_type" IN_LIST CMAKE_CXX_COMPILE_FEATURES AND "cxx_constexpr" IN_LIST CMAKE_CXX_COMPILE_FEATURES) + list(APPEND SOURCES issue1120.cpp) +endif() + +if("cxx_long_long_type" IN_LIST CMAKE_CXX_COMPILE_FEATURES) + list(APPEND SOURCES use_long_long_0.cpp use_long_long_1.cpp) +endif() + +if(NOT SOURCES) + return() +endif() + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_executable(Cpp11Tests ${SOURCES}) + +add_test(Cpp11 Cpp11Tests) + +set_tests_properties(Cpp11 + PROPERTIES + LABELS "Catch" +) diff --git a/extras/tests/MixedConfiguration/cpp11.cpp b/extras/tests/Cpp11/issue1120.cpp similarity index 68% rename from extras/tests/MixedConfiguration/cpp11.cpp rename to extras/tests/Cpp11/issue1120.cpp index df81e35c..fbee4778 100644 --- a/extras/tests/MixedConfiguration/cpp11.cpp +++ b/extras/tests/Cpp11/issue1120.cpp @@ -2,40 +2,6 @@ #include -#if __cplusplus >= 201103L - -TEST_CASE("nullptr") { - DynamicJsonDocument doc(4096); - JsonVariant variant = doc.to(); - - SECTION("JsonVariant == nullptr") { - REQUIRE((variant == nullptr)); - REQUIRE_FALSE((variant != nullptr)); - } - - SECTION("JsonVariant != nullptr") { - variant.set(42); - - REQUIRE_FALSE((variant == nullptr)); - REQUIRE((variant != nullptr)); - } - - SECTION("JsonVariant.set(nullptr)") { - variant.set(42); - variant.set(nullptr); - - REQUIRE(variant.isNull()); - } - - SECTION("JsonVariant.is()") { - variant.set(42); - REQUIRE(variant.is() == false); - - variant.clear(); - REQUIRE(variant.is() == true); - } -} - TEST_CASE("Issue #1120") { StaticJsonDocument<500> doc; constexpr char str[] = @@ -90,5 +56,3 @@ TEST_CASE("Issue #1120") { } } } - -#endif diff --git a/extras/tests/Cpp11/nullptr.cpp b/extras/tests/Cpp11/nullptr.cpp new file mode 100644 index 00000000..cb3ba0ff --- /dev/null +++ b/extras/tests/Cpp11/nullptr.cpp @@ -0,0 +1,39 @@ +#include + +#include + +#if !ARDUINOJSON_HAS_NULLPTR +# error ARDUINOJSON_HAS_NULLPTR must be set to 1 +#endif + +TEST_CASE("nullptr") { + DynamicJsonDocument doc(4096); + JsonVariant variant = doc.to(); + + SECTION("JsonVariant == nullptr") { + REQUIRE((variant == nullptr)); + REQUIRE_FALSE((variant != nullptr)); + } + + SECTION("JsonVariant != nullptr") { + variant.set(42); + + REQUIRE_FALSE((variant == nullptr)); + REQUIRE((variant != nullptr)); + } + + SECTION("JsonVariant.set(nullptr)") { + variant.set(42); + variant.set(nullptr); + + REQUIRE(variant.isNull()); + } + + SECTION("JsonVariant.is()") { + variant.set(42); + REQUIRE(variant.is() == false); + + variant.clear(); + REQUIRE(variant.is() == true); + } +} diff --git a/extras/tests/MixedConfiguration/use_long_long_0.cpp b/extras/tests/Cpp11/use_long_long_0.cpp similarity index 100% rename from extras/tests/MixedConfiguration/use_long_long_0.cpp rename to extras/tests/Cpp11/use_long_long_0.cpp diff --git a/extras/tests/MixedConfiguration/use_long_long_1.cpp b/extras/tests/Cpp11/use_long_long_1.cpp similarity index 100% rename from extras/tests/MixedConfiguration/use_long_long_1.cpp rename to extras/tests/Cpp11/use_long_long_1.cpp diff --git a/extras/tests/MixedConfiguration/CMakeLists.txt b/extras/tests/MixedConfiguration/CMakeLists.txt index 9d63caf2..de6db8cb 100644 --- a/extras/tests/MixedConfiguration/CMakeLists.txt +++ b/extras/tests/MixedConfiguration/CMakeLists.txt @@ -2,11 +2,7 @@ # Copyright Benoit Blanchon 2014-2021 # MIT License -# we need C++11 for 'long long' -set(CMAKE_CXX_STANDARD 11) - add_executable(MixedConfigurationTests - cpp11.cpp decode_unicode_0.cpp decode_unicode_1.cpp enable_alignment_0.cpp @@ -22,8 +18,6 @@ add_executable(MixedConfigurationTests enable_string_deduplication_1.cpp use_double_0.cpp use_double_1.cpp - use_long_long_0.cpp - use_long_long_1.cpp ) set_target_properties(MixedConfigurationTests PROPERTIES UNITY_BUILD OFF) @@ -33,4 +27,4 @@ add_test(MixedConfiguration MixedConfigurationTests) set_tests_properties(MixedConfiguration PROPERTIES LABELS "Catch" -) \ No newline at end of file +) diff --git a/extras/tests/MixedConfiguration/enable_infinity_0.cpp b/extras/tests/MixedConfiguration/enable_infinity_0.cpp index 66167ed5..521fb847 100644 --- a/extras/tests/MixedConfiguration/enable_infinity_0.cpp +++ b/extras/tests/MixedConfiguration/enable_infinity_0.cpp @@ -6,7 +6,7 @@ static void assertParseFails(const char* json) { DynamicJsonDocument doc(4096); - auto err = deserializeJson(doc, json); + DeserializationError err = deserializeJson(doc, json); REQUIRE(err == DeserializationError::InvalidInput); } diff --git a/extras/tests/MixedConfiguration/enable_infinity_1.cpp b/extras/tests/MixedConfiguration/enable_infinity_1.cpp index 56e49fad..19e0d5bf 100644 --- a/extras/tests/MixedConfiguration/enable_infinity_1.cpp +++ b/extras/tests/MixedConfiguration/enable_infinity_1.cpp @@ -22,7 +22,8 @@ TEST_CASE("ARDUINOJSON_ENABLE_INFINITY == 1") { } SECTION("deserializeJson()") { - auto err = deserializeJson(doc, "[Infinity,-Infinity,+Infinity]"); + DeserializationError err = + deserializeJson(doc, "[Infinity,-Infinity,+Infinity]"); float a = doc[0]; float b = doc[1]; float c = doc[2]; diff --git a/extras/tests/MixedConfiguration/enable_nan_0.cpp b/extras/tests/MixedConfiguration/enable_nan_0.cpp index 88425253..e015a0db 100644 --- a/extras/tests/MixedConfiguration/enable_nan_0.cpp +++ b/extras/tests/MixedConfiguration/enable_nan_0.cpp @@ -18,7 +18,7 @@ TEST_CASE("ARDUINOJSON_ENABLE_NAN == 0") { } SECTION("deserializeJson()") { - auto err = deserializeJson(doc, "{\"X\":NaN}"); + DeserializationError err = deserializeJson(doc, "{\"X\":NaN}"); REQUIRE(err == DeserializationError::InvalidInput); } diff --git a/extras/tests/MixedConfiguration/enable_nan_1.cpp b/extras/tests/MixedConfiguration/enable_nan_1.cpp index 19e50d82..34d94fca 100644 --- a/extras/tests/MixedConfiguration/enable_nan_1.cpp +++ b/extras/tests/MixedConfiguration/enable_nan_1.cpp @@ -22,7 +22,7 @@ TEST_CASE("ARDUINOJSON_ENABLE_NAN == 1") { } SECTION("deserializeJson()") { - auto err = deserializeJson(doc, "{\"X\":NaN}"); + DeserializationError err = deserializeJson(doc, "{\"X\":NaN}"); float x = doc["X"]; REQUIRE(err == DeserializationError::Ok); diff --git a/src/ArduinoJson/Configuration.hpp b/src/ArduinoJson/Configuration.hpp index dbc8ff1d..fca32187 100644 --- a/src/ArduinoJson/Configuration.hpp +++ b/src/ArduinoJson/Configuration.hpp @@ -6,14 +6,20 @@ #if __cplusplus >= 201103L # define ARDUINOJSON_HAS_LONG_LONG 1 -# define ARDUINOJSON_HAS_NULLPTR 1 # define ARDUINOJSON_HAS_RVALUE_REFERENCES 1 #else # define ARDUINOJSON_HAS_LONG_LONG 0 -# define ARDUINOJSON_HAS_NULLPTR 0 # define ARDUINOJSON_HAS_RVALUE_REFERENCES 0 #endif +#ifndef ARDUINOJSON_HAS_NULLPTR +# if __cplusplus >= 201103L +# define ARDUINOJSON_HAS_NULLPTR 1 +# else +# define ARDUINOJSON_HAS_NULLPTR 0 +# endif +#endif + #if defined(_MSC_VER) && !ARDUINOJSON_HAS_LONG_LONG # define ARDUINOJSON_HAS_INT64 1 #else