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.h"
19 
20 #include "oneapi/tbb/tick_count.h"
21 #include "oneapi/tbb/detail/_template_helpers.h"
22 #include <type_traits>
23 
24 //! \file conformance_tick_count.cpp
25 //! \brief Test for [timing] specification
26 
27 //! Testing default construction
28 //! \brief \ref interface
29 TEST_CASE("Default construction") {
30     oneapi::tbb::tick_count t1;
31     utils::suppress_unused_warning(t1);
32 }
33 
34 //! Testing subtraction operation
35 //! Subtraction of two equal tick_counts after call seconds must be equal to 0
36 //! \brief \ref interface \ref requirement
37 TEST_CASE("Subtraction of equal tick_counts") {
38     oneapi::tbb::tick_count tick_f = oneapi::tbb::tick_count::now();
39     oneapi::tbb::tick_count tick_s(tick_f);
40     CHECK_EQ((tick_f - tick_s).seconds(), 0);
41 }
42 
43 //! Testing subsequent timestamp
44 //! \brief \ref requirement
45 TEST_CASE("Subtraction subsequent timestamp") {
46     oneapi::tbb::tick_count tick_f(oneapi::tbb::tick_count::now());
47     oneapi::tbb::tick_count tick_s(oneapi::tbb::tick_count::now());
48     while ((tick_s - tick_f).seconds() == 0) {
49         tick_s = oneapi::tbb::tick_count::now();
50     }
51     CHECK_GT((tick_s - tick_f).seconds(), 0);
52 }
53 
54 // Wait for given duration.
55 // The duration parameter is in units of seconds.
WaitForDuration(double duration)56 static void WaitForDuration( double duration ) {
57     CHECK_GT(duration, 0);
58     oneapi::tbb::tick_count start = oneapi::tbb::tick_count::now();
59     double sec = 0;
60     do {
61         sec = (oneapi::tbb::tick_count::now() - start).seconds();
62         CHECK_GE(sec, 0);
63     } while (sec < duration);
64 }
65 
66 // CHECK that two times in seconds are very close.
CheckNear(double x,double y)67 void CheckNear( double x, double y ) {
68     CHECK_GE(x-y, -1.0E-10);
69     CHECK_GE(1.0E-10, x-y);
70 }
71 
72 //! Test arithmetic operators on tick_count::interval_t
73 //! \brief \ref interface \ref requirement
74 TEST_CASE("Arithmetic operators") {
75     oneapi::tbb::tick_count t1 = oneapi::tbb::tick_count::now();
76     WaitForDuration(0.000001);
77     oneapi::tbb::tick_count t2 = oneapi::tbb::tick_count::now();
78     WaitForDuration(0.0000012);
79     oneapi::tbb::tick_count t3 = oneapi::tbb::tick_count::now();
80 
81     using interval_type = oneapi::tbb::tick_count::interval_t;
82     interval_type i = t2 - t1;
83     interval_type j = t3 - t2;
84     interval_type k = t3 - t1;
85     CHECK_EQ(std::is_same<oneapi::tbb::tick_count::interval_t, decltype(i - j)>::value, true);
86     CHECK_EQ(std::is_same<oneapi::tbb::tick_count::interval_t, decltype(i + j)>::value, true);
87     CheckNear((i + j).seconds(), k.seconds());
88     CheckNear((k - j).seconds(), i.seconds());
89     CheckNear(((k - j) + (j - i)).seconds(), k.seconds() - i.seconds());
90     interval_type sum;
91     sum += i;
92     sum += j;
93     CheckNear(sum.seconds(), k.seconds());
94     sum -= i;
95     CheckNear(sum.seconds(), j.seconds());
96     sum -= j;
97     CheckNear(sum.seconds(), 0.0);
98 }
99 
100 
101 //! Test resolution of oneapi::tbb::tick_count::interval_t
102 //! \brief \ref interface \ref requirement
103 TEST_CASE("oneapi::tbb::tick_count::interval_t resolution") {
104     static double target_value = 0.314159265358979323846264338327950288419;
105     static double step_value = 0.00027182818284590452353602874713526624977572;
106     static int range_value = 100;
107     for (int i = -range_value; i <= range_value; ++i) {
108         double my_time = target_value + step_value * i;
109         oneapi::tbb::tick_count::interval_t t0(my_time);
110         double interval_time = t0.seconds();
111         //! time always truncates
112         CHECK_GE(interval_time, 0);
113         CHECK_LT(my_time - interval_time, oneapi::tbb::tick_count::resolution());
114     }
115 }
116