StringAdapters.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  3. // MIT License
  4. #include "custom_string.hpp"
  5. #include "progmem_emulation.hpp"
  6. #include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
  7. #include <ArduinoJson/Strings/FlashStringAdapter.hpp>
  8. #include <ArduinoJson/Strings/StlStringAdapter.hpp>
  9. #include <catch.hpp>
  10. using namespace ARDUINOJSON_NAMESPACE;
  11. TEST_CASE("ConstRamStringAdapter") {
  12. SECTION("null") {
  13. ConstRamStringAdapter adapter(NULL);
  14. REQUIRE(adapter.compare("bravo") < 0);
  15. REQUIRE(adapter.compare(NULL) == 0);
  16. REQUIRE(adapter.equals(NULL));
  17. REQUIRE_FALSE(adapter.equals("charlie"));
  18. REQUIRE(adapter.size() == 0);
  19. }
  20. SECTION("non-null") {
  21. ConstRamStringAdapter adapter("bravo");
  22. REQUIRE(adapter.compare(NULL) > 0);
  23. REQUIRE(adapter.compare("alpha") > 0);
  24. REQUIRE(adapter.compare("bravo") == 0);
  25. REQUIRE(adapter.compare("charlie") < 0);
  26. REQUIRE(adapter.equals("bravo"));
  27. REQUIRE_FALSE(adapter.equals("charlie"));
  28. REQUIRE(adapter.size() == 5);
  29. }
  30. }
  31. TEST_CASE("FlashStringAdapter") {
  32. SECTION("null") {
  33. FlashStringAdapter adapter(NULL);
  34. REQUIRE(adapter.compare("bravo") < 0);
  35. REQUIRE(adapter.compare(NULL) == 0);
  36. REQUIRE(adapter.equals(NULL));
  37. REQUIRE_FALSE(adapter.equals("charlie"));
  38. REQUIRE(adapter.size() == 0);
  39. }
  40. SECTION("non-null") {
  41. FlashStringAdapter adapter = adaptString(F("bravo"));
  42. REQUIRE(adapter.compare(NULL) > 0);
  43. REQUIRE(adapter.compare("alpha") > 0);
  44. REQUIRE(adapter.compare("bravo") == 0);
  45. REQUIRE(adapter.compare("charlie") < 0);
  46. REQUIRE(adapter.equals("bravo"));
  47. REQUIRE_FALSE(adapter.equals("charlie"));
  48. REQUIRE(adapter.size() == 5);
  49. }
  50. }
  51. TEST_CASE("std::string") {
  52. std::string str("bravo");
  53. StlStringAdapter<std::string> adapter = adaptString(str);
  54. REQUIRE(adapter.compare(NULL) > 0);
  55. REQUIRE(adapter.compare("alpha") > 0);
  56. REQUIRE(adapter.compare("bravo") == 0);
  57. REQUIRE(adapter.compare("charlie") < 0);
  58. REQUIRE(adapter.equals("bravo"));
  59. REQUIRE_FALSE(adapter.equals("charlie"));
  60. REQUIRE(adapter.size() == 5);
  61. }
  62. TEST_CASE("custom_string") {
  63. custom_string str("bravo");
  64. StlStringAdapter<custom_string> adapter = adaptString(str);
  65. REQUIRE(adapter.compare(NULL) > 0);
  66. REQUIRE(adapter.compare("alpha") > 0);
  67. REQUIRE(adapter.compare("bravo") == 0);
  68. REQUIRE(adapter.compare("charlie") < 0);
  69. REQUIRE(adapter.equals("bravo"));
  70. REQUIRE_FALSE(adapter.equals("charlie"));
  71. REQUIRE(adapter.size() == 5);
  72. }
  73. TEST_CASE("IsString<T>") {
  74. SECTION("std::string") {
  75. REQUIRE(IsString<std::string>::value == true);
  76. }
  77. SECTION("basic_string<wchar_t>") {
  78. REQUIRE(IsString<std::basic_string<wchar_t> >::value == false);
  79. }
  80. SECTION("custom_string") {
  81. REQUIRE(IsString<custom_string>::value == true);
  82. }
  83. SECTION("const __FlashStringHelper*") {
  84. REQUIRE(IsString<const __FlashStringHelper*>::value == true);
  85. }
  86. }