remove.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. TEST_CASE("JsonArray::remove()") {
  8. JsonDocument doc;
  9. JsonArray array = doc.to<JsonArray>();
  10. array.add(1);
  11. array.add(2);
  12. array.add(3);
  13. SECTION("remove first by index") {
  14. array.remove(0);
  15. REQUIRE(2 == array.size());
  16. REQUIRE(array[0] == 2);
  17. REQUIRE(array[1] == 3);
  18. }
  19. SECTION("remove middle by index") {
  20. array.remove(1);
  21. REQUIRE(2 == array.size());
  22. REQUIRE(array[0] == 1);
  23. REQUIRE(array[1] == 3);
  24. }
  25. SECTION("remove last by index") {
  26. array.remove(2);
  27. REQUIRE(2 == array.size());
  28. REQUIRE(array[0] == 1);
  29. REQUIRE(array[1] == 2);
  30. }
  31. SECTION("remove first by iterator") {
  32. JsonArray::iterator it = array.begin();
  33. array.remove(it);
  34. REQUIRE(2 == array.size());
  35. REQUIRE(array[0] == 2);
  36. REQUIRE(array[1] == 3);
  37. }
  38. SECTION("remove middle by iterator") {
  39. JsonArray::iterator it = array.begin();
  40. ++it;
  41. array.remove(it);
  42. REQUIRE(2 == array.size());
  43. REQUIRE(array[0] == 1);
  44. REQUIRE(array[1] == 3);
  45. }
  46. SECTION("remove last bty iterator") {
  47. JsonArray::iterator it = array.begin();
  48. ++it;
  49. ++it;
  50. array.remove(it);
  51. REQUIRE(2 == array.size());
  52. REQUIRE(array[0] == 1);
  53. REQUIRE(array[1] == 2);
  54. }
  55. SECTION("remove end()") {
  56. array.remove(array.end());
  57. REQUIRE(3 == array.size());
  58. }
  59. SECTION("In a loop") {
  60. for (JsonArray::iterator it = array.begin(); it != array.end(); ++it) {
  61. if (*it == 2)
  62. array.remove(it);
  63. }
  64. REQUIRE(2 == array.size());
  65. REQUIRE(array[0] == 1);
  66. REQUIRE(array[1] == 3);
  67. }
  68. SECTION("remove by index on unbound reference") {
  69. JsonArray unboundArray;
  70. unboundArray.remove(20);
  71. }
  72. SECTION("remove by iterator on unbound reference") {
  73. JsonArray unboundArray;
  74. unboundArray.remove(unboundArray.begin());
  75. }
  76. SECTION("use JsonVariant as index") {
  77. array.remove(array[3]); // no effect with null variant
  78. array.remove(array[0]); // remove element at index 1
  79. REQUIRE(2 == array.size());
  80. REQUIRE(array[0] == 1);
  81. REQUIRE(array[1] == 3);
  82. }
  83. }
  84. TEST_CASE("Removed elements are recycled") {
  85. SpyingAllocator spy;
  86. JsonDocument doc(&spy);
  87. JsonArray array = doc.to<JsonArray>();
  88. // fill the pool entirely
  89. for (int i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++)
  90. array.add(i);
  91. // free one slot in the pool
  92. array.remove(0);
  93. // add one element; it should use the free slot
  94. array.add(42);
  95. REQUIRE(spy.log() == AllocatorLog{
  96. Allocate(sizeofPool()), // only one pool
  97. });
  98. }