enable_progmem_1.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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("strlen_P") {
  38. CHECK(strlen_P(FC("")) == 0);
  39. CHECK(strlen_P(FC("a")) == 1);
  40. CHECK(strlen_P(FC("ac")) == 2);
  41. }
  42. TEST_CASE("strncmp_P") {
  43. CHECK(strncmp_P("a", FC("b"), 0) == 0);
  44. CHECK(strncmp_P("a", FC("b"), 1) == -1);
  45. CHECK(strncmp_P("b", FC("a"), 1) == 1);
  46. CHECK(strncmp_P("a", FC("a"), 0) == 0);
  47. CHECK(strncmp_P("a", FC("b"), 2) == -1);
  48. CHECK(strncmp_P("b", FC("a"), 2) == 1);
  49. CHECK(strncmp_P("a", FC("a"), 2) == 0);
  50. }
  51. TEST_CASE("strcmp_P") {
  52. CHECK(strcmp_P("a", FC("b")) == -1);
  53. CHECK(strcmp_P("b", FC("a")) == 1);
  54. CHECK(strcmp_P("a", FC("a")) == 0);
  55. CHECK(strcmp_P("aa", FC("ab")) == -1);
  56. CHECK(strcmp_P("ab", FC("aa")) == 1);
  57. CHECK(strcmp_P("aa", FC("aa")) == 0);
  58. }
  59. TEST_CASE("memcpy_P") {
  60. char dst[4];
  61. CHECK(memcpy_P(dst, FC("ABC"), 4) == dst);
  62. CHECK(dst[0] == 'A');
  63. CHECK(dst[1] == 'B');
  64. CHECK(dst[2] == 'C');
  65. CHECK(dst[3] == 0);
  66. }
  67. TEST_CASE("BoundedReader<const __FlashStringHelper*>") {
  68. using namespace ARDUINOJSON_NAMESPACE;
  69. SECTION("read") {
  70. BoundedReader<const __FlashStringHelper*> reader(F("\x01\xFF"), 2);
  71. REQUIRE(reader.read() == 0x01);
  72. REQUIRE(reader.read() == 0xFF);
  73. REQUIRE(reader.read() == -1);
  74. REQUIRE(reader.read() == -1);
  75. }
  76. SECTION("readBytes() all at once") {
  77. BoundedReader<const __FlashStringHelper*> reader(F("ABCD"), 3);
  78. char buffer[8] = "abcd";
  79. REQUIRE(reader.readBytes(buffer, 4) == 3);
  80. REQUIRE(buffer[0] == 'A');
  81. REQUIRE(buffer[1] == 'B');
  82. REQUIRE(buffer[2] == 'C');
  83. REQUIRE(buffer[3] == 'd');
  84. }
  85. SECTION("readBytes() in two parts") {
  86. BoundedReader<const __FlashStringHelper*> reader(F("ABCDEF"), 6);
  87. char buffer[8] = "abcdefg";
  88. REQUIRE(reader.readBytes(buffer, 4) == 4);
  89. REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
  90. REQUIRE(buffer[0] == 'A');
  91. REQUIRE(buffer[1] == 'B');
  92. REQUIRE(buffer[2] == 'C');
  93. REQUIRE(buffer[3] == 'D');
  94. REQUIRE(buffer[4] == 'E');
  95. REQUIRE(buffer[5] == 'F');
  96. REQUIRE(buffer[6] == 'g');
  97. }
  98. }
  99. TEST_CASE("Reader<const __FlashStringHelper*>") {
  100. using namespace ARDUINOJSON_NAMESPACE;
  101. SECTION("read()") {
  102. Reader<const __FlashStringHelper*> reader(F("\x01\xFF\x00\x12"));
  103. REQUIRE(reader.read() == 0x01);
  104. REQUIRE(reader.read() == 0xFF);
  105. REQUIRE(reader.read() == 0);
  106. REQUIRE(reader.read() == 0x12);
  107. }
  108. SECTION("readBytes() all at once") {
  109. Reader<const __FlashStringHelper*> reader(F("ABCD"));
  110. char buffer[8] = "abcd";
  111. REQUIRE(reader.readBytes(buffer, 3) == 3);
  112. REQUIRE(buffer[0] == 'A');
  113. REQUIRE(buffer[1] == 'B');
  114. REQUIRE(buffer[2] == 'C');
  115. REQUIRE(buffer[3] == 'd');
  116. }
  117. SECTION("readBytes() in two parts") {
  118. Reader<const __FlashStringHelper*> reader(F("ABCDEF"));
  119. char buffer[8] = "abcdefg";
  120. REQUIRE(reader.readBytes(buffer, 4) == 4);
  121. REQUIRE(reader.readBytes(buffer + 4, 2) == 2);
  122. REQUIRE(buffer[0] == 'A');
  123. REQUIRE(buffer[1] == 'B');
  124. REQUIRE(buffer[2] == 'C');
  125. REQUIRE(buffer[3] == 'D');
  126. REQUIRE(buffer[4] == 'E');
  127. REQUIRE(buffer[5] == 'F');
  128. REQUIRE(buffer[6] == 'g');
  129. }
  130. }