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 bluetooth::common::time_get_os_boottime_ms()
46 // is monotonically increasing within reasonable boundries.
47 //
TEST(TimeTest,test_time_get_os_boottime_ms_increases_upper_bound)48 TEST(TimeTest, test_time_get_os_boottime_ms_increases_upper_bound) {
49 uint64_t t1 = bluetooth::common::time_get_os_boottime_ms();
50 uint64_t t2 = bluetooth::common::time_get_os_boottime_ms();
51 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS);
52 }
53
54 //
55 // Test that the return value of bluetooth::common::time_get_os_boottime_us()
56 // is monotonically increasing within reasonable boundries.
57 //
TEST(TimeTest,test_time_get_os_boottime_us_increases_upper_bound)58 TEST(TimeTest, test_time_get_os_boottime_us_increases_upper_bound) {
59 uint64_t t1 = bluetooth::common::time_get_os_boottime_us();
60 uint64_t t2 = bluetooth::common::time_get_os_boottime_us();
61 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000);
62 }
63
64 //
65 // Test that the return value of bluetooth::common::time_get_os_boottime_ms()
66 // is increasing.
67 //
TEST(TimeTest,test_time_get_os_boottime_ms_increases_lower_bound)68 TEST(TimeTest, test_time_get_os_boottime_ms_increases_lower_bound) {
69 static const uint32_t TEST_TIME_SLEEP_MS = 100;
70 struct timespec delay = {};
71
72 delay.tv_sec = TEST_TIME_SLEEP_MS / 1000;
73 delay.tv_nsec = 1000 * 1000 * (TEST_TIME_SLEEP_MS % 1000);
74
75 // Take two timestamps with sleep in-between
76 uint64_t t1 = bluetooth::common::time_get_os_boottime_ms();
77 int err = nanosleep(&delay, &delay);
78 uint64_t t2 = bluetooth::common::time_get_os_boottime_ms();
79
80 ASSERT_EQ(err, 0);
81 ASSERT_TRUE((t2 - t1) >= TEST_TIME_SLEEP_MS);
82 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS);
83 }
84
85 //
86 // Test that the return value of bluetooth::common::time_get_os_boottime_us()
87 // is increasing.
88 //
TEST(TimeTest,test_time_get_os_boottime_us_increases_lower_bound)89 TEST(TimeTest, test_time_get_os_boottime_us_increases_lower_bound) {
90 static const uint64_t TEST_TIME_SLEEP_US = 100 * 1000;
91 struct timespec delay = {};
92
93 delay.tv_sec = TEST_TIME_SLEEP_US / (1000 * 1000);
94 delay.tv_nsec = 1000 * (TEST_TIME_SLEEP_US % (1000 * 1000));
95
96 // Take two timestamps with sleep in-between
97 uint64_t t1 = bluetooth::common::time_get_os_boottime_us();
98 int err = nanosleep(&delay, &delay);
99 uint64_t t2 = bluetooth::common::time_get_os_boottime_us();
100
101 ASSERT_EQ(err, 0);
102 ASSERT_GT(t2, t1);
103 ASSERT_TRUE((t2 - t1) >= TEST_TIME_SLEEP_US);
104 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000);
105 }
106
107 //
108 // Test that the return value of bluetooth::common::time_gettimeofday_us() is
109 // not zero.
110 //
TEST(TimeTest,test_time_gettimeofday_us_not_zero)111 TEST(TimeTest, test_time_gettimeofday_us_not_zero) {
112 uint64_t t1 = bluetooth::common::time_gettimeofday_us();
113 ASSERT_GT(t1, uint64_t(0));
114 }
115
116 //
117 // Test that the return value of bluetooth::common::time_gettimeofday_us()
118 // is monotonically increasing within reasonable boundaries.
119 //
TEST(TimeTest,test_time_gettimeofday_us_increases_upper_bound)120 TEST(TimeTest, test_time_gettimeofday_us_increases_upper_bound) {
121 uint64_t t1 = bluetooth::common::time_gettimeofday_us();
122 uint64_t t2 = bluetooth::common::time_gettimeofday_us();
123 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000);
124 }
125
126 //
127 // Test that the return value of bluetooth::common::time_gettimeofday_us()
128 // is increasing.
129 //
TEST(TimeTest,test_time_gettimeofday_us_increases_lower_bound)130 TEST(TimeTest, test_time_gettimeofday_us_increases_lower_bound) {
131 static const uint64_t TEST_TIME_SLEEP_US = 100 * 1000;
132 struct timespec delay = {};
133
134 delay.tv_sec = TEST_TIME_SLEEP_US / (1000 * 1000);
135 delay.tv_nsec = 1000 * (TEST_TIME_SLEEP_US % (1000 * 1000));
136
137 // Take two timestamps with sleep in-between
138 uint64_t t1 = bluetooth::common::time_gettimeofday_us();
139 int err = nanosleep(&delay, &delay);
140 uint64_t t2 = bluetooth::common::time_gettimeofday_us();
141
142 ASSERT_EQ(err, 0);
143 ASSERT_GT(t2, t1);
144 ASSERT_TRUE((t2 - t1) >= TEST_TIME_SLEEP_US);
145 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000);
146 }
147