enable_progmem_1.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. }