Mercurial > malikania
view tests/libcommon/js-elapsed-timer/main.cpp @ 77:61ea80e925ab
CMake: import pugixml 1.8, closes #613
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 20 Jan 2017 11:00:14 +0100 |
parents | 858621081b95 |
children | 119bcc5a727e |
line wrap: on
line source
/* * main.cpp -- test ElapsedTimer (JavaScript binding) * * Copyright (c) 2013-2017 Malikania Authors * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <thread> #define BOOST_TEST_MODULE "Javascript ElapsedTimer" #include <boost/format.hpp> #include <boost/test/unit_test.hpp> #include <malikania/js_elapsed_timer.hpp> using boost::format; using boost::str; using namespace mlk; using namespace std::chrono_literals; /* * For all tests, we tolerate 30 ms because some systems have bigger lags. */ static constexpr int margin = 30; class test_elapsed_timer { protected: dukx_context m_ctx; test_elapsed_timer() { duk_push_object(m_ctx); duk_put_global_string(m_ctx, "Malikania"); mlk::dukx_load_elapsedtimer(m_ctx); } inline void assert_range(int value, int expected) const { if (value < (expected - margin) || value > (expected + margin)) { throw std::invalid_argument( str(format("%d is bigger than [%d, %d]") % value % (expected - margin) % (expected + margin))); } } }; BOOST_FIXTURE_TEST_SUITE(test_elapsed_timer_suite, test_elapsed_timer) BOOST_AUTO_TEST_CASE(standard) { try { if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) { throw dukx_get_exception(m_ctx, -1); } std::this_thread::sleep_for(300ms); if (duk_peval_string(m_ctx, "result = timer.elapsed();") != 0) { throw dukx_get_exception(m_ctx, -1); } duk_get_global_string(m_ctx, "result"); assert_range(duk_to_int(m_ctx, -1), 300); duk_pop(m_ctx); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(reset) { try { if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) { throw dukx_get_exception(m_ctx, -1); } std::this_thread::sleep_for(300ms); if (duk_peval_string(m_ctx, "timer.reset(); result = timer.elapsed();") != 0) { throw dukx_get_exception(m_ctx, -1); } duk_get_global_string(m_ctx, "result"); assert_range(duk_to_int(m_ctx, -1), 0); duk_pop(m_ctx); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(pause) { try { if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) { throw dukx_get_exception(m_ctx, -1); } /* * Simulate a pause in the game like this: * * start pause restart elapsed * | 10ms |.5ms.| 6ms | * * Since the game was paused, the 5ms must not be totalized. */ std::this_thread::sleep_for(10ms); if (duk_peval_string(m_ctx, "timer.pause();") != 0) { throw dukx_get_exception(m_ctx, -1); } std::this_thread::sleep_for(5ms); if (duk_peval_string(m_ctx, "timer.restart();") != 0) { throw dukx_get_exception(m_ctx, -1); } std::this_thread::sleep_for(6ms); if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) { throw dukx_get_exception(m_ctx, -1); } duk_get_global_string(m_ctx, "result"); assert_range(duk_to_int(m_ctx, -1), 16); duk_pop(m_ctx); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(doublecheck) { try { if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) { throw dukx_get_exception(m_ctx, -1); } std::this_thread::sleep_for(50ms); if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) { throw dukx_get_exception(m_ctx, -1); } std::this_thread::sleep_for(50ms); if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) { throw dukx_get_exception(m_ctx, -1); } duk_get_global_string(m_ctx, "result"); assert_range(duk_to_int(m_ctx, -1), 100); duk_pop(m_ctx); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_SUITE_END()