deserializeArray.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("deserialize MsgPack array") {
  7. DynamicJsonDocument doc(4096);
  8. SECTION("fixarray") {
  9. SECTION("empty") {
  10. const char* input = "\x90";
  11. DeserializationError error = deserializeMsgPack(doc, input);
  12. JsonArray array = doc.as<JsonArray>();
  13. REQUIRE(error == DeserializationError::Ok);
  14. REQUIRE(array.size() == 0);
  15. }
  16. SECTION("two integers") {
  17. const char* input = "\x92\x01\x02";
  18. DeserializationError error = deserializeMsgPack(doc, input);
  19. JsonArray array = doc.as<JsonArray>();
  20. REQUIRE(error == DeserializationError::Ok);
  21. REQUIRE(array.size() == 2);
  22. REQUIRE(array[0] == 1);
  23. REQUIRE(array[1] == 2);
  24. }
  25. }
  26. SECTION("array 16") {
  27. SECTION("empty") {
  28. const char* input = "\xDC\x00\x00";
  29. DeserializationError error = deserializeMsgPack(doc, input);
  30. JsonArray array = doc.as<JsonArray>();
  31. REQUIRE(error == DeserializationError::Ok);
  32. REQUIRE(array.size() == 0);
  33. }
  34. SECTION("two strings") {
  35. const char* input = "\xDC\x00\x02\xA5hello\xA5world";
  36. DeserializationError error = deserializeMsgPack(doc, input);
  37. JsonArray array = doc.as<JsonArray>();
  38. REQUIRE(error == DeserializationError::Ok);
  39. REQUIRE(array.size() == 2);
  40. REQUIRE(array[0] == "hello");
  41. REQUIRE(array[1] == "world");
  42. }
  43. }
  44. SECTION("array 32") {
  45. SECTION("empty") {
  46. const char* input = "\xDD\x00\x00\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 floats") {
  53. const char* input =
  54. "\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3";
  55. DeserializationError error = deserializeMsgPack(doc, input);
  56. JsonArray array = doc.as<JsonArray>();
  57. REQUIRE(error == DeserializationError::Ok);
  58. REQUIRE(array.size() == 2);
  59. REQUIRE(array[0] == 0.0f);
  60. REQUIRE(array[1] == 3.14f);
  61. }
  62. }
  63. }