Debian Codegraph @

rpython/translator/c/src/stacklet/tests.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <assert.h>
5 #include "stacklet.h"
6
7
8 static stacklet_thread_handle thrd;
9
10 /************************************************************/
11
12 stacklet_handle empty_callback(stacklet_handle h, void *arg)
13 {
14 assert(arg == (void *)123);
15 return h;
16 }
17
18 void test_new(void)
19 {
20 stacklet_handle h = stacklet_new(thrd, empty_callback, (void *)123);
21 assert(h == EMPTY_STACKLET_HANDLE);
22 }
23
24 /************************************************************/
25
26 static int status;
27
28 stacklet_handle switchbackonce_callback(stacklet_handle h, void *arg)
29 {
30 assert(arg == (void *)123);
31 assert(status == 0);
32 status = 1;
33 assert(h != EMPTY_STACKLET_HANDLE);
34 h = stacklet_switch(h);
35 assert(status == 2);
36 assert(h != EMPTY_STACKLET_HANDLE);
37 status = 3;
38 return h;
39 }
40
41 void test_simple_switch(void)
42 {
43 status = 0;
44 stacklet_handle h = stacklet_new(thrd, switchbackonce_callback, (void *)123);
45 assert(h != EMPTY_STACKLET_HANDLE);
46 assert(status == 1);
47 status = 2;
48 h = stacklet_switch(h);
49 assert(status == 3);
50 assert(h == EMPTY_STACKLET_HANDLE);
51 }
52
53 /************************************************************/
54
55 static stacklet_handle handles[10];
56 static int nextstep, comefrom, gointo;
57 static const int statusmax = 5000;
58
59 int withdepth(int self, float d);
60
61 stacklet_handle variousdepths_callback(stacklet_handle h, void *arg)
62 {
63 int self, n;
64 assert(nextstep == status);
65 nextstep = -1;
66 self = (ptrdiff_t)arg;
67 assert(self == gointo);
68 assert(0 <= self && self < 10);
69 assert(handles[self] == NULL);
70 assert(0 <= comefrom && comefrom < 10);
71 assert(handles[comefrom] == NULL);
72 assert(h != NULL && h != EMPTY_STACKLET_HANDLE);
73 handles[comefrom] = h;
74 comefrom = -1;
75 gointo = -1;
76
77 while (withdepth(self, rand() % 20) == 0)
78 ;
79
80 assert(handles[self] == NULL);
81
82 do {
83 n = rand() % 10;
84 } while (handles[n] == NULL);
85
86 h = handles[n];
87 assert(h != EMPTY_STACKLET_HANDLE);
88 handles[n] = NULL;
89 comefrom = -42;
90 gointo = n;
91 assert(nextstep == -1);
92 nextstep = ++status;
93 //printf("LEAVING %d to go to %d\n", self, n);
94 return h;
95 }
96
97 typedef struct foo_s {
98 int self;
99 float d;
100 struct foo_s *next;
101 } foo_t;
102
103 int withdepth(int self, float d)
104 {
105 int res = 0;
106 if (d > 0.0)
107 {
108 foo_t *foo = malloc(sizeof(foo_t));
109 foo_t *foo2 = malloc(sizeof(foo_t));
110 foo->self = self;
111 foo->d = d;
112 foo->next = foo2;
113 foo2->self = self + 100;
114 foo2->d = d;
115 foo2->next = NULL;
116 res = withdepth(self, d - 1.1);
117 assert(foo->self == self);
118 assert(foo->d == d);
119 assert(foo->next == foo2);
120 assert(foo2->self == self + 100);
121 assert(foo2->d == d);
122 assert(foo2->next == NULL);
123 free(foo2);
124 free(foo);
125 }
126 else
127 {
128 stacklet_handle h;
129 int n = rand() % 10;
130 if (n == self || (status >= statusmax && handles[n] == NULL))
131 return 1;
132
133 //printf("status == %d, self = %d\n", status, self);
134 assert(handles[self] == NULL);
135 assert(nextstep == -1);
136 nextstep = ++status;
137 comefrom = self;
138 gointo = n;
139 if (handles[n] == NULL)
140 {
141 /* start a new stacklet */
142 //printf("new %d\n", n);
143 h = stacklet_new(thrd, variousdepths_callback, (void *)(ptrdiff_t)n);
144 }
145 else
146 {
147 /* switch to this stacklet */
148 //printf("switch to %d\n", n);
149 h = handles[n];
150 handles[n] = NULL;
151 h = stacklet_switch(h);
152 }
153 //printf("back in self = %d, coming from %d\n", self, comefrom);
154 assert(nextstep == status);
155 nextstep = -1;
156 assert(gointo == self);
157 assert(comefrom != self);
158 assert(handles[self] == NULL);
159 if (comefrom != -42)
160 {
161 assert(0 <= comefrom && comefrom < 10);
162 assert(handles[comefrom] == NULL);
163 handles[comefrom] = h;
164 }
165 else
166 assert(h == EMPTY_STACKLET_HANDLE);
167 comefrom = -1;
168 gointo = -1;
169 }
170 assert((res & (res-1)) == 0); /* to prevent a tail-call to withdepth() */
171 return res;
172 }
173
174 int any_alive(void)
175 {
176 int i;
177 for (i=0; i<10; i++)
178 if (handles[i] != NULL)
179 return 1;
180 return 0;
181 }
182
183 void test_various_depths(void)
184 {
185 int i;
186 for (i=0; i<10; i++)
187 handles[i] = NULL;
188
189 nextstep = -1;
190 comefrom = -1;
191 status = 0;
192 while (status < statusmax || any_alive())
193 withdepth(0, rand() % 50);
194 }
195
196 /************************************************************/
197 #if 0
198
199 static tealet_t *runner1(tealet_t *cur)
200 {
201 abort();
202 }
203
204 void test_new_pending(void)
205 {
206 tealet_t *g1 = tealet_new();
207 tealet_t *g2 = tealet_new();
208 int r1 = tealet_fill(g1, runner1);
209 int r2 = tealet_fill(g2, runner1);
210 assert(r1 == TEALET_OK);
211 assert(r2 == TEALET_OK);
212 assert(g1->suspended == 1);
213 assert(g2->suspended == 1);
214 tealet_delete(g1);
215 tealet_delete(g2);
216 }
217
218 /************************************************************/
219
220 void test_not_switched(void)
221 {
222 tealet_t *g1 = tealet_new();
223 tealet_t *g2 = tealet_new();
224 tealet_t *g3 = tealet_switch(g2, g1);
225 assert(!TEALET_ERROR(g3));
226 assert(g3 == g1);
227 tealet_delete(g1);
228 tealet_delete(g2);
229 }
230
231 /************************************************************/
232
233 static tealet_t *g_main;
234
235 static void step(int newstatus)
236 {
237 assert(status == newstatus - 1);
238 status = newstatus;
239 }
240
241 static tealet_t *simple_run(tealet_t *t1)
242 {
243 assert(t1 != g_main);
244 step(2);
245 tealet_delete(t1);
246 return g_main;
247 }
248
249 void test_simple(void)
250 {
251 tealet_t *t1, *tmain;
252 int res;
253
254 status = 0;
255 g_main = tealet_new();
256 t1 = tealet_new();
257 res = tealet_fill(t1, simple_run);
258 assert(res == TEALET_OK);
259 step(1);
260 tmain = tealet_switch(g_main, t1);
261 step(3);
262 assert(tmain == g_main);
263 tealet_delete(g_main);
264 step(4);
265 }
266
267 /************************************************************/
268
269 static tealet_t *simple_exit(tealet_t *t1)
270 {
271 int res;
272 assert(t1 != g_main);
273 step(2);
274 tealet_delete(t1);
275 res = tealet_exit_to(g_main);
276 assert(!"oups");
277 }
278
279 void test_exit(void)
280 {
281 tealet_t *t1, *tmain;
282 int res;
283
284 status = 0;
285 g_main = tealet_new();
286 t1 = tealet_new();
287 res = tealet_fill(t1, simple_exit);
288 assert(res == TEALET_OK);
289 step(1);
290 tmain = tealet_switch(g_main, t1);
291 step(3);
292 assert(tmain == g_main);
293 tealet_delete(g_main);
294 step(4);
295 }
296
297 /************************************************************/
298
299 static tealet_t *g_other;
300
301 static tealet_t *three_run_1(tealet_t *t1)
302 {
303 assert(t1 != g_main);
304 assert(t1 != g_other);
305 step(2);
306 tealet_delete(t1);
307 return g_other;
308 }
309
310 static tealet_t *three_run_2(tealet_t *t2)
311 {
312 assert(t2 == g_other);
313 step(3);
314 tealet_delete(t2);
315 return g_main;
316 }
317
318 void test_three_tealets(void)
319 {
320 tealet_t *t1, *t2, *tmain;
321 int res;
322
323 status = 0;
324 g_main = tealet_new();
325 t1 = tealet_new();
326 t2 = tealet_new();
327 res = tealet_fill(t1, three_run_1);
328 assert(res == TEALET_OK);
329 res = tealet_fill(t2, three_run_2);
330 assert(res == TEALET_OK);
331 step(1);
332 g_other = t2;
333 tmain = tealet_switch(g_main, t1);
334 step(4);
335 assert(tmain == g_main);
336 tealet_delete(g_main);
337 step(5);
338 }
339
340 /************************************************************/
341
342 static tealet_t *glob_t1;
343 static tealet_t *glob_t2;
344
345 tealet_t *test_switch_2(tealet_t *t2)
346 {
347 assert(t2 != g_main);
348 assert(t2 != glob_t1);
349 glob_t2 = t2;
350
351 step(2);
352 t2 = tealet_switch(glob_t2, glob_t1);
353 assert(t2 == glob_t2);
354
355 step(4);
356 assert(glob_t1->suspended == 1);
357 t2 = tealet_switch(glob_t2, glob_t1);
358 assert(t2 == glob_t2);
359
360 step(6);
361 assert(glob_t1->suspended == 0);
362 t2 = tealet_switch(glob_t2, glob_t1);
363 assert(t2 == glob_t1);
364 printf("ok!\n");
365
366 return g_main;
367 }
368
369 tealet_t *test_switch_1(tealet_t *t1)
370 {
371 tealet_t *t2 = tealet_new();
372 assert(t1 != g_main);
373 tealet_fill(t2, test_switch_2);
374 glob_t1 = t1;
375
376 step(1);
377 t1 = tealet_switch(glob_t1, t2);
378 assert(t1 == glob_t1);
379 assert(t2 == glob_t2);
380
381 step(3);
382 t1 = tealet_switch(glob_t1, t2);
383 assert(t1 == glob_t1);
384 assert(t2 == glob_t2);
385
386 step(5);
387 return t2;
388 }
389
390 void test_switch(void)
391 {
392 int res;
393 tealet_t *t, *t2;
394
395 g_main = tealet_new();
396 status = 0;
397 t = tealet_new();
398 res = tealet_fill(t, test_switch_1);
399 assert(res == TEALET_OK);
400 t2 = tealet_switch(g_main, t);
401 assert(!TEALET_ERROR(t2));
402
403 step(7);
404 tealet_delete(g_main);
405 tealet_delete(glob_t1);
406 tealet_delete(glob_t2);
407 }
408
409 /************************************************************/
410
411 #define ARRAYSIZE 127
412 #define MAX_STATUS 50000
413
414 static tealet_t *tealetarray[ARRAYSIZE] = {NULL};
415 static int got_index;
416
417 tealet_t *random_new_tealet(tealet_t*);
418
419 static void random_run(tealet_t* cur, int index)
420 {
421 int i, prevstatus;
422 tealet_t *t, *tres;
423 assert(tealetarray[index] == cur);
424 do
425 {
426 i = rand() % (ARRAYSIZE + 1);
427 status += 1;
428 if (i == ARRAYSIZE)
429 break;
430 prevstatus = status;
431 got_index = i;
432 if (tealetarray[i] == NULL)
433 {
434 if (status >= MAX_STATUS)
435 break;
436 t = tealet_new();
437 tealet_fill(t, random_new_tealet);
438 t->data = (void*)(ptrdiff_t)i;
439 }
440 else
441 {
442 t = tealetarray[i];
443 }
444 tres = tealet_switch(cur, t);
445 assert(tres == cur);
446
447 assert(status >= prevstatus);
448 assert(tealetarray[index] == cur);
449 assert(got_index == index);
450 }
451 while (status < MAX_STATUS);
452 }
453
454 tealet_t *random_new_tealet(tealet_t* cur)
455 {
456 int i = got_index;
457 assert(i == (ptrdiff_t)(cur->data));
458 assert(i > 0 && i < ARRAYSIZE);
459 assert(tealetarray[i] == NULL);
460 tealetarray[i] = cur;
461 random_run(cur, i);
462 tealetarray[i] = NULL;
463 tealet_delete(cur);
464
465 i = rand() % ARRAYSIZE;
466 if (tealetarray[i] == NULL)
467 {
468 assert(tealetarray[0] != NULL);
469 i = 0;
470 }
471 got_index = i;
472 return tealetarray[i];
473 }
474
475 void test_random(void)
476 {
477 int i;
478 g_main = tealet_new();
479 for( i=0; i<ARRAYSIZE; i++)
480 tealetarray[i] = NULL;
481 tealetarray[0] = g_main;
482 status = 0;
483 while (status < MAX_STATUS)
484 random_run(g_main, 0);
485
486 assert(g_main == tealetarray[0]);
487 for (i=1; i<ARRAYSIZE; i++)
488 while (tealetarray[i] != NULL)
489 random_run(g_main, 0);
490
491 tealet_delete(g_main);
492 }
493
494 /************************************************************/
495
496 tealet_t *test_double_run(tealet_t *current)
497 {
498 double d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, *numbers;
499 numbers = (double *)current->data;
500 d0 = numbers[0] + 1 / 1.0;
501 d1 = numbers[1] + 1 / 2.0;
502 d2 = numbers[2] + 1 / 4.0;
503 d3 = numbers[3] + 1 / 8.0;
504 d4 = numbers[4] + 1 / 16.0;
505 d5 = numbers[5] + 1 / 32.0;
506 d6 = numbers[6] + 1 / 64.0;
507 d7 = numbers[7] + 1 / 128.0;
508 d8 = numbers[8] + 1 / 256.0;
509 d9 = numbers[9] + 1 / 512.0;
510 numbers[0] = d0;
511 numbers[1] = d1;
512 numbers[2] = d2;
513 numbers[3] = d3;
514 numbers[4] = d4;
515 numbers[5] = d5;
516 numbers[6] = d6;
517 numbers[7] = d7;
518 numbers[8] = d8;
519 numbers[9] = d9;
520 tealet_delete(current);
521 return g_main;
522 }
523
524 void test_double(void)
525 {
526 int i;
527 double d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, numbers[10];
528 g_main = tealet_new();
529
530 d0 = d1 = d2 = d3 = d4 = d5 = d6 = d7 = d8 = d9 = 0.0;
531 for (i=0; i<10; i++)
532 numbers[i] = 0.0;
533
534 for (i=0; i<99; i++)
535 {
536 tealet_t *t = tealet_new();
537 tealet_t *tres;
538 tealet_fill(t, test_double_run);
539 t->data = numbers;
540 tres = tealet_switch(g_main, t);
541 assert(tres == g_main);
542 d0 += numbers[0];
543 d1 += numbers[1];
544 d2 += numbers[2];
545 d3 += numbers[3];
546 d4 += numbers[4];
547 d5 += numbers[5];
548 d6 += numbers[6];
549 d7 += numbers[7];
550 d8 += numbers[8];
551 d9 += numbers[9];
552 }
553
554 assert(d0 == 4950.0 / 1.0);
555 assert(d1 == 4950.0 / 2.0);
556 assert(d2 == 4950.0 / 4.0);
557 assert(d3 == 4950.0 / 8.0);
558 assert(d4 == 4950.0 / 16.0);
559 assert(d5 == 4950.0 / 32.0);
560 assert(d6 == 4950.0 / 64.0);
561 assert(d7 == 4950.0 / 128.0);
562 assert(d8 == 4950.0 / 256.0);
563 assert(d9 == 4950.0 / 512.0);
564 tealet_delete(g_main);
565 }
566
567 /************************************************************/
568
569 static tealet_t *g_main2, *g_sub, *g_sub2;
570
571 tealet_t *test_two_mains_green(tealet_t *current)
572 {
573 tealet_t *tres;
574 assert(current == g_sub2);
575
576 step(3); printf("3 G: M1 [S1] M2 [S2]\n");
577 tres = tealet_switch(g_sub, g_main);
578 assert(tres == g_sub);
579
580 step(6); printf("6 G: M1 [S1] [M2] S2\n");
581 return g_sub2;
582 }
583
584 tealet_t *test_two_mains_red(tealet_t *current)
585 {
586 tealet_t *tres;
587 assert(current == g_sub);
588
589 step(2); printf("2 R: M1 [S1] [M2] S2\n");
590 tres = tealet_switch(g_main2, g_sub2);
591 assert(tres == g_main2);
592
593 step(5); printf("5 R: [M1] S1 [M2] S2\n");
594 return g_sub;
595 }
596
597 void test_two_mains(void)
598 {
599 int res;
600 tealet_t *tres;
601
602 status = 0;
603 g_main = tealet_new();
604 g_main2 = tealet_new();
605 g_sub = tealet_new();
606 g_sub2 = tealet_new();
607 res = tealet_fill(g_sub, test_two_mains_red);
608 assert(res == TEALET_OK);
609 res = tealet_fill(g_sub2, test_two_mains_green);
610 assert(res == TEALET_OK);
611
612 step(1); printf("1 W: [M1] S1 [M2] S2\n");
613 tres = tealet_switch(g_main, g_sub);
614 assert(tres == g_main);
615
616 step(4); printf("4 W: [M1] S1 M2 [S2]\n");
617 tres = tealet_switch(g_sub2, g_main2);
618 assert(tres == g_sub2);
619
620 step(7); printf("7 W: M1 [S1] M2 [S2]\n");
621
622 tealet_delete(g_main);
623 tealet_delete(g_main2);
624 tealet_delete(g_sub);
625 tealet_delete(g_sub2);
626 }
627 #endif
628 /************************************************************/
629
630 #define TEST(name) { name, #name }
631
632 typedef struct {
633 void (*runtest)(void);
634 const char *name;
635 } test_t;
636
637 static test_t test_list[] = {
638 TEST(test_new),
639 TEST(test_simple_switch),
640 TEST(test_various_depths),
641 #if 0
642 TEST(test_new_pending),
643 TEST(test_not_switched),
644 TEST(test_simple),
645 TEST(test_exit),
646 TEST(test_three_tealets),
647 TEST(test_two_mains),
648 TEST(test_switch),
649 TEST(test_double),
650 TEST(test_random),
651 #endif
652 { NULL, NULL }
653 };
654
655
656 int main(int argc, char **argv)
657 {
658 test_t *tst;
659 if (argc > 1)
660 srand(atoi(argv[1]));
661
662 thrd = stacklet_newthread();
663 for (tst=test_list; tst->runtest; tst++)
664 {
665 printf("+++ Running %s... +++\n", tst->name);
666 tst->runtest();
667 }
668 stacklet_deletethread(thrd);
669 printf("+++ All ok. +++\n");
670 return 0;
671 }