-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit-test.cpp
138 lines (115 loc) · 4.47 KB
/
unit-test.cpp
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
#include "vm.h"
#include "minunit.h"
#include "reader.h"
#include "compile.h"
#include "value_helpers.h"
#include "lib.h"
#include <stdio.h>
#include <stdlib.h>
int tests_run = 0;
// (eq (< 2 1) 't)
// Tests are of form static char *test_foo() ...
// Run them with mu_run_test(func_name)
// Use mu_assert(msg, bool) to do assert tests. Returns on error
///////////////////////// VM ////////////////////////////////////
static char const * test_vm_start()
{
printf("///////////////////////// VM ////////////////////////////////////\n");
vm_t *vm = vm_create(1024, NULL);
mu_assert("vm is NULL", vm != NULL);
vm_destroy(vm);
return 0;
}
//////////////////////// LAMBDA /////////////////////////////////
static char const *test_lambda_basic()
{
printf("//////////////////////// LAMBDA /////////////////////////////////\n");
vm_t *vm = vm_create(1024, NULL);
lib_init(vm);
stream_t *strm = stream_create("(lambda () (+ 1 2))");
reader(vm, strm, false);
value_t *rd = vm->m_stack[vm->m_sp - 1];
eval(vm, rd);
mu_assert("(lambda () (+ 1 2)) -> Did not have a vm->m_sp of 1\n", vm->m_sp == 1);
mu_assert("(lambda () (+ 1 2)) -> Did not return a closure\n", is_closure(vm, vm->m_stack[vm->m_sp - 1]));
vm_destroy(vm);
return 0;
}
//////////////////////// FUNCALL /////////////////////////////////
static char const *test_funcall_basic()
{
printf("//////////////////////// FUNCALL /////////////////////////////////\n");
vm_t *vm = vm_create(1024, NULL);
lib_init(vm);
stream_t *strm = stream_create("(funcall (lambda () (+ 1 3)))");
reader(vm, strm, false);
value_t *rd = vm->m_stack[vm->m_sp - 1];
eval(vm, rd);
mu_assert("(funcall (lambda () (+ 1 3))) -> Did not have a vm->m_sp of 1\n", vm->m_sp == 1);
mu_assert("(funcall (lambda () (+ 1 3))) -> Did not return a fixnum\n", is_fixnum(vm->m_stack[vm->m_sp - 1]));
mu_assert("(funcall (lambda () (+ 1 3))) -> Did not return 4\n", to_fixnum(vm->m_stack[vm->m_sp - 1]) == 4);
vm->m_sp = 0;
strm = stream_create("(funcall (lambda () (+ 1 3) (+ 1 1)))");
reader(vm, strm, false);
rd = vm->m_stack[vm->m_sp - 1];
eval(vm, rd);
mu_assert("(funcall (lambda () (+ 1 3) (+ 1 1))) -> Did not have a vm->m_sp of 1\n", vm->m_sp == 1);
mu_assert("(funcall (lambda () (+ 1 3) (+ 1 1))) -> Did not return a fixnum\n", is_fixnum(vm->m_stack[vm->m_sp - 1]));
mu_assert("(funcall (lambda () (+ 1 3) (+ 1 1))) -> Did not return 2\n", to_fixnum(vm->m_stack[vm->m_sp - 1]) == 2);
vm->m_sp = 0;
vm_destroy(vm);
return 0;
}
//////////////////////// PROGN /////////////////////////////////
static char const *test_progn_basic()
{
printf("//////////////////////// PROGN /////////////////////////////////\n");
vm_t *vm = vm_create(1024, NULL);
lib_init(vm);
stream_t *strm = stream_create("(progn (+ 1 3))");
reader(vm, strm, false);
value_t *rd = vm->m_stack[vm->m_sp - 1];
eval(vm, rd);
printf("sp: %lu\n", vm->m_sp);
mu_assert("(progn (+ 1 3)) -> Did not have a vm->m_sp of 1\n", vm->m_sp == 1);
mu_assert("(progn (+ 1 3)) -> Did not return a fixnum\n", is_fixnum(vm->m_stack[vm->m_sp - 1]));
mu_assert("(progn (+ 1 3)) -> Did not return 4\n", to_fixnum(vm->m_stack[vm->m_sp - 1]) == 4);
vm->m_sp = 0;
strm = stream_create("(progn (+ 1 3) (+ 1 1))");
reader(vm, strm, false);
rd = vm->m_stack[vm->m_sp - 1];
eval(vm, rd);
mu_assert("(progn (+ 1 3) (+ 1 1)) -> Did not have a vm->m_sp of 1\n", vm->m_sp == 1);
mu_assert("(progn (+ 1 3) (+ 1 1)) -> Did not return a fixnum\n", is_fixnum(vm->m_stack[vm->m_sp - 1]));
mu_assert("(progn (+ 1 3) (+ 1 1)) -> Did not return 2\n", to_fixnum(vm->m_stack[vm->m_sp - 1]) == 2);
vm->m_sp = 0;
strm = stream_create("(progn (progn (progn (+ 1 3)) (+ 1 2)))");
reader(vm, strm, false);
rd = vm->m_stack[vm->m_sp - 1];
eval(vm, rd);
mu_assert("(progn (progn (progn (+ 1 3)) (+ 1 2))) -> Did not have a vm->m_sp of 1\n", vm->m_sp == 1);
mu_assert("(progn (progn (progn (+ 1 3)) (+ 1 2))) -> Did not return a fixnum\n", is_fixnum(vm->m_stack[vm->m_sp - 1]));
mu_assert("(progn (progn (progn (+ 1 3)) (+ 1 2))) -> Did not return 3\n", to_fixnum(vm->m_stack[vm->m_sp - 1]) == 3);
vm->m_sp = 0;
vm_destroy(vm);
return 0;
}
//////////////////////// ENV /////////////////////////////////
static char const * all_tests()
{
mu_run_test(test_vm_start);
mu_run_test(test_lambda_basic);
mu_run_test(test_funcall_basic);
mu_run_test(test_progn_basic);
return 0;
}
void unit_test()
{
g_debug_display = true;
char const *result = all_tests();
if (result != 0) {
printf("%s\n", result);
} else {
printf("ALL TESTS PASS\n");
}
}