-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathyla_vm_test1.c
188 lines (141 loc) · 4.88 KB
/
yla_vm_test1.c
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Virtual machine Test #1
This file is part of YLA VM (Yet another Language for Academic purpose: Virtual Machine).
YLA VM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include "yla_vm.h"
#include "yla_cop.h"
#include "yla_test.h"
#include "yla_test_gencode.h"
static int test_gencode()
{
yla_cop_type buf[100];
yla_cop_type *ptr = buf;
put_commd(&ptr, 0x12);
put_value(&ptr, 0x3456);
put_commd(&ptr, 0x78);
put_commd(&ptr, 0x9a);
YLATEST_ASSERT_TRUE(buf[0] == 0x12, "put_commd");
YLATEST_ASSERT_TRUE(buf[1] == 0x34, "put_value");
YLATEST_ASSERT_TRUE(buf[2] == 0x56, "put_value");
YLATEST_ASSERT_TRUE(buf[3] == 0x78, "put_value");
YLATEST_ASSERT_TRUE(buf[4] == 0x9a, "put_value");
return 0;
}
static int test_init_null()
{
yla_vm vm;
yla_cop_type buf[100];
YLATEST_ASSERT_FALSE(yla_vm_init(&vm, NULL, 100), "init NULL");
YLATEST_ASSERT_TRUE(yla_vm_last_error(&vm)==YLA_VM_ERROR_NO_PROGRAM_CODE, "init NULL");
return 0;
}
static int test_init_0()
{
yla_cop_type code[1];
yla_vm vm;
YLATEST_ASSERT_FALSE(yla_vm_init(&vm, code, 0), "init 0 size code");
YLATEST_ASSERT_TRUE(yla_vm_last_error(&vm)==YLA_VM_ERROR_NO_PROGRAM_CODE, "init 0 size code");
return 0;
}
static int test_init_simple()
{
yla_cop_type prg[HEADER_SIZE + 1];
yla_cop_type *ptr = prg;
put_header(&ptr, 0, 0, 1);
put_commd(&ptr, CHALT);
yla_vm vm;
YLATEST_ASSERT_TRUE(yla_vm_init(&vm, prg, HEADER_SIZE + 1), "normal");
YLATEST_ASSERT_TRUE(yla_vm_do_command(&vm) == -1, "halt expected")
YLATEST_ASSERT_TRUE(yla_vm_done(&vm), "normal");
return 0;
}
static int test_init_simple2()
{
yla_cop_type prg[HEADER_SIZE + 2];
yla_cop_type *ptr = prg;
put_header(&ptr, 0, 0, 2);
put_commd(&ptr, CNOP);
put_commd(&ptr, CHALT);
yla_vm vm;
YLATEST_ASSERT_TRUE(yla_vm_init(&vm, prg, HEADER_SIZE + 2), "normal");
YLATEST_ASSERT_TRUE(yla_vm_do_command(&vm) == 1, "OK expected")
YLATEST_ASSERT_TRUE(yla_vm_do_command(&vm) == -1, "halt expected")
YLATEST_ASSERT_TRUE(yla_vm_done(&vm), "normal");
return 0;
}
static int test_init_simple_run()
{
yla_cop_type prg[HEADER_SIZE + 2];
yla_cop_type *ptr = prg;
put_header(&ptr, 0, 0, 2);
put_commd(&ptr, CNOP);
put_commd(&ptr, CHALT);
yla_vm vm;
YLATEST_ASSERT_TRUE(yla_vm_init(&vm, prg, HEADER_SIZE + 2), "normal");
YLATEST_ASSERT_TRUE(yla_vm_run(&vm), "normal")
YLATEST_ASSERT_TRUE(yla_vm_done(&vm), "normal");
return 0;
}
static int test_push()
{
yla_cop_type prg[HEADER_SIZE + 4];
yla_cop_type *ptr = prg;
put_header(&ptr, 1, 0, 4);
put_commd(&ptr, CPUSH);
put_value(&ptr, 0x1234);
put_commd(&ptr, CHALT);
yla_vm vm;
YLATEST_ASSERT_TRUE(yla_vm_init(&vm, prg, HEADER_SIZE + 4), "normal");
YLATEST_ASSERT_TRUE(yla_vm_run(&vm), "normal")
YLATEST_ASSERT_TRUE(yla_vm_done(&vm), "normal");
return 0;
}
static int test_get_stack_full()
{
yla_cop_type prg[HEADER_SIZE + 4];
yla_cop_type *ptr = prg;
put_header(&ptr, 0, 0, 4);
put_commd(&ptr, CPUSH);
put_value(&ptr, 0x1234);
put_commd(&ptr, CHALT);
yla_vm vm;
YLATEST_ASSERT_TRUE(yla_vm_init(&vm, prg, HEADER_SIZE + 4), "normal");
YLATEST_ASSERT_FALSE(yla_vm_run(&vm), "normal")
YLATEST_ASSERT_TRUE(yla_vm_last_error(&vm) == YLA_VM_ERROR_STACK_FULL, "incorrect error code");
YLATEST_ASSERT_TRUE(yla_vm_done(&vm), "normal");
return 0;
}
static int test_code_limit()
{
yla_cop_type prg[HEADER_SIZE + 1];
yla_cop_type *ptr = prg;
put_header(&ptr, 0, 0, 1);
put_commd(&ptr, CNOP);
yla_vm vm;
YLATEST_ASSERT_TRUE(yla_vm_init(&vm, prg, HEADER_SIZE + 1), "normal");
YLATEST_ASSERT_FALSE(yla_vm_run(&vm), "normal")
YLATEST_ASSERT_TRUE(yla_vm_last_error(&vm) == YLA_VM_ERROR_CODE_SEG_EXCEED, "incorrect error code");
YLATEST_ASSERT_TRUE(yla_vm_done(&vm), "normal");
return 0;
}
YLATEST_BEGIN(yla_vm_test1)
YLATEST_ADD_TEST_CASE(test_gencode)
YLATEST_ADD_TEST_CASE(test_init_null)
YLATEST_ADD_TEST_CASE(test_init_0)
YLATEST_ADD_TEST_CASE(test_init_simple)
YLATEST_ADD_TEST_CASE(test_init_simple2)
YLATEST_ADD_TEST_CASE(test_init_simple_run)
YLATEST_ADD_TEST_CASE(test_push)
YLATEST_ADD_TEST_CASE(test_get_stack_full)
YLATEST_ADD_TEST_CASE(test_code_limit)
YLATEST_END