string_length_size_4.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #define ARDUINOJSON_STRING_LENGTH_SIZE 4
  2. #include <ArduinoJson.h>
  3. #include <catch.hpp>
  4. #include <string>
  5. TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
  6. JsonDocument doc;
  7. SECTION("set(std::string)") {
  8. SECTION("returns true if string length >= 65536") {
  9. auto result = doc.set(std::string(65536, '?'));
  10. REQUIRE(result == true);
  11. REQUIRE(doc.overflowed() == false);
  12. }
  13. }
  14. SECTION("set(MsgPackBinary)") {
  15. SECTION("returns true if size >= 65536") {
  16. auto str = std::string(65536, '?');
  17. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  18. REQUIRE(result == true);
  19. REQUIRE(doc.overflowed() == false);
  20. }
  21. }
  22. SECTION("deserializeJson()") {
  23. SECTION("returns Ok if string length >= 65536") {
  24. auto input = "\"" + std::string(65536, '?') + "\"";
  25. auto err = deserializeJson(doc, input);
  26. REQUIRE(err == DeserializationError::Ok);
  27. }
  28. }
  29. SECTION("deserializeMsgPack()") {
  30. SECTION("returns Ok if string size >= 65536") {
  31. auto input = "\xda\xff\xff" + std::string(65536, '?');
  32. auto err = deserializeMsgPack(doc, input);
  33. REQUIRE(err == DeserializationError::Ok);
  34. }
  35. SECTION("returns Ok if binary size >= 65536") {
  36. auto input = "\xc5\xff\xff" + std::string(65536, '?');
  37. auto err = deserializeMsgPack(doc, input);
  38. REQUIRE(err == DeserializationError::Ok);
  39. }
  40. }
  41. SECTION("bin 32 deserialization") {
  42. auto str = std::string(65536, '?');
  43. auto input = std::string("\xc6\x00\x01\x00\x00", 5) + str;
  44. auto err = deserializeMsgPack(doc, input);
  45. REQUIRE(err == DeserializationError::Ok);
  46. REQUIRE(doc.is<MsgPackBinary>());
  47. auto binary = doc.as<MsgPackBinary>();
  48. REQUIRE(binary.size() == 65536);
  49. REQUIRE(binary.data() != nullptr);
  50. REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
  51. binary.size()) == str);
  52. }
  53. SECTION("bin 32 serialization") {
  54. auto str = std::string(65536, '?');
  55. doc.set(MsgPackBinary(str.data(), str.size()));
  56. std::string output;
  57. auto result = serializeMsgPack(doc, output);
  58. REQUIRE(result == 5 + str.size());
  59. REQUIRE(output == std::string("\xc6\x00\x01\x00\x00", 5) + str);
  60. }
  61. }