Source

pypy / pypy / translator / c / src / stacklet / tests.c

The branch 'continulet-jit' does not exist.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
#include "stacklet.h"


static stacklet_thread_handle thrd;

/************************************************************/

stacklet_handle empty_callback(stacklet_handle h, void *arg)
{
  assert(arg == (void *)123);
  return h;
}

void test_new(void)
{
  stacklet_handle h = stacklet_new(thrd, empty_callback, (void *)123);
  assert(h == EMPTY_STACKLET_HANDLE);
}

/************************************************************/

static int status;

stacklet_handle switchbackonce_callback(stacklet_handle h, void *arg)
{
  assert(arg == (void *)123);
  assert(status == 0);
  status = 1;
  assert(h != EMPTY_STACKLET_HANDLE);
  h = stacklet_switch(thrd, h);
  assert(status == 2);
  assert(h != EMPTY_STACKLET_HANDLE);
  status = 3;
  return h;
}

void test_simple_switch(void)
{
  status = 0;
  stacklet_handle h = stacklet_new(thrd, switchbackonce_callback, (void *)123);
  assert(h != EMPTY_STACKLET_HANDLE);
  assert(status == 1);
  status = 2;
  h = stacklet_switch(thrd, h);
  assert(status == 3);
  assert(h == EMPTY_STACKLET_HANDLE);
}

/************************************************************/

static stacklet_handle handles[10];
static int nextstep, comefrom, gointo;
static const int statusmax = 5000;

int withdepth(int self, float d);

stacklet_handle variousdepths_callback(stacklet_handle h, void *arg)
{
  int self, n;
  assert(nextstep == status);
  nextstep = -1;
  self = (ptrdiff_t)arg;
  assert(self == gointo);
  assert(0 <= self && self < 10);
  assert(handles[self] == NULL);
  assert(0 <= comefrom && comefrom < 10);
  assert(handles[comefrom] == NULL);
  assert(h != NULL && h != EMPTY_STACKLET_HANDLE);
  handles[comefrom] = h;
  comefrom = -1;
  gointo = -1;

  while (withdepth(self, rand() % 20) == 0)
    ;

  assert(handles[self] == NULL);

  do {
    n = rand() % 10;
  } while (handles[n] == NULL);

  h = handles[n];
  assert(h != EMPTY_STACKLET_HANDLE);
  handles[n] = NULL;
  comefrom = -42;
  gointo = n;
  assert(nextstep == -1);
  nextstep = ++status;
  //printf("LEAVING %d to go to %d\n", self, n);
  return h;
}

typedef struct foo_s {
  int self;
  float d;
  struct foo_s *next;
} foo_t;

int withdepth(int self, float d)
{
  int res = 0;
  if (d > 0.0)
    {
      foo_t *foo = malloc(sizeof(foo_t));
      foo_t *foo2 = malloc(sizeof(foo_t));
      foo->self = self;
      foo->d = d;
      foo->next = foo2;
      foo2->self = self + 100;
      foo2->d = d;
      foo2->next = NULL;
      res = withdepth(self, d - 1.1);
      assert(foo->self == self);
      assert(foo->d    == d);
      assert(foo->next == foo2);
      assert(foo2->self == self + 100);
      assert(foo2->d    == d);
      assert(foo2->next == NULL);
      free(foo2);
      free(foo);
    }
  else
    {
      stacklet_handle h;
      int n = rand() % 10;
      if (n == self || (status >= statusmax && handles[n] == NULL))
        return 1;

      //printf("status == %d, self = %d\n", status, self);
      assert(handles[self] == NULL);
      assert(nextstep == -1);
      nextstep = ++status;
      comefrom = self;
      gointo = n;
      if (handles[n] == NULL)
        {
          /* start a new stacklet */
          //printf("new %d\n", n);
          h = stacklet_new(thrd, variousdepths_callback, (void *)(ptrdiff_t)n);
        }
      else
        {
          /* switch to this stacklet */
          //printf("switch to %d\n", n);
          h = handles[n];
          handles[n] = NULL;
          h = stacklet_switch(thrd, h);
        }
      //printf("back in self = %d, coming from %d\n", self, comefrom);
      assert(nextstep == status);
      nextstep = -1;
      assert(gointo == self);
      assert(comefrom != self);
      assert(handles[self] == NULL);
      if (comefrom != -42)
        {
          assert(0 <= comefrom && comefrom < 10);
          assert(handles[comefrom] == NULL);
          handles[comefrom] = h;
        }
      else
        assert(h == EMPTY_STACKLET_HANDLE);
      comefrom = -1;
      gointo = -1;
    }
  assert((res & (res-1)) == 0);   /* to prevent a tail-call to withdepth() */
  return res;
}

int any_alive(void)
{
  int i;
  for (i=0; i<10; i++)
    if (handles[i] != NULL)
      return 1;
  return 0;
}

void test_various_depths(void)
{
  int i;
  for (i=0; i<10; i++)
    handles[i] = NULL;

  nextstep = -1;
  comefrom = -1;
  status = 0;
  while (status < statusmax || any_alive())
    withdepth(0, rand() % 50);
}

/************************************************************/
#if 0

static tealet_t *runner1(tealet_t *cur)
{
  abort();
}

void test_new_pending(void)
{
  tealet_t *g1 = tealet_new();
  tealet_t *g2 = tealet_new();
  int r1 = tealet_fill(g1, runner1);
  int r2 = tealet_fill(g2, runner1);
  assert(r1 == TEALET_OK);
  assert(r2 == TEALET_OK);
  assert(g1->suspended == 1);
  assert(g2->suspended == 1);
  tealet_delete(g1);
  tealet_delete(g2);
}

/************************************************************/

void test_not_switched(void)
{
  tealet_t *g1 = tealet_new();
  tealet_t *g2 = tealet_new();
  tealet_t *g3 = tealet_switch(g2, g1);
  assert(!TEALET_ERROR(g3));
  assert(g3 == g1);
  tealet_delete(g1);
  tealet_delete(g2);
}

/************************************************************/

static tealet_t *g_main;

static void step(int newstatus)
{
  assert(status == newstatus - 1);
  status = newstatus;
}

static tealet_t *simple_run(tealet_t *t1)
{
  assert(t1 != g_main);
  step(2);
  tealet_delete(t1);
  return g_main;
}

void test_simple(void)
{
  tealet_t *t1, *tmain;
  int res;

  status = 0;
  g_main = tealet_new();
  t1 = tealet_new();
  res = tealet_fill(t1, simple_run);
  assert(res == TEALET_OK);
  step(1);
  tmain = tealet_switch(g_main, t1);
  step(3);
  assert(tmain == g_main);
  tealet_delete(g_main);
  step(4);
}

/************************************************************/

static tealet_t *simple_exit(tealet_t *t1)
{
  int res;
  assert(t1 != g_main);
  step(2);
  tealet_delete(t1);
  res = tealet_exit_to(g_main);
  assert(!"oups");
}

void test_exit(void)
{
  tealet_t *t1, *tmain;
  int res;

  status = 0;
  g_main = tealet_new();
  t1 = tealet_new();
  res = tealet_fill(t1, simple_exit);
  assert(res == TEALET_OK);
  step(1);
  tmain = tealet_switch(g_main, t1);
  step(3);
  assert(tmain == g_main);
  tealet_delete(g_main);
  step(4);
}

/************************************************************/

static tealet_t *g_other;

static tealet_t *three_run_1(tealet_t *t1)
{
  assert(t1 != g_main);
  assert(t1 != g_other);
  step(2);
  tealet_delete(t1);
  return g_other;
}

static tealet_t *three_run_2(tealet_t *t2)
{
  assert(t2 == g_other);
  step(3);
  tealet_delete(t2);
  return g_main;
}

void test_three_tealets(void)
{
  tealet_t *t1, *t2, *tmain;
  int res;

  status = 0;
  g_main = tealet_new();
  t1 = tealet_new();
  t2 = tealet_new();
  res = tealet_fill(t1, three_run_1);
  assert(res == TEALET_OK);
  res = tealet_fill(t2, three_run_2);
  assert(res == TEALET_OK);
  step(1);
  g_other = t2;
  tmain = tealet_switch(g_main, t1);
  step(4);
  assert(tmain == g_main);
  tealet_delete(g_main);
  step(5);
}

/************************************************************/

static tealet_t *glob_t1;
static tealet_t *glob_t2;

tealet_t *test_switch_2(tealet_t *t2)
{
  assert(t2 != g_main);
  assert(t2 != glob_t1);
  glob_t2 = t2;

  step(2);
  t2 = tealet_switch(glob_t2, glob_t1);
  assert(t2 == glob_t2);

  step(4);
  assert(glob_t1->suspended == 1);
  t2 = tealet_switch(glob_t2, glob_t1);
  assert(t2 == glob_t2);

  step(6);
  assert(glob_t1->suspended == 0);
  t2 = tealet_switch(glob_t2, glob_t1);
  assert(t2 == glob_t1);
  printf("ok!\n");

  return g_main;
}

tealet_t *test_switch_1(tealet_t *t1)
{
  tealet_t *t2 = tealet_new();
  assert(t1 != g_main);
  tealet_fill(t2, test_switch_2);
  glob_t1 = t1;

  step(1);
  t1 = tealet_switch(glob_t1, t2);
  assert(t1 == glob_t1);
  assert(t2 == glob_t2);

  step(3);
  t1 = tealet_switch(glob_t1, t2);
  assert(t1 == glob_t1);
  assert(t2 == glob_t2);

  step(5);
  return t2;
}

void test_switch(void)
{
  int res;
  tealet_t *t, *t2;

  g_main = tealet_new();
  status = 0;
  t = tealet_new();
  res = tealet_fill(t, test_switch_1);
  assert(res == TEALET_OK);
  t2 = tealet_switch(g_main, t);
  assert(!TEALET_ERROR(t2));

  step(7);
  tealet_delete(g_main);
  tealet_delete(glob_t1);
  tealet_delete(glob_t2);
}

/************************************************************/

#define ARRAYSIZE  127
#define MAX_STATUS 50000

static tealet_t *tealetarray[ARRAYSIZE] = {NULL};
static int got_index;

tealet_t *random_new_tealet(tealet_t*);

static void random_run(tealet_t* cur, int index)
{
  int i, prevstatus;
  tealet_t *t, *tres;
  assert(tealetarray[index] == cur);
  do
    {
      i = rand() % (ARRAYSIZE + 1);
      status += 1;
      if (i == ARRAYSIZE)
        break;
      prevstatus = status;
      got_index = i;
      if (tealetarray[i] == NULL)
        {
          if (status >= MAX_STATUS)
            break;
          t = tealet_new();
          tealet_fill(t, random_new_tealet);
          t->data = (void*)(ptrdiff_t)i;
        }
      else
        {
          t = tealetarray[i];
        }
      tres = tealet_switch(cur, t);
      assert(tres == cur);

      assert(status >= prevstatus);
      assert(tealetarray[index] == cur);
      assert(got_index == index);
    }
  while (status < MAX_STATUS);
}

tealet_t *random_new_tealet(tealet_t* cur)
{
  int i = got_index;
  assert(i == (ptrdiff_t)(cur->data));
  assert(i > 0 && i < ARRAYSIZE);
  assert(tealetarray[i] == NULL);
  tealetarray[i] = cur;
  random_run(cur, i);
  tealetarray[i] = NULL;
  tealet_delete(cur);

  i = rand() % ARRAYSIZE;
  if (tealetarray[i] == NULL)
    {
      assert(tealetarray[0] != NULL);
      i = 0;
    }
  got_index = i;
  return tealetarray[i];
}

void test_random(void)
{
  int i;
  g_main = tealet_new();
  for( i=0; i<ARRAYSIZE; i++)
      tealetarray[i] = NULL;
  tealetarray[0] = g_main;
  status = 0;
  while (status < MAX_STATUS)
    random_run(g_main, 0);

  assert(g_main == tealetarray[0]);
  for (i=1; i<ARRAYSIZE; i++)
    while (tealetarray[i] != NULL)
      random_run(g_main, 0);

  tealet_delete(g_main);
}

/************************************************************/

tealet_t *test_double_run(tealet_t *current)
{
  double d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, *numbers;
  numbers = (double *)current->data;
  d0 = numbers[0] + 1 / 1.0;
  d1 = numbers[1] + 1 / 2.0;
  d2 = numbers[2] + 1 / 4.0;
  d3 = numbers[3] + 1 / 8.0;
  d4 = numbers[4] + 1 / 16.0;
  d5 = numbers[5] + 1 / 32.0;
  d6 = numbers[6] + 1 / 64.0;
  d7 = numbers[7] + 1 / 128.0;
  d8 = numbers[8] + 1 / 256.0;
  d9 = numbers[9] + 1 / 512.0;
  numbers[0] = d0;
  numbers[1] = d1;
  numbers[2] = d2;
  numbers[3] = d3;
  numbers[4] = d4;
  numbers[5] = d5;
  numbers[6] = d6;
  numbers[7] = d7;
  numbers[8] = d8;
  numbers[9] = d9;
  tealet_delete(current);
  return g_main;
}

void test_double(void)
{
  int i;
  double d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, numbers[10];
  g_main = tealet_new();

  d0 = d1 = d2 = d3 = d4 = d5 = d6 = d7 = d8 = d9 = 0.0;
  for (i=0; i<10; i++)
    numbers[i] = 0.0;

  for (i=0; i<99; i++)
    {
      tealet_t *t = tealet_new();
      tealet_t *tres;
      tealet_fill(t, test_double_run);
      t->data = numbers;
      tres = tealet_switch(g_main, t);
      assert(tres == g_main);
      d0 += numbers[0];
      d1 += numbers[1];
      d2 += numbers[2];
      d3 += numbers[3];
      d4 += numbers[4];
      d5 += numbers[5];
      d6 += numbers[6];
      d7 += numbers[7];
      d8 += numbers[8];
      d9 += numbers[9];
    }

  assert(d0 == 4950.0 / 1.0);
  assert(d1 == 4950.0 / 2.0);
  assert(d2 == 4950.0 / 4.0);
  assert(d3 == 4950.0 / 8.0);
  assert(d4 == 4950.0 / 16.0);
  assert(d5 == 4950.0 / 32.0);
  assert(d6 == 4950.0 / 64.0);
  assert(d7 == 4950.0 / 128.0);
  assert(d8 == 4950.0 / 256.0);
  assert(d9 == 4950.0 / 512.0);
  tealet_delete(g_main);
}

/************************************************************/

static tealet_t *g_main2, *g_sub, *g_sub2;

tealet_t *test_two_mains_green(tealet_t *current)
{
  tealet_t *tres;
  assert(current == g_sub2);

  step(3); printf("3 G: M1 [S1]  M2 [S2]\n");
  tres = tealet_switch(g_sub, g_main);
  assert(tres == g_sub);

  step(6); printf("6 G: M1 [S1]  [M2] S2\n");
  return g_sub2;
}

tealet_t *test_two_mains_red(tealet_t *current)
{
  tealet_t *tres;
  assert(current == g_sub);

  step(2); printf("2 R: M1 [S1]  [M2] S2\n");
  tres = tealet_switch(g_main2, g_sub2);
  assert(tres == g_main2);

  step(5); printf("5 R: [M1] S1  [M2] S2\n");
  return g_sub;
}

void test_two_mains(void)
{
  int res;
  tealet_t *tres;

  status = 0;
  g_main = tealet_new();
  g_main2 = tealet_new();
  g_sub = tealet_new();
  g_sub2 = tealet_new();
  res = tealet_fill(g_sub, test_two_mains_red);
  assert(res == TEALET_OK);
  res = tealet_fill(g_sub2, test_two_mains_green);
  assert(res == TEALET_OK);

  step(1); printf("1 W: [M1] S1  [M2] S2\n");
  tres = tealet_switch(g_main, g_sub);
  assert(tres == g_main);

  step(4); printf("4 W: [M1] S1  M2 [S2]\n");
  tres = tealet_switch(g_sub2, g_main2);
  assert(tres == g_sub2);

  step(7); printf("7 W: M1 [S1]  M2 [S2]\n");

  tealet_delete(g_main);
  tealet_delete(g_main2);
  tealet_delete(g_sub);
  tealet_delete(g_sub2);
}
#endif
/************************************************************/

struct test_captr_s {
    long c, c0, c1, c2;
    char **fooref;
    char **fooref0;
    char **fooref1;
    char **fooref2;
    stacklet_handle hmain;
    stacklet_handle h1;
    stacklet_handle h2;
} tid;

int cap_check_all(int depth);

stacklet_handle stacklet_captr_callback_1(stacklet_handle h, void *arg)
{
    tid.hmain = h;
    tid.h1 = NULL;
    assert(status == 0);
    status = 1;

    assert(_stacklet_get_captured_pointer(tid.c) == tid.fooref);
    assert(_stacklet_get_captured_context(tid.c) == NULL);
    assert(*_stacklet_translate_pointer(tid.hmain, tid.fooref) == (char*)-42);

    char *ref1 = (char*)1111;
    tid.c1 = _stacklet_capture_stack_pointer(thrd, &ref1);
    tid.fooref1 = &ref1;
    assert(_stacklet_get_captured_pointer(tid.c1) == &ref1);
    assert(_stacklet_get_captured_context(tid.c1) == NULL);

    h = stacklet_switch(thrd, h);
    tid.hmain = h;
    tid.h1 = NULL;

    cap_check_all(20);

    assert(status == 2);
    status = 3;
    return stacklet_switch(thrd, h);
}

stacklet_handle stacklet_captr_callback_2(stacklet_handle h, void *arg)
{
    tid.hmain = h;
    tid.h2 = NULL;
    assert(status == 1);
    status = 2;

    char *ref2 = (char*)2222;
    tid.c2 = _stacklet_capture_stack_pointer(thrd, &ref2);
    tid.fooref2 = &ref2;
    assert(_stacklet_get_captured_pointer(tid.c2) == &ref2);
    assert(_stacklet_get_captured_context(tid.c2) == NULL);

    h = stacklet_switch(thrd, h);
    tid.hmain = h;
    tid.h2 = NULL;

    cap_check_all(20);

    assert(status == 5);
    status = 6;

    return stacklet_switch(thrd, h);
}

int cap_check_all(int depth)
{
    assert(_stacklet_get_captured_pointer(tid.c) == tid.fooref);
    /* we always get NULL because it's before the portion of the stack
       that is copied away and restored: */
    assert(_stacklet_get_captured_context(tid.c) == NULL);
    assert(*_stacklet_translate_pointer(tid.hmain, tid.fooref) == (char*)-42);

    assert(_stacklet_get_captured_pointer(tid.c0) == tid.fooref0);
    assert(_stacklet_get_captured_context(tid.c) == tid.hmain);
    assert(*_stacklet_translate_pointer(tid.hmain, tid.fooref) == (char*)6363);

    assert(_stacklet_get_captured_pointer(tid.c1) == tid.fooref1);
    assert(_stacklet_get_captured_context(tid.c1) == tid.h1);
    assert(*_stacklet_translate_pointer(tid.h1, tid.fooref1) == (char*)1111);

    assert(_stacklet_get_captured_pointer(tid.c2) == tid.fooref2);
    assert(_stacklet_get_captured_context(tid.c2) == tid.h2);
    assert(*_stacklet_translate_pointer(tid.h2, tid.fooref2) == (char*)2222);

    if (depth > 0)
        return cap_check_all(depth - 1) * depth;
    return 1;
}

int cap_with_extra_stack(int depth)
{
    stacklet_handle h1, h2;
    char *foo0 = (char*)6363;

    if (depth > 0)
        return cap_with_extra_stack(depth - 1) * depth;

    tid.c0 = _stacklet_capture_stack_pointer(thrd, &foo0);
    tid.fooref0 = &foo0;
    assert(tid.c0 == (long)tid.fooref0);
    assert(_stacklet_get_captured_pointer(tid.c0) == &foo0);
    assert(_stacklet_get_captured_context(tid.c0) == NULL);

    cap_check_all(20);
    assert(_stacklet_capture_stack_pointer(thrd, tid.fooref) == tid.c);

    assert(status == 2);
    status = 3;
    h1 = stacklet_switch(thrd, h1);
    tid.hmain = NULL;
    tid.h1 = h1;

    cap_check_all(20);

    assert(status == 4);
    status = 5;
    h2 = stacklet_switch(thrd, h2);
    tid.hmain = NULL;
    tid.h2 = h2;

    cap_check_all(20);
    return 1;
}

void test_stacklet_capture(void)
{
    char *foo = (char*)-42;
    tid.c = _stacklet_capture_stack_pointer(thrd, &foo);
    tid.fooref = &foo;
    assert(tid.c == (long)tid.fooref);
    assert(_stacklet_get_captured_pointer(tid.c) == &foo);
    assert(_stacklet_get_captured_context(tid.c) == NULL);

    status = 0;
    stacklet_handle h1 = stacklet_new(thrd, stacklet_captr_callback_1, NULL);
    stacklet_handle h2 = stacklet_new(thrd, stacklet_captr_callback_2, NULL);
    tid.hmain = NULL;
    tid.h1 = h1;
    tid.h2 = h2;

    cap_with_extra_stack(20);

    assert(status == 6);
    h1 = stacklet_switch(thrd, tid.h1);
    assert(h1 == EMPTY_STACKLET_HANDLE);
    h2 = stacklet_switch(thrd, tid.h2);
    assert(h2 == EMPTY_STACKLET_HANDLE);
}

/************************************************************/

#define TEST(name)   { name, #name }

typedef struct {
  void (*runtest)(void);
  const char *name;
} test_t;

static test_t test_list[] = {
  TEST(test_new),
  TEST(test_simple_switch),
  TEST(test_various_depths),
#if 0
  TEST(test_new_pending),
  TEST(test_not_switched),
  TEST(test_simple),
  TEST(test_exit),
  TEST(test_three_tealets),
  TEST(test_two_mains),
  TEST(test_switch),
  TEST(test_double),
  TEST(test_random),
#endif
  TEST(test_stacklet_capture),
  { NULL, NULL }
};


int main(int argc, char **argv)
{
  test_t *tst;
  if (argc > 1)
    srand(atoi(argv[1]));

  thrd = stacklet_newthread();
  for (tst=test_list; tst->runtest; tst++)
    {
      printf("+++ Running %s... +++\n", tst->name);
      tst->runtest();
    }
  stacklet_deletethread(thrd);
  printf("+++ All ok. +++\n");
  return 0;
}