-
Notifications
You must be signed in to change notification settings - Fork 24
/
operator_overloading_test.go
91 lines (80 loc) · 1.64 KB
/
operator_overloading_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
package gscript
import "testing"
func TestOperator(t *testing.T) {
script := `
class Person{
int age;
Person(int a){
age = a;
}
}
Person operator + (Person p1, Person p2){
Person pp = Person(p1.age+p2.age);
return pp;
}
Person operator - (Person p1, Person p2){
Person pp = Person(p1.age-p2.age);
return pp;
}
Person operator * (Person p1, Person p2){
Person pp = Person(p1.age * p2.age);
return pp;
}
Person operator / (Person p1, Person p2){
Person pp = Person(p1.age / p2.age);
return pp;
}
bool operator == (Person p1, Person p2){
return p1.age==p2.age;
}
bool operator != (Person p1, Person p2){
return p1.age!=p2.age;
}
bool operator > (Person p1, Person p2){
return p1.age>p2.age;
}
bool operator >= (Person p1, Person p2){
return p1.age>=p2.age;
}
bool operator < (Person p1, Person p2){
return p1.age<p2.age;
}
bool operator <= (Person p1, Person p2){
return p1.age<=p2.age;
}
Person p1 = Person(10);
Person p2 = Person(20);
//Person p3 = operator(p1,p2);
Person p3 = p1+p2;
println("p3.age="+p3.age);
assertEqual(p3.age, 30);
Person p4 = p1-p2;
println("p4.age="+p4.age);
println(100-10);
Person p5 = p1*p2;
println("p5.age="+p5.age);
assertEqual(p5.age, 200);
Person p6 = p2/p1;
println("p6.age="+p6.age);
assertEqual(p6.age, 2);
bool b1 = p1 == p2;
println("b1=="+b1);
assertEqual(b1,false);
bool b2 = p1 != p2;
println("b2=="+b2);
assertEqual(b2,true);
bool b3 = p1 > p2;
println("b3=="+b3);
assertEqual(b3,false);
bool b4 = p1 >= p2;
println("b4=="+b4);
assertEqual(b4,false);
bool b5 = p1 < p2;
println("b5=="+b5);
assertEqual(b5,true);
bool b6 = p1 <= p2;
println("b6=="+b6);
assertEqual(b6,true);
`
NewCompiler().Compiler(script)
}