remove.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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("JsonArray::remove()") {
  7. DynamicJsonDocument doc(4096);
  8. JsonArray array = doc.to<JsonArray>();
  9. array.add(1);
  10. array.add(2);
  11. array.add(3);
  12. SECTION("remove first by index") {
  13. array.remove(0);
  14. REQUIRE(2 == array.size());
  15. REQUIRE(array[0] == 2);
  16. REQUIRE(array[1] == 3);
  17. }
  18. SECTION("remove middle by index") {
  19. array.remove(1);
  20. REQUIRE(2 == array.size());
  21. REQUIRE(array[0] == 1);
  22. REQUIRE(array[1] == 3);
  23. }
  24. SECTION("remove last by index") {
  25. array.remove(2);
  26. REQUIRE(2 == array.size());
  27. REQUIRE(array[0] == 1);
  28. REQUIRE(array[1] == 2);
  29. }
  30. SECTION("remove first by iterator") {
  31. JsonArray::iterator it = array.begin();
  32. array.remove(it);
  33. REQUIRE(2 == array.size());
  34. REQUIRE(array[0] == 2);
  35. REQUIRE(array[1] == 3);
  36. }
  37. SECTION("remove middle by iterator") {
  38. JsonArray::iterator it = array.begin();
  39. ++it;
  40. array.remove(it);
  41. REQUIRE(2 == array.size());
  42. REQUIRE(array[0] == 1);
  43. REQUIRE(array[1] == 3);
  44. }
  45. SECTION("remove last bty iterator") {
  46. JsonArray::iterator it = array.begin();
  47. ++it;
  48. ++it;
  49. array.remove(it);
  50. REQUIRE(2 == array.size());
  51. REQUIRE(array[0] == 1);
  52. REQUIRE(array[1] == 2);
  53. }
  54. SECTION("In a loop") {
  55. for (JsonArray::iterator it = array.begin(); it != array.end(); ++it) {
  56. if (*it == 2)
  57. array.remove(it);
  58. }
  59. REQUIRE(2 == array.size());
  60. REQUIRE(array[0] == 1);
  61. REQUIRE(array[1] == 3);
  62. }
  63. SECTION("remove by index on unbound reference") {
  64. JsonArray unboundArray;
  65. unboundArray.remove(20);
  66. }
  67. SECTION("remove by iterator on unbound reference") {
  68. JsonArray unboundArray;
  69. unboundArray.remove(unboundArray.begin());
  70. }
  71. }