llvm/flang/unittests/RuntimeGTest/Time.cpp
Diana Picus 57e85622bb [flang] Add initial implementation for CPU_TIME
Add an implementation for CPU_TIME based on std::clock(), which should
be available on all the platforms that we support.

Also add a test that's basically just a sanity check to make sure we
return positive values and that the value returned at the start of some
amount of work is larger than the one returned after the end.

Differential Revision: https://reviews.llvm.org/D104019
2021-06-14 07:48:09 +00:00

36 lines
1.1 KiB
C++

//===-- 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"
#include "../../runtime/time-intrinsic.h"
using namespace Fortran::runtime;
volatile int x = 0;
void LookBusy() {
// We're trying to track actual processor time, so sleeping is not an option.
// Doing some writes to a volatile variable should do the trick.
for (int i = 0; i < (1 << 8); ++i) {
x = i;
}
}
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.
double start = RTNAME(CpuTime)();
LookBusy();
double end = RTNAME(CpuTime)();
ASSERT_GE(start, 0.0);
ASSERT_GT(end, 0.0);
ASSERT_GT(end, start);
}