Fix test failures introduced by PR #113697 (#116941)
[llvm-project.git] / llvm / unittests / Support / TimerTest.cpp
blob5686b394e16cddf56b6c388772a5bc4ac5177cab
1 //===- unittests/TimerTest.cpp - Timer tests ------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Timer.h"
10 #include "gtest/gtest.h"
12 #if _WIN32
13 #include <windows.h>
14 #else
15 #include <time.h>
16 #endif
18 using namespace llvm;
20 namespace {
22 // FIXME: Put this somewhere in Support, it's also used in LockFileManager.
23 void SleepMS() {
24 #if _WIN32
25 Sleep(1);
26 #else
27 struct timespec Interval;
28 Interval.tv_sec = 0;
29 Interval.tv_nsec = 1000000;
30 #if defined(__MVS__)
31 long Microseconds = (Interval.tv_nsec + 999) / 1000;
32 usleep(Microseconds);
33 #else
34 nanosleep(&Interval, nullptr);
35 #endif
36 #endif
39 TEST(Timer, Additivity) {
40 Timer T1("T1", "T1");
42 EXPECT_TRUE(T1.isInitialized());
44 T1.startTimer();
45 T1.stopTimer();
46 auto TR1 = T1.getTotalTime();
48 T1.startTimer();
49 SleepMS();
50 T1.stopTimer();
51 auto TR2 = T1.getTotalTime();
53 EXPECT_LT(TR1, TR2);
56 TEST(Timer, CheckIfTriggered) {
57 Timer T1("T1", "T1");
59 EXPECT_FALSE(T1.hasTriggered());
60 T1.startTimer();
61 EXPECT_TRUE(T1.hasTriggered());
62 T1.stopTimer();
63 EXPECT_TRUE(T1.hasTriggered());
65 T1.clear();
66 EXPECT_FALSE(T1.hasTriggered());
69 } // end anon namespace