deserializeArray.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. TEST_CASE("deserialize MsgPack array") {
  8. SpyingAllocator spy;
  9. JsonDocument doc(&spy);
  10. SECTION("fixarray") {
  11. SECTION("empty") {
  12. const char* input = "\x90";
  13. DeserializationError error = deserializeMsgPack(doc, input);
  14. JsonArray array = doc.as<JsonArray>();
  15. REQUIRE(error == DeserializationError::Ok);
  16. REQUIRE(array.size() == 0);
  17. }
  18. SECTION("two integers") {
  19. const char* input = "\x92\x01\x02";
  20. DeserializationError error = deserializeMsgPack(doc, input);
  21. JsonArray array = doc.as<JsonArray>();
  22. REQUIRE(error == DeserializationError::Ok);
  23. REQUIRE(array.size() == 2);
  24. REQUIRE(array[0] == 1);
  25. REQUIRE(array[1] == 2);
  26. }
  27. SECTION("tiny strings") {
  28. DeserializationError error =
  29. deserializeMsgPack(doc, "\x92\xA3xxx\xA3yyy");
  30. REQUIRE(error == DeserializationError::Ok);
  31. REQUIRE(doc.is<JsonArray>());
  32. REQUIRE(doc.size() == 2);
  33. REQUIRE(doc[0] == "xxx");
  34. REQUIRE(doc[1] == "yyy");
  35. REQUIRE(spy.log() == AllocatorLog{
  36. Allocate(sizeofPool()),
  37. Allocate(sizeofString("xxx")),
  38. // Buffer is reused for the next string
  39. Deallocate(sizeofString("xxx")),
  40. Reallocate(sizeofPool(), sizeofPool(2)),
  41. });
  42. }
  43. }
  44. SECTION("array 16") {
  45. SECTION("empty") {
  46. const char* input = "\xDC\x00\x00";
  47. DeserializationError error = deserializeMsgPack(doc, input);
  48. JsonArray array = doc.as<JsonArray>();
  49. REQUIRE(error == DeserializationError::Ok);
  50. REQUIRE(array.size() == 0);
  51. }
  52. SECTION("two strings") {
  53. const char* input = "\xDC\x00\x02\xA5hello\xA5world";
  54. DeserializationError error = deserializeMsgPack(doc, input);
  55. JsonArray array = doc.as<JsonArray>();
  56. REQUIRE(error == DeserializationError::Ok);
  57. REQUIRE(array.size() == 2);
  58. REQUIRE(array[0] == "hello");
  59. REQUIRE(array[1] == "world");
  60. }
  61. }
  62. SECTION("array 32") {
  63. SECTION("empty") {
  64. const char* input = "\xDD\x00\x00\x00\x00";
  65. DeserializationError error = deserializeMsgPack(doc, input);
  66. JsonArray array = doc.as<JsonArray>();
  67. REQUIRE(error == DeserializationError::Ok);
  68. REQUIRE(array.size() == 0);
  69. }
  70. SECTION("two floats") {
  71. const char* input =
  72. "\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3";
  73. DeserializationError error = deserializeMsgPack(doc, input);
  74. JsonArray array = doc.as<JsonArray>();
  75. REQUIRE(error == DeserializationError::Ok);
  76. REQUIRE(array.size() == 2);
  77. REQUIRE(array[0] == 0.0f);
  78. REQUIRE(array[1] == 3.14f);
  79. }
  80. }
  81. }