string_length_size_4.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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() returns true if string has 65536 characters") {
  8. auto result = doc.set(std::string(65536, '?'));
  9. REQUIRE(result == true);
  10. REQUIRE(doc.overflowed() == false);
  11. }
  12. SECTION("set() returns true if binary has 65536 characters") {
  13. auto str = std::string(65536, '?');
  14. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  15. REQUIRE(result == true);
  16. REQUIRE(doc.overflowed() == false);
  17. }
  18. SECTION("deserializeJson() returns Ok if string has 65536 characters") {
  19. auto input = "\"" + std::string(65536, '?') + "\"";
  20. auto err = deserializeJson(doc, input);
  21. REQUIRE(err == DeserializationError::Ok);
  22. }
  23. SECTION("deserializeMsgPack() returns Ok if string has 65536 characters") {
  24. auto input = "\xda\xff\xff" + std::string(65536, '?');
  25. auto err = deserializeMsgPack(doc, input);
  26. REQUIRE(err == DeserializationError::Ok);
  27. }
  28. SECTION("deserializeMsgPack() returns Ok if binary has 65536 characters") {
  29. auto input = "\xc5\xff\xff" + std::string(65536, '?');
  30. auto err = deserializeMsgPack(doc, input);
  31. REQUIRE(err == DeserializationError::Ok);
  32. }
  33. SECTION("bin 32 deserialization") {
  34. auto str = std::string(65536, '?');
  35. auto input = std::string("\xc6\x00\x01\x00\x00", 5) + str;
  36. auto err = deserializeMsgPack(doc, input);
  37. REQUIRE(err == DeserializationError::Ok);
  38. REQUIRE(doc.is<MsgPackBinary>());
  39. auto binary = doc.as<MsgPackBinary>();
  40. REQUIRE(binary.size() == 65536);
  41. REQUIRE(binary.data() != nullptr);
  42. REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
  43. binary.size()) == str);
  44. }
  45. SECTION("bin 32 serialization") {
  46. auto str = std::string(65536, '?');
  47. doc.set(MsgPackBinary(str.data(), str.size()));
  48. std::string output;
  49. auto result = serializeMsgPack(doc, output);
  50. REQUIRE(result == 5 + str.size());
  51. REQUIRE(output == std::string("\xc6\x00\x01\x00\x00", 5) + str);
  52. }
  53. }