comparison tests/libcommon/js-elapsed-timer/main.cpp @ 63:96ba0c5cf893

Misc: update duktape.hpp to new style
author David Demelier <markand@malikania.fr>
date Fri, 16 Dec 2016 16:11:24 +0100
parents f30c84b4b9ed
children 858621081b95
comparison
equal deleted inserted replaced
62:a0081be977b5 63:96ba0c5cf893
35 */ 35 */
36 static constexpr int margin = 30; 36 static constexpr int margin = 30;
37 37
38 class test_elapsed_timer { 38 class test_elapsed_timer {
39 protected: 39 protected:
40 UniqueContext m_ctx; 40 dukx_context m_ctx;
41 41
42 test_elapsed_timer() 42 test_elapsed_timer()
43 { 43 {
44 duk_push_object(m_ctx); 44 duk_push_object(m_ctx);
45 duk_put_global_string(m_ctx, "Malikania"); 45 duk_put_global_string(m_ctx, "Malikania");
59 59
60 BOOST_AUTO_TEST_CASE(standard) 60 BOOST_AUTO_TEST_CASE(standard)
61 { 61 {
62 try { 62 try {
63 if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) { 63 if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) {
64 throw dukx_exception(m_ctx, -1); 64 throw dukx_get_exception(m_ctx, -1);
65 } 65 }
66 66
67 std::this_thread::sleep_for(300ms); 67 std::this_thread::sleep_for(300ms);
68 68
69 if (duk_peval_string(m_ctx, "result = timer.elapsed();") != 0) { 69 if (duk_peval_string(m_ctx, "result = timer.elapsed();") != 0) {
70 throw dukx_exception(m_ctx, -1); 70 throw dukx_get_exception(m_ctx, -1);
71 } 71 }
72 72
73 duk_get_global_string(m_ctx, "result"); 73 duk_get_global_string(m_ctx, "result");
74 assert_range(duk_to_int(m_ctx, -1), 300); 74 assert_range(duk_to_int(m_ctx, -1), 300);
75 duk_pop(m_ctx); 75 duk_pop(m_ctx);
80 80
81 BOOST_AUTO_TEST_CASE(reset) 81 BOOST_AUTO_TEST_CASE(reset)
82 { 82 {
83 try { 83 try {
84 if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) { 84 if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) {
85 throw dukx_exception(m_ctx, -1); 85 throw dukx_get_exception(m_ctx, -1);
86 } 86 }
87 87
88 std::this_thread::sleep_for(300ms); 88 std::this_thread::sleep_for(300ms);
89 89
90 if (duk_peval_string(m_ctx, "timer.reset(); result = timer.elapsed();") != 0) { 90 if (duk_peval_string(m_ctx, "timer.reset(); result = timer.elapsed();") != 0) {
91 throw dukx_exception(m_ctx, -1); 91 throw dukx_get_exception(m_ctx, -1);
92 } 92 }
93 93
94 duk_get_global_string(m_ctx, "result"); 94 duk_get_global_string(m_ctx, "result");
95 assert_range(duk_to_int(m_ctx, -1), 0); 95 assert_range(duk_to_int(m_ctx, -1), 0);
96 duk_pop(m_ctx); 96 duk_pop(m_ctx);
101 101
102 BOOST_AUTO_TEST_CASE(pause) 102 BOOST_AUTO_TEST_CASE(pause)
103 { 103 {
104 try { 104 try {
105 if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) { 105 if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) {
106 throw dukx_exception(m_ctx, -1); 106 throw dukx_get_exception(m_ctx, -1);
107 } 107 }
108 108
109 /* 109 /*
110 * Simulate a pause in the game like this: 110 * Simulate a pause in the game like this:
111 * 111 *
115 * Since the game was paused, the 5ms must not be totalized. 115 * Since the game was paused, the 5ms must not be totalized.
116 */ 116 */
117 std::this_thread::sleep_for(10ms); 117 std::this_thread::sleep_for(10ms);
118 118
119 if (duk_peval_string(m_ctx, "timer.pause();") != 0) { 119 if (duk_peval_string(m_ctx, "timer.pause();") != 0) {
120 throw dukx_exception(m_ctx, -1); 120 throw dukx_get_exception(m_ctx, -1);
121 } 121 }
122 122
123 std::this_thread::sleep_for(5ms); 123 std::this_thread::sleep_for(5ms);
124 124
125 if (duk_peval_string(m_ctx, "timer.restart();") != 0) { 125 if (duk_peval_string(m_ctx, "timer.restart();") != 0) {
126 throw dukx_exception(m_ctx, -1); 126 throw dukx_get_exception(m_ctx, -1);
127 } 127 }
128 128
129 std::this_thread::sleep_for(6ms); 129 std::this_thread::sleep_for(6ms);
130 130
131 if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) { 131 if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) {
132 throw dukx_exception(m_ctx, -1); 132 throw dukx_get_exception(m_ctx, -1);
133 } 133 }
134 134
135 duk_get_global_string(m_ctx, "result"); 135 duk_get_global_string(m_ctx, "result");
136 assert_range(duk_to_int(m_ctx, -1), 16); 136 assert_range(duk_to_int(m_ctx, -1), 16);
137 duk_pop(m_ctx); 137 duk_pop(m_ctx);
142 142
143 BOOST_AUTO_TEST_CASE(doublecheck) 143 BOOST_AUTO_TEST_CASE(doublecheck)
144 { 144 {
145 try { 145 try {
146 if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) { 146 if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) {
147 throw dukx_exception(m_ctx, -1); 147 throw dukx_get_exception(m_ctx, -1);
148 } 148 }
149 149
150 std::this_thread::sleep_for(50ms); 150 std::this_thread::sleep_for(50ms);
151 151
152 if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) { 152 if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) {
153 throw dukx_exception(m_ctx, -1); 153 throw dukx_get_exception(m_ctx, -1);
154 } 154 }
155 155
156 std::this_thread::sleep_for(50ms); 156 std::this_thread::sleep_for(50ms);
157 157
158 if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) { 158 if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) {
159 throw dukx_exception(m_ctx, -1); 159 throw dukx_get_exception(m_ctx, -1);
160 } 160 }
161 161
162 duk_get_global_string(m_ctx, "result"); 162 duk_get_global_string(m_ctx, "result");
163 assert_range(duk_to_int(m_ctx, -1), 100); 163 assert_range(duk_to_int(m_ctx, -1), 100);
164 duk_pop(m_ctx); 164 duk_pop(m_ctx);