-
Notifications
You must be signed in to change notification settings - Fork 24
/
variable_args_test.go
100 lines (92 loc) · 1.47 KB
/
variable_args_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
package gscript
import (
"fmt"
"testing"
)
func TestVariableArgs(t *testing.T) {
script := `
int get(int ...a){
println(a);
return a;
}
int[] b = get(10);
println(b);
int x = b[0];
assertEqual(x,10);
`
NewCompiler().Compiler(script)
}
func TestVariableArgs2(t *testing.T) {
script := `
int get(int ...a){
println(a);
return a;
}
int[] b = get(10,2);
println(b);
string get2(string a, int ...b){
println(a);
println(b);
int x = b[0];
assertEqual(x,2);
for(int i=0;i<len(b);i++){
int x = b[i];
assertEqual(x,i+2);
println(x);
}
return a;
}
string g = get2("1",2,3,4,5);
println(g);
assertEqual(g, "1");
`
NewCompiler().Compiler(script)
}
func TestVariableArgs3(t *testing.T) {
script := `
string get2(string a,float c, int ...b){
println(a);
println(b);
println(c);
int x = b[0];
assertEqual(x,2);
for(int i=0;i<len(b);i++){
int x = b[i];
assertEqual(x,i+2);
println(x);
}
return a;
}
string g = get2("1",999.99, 2,3,4,5);
//println(g);
assertEqual(g, "1");
`
NewCompiler().Compiler(script)
}
func TestVariableArgs4(t *testing.T) {
script := `
int add(string s, int ...num){
println(s);
int sum = 0;
for(int i=0;i<len(num);i++){
int v = num[i];
sum = sum+v;
}
return sum;
}
int x = add("abc", 1,2,3,4);
println(x);
assertEqual(x, 10);
`
NewCompiler().Compiler(script)
}
func TestVariable(t *testing.T) {
test("123", 4, 5, 6)
}
func test(a string, b ...int) {
fmt.Println(a, b)
x := b[0 : len(b)-1]
fmt.Println(x)
ints := b[1:]
fmt.Println(ints)
}