Mercurial > irccd
comparison tests/js-elapsedtimer/main.cpp @ 75:f8160d515a76
Irccd: rework a lot the JavaScript library
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 30 Mar 2016 13:52:47 +0200 |
parents | 1158cffe5a5e |
children | 1125d90b3b44 |
comparison
equal
deleted
inserted
replaced
74:35ef15100de8 | 75:f8160d515a76 |
---|---|
18 | 18 |
19 #include <gtest/gtest.h> | 19 #include <gtest/gtest.h> |
20 | 20 |
21 #include <thread> | 21 #include <thread> |
22 | 22 |
23 #include <js-irccd.h> | 23 #include <irccd/js-irccd.h> |
24 #include <js-elapsed-timer.h> | 24 #include <irccd/js-elapsed-timer.h> |
25 | 25 |
26 using namespace irccd; | 26 using namespace irccd; |
27 using namespace std::chrono_literals; | 27 using namespace std::chrono_literals; |
28 | 28 |
29 /* | 29 /* |
31 */ | 31 */ |
32 static constexpr int margin = 30; | 32 static constexpr int margin = 30; |
33 | 33 |
34 class TestElapsedTimer : public testing::Test { | 34 class TestElapsedTimer : public testing::Test { |
35 protected: | 35 protected: |
36 js::Context m_context; | 36 duk::Context m_context; |
37 | 37 |
38 TestElapsedTimer() | 38 TestElapsedTimer() |
39 { | 39 { |
40 loadJsIrccd(m_context); | 40 loadJsIrccd(m_context); |
41 loadJsElapsedTimer(m_context); | 41 loadJsElapsedTimer(m_context); |
48 } | 48 } |
49 }; | 49 }; |
50 | 50 |
51 TEST_F(TestElapsedTimer, standard) | 51 TEST_F(TestElapsedTimer, standard) |
52 { | 52 { |
53 m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); | 53 try { |
54 if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) | |
55 throw duk::error(m_context, -1); | |
54 | 56 |
55 std::this_thread::sleep_for(300ms); | 57 std::this_thread::sleep_for(300ms); |
56 | 58 |
57 m_context.peval(js::Script{"result = timer.elapsed();"}); | 59 if (duk::pevalString(m_context, "result = timer.elapsed();") != 0) |
58 assertRange(m_context.getGlobal<int>("result"), 300); | 60 throw duk::error(m_context, -1); |
61 | |
62 assertRange(duk::getGlobal<int>(m_context, "result"), 300); | |
63 } catch (const std::exception &ex) { | |
64 FAIL() << ex.what(); | |
65 } | |
59 } | 66 } |
60 | 67 |
61 TEST_F(TestElapsedTimer, reset) | 68 TEST_F(TestElapsedTimer, reset) |
62 { | 69 { |
63 m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); | 70 try { |
71 if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) | |
72 throw duk::error(m_context, -1); | |
64 | 73 |
65 std::this_thread::sleep_for(300ms); | 74 std::this_thread::sleep_for(300ms); |
66 | 75 |
67 m_context.peval(js::Script{"timer.reset(); result = timer.elapsed();"}); | 76 if (duk::pevalString(m_context, "timer.reset(); result = timer.elapsed();") != 0) |
68 assertRange(m_context.getGlobal<int>("result"), 0); | 77 throw duk::error(m_context, -1); |
78 | |
79 assertRange(duk::getGlobal<int>(m_context, "result"), 0); | |
80 } catch (const std::exception &ex) { | |
81 FAIL() << ex.what(); | |
82 } | |
69 } | 83 } |
70 | 84 |
71 TEST_F(TestElapsedTimer, pause) | 85 TEST_F(TestElapsedTimer, pause) |
72 { | 86 { |
73 m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); | 87 try { |
88 if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) | |
89 throw duk::error(m_context, -1); | |
74 | 90 |
75 /* | 91 /* |
76 * Simulate a pause in the game like this: | 92 * Simulate a pause in the game like this: |
77 * | 93 * |
78 * start pause restart elapsed | 94 * start pause restart elapsed |
79 * | 10ms |.5ms.| 6ms | | 95 * | 10ms |.5ms.| 6ms | |
80 * | 96 * |
81 * Since the game was paused, the 5ms must not be totalized. | 97 * Since the game was paused, the 5ms must not be totalized. |
82 */ | 98 */ |
83 std::this_thread::sleep_for(10ms); | 99 std::this_thread::sleep_for(10ms); |
84 | 100 |
85 m_context.peval(js::Script{"timer.pause();"}); | 101 if (duk::pevalString(m_context, "timer.pause();") != 0) |
102 throw duk::error(m_context, -1); | |
86 | 103 |
87 std::this_thread::sleep_for(5ms); | 104 std::this_thread::sleep_for(5ms); |
88 | 105 |
89 m_context.peval(js::Script{"timer.restart();"}); | 106 if (duk::pevalString(m_context, "timer.restart();") != 0) |
107 throw duk::error(m_context, -1); | |
90 | 108 |
91 std::this_thread::sleep_for(6ms); | 109 std::this_thread::sleep_for(6ms); |
92 | 110 |
93 m_context.peval(js::Script{"result = timer.elapsed()"}); | 111 if (duk::pevalString(m_context, "result = timer.elapsed()") != 0) |
94 assertRange(m_context.getGlobal<int>("result"), 16); | 112 throw duk::error(m_context, -1); |
113 | |
114 assertRange(duk::getGlobal<int>(m_context, "result"), 16); | |
115 } catch (const std::exception &ex) { | |
116 FAIL() << ex.what(); | |
117 } | |
95 } | 118 } |
96 | 119 |
97 TEST_F(TestElapsedTimer, doublecheck) | 120 TEST_F(TestElapsedTimer, doublecheck) |
98 { | 121 { |
99 m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); | 122 try { |
123 if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) | |
124 throw duk::error(m_context, -1); | |
100 | 125 |
101 std::this_thread::sleep_for(50ms); | 126 std::this_thread::sleep_for(50ms); |
102 | 127 |
103 m_context.peval(js::Script{"result = timer.elapsed()"}); | 128 if (duk::pevalString(m_context, "result = timer.elapsed()") != 0) |
129 throw duk::error(m_context, -1); | |
104 | 130 |
105 std::this_thread::sleep_for(50ms); | 131 std::this_thread::sleep_for(50ms); |
106 | 132 |
107 m_context.peval(js::Script{"result = timer.elapsed()"}); | 133 if (duk::pevalString(m_context, "result = timer.elapsed()") != 0) |
108 assertRange(m_context.getGlobal<int>("result"), 100); | 134 throw duk::error(m_context, -1); |
135 | |
136 assertRange(duk::getGlobal<int>(m_context, "result"), 100); | |
137 } catch (const std::exception &ex) { | |
138 FAIL() << ex.what(); | |
139 } | |
109 } | 140 } |
110 | 141 |
111 int main(int argc, char **argv) | 142 int main(int argc, char **argv) |
112 { | 143 { |
113 testing::InitGoogleTest(&argc, argv); | 144 testing::InitGoogleTest(&argc, argv); |