Jlm
TimerTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2024 HÃ¥vard Krogstie <krogstie.havard@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #include <gtest/gtest.h>
7 
8 #include <jlm/util/time.hpp>
9 
10 #include <chrono>
11 #include <thread>
12 
13 using namespace jlm::util;
14 
15 static void
16 sleepUs(int us)
17 {
18  std::this_thread::sleep_for(std::chrono::duration<int, std::micro>(us));
19 }
20 
21 TEST(TimerTests, TestStartStop)
22 {
23  Timer t;
24  EXPECT_EQ(t.ns(), 0u);
25  EXPECT_FALSE(t.isRunning());
26 
27  t.start();
28  EXPECT_TRUE(t.isRunning());
29  sleepUs(10);
30  t.stop();
31  EXPECT_FALSE(t.isRunning());
32  auto ns = t.ns();
33  EXPECT_GE(ns, 10000u);
34 
35  // Add more time
36  t.start();
37  sleepUs(1);
38  t.stop();
39  EXPECT_GE(t.ns(), ns + 1000);
40 }
41 
42 TEST(TimerTests, TestReset)
43 {
44  Timer t;
45  EXPECT_EQ(t.ns(), 0u);
46  EXPECT_FALSE(t.isRunning());
47 
48  t.start();
49  sleepUs(1);
50  t.stop();
51 
52  EXPECT_NE(t.ns(), 0u);
53  t.reset();
54  EXPECT_EQ(t.ns(), 0u);
55  EXPECT_FALSE(t.isRunning());
56 
57  // Resetting while running
58  t.start();
59  t.reset();
60  EXPECT_EQ(t.ns(), 0u);
61  EXPECT_FALSE(t.isRunning());
62 }
static void sleepUs(int us)
Definition: TimerTests.cpp:16
TEST(TimerTests, TestStartStop)
Definition: TimerTests.cpp:21
void reset() noexcept
Definition: time.hpp:43
void start() noexcept
Definition: time.hpp:54
void stop() noexcept
Definition: time.hpp:67
size_t ns() const
Definition: time.hpp:83
bool isRunning() const noexcept
Definition: time.hpp:33