aboutsummaryrefslogtreecommitdiff
path: root/tests/emscripten_get_now.cpp
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-08-30 11:21:48 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-08-30 11:21:48 -0700
commitb5b49215d4a40566380a769f47a9c1cce74a28b0 (patch)
tree68308b6059798a81f24f6a8a1ac28a0091c5d066 /tests/emscripten_get_now.cpp
parent1cc28b8e9e94267041bc71afebfbbe3059db4a3f (diff)
parentb895cdc7df2085d324003c9df582a3dcc1927697 (diff)
Merge branch 'incoming'
Diffstat (limited to 'tests/emscripten_get_now.cpp')
-rw-r--r--tests/emscripten_get_now.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/emscripten_get_now.cpp b/tests/emscripten_get_now.cpp
new file mode 100644
index 00000000..17aa7d32
--- /dev/null
+++ b/tests/emscripten_get_now.cpp
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include "emscripten.h"
+
+#ifndef REPORT_RESULT
+// To be able to run this test outside the browser harness in node.js/spidermonkey:
+#define REPORT_RESULT int dummy
+#endif
+
+int result = 0;
+
+int main() {
+ // This code tests three things:
+ // a) Calling emscripten_get_now(), time actually proceeds.
+ // b) Values returned by emscripten_get_now() are strictly nondecreasing.
+ // c) emscripten_get_now() is able to return sub-millisecond precision timer values.
+ bool detected_good_timer_precision = false;
+ float smallest_delta = 0.f;
+ for(int x = 0; x < 1000; ++x) { // Have several attempts to find a good small delta, i.e. give time to JS engine to warm up the code and so on.
+ float t = emscripten_get_now();
+ float t2 = emscripten_get_now();
+ for(int i = 0; i < 100 && t == t2; ++i) {
+ t2 = emscripten_get_now();
+ }
+
+ if (t2 < t && t2 - t < 1000.f) { // Timer must be monotonous.
+ printf("Timer is not monotonous!\\n");
+ smallest_delta = t2 - t;
+ break;
+ }
+ if (t2 > t && t2 - t < 0.7f) { // Must pass less than a millisecond between two calls.
+ detected_good_timer_precision = true;
+ smallest_delta = t2 - t;
+ break;
+ }
+ }
+
+ if (detected_good_timer_precision) {
+ printf("Timer resolution is good. (%f msecs)\\n", smallest_delta);
+ result = 1;
+ } else {
+ printf("Error: Bad timer precision: Smallest timer delta: %f msecs\\n", smallest_delta);
+ result = 0;
+ }
+ REPORT_RESULT();
+ return 0;
+}