overflowed.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2021
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonDocument::overflowed()") {
  7. SECTION("returns false on a fresh object") {
  8. StaticJsonDocument<0> doc;
  9. CHECK(doc.overflowed() == false);
  10. }
  11. SECTION("returns true after a failed insertion") {
  12. StaticJsonDocument<0> doc;
  13. doc.add(0);
  14. CHECK(doc.overflowed() == true);
  15. }
  16. SECTION("returns false after successful insertion") {
  17. StaticJsonDocument<JSON_ARRAY_SIZE(1)> doc;
  18. doc.add(0);
  19. CHECK(doc.overflowed() == false);
  20. }
  21. SECTION("returns true after a failed string copy") {
  22. StaticJsonDocument<JSON_ARRAY_SIZE(1)> doc;
  23. doc.add(std::string("example"));
  24. CHECK(doc.overflowed() == true);
  25. }
  26. SECTION("returns false after a successful string copy") {
  27. StaticJsonDocument<JSON_ARRAY_SIZE(1) + 8> doc;
  28. doc.add(std::string("example"));
  29. CHECK(doc.overflowed() == false);
  30. }
  31. SECTION("returns true after a failed deserialization") {
  32. StaticJsonDocument<JSON_ARRAY_SIZE(1)> doc;
  33. deserializeJson(doc, "[\"example\"]");
  34. CHECK(doc.overflowed() == true);
  35. }
  36. SECTION("returns false after a successful deserialization") {
  37. StaticJsonDocument<JSON_ARRAY_SIZE(1) + 8> doc;
  38. deserializeJson(doc, "[\"example\"]");
  39. CHECK(doc.overflowed() == false);
  40. }
  41. SECTION("returns false after clear()") {
  42. StaticJsonDocument<0> doc;
  43. doc.add(0);
  44. doc.clear();
  45. CHECK(doc.overflowed() == false);
  46. }
  47. SECTION("remains false after shrinkToFit()") {
  48. DynamicJsonDocument doc(JSON_ARRAY_SIZE(1));
  49. doc.add(0);
  50. doc.shrinkToFit();
  51. CHECK(doc.overflowed() == false);
  52. }
  53. SECTION("remains true after shrinkToFit()") {
  54. DynamicJsonDocument doc(JSON_ARRAY_SIZE(1));
  55. doc.add(0);
  56. doc.add(0);
  57. doc.shrinkToFit();
  58. CHECK(doc.overflowed() == true);
  59. }
  60. SECTION("return false after garbageCollect()") {
  61. DynamicJsonDocument doc(JSON_ARRAY_SIZE(1));
  62. doc.add(0);
  63. doc.add(0);
  64. doc.garbageCollect();
  65. CHECK(doc.overflowed() == false);
  66. }
  67. }