1 contributor
150 lines4.7 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_StartSystem();
141 Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
142 Coroutine_StopSystem();
143
144 printf("\n\n\n\nSartSystem with stack limit\n");
145 Coroutine_StartSystem();
146 Coroutine_SetStackLimit((unsigned char *)&argc - 1024*1024);
147 Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
148 Coroutine_StopSystem();
149}
150