~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/emscripten_get_now.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include "emscripten.h"
 
3
 
 
4
#ifndef REPORT_RESULT
 
5
// To be able to run this test outside the browser harness in node.js/spidermonkey:
 
6
#define REPORT_RESULT int dummy
 
7
#endif
 
8
 
 
9
int result = 0;
 
10
 
 
11
int main() {
 
12
  // This code tests three things:
 
13
  // a) Calling emscripten_get_now(), time actually proceeds.
 
14
  // b) Values returned by emscripten_get_now() are strictly nondecreasing.
 
15
  // c) emscripten_get_now() is able to return sub-millisecond precision timer values.
 
16
  bool detected_good_timer_precision = false;
 
17
  float smallest_delta = 0.f;
 
18
  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.
 
19
    float t = emscripten_get_now();
 
20
    float t2 = emscripten_get_now();
 
21
    for(int i = 0; i < 100 && t == t2; ++i) {
 
22
      t2 = emscripten_get_now();
 
23
    }
 
24
 
 
25
    if (t2 < t && t2 - t < 1000.f) { // Timer must be monotonous.
 
26
      printf("Timer is not monotonous!\\n");
 
27
      smallest_delta = t2 - t;
 
28
      break;
 
29
    }
 
30
    if (t2 > t && t2 - t < 0.7f) { // Must pass less than a millisecond between two calls.
 
31
      detected_good_timer_precision = true;
 
32
      smallest_delta = t2 - t;
 
33
      break;
 
34
    }
 
35
  }
 
36
  
 
37
  if (detected_good_timer_precision) {
 
38
    printf("Timer resolution is good. (%f msecs)\\n", smallest_delta);
 
39
    result = 1;
 
40
  } else {
 
41
    printf("Error: Bad timer precision: Smallest timer delta: %f msecs\\n", smallest_delta);
 
42
    result = 0;
 
43
  }
 
44
  REPORT_RESULT();
 
45
  return 0;
 
46
}