1 //===-- flang/unittests/RuntimeGTest/Time.cpp -----------------------===//
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 "gtest/gtest.h"
10 #include "../../runtime/time-intrinsic.h"
11 
12 using namespace Fortran::runtime;
13 
14 TEST(TimeIntrinsics, CpuTime) {
15   // We can't really test that we get the "right" result for CPU_TIME, but we
16   // can have a smoke test to see that we get something reasonable on the
17   // platforms where we expect to support it.
18   double start{RTNAME(CpuTime)()};
19   ASSERT_GE(start, 0.0);
20 
21   // Loop until we get a different value from CpuTime. If we don't get one
22   // before we time out, then we should probably look into an implementation
23   // for CpuTime with a better timer resolution.
24   for (double end = start; end == start; end = RTNAME(CpuTime)()) {
25     ASSERT_GE(end, 0.0);
26     ASSERT_GE(end, start);
27   }
28 }
29 
30 using count_t = CppTypeFor<TypeCategory::Integer, 8>;
31 
32 TEST(TimeIntrinsics, SystemClock) {
33   // We can't really test that we get the "right" result for SYSTEM_CLOCK, but
34   // we can have a smoke test to see that we get something reasonable on the
35   // platforms where we expect to support it.
36 
37   // The value of the count rate and max will vary by platform, but they should
38   // always be strictly positive if we have a working implementation of
39   // SYSTEM_CLOCK.
40   EXPECT_GT(RTNAME(SystemClockCountRate)(), 0);
41 
42   count_t max{RTNAME(SystemClockCountMax)()};
43   EXPECT_GT(max, 0);
44 
45   count_t start{RTNAME(SystemClockCount)()};
46   EXPECT_GE(start, 0);
47   EXPECT_LE(start, max);
48 
49   // Loop until we get a different value from SystemClockCount. If we don't get
50   // one before we time out, then we should probably look into an implementation
51   // for SystemClokcCount with a better timer resolution on this platform.
52   for (count_t end = start; end == start; end = RTNAME(SystemClockCount)()) {
53     EXPECT_GE(end, 0);
54     EXPECT_LE(end, max);
55 
56     EXPECT_GE(end, start);
57   }
58 }
59