string_length_size_1.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #define ARDUINOJSON_STRING_LENGTH_SIZE 1
  2. #include <ArduinoJson.h>
  3. #include <catch.hpp>
  4. #include <string>
  5. TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 1") {
  6. JsonDocument doc;
  7. SECTION("set() returns true if string has 255 characters") {
  8. auto result = doc.set(std::string(255, '?'));
  9. REQUIRE(result == true);
  10. REQUIRE(doc.overflowed() == false);
  11. }
  12. SECTION("set() returns false if string has 256 characters") {
  13. auto result = doc.set(std::string(256, '?'));
  14. REQUIRE(result == false);
  15. REQUIRE(doc.overflowed() == true);
  16. }
  17. SECTION("set() returns true if binary has 253 characters") {
  18. auto str = std::string(253, '?');
  19. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  20. REQUIRE(result == true);
  21. REQUIRE(doc.overflowed() == false);
  22. }
  23. SECTION("set() returns false if binary has 254 characters") {
  24. auto str = std::string(254, '?');
  25. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  26. REQUIRE(result == false);
  27. REQUIRE(doc.overflowed() == true);
  28. }
  29. SECTION("deserializeJson() returns Ok if string has 255 characters") {
  30. auto input = "\"" + std::string(255, '?') + "\"";
  31. auto err = deserializeJson(doc, input);
  32. REQUIRE(err == DeserializationError::Ok);
  33. }
  34. SECTION("deserializeJson() returns NoMemory if string has 256 characters") {
  35. auto input = "\"" + std::string(256, '?') + "\"";
  36. auto err = deserializeJson(doc, input);
  37. REQUIRE(err == DeserializationError::NoMemory);
  38. }
  39. SECTION("deserializeMsgPack() returns Ok if string has 255 characters") {
  40. auto input = "\xd9\xff" + std::string(255, '?');
  41. auto err = deserializeMsgPack(doc, input);
  42. REQUIRE(err == DeserializationError::Ok);
  43. }
  44. SECTION(
  45. "deserializeMsgPack() returns NoMemory if string has 256 characters") {
  46. auto input = std::string("\xda\x01\x00", 3) + std::string(256, '?');
  47. auto err = deserializeMsgPack(doc, input);
  48. REQUIRE(err == DeserializationError::NoMemory);
  49. }
  50. SECTION("deserializeMsgPack() returns Ok if binary has 253 characters") {
  51. auto input = "\xc4\xfd" + std::string(253, '?');
  52. auto err = deserializeMsgPack(doc, input);
  53. REQUIRE(err == DeserializationError::Ok);
  54. }
  55. SECTION(
  56. "deserializeMsgPack() returns NoMemory if binary has 254 characters") {
  57. auto input = "\xc4\xfe" + std::string(254, '?');
  58. auto err = deserializeMsgPack(doc, input);
  59. REQUIRE(err == DeserializationError::NoMemory);
  60. }
  61. }