enable_progmem_1.cpp 4.9 KB

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