From 6841b804662a4f727de65bfce95373ab405d86a9 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 30 Jul 2020 09:40:35 +0200 Subject: [PATCH] Added JsonString::operator!= --- CHANGELOG.md | 1 + extras/tests/Misc/CMakeLists.txt | 3 +- extras/tests/Misc/JsonString.cpp | 60 ++++++++++++++++++++++++++++++ src/ArduinoJson/Strings/String.hpp | 10 +++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 extras/tests/Misc/JsonString.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 326dc2f4..d4d6792c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ HEAD * Added comparisons (`>`, `>=`, `==`, `!=`, `<`, and `<=`) between `JsonVariant`s * Added string deduplication (issue #1303) +* Added `JsonString::operator!=` * Set `ARDUINOJSON_DECODE_UNICODE` to `1` by default * Fixed `copyArray()` not working with `String`, `ElementProxy`, and `MemberProxy` * Fixed error `getOrAddElement is not a member of ElementProxy` (issue #1311) diff --git a/extras/tests/Misc/CMakeLists.txt b/extras/tests/Misc/CMakeLists.txt index 042264a4..2723e663 100644 --- a/extras/tests/Misc/CMakeLists.txt +++ b/extras/tests/Misc/CMakeLists.txt @@ -6,13 +6,14 @@ add_executable(MiscTests arithmeticCompare.cpp conflicts.cpp FloatParts.cpp + JsonString.cpp Readers.cpp StringAdapters.cpp StringWriter.cpp TypeTraits.cpp unsigned_char.cpp - Utf8.cpp Utf16.cpp + Utf8.cpp version.cpp ) diff --git a/extras/tests/Misc/JsonString.cpp b/extras/tests/Misc/JsonString.cpp new file mode 100644 index 00000000..a0c6d3ae --- /dev/null +++ b/extras/tests/Misc/JsonString.cpp @@ -0,0 +1,60 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2020 +// MIT License + +#include +#include + +TEST_CASE("JsonString") { + SECTION("Default constructor creates a null JsonString") { + JsonString s; + + CHECK(s.isNull() == true); + CHECK(s.c_str() == 0); + CHECK(s.isStatic() == true); + } + + SECTION("Compare null with null") { + JsonString a, b; + + CHECK(a == b); + CHECK_FALSE(a != b); + } + + SECTION("Compare null with non-null") { + JsonString a(0), b("hello"); + + CHECK_FALSE(a == b); + CHECK(a != b); + } + + SECTION("Compare non-null with null") { + JsonString a("hello"), b(0); + + CHECK_FALSE(a == b); + CHECK(a != b); + } + + SECTION("Compare different strings") { + JsonString a("hello"), b("world"); + + CHECK_FALSE(a == b); + CHECK(a != b); + } + + SECTION("Compare identical by pointer") { + JsonString a("hello"), b("hello"); + + CHECK(a == b); + CHECK_FALSE(a != b); + } + + SECTION("Compare identical by value") { + char s1[] = "hello"; + char s2[] = "hello"; + JsonString a(s1), b(s2); + + CHECK(a == b); + CHECK_FALSE(a != b); + } +} diff --git a/src/ArduinoJson/Strings/String.hpp b/src/ArduinoJson/Strings/String.hpp index 02f0ef2d..641e6ca3 100644 --- a/src/ArduinoJson/Strings/String.hpp +++ b/src/ArduinoJson/Strings/String.hpp @@ -38,6 +38,16 @@ class String { return strcmp(lhs._data, rhs._data) == 0; } + friend bool operator!=(String lhs, String rhs) { + if (lhs._data == rhs._data) + return false; + if (!lhs._data) + return true; + if (!rhs._data) + return true; + return strcmp(lhs._data, rhs._data) != 0; + } + private: const char* _data; bool _isStatic;