#include <catch.hpp>
#include <array>

#include <pls/pls.h>

using namespace pls;

TEST_CASE("for_each functions correctly", "[algorithms/for_each.h]") {
  malloc_scheduler_memory my_scheduler_memory{8, 2 << 12};
  scheduler my_scheduler{&my_scheduler_memory, 2};
  my_scheduler.perform_work([]() {
    constexpr int SIZE = 1000;
    std::array<int, SIZE> result_array{};
    result_array.fill(0);

    SECTION("integer ranges are processed exactly once") {
      pls::for_each_range(0, SIZE, [&result_array](int i) {
        result_array[i]++;
      });

      bool all_equal = true;
      for (int i = 0; i < SIZE; i++) {
        all_equal &= result_array[i] == 1;
      }
      REQUIRE (all_equal);
    }

    SECTION("iterators are processed exactly once") {
      std::array<int, SIZE> iterator_array{};
      for (int i = 0; i < SIZE; i++) {
        iterator_array[i] = i;
      }

      pls::for_each(iterator_array.begin(), iterator_array.end(), [&result_array](int i) {
        result_array[i]++;
      });

      bool all_equal = true;
      for (int i = 0; i < SIZE; i++) {
        all_equal &= result_array[i] == 1;
      }
      REQUIRE (all_equal);
    }
  });
}
