serializeArray.cpp 1.5 KB

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