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/time/clock_gettime.h"
10#include "test/UnitTest/Test.h"
11
12#include <time.h>
13
14TEST(LlvmLibcClockGetTime, RealTime) {
15 struct timespec tp;
16 int result;
17 result = LIBC_NAMESPACE::clock_gettime(CLOCK_REALTIME, tp: &tp);
18 ASSERT_EQ(result, 0);
19 ASSERT_GT(tp.tv_sec, time_t(0));
20}
21
22#ifdef CLOCK_MONOTONIC
23TEST(LlvmLibcClockGetTime, MonotonicTime) {
24 struct timespec tp1, tp2;
25 int result;
26 result = LIBC_NAMESPACE::clock_gettime(CLOCK_MONOTONIC, tp: &tp1);
27 ASSERT_EQ(result, 0);
28 ASSERT_GT(tp1.tv_sec, time_t(0));
29 result = LIBC_NAMESPACE::clock_gettime(CLOCK_MONOTONIC, tp: &tp2);
30 ASSERT_EQ(result, 0);
31 ASSERT_GE(tp2.tv_sec, tp1.tv_sec); // The monotonic clock should increase.
32 if (tp2.tv_sec == tp1.tv_sec) {
33 ASSERT_GE(tp2.tv_nsec, tp1.tv_nsec);
34 }
35}
36#endif // CLOCK_MONOTONIC
37

source code of libc/test/src/time/clock_gettime_test.cpp