iterator.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. template <typename TArray>
  7. static void run_iterator_test() {
  8. StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc;
  9. JsonArray tmp = doc.to<JsonArray>();
  10. tmp.add(12);
  11. tmp.add(34);
  12. TArray array = tmp;
  13. typename TArray::iterator it = array.begin();
  14. typename TArray::iterator end = array.end();
  15. REQUIRE(end != it);
  16. REQUIRE(12 == it->template as<int>());
  17. REQUIRE(12 == static_cast<int>(*it));
  18. ++it;
  19. REQUIRE(end != it);
  20. REQUIRE(34 == it->template as<int>());
  21. REQUIRE(34 == static_cast<int>(*it));
  22. ++it;
  23. REQUIRE(end == it);
  24. }
  25. TEST_CASE("JsonArray::begin()/end()") {
  26. SECTION("Non null JsonArray") {
  27. run_iterator_test<JsonArray>();
  28. }
  29. SECTION("Null JsonArray") {
  30. JsonArray array;
  31. REQUIRE(array.begin() == array.end());
  32. }
  33. }
  34. TEST_CASE("JsonArrayConst::begin()/end()") {
  35. SECTION("Non null JsonArrayConst") {
  36. run_iterator_test<JsonArrayConst>();
  37. }
  38. SECTION("Null JsonArrayConst") {
  39. JsonArrayConst array;
  40. REQUIRE(array.begin() == array.end());
  41. }
  42. }