serializeArray.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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++) array.add(i);
  36. check(array,
  37. "\xDC\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D"
  38. "\x0E\x0F");
  39. }
  40. SECTION("array 32") {
  41. const char* nil = 0;
  42. for (int i = 0; i < 65536; i++) array.add(nil);
  43. check(array,
  44. std::string("\xDD\x00\x01\x00\x00", 5) + std::string(65536, '\xc0'));
  45. }
  46. }