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 "AllocationTestHarness.h" 22 23 #include "osi/include/time.h" 24 25 // Generous upper bound: 10 seconds 26 static const uint32_t TEST_TIME_DELTA_UPPER_BOUND_MS = 10 * 1000; 27 28 class TimeTest : public AllocationTestHarness {}; 29 30 // 31 // Test that the return value of time_get_os_boottime_ms() is not zero. 32 // 33 // NOTE: For now this test is disabled, because the return value 34 // of time_get_os_boottime_ms() is 32-bits integer that could wrap-around 35 // in 49.7 days. It should be re-enabled if/after the wrap-around issue 36 // is resolved (e.g., if the return value is 64-bits integer). 37 // 38 #if 0 39 TEST_F(TimeTest, test_time_get_os_boottime_ms_not_zero) { 40 uint32_t t1 = time_get_os_boottime_ms(); 41 ASSERT_TRUE(t1 > 0); 42 } 43 #endif 44 45 // 46 // Test that the return value of time_get_os_boottime_us() is not zero. 47 // 48 TEST_F(TimeTest, test_time_get_os_boottime_us_not_zero) { 49 uint64_t t1 = time_get_os_boottime_us(); 50 ASSERT_TRUE(t1 > 0); 51 } 52 53 // 54 // Test that the return value of time_get_os_boottime_ms() 55 // is monotonically increasing within reasonable boundries. 56 // 57 TEST_F(TimeTest, test_time_get_os_boottime_ms_increases_upper_bound) { 58 uint32_t t1 = time_get_os_boottime_ms(); 59 uint32_t t2 = 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 time_get_os_boottime_us() 65 // is monotonically increasing within reasonable boundries. 66 // 67 TEST_F(TimeTest, test_time_get_os_boottime_us_increases_upper_bound) { 68 uint64_t t1 = time_get_os_boottime_us(); 69 uint64_t t2 = 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 time_get_os_boottime_ms() 75 // is increasing. 76 // 77 TEST_F(TimeTest, test_time_get_os_boottime_ms_increases_lower_bound) { 78 static const uint32_t TEST_TIME_SLEEP_MS = 100; 79 struct timespec delay; 80 81 delay.tv_sec = TEST_TIME_SLEEP_MS / 1000; 82 delay.tv_nsec = 1000 * 1000 * (TEST_TIME_SLEEP_MS % 1000); 83 84 // Take two timestamps with sleep in-between 85 uint32_t t1 = time_get_os_boottime_ms(); 86 int err = nanosleep(&delay, &delay); 87 uint32_t t2 = time_get_os_boottime_ms(); 88 89 ASSERT_TRUE(err == 0); 90 ASSERT_TRUE((t2 - t1) >= TEST_TIME_SLEEP_MS); 91 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS); 92 } 93 94 // 95 // Test that the return value of time_get_os_boottime_us() 96 // is increasing. 97 // 98 TEST_F(TimeTest, test_time_get_os_boottime_us_increases_lower_bound) { 99 static const uint64_t TEST_TIME_SLEEP_US = 100 * 1000; 100 struct timespec delay; 101 102 delay.tv_sec = TEST_TIME_SLEEP_US / (1000 * 1000); 103 delay.tv_nsec = 1000 * (TEST_TIME_SLEEP_US % (1000 * 1000)); 104 105 // Take two timestamps with sleep in-between 106 uint64_t t1 = time_get_os_boottime_us(); 107 int err = nanosleep(&delay, &delay); 108 uint64_t t2 = time_get_os_boottime_us(); 109 110 ASSERT_TRUE(err == 0); 111 ASSERT_TRUE(t2 > t1); 112 ASSERT_TRUE((t2 - t1) >= TEST_TIME_SLEEP_US); 113 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000); 114 } 115 116 // 117 // Test that the return value of time_gettimeofday_us() is not zero. 118 // 119 TEST_F(TimeTest, test_time_gettimeofday_us_not_zero) { 120 uint64_t t1 = time_gettimeofday_us(); 121 ASSERT_TRUE(t1 > 0); 122 } 123 124 // 125 // Test that the return value of time_gettimeofday_us() 126 // is monotonically increasing within reasonable boundaries. 127 // 128 TEST_F(TimeTest, test_time_gettimeofday_us_increases_upper_bound) { 129 uint64_t t1 = time_gettimeofday_us(); 130 uint64_t t2 = time_gettimeofday_us(); 131 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000); 132 } 133 134 // 135 // Test that the return value of time_gettimeofday_us() 136 // is increasing. 137 // 138 TEST_F(TimeTest, test_time_gettimeofday_us_increases_lower_bound) { 139 static const uint64_t TEST_TIME_SLEEP_US = 100 * 1000; 140 struct timespec delay; 141 142 delay.tv_sec = TEST_TIME_SLEEP_US / (1000 * 1000); 143 delay.tv_nsec = 1000 * (TEST_TIME_SLEEP_US % (1000 * 1000)); 144 145 // Take two timestamps with sleep in-between 146 uint64_t t1 = time_gettimeofday_us(); 147 int err = nanosleep(&delay, &delay); 148 uint64_t t2 = time_gettimeofday_us(); 149 150 ASSERT_TRUE(err == 0); 151 ASSERT_TRUE(t2 > t1); 152 ASSERT_TRUE((t2 - t1) >= TEST_TIME_SLEEP_US); 153 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS * 1000); 154 } 155