blob: 082a7e35db52ae7e4d120d92ce2370d4260a9598 [file] [log] [blame]
Vedant Kumard1675862015-12-22 17:36:17 +00001//===- unittests/TimerTest.cpp - Timer tests ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/Timer.h"
11#include "gtest/gtest.h"
Nico Weber07e602e2015-12-23 01:04:53 +000012
13#if LLVM_ON_WIN32
14#include <windows.h>
15#else
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000016#include <time.h>
Nico Weber07e602e2015-12-23 01:04:53 +000017#endif
Vedant Kumard1675862015-12-22 17:36:17 +000018
19using namespace llvm;
20
21namespace {
22
Nico Weber07e602e2015-12-23 01:04:53 +000023// FIXME: Put this somewhere in Support, it's also used in LockFileManager.
24void SleepMS() {
25#if LLVM_ON_WIN32
26 Sleep(1);
27#else
28 struct timespec Interval;
29 Interval.tv_sec = 0;
30 Interval.tv_nsec = 1000000;
31 nanosleep(&Interval, nullptr);
32#endif
33}
34
Vedant Kumard1675862015-12-22 17:36:17 +000035TEST(Timer, Additivity) {
Matthias Braun9f15a792016-11-18 19:43:18 +000036 Timer T1("T1", "T1");
Vedant Kumard1675862015-12-22 17:36:17 +000037
38 EXPECT_TRUE(T1.isInitialized());
39
40 T1.startTimer();
41 T1.stopTimer();
42 auto TR1 = T1.getTotalTime();
43
44 T1.startTimer();
Nico Weber07e602e2015-12-23 01:04:53 +000045 SleepMS();
Vedant Kumard1675862015-12-22 17:36:17 +000046 T1.stopTimer();
47 auto TR2 = T1.getTotalTime();
48
49 EXPECT_TRUE(TR1 < TR2);
50}
51
52TEST(Timer, CheckIfTriggered) {
Matthias Braun9f15a792016-11-18 19:43:18 +000053 Timer T1("T1", "T1");
Vedant Kumard1675862015-12-22 17:36:17 +000054
55 EXPECT_FALSE(T1.hasTriggered());
56 T1.startTimer();
57 EXPECT_TRUE(T1.hasTriggered());
58 T1.stopTimer();
59 EXPECT_TRUE(T1.hasTriggered());
60
61 T1.clear();
62 EXPECT_FALSE(T1.hasTriggered());
63}
64
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000065} // end anon namespace