From f224408c0743f65ceb2f0c438991be110b59aa92 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 3 Nov 2014 18:23:39 +0100 Subject: [PATCH] Added tests of comparison operators --- include/ArduinoJson/JsonValue.hpp | 65 +++++++++++++++++++ test/JsonValue_Comparison_Tests.cpp | 96 +++++++++++++++++++++++++++++ test/Printers.cpp | 9 ++- 3 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 test/JsonValue_Comparison_Tests.cpp diff --git a/include/ArduinoJson/JsonValue.hpp b/include/ArduinoJson/JsonValue.hpp index 0a6d00b8..0591bc0a 100644 --- a/include/ArduinoJson/JsonValue.hpp +++ b/include/ArduinoJson/JsonValue.hpp @@ -78,6 +78,11 @@ class JsonValue { return static_cast(*this); } + template + bool is() const { + return false; + } + static JsonValue &invalid() { return _invalid; } bool success() { return _type != Internals::JSON_INVALID; } @@ -93,6 +98,16 @@ class JsonValue { static JsonValue _invalid; }; +template <> +inline bool JsonValue::is() const { + return _type == Internals::JSON_LONG; +} + +template <> +inline bool JsonValue::is() const { + return _type >= Internals::JSON_DOUBLE_0_DECIMALS; +} + template inline bool operator==(const JsonValue &left, T right) { return left.as() == right; @@ -102,4 +117,54 @@ template inline bool operator==(T left, const JsonValue &right) { return left == right.as(); } + +template +inline bool operator!=(const JsonValue &left, T right) { + return left.as() != right; +} + +template +inline bool operator!=(T left, const JsonValue &right) { + return left != right.as(); +} + +template +inline bool operator<=(const JsonValue &left, T right) { + return left.as() <= right; +} + +template +inline bool operator<=(T left, const JsonValue &right) { + return left <= right.as(); +} + +template +inline bool operator>=(const JsonValue &left, T right) { + return left.as() >= right; +} + +template +inline bool operator>=(T left, const JsonValue &right) { + return left >= right.as(); +} + +template +inline bool operator<(const JsonValue &left, T right) { + return left.as() < right; +} + +template +inline bool operator<(T left, const JsonValue &right) { + return left < right.as(); +} + +template +inline bool operator>(const JsonValue &left, T right) { + return left.as() > right; +} + +template +inline bool operator>(T left, const JsonValue &right) { + return left > right.as(); +} } diff --git a/test/JsonValue_Comparison_Tests.cpp b/test/JsonValue_Comparison_Tests.cpp new file mode 100644 index 00000000..36ae2789 --- /dev/null +++ b/test/JsonValue_Comparison_Tests.cpp @@ -0,0 +1,96 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#include +#include +#include "Printers.hpp" + +using namespace ArduinoJson; + +class JsonValue_Comparison_Tests : public ::testing::Test { + protected: + template + void testValue(T low, T mid, T high) { + setValueTo(mid); + mustBeEqualTo(mid); + mustBeGreaterThan(low); + mustBeLessThan(high); + } + + private: + template + void setValueTo(T expected) { + jsonValue = expected; + } + + template + void mustBeEqualTo(T expected) { + EXPECT_EQ(expected, jsonValue); // operator== + EXPECT_EQ(jsonValue, expected); // operator== + EXPECT_LE(expected, jsonValue); // operator<= + EXPECT_LE(jsonValue, expected); // operator<= + EXPECT_GE(expected, jsonValue); // operator>= + EXPECT_GE(jsonValue, expected); // operator>= + } + + template + void mustBeGreaterThan(T expected) { + EXPECT_GT(jsonValue, expected); // operator> + EXPECT_LT(expected, jsonValue); // operator< + EXPECT_NE(jsonValue, expected); // operator!= + EXPECT_NE(expected, jsonValue); // operator!= + } + + template + void mustBeLessThan(T expected) { + EXPECT_LT(jsonValue, expected); // operator< + EXPECT_GT(expected, jsonValue); // operator< + EXPECT_NE(jsonValue, expected); // operator!= + EXPECT_NE(expected, jsonValue); // operator!= + } + + JsonValue jsonValue; +}; + +TEST_F(JsonValue_Comparison_Tests, Double) { + testValue(123.44, 123.45, 123.46); +} + +TEST_F(JsonValue_Comparison_Tests, Float) { + testValue(123.44f, 123.45f, 123.46f); +} + +TEST_F(JsonValue_Comparison_Tests, SChar) { + testValue(122, 123, 124); +} + +TEST_F(JsonValue_Comparison_Tests, SInt) { + testValue(122, 123, 124); +} + +TEST_F(JsonValue_Comparison_Tests, SLong) { + testValue(122L, 123L, 124L); +} + +TEST_F(JsonValue_Comparison_Tests, SShort) { + testValue(122, 123, 124); +} + +TEST_F(JsonValue_Comparison_Tests, UChar) { + testValue(122, 123, 124); +} + +TEST_F(JsonValue_Comparison_Tests, UInt) { + testValue(122, 123, 124); +} + +TEST_F(JsonValue_Comparison_Tests, ULong) { + testValue(122L, 123L, 124L); +} + +TEST_F(JsonValue_Comparison_Tests, UShort) { + testValue(122, 123, 124); +} diff --git a/test/Printers.cpp b/test/Printers.cpp index d2dd4916..d19268bf 100644 --- a/test/Printers.cpp +++ b/test/Printers.cpp @@ -7,7 +7,12 @@ #include "Printers.hpp" std::ostream& ArduinoJson::operator<<(std::ostream& os, - const ArduinoJson::JsonValue&) { - os << "JsonValue"; // TODO + const ArduinoJson::JsonValue& v) { + if (v.is()) + os << v.as(); + else if (v.is()) + os << v.as(); + else + os << "JsonValue"; // TODO return os; }