string_view.cpp 2.8 KB

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