JsonVariant_Comparison_Tests.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright Benoit Blanchon 2014-2016
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <gtest/gtest.h>
  9. class JsonVariant_Comparison_Tests : public ::testing::Test {
  10. protected:
  11. template <typename T>
  12. void testValue(T low, T mid, T high) {
  13. setValueTo(mid);
  14. mustBeEqualTo(mid);
  15. mustBeGreaterThan(low);
  16. mustBeLessThan(high);
  17. }
  18. private:
  19. template <typename T>
  20. void setValueTo(T expected) {
  21. actual = expected;
  22. }
  23. template <typename T>
  24. void mustBeEqualTo(T expected) {
  25. EXPECT_EQ(expected, actual); // operator==
  26. EXPECT_EQ(actual, expected); // operator==
  27. EXPECT_LE(expected, actual); // operator<=
  28. EXPECT_LE(actual, expected); // operator<=
  29. EXPECT_GE(expected, actual); // operator>=
  30. EXPECT_GE(actual, expected); // operator>=
  31. }
  32. template <typename T>
  33. void mustBeGreaterThan(T expected) {
  34. EXPECT_GT(actual, expected); // operator>
  35. EXPECT_LT(expected, actual); // operator<
  36. EXPECT_NE(actual, expected); // operator!=
  37. EXPECT_NE(expected, actual); // operator!=
  38. }
  39. template <typename T>
  40. void mustBeLessThan(T expected) {
  41. EXPECT_LT(actual, expected); // operator<
  42. EXPECT_GT(expected, actual); // operator<
  43. EXPECT_NE(actual, expected); // operator!=
  44. EXPECT_NE(expected, actual); // operator!=
  45. }
  46. JsonVariant actual;
  47. };
  48. TEST_F(JsonVariant_Comparison_Tests, Double) {
  49. testValue<double>(123.44, 123.45, 123.46);
  50. }
  51. TEST_F(JsonVariant_Comparison_Tests, Float) {
  52. testValue<float>(123.44f, 123.45f, 123.46f);
  53. }
  54. TEST_F(JsonVariant_Comparison_Tests, SChar) {
  55. testValue<signed char>(122, 123, 124);
  56. }
  57. TEST_F(JsonVariant_Comparison_Tests, SInt) {
  58. testValue<signed int>(122, 123, 124);
  59. }
  60. TEST_F(JsonVariant_Comparison_Tests, SLong) {
  61. testValue<signed long>(122L, 123L, 124L);
  62. }
  63. TEST_F(JsonVariant_Comparison_Tests, SShort) {
  64. testValue<signed short>(122, 123, 124);
  65. }
  66. TEST_F(JsonVariant_Comparison_Tests, UChar) {
  67. testValue<unsigned char>(122, 123, 124);
  68. }
  69. TEST_F(JsonVariant_Comparison_Tests, UInt) {
  70. testValue<unsigned int>(122, 123, 124);
  71. }
  72. TEST_F(JsonVariant_Comparison_Tests, ULong) {
  73. testValue<unsigned long>(122L, 123L, 124L);
  74. }
  75. TEST_F(JsonVariant_Comparison_Tests, UShort) {
  76. testValue<unsigned short>(122, 123, 124);
  77. }
  78. TEST_F(JsonVariant_Comparison_Tests, StringLiteral) {
  79. DynamicJsonBuffer jsonBuffer;
  80. JsonVariant variant = jsonBuffer.parse("\"hello\"");
  81. ASSERT_TRUE(variant == "hello");
  82. ASSERT_FALSE(variant != "hello");
  83. ASSERT_TRUE(variant != "world");
  84. ASSERT_FALSE(variant == "world");
  85. ASSERT_TRUE("hello" == variant);
  86. ASSERT_FALSE("hello" != variant);
  87. ASSERT_TRUE("world" != variant);
  88. ASSERT_FALSE("world" == variant);
  89. }
  90. TEST_F(JsonVariant_Comparison_Tests, String) {
  91. DynamicJsonBuffer jsonBuffer;
  92. JsonVariant variant = jsonBuffer.parse("\"hello\"");
  93. ASSERT_TRUE(variant == std::string("hello"));
  94. ASSERT_FALSE(variant != std::string("hello"));
  95. ASSERT_TRUE(variant != std::string("world"));
  96. ASSERT_FALSE(variant == std::string("world"));
  97. ASSERT_TRUE(std::string("hello") == variant);
  98. ASSERT_FALSE(std::string("hello") != variant);
  99. ASSERT_TRUE(std::string("world") != variant);
  100. ASSERT_FALSE(std::string("world") == variant);
  101. }