serializeArray.cpp 1.5 KB

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