xref: /oneTBB/test/tbb/test_blocked_range.cpp (revision d86ed7fb)
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/test.h"
18 #include "common/utils.h"
19 #include "common/utils_report.h"
20 #include "common/range_based_for_support.h"
21 #include "common/config.h"
22 
23 #include "tbb/blocked_range.h"
24 
25 //! \file test_blocked_range.cpp
26 //! \brief Test for [algorithms.blocked_range] specification
27 
28 #include <utility> //for std::pair
29 #include <functional>
30 
31 //! Testing blocked_range with range based for
32 //! \brief \ref interface
33 TEST_CASE("Range based for") {
34     using namespace range_based_for_support_tests;
35 
36     const std::size_t sequence_length = 100;
37     std::size_t int_array[sequence_length] = {0};
38 
39     for (std::size_t i = 0; i < sequence_length; ++i) {
40         int_array[i] = i + 1;
41     }
42     const tbb::blocked_range<std::size_t*> r(int_array, int_array + sequence_length, 1);
43 
44     CHECK_MESSAGE(range_based_for_accumulate<std::size_t>(r, std::plus<std::size_t>(), std::size_t(0))
45             == gauss_summ_of_int_sequence(sequence_length), "incorrect accumulated value generated via range based for ?");
46 }
47 
48 //! Proportional split does not overflow with blocked range
49 //! \brief \ref error_guessing \ref boundary
50 TEST_CASE("Proportional split overflow") {
51     using tbb::blocked_range;
52     using tbb::proportional_split;
53 
54     blocked_range<std::size_t> r1(0, std::size_t(-1) / 2);
55     std::size_t size = r1.size();
56     std::size_t begin = r1.begin();
57     std::size_t end = r1.end();
58 
59     proportional_split p(1, 3);
60     blocked_range<std::size_t> r2(r1, p);
61 
62     // overflow-free computation
63     std::size_t parts = p.left() + p.right();
64     std::size_t int_part = size / parts;
65     std::size_t fraction = size - int_part * parts; // fraction < parts
66     std::size_t right_idx = int_part * p.right() + fraction * p.right() / parts + 1;
67     std::size_t newRangeBegin = end - right_idx;
68 
69     // Division in 'right_idx' very likely is inexact also.
70     std::size_t tolerance = 1;
71     std::size_t diff = (r2.begin() < newRangeBegin) ? (newRangeBegin - r2.begin()) : (r2.begin() - newRangeBegin);
72     bool is_split_correct = diff <= tolerance;
73     bool test_passed = (r1.begin() == begin && r1.end() == r2.begin() && is_split_correct &&
74                         r2.end() == end);
75     if (!test_passed) {
76         REPORT("Incorrect split of blocked range[%lu, %lu) into r1[%lu, %lu) and r2[%lu, %lu), "
77                "must be r1[%lu, %lu) and r2[%lu, %lu)\n", begin, end, r1.begin(), r1.end(), r2.begin(), r2.end(), begin, newRangeBegin, newRangeBegin, end);
78         CHECK(test_passed);
79     }
80 }
81 
82