string_view.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, Benoit BLANCHON
  3. // MIT License
  4. // we expect ArduinoJson.h to include <string_view>
  5. // but we don't want it to included accidentally
  6. #undef ARDUINO
  7. #define ARDUINOJSON_ENABLE_STD_STREAM 0
  8. #define ARDUINOJSON_ENABLE_STD_STRING 0
  9. #include <ArduinoJson.h>
  10. #include <catch.hpp>
  11. #if !ARDUINOJSON_ENABLE_STRING_VIEW
  12. # error ARDUINOJSON_ENABLE_STRING_VIEW must be set to 1
  13. #endif
  14. TEST_CASE("string_view") {
  15. StaticJsonDocument<256> doc;
  16. JsonVariant variant = doc.to<JsonVariant>();
  17. SECTION("deserializeJson()") {
  18. auto err = deserializeJson(doc, std::string_view("123", 2));
  19. REQUIRE(err == DeserializationError::Ok);
  20. REQUIRE(doc.as<int>() == 12);
  21. }
  22. SECTION("JsonDocument::set()") {
  23. doc.set(std::string_view("123", 2));
  24. REQUIRE(doc.as<std::string_view>() == "12");
  25. }
  26. SECTION("JsonDocument::operator[]() const") {
  27. doc["ab"] = "Yes";
  28. doc["abc"] = "No";
  29. REQUIRE(doc[std::string_view("abc", 2)] == "Yes");
  30. }
  31. SECTION("JsonDocument::operator[]()") {
  32. doc[std::string_view("abc", 2)] = "Yes";
  33. REQUIRE(doc["ab"] == "Yes");
  34. }
  35. SECTION("JsonVariant::operator==()") {
  36. variant.set("A");
  37. REQUIRE(variant == std::string_view("AX", 1));
  38. REQUIRE_FALSE(variant == std::string_view("BX", 1));
  39. }
  40. SECTION("JsonVariant::operator>()") {
  41. variant.set("B");
  42. REQUIRE(variant > std::string_view("AX", 1));
  43. REQUIRE_FALSE(variant > std::string_view("CX", 1));
  44. }
  45. SECTION("JsonVariant::operator<()") {
  46. variant.set("B");
  47. REQUIRE(variant < std::string_view("CX", 1));
  48. REQUIRE_FALSE(variant < std::string_view("AX", 1));
  49. }
  50. SECTION("String deduplication") {
  51. doc.add(std::string_view("example one", 7));
  52. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1) + 8);
  53. doc.add(std::string_view("example two", 7));
  54. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 8);
  55. doc.add(std::string_view("example\0tree", 12));
  56. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(3) + 21);
  57. doc.add(std::string_view("example\0tree and a half", 12));
  58. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(4) + 21);
  59. }
  60. SECTION("as<std::string_view>()") {
  61. doc["s"] = "Hello World";
  62. doc["i"] = 42;
  63. REQUIRE(doc["s"].as<std::string_view>() == std::string_view("Hello World"));
  64. REQUIRE(doc["i"].as<std::string_view>() == std::string_view());
  65. }
  66. SECTION("is<std::string_view>()") {
  67. doc["s"] = "Hello World";
  68. doc["i"] = 42;
  69. REQUIRE(doc["s"].is<std::string_view>() == true);
  70. REQUIRE(doc["i"].is<std::string_view>() == false);
  71. }
  72. SECTION("String containing NUL") {
  73. doc.set(std::string("hello\0world", 11));
  74. REQUIRE(doc.as<std::string_view>().size() == 11);
  75. REQUIRE(doc.as<std::string_view>() == std::string_view("hello\0world", 11));
  76. }
  77. }
  78. using ArduinoJson::detail::adaptString;
  79. TEST_CASE("StringViewAdapter") {
  80. std::string_view str("bravoXXX", 5);
  81. auto adapter = adaptString(str);
  82. CHECK(stringCompare(adapter, adaptString("alpha", 5)) > 0);
  83. CHECK(stringCompare(adapter, adaptString("bravo", 5)) == 0);
  84. CHECK(stringCompare(adapter, adaptString("charlie", 7)) < 0);
  85. CHECK(adapter.size() == 5);
  86. }