comparison tests/timer/main.cpp @ 0:1158cffe5a5e

Initial import
author David Demelier <markand@malikania.fr>
date Mon, 08 Feb 2016 16:43:14 +0100
parents
children f8160d515a76
comparison
equal deleted inserted replaced
-1:000000000000 0:1158cffe5a5e
1 /*
2 * main.cpp -- test irccd timer
3 *
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <gtest/gtest.h>
20
21 #include <elapsed-timer.h>
22 #include <timer.h>
23
24 using namespace irccd;
25 using namespace std::chrono_literals;
26
27 /* --------------------------------------------------------
28 * Timer object itself
29 * -------------------------------------------------------- */
30
31 TEST(Basic, single)
32 {
33 Timer timer(TimerType::Single, 1000);
34 ElapsedTimer elapsed;
35 int count = 0;
36
37 timer.onSignal.connect([&] () {
38 count = elapsed.elapsed();
39 });
40
41 elapsed.reset();
42 timer.start();
43
44 std::this_thread::sleep_for(3s);
45
46 ASSERT_TRUE(count >= 950 && count <= 1050);
47 }
48
49 TEST(Basic, repeat)
50 {
51 Timer timer(TimerType::Repeat, 500);
52 int max = 0;
53
54 timer.onSignal.connect([&] () {
55 max ++;
56 });
57
58 timer.start();
59
60 // Should be at least 5
61 std::this_thread::sleep_for(3s);
62
63 ASSERT_TRUE(max >= 5);
64
65 timer.stop();
66 }
67
68 TEST(Basic, restart)
69 {
70 Timer timer(TimerType::Repeat, 500);
71 int max = 0;
72
73 timer.onSignal.connect([&] () {
74 max ++;
75 });
76
77 timer.start();
78 std::this_thread::sleep_for(3s);
79 timer.stop();
80 std::this_thread::sleep_for(3s);
81 timer.start();
82 std::this_thread::sleep_for(3s);
83
84 ASSERT_GE(max, 10);
85 ASSERT_LT(max, 15);
86
87 timer.stop();
88 }
89
90 int main(int argc, char **argv)
91 {
92 testing::InitGoogleTest(&argc, argv);
93
94 return RUN_ALL_TESTS();
95 }