xref: /oneTBB/test/tbb/test_tick_count.cpp (revision b15aabb3)
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/test.h"
18 #include "common/utils.h"
19 #include "common/utils_concurrency_limit.h"
20 #include "common/spin_barrier.h"
21 
22 #include <tbb/tick_count.h>
23 #include <thread>
24 
25 //! \file test_tick_count.cpp
26 //! \brief Test for [timing] specification
27 
28 //! Testing clock type of tbb::tick_count
29 //! Clock in tbb::tick_count should be steady
30 //! \brief \ref requirement
31 TEST_CASE("Clock in tbb::tick_count should be steady") {
32     CHECK_EQ(tbb::tick_count::clock_type::is_steady, true);
33 }
34 
35 #if TBB_USE_EXCEPTIONS
36 //! Subtraction of equal tick_counts should not throw
37 //! \brief \ref error_guessing
38 TEST_CASE("Subtraction of equal tick_counts should not throw") {
39     tbb::tick_count tick_f = tbb::tick_count::now();
40     tbb::tick_count tick_s(tick_f);
41     CHECK_NOTHROW(tick_f - tick_s);
42 }
43 #endif
44 
45 //! Test that two tick_count values recorded on different threads can be meaningfully subtracted.
46 //! \brief \ref error_guessing
47 TEST_CASE("Test for subtracting calls to tick_count from different threads") {
48     auto num_of_threads = utils::get_platform_max_threads();
49 
50     utils::SpinBarrier thread_barrier(num_of_threads);
51     tbb::tick_count start_time;
52 
__anon7e030fbd0102(std::size_t ) 53     auto diff_func = [&thread_barrier, &start_time] (std::size_t ) {
54         thread_barrier.wait([&start_time] { start_time = tbb::tick_count::now(); });
55 
56         tbb::tick_count end_time(tbb::tick_count::now());
57         while ((end_time - start_time).seconds() == 0) {
58             end_time = tbb::tick_count::now();
59         }
60 
61         CHECK_GT((end_time - start_time).seconds(), 0);
62     };
63 
64     for (std::size_t i = 0; i < 10; ++i) {
65         utils::NativeParallelFor(num_of_threads, diff_func);
66     }
67 }
68