string_view.cpp 3.0 KB

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