serializeArray.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  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. // TODO: this function is used by the commented test
  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. DynamicJsonDocument 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++) 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. // TODO: this test is too slow
  42. // SECTION("array 32") {
  43. // const char* nil = 0;
  44. // for (int i = 0; i < 65536; i++) array.add(nil);
  45. //
  46. // check(array,
  47. // std::string("\xDD\x00\x01\x00\x00", 5) + std::string(65536, 0xC0));
  48. // }
  49. }