string_view.cpp 3.2 KB

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