StringAdapters.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #define ARDUINOJSON_ENABLE_PROGMEM 1
  5. #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
  6. #include "custom_string.hpp"
  7. #include "progmem_emulation.hpp"
  8. #include "weird_strcmp.hpp"
  9. #include <ArduinoJson/Strings/IsString.hpp>
  10. #include <ArduinoJson/Strings/StringAdapters.hpp>
  11. #include <catch.hpp>
  12. using namespace ARDUINOJSON_NAMESPACE;
  13. TEST_CASE("ZeroTerminatedRamString") {
  14. SECTION("null") {
  15. ZeroTerminatedRamString s = adaptString(static_cast<const char*>(0));
  16. CHECK(s.isNull() == true);
  17. CHECK(s.size() == 0);
  18. }
  19. SECTION("non-null") {
  20. ZeroTerminatedRamString s = adaptString("bravo");
  21. CHECK(s.isNull() == false);
  22. CHECK(s.size() == 5);
  23. }
  24. }
  25. TEST_CASE("SizedRamString") {
  26. SECTION("null") {
  27. SizedRamString s = adaptString(static_cast<const char*>(0), 10);
  28. CHECK(s.isNull() == true);
  29. }
  30. SECTION("non-null") {
  31. SizedRamString s = adaptString("bravo", 5);
  32. CHECK(s.isNull() == false);
  33. CHECK(s.size() == 5);
  34. }
  35. }
  36. TEST_CASE("FlashString") {
  37. SECTION("null") {
  38. FlashString s = adaptString(static_cast<const __FlashStringHelper*>(0));
  39. CHECK(s.isNull() == true);
  40. CHECK(s.size() == 0);
  41. }
  42. SECTION("non-null") {
  43. FlashString s = adaptString(F("bravo"));
  44. CHECK(s.isNull() == false);
  45. CHECK(s.size() == 5);
  46. }
  47. }
  48. TEST_CASE("std::string") {
  49. std::string orig("bravo");
  50. SizedRamString s = adaptString(orig);
  51. CHECK(s.isNull() == false);
  52. CHECK(s.size() == 5);
  53. }
  54. TEST_CASE("Arduino String") {
  55. ::String orig("bravo");
  56. SizedRamString s = adaptString(orig);
  57. CHECK(s.isNull() == false);
  58. CHECK(s.size() == 5);
  59. }
  60. TEST_CASE("custom_string") {
  61. custom_string orig("bravo");
  62. SizedRamString s = adaptString(orig);
  63. CHECK(s.isNull() == false);
  64. CHECK(s.size() == 5);
  65. }
  66. struct EmptyStruct {};
  67. TEST_CASE("IsString<T>") {
  68. CHECK(IsString<std::string>::value == true);
  69. CHECK(IsString<std::basic_string<wchar_t> >::value == false);
  70. CHECK(IsString<custom_string>::value == true);
  71. CHECK(IsString<const __FlashStringHelper*>::value == true);
  72. CHECK(IsString<const char*>::value == true);
  73. CHECK(IsString<const char[8]>::value == true);
  74. CHECK(IsString< ::String>::value == true);
  75. CHECK(IsString< ::StringSumHelper>::value == true);
  76. CHECK(IsString<const EmptyStruct*>::value == false);
  77. }
  78. TEST_CASE("stringCompare") {
  79. SECTION("ZeroTerminatedRamString vs ZeroTerminatedRamString") {
  80. CHECK(stringCompare(adaptString("bravo"), adaptString("alpha")) > 0);
  81. CHECK(stringCompare(adaptString("bravo"), adaptString("bravo")) == 0);
  82. CHECK(stringCompare(adaptString("bravo"), adaptString("charlie")) < 0);
  83. }
  84. SECTION("ZeroTerminatedRamString vs SizedRamString") {
  85. CHECK(stringCompare(adaptString("bravo"), adaptString("alpha?", 5)) > 0);
  86. CHECK(stringCompare(adaptString("bravo"), adaptString("bravo?", 4)) > 0);
  87. CHECK(stringCompare(adaptString("bravo"), adaptString("bravo?", 5)) == 0);
  88. CHECK(stringCompare(adaptString("bravo"), adaptString("bravo?", 6)) < 0);
  89. CHECK(stringCompare(adaptString("bravo"), adaptString("charlie?", 7)) < 0);
  90. }
  91. SECTION("SizedRamString vs SizedRamString") {
  92. // clang-format off
  93. CHECK(stringCompare(adaptString("bravo!", 5), adaptString("alpha?", 5)) > 0);
  94. CHECK(stringCompare(adaptString("bravo!", 5), adaptString("bravo?", 5)) == 0);
  95. CHECK(stringCompare(adaptString("bravo!", 5), adaptString("charlie?", 7)) < 0);
  96. CHECK(stringCompare(adaptString("bravo!", 5), adaptString("bravo!", 4)) > 0);
  97. CHECK(stringCompare(adaptString("bravo!", 5), adaptString("bravo!", 5)) == 0);
  98. CHECK(stringCompare(adaptString("bravo!", 5), adaptString("bravo!", 6)) < 0);
  99. // clang-format on
  100. }
  101. SECTION("FlashString vs FlashString") {
  102. // clang-format off
  103. CHECK(stringCompare(adaptString(F("bravo")), adaptString(F("alpha"))) > 0);
  104. CHECK(stringCompare(adaptString(F("bravo")), adaptString(F("bravo"))) == 0);
  105. CHECK(stringCompare(adaptString(F("bravo")), adaptString(F("charlie"))) < 0);
  106. // clang-format on
  107. }
  108. SECTION("FlashString vs SizedRamString") {
  109. // clang-format off
  110. CHECK(stringCompare(adaptString(F("bravo")), adaptString("alpha?", 5)) > 0);
  111. CHECK(stringCompare(adaptString(F("bravo")), adaptString("bravo?", 5)) == 0);
  112. CHECK(stringCompare(adaptString(F("bravo")), adaptString("charlie?", 7)) < 0);
  113. CHECK(stringCompare(adaptString(F("bravo")), adaptString("bravo!", 4)) > 0);
  114. CHECK(stringCompare(adaptString(F("bravo")), adaptString("bravo!", 5)) == 0);
  115. CHECK(stringCompare(adaptString(F("bravo")), adaptString("bravo!", 6)) < 0);
  116. // clang-format on
  117. }
  118. SECTION("ZeroTerminatedRamString vs FlashString") {
  119. // clang-format off
  120. CHECK(stringCompare(adaptString("bravo"), adaptString(F("alpha?"), 5)) > 0);
  121. CHECK(stringCompare(adaptString("bravo"), adaptString(F("bravo?"), 4)) > 0);
  122. CHECK(stringCompare(adaptString("bravo"), adaptString(F("bravo?"), 5)) == 0);
  123. CHECK(stringCompare(adaptString("bravo"), adaptString(F("bravo?"), 6)) < 0);
  124. CHECK(stringCompare(adaptString("bravo"), adaptString(F("charlie?"), 7)) < 0);
  125. // clang-format on
  126. }
  127. }
  128. TEST_CASE("stringEquals()") {
  129. SECTION("ZeroTerminatedRamString vs ZeroTerminatedRamString") {
  130. CHECK(stringEquals(adaptString("bravo"), adaptString("brav")) == false);
  131. CHECK(stringEquals(adaptString("bravo"), adaptString("bravo")) == true);
  132. CHECK(stringEquals(adaptString("bravo"), adaptString("bravo!")) == false);
  133. }
  134. SECTION("ZeroTerminatedRamString vs SizedRamString") {
  135. // clang-format off
  136. CHECK(stringEquals(adaptString("bravo"), adaptString("bravo!", 4)) == false);
  137. CHECK(stringEquals(adaptString("bravo"), adaptString("bravo!", 5)) == true);
  138. CHECK(stringEquals(adaptString("bravo"), adaptString("bravo!", 6)) == false);
  139. // clang-format on
  140. }
  141. SECTION("FlashString vs SizedRamString") {
  142. // clang-format off
  143. CHECK(stringEquals(adaptString(F("bravo")), adaptString("bravo!", 4)) == false);
  144. CHECK(stringEquals(adaptString(F("bravo")), adaptString("bravo!", 5)) == true);
  145. CHECK(stringEquals(adaptString(F("bravo")), adaptString("bravo!", 6)) == false);
  146. // clang-format on
  147. }
  148. SECTION("SizedRamString vs SizedRamString") {
  149. // clang-format off
  150. CHECK(stringEquals(adaptString("bravo?", 5), adaptString("bravo!", 4)) == false);
  151. CHECK(stringEquals(adaptString("bravo?", 5), adaptString("bravo!", 5)) == true);
  152. CHECK(stringEquals(adaptString("bravo?", 5), adaptString("bravo!", 6)) == false);
  153. // clang-format on
  154. }
  155. }