algorithm_test.cpp 1.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#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);
    }
  });
}