string_view.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, 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. #include "Allocators.hpp"
  12. #include "Literals.hpp"
  13. #if !ARDUINOJSON_ENABLE_STRING_VIEW
  14. # error ARDUINOJSON_ENABLE_STRING_VIEW must be set to 1
  15. #endif
  16. using ArduinoJson::detail::sizeofArray;
  17. TEST_CASE("string_view") {
  18. SpyingAllocator spy;
  19. JsonDocument doc(&spy);
  20. JsonVariant variant = doc.to<JsonVariant>();
  21. SECTION("deserializeJson()") {
  22. auto err = deserializeJson(doc, std::string_view("123", 2));
  23. REQUIRE(err == DeserializationError::Ok);
  24. REQUIRE(doc.as<int>() == 12);
  25. }
  26. SECTION("JsonDocument::set()") {
  27. doc.set(std::string_view("123", 2));
  28. REQUIRE(doc.as<std::string_view>() == "12");
  29. }
  30. SECTION("JsonDocument::operator[]() const") {
  31. doc["ab"] = "Yes";
  32. doc["abc"] = "No";
  33. REQUIRE(doc[std::string_view("abc", 2)] == "Yes");
  34. }
  35. SECTION("JsonDocument::operator[]()") {
  36. doc[std::string_view("abc", 2)] = "Yes";
  37. REQUIRE(doc["ab"] == "Yes");
  38. }
  39. SECTION("JsonVariant::operator==()") {
  40. variant.set("A");
  41. REQUIRE(variant == std::string_view("AX", 1));
  42. REQUIRE_FALSE(variant == std::string_view("BX", 1));
  43. }
  44. SECTION("JsonVariant::operator>()") {
  45. variant.set("B");
  46. REQUIRE(variant > std::string_view("AX", 1));
  47. REQUIRE_FALSE(variant > std::string_view("CX", 1));
  48. }
  49. SECTION("JsonVariant::operator<()") {
  50. variant.set("B");
  51. REQUIRE(variant < std::string_view("CX", 1));
  52. REQUIRE_FALSE(variant < std::string_view("AX", 1));
  53. }
  54. SECTION("String deduplication") {
  55. doc.add(std::string_view("example one", 7));
  56. doc.add(std::string_view("example two", 7));
  57. doc.add(std::string_view("example\0tree", 12));
  58. doc.add(std::string_view("example\0tree and a half", 12));
  59. REQUIRE(spy.log() == AllocatorLog{
  60. Allocate(sizeofPool()),
  61. Allocate(sizeofString("example")),
  62. Allocate(sizeofString("example tree")),
  63. });
  64. }
  65. SECTION("as<std::string_view>()") {
  66. doc["s"] = "Hello World";
  67. doc["i"] = 42;
  68. REQUIRE(doc["s"].as<std::string_view>() == std::string_view("Hello World"));
  69. REQUIRE(doc["i"].as<std::string_view>() == std::string_view());
  70. }
  71. SECTION("is<std::string_view>()") {
  72. doc["s"] = "Hello World";
  73. doc["i"] = 42;
  74. REQUIRE(doc["s"].is<std::string_view>() == true);
  75. REQUIRE(doc["i"].is<std::string_view>() == false);
  76. }
  77. SECTION("String containing NUL") {
  78. doc.set("hello\0world"_s);
  79. REQUIRE(doc.as<std::string_view>().size() == 11);
  80. REQUIRE(doc.as<std::string_view>() == std::string_view("hello\0world", 11));
  81. }
  82. }
  83. using ArduinoJson::detail::adaptString;
  84. TEST_CASE("StringViewAdapter") {
  85. std::string_view str("bravoXXX", 5);
  86. auto adapter = adaptString(str);
  87. CHECK(stringCompare(adapter, adaptString("alpha", 5)) > 0);
  88. CHECK(stringCompare(adapter, adaptString("bravo", 5)) == 0);
  89. CHECK(stringCompare(adapter, adaptString("charlie", 7)) < 0);
  90. CHECK(adapter.size() == 5);
  91. }