input_types.cpp 2.1 KB

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