overflowed.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  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 member add") {
  32. StaticJsonDocument<1> doc;
  33. doc["example"] = true;
  34. CHECK(doc.overflowed() == true);
  35. }
  36. SECTION("returns true after a failed deserialization") {
  37. StaticJsonDocument<JSON_ARRAY_SIZE(1)> doc;
  38. deserializeJson(doc, "[\"example\"]");
  39. CHECK(doc.overflowed() == true);
  40. }
  41. SECTION("returns false after a successful deserialization") {
  42. StaticJsonDocument<JSON_ARRAY_SIZE(1) + 8> doc;
  43. deserializeJson(doc, "[\"example\"]");
  44. CHECK(doc.overflowed() == false);
  45. }
  46. SECTION("returns false after clear()") {
  47. StaticJsonDocument<0> doc;
  48. doc.add(0);
  49. doc.clear();
  50. CHECK(doc.overflowed() == false);
  51. }
  52. SECTION("remains false after shrinkToFit()") {
  53. DynamicJsonDocument doc(JSON_ARRAY_SIZE(1));
  54. doc.add(0);
  55. doc.shrinkToFit();
  56. CHECK(doc.overflowed() == false);
  57. }
  58. SECTION("remains true after shrinkToFit()") {
  59. DynamicJsonDocument doc(JSON_ARRAY_SIZE(1));
  60. doc.add(0);
  61. doc.add(0);
  62. doc.shrinkToFit();
  63. CHECK(doc.overflowed() == true);
  64. }
  65. SECTION("return false after garbageCollect()") {
  66. DynamicJsonDocument doc(JSON_ARRAY_SIZE(1));
  67. doc.add(0);
  68. doc.add(0);
  69. doc.garbageCollect();
  70. CHECK(doc.overflowed() == false);
  71. }
  72. }