summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/test/TestStopwatch.cpp
blob: 82f555d99ee9f015bab035a10e9b9eae7606e757 (plain)
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
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "threads/Thread.h"
#include "utils/Stopwatch.h"

#include <gtest/gtest.h>

using namespace std::chrono_literals;

class CTestStopWatchThread : public CThread
{
public:
  CTestStopWatchThread() :
    CThread("TestStopWatch"){}
};

TEST(TestStopWatch, Initialization)
{
  CStopWatch a;
  EXPECT_FALSE(a.IsRunning());
  EXPECT_EQ(0.0f, a.GetElapsedSeconds());
  EXPECT_EQ(0.0f, a.GetElapsedMilliseconds());
}

TEST(TestStopWatch, Start)
{
  CStopWatch a;
  a.Start();
  EXPECT_TRUE(a.IsRunning());
}

TEST(TestStopWatch, Stop)
{
  CStopWatch a;
  a.Start();
  a.Stop();
  EXPECT_FALSE(a.IsRunning());
}

TEST(TestStopWatch, ElapsedTime)
{
  CStopWatch a;
  CTestStopWatchThread thread;
  a.Start();
  thread.Sleep(1ms);
  EXPECT_GT(a.GetElapsedSeconds(), 0.0f);
  EXPECT_GT(a.GetElapsedMilliseconds(), 0.0f);
}

TEST(TestStopWatch, Reset)
{
  CStopWatch a;
  CTestStopWatchThread thread;
  a.StartZero();
  thread.Sleep(2ms);
  EXPECT_GT(a.GetElapsedMilliseconds(), 1);
  thread.Sleep(3ms);
  a.Reset();
  EXPECT_LT(a.GetElapsedMilliseconds(), 5);
}