1 /******************************************************************************
2 *
3 * Copyright 2015 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include <gtest/gtest.h>
20
21 #include "common/time_util.h"
22
23 // Generous upper bound: 10 seconds
24 static const uint32_t TEST_TIME_DELTA_UPPER_BOUND_MS = 10 * 1000;
25
26 //
27 // Test that the return value of bluetooth::common::time_get_os_boottime_ms() is
28 // not zero.
29 //
TEST(TimeTest,test_time_get_os_boottime_ms_not_zero)30 TEST(TimeTest, test_time_get_os_boottime_ms_not_zero) {
31 uint64_t t1 = bluetooth::common::time_get_os_boottime_ms();
32 ASSERT_GT(t1, uint64_t(0));
33 }
34
35 //
36 // Test that the return value of bluetooth::common::time_get_os_boottime_us() is
37 // not zero.
38 //
TEST(TimeTest,test_time_get_os_boottime_us_not_zero)39 TEST(TimeTest, test_time_get_os_boottime_us_not_zero) {
40 uint64_t t1 = bluetooth::common::time_get_os_boottime_us();
41 ASSERT_GT(t1, uint64_t(0));
42 }
43
44 //
45 // Test that the return value of
46 // bluetooth::common::time_get_os_monotonic_raw_us() is not zero.
47 //
TEST(TimeTest,test_time_get_os_monotonic_raw_us_not_zero)48 TEST(TimeTest, test_time_get_os_monotonic_raw_us_not_zero) {
49 uint64_t t1 = bluetooth::common::time_get_os_monotonic_raw_us();
50 ASSERT_GT(t1, uint64_t(0));
51 }
52
53 //
54 // Test that the return value of bluetooth::common::time_get_os_boottime_ms()
55 // is monotonically increasing within reasonable boundaries.
56 //
TEST(TimeTest,test_time_get_os_boottime_ms_increases_upper_bound)57 TEST(TimeTest, test_time_get_os_boottime_ms_increases_upper_bound) {
58 uint64_t t1 = bluetooth::common::time_get_os_boottime_ms();
59 uint64_t t2 = bluetooth::common::time_get_os_boottime_ms();
60 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS);
61 }
62
63 //
64 // Test that the return value of bluetooth::common::time_get_os_boottime_us()
65 // is monotonically increasing within reasonable boundaries.
66 //
TEST(TimeTest,test_time_get_os_boottime_us_increases_upper_bound)67 TEST(TimeTest, test_time_get_os_boottime_us_increases_upper_bound) {
68 uint64_t t1 = bluetooth::common::time_get_os_boottime_us();
69 uint64_t t2 = bluetooth::common::time_get_os_boottime_us();
70 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000);
71 }
72
73 //
74 // Test that the return value of
75 // bluetooth::common::time_get_os_monotonic_raw_us() is monotonically increasing
76 // within reasonable boundaries.
77 //
TEST(TimeTest,test_time_get_os_monotonic_raw_time_us_increases_upper_bound)78 TEST(TimeTest, test_time_get_os_monotonic_raw_time_us_increases_upper_bound) {
79 uint64_t t1 = bluetooth::common::time_get_os_monotonic_raw_us();
80 uint64_t t2 = bluetooth::common::time_get_os_monotonic_raw_us();
81 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000);
82 }
83
84 //
85 // Test that the return value of bluetooth::common::time_get_os_boottime_ms()
86 // is increasing.
87 //
TEST(TimeTest,test_time_get_os_boottime_ms_increases_lower_bound)88 TEST(TimeTest, test_time_get_os_boottime_ms_increases_lower_bound) {
89 static const uint32_t TEST_TIME_SLEEP_MS = 100;
90 struct timespec delay = {};
91
92 delay.tv_sec = TEST_TIME_SLEEP_MS / 1000;
93 delay.tv_nsec = 1000 * 1000 * (TEST_TIME_SLEEP_MS % 1000);
94
95 // Take two timestamps with sleep in-between
96 uint64_t t1 = bluetooth::common::time_get_os_boottime_ms();
97 int err = nanosleep(&delay, &delay);
98 uint64_t t2 = bluetooth::common::time_get_os_boottime_ms();
99
100 ASSERT_EQ(err, 0);
101 ASSERT_TRUE((t2 - t1) >= TEST_TIME_SLEEP_MS);
102 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS);
103 }
104
105 //
106 // Test that the return value of bluetooth::common::time_get_os_boottime_us()
107 // is increasing.
108 //
TEST(TimeTest,test_time_get_os_boottime_us_increases_lower_bound)109 TEST(TimeTest, test_time_get_os_boottime_us_increases_lower_bound) {
110 static const uint64_t TEST_TIME_SLEEP_US = 100 * 1000;
111 struct timespec delay = {};
112
113 delay.tv_sec = TEST_TIME_SLEEP_US / (1000 * 1000);
114 delay.tv_nsec = 1000 * (TEST_TIME_SLEEP_US % (1000 * 1000));
115
116 // Take two timestamps with sleep in-between
117 uint64_t t1 = bluetooth::common::time_get_os_boottime_us();
118 int err = nanosleep(&delay, &delay);
119 uint64_t t2 = bluetooth::common::time_get_os_boottime_us();
120
121 ASSERT_EQ(err, 0);
122 ASSERT_GT(t2, t1);
123 ASSERT_TRUE((t2 - t1) >= TEST_TIME_SLEEP_US);
124 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000);
125 }
126
127 //
128 // Test that the return value of bluetooth::common::time_gettimeofday_us() is
129 // not zero.
130 //
TEST(TimeTest,test_time_gettimeofday_us_not_zero)131 TEST(TimeTest, test_time_gettimeofday_us_not_zero) {
132 uint64_t t1 = bluetooth::common::time_gettimeofday_us();
133 ASSERT_GT(t1, uint64_t(0));
134 }
135
136 //
137 // Test that the return value of bluetooth::common::time_gettimeofday_us()
138 // is monotonically increasing within reasonable boundaries.
139 //
TEST(TimeTest,test_time_gettimeofday_us_increases_upper_bound)140 TEST(TimeTest, test_time_gettimeofday_us_increases_upper_bound) {
141 uint64_t t1 = bluetooth::common::time_gettimeofday_us();
142 uint64_t t2 = bluetooth::common::time_gettimeofday_us();
143 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000);
144 }
145
146 //
147 // Test that the return value of bluetooth::common::time_gettimeofday_us()
148 // is increasing.
149 //
TEST(TimeTest,test_time_gettimeofday_us_increases_lower_bound)150 TEST(TimeTest, test_time_gettimeofday_us_increases_lower_bound) {
151 static const uint64_t TEST_TIME_SLEEP_US = 100 * 1000;
152 struct timespec delay = {};
153
154 delay.tv_sec = TEST_TIME_SLEEP_US / (1000 * 1000);
155 delay.tv_nsec = 1000 * (TEST_TIME_SLEEP_US % (1000 * 1000));
156
157 // Take two timestamps with sleep in-between
158 uint64_t t1 = bluetooth::common::time_gettimeofday_us();
159 int err = nanosleep(&delay, &delay);
160 uint64_t t2 = bluetooth::common::time_gettimeofday_us();
161
162 ASSERT_EQ(err, 0);
163 ASSERT_GT(t2, t1);
164 ASSERT_TRUE((t2 - t1) >= TEST_TIME_SLEEP_US);
165 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000);
166 }
167