Merge "Increase coverage of Timers to 100%." am: 2dc28bcfc4 am: 1db66002c1 am: b9a7a9886f am: 72e2e0c164

Original change: https://android-review.googlesource.com/c/platform/system/core/+/1710936

Change-Id: Ib4120727615f6b8e9d4ea963cd6e3d71e81d47be
This commit is contained in:
Elliott Hughes 2021-05-20 21:07:05 +00:00 committed by Automerger Merge Worker
commit 3b9238cf0f
2 changed files with 17 additions and 18 deletions

View file

@ -14,9 +14,6 @@
* limitations under the License.
*/
//
// Timer functions.
//
#include <utils/Timers.h>
#include <limits.h>
@ -24,11 +21,12 @@
#include <time.h>
#include <android-base/macros.h>
#include <utils/Log.h>
static constexpr size_t clock_id_max = 5;
static void checkClockId(int clock) {
if (clock < 0 || clock >= clock_id_max) abort();
LOG_ALWAYS_FATAL_IF(clock < 0 || clock >= clock_id_max, "invalid clock id");
}
#if defined(__linux__)
@ -56,18 +54,10 @@ nsecs_t systemTime(int clock) {
}
#endif
int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime)
{
nsecs_t timeoutDelayMillis;
if (timeoutTime > referenceTime) {
uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime);
if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) {
timeoutDelayMillis = -1;
} else {
timeoutDelayMillis = (timeoutDelay + 999999LL) / 1000000LL;
}
} else {
timeoutDelayMillis = 0;
}
return (int)timeoutDelayMillis;
int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime) {
if (timeoutTime <= referenceTime) return 0;
uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime);
if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) return -1;
return (timeoutDelay + 999999LL) / 1000000LL;
}

View file

@ -27,3 +27,12 @@ TEST(Timers, systemTime_invalid) {
systemTime(SYSTEM_TIME_BOOTTIME);
EXPECT_EXIT(systemTime(SYSTEM_TIME_BOOTTIME + 1), testing::KilledBySignal(SIGABRT), "");
}
TEST(Timers, toMillisecondTimeoutDelay) {
EXPECT_EQ(0, toMillisecondTimeoutDelay(100, 100));
EXPECT_EQ(0, toMillisecondTimeoutDelay(100, 10));
EXPECT_EQ(-1, toMillisecondTimeoutDelay(0, INT_MAX * 1000000LL));
EXPECT_EQ(123, toMillisecondTimeoutDelay(0, 123000000));
}