iterator.cpp 923 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <catch.hpp>
  9. template <typename TIterator>
  10. static void run_iterator_test() {
  11. StaticJsonBuffer<JSON_ARRAY_SIZE(2)> jsonBuffer;
  12. JsonArray &array = jsonBuffer.createArray();
  13. array.add(12);
  14. array.add(34);
  15. TIterator it = array.begin();
  16. TIterator end = array.end();
  17. REQUIRE(end != it);
  18. REQUIRE(12 == it->template as<int>());
  19. REQUIRE(12 == static_cast<int>(*it));
  20. ++it;
  21. REQUIRE(end != it);
  22. REQUIRE(34 == it->template as<int>());
  23. REQUIRE(34 == static_cast<int>(*it));
  24. ++it;
  25. REQUIRE(end == it);
  26. }
  27. TEST_CASE("JsonArray::begin()/end()") {
  28. SECTION("Mutable") {
  29. run_iterator_test<JsonArray::iterator>();
  30. }
  31. SECTION("Const") {
  32. run_iterator_test<JsonArray::const_iterator>();
  33. }
  34. }