Debian Codegraph @

src/test-vmstack.c

raw
1 /*
2 * Test VM stack sanity
3 */
4
5 #include <stdio.h>
6 #include "gauche.h"
7 #include "gauche/priv/configP.h"
8 #include "gauche/vm.h"
9 #include "gauche/priv/vmP.h"
10
11 int errcount = 0;
12
13 void message(FILE *out, const char *m, int filler)
14 {
15 int i;
16 fprintf(out, "%s", m);
17 if (filler) {
18 int len = 79 - (int)strlen(m);
19 if (len < 0) len = 5;
20 for (i=0; i<len; i++) putc(filler, out);
21 }
22 putc('\n', out);
23 }
24
25 int is_valid_stack_outcome(ScmObj *pre_sp, ScmVM *post_vm)
26 {
27 ScmObj *post_sp = post_vm->sp;
28
29 if (pre_sp == post_sp) return TRUE;
30
31 /* Boundary cont frame pushes ScmPromptData before it. After boundary
32 cont frame is popped, ScmPromptData still remains. (It will eventually
33 be GC-ed when active frames get moved to the heap.)
34 */
35 if (pre_sp + (sizeof(ScmPromptData)/sizeof(ScmObj)) == post_sp)
36 return TRUE;
37
38 /* Stack has been emptied. */
39 if (post_sp == post_vm->stack) return TRUE;
40
41 return FALSE;
42 }
43
44 void test_eval(const char *msg, const char *sexp)
45 {
46 ScmVM *vm = Scm_VM();
47 ScmObj *pre_sp = vm->sp;
48 ScmObj x = Scm_ReadFromCString(sexp);
49 printf("%s ... ", msg);
50 Scm_Eval(x, SCM_UNBOUND, NULL); /* ignore errors */
51
52 if (!is_valid_stack_outcome(pre_sp, vm)) {
53 printf("ERROR\n");
54 errcount++;
55 } else {
56 printf("ok\n");
57 }
58 }
59
60 ScmObj dummy_eproc(ScmObj *args SCM_UNUSED,
61 int nargs SCM_UNUSED,
62 void *data SCM_UNUSED)
63 {
64 return SCM_UNDEFINED;
65 }
66
67 int main(int argc SCM_UNUSED, char **argv SCM_UNUSED)
68 {
69 ScmObj eproc;
70 const char *testmsg = "Testing VM stack sanity... ";
71
72 fprintf(stderr, "%-65s", testmsg);
73 message(stdout, testmsg, '=');
74 Scm_Init(GAUCHE_SIGNATURE);
75
76 eproc = Scm_MakeSubr(dummy_eproc, NULL, 0, 1, SCM_FALSE);
77 Scm_VM()->customErrorReporter = eproc;
78
79 test_eval("simple expression", "(+ 1 2 3)");
80 test_eval("with-error-handler (1)",
81 "(with-error-handler (lambda (e) #f) (lambda () 1)))");
82 test_eval("with-error-handler (2)",
83 "(with-error-handler (lambda (e) #f) (lambda () (car 1))))");
84 test_eval("with-error-handler (3)",
85 "(car 3)");
86
87 if (errcount) {
88 fprintf(stderr, "failed.\n");
89 fprintf(stdout, "failed.\n");
90 return 1;
91 } else {
92 fprintf(stderr, "passed.\n");
93 fprintf(stdout, "passed.\n");
94 return 0;
95 }
96 }