File: TimerTest.cpp

package info (click to toggle)
cli11 2.4.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,120 kB
  • sloc: cpp: 23,299; python: 129; sh: 64; makefile: 11; ruby: 7
file content (68 lines) | stat: -rw-r--r-- 2,037 bytes parent folder | download
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
// Copyright (c) 2017-2024, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause

#include "CLI/Timer.hpp"

#include "catch.hpp"
#include <chrono>
#include <sstream>
#include <string>
#include <thread>

TEST_CASE("Timer: MSTimes", "[timer]") {
    CLI::Timer timer{"My Timer"};
    std::this_thread::sleep_for(std::chrono::milliseconds(123));
    std::string output = timer.to_string();
    std::string new_output = (timer / 1000000).to_string();
    CHECK_THAT(output, Contains("My Timer"));
    CHECK_THAT(output, Contains(" ms"));
    CHECK_THAT(new_output, Contains(" ns"));
}

/* Takes too long
TEST_CASE("Timer: STimes", "[timer]") {
    CLI::Timer timer;
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::string output = timer.to_string();
    CHECK_THAT (output, Contains(" s"));
}
*/

// Fails on Windows
// TEST_CASE("Timer: UStimes", "[timer]") {
//    CLI::Timer timer;
//    std::this_thread::sleep_for(std::chrono::microseconds(2));
//    std::string output = timer.to_string();
//    CHECK_THAT (output, Contains(" ms"));
//}

TEST_CASE("Timer: BigTimer", "[timer]") {
    CLI::Timer timer{"My Timer", CLI::Timer::Big};
    std::string output = timer.to_string();
    CHECK_THAT(output, Contains("Time ="));
    CHECK_THAT(output, Contains("-----------"));
}

TEST_CASE("Timer: AutoTimer", "[timer]") {
    CLI::AutoTimer timer;
    std::string output = timer.to_string();
    CHECK_THAT(output, Contains("Timer"));
}

TEST_CASE("Timer: PrintTimer", "[timer]") {
    std::stringstream out;
    CLI::AutoTimer timer;
    out << timer;
    std::string output = out.str();
    CHECK_THAT(output, Contains("Timer"));
}

TEST_CASE("Timer: TimeItTimer", "[timer]") {
    CLI::Timer timer;
    std::string output = timer.time_it([]() { std::this_thread::sleep_for(std::chrono::milliseconds(10)); }, .1);
    std::cout << output << '\n';
    CHECK_THAT(output, Contains("ms"));
}