2021-06-09 16:19:43 +08:00
|
|
|
//===-- flang/unittests/RuntimeGTest/Time.cpp -----------------------===//
|
|
|
|
//
|
|
|
|
// 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 "gtest/gtest.h"
|
2021-09-02 07:00:53 +08:00
|
|
|
#include "flang/Runtime/time-intrinsic.h"
|
2021-08-25 17:15:22 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
#include <charconv>
|
|
|
|
#include <string>
|
2021-06-09 16:19:43 +08:00
|
|
|
|
|
|
|
using namespace Fortran::runtime;
|
|
|
|
|
|
|
|
TEST(TimeIntrinsics, CpuTime) {
|
|
|
|
// We can't really test that we get the "right" result for CPU_TIME, but we
|
|
|
|
// can have a smoke test to see that we get something reasonable on the
|
|
|
|
// platforms where we expect to support it.
|
2021-07-27 18:21:10 +08:00
|
|
|
double start{RTNAME(CpuTime)()};
|
2021-06-09 16:19:43 +08:00
|
|
|
ASSERT_GE(start, 0.0);
|
2021-06-18 17:04:01 +08:00
|
|
|
|
|
|
|
// Loop until we get a different value from CpuTime. If we don't get one
|
|
|
|
// before we time out, then we should probably look into an implementation
|
|
|
|
// for CpuTime with a better timer resolution.
|
|
|
|
for (double end = start; end == start; end = RTNAME(CpuTime)()) {
|
2021-07-12 22:04:34 +08:00
|
|
|
ASSERT_GE(end, 0.0);
|
2021-06-18 17:04:01 +08:00
|
|
|
ASSERT_GE(end, start);
|
|
|
|
}
|
2021-06-09 16:19:43 +08:00
|
|
|
}
|
2021-07-12 21:45:44 +08:00
|
|
|
|
2021-10-07 05:03:48 +08:00
|
|
|
using count_t = std::int64_t;
|
2021-07-12 21:45:44 +08:00
|
|
|
|
|
|
|
TEST(TimeIntrinsics, SystemClock) {
|
|
|
|
// We can't really test that we get the "right" result for SYSTEM_CLOCK, but
|
|
|
|
// we can have a smoke test to see that we get something reasonable on the
|
|
|
|
// platforms where we expect to support it.
|
|
|
|
|
|
|
|
// The value of the count rate and max will vary by platform, but they should
|
|
|
|
// always be strictly positive if we have a working implementation of
|
|
|
|
// SYSTEM_CLOCK.
|
|
|
|
EXPECT_GT(RTNAME(SystemClockCountRate)(), 0);
|
|
|
|
|
2021-10-07 05:03:48 +08:00
|
|
|
count_t max1{RTNAME(SystemClockCountMax)(1)};
|
|
|
|
EXPECT_GT(max1, 0);
|
|
|
|
EXPECT_LE(max1, static_cast<count_t>(0x7f));
|
|
|
|
count_t start1{RTNAME(SystemClockCount)(1)};
|
|
|
|
EXPECT_GE(start1, 0);
|
|
|
|
EXPECT_LE(start1, max1);
|
2021-07-12 21:45:44 +08:00
|
|
|
|
2021-10-07 05:03:48 +08:00
|
|
|
count_t max2{RTNAME(SystemClockCountMax)(2)};
|
|
|
|
EXPECT_GT(max2, 0);
|
|
|
|
EXPECT_LE(max2, static_cast<count_t>(0x7fff));
|
|
|
|
count_t start2{RTNAME(SystemClockCount)(2)};
|
|
|
|
EXPECT_GE(start2, 0);
|
|
|
|
EXPECT_LE(start2, max2);
|
|
|
|
|
|
|
|
count_t max4{RTNAME(SystemClockCountMax)(4)};
|
|
|
|
EXPECT_GT(max4, 0);
|
|
|
|
EXPECT_LE(max4, static_cast<count_t>(0x7fffffff));
|
|
|
|
count_t start4{RTNAME(SystemClockCount)(4)};
|
|
|
|
EXPECT_GE(start4, 0);
|
|
|
|
EXPECT_LE(start4, max4);
|
|
|
|
|
|
|
|
count_t max8{RTNAME(SystemClockCountMax)(8)};
|
|
|
|
EXPECT_GT(max8, 0);
|
|
|
|
count_t start8{RTNAME(SystemClockCount)(8)};
|
|
|
|
EXPECT_GE(start8, 0);
|
|
|
|
EXPECT_LT(start8, max8);
|
|
|
|
|
|
|
|
count_t max16{RTNAME(SystemClockCountMax)(16)};
|
|
|
|
EXPECT_GT(max16, 0);
|
|
|
|
count_t start16{RTNAME(SystemClockCount)(16)};
|
|
|
|
EXPECT_GE(start16, 0);
|
|
|
|
EXPECT_LT(start16, max16);
|
2021-07-12 21:45:44 +08:00
|
|
|
|
|
|
|
// Loop until we get a different value from SystemClockCount. If we don't get
|
|
|
|
// one before we time out, then we should probably look into an implementation
|
|
|
|
// for SystemClokcCount with a better timer resolution on this platform.
|
2021-10-07 05:03:48 +08:00
|
|
|
for (count_t end{start8}; end == start8; end = RTNAME(SystemClockCount)(8)) {
|
2021-07-12 21:45:44 +08:00
|
|
|
EXPECT_GE(end, 0);
|
2021-10-07 05:03:48 +08:00
|
|
|
EXPECT_LE(end, max8);
|
|
|
|
EXPECT_GE(end, start8);
|
2021-07-12 21:45:44 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-25 17:15:22 +08:00
|
|
|
|
|
|
|
TEST(TimeIntrinsics, DateAndTime) {
|
|
|
|
constexpr std::size_t bufferSize{16};
|
|
|
|
std::string date(bufferSize, 'Z'), time(bufferSize, 'Z'),
|
|
|
|
zone(bufferSize, 'Z');
|
|
|
|
RTNAME(DateAndTime)
|
|
|
|
(date.data(), date.size(), time.data(), time.size(), zone.data(), zone.size(),
|
|
|
|
/*source=*/nullptr, /*line=*/0, /*values=*/nullptr);
|
|
|
|
auto isBlank = [](const std::string &s) -> bool {
|
|
|
|
return std::all_of(
|
|
|
|
s.begin(), s.end(), [](char c) { return std::isblank(c); });
|
|
|
|
};
|
|
|
|
// Validate date is blank or YYYYMMDD.
|
|
|
|
if (isBlank(date)) {
|
|
|
|
EXPECT_TRUE(true);
|
|
|
|
} else {
|
|
|
|
count_t number{-1};
|
|
|
|
auto [_, ec]{
|
|
|
|
std::from_chars(date.data(), date.data() + date.size(), number)};
|
|
|
|
ASSERT_TRUE(ec != std::errc::invalid_argument &&
|
|
|
|
ec != std::errc::result_out_of_range);
|
|
|
|
EXPECT_GE(number, 0);
|
|
|
|
auto year = number / 10000;
|
|
|
|
auto month = (number - year * 10000) / 100;
|
|
|
|
auto day = number % 100;
|
|
|
|
// Do not assume anything about the year, the test could be
|
|
|
|
// run on system with fake/outdated dates.
|
|
|
|
EXPECT_LE(month, 12);
|
|
|
|
EXPECT_GT(month, 0);
|
|
|
|
EXPECT_LE(day, 31);
|
|
|
|
EXPECT_GT(day, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate time is hhmmss.sss or blank.
|
|
|
|
if (isBlank(time)) {
|
|
|
|
EXPECT_TRUE(true);
|
|
|
|
} else {
|
|
|
|
count_t number{-1};
|
|
|
|
auto [next, ec]{
|
|
|
|
std::from_chars(time.data(), time.data() + date.size(), number)};
|
|
|
|
ASSERT_TRUE(ec != std::errc::invalid_argument &&
|
|
|
|
ec != std::errc::result_out_of_range);
|
|
|
|
ASSERT_GE(number, 0);
|
|
|
|
auto hours = number / 10000;
|
|
|
|
auto minutes = (number - hours * 10000) / 100;
|
|
|
|
auto seconds = number % 100;
|
|
|
|
EXPECT_LE(hours, 23);
|
|
|
|
EXPECT_LE(minutes, 59);
|
|
|
|
// Accept 60 for leap seconds.
|
|
|
|
EXPECT_LE(seconds, 60);
|
|
|
|
ASSERT_TRUE(next != time.data() + time.size());
|
|
|
|
EXPECT_EQ(*next, '.');
|
|
|
|
|
|
|
|
count_t milliseconds{-1};
|
|
|
|
ASSERT_TRUE(next + 1 != time.data() + time.size());
|
|
|
|
auto [_, ec2]{
|
|
|
|
std::from_chars(next + 1, time.data() + date.size(), milliseconds)};
|
|
|
|
ASSERT_TRUE(ec2 != std::errc::invalid_argument &&
|
|
|
|
ec2 != std::errc::result_out_of_range);
|
|
|
|
EXPECT_GE(milliseconds, 0);
|
|
|
|
EXPECT_LE(milliseconds, 999);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate zone is +hhmm or -hhmm or blank.
|
|
|
|
if (isBlank(zone)) {
|
|
|
|
EXPECT_TRUE(true);
|
|
|
|
} else {
|
|
|
|
ASSERT_TRUE(zone.size() > 1);
|
|
|
|
EXPECT_TRUE(zone[0] == '+' || zone[0] == '-');
|
|
|
|
count_t number{-1};
|
|
|
|
auto [next, ec]{
|
|
|
|
std::from_chars(zone.data() + 1, zone.data() + zone.size(), number)};
|
|
|
|
ASSERT_TRUE(ec != std::errc::invalid_argument &&
|
|
|
|
ec != std::errc::result_out_of_range);
|
|
|
|
ASSERT_GE(number, 0);
|
|
|
|
auto hours = number / 100;
|
|
|
|
auto minutes = number % 100;
|
|
|
|
EXPECT_LE(hours, 23);
|
|
|
|
EXPECT_LE(minutes, 59);
|
|
|
|
}
|
|
|
|
}
|