string_length_size_1.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(std::string)") {
  8. SECTION("returns true if len <= 255") {
  9. auto result = doc.set(std::string(255, '?'));
  10. REQUIRE(result == true);
  11. REQUIRE(doc.overflowed() == false);
  12. }
  13. SECTION("returns false if len >= 256") {
  14. auto result = doc.set(std::string(256, '?'));
  15. REQUIRE(result == false);
  16. REQUIRE(doc.overflowed() == true);
  17. }
  18. }
  19. SECTION("set(MsgPackBinary)") {
  20. SECTION("returns true if size <= 253") {
  21. auto str = std::string(253, '?');
  22. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  23. REQUIRE(result == true);
  24. REQUIRE(doc.overflowed() == false);
  25. }
  26. SECTION("returns false if size >= 254") {
  27. auto str = std::string(254, '?');
  28. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  29. REQUIRE(result == false);
  30. REQUIRE(doc.overflowed() == true);
  31. }
  32. }
  33. SECTION("deserializeJson()") {
  34. SECTION("returns Ok if string length <= 255") {
  35. auto input = "\"" + std::string(255, '?') + "\"";
  36. auto err = deserializeJson(doc, input);
  37. REQUIRE(err == DeserializationError::Ok);
  38. }
  39. SECTION("returns NoMemory if string length >= 256") {
  40. auto input = "\"" + std::string(256, '?') + "\"";
  41. auto err = deserializeJson(doc, input);
  42. REQUIRE(err == DeserializationError::NoMemory);
  43. }
  44. }
  45. SECTION("deserializeMsgPack()") {
  46. SECTION("returns Ok if string length <= 255") {
  47. auto input = "\xd9\xff" + std::string(255, '?');
  48. auto err = deserializeMsgPack(doc, input);
  49. REQUIRE(err == DeserializationError::Ok);
  50. }
  51. SECTION("returns NoMemory if string length >= 256") {
  52. auto input = std::string("\xda\x01\x00", 3) + std::string(256, '?');
  53. auto err = deserializeMsgPack(doc, input);
  54. REQUIRE(err == DeserializationError::NoMemory);
  55. }
  56. SECTION("returns Ok if binary size <= 253") {
  57. auto input = "\xc4\xfd" + std::string(253, '?');
  58. auto err = deserializeMsgPack(doc, input);
  59. REQUIRE(err == DeserializationError::Ok);
  60. }
  61. SECTION("returns NoMemory if binary size >= 254") {
  62. auto input = "\xc4\xfe" + std::string(254, '?');
  63. auto err = deserializeMsgPack(doc, input);
  64. REQUIRE(err == DeserializationError::NoMemory);
  65. }
  66. }
  67. }