input_types.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "CustomReader.hpp"
  7. TEST_CASE("deserializeMsgPack(const std::string&)") {
  8. DynamicJsonDocument doc(4096);
  9. SECTION("should accept const string") {
  10. const std::string input("\x92\x01\x02");
  11. DeserializationError err = deserializeMsgPack(doc, input);
  12. REQUIRE(err == DeserializationError::Ok);
  13. }
  14. SECTION("should accept temporary string") {
  15. DeserializationError err =
  16. deserializeMsgPack(doc, std::string("\x92\x01\x02"));
  17. REQUIRE(err == DeserializationError::Ok);
  18. }
  19. SECTION("should duplicate content") {
  20. std::string input("\x91\xA5hello");
  21. DeserializationError err = deserializeMsgPack(doc, input);
  22. input[2] = 'X'; // alter the string tomake sure we made a copy
  23. JsonArray array = doc.as<JsonArray>();
  24. REQUIRE(err == DeserializationError::Ok);
  25. REQUIRE(std::string("hello") == array[0]);
  26. }
  27. SECTION("should accept a zero in input") {
  28. DeserializationError err =
  29. deserializeMsgPack(doc, std::string("\x92\x00\x02", 3));
  30. REQUIRE(err == DeserializationError::Ok);
  31. JsonArray arr = doc.as<JsonArray>();
  32. REQUIRE(arr[0] == 0);
  33. REQUIRE(arr[1] == 2);
  34. }
  35. }
  36. TEST_CASE("deserializeMsgPack(std::istream&)") {
  37. DynamicJsonDocument doc(4096);
  38. SECTION("should accept a zero in input") {
  39. std::istringstream input(std::string("\x92\x00\x02", 3));
  40. DeserializationError err = deserializeMsgPack(doc, input);
  41. REQUIRE(err == DeserializationError::Ok);
  42. JsonArray arr = doc.as<JsonArray>();
  43. REQUIRE(arr[0] == 0);
  44. REQUIRE(arr[1] == 2);
  45. }
  46. SECTION("should detect incomplete input") {
  47. std::istringstream input("\x92\x00\x02");
  48. DeserializationError err = deserializeMsgPack(doc, input);
  49. REQUIRE(err == DeserializationError::IncompleteInput);
  50. }
  51. }
  52. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  53. TEST_CASE("deserializeMsgPack(VLA)") {
  54. int i = 16;
  55. char vla[i];
  56. memcpy(vla, "\xDE\x00\x01\xA5Hello\xA5world", 15);
  57. StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
  58. DeserializationError err = deserializeMsgPack(doc, vla);
  59. REQUIRE(err == DeserializationError::Ok);
  60. }
  61. #endif
  62. TEST_CASE("deserializeMsgPack(CustomReader)") {
  63. DynamicJsonDocument doc(4096);
  64. CustomReader reader("\x92\xA5Hello\xA5world");
  65. DeserializationError err = deserializeMsgPack(doc, reader);
  66. REQUIRE(err == DeserializationError::Ok);
  67. REQUIRE(doc.size() == 2);
  68. REQUIRE(doc[0] == "Hello");
  69. REQUIRE(doc[1] == "world");
  70. }