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 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Support/Timer.h" 10 #include "gtest/gtest.h" 11 12 #if _WIN32 13 #include <windows.h> 14 #else 15 #include <time.h> 16 #endif 17 18 using namespace llvm; 19 20 namespace { 21 22 // FIXME: Put this somewhere in Support, it's also used in LockFileManager. SleepMS()23void 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 32 } 33 TEST(Timer,Additivity)34TEST(Timer, Additivity) { 35 Timer T1("T1", "T1"); 36 37 EXPECT_TRUE(T1.isInitialized()); 38 39 T1.startTimer(); 40 T1.stopTimer(); 41 auto TR1 = T1.getTotalTime(); 42 43 T1.startTimer(); 44 SleepMS(); 45 T1.stopTimer(); 46 auto TR2 = T1.getTotalTime(); 47 48 EXPECT_TRUE(TR1 < TR2); 49 } 50 TEST(Timer,CheckIfTriggered)51TEST(Timer, CheckIfTriggered) { 52 Timer T1("T1", "T1"); 53 54 EXPECT_FALSE(T1.hasTriggered()); 55 T1.startTimer(); 56 EXPECT_TRUE(T1.hasTriggered()); 57 T1.stopTimer(); 58 EXPECT_TRUE(T1.hasTriggered()); 59 60 T1.clear(); 61 EXPECT_FALSE(T1.hasTriggered()); 62 } 63 64 } // end anon namespace 65