146 lines4.5 KB
1#include <stdio.h>
2#include <stddef.h>
3#include "coroutine.h"
4#include "generator.h"
5#include "asleep.h"
6#include "task.h"
7
8#include <dirent.h>
9#include <string.h>
10#include <stdlib.h>
11#include <time.h>
12
13#define DEMO_STACK_SIZE (8192*sizeof(void *))
14
15Coroutine *cor_main;
16
17typedef struct {
18 intptr_t headroom;
19 bool canstartcoroutine;
20} TestResult;
21
22void *dotests(
23 void *param
24){
25 (void)param;
26
27 Coroutine_Dump_();
28 Coroutine *active = Coroutine_GetActive();
29 Coroutine *a = Coroutine_New(DEMO_STACK_SIZE, dotests);
30 Coroutine *b = Coroutine_New(DEMO_STACK_SIZE, dotests);
31 Coroutine *c = Coroutine_New(DEMO_STACK_SIZE, dotests);
32 printf("[stack=aabbcc]\n");
33 Coroutine_Dump_();
34 printf("%ld %ld %ld (a, b, c: a < b < c)\n", (unsigned char *)active - (unsigned char *)a, (unsigned char *)active - (unsigned char *)b, (unsigned char *)active - (unsigned char *)c);
35 Coroutine_Delete(b);
36 printf("[stack=aaffcc]\n");
37 Coroutine_Dump_();
38 b = Coroutine_New(DEMO_STACK_SIZE, dotests);
39 printf("[stack=aabbcc]\n");
40 Coroutine_Dump_();
41 printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
42 Coroutine_Delete(b);
43 printf("[stack=aaffcc]\n");
44 Coroutine_Dump_();
45 Coroutine *d = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
46 printf("[stack=aaffccdddd]\n");
47 printf("%ld (d: c < d)\n", (unsigned char *)active - (unsigned char *)d);
48 Coroutine_Dump_();
49 printf("[testing merge before]\n");
50 Coroutine_Delete(c);
51 printf("[stack=aaffffdddd]\n");
52 Coroutine_Dump_();
53 b = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
54 printf("[stack=aabbbbdddd]\n");
55 printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
56 Coroutine_Dump_();
57 printf("[testing merge after]\n");
58 Coroutine_Delete(b);
59 printf("[stack=aaffffdddd]\n");
60 Coroutine_Dump_();
61 Coroutine_Delete(a);
62 printf("[stack=ffffffdddd]\n");
63 Coroutine_Dump_();
64 a = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
65 printf("[stack=aaaaffdddd]\n");
66 printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
67 Coroutine_Dump_();
68 printf("[testing merge both ways]\n");
69 Coroutine_Delete(a);
70 printf("[stack=ffffffdddd]\n");
71 Coroutine_Dump_();
72 a = Coroutine_New(DEMO_STACK_SIZE, dotests);
73 printf("[stack=aaffffdddd]\n");
74 Coroutine_Dump_();
75 printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
76 b = Coroutine_New(DEMO_STACK_SIZE, dotests);
77 printf("[stack=aabbffdddd]\n");
78 Coroutine_Dump_();
79 printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
80 c = Coroutine_New(DEMO_STACK_SIZE, dotests);
81 printf("[stack=aabbccdddd]\n");
82 printf("%ld (c again)\n", (unsigned char *)active - (unsigned char *)c);
83 Coroutine_Delete(a);
84 printf("[stack=ffbbccdddd]\n");
85 Coroutine_Delete(c);
86 printf("[stack=ffbbffdddd]\n");
87 Coroutine_Delete(b);
88 printf("[stack=ffffffdddd]\n");
89 Coroutine_Dump_();
90 a = Coroutine_New(DEMO_STACK_SIZE*3, dotests);
91 printf("[stack=aaaaaadddd]\n");
92 printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
93 Coroutine_Dump_();
94
95 Coroutine_Delete(a);
96 printf("[stack=ffffffdddd]\n");
97 Coroutine_Dump_();
98 Coroutine_Delete(d);
99 printf("[stack=fffffffffff]\n");
100 Coroutine_Dump_();
101 a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
102 printf("[stack=affffffffff]\n");
103 Coroutine_Dump_();
104 b = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
105 printf("[stack=abfffffffff]\n");
106 Coroutine_Dump_();
107 c = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
108 printf("[stack=abcffffffff]\n");
109 Coroutine_Dump_();
110 Coroutine_Delete(a);
111 printf("[stack=fbcffffffff]\n");
112 Coroutine_Dump_();
113 Coroutine_Delete(b);
114 printf("[stack=ffcffffffff]\n");
115 Coroutine_Dump_();
116 a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
117 printf("[stack=afcffffffff]\n");
118 Coroutine_Dump_();
119
120 Coroutine_Delete(c);
121 printf("[stack=affffffffff]\n");
122 Coroutine_Dump_();
123
124 Coroutine_Delete(a);
125 printf("[stack=fffffffffff]\n");
126 Coroutine_Dump_();
127
128 return NULL;
129}
130
131
132int main(int argc, char *argv[]) {
133 (void)argc;
134 (void)argv;
135
136 printf("No StartSystem\n");
137 Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
138
139 printf("\n\n\n\nStartSystem\n");
140 Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
141
142 printf("\n\n\n\nSartSystem with stack limit\n");
143 Coroutine_SetStackLimit((unsigned char *)&argc - 1024*1024);
144 Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
145}
146