xref: /aosp_15_r20/external/llvm-libc/test/src/time/clock_gettime_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for clock_gettime ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/__support/macros/properties/architectures.h"
10 #include "src/time/clock_gettime.h"
11 #include "test/UnitTest/Test.h"
12 
13 #include <time.h>
14 
TEST(LlvmLibcClockGetTime,RealTime)15 TEST(LlvmLibcClockGetTime, RealTime) {
16   timespec tp;
17   int result;
18   result = LIBC_NAMESPACE::clock_gettime(CLOCK_REALTIME, &tp);
19   // The GPU does not implement CLOCK_REALTIME but provides it so programs will
20   // compile.
21 #ifdef LIBC_TARGET_ARCH_IS_GPU
22   ASSERT_EQ(result, -1);
23 #else
24   ASSERT_EQ(result, 0);
25   ASSERT_GT(tp.tv_sec, time_t(0));
26 #endif
27 }
28 
29 #ifdef CLOCK_MONOTONIC
TEST(LlvmLibcClockGetTime,MonotonicTime)30 TEST(LlvmLibcClockGetTime, MonotonicTime) {
31   timespec tp1, tp2;
32   int result;
33   result = LIBC_NAMESPACE::clock_gettime(CLOCK_MONOTONIC, &tp1);
34   ASSERT_EQ(result, 0);
35   ASSERT_GT(tp1.tv_sec, time_t(0));
36   result = LIBC_NAMESPACE::clock_gettime(CLOCK_MONOTONIC, &tp2);
37   ASSERT_EQ(result, 0);
38   ASSERT_GE(tp2.tv_sec, tp1.tv_sec); // The monotonic clock should increase.
39   if (tp2.tv_sec == tp1.tv_sec) {
40     ASSERT_GE(tp2.tv_nsec, tp1.tv_nsec);
41   }
42 }
43 #endif // CLOCK_MONOTONIC
44