Mercurial > irccd
comparison tests/src/libirccd-js/js-api-chrono/main.cpp @ 846:dcef68d82fd3
irccd: rework Irccd.ElapsedTimer -> Irccd.Chrono API, closes #1667
- Add new start function to restart a paused timer (closes #1669),
- Rename restart function to resume (closes #1668),
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 10 Jul 2019 13:39:20 +0200 |
parents | tests/src/libirccd-js/js-api-elapsedtimer/main.cpp@06cc2f95f479 |
children | 7619105cc818 |
comparison
equal
deleted
inserted
replaced
845:00a4720c4874 | 846:dcef68d82fd3 |
---|---|
1 /* | |
2 * main.cpp -- test Irccd.Chrono API | |
3 * | |
4 * Copyright (c) 2013-2019 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 #define BOOST_TEST_MODULE "Chrono Javascript API" | |
20 #include <boost/test/unit_test.hpp> | |
21 | |
22 #include <thread> | |
23 | |
24 #include <irccd/test/js_fixture.hpp> | |
25 | |
26 using namespace std::chrono_literals; | |
27 | |
28 using namespace irccd::js; | |
29 using namespace irccd::test; | |
30 | |
31 namespace irccd { | |
32 | |
33 namespace { | |
34 | |
35 BOOST_FIXTURE_TEST_SUITE(chrono_js_api_suite, js_fixture) | |
36 | |
37 BOOST_AUTO_TEST_CASE(simple) | |
38 { | |
39 if (duk_peval_string(plugin_->get_context(), "timer = new Irccd.Chrono();") != 0) | |
40 throw duk::get_stack(plugin_->get_context(), -1); | |
41 | |
42 std::this_thread::sleep_for(300ms); | |
43 | |
44 if (duk_peval_string(plugin_->get_context(), "result = timer.elapsed();") != 0) | |
45 throw duk::get_stack(plugin_->get_context(), -1); | |
46 | |
47 BOOST_REQUIRE(duk_get_global_string(plugin_->get_context(), "result")); | |
48 BOOST_REQUIRE_GE(duk_get_int(plugin_->get_context(), -1), 250); | |
49 BOOST_REQUIRE_LE(duk_get_int(plugin_->get_context(), -1), 350); | |
50 } | |
51 | |
52 BOOST_AUTO_TEST_CASE(pause) | |
53 { | |
54 /* | |
55 * Create a time and stop it immediately. Then wait for 1 seconds, | |
56 * the time must still be near 0. | |
57 */ | |
58 if (duk_peval_string(plugin_->get_context(), "timer = new Irccd.Chrono(); timer.pause();") != 0) | |
59 throw duk::get_stack(plugin_->get_context(), -1); | |
60 | |
61 std::this_thread::sleep_for(1s); | |
62 | |
63 if (duk_peval_string(plugin_->get_context(), "result = timer.elapsed();") != 0) | |
64 throw duk::get_stack(plugin_->get_context(), -1); | |
65 | |
66 BOOST_REQUIRE(duk_get_global_string(plugin_->get_context(), "result")); | |
67 BOOST_REQUIRE_LE(duk_get_int(plugin_->get_context(), -1), 50); | |
68 } | |
69 | |
70 BOOST_AUTO_TEST_CASE(resume) | |
71 { | |
72 /* | |
73 * Create a time and stop it immediately. Then wait for 1 seconds, | |
74 * resume it and wait for 1 second more. The elapsed time must not be | |
75 * greater than 1s. | |
76 */ | |
77 if (duk_peval_string(plugin_->get_context(), "timer = new Irccd.Chrono(); timer.pause();") != 0) | |
78 throw duk::get_stack(plugin_->get_context(), -1); | |
79 | |
80 std::this_thread::sleep_for(1s); | |
81 | |
82 if (duk_peval_string(plugin_->get_context(), "timer.resume();") != 0) | |
83 throw duk::get_stack(plugin_->get_context(), -1); | |
84 | |
85 std::this_thread::sleep_for(1s); | |
86 | |
87 if (duk_peval_string(plugin_->get_context(), "result = timer.elapsed();") != 0) | |
88 throw duk::get_stack(plugin_->get_context(), -1); | |
89 | |
90 BOOST_REQUIRE(duk_get_global_string(plugin_->get_context(), "result")); | |
91 BOOST_REQUIRE_GE(duk_get_int(plugin_->get_context(), -1), 950); | |
92 BOOST_REQUIRE_LE(duk_get_int(plugin_->get_context(), -1), 1050); | |
93 } | |
94 | |
95 BOOST_AUTO_TEST_CASE(start) | |
96 { | |
97 /* | |
98 * Create a timer and wait for it to accumulate some time. Then use | |
99 * start to reset its value and wait for 1s. The elapsed time must not | |
100 * be greater than 1s. | |
101 */ | |
102 if (duk_peval_string(plugin_->get_context(), "timer = new Irccd.Chrono(); timer.start();") != 0) | |
103 throw duk::get_stack(plugin_->get_context(), -1); | |
104 | |
105 std::this_thread::sleep_for(1s); | |
106 | |
107 if (duk_peval_string(plugin_->get_context(), "timer.start();") != 0) | |
108 throw duk::get_stack(plugin_->get_context(), -1); | |
109 | |
110 std::this_thread::sleep_for(1s); | |
111 | |
112 if (duk_peval_string(plugin_->get_context(), "result = timer.elapsed();") != 0) | |
113 throw duk::get_stack(plugin_->get_context(), -1); | |
114 | |
115 BOOST_REQUIRE(duk_get_global_string(plugin_->get_context(), "result")); | |
116 BOOST_REQUIRE_GE(duk_get_int(plugin_->get_context(), -1), 950); | |
117 BOOST_REQUIRE_LE(duk_get_int(plugin_->get_context(), -1), 1050); | |
118 } | |
119 | |
120 BOOST_AUTO_TEST_SUITE_END() | |
121 | |
122 } // !namespace | |
123 | |
124 } // !irccd |