From ce936ae658c431bfef49c7cdb842474d1a411b53 Mon Sep 17 00:00:00 2001 From: FritzFlorian Date: Fri, 14 Jun 2019 14:35:00 +0200 Subject: [PATCH] Add test for for_each and for_each_range. --- app/benchmark_matrix/main.cpp | 2 +- lib/pls/include/pls/pls.h | 1 + test/CMakeLists.txt | 3 ++- test/algorithm_test.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 test/algorithm_test.cpp 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); + } + }); +} -- libgit2 0.26.0