enable_progmem_1.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2021
  3. // MIT License
  4. #define ARDUINOJSON_ENABLE_PROGMEM 1
  5. #include <ArduinoJson.h>
  6. #include <catch.hpp>
  7. TEST_CASE("Flash strings") {
  8. DynamicJsonDocument doc(2048);
  9. SECTION("deserializeJson()") {
  10. DeserializationError err = deserializeJson(doc, F("{'hello':'world'}"));
  11. REQUIRE(err == DeserializationError::Ok);
  12. REQUIRE(doc["hello"] == "world");
  13. }
  14. SECTION("JsonDocument::operator[]") {
  15. doc[F("hello")] = F("world");
  16. REQUIRE(doc["hello"] == "world");
  17. }
  18. SECTION("JsonDocument::add()") {
  19. doc.add(F("world"));
  20. REQUIRE(doc[0] == "world");
  21. }
  22. SECTION("JsonVariant::set()") {
  23. JsonVariant var = doc.to<JsonVariant>();
  24. var.set(F("world"));
  25. REQUIRE(var == "world");
  26. }
  27. SECTION("MemberProxy::operator==") {
  28. doc["hello"] = "world";
  29. REQUIRE(doc["hello"] == F("world"));
  30. }
  31. SECTION("ElementProxy::operator==") {
  32. doc.add("world");
  33. REQUIRE(doc[0] == F("world"));
  34. }
  35. }
  36. TEST_CASE("strlen_P") {
  37. CHECK(strlen_P(PSTR("")) == 0);
  38. CHECK(strlen_P(PSTR("a")) == 1);
  39. CHECK(strlen_P(PSTR("ac")) == 2);
  40. }
  41. TEST_CASE("strncmp_P") {
  42. CHECK(strncmp_P("a", PSTR("b"), 0) == 0);
  43. CHECK(strncmp_P("a", PSTR("b"), 1) == -1);
  44. CHECK(strncmp_P("b", PSTR("a"), 1) == 1);
  45. CHECK(strncmp_P("a", PSTR("a"), 0) == 0);
  46. CHECK(strncmp_P("a", PSTR("b"), 2) == -1);
  47. CHECK(strncmp_P("b", PSTR("a"), 2) == 1);
  48. CHECK(strncmp_P("a", PSTR("a"), 2) == 0);
  49. }
  50. TEST_CASE("strcmp_P") {
  51. CHECK(strcmp_P("a", PSTR("b")) == -1);
  52. CHECK(strcmp_P("b", PSTR("a")) == 1);
  53. CHECK(strcmp_P("a", PSTR("a")) == 0);
  54. CHECK(strcmp_P("aa", PSTR("ab")) == -1);
  55. CHECK(strcmp_P("ab", PSTR("aa")) == 1);
  56. CHECK(strcmp_P("aa", PSTR("aa")) == 0);
  57. }
  58. TEST_CASE("memcpy_P") {
  59. char dst[4];
  60. CHECK(memcpy_P(dst, PSTR("ABC"), 4) == dst);
  61. CHECK(dst[0] == 'A');
  62. CHECK(dst[1] == 'B');
  63. CHECK(dst[2] == 'C');
  64. CHECK(dst[3] == 0);
  65. }
  66. TEST_CASE("BoundedReader<const __FlashStringHelper*>") {
  67. using namespace ARDUINOJSON_NAMESPACE;
  68. SECTION("read") {
  69. BoundedReader<const __FlashStringHelper*> reader(F("\x01\xFF"), 2);
  70. REQUIRE(reader.read() == 0x01);
  71. REQUIRE(reader.read() == 0xFF);
  72. REQUIRE(reader.read() == -1);
  73. REQUIRE(reader.read() == -1);
  74. }
  75. SECTION("readBytes() all at once") {
  76. BoundedReader<const __FlashStringHelper*> reader(F("ABCD"), 3);
  77. char buffer[8] = "abcd";
  78. REQUIRE(reader.readBytes(buffer, 4) == 3);
  79. REQUIRE(buffer[0] == 'A');
  80. REQUIRE(buffer[1] == 'B');
  81. REQUIRE(buffer[2] == 'C');
  82. REQUIRE(buffer[3] == 'd');
  83. }
  84. SECTION("readBytes() in two parts") {
  85. BoundedReader<const __FlashStringHelper*> reader(F("ABCDEF"), 6);
  86. char buffer[8] = "abcdefg";
  87. REQUIRE(reader.readBytes(buffer, 4) == 4);
  88. REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
  89. REQUIRE(buffer[0] == 'A');
  90. REQUIRE(buffer[1] == 'B');
  91. REQUIRE(buffer[2] == 'C');
  92. REQUIRE(buffer[3] == 'D');
  93. REQUIRE(buffer[4] == 'E');
  94. REQUIRE(buffer[5] == 'F');
  95. REQUIRE(buffer[6] == 'g');
  96. }
  97. }
  98. TEST_CASE("Reader<const __FlashStringHelper*>") {
  99. using namespace ARDUINOJSON_NAMESPACE;
  100. SECTION("read()") {
  101. Reader<const __FlashStringHelper*> reader(F("\x01\xFF\x00\x12"));
  102. REQUIRE(reader.read() == 0x01);
  103. REQUIRE(reader.read() == 0xFF);
  104. REQUIRE(reader.read() == 0);
  105. REQUIRE(reader.read() == 0x12);
  106. }
  107. SECTION("readBytes() all at once") {
  108. Reader<const __FlashStringHelper*> reader(F("ABCD"));
  109. char buffer[8] = "abcd";
  110. REQUIRE(reader.readBytes(buffer, 3) == 3);
  111. REQUIRE(buffer[0] == 'A');
  112. REQUIRE(buffer[1] == 'B');
  113. REQUIRE(buffer[2] == 'C');
  114. REQUIRE(buffer[3] == 'd');
  115. }
  116. SECTION("readBytes() in two parts") {
  117. Reader<const __FlashStringHelper*> reader(F("ABCDEF"));
  118. char buffer[8] = "abcdefg";
  119. REQUIRE(reader.readBytes(buffer, 4) == 4);
  120. REQUIRE(reader.readBytes(buffer + 4, 2) == 2);
  121. REQUIRE(buffer[0] == 'A');
  122. REQUIRE(buffer[1] == 'B');
  123. REQUIRE(buffer[2] == 'C');
  124. REQUIRE(buffer[3] == 'D');
  125. REQUIRE(buffer[4] == 'E');
  126. REQUIRE(buffer[5] == 'F');
  127. REQUIRE(buffer[6] == 'g');
  128. }
  129. }
  130. static void testStringification(DeserializationError error,
  131. std::string expected) {
  132. const __FlashStringHelper* s = error.f_str();
  133. CHECK(reinterpret_cast<const char*>(convertFlashToPtr(s)) == expected);
  134. }
  135. #define TEST_STRINGIFICATION(symbol) \
  136. testStringification(DeserializationError::symbol, #symbol)
  137. TEST_CASE("DeserializationError::f_str()") {
  138. TEST_STRINGIFICATION(Ok);
  139. TEST_STRINGIFICATION(EmptyInput);
  140. TEST_STRINGIFICATION(IncompleteInput);
  141. TEST_STRINGIFICATION(InvalidInput);
  142. TEST_STRINGIFICATION(NoMemory);
  143. TEST_STRINGIFICATION(TooDeep);
  144. }