File: timer_test.cpp

package info (click to toggle)
libbiosoup-dev 0.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 168 kB
  • sloc: cpp: 646; makefile: 12
file content (71 lines) | stat: -rw-r--r-- 2,161 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2020 Robert Vaser

#include "biosoup/timer.hpp"

#include <thread>  // NOLINT

#include "gtest/gtest.h"

namespace biosoup {
namespace test {

bool ExpectWithinInclusive(double lower, double upper, double value) {
  return lower <= value && value <= upper;
}

TEST(BiosoupTimerTest, Start) {
  Timer t{};
  t.Start();
  std::this_thread::sleep_for(std::chrono::milliseconds(256));
  EXPECT_TRUE(ExpectWithinInclusive(0.256, 0.384, t.Stop()));
  t.Start();
  std::this_thread::sleep_for(std::chrono::milliseconds(256));
  t.Start();
  EXPECT_TRUE(ExpectWithinInclusive(0, 0.001, t.Stop()));
}

TEST(BiosoupTimerTest, Stop) {
  Timer t{};
  EXPECT_TRUE(ExpectWithinInclusive(0, 0.001, t.Stop()));
  EXPECT_EQ(0, t.elapsed_time());
  t.Start();
  std::this_thread::sleep_for(std::chrono::milliseconds(256));
  t.Stop();
  std::this_thread::sleep_for(std::chrono::milliseconds(256));
  EXPECT_TRUE(ExpectWithinInclusive(0.256, 0.384, t.elapsed_time()));
  EXPECT_TRUE(ExpectWithinInclusive(0, 0.001, t.Stop()));
  EXPECT_TRUE(ExpectWithinInclusive(0.256, 0.384, t.elapsed_time()));
  t.Start();
  std::this_thread::sleep_for(std::chrono::milliseconds(256));
  EXPECT_TRUE(ExpectWithinInclusive(0.256, 0.384, t.Stop()));
  EXPECT_TRUE(ExpectWithinInclusive(0.512, 0.768, t.elapsed_time()));
}

TEST(BiosoupTimerTest, Lap) {
  Timer t{};
  t.Start();
  EXPECT_TRUE(ExpectWithinInclusive(0, 0.001, t.Lap()));
  std::this_thread::sleep_for(std::chrono::milliseconds(256));
  EXPECT_TRUE(ExpectWithinInclusive(0.256, 0.384, t.Lap()));
  EXPECT_EQ(0, t.elapsed_time());
  EXPECT_TRUE(ExpectWithinInclusive(0.256, 0.384, t.Stop()));
  EXPECT_TRUE(ExpectWithinInclusive(0, 0.001, t.Lap()));
}

TEST(BiosoupTimerTest, Reset) {
  Timer t{};
  t.Start();
  std::this_thread::sleep_for(std::chrono::milliseconds(256));
  t.Reset();
  t.Stop();
  std::this_thread::sleep_for(std::chrono::milliseconds(256));
  t.Start();
  std::this_thread::sleep_for(std::chrono::milliseconds(256));
  t.Stop();
  EXPECT_TRUE(ExpectWithinInclusive(0.256, 0.384, t.elapsed_time()));
  t.Reset();
  EXPECT_EQ(0, t.elapsed_time());
}

}  // namespace test
}  // namespace biosoup