diff --git a/app/benchmark_matrix/main.cpp b/app/benchmark_matrix/main.cpp index 327cfca..c6fc793 100644 --- a/app/benchmark_matrix/main.cpp +++ b/app/benchmark_matrix/main.cpp @@ -14,7 +14,7 @@ class matrix { } void multiply(const matrix &a, const matrix &b) { - pls::algorithm::for_each_range(0, SIZE, [&](int i) { + pls::for_each_range(0, SIZE, [&](int i) { this->multiply_column(i, a, b); }); } diff --git a/lib/pls/include/pls/pls.h b/lib/pls/include/pls/pls.h index 9308710..f90d55e 100644 --- a/lib/pls/include/pls/pls.h +++ b/lib/pls/include/pls/pls.h @@ -24,6 +24,7 @@ using internal::scheduling::task; using algorithm::invoke; using algorithm::for_each; +using algorithm::for_each_range; using algorithm::scan; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 416b530..f9af66a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,6 @@ add_executable(tests main.cpp data_structures_test.cpp - scheduling_tests.cpp) + scheduling_tests.cpp + algorithm_test.cpp) target_link_libraries(tests catch2 pls) diff --git a/test/algorithm_test.cpp b/test/algorithm_test.cpp new file mode 100644 index 0000000..41eb56b --- /dev/null +++ b/test/algorithm_test.cpp @@ -0,0 +1,45 @@ +#include +#include + +#include + +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 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 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); + } + }); +}