1 /*
2     Copyright (c) 2005-2020 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_Modifiable<utils::InputIterator<value_t>>(depth);
33         }
34     }
35 }
36 
37 //! Test container based overload
38 //! \brief \ref requirement \ref interface
39 TEST_CASE("Container based overload - input iterator based container") {
40     container_based_overload_test_case<utils::InputIterator>(/*expected_value*/0);
41 }
42 
43 const size_t elements = 10000;
44 const size_t init_sum = 0;
45 std::atomic<size_t> element_counter;
46 
47 template<size_t K>
48 struct set_to {
49     void operator()(size_t& x) const {
50         x = K;
51         ++element_counter;
52     }
53 };
54 
55 #include "common/range_based_for_support.h"
56 #include <functional>
57 #include <deque>
58 
59 template<typename... Context>
60 void WorkProducingTest(Context&... context) {
61     for ( auto concurrency_level : utils::concurrency_range() ) {
62         oneapi::tbb::global_control control(oneapi::tbb::global_control::max_allowed_parallelism, concurrency_level);
63 
64         using namespace range_based_for_support_tests;
65         std::deque<size_t> v(elements, 0);
66 
67         element_counter = 0;
68         oneapi::tbb::parallel_for_each(v.begin(), v.end(), set_to<0>(), context...);
69         REQUIRE_MESSAGE((element_counter == v.size() && element_counter == elements),
70             "not all elements were set");
71         REQUIRE_MESSAGE(range_based_for_accumulate(v, std::plus<size_t>(), init_sum) == init_sum,
72             "elements of v not all ones");
73 
74         element_counter = 0;
75         oneapi::tbb::parallel_for_each(v, set_to<1>(), context...);
76         REQUIRE_MESSAGE((element_counter == v.size() && element_counter == elements),
77             "not all elements were set");
78         REQUIRE_MESSAGE(range_based_for_accumulate(v, std::plus<size_t>(), init_sum) == v.size(),
79             "elements of v not all ones");
80 
81         element_counter = 0;
82         oneapi::tbb::parallel_for_each(oneapi::tbb::blocked_range<std::deque<size_t>::iterator>(v.begin(), v.end()), set_to<0>(), context...);
83         REQUIRE_MESSAGE((element_counter == v.size() && element_counter == elements),
84             "not all elements were set");
85         REQUIRE_MESSAGE(range_based_for_accumulate(v, std::plus<size_t>(), init_sum) == init_sum,
86             "elements of v not all zeros");
87     }
88 }
89 
90 //! Test that all elements were produced
91 //! \brief \ref requirement \ref stress
92 TEST_CASE("Test that all elements in range were produced through body (without task_group_context)") {
93     WorkProducingTest();
94 }
95 
96 //! Test that all elements were produced (with task_group_context)
97 //! \brief \ref requirement \ref interface \ref stress
98 TEST_CASE("Test that all elements in range were produced through body (with task_group_context)") {
99     oneapi::tbb::task_group_context context;
100     WorkProducingTest(context);
101 }
102 
103 //! Move iterator test for class that supports both move and copy semantics
104 //! \brief \ref requirement \ref interface
105 TEST_CASE("Move Semantics Test | Item: MovePreferable") {
106     DoTestMoveSemantics<TestMoveSem::MovePreferable>();
107 }
108 
109 //!  Move semantic test for move only class
110 //! \brief \ref requirement \ref interface
111 TEST_CASE("Move Semantics | Item: MoveOnly") {
112     //  parallel_for_each uses is_copy_constructible to support non-copyable types
113     DoTestMoveSemantics<TestMoveSem::MoveOnly>();
114 }
115