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

« back to all changes in this revision

Viewing changes to tests/worker_api_2_main.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <assert.h>
 
3
#include <emscripten.h>
 
4
 
 
5
struct Info {
 
6
  int i;
 
7
  float f;
 
8
  char c;
 
9
  double d;
 
10
};
 
11
 
 
12
int w1;
 
13
 
 
14
Info x[3] = { {    22,      3.159,  97, 2.1828 },
 
15
              { 55123, 987612.563, 190, 0.0009 },
 
16
              {  -102,    -12.532, -21, -51252 } };
 
17
 
 
18
int stage = -1;
 
19
 
 
20
int c3_7 = 0, c3_8 = 0;
 
21
 
 
22
void c3(char *data, int size, void *arg) { // tests calls different in different workers.
 
23
  int calls = *((int*)data);
 
24
  printf("%d: %d\n", (int)arg, calls);
 
25
  if ((int)arg == 7) {
 
26
    assert(c3_7 == 0);
 
27
    c3_7++;
 
28
    assert(calls == 5);
 
29
  } else {
 
30
    assert((int)arg == 8);
 
31
    assert(c3_8 == 0);
 
32
    c3_8++;
 
33
    assert(calls == 1);
 
34
  }
 
35
  if (c3_7 && c3_7) { // note: racey, responses from 2 workers here
 
36
    emscripten_destroy_worker(w1);
 
37
    int result = 11;
 
38
    REPORT_RESULT();
 
39
  }
 
40
}
 
41
 
 
42
void c2(char *data, int size, void *arg) { // tests queuing up several messages, each with different data
 
43
  assert((int)arg == stage);
 
44
  Info *x2 = (Info*)data;
 
45
 
 
46
  int i = stage - 3;
 
47
  printf("c2-%d\n", i);
 
48
  printf("%d, %.2f, %d, %.2f\n", x2[0].i, x2[0].f, x2[0].c, x2[0].d);
 
49
  printf("%d, %.2f, %d, %.2f\n", x[i].i, x[i].f, x[i].c, x[i].d);
 
50
  assert(x2[0].i == x[i].i+1);
 
51
  assert(x2[0].f == x[i].f-1);
 
52
  assert(x2[0].c == x[i].c+1);
 
53
  assert(x2[0].d == x[i].d-1);
 
54
 
 
55
  if (stage == 5) {
 
56
    int w2 = emscripten_create_worker("worker.js");
 
57
    emscripten_call_worker(w2, "three", NULL, 0, NULL, (void*)6); // bump calls in new worker, once
 
58
 
 
59
    emscripten_call_worker(w1, "four", NULL, 0, c3, (void*)7);
 
60
    emscripten_call_worker(w2, "four", NULL, 0, c3, (void*)8);
 
61
  }
 
62
  stage++;
 
63
}
 
64
 
 
65
void c1(char *data, int size, void *arg) { // tests copying + buffer enlargement
 
66
  assert((int)arg == stage);
 
67
  if (stage == 1) {
 
68
    printf("wait 0? %d\n", emscripten_get_worker_queue_size(w1));
 
69
    assert(emscripten_get_worker_queue_size(w1) == 0);
 
70
  }
 
71
  Info *x2 = (Info*)data;
 
72
  assert(x2 != x && x2 != x+1 && x2 != x+2);
 
73
  for (int i = 0; i < size/sizeof(Info); i++) {
 
74
    printf("c1-%d\n", i);
 
75
    printf("  %d, %.2f, %d, %.2f\n", x2[i].i, x2[i].f, x2[i].c, x2[i].d);
 
76
    printf("  %d, %.2f, %d, %.2f\n", x[i].i, x[i].f, x[i].c, x[i].d);
 
77
    assert(x2[i].i == x[i].i);
 
78
    assert(x2[i].f == x[i].f);
 
79
    assert(x2[i].c == x[i].c);
 
80
    assert(x2[i].d == x[i].d);
 
81
  }
 
82
  if (stage < 2) {
 
83
    emscripten_call_worker(w1, "one", (char*)x, sizeof(Info)*3, c1, (void*)2);
 
84
  } else {
 
85
    printf("wait 1? %d\n", emscripten_get_worker_queue_size(w1));
 
86
    assert(emscripten_get_worker_queue_size(w1) == 0);
 
87
    emscripten_call_worker(w1, "two", (char*)&x[0], sizeof(Info), c2, (void*)3);
 
88
    emscripten_call_worker(w1, "two", (char*)&x[1], sizeof(Info), c2, (void*)4);
 
89
    emscripten_call_worker(w1, "two", (char*)&x[2], sizeof(Info), c2, (void*)5);
 
90
    printf("wait 2? %d\n", emscripten_get_worker_queue_size(w1));
 
91
    assert(emscripten_get_worker_queue_size(w1) == 3);
 
92
  }
 
93
  stage++;
 
94
}
 
95
 
 
96
int main() {
 
97
  w1 = emscripten_create_worker("worker.js");
 
98
 
 
99
  printf("wait -1? %d\n", emscripten_get_worker_queue_size(w1));
 
100
  assert(emscripten_get_worker_queue_size(w1) == 0);
 
101
  emscripten_call_worker(w1, "one", (char*)x, sizeof(Info)*2, c1, (void*)1);
 
102
  printf("wait -1? %d\n", emscripten_get_worker_queue_size(w1));
 
103
  assert(emscripten_get_worker_queue_size(w1) == 1);
 
104
  stage = 1; // make sure we get here
 
105
 
 
106
  return 0;
 
107
}
 
108