diff --git a/test/stdpar/tests/exclusive_scan.cpp b/test/stdpar/tests/exclusive_scan.cpp new file mode 100644 index 00000000000..a4273cade20 --- /dev/null +++ b/test/stdpar/tests/exclusive_scan.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include + +int main() +{ + constexpr std::size_t N = 1 << 16; + + std::vector in(N); + std::vector out(N); + + std::iota(in.begin(), in.end(), 1); + + // exclusive_scan with non-zero initial value + const int init = 42; + std::exclusive_scan(std::execution::par, in.begin(), in.end(), out.begin(), init); + + int running_sum = init; + for (std::size_t i = 0; i < N; ++i) + { + if (out[i] != running_sum) + { + return 1; + } + running_sum += in[i]; + } + + // exclusive_scan with initial value & binary ops + const int mul_init = 1; + std::exclusive_scan(std::execution::par, in.begin(), in.end(), out.begin(), mul_init, std::multiplies{}); + + int expected = mul_init; + for (std::size_t i = 0; i < N; ++i) + { + if (out[i] != expected) + { + return 1; + } + expected *= 2; + } + + // exclusive_scan with initial value & custom binary ops + const int init2 = 7; + std::exclusive_scan( + std::execution::par, + in.begin(), + in.end(), + out.begin(), + init2, + [](int lhs, int rhs) { + return lhs + rhs + 1; + }) + + for (std::size_t i = 0; i < N; ++i) + { + const int expected = init2 + static_cast(2 * i); + if (out[i] != expected) + { + return 1; + } + } + + return 0; +}