overflowed.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. #include "Literals.hpp"
  8. TEST_CASE("JsonDocument::overflowed()") {
  9. TimebombAllocator timebomb(10);
  10. JsonDocument doc(&timebomb);
  11. SECTION("returns false on a fresh object") {
  12. timebomb.setCountdown(0);
  13. CHECK(doc.overflowed() == false);
  14. }
  15. SECTION("returns true after a failed insertion") {
  16. timebomb.setCountdown(0);
  17. doc.add(0);
  18. CHECK(doc.overflowed() == true);
  19. }
  20. SECTION("returns false after successful insertion") {
  21. timebomb.setCountdown(2);
  22. doc.add(0);
  23. CHECK(doc.overflowed() == false);
  24. }
  25. SECTION("returns true after a failed string copy") {
  26. timebomb.setCountdown(0);
  27. doc.add("example"_s);
  28. CHECK(doc.overflowed() == true);
  29. }
  30. SECTION("returns false after a successful string copy") {
  31. timebomb.setCountdown(3);
  32. doc.add("example"_s);
  33. CHECK(doc.overflowed() == false);
  34. }
  35. SECTION("returns true after a failed member add") {
  36. timebomb.setCountdown(0);
  37. doc["example"] = true;
  38. CHECK(doc.overflowed() == true);
  39. }
  40. SECTION("returns true after a failed deserialization") {
  41. timebomb.setCountdown(0);
  42. deserializeJson(doc, "[1, 2]");
  43. CHECK(doc.overflowed() == true);
  44. }
  45. SECTION("returns false after a successful deserialization") {
  46. timebomb.setCountdown(3);
  47. deserializeJson(doc, "[\"example\"]");
  48. CHECK(doc.overflowed() == false);
  49. }
  50. SECTION("returns false after clear()") {
  51. timebomb.setCountdown(0);
  52. doc.add(0);
  53. doc.clear();
  54. CHECK(doc.overflowed() == false);
  55. }
  56. SECTION("remains false after shrinkToFit()") {
  57. timebomb.setCountdown(2);
  58. doc.add(0);
  59. timebomb.setCountdown(2);
  60. doc.shrinkToFit();
  61. CHECK(doc.overflowed() == false);
  62. }
  63. SECTION("remains true after shrinkToFit()") {
  64. timebomb.setCountdown(0);
  65. doc.add(0);
  66. timebomb.setCountdown(2);
  67. doc.shrinkToFit();
  68. CHECK(doc.overflowed() == true);
  69. }
  70. SECTION("returns false when string length doesn't overflow") {
  71. auto maxLength = ArduinoJson::detail::StringNode::maxLength;
  72. CHECK(doc.set(std::string(maxLength, 'a')) == true);
  73. CHECK(doc.overflowed() == false);
  74. }
  75. SECTION("returns true when string length overflows") {
  76. auto maxLength = ArduinoJson::detail::StringNode::maxLength;
  77. CHECK(doc.set(std::string(maxLength + 1, 'a')) == false);
  78. CHECK(doc.overflowed() == true);
  79. }
  80. }