Mercurial > irccd
comparison tests/js-elapsedtimer/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.ElapsedTimer API | |
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 <thread> | |
22 | |
23 #include <js-irccd.h> | |
24 #include <js-elapsed-timer.h> | |
25 | |
26 using namespace irccd; | |
27 using namespace std::chrono_literals; | |
28 | |
29 /* | |
30 * For all tests, we tolerate 30 ms because some systems have bigger lags. | |
31 */ | |
32 static constexpr int margin = 30; | |
33 | |
34 class TestElapsedTimer : public testing::Test { | |
35 protected: | |
36 js::Context m_context; | |
37 | |
38 TestElapsedTimer() | |
39 { | |
40 loadJsIrccd(m_context); | |
41 loadJsElapsedTimer(m_context); | |
42 } | |
43 | |
44 inline void assertRange(int value, int expected) const noexcept | |
45 { | |
46 if (value < (expected - margin) || value > (expected + margin)) | |
47 FAIL() << value << " is bigger than [" << (expected - margin) << ", " << (expected + margin) << "]"; | |
48 } | |
49 }; | |
50 | |
51 TEST_F(TestElapsedTimer, standard) | |
52 { | |
53 m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); | |
54 | |
55 std::this_thread::sleep_for(300ms); | |
56 | |
57 m_context.peval(js::Script{"result = timer.elapsed();"}); | |
58 assertRange(m_context.getGlobal<int>("result"), 300); | |
59 } | |
60 | |
61 TEST_F(TestElapsedTimer, reset) | |
62 { | |
63 m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); | |
64 | |
65 std::this_thread::sleep_for(300ms); | |
66 | |
67 m_context.peval(js::Script{"timer.reset(); result = timer.elapsed();"}); | |
68 assertRange(m_context.getGlobal<int>("result"), 0); | |
69 } | |
70 | |
71 TEST_F(TestElapsedTimer, pause) | |
72 { | |
73 m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); | |
74 | |
75 /* | |
76 * Simulate a pause in the game like this: | |
77 * | |
78 * start pause restart elapsed | |
79 * | 10ms |.5ms.| 6ms | | |
80 * | |
81 * Since the game was paused, the 5ms must not be totalized. | |
82 */ | |
83 std::this_thread::sleep_for(10ms); | |
84 | |
85 m_context.peval(js::Script{"timer.pause();"}); | |
86 | |
87 std::this_thread::sleep_for(5ms); | |
88 | |
89 m_context.peval(js::Script{"timer.restart();"}); | |
90 | |
91 std::this_thread::sleep_for(6ms); | |
92 | |
93 m_context.peval(js::Script{"result = timer.elapsed()"}); | |
94 assertRange(m_context.getGlobal<int>("result"), 16); | |
95 } | |
96 | |
97 TEST_F(TestElapsedTimer, doublecheck) | |
98 { | |
99 m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); | |
100 | |
101 std::this_thread::sleep_for(50ms); | |
102 | |
103 m_context.peval(js::Script{"result = timer.elapsed()"}); | |
104 | |
105 std::this_thread::sleep_for(50ms); | |
106 | |
107 m_context.peval(js::Script{"result = timer.elapsed()"}); | |
108 assertRange(m_context.getGlobal<int>("result"), 100); | |
109 } | |
110 | |
111 int main(int argc, char **argv) | |
112 { | |
113 testing::InitGoogleTest(&argc, argv); | |
114 | |
115 return RUN_ALL_TESTS(); | |
116 } |