serializeArray.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2024, Benoit BLANCHON
  3. // MIT License
  4. #define ARDUINOJSON_SLOT_ID_SIZE 4 // required to reach 65536 elements
  5. #include <ArduinoJson.h>
  6. #include <catch.hpp>
  7. static void check(const JsonArray array, const char* expected_data,
  8. size_t expected_len) {
  9. std::string expected(expected_data, expected_data + expected_len);
  10. std::string actual;
  11. size_t len = serializeMsgPack(array, actual);
  12. CAPTURE(array);
  13. REQUIRE(len == expected_len);
  14. REQUIRE(actual == expected);
  15. }
  16. template <size_t N>
  17. static void check(const JsonArray array, const char (&expected_data)[N]) {
  18. const size_t expected_len = N - 1;
  19. check(array, expected_data, expected_len);
  20. }
  21. static void check(const JsonArray array, const std::string& expected) {
  22. check(array, expected.data(), expected.length());
  23. }
  24. TEST_CASE("serialize MsgPack array") {
  25. JsonDocument doc;
  26. JsonArray array = doc.to<JsonArray>();
  27. SECTION("empty") {
  28. check(array, "\x90");
  29. }
  30. SECTION("fixarray") {
  31. array.add("hello");
  32. array.add("world");
  33. check(array, "\x92\xA5hello\xA5world");
  34. }
  35. SECTION("array 16") {
  36. for (int i = 0; i < 16; i++)
  37. array.add(i);
  38. check(array,
  39. "\xDC\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D"
  40. "\x0E\x0F");
  41. }
  42. SECTION("array 32") {
  43. const char* nil = 0;
  44. for (int i = 0; i < 65536; i++)
  45. array.add(nil);
  46. REQUIRE(array.size() == 65536);
  47. check(array,
  48. std::string("\xDD\x00\x01\x00\x00", 5) + std::string(65536, '\xc0'));
  49. }
  50. }