1 /*
2     Copyright (c) 2020-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/test.h"
18 #include "common/utils_concurrency_limit.h"
19 
20 #include "oneapi/tbb/parallel_sort.h"
21 #include "oneapi/tbb/global_control.h"
22 
23 #include <vector>
24 
25 //! \file conformance_parallel_sort.cpp
26 //! \brief Test for [algorithms.parallel_sort]
27 
28 const int vector_size = 10000;
29 
get_random_vector()30 std::vector<int> get_random_vector() {
31     std::vector<int> result(vector_size);
32     for (int i = 0; i < vector_size; ++i)
33         result[i] = rand() % vector_size;
34     return result;
35 }
36 
37 //! Iterator based range sorting test (default comparator)
38 //! \brief \ref requirement \ref interface
39 TEST_CASE("Iterator based range sorting test (default comparator)") {
40     for ( auto concurrency_level : utils::concurrency_range() ) {
41         oneapi::tbb::global_control control(oneapi::tbb::global_control::max_allowed_parallelism, concurrency_level);
42 
43         auto test_vector = get_random_vector();
44         oneapi::tbb::parallel_sort(test_vector.begin(), test_vector.end());
45 
46         for(auto it = test_vector.begin(); it != test_vector.end() - 1; ++it)
47             REQUIRE_MESSAGE(*it <= *(it+1), "Testing data not sorted");
48     }
49 }
50 
51 //! Iterator based range sorting test (greater comparator)
52 //! \brief \ref requirement \ref interface
53 TEST_CASE ("Iterator based range sorting test (greater comparator)") {
54     for ( auto concurrency_level : utils::concurrency_range() ) {
55         oneapi::tbb::global_control control(oneapi::tbb::global_control::max_allowed_parallelism, concurrency_level);
56 
57         auto test_vector = get_random_vector();
58         oneapi::tbb::parallel_sort(test_vector.begin(), test_vector.end(), std::greater<int>());
59 
60         for(auto it = test_vector.begin(); it != test_vector.end() - 1; ++it)
61             REQUIRE_MESSAGE(*it >= *(it+1), "Testing data not sorted");
62     }
63 }
64 
65 //! Range sorting test (default comparator)
66 //! \brief \ref requirement \ref interface
67 TEST_CASE ("Range sorting test (default comparator)") {
68     for ( auto concurrency_level : utils::concurrency_range() ) {
69         oneapi::tbb::global_control control(oneapi::tbb::global_control::max_allowed_parallelism, concurrency_level);
70 
71         auto test_vector = get_random_vector();
72         oneapi::tbb::parallel_sort(test_vector);
73 
74         for(auto it = test_vector.begin(); it != test_vector.end() - 1; ++it)
75             REQUIRE_MESSAGE(*it <= *(it+1), "Testing data not sorted");
76     }
77 }
78 
79 //! Range sorting test (greater comparator)
80 //! \brief \ref requirement \ref interface
81 TEST_CASE ("Range sorting test (greater comparator)") {
82     for ( auto concurrency_level : utils::concurrency_range() ) {
83         oneapi::tbb::global_control control(oneapi::tbb::global_control::max_allowed_parallelism, concurrency_level);
84 
85         auto test_vector = get_random_vector();
86         oneapi::tbb::parallel_sort(test_vector, std::greater<int>());
87 
88         for(auto it = test_vector.begin(); it != test_vector.end() - 1; ++it)
89             REQUIRE_MESSAGE(*it >= *(it+1), "Testing data not sorted");
90     }
91 }
92