summaryrefslogtreecommitdiff
path: root/translate/grt/config/teststack.c
blob: 6a6966d6f444eedfb0ee3a65eea737c4242ebd55 (plain)
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
#include <stdlib.h>
#include <stdio.h>

extern void grt_stack_init (void);
extern void grt_stack_switch (void *from, void *to);
extern void *grt_stack_create (void (*func)(void *), void *arg);

int stack_size = 4096;
int stack_max_size = 8 * 4096;

static void *stack1;
static void *stack2;
void *grt_stack_main_stack;

void *grt_cur_proc;

static int step;

void
grt_overflow_error (void)
{
  abort ();
}

void
grt_stack_error_null_access (void)
{
  abort ();
}

void
grt_stack_error_memory_access (void)
{
  abort ();
}

void
grt_stack_error_grow_failed (void)
{
  abort ();
}

void
error (void)
{
  printf ("Test failure at step %d\n", step);
  fflush (stdout);
  exit (1);
}

static void
func1 (void *ptr)
{
  if (ptr != (void *)1)
    error ();

  if (step != 0)
    error ();

  step = 1;

  grt_stack_switch (grt_stack_main_stack, stack1);

  if (step != 5)
    error ();

  step = 6;

  grt_stack_switch (grt_stack_main_stack, stack1);

  if (step != 7)
    error ();

  step = 8;

  grt_stack_switch (stack2, stack1);

  if (step != 9)
    error ();

  step = 10;

  grt_stack_switch (grt_stack_main_stack, stack1);

  error ();
}

static void
func2 (void *ptr)
{
  if (ptr != (void *)2)
    error ();

  if (step == 11)
    {
      step = 12;

      grt_stack_switch (grt_stack_main_stack, stack2);

      error ();
    }

  if (step != 1)
    error ();

  step = 2;

  grt_stack_switch (grt_stack_main_stack, stack2);

  if (step != 3)
    error ();

  step = 4;

  grt_stack_switch (grt_stack_main_stack, stack2);

  if (step != 8)
    error ();

  step = 9;

  grt_stack_switch (stack1, stack2);
}

int
main (void)
{
  grt_stack_init ();

  stack1 = grt_stack_create (&func1, (void *)1);
  stack2 = grt_stack_create (&func2, (void *)2);

  step = 0;
  grt_stack_switch (stack1, grt_stack_main_stack);

  if (step != 1)
    error ();

  grt_stack_switch (stack2, grt_stack_main_stack);

  if (step != 2)
    error ();

  step = 3;

  grt_stack_switch (stack2, grt_stack_main_stack);

  if (step != 4)
    error ();

  step = 5;

  grt_stack_switch (stack1, grt_stack_main_stack);

  if (step != 6)
    error ();

  step = 7;

  grt_stack_switch (stack1, grt_stack_main_stack);

  if (step != 10)
    error ();

  step = 11;

  grt_stack_switch (stack2, grt_stack_main_stack);

  if (step != 12)
    error ();

  printf ("Test successful\n");
  return 0;
}