• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- 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"
12 
13 #if _WIN32
14 #include <windows.h>
15 #else
16 #include <time.h>
17 #endif
18 
19 using namespace llvm;
20 
21 namespace {
22 
23 // FIXME: Put this somewhere in Support, it's also used in LockFileManager.
SleepMS()24 void SleepMS() {
25 #if _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 
TEST(Timer,Additivity)35 TEST(Timer, Additivity) {
36   Timer T1("T1", "T1");
37 
38   EXPECT_TRUE(T1.isInitialized());
39 
40   T1.startTimer();
41   T1.stopTimer();
42   auto TR1 = T1.getTotalTime();
43 
44   T1.startTimer();
45   SleepMS();
46   T1.stopTimer();
47   auto TR2 = T1.getTotalTime();
48 
49   EXPECT_TRUE(TR1 < TR2);
50 }
51 
TEST(Timer,CheckIfTriggered)52 TEST(Timer, CheckIfTriggered) {
53   Timer T1("T1", "T1");
54 
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 
65 } // end anon namespace
66