[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / Support / TimerTest.cpp
blob09545eb6939aebf8b716c30427aa323d2191d176
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 nanosleep(&Interval, nullptr);
31 #endif
34 TEST(Timer, Additivity) {
35 Timer T1("T1", "T1");
37 EXPECT_TRUE(T1.isInitialized());
39 T1.startTimer();
40 T1.stopTimer();
41 auto TR1 = T1.getTotalTime();
43 T1.startTimer();
44 SleepMS();
45 T1.stopTimer();
46 auto TR2 = T1.getTotalTime();
48 EXPECT_LT(TR1, TR2);
51 TEST(Timer, CheckIfTriggered) {
52 Timer T1("T1", "T1");
54 EXPECT_FALSE(T1.hasTriggered());
55 T1.startTimer();
56 EXPECT_TRUE(T1.hasTriggered());
57 T1.stopTimer();
58 EXPECT_TRUE(T1.hasTriggered());
60 T1.clear();
61 EXPECT_FALSE(T1.hasTriggered());
64 } // end anon namespace