1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #include "common/parallel_for_each_common.h"
18 
19 //! \file conformance_parallel_for_each.cpp
20 //! \brief Test for [algorithms.parallel_for_each] specification
21 
22 //! Test input access iterator support
23 //! \brief \ref requirement \ref interface
24 TEST_CASE("Input iterator support") {
25     for ( auto concurrency_level : utils::concurrency_range() ) {
26         oneapi::tbb::global_control control(oneapi::tbb::global_control::max_allowed_parallelism, concurrency_level);
27 
28         for( size_t depth = 0; depth <= depths_nubmer; ++depth ) {
29             g_tasks_expected = 0;
30             for ( size_t i=0; i < depth; ++i )
31                 g_tasks_expected += FindNumOfTasks(g_depths[i].value());
32             TestIterator_Const<utils::InputIterator<value_t>>(depth);
33             TestIterator_Move<utils::InputIterator<value_t>>(depth);
34 #if __TBB_CPP14_GENERIC_LAMBDAS_PRESENT
35             TestGenericLambdasCommon<utils::InputIterator<value_t>>(depth);
36 #endif
37         }
38     }
39 }
40 
41 //! Test container based overload
42 //! \brief \ref requirement \ref interface
43 TEST_CASE("Container based overload - input iterator based container") {
44     container_based_overload_test_case<utils::InputIterator, incremental_functor_const>(/*expected_value*/0);
45 }
46 
47 const size_t elements = 10000;
48 const size_t init_sum = 0;
49 std::atomic<size_t> element_counter;
50 
51 template<size_t K>
52 struct set_to {
53     void operator()(size_t& x) const {
54         x = K;
55         ++element_counter;
56     }
57 };
58 
59 #include "common/range_based_for_support.h"
60 #include <functional>
61 #include <deque>
62 
63 template<typename... Context>
64 void WorkProducingTest(Context&... context) {
65     for ( auto concurrency_level : utils::concurrency_range() ) {
66         oneapi::tbb::global_control control(oneapi::tbb::global_control::max_allowed_parallelism, concurrency_level);
67 
68         using namespace range_based_for_support_tests;
69         std::deque<size_t> v(elements, 0);
70 
71         element_counter = 0;
72         oneapi::tbb::parallel_for_each(v.begin(), v.end(), set_to<0>(), context...);
73         REQUIRE_MESSAGE((element_counter == v.size() && element_counter == elements),
74             "not all elements were set");
75         REQUIRE_MESSAGE(range_based_for_accumulate(v, std::plus<size_t>(), init_sum) == init_sum,
76             "elements of v not all ones");
77 
78         element_counter = 0;
79         oneapi::tbb::parallel_for_each(v, set_to<1>(), context...);
80         REQUIRE_MESSAGE((element_counter == v.size() && element_counter == elements),
81             "not all elements were set");
82         REQUIRE_MESSAGE(range_based_for_accumulate(v, std::plus<size_t>(), init_sum) == v.size(),
83             "elements of v not all ones");
84 
85         element_counter = 0;
86         oneapi::tbb::parallel_for_each(oneapi::tbb::blocked_range<std::deque<size_t>::iterator>(v.begin(), v.end()), set_to<0>(), context...);
87         REQUIRE_MESSAGE((element_counter == v.size() && element_counter == elements),
88             "not all elements were set");
89         REQUIRE_MESSAGE(range_based_for_accumulate(v, std::plus<size_t>(), init_sum) == init_sum,
90             "elements of v not all zeros");
91     }
92 }
93 
94 //! Test that all elements were produced
95 //! \brief \ref requirement \ref stress
96 TEST_CASE("Test that all elements in range were produced through body (without task_group_context)") {
97     WorkProducingTest();
98 }
99 
100 //! Test that all elements were produced (with task_group_context)
101 //! \brief \ref requirement \ref interface \ref stress
102 TEST_CASE("Test that all elements in range were produced through body (with task_group_context)") {
103     oneapi::tbb::task_group_context context;
104     WorkProducingTest(context);
105 }
106 
107 //! Move iterator test for class that supports both move and copy semantics
108 //! \brief \ref requirement \ref interface
109 TEST_CASE("Move Semantics Test | Item: MovePreferable") {
110     DoTestMoveSemantics<TestMoveSem::MovePreferable>();
111 }
112 
113 //!  Move semantic test for move only class
114 //! \brief \ref requirement \ref interface
115 TEST_CASE("Move Semantics | Item: MoveOnly") {
116     //  parallel_for_each uses is_copy_constructible to support non-copyable types
117     DoTestMoveSemantics<TestMoveSem::MoveOnly>();
118 }
119