Commit 3aa44890 by Tianqi Yang

test(operator): add tests for operator overloading

Add test cases to test suite 'positive': - op_ex_mod_int: Example of integer a modulo - overload_operator1: operator overloading with override Add test cases to test suite 'negative': - bin_op_ambiguous_error1: Ambiguous binary operator - bin_op_ambiguous_error2: Check the return type of binary operator at TypeCheck - bin_op_not_found_error1: Binary operator not found - print_not_found_error1 Print function not found - unary_op_not_found_error1 Unary operator not found
parent 9916747d
class A
{
int op__neg__ ()
{
Print ( "-A\n" );
return 0;
}
void print ()
{
Print ( "A.print\n" );
}
}
class B extends A
{
int op__add__ ( class A a )
{
Print ( "^B + A\n" );
return 0;
}
void print ()
{
Print ( "B.print\n" );
}
}
class C extends B
{
int op__add__ ( class A a )
{
Print ( "^C + A\n" );
return 0;
}
string op__neg__ ()
{
Print ( "-C\n" );
}
static int op__add__ ( class C c )
{
Print ( "^C + C\n" );
return 0;
}
void my_print ()
{
Print ( "C.print\n" );
}
}
class D extends B
{
int rop__add__ ( class B b )
{
Print ( "B + ^D\n" );
return 0;
}
int op__add__ ( class B b )
{
Print ( "^D + B\n" );
return 0;
}
int rop__add__ ( class D d )
{
Print ( "D + ^D\n" );
return 0;
}
string op__neg__ ()
{
Print ( "-D\n" );
}
void print ()
{
Print ( "D.print\n" );
}
}
class Main
{
static void main ()
{
class A a;
a = new A ();
class B b;
b = new B ();
class C c;
c = new C ();
class D d;
d = new D ();
int ans;
ans = b + a;
ans = b + b;
ans = b + c;
ans = b + d;
ans = c + a;
ans = c + b;
ans = c + c;
ans = c + d;
ans = d + b;
ans = d + c;
ans = d + d;
Print ( a );
Print ( b );
Print ( c );
Print ( d );
ans = -a;
ans = -b;
string s;
s = -c;
s = -d;
}
}
\ No newline at end of file
class A
{
int op__neg__ ()
{
Print ( "-A\n" );
return 0;
}
int op__add__ ( class A a )
{
return 1;
}
string rop__add__ ( class A a )
{
return "1";
}
int op__sub__ ( class A a )
{
return -1;
}
int rop__sub__ ( class A a )
{
return -2;
}
void print ()
{
Print ( "A.print\n" );
}
}
class Main
{
static void main ()
{
class A a;
a = new A ();
if (( a + a ) == 1) {
Print ( "ok1\n" );
}
if (( a + a ) == "1") {
Print ( "ok2\n" );
}
if (( a - a ) == 1) {
Print ( "ok3\n" );
}
if (( a - a ) == "1") {
Print ( "ok4\n" );
}
}
}
\ No newline at end of file
class A
{
int op__neg__ ()
{
Print ( "-A\n" );
return 0;
}
void print ()
{
Print ( "A.print\n" );
}
}
class B extends A
{
int op__add__ ( class A a )
{
Print ( "^B + A\n" );
return 0;
}
void print ()
{
Print ( "B.print\n" );
}
}
class C extends B
{
int op__add__ ( class A a )
{
Print ( "^C + A\n" );
return 0;
}
string op__neg__ ()
{
Print ( "-C\n" );
}
static int op__add__ ( class C c )
{
Print ( "^C + C\n" );
return 0;
}
void my_print ()
{
Print ( "C.print\n" );
}
}
class D extends B
{
int rop__add__ ( class B b )
{
Print ( "B + ^D\n" );
return 0;
}
int op__add__ ( class B b )
{
Print ( "^D + B\n" );
return 0;
}
int rop__add__ ( class D d )
{
Print ( "D + ^D\n" );
return 0;
}
string op__neg__ ()
{
Print ( "-D\n" );
}
void print ()
{
Print ( "D.print\n" );
}
}
class Main
{
static void main ()
{
class A a;
a = new A ();
class B b;
b = new B ();
class C c;
c = new C ();
class D d;
d = new D ();
int ans;
ans = a + a;
ans = b + a;
ans = b + b;
ans = b + c;
ans = b + d;
ans = c + a;
ans = c + b;
ans = c + c;
// ans = c + d;
ans = d + b;
ans = d + c;
ans = d + d;
Print ( a );
Print ( b );
Print ( c );
Print ( d );
ans = -a;
ans = -b;
string s;
s = -c;
s = -d;
}
}
\ No newline at end of file
class A
{
int op__neg__ ()
{
Print ( "-A\n" );
return 0;
}
}
class B extends A
{
int op__add__ ( class A a )
{
Print ( "^B + A\n" );
return 0;
}
void print ()
{
Print ( "B.print\n" );
}
}
class C extends B
{
int op__add__ ( class A a )
{
Print ( "^C + A\n" );
return 0;
}
string op__neg__ ()
{
Print ( "-C\n" );
}
static int op__add__ ( class C c )
{
Print ( "^C + C\n" );
return 0;
}
void my_print ()
{
Print ( "C.print\n" );
}
}
class D extends B
{
int rop__add__ ( class B b )
{
Print ( "B + ^D\n" );
return 0;
}
int op__add__ ( class B b )
{
Print ( "^D + B\n" );
return 0;
}
int rop__add__ ( class D d )
{
Print ( "D + ^D\n" );
return 0;
}
string op__neg__ ()
{
Print ( "-D\n" );
}
void print ()
{
Print ( "D.print\n" );
}
}
class Main
{
static void main ()
{
class A a;
a = new A ();
class B b;
b = new B ();
class C c;
c = new C ();
class D d;
d = new D ();
int ans;
ans = b + a;
ans = b + b;
ans = b + c;
ans = b + d;
ans = c + a;
ans = c + b;
ans = c + c;
// ans = c + d;
ans = d + b;
ans = d + c;
ans = d + d;
Print ( a );
Print ( b );
Print ( c );
Print ( d );
ans = -a;
ans = -b;
string s;
s = -c;
s = -d;
}
}
\ No newline at end of file
*** Error at (106,11): call of overloaded operator 'class : C + class : D' is ambiguous, candidates are :
*** note : candidate at (31,6) : int op__add__(class : A)
*** note : candidate at (56,6) : int rop__add__(class : B)
*** Error at (42,11): call of overloaded operator 'class : A + class : A' is ambiguous, candidates are :
*** note : candidate at (9,6) : int op__add__(class : A)
*** note : candidate at (14,9) : string rop__add__(class : A)
*** Error at (45,11): call of overloaded operator 'class : A + class : A' is ambiguous, candidates are :
*** note : candidate at (9,6) : int op__add__(class : A)
*** note : candidate at (14,9) : string rop__add__(class : A)
*** Error at (49,11): call of overloaded operator 'class : A - class : A' is ambiguous, candidates are :
*** note : candidate at (19,6) : int op__sub__(class : A)
*** note : candidate at (24,6) : int rop__sub__(class : A)
*** Error at (52,11): call of overloaded operator 'class : A - class : A' is ambiguous, candidates are :
*** note : candidate at (19,6) : int op__sub__(class : A)
*** note : candidate at (24,6) : int rop__sub__(class : A)
*** Error at (52,17): incompatible operands: int == string
*** Error at (99,11): no match method for call to operator 'class : A + class : A'
*** Error at (106,11): incompatible argument 1: class : A given, int/bool/string or class with method 'print()' expected
*** Error at (110,9): no match method for call to operator '- class : A'
*** Error at (111,9): no match method for call to operator '- class : B'
class A
{
void print ()
{
Print ( "A.print\n" );
}
}
class B extends A
{
int op__add__ ( class A a )
{
Print ( "^B + A\n" );
return 0;
}
void print ()
{
Print ( "B.print\n" );
}
}
class C extends B
{
int op__add__ ( class A a )
{
Print ( "^C + A\n" );
return 0;
}
string op__neg__ ()
{
Print ( "-C\n" );
}
static int op__add__ ( class C c )
{
Print ( "^C + C\n" );
return 0;
}
void my_print ()
{
Print ( "C.print\n" );
}
}
class D extends B
{
int rop__add__ ( class B b )
{
Print ( "B + ^D\n" );
return 0;
}
int op__add__ ( class B b )
{
Print ( "^D + B\n" );
return 0;
}
int rop__add__ ( class D d )
{
Print ( "D + ^D\n" );
return 0;
}
string op__neg__ ()
{
Print ( "-D\n" );
}
void print ()
{
Print ( "D.print\n" );
}
}
class Main
{
static void main ()
{
class A a;
a = new A ();
class B b;
b = new B ();
class C c;
c = new C ();
class D d;
d = new D ();
int ans;
ans = b + a;
ans = b + b;
ans = b + c;
ans = b + d;
ans = c + a;
ans = c + b;
ans = c + c;
// ans = c + d;
ans = d + b;
ans = d + c;
ans = d + d;
Print ( a );
Print ( b );
Print ( c );
Print ( d );
ans = -a;
ans = -b;
string s;
s = -c;
s = -d;
}
}
\ No newline at end of file
class Integer
{
static class Integer toInteger ( int x )
{
class Integer ans;
ans = new Integer ();
ans.x = ( x % MOD() + MOD() ) % MOD();
return ans;
}
static class Integer qpow ( int a, int b )
{
int base;
int ans;
base = a;
ans = 1;
while ( b > 0 ){
if ( b % 2 == 1 ) ans = ( ans * base ) % MOD();
base = ( base * base ) % MOD();
b = b / 2;
}
return toInteger ( ans );
}
class Integer op__add__ ( class Integer a )
{
return toInteger ( x + a.x );
}
class Integer op__sub__ ( class Integer a )
{
return toInteger ( x - a.x );
}
class Integer op__mul__ ( class Integer a )
{
return toInteger ( x * a.x );
}
class Integer op__div__ ( class Integer a )
{
return toInteger ( x * qpow ( a.x, MOD()-2 ).x );
}
class Integer op__mod__ ( class Integer a )
{
return toInteger ( x % a.x );
}
bool op__and__ ( class Integer a )
{
return ( x > 0 ) && ( a.x > 0 );
}
bool op__or__ ( class Integer a )
{
return ( x > 0 ) || ( a.x > 0 );
}
bool op__eq__ ( class Integer a )
{
return x == a.x;
}
bool op__ne__ ( class Integer a )
{
return x != a.x;
}
bool op__lt__ ( class Integer a )
{
return x < a.x;
}
bool op__gt__ ( class Integer a )
{
return x > a.x;
}
bool op__le__ ( class Integer a )
{
return x <= a.x;
}
bool op__ge__ ( class Integer a )
{
return x >= a.x;
}
class Integer op__neg__ ()
{
return toInteger ( -x );
}
bool op__not__ ()
{
return ( x == 0 );
}
static int MOD ()
{
return 17;
}
void setx (int x)
{
this.x = x;
}
int getx ()
{
return x;
}
void print ()
{
Print (x);
}
int x;
}
class Main
{
static void main ()
{
class Integer a;
class Integer b;
a = new Integer ();
b = new Integer ();
a.setx ( 15 );
b.setx ( 6 );
Print ( a, "\n" );
Print ( b, "\n" );
Print ( a + b, "\n" );
Print ( a - b, "\n" );
Print ( a * b, "\n" );
Print ( a / b, "\n" );
Print ( a % b, "\n" );
Print ( a && b, "\n" );
Print ( Integer.toInteger ( 1 ) && Integer.toInteger ( 0 ), "\n" );
Print ( a || b, "\n" );
Print ( Integer.toInteger ( 1 ) || Integer.toInteger ( 0 ), "\n" );
Print ( a == b, "\n" );
Print ( Integer.toInteger ( 3 ) == Integer.toInteger ( 3 ), "\n" );
Print ( a != b, "\n" );
Print ( Integer.toInteger ( 3 ) != Integer.toInteger ( 3 ), "\n" );
Print ( a < b, "\n" );
Print ( b < a, "\n" );
Print ( a <= b, "\n" );
Print ( b <= a, "\n" );
Print ( a > b, "\n" );
Print ( b > a, "\n" );
Print ( a >= b, "\n" );
Print ( b >= a, "\n" );
Print ( Integer.toInteger ( 3 ) <= Integer.toInteger ( 3 ), "\n" );
Print ( Integer.toInteger ( 3 ) >= Integer.toInteger ( 3 ), "\n" );
Print ( -a, "\n" );
Print ( !a, "\n" );
Print ( !Integer.toInteger ( 0 ), "\n" );
}
}
\ No newline at end of file
class A
{
int op__neg__ ()
{
Print ( "-A\n" );
}
void print ()
{
Print ( "A.print\n" );
}
}
class B extends A
{
int op__add__ ( class A a )
{
Print ( "^B + A\n" );
return 0;
}
void print ()
{
Print ( "B.print\n" );
}
}
class C extends B
{
int op__add__ ( class A a )
{
Print ( "^C + A\n" );
return 0;
}
string op__neg__ ()
{
Print ( "-C\n" );
}
static int op__add__ ( class C c )
{
Print ( "^C + C\n" );
return 0;
}
void my_print ()
{
Print ( "C.print\n" );
}
}
class D extends B
{
int rop__add__ ( class B b )
{
Print ( "B + ^D\n" );
return 0;
}
int op__add__ ( class B b )
{
Print ( "^D + B\n" );
return 0;
}
int rop__add__ ( class D d )
{
Print ( "D + ^D\n" );
return 0;
}
string op__neg__ ()
{
Print ( "-D\n" );
}
void print ()
{
Print ( "D.print\n" );
}
}
class Main
{
static void main ()
{
class A a;
a = new A ();
class B b;
b = new B ();
class C c;
c = new C ();
class D d;
d = new D ();
int ans;
ans = b + a;
ans = b + b;
ans = b + c;
ans = b + d;
ans = c + a;
ans = c + b;
ans = c + c;
// ans = c + d;
ans = d + b;
ans = d + c;
ans = d + d;
Print ( a );
Print ( b );
Print ( c );
Print ( d );
ans = -a;
ans = -b;
string s;
s = -c;
s = -d;
}
}
\ No newline at end of file
15
6
4
9
5
11
3
true
false
true
true
false
true
true
false
false
true
false
true
true
false
true
false
true
true
2
false
true
^B + A
^B + A
^B + A
B + ^D
^C + A
^C + A
^C + C
^D + B
^D + B
D + ^D
A.print
B.print
B.print
D.print
-A
-A
-C
-D
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment