-
Notifications
You must be signed in to change notification settings - Fork 24
/
for_test.go
394 lines (377 loc) · 6.1 KB
/
for_test.go
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package gscript
import (
"fmt"
"testing"
)
func TestFor(t *testing.T) {
script := `
int[] nums = {1,2,3};
for (int j = len(nums) -1 ;j > 0 ;j--){
int r = nums[j];
println(r);
//println(j);
}
`
NewCompiler().Compiler(script)
}
func TestCompiler_For2(t *testing.T) {
script := `
int a=0;
for(a<=10){
println(a);
a++;
if(a==5){
break;
}
}
assertEqual(a,5);
`
NewCompiler().Compiler(script)
}
func TestCompiler_For0(t *testing.T) {
script := `
int a=0;
for(a<3){
a++;
if(a==2){
continue;
}
println(a);
}
assertEqual(a,3);
`
NewCompiler().Compiler(script)
}
func TestCompiler_For3(t *testing.T) {
script := `
for(int i=0;i<5;i++){
println(i);
if (i==3){
break;
}
}
`
NewCompiler().Compiler(script)
}
func TestCompiler_For4(t *testing.T) {
script := `
for(int i=0;i<5;i++){
if (i==3){
continue;
}
println(i);
}
`
NewCompiler().Compiler(script)
}
func TestCompiler_Return(t *testing.T) {
script := `
for(int i=0;i<5;i++){
if (i==3){
return;
}
println(i);
}
`
NewCompiler().Compiler(script)
}
func TestCompiler_Return5(t *testing.T) {
script := `
int run(){
for(int i=0;i<3;i++){
for(int i=0;i<2;i++){
//println("inner i=" +i);
// todo crossoverJie 应该直接返回
//println("return finish i=" + i);
return 100;
}
println("outer i=" +i);
}
println("不会执行");
return 200;
}
int ret = run();
println(ret);
assertEqual(ret,100);
`
NewCompiler().Compiler(script)
}
func TestCompiler_Return6(t *testing.T) {
script := `
int run(int ret){
int a=1;
for (a<=3) {
println("外层if,a=" + a);
a++;
return ret;
println("return 之后不打印");
}
println("!!最终a="+ a);
return 0;
}
int ret = run(100);
println("run 之后 ret="+ ret);
assertEqual(ret,100);
println("开始调第二次");
ret = run(200);
println("run 之后 ret="+ ret);
assertEqual(ret,200);
`
NewCompiler().Compiler(script)
}
func TestCompiler_Return2(t *testing.T) {
script := `
int a=0;
for(a<=10){
println(a);
a++;
if(a==5){
return;
}
println("after return=" + a);
}
assertEqual(a,5);
`
NewCompiler().Compiler(script)
}
func TestCompiler_Return3(t *testing.T) {
script := `
int a=0;
bool x = false;
for(a<=10){
a++;
if(a==5){
x =true;
return;
}
println("after return="+a);
}
if(x){
println(x);
}
`
NewCompiler().Compiler(script)
}
func TestCompiler_Return4(t *testing.T) {
script := `
int run(){
for(1<2){
for(1<2){
return 100;
}
println(12);
}
println("不会执行");
return 200;
}
int x=run();
println(x);
assertEqual(x,100);
int a =9999;
println(a);
assertEqual(a,9999);
`
NewCompiler().Compiler(script)
}
func TestFor99(t *testing.T) {
a := 1
for a <= 3 {
//if a<=2 {
// println("return a=",a);
// return;
//}
println("外层if,a=", a)
a++
return
}
fmt.Println("!!最终a=", a)
}
func TestFor100(t *testing.T) {
for i := 0; i < 3; i++ {
for i := 0; i < 2; i++ {
println("inner i=", i)
println("return finish")
return
}
println("outer i=", i)
}
}
func TestFor101(t *testing.T) {
script := `
for (int i = 0; i < 3; i++) {
for (int i=0; i < 2; i++) {
println("inner i="+i);
println("return finish");
return;
}
println("outer i="+i);
}
`
NewCompiler().Compiler(script)
}
func TestFor98(t *testing.T) {
script := `
int f1(int x){
f1(1);
return 0;
}
`
NewCompiler().CompilerWithoutNative(script)
}
func TestFor102(t *testing.T) {
script := `
printf(string format, any ...a){}
println(any a){}
assertEqual(any a1, any a2){}
int r(int x, int ret){
if(x==0){
return ret;
}else{
ret = ret+1;
}
int b = r(x-1, ret);
printf("b=%d ", b);
return b;
}
int i = r(10, 0);
println(i);
assertEqual(i,10);
`
NewCompiler().CompilerWithoutNative(script)
}
func TestFor103(t *testing.T) {
script := `
printf(string format, any ...a){}
println(any a){}
assertEqual(any a1, any a2){}
int r(int x, int ret){
if(x==0){
return ret;
}else{
ret = ret+1;
}
//int b = r(x-1, ret);
//int d = r(x-1, ret);
//printf("b=%d d=%d", b,d);
//printf("11");
//int c = r(x-1, ret) +r(x-1, ret);
//int c = r(x-1, ret) +r(x-1, ret);
return r(x-1, ret) +r(x-1, ret);
//return r(x-1, ret) +r(x-1, ret);
}
int i = r(10, 0);
println(i);
assertEqual(i,10240);
`
NewCompiler().CompilerWithoutNative(script)
}
func TestFor104(t *testing.T) {
script := `
printf(string format, any ...a){}
println(any a){}
assertEqual(any a1, any a2){}
r(int x, int ret){
if(x==0){
return ret;
}else{
ret = ret+1;
}
r(x-1, ret);
println("r(x-1, ret);");
}
r(10, 0);
`
NewCompiler().CompilerWithoutNative(script)
}
func TestRe(t *testing.T) {
i := r(10, 0)
fmt.Println(i)
}
func r(x, ret int) int {
if x == 0 {
return ret
} else {
ret = ret + 1
}
i := r(x-1, ret)
d := r(x-1, ret)
fmt.Println("i=" + fmt.Sprint(i))
return i + d
}
func TestScope(t *testing.T) {
scope()
fmt.Println("test")
}
func scope() {
a := 1
for a <= 3 {
println("外层if,a=", a)
a++
return
}
fmt.Println("!!最终a=", a)
}
func TestSlice(t *testing.T) {
print(5)
//fmt.Print("123" + " ")
//fmt.Print("456" + " ")
}
func num(x, y int) int {
if y == 1 || y == x {
return 1
}
c := num(x-1, y-1) + num(x-1, y)
return c
}
func print(row int) {
for i := 0; i < row; i++ {
for j := i; j <= row-1; j++ {
fmt.Print(" ")
}
for j := 1; j <= i; j++ {
v := num(i, j)
fmt.Print(fmt.Sprint(v) + " ")
}
fmt.Println("")
}
}
func TestTowSum(t *testing.T) {
script := `
int twoSum(int target){
for(1<2){
for (3<4){
if (5+5==10){
println("准备return");
return target;
}
}
// 这里有注释,VisitBlockStms 特殊处理
//println("11");
}
println("不会执行");
return 100;
}
println("开始调用");
int ret = twoSum( 9);
println(ret);
assertEqual(ret,9);
`
NewCompiler().Compiler(script)
}
func TestFor2(t *testing.T) {
script := `
for (float y= 1.5; y > -1.5; y = y - 0.15) {
for (float x = -1.5; x < 1.5; x = x + 0.07) {
float a = x*x + y*y - 1.0;
if (a*a*a < x*x*y*y*y) {
print("*");
} else {
print(" ");
}
}
println("");
}
//println(dumpSymbol());
`
NewCompiler().Compiler(script)
}