aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/time/clock_getres_test.cpp
blob: d8b3f01b37a38a160344d5112fc024ed2fcdd097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//===-- Unittests for clock_getres- ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "hdr/time_macros.h"
#include "src/time/clock_getres.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;

TEST(LlvmLibcClockGetRes, Invalid) {
  timespec tp;
  EXPECT_THAT(LIBC_NAMESPACE::clock_getres(-1, &tp), Fails(EINVAL));
}

TEST(LlvmLibcClockGetRes, NullSpec) {
  EXPECT_THAT(LIBC_NAMESPACE::clock_getres(CLOCK_REALTIME, nullptr),
              Succeeds());
}

TEST(LlvmLibcClockGetRes, Realtime) {
  timespec tp;
  EXPECT_THAT(LIBC_NAMESPACE::clock_getres(CLOCK_REALTIME, &tp), Succeeds());
  EXPECT_GE(tp.tv_sec, static_cast<decltype(tp.tv_sec)>(0));
  EXPECT_GE(tp.tv_nsec, static_cast<decltype(tp.tv_nsec)>(0));
}

TEST(LlvmLibcClockGetRes, Monotonic) {
  timespec tp;
  ASSERT_THAT(LIBC_NAMESPACE::clock_getres(CLOCK_MONOTONIC, &tp), Succeeds());
  EXPECT_GE(tp.tv_sec, static_cast<decltype(tp.tv_sec)>(0));
  EXPECT_GE(tp.tv_nsec, static_cast<decltype(tp.tv_nsec)>(0));
}

TEST(LlvmLibcClockGetRes, ProcessCpuTime) {
  timespec tp;
  ASSERT_THAT(LIBC_NAMESPACE::clock_getres(CLOCK_PROCESS_CPUTIME_ID, &tp),
              Succeeds());
  EXPECT_GE(tp.tv_sec, static_cast<decltype(tp.tv_sec)>(0));
  EXPECT_GE(tp.tv_nsec, static_cast<decltype(tp.tv_nsec)>(0));
}

TEST(LlvmLibcClockGetRes, ThreadCpuTime) {
  timespec tp;
  ASSERT_THAT(LIBC_NAMESPACE::clock_getres(CLOCK_THREAD_CPUTIME_ID, &tp),
              Succeeds());
  EXPECT_GE(tp.tv_sec, static_cast<decltype(tp.tv_sec)>(0));
  EXPECT_GE(tp.tv_nsec, static_cast<decltype(tp.tv_nsec)>(0));
}