1//===-- Unittests for timespec_get ----------------------------------------===//
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 "hdr/time_macros.h"
10#include "hdr/types/struct_timespec.h"
11#include "src/__support/macros/properties/architectures.h"
12#include "src/time/timespec_get.h"
13#include "test/UnitTest/Test.h"
14
15TEST(LlvmLibcTimespecGet, Utc) {
16 timespec ts;
17 int result;
18 result = LIBC_NAMESPACE::timespec_get(&ts, TIME_UTC);
19#ifdef LIBC_TARGET_ARCH_IS_GPU
20 ASSERT_EQ(result, 0);
21#else
22 ASSERT_EQ(result, TIME_UTC);
23 ASSERT_GT(ts.tv_sec, time_t(0));
24#endif
25}
26
27TEST(LlvmLibcTimespecGet, Monotonic) {
28 timespec ts1, ts2;
29 int result;
30 result = LIBC_NAMESPACE::timespec_get(&ts1, TIME_MONOTONIC);
31 ASSERT_EQ(result, TIME_MONOTONIC);
32 ASSERT_GT(ts1.tv_sec, time_t(0));
33 result = LIBC_NAMESPACE::timespec_get(&ts2, TIME_MONOTONIC);
34 ASSERT_EQ(result, TIME_MONOTONIC);
35 ASSERT_GE(ts2.tv_sec, ts1.tv_sec); // The monotonic time should increase.
36 if (ts2.tv_sec == ts1.tv_sec) {
37 ASSERT_GE(ts2.tv_nsec, ts1.tv_nsec);
38 }
39}
40
41TEST(LlvmLibcTimespecGet, Unknown) {
42 timespec ts;
43 int result;
44 result = LIBC_NAMESPACE::timespec_get(&ts, 0);
45 ASSERT_EQ(result, 0);
46}
47

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