string_length_size_1.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #define ARDUINOJSON_STRING_LENGTH_SIZE 1
  2. #include <ArduinoJson.h>
  3. #include <catch.hpp>
  4. #include <string>
  5. #include "Literals.hpp"
  6. TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 1") {
  7. JsonDocument doc;
  8. SECTION("set(std::string)") {
  9. SECTION("returns true if len <= 255") {
  10. auto result = doc.set(std::string(255, '?'));
  11. REQUIRE(result == true);
  12. REQUIRE(doc.overflowed() == false);
  13. }
  14. SECTION("returns false if len >= 256") {
  15. auto result = doc.set(std::string(256, '?'));
  16. REQUIRE(result == false);
  17. REQUIRE(doc.overflowed() == true);
  18. }
  19. }
  20. SECTION("set(MsgPackBinary)") {
  21. SECTION("returns true if size <= 253") {
  22. auto str = std::string(253, '?');
  23. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  24. REQUIRE(result == true);
  25. REQUIRE(doc.overflowed() == false);
  26. }
  27. SECTION("returns false if size >= 254") {
  28. auto str = std::string(254, '?');
  29. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  30. REQUIRE(result == false);
  31. REQUIRE(doc.overflowed() == true);
  32. }
  33. }
  34. SECTION("set(MsgPackExtension)") {
  35. SECTION("returns true if size <= 252") {
  36. auto str = std::string(252, '?');
  37. auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
  38. REQUIRE(result == true);
  39. REQUIRE(doc.overflowed() == false);
  40. }
  41. SECTION("returns false if size >= 253") {
  42. auto str = std::string(253, '?');
  43. auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
  44. REQUIRE(result == false);
  45. REQUIRE(doc.overflowed() == true);
  46. }
  47. }
  48. SECTION("deserializeJson()") {
  49. SECTION("returns Ok if string length <= 255") {
  50. auto input = "\"" + std::string(255, '?') + "\"";
  51. auto err = deserializeJson(doc, input);
  52. REQUIRE(err == DeserializationError::Ok);
  53. }
  54. SECTION("returns NoMemory if string length >= 256") {
  55. auto input = "\"" + std::string(256, '?') + "\"";
  56. auto err = deserializeJson(doc, input);
  57. REQUIRE(err == DeserializationError::NoMemory);
  58. }
  59. }
  60. SECTION("deserializeMsgPack()") {
  61. SECTION("returns Ok if string length <= 255") {
  62. auto input = "\xd9\xff" + std::string(255, '?');
  63. auto err = deserializeMsgPack(doc, input);
  64. REQUIRE(err == DeserializationError::Ok);
  65. }
  66. SECTION("returns NoMemory if string length >= 256") {
  67. auto input = "\xda\x01\x00"_s + std::string(256, '?');
  68. auto err = deserializeMsgPack(doc, input);
  69. REQUIRE(err == DeserializationError::NoMemory);
  70. }
  71. SECTION("returns Ok if binary size <= 253") {
  72. auto input = "\xc4\xfd" + std::string(253, '?');
  73. auto err = deserializeMsgPack(doc, input);
  74. REQUIRE(err == DeserializationError::Ok);
  75. }
  76. SECTION("returns NoMemory if binary size >= 254") {
  77. auto input = "\xc4\xfe" + std::string(254, '?');
  78. auto err = deserializeMsgPack(doc, input);
  79. REQUIRE(err == DeserializationError::NoMemory);
  80. }
  81. SECTION("returns Ok if extension size <= 252") {
  82. auto input = "\xc7\xfc\x01" + std::string(252, '?');
  83. auto err = deserializeMsgPack(doc, input);
  84. REQUIRE(err == DeserializationError::Ok);
  85. }
  86. SECTION("returns NoMemory if binary size >= 253") {
  87. auto input = "\xc7\xfd\x01" + std::string(253, '?');
  88. auto err = deserializeMsgPack(doc, input);
  89. REQUIRE(err == DeserializationError::NoMemory);
  90. }
  91. }
  92. }