Homework 4

This commit is contained in:
Carlos Galindo 2020-01-15 22:34:57 +01:00
parent 0afc86ceeb
commit 72cc3206c4
Signed by: kauron
GPG key ID: 83E68706DEE119A3
125 changed files with 4200 additions and 1636 deletions

View file

@ -0,0 +1,12 @@
/* Test accessing arrays with invalid index*/
class Main {
void main() {
int[] x;
x = new int[5];
x[0] = 3;
x[1] = 4;
x[2] = 5;
x[5] = 5; //this should fail
}
}

View file

@ -0,0 +1,9 @@
/* Test accessing arrays with invalid index*/
class Main {
void main() {
int[] x;
x = new int[5];
x[8] = 5; //this should fail
}
}

View file

@ -0,0 +1,9 @@
/* Test accessing arrays with invalid index*/
class Main {
void main() {
int[] x;
x = new int[5];
x[-1] = 5; //this should fail
}
}

View file

@ -0,0 +1,10 @@
/* Test access an array on a null pointer */
class Main {
void main() {
int[] x;
x = null;
x[1] = 5; //this should throw null pointer exception
}
}

View file

@ -0,0 +1,9 @@
/* Test access an array on a null pointer */
class Main {
void main() {
int[] x;
x[1] = 5; //this should throw null pointer exception
}
}

View file

@ -0,0 +1,10 @@
/* Test creating an array with negative length */
class Main {
void main() {
int[] x;
x = new int[-3];
x[0] = 3;
x[1] = 4;
x[2] = 5;
}
}

View file

@ -0,0 +1,16 @@
/* Test accessing arrays */
class Main {
void main() {
int[] x;
int i;
x = new int[3];
x[0] = 3;
x[1] = 4;
x[2] = 5;
i = x[0] + x[1] + x[2];
x[2]=55;
write(i);
writeln();
}
}

View file

@ -0,0 +1,15 @@
/* Test creating arrays of objects and accessing a null element*/
class A{}
class Main {
void main() {
A[] x;
A a;
x = new A[3];
x[1] = new A();
x[2] = a;
x[2] = new A();
}
}

View file

@ -0,0 +1,8 @@
/* Test array size=0 */
class Main {
void main() {
int[] x;
x = new int[0];
}
}

View file

@ -0,0 +1,12 @@
/* Test Arrays as Fields */
class Main {
int[] x;
void main() {
int i;
x = new int[3];
x[0] = 3;
x[1] = 4;
x[2] = 5;
}
}

View file

@ -0,0 +1,19 @@
/* Test inherited Array */
class Main {
void main() {
C1 c1;
C2 c2;
c1 = new C1();
c2 = new C2();
c1.x = new int[3];
c2.x = new int[4];
}
}
class C1{
int[] x;
}
class C2 extends C1 {}

View file

@ -0,0 +1,11 @@
/* Test creating arrays of objects */
class A{}
class Main {
void main() {
A[] x;
x = new A[2];
x[0] = new A();
}
}

View file

@ -0,0 +1,14 @@
// any array is a subtype of Object
// assignment and 'is equal' with an object of type 'Object' is fine
class Main {
void main() {
boolean y;
Object x;
int[] testArray;
testArray = new int[10];
x = testArray;
testArray = null;
x = null;
}
}

View file

@ -0,0 +1,21 @@
// assignment of subtypes
class Main {
void main() {
A a1, a2;
B b1, b2;
C c;
a1 = new A();
b2 = new B();
c = new C();
a2 = a1;
b1 = c;
}
}
class A {}
class B extends A {}
class C extends B {}

View file

@ -0,0 +1,8 @@
// Test boolean && and || operators
class Main {
void main() {
boolean a, b, c;
a = b && c || a;
}
}

View file

@ -0,0 +1,31 @@
// Test the equal/not equal operations, one of the types must be a subtype of the other
class Main {
void main() {
C1 c1;
C2 c2;
C3 c3;
Object o1, o2;
boolean s;
o1 = new Object();
o2 = new Object();
c1 = new C1();
c2 = new C2();
c3 = new C3();
s = o1 == o2;
s = c1 != c2;
s = c3 == c1;
s = o2 != o1;
s = null == c2;
s = o1 == c2;
//s = null == o;
}
}
class C1 {}
class C2 extends C1{}
class C3 extends C2{}

View file

@ -0,0 +1,15 @@
// Test the equal/not equal operations, one of the types must be a subtype of the other
class Main {
void main() {
int a,b;
boolean c;
a = 8;
b = 6;
c = a==b;
if (!c){
write(5);}
}
}

View file

@ -0,0 +1,14 @@
// Test the equal/not equal operations, one of the types must be a subtype of the other
class Main {
void main() {
boolean a,b,c;
a = true;
b = 6<10;
c = a==b;
if (c){
write(5);}
}
}

View file

@ -0,0 +1,36 @@
// Test '!' operator
class Main {
void main() {
boolean a,b,c,d;
int x,y;
a = true;
c = !false;
b = !a;
x = 100;
y = 5;
if (a) {
write(1);}
writeln();
if (!b){
write(2);
}
writeln();
while(c){
write(3);
c = false;
}
writeln();
// !x < 2 * y --> !x type error
// !(x < 2 * y) --> correct syntax
while(!(x<2*y)){
write(y);
y = y*2;
}
}
}

View file

@ -0,0 +1,21 @@
// Test types in a Cast (cannot cast to unrelated type)
class Main {
void main() {
C1 a, b;
C2 c, d;
Object o;
c = new C2();
a = new C1();
b = (C1) c;
d = (C2) a;
o = (Object) a;
o = (Object) c;
o = (Main) c;
}
}
class C1 {}
class C2 extends C1{}

View file

@ -0,0 +1,14 @@
// Test downcast from Object to array
class Main {
void main() {
A[] a;
Object o;
o = new Object();
a = (A[]) o;
}
}
class A{}

View file

@ -0,0 +1,12 @@
// Test types in a Cast
// cannot cast an int to an boolean
class Main {
void main() {
int a;
boolean b;
a = 1;
b = (boolean) a;
}
}

View file

@ -0,0 +1,11 @@
// Test types in a Cast (cannot cast to unrelated type)
class Main {
void main() {
int a;
boolean b;
//b = false;
a = (int) b;
}
}

View file

@ -0,0 +1,12 @@
// Test downcast from array to Object
class Main {
void main() {
int[] x;
Object o;
x = new int[5];
o = (Object) x;
}
}

View file

@ -0,0 +1,14 @@
// Test downcast from array to Object
class Main {
void main() {
A[] a;
Object o;
a = new A[5];
o = (Object) a;
}
}
class A{}

View file

@ -0,0 +1,15 @@
// Test cast from a type to the same type
class Main {
void main() {
C c,d;
Object o,g;
c = new C();
g = new Object();
d = (C) c;
o = (Object) g;
}
}
class C {}

View file

@ -0,0 +1,26 @@
// Test valid casts
class Main {
void main() {
C1 a, x;
C2 c,d;
C4 b,f;
Object o;
c = new C2();
a = new C1();
b = new C4();
f = new C4();
x = (C1) b;
o = (Object) f;
}
}
class C1 {}
class C2 extends C1{}
class C3 extends C2{}
class C4 extends C3{}

View file

@ -0,0 +1,23 @@
// Test casts from type to object
class Main {
void main() {
C1 a;
Object o,f,g;
int x;
boolean y;
a = new C1();
x = 2;
y = true;
o = (Object) a;
//f = (Object) x;
//g = (Object) y;
}
}
class C1 {}

View file

@ -0,0 +1,20 @@
/* Test expression evaluation with 8 variables */
class Main {
void main() {
int r1, r2, r3;
int i0, i1, i2, i3, i4, i5, i6, i7;
i0 = 0;
i1 = 1;
i2 = 2;
i3 = 3;
i4 = 4;
i5 = 5;
i6 = 6;
i7 = 7;
write(i0 + (i1 + ( i2 + ( i3 + ( i4 + (i5 + (i6 + (i7 + i0)))))))); writeln();
write(((((((i0 + i1) + i2) + i3) + i4) + i5) + i6) + i7); writeln();
write(((i0 + i1) + (i2 + i3)) + ((i4 + i5) + (i6 + i7))); writeln();
}
}

View file

@ -0,0 +1,10 @@
// test division by zero
class Main {
void main() {
int a;
int b;
a = 10;
b = a / 0;
}
}

View file

@ -0,0 +1,27 @@
// access a field from a class and a superclass (also includes hidden fields)
class Main {
void main() {
C1 c1;
C2 c2;
C4 c4;
c1 = new C1();
c2 = new C2();
c4 = new C4();
c1.a = 5;
c2.a = 6;
c4.a = false;
}
}
class C1{
int a;
}
class C2 extends C1 {}
class C3 extends C2 {}
class C4 extends C3 {
boolean a;
}

View file

@ -0,0 +1,19 @@
// test an Array of Objects and access their field
class Main {
void main() {
D[] x;
x = new D[50];
x[22].c.a = 2;
}
}
class C{
int a;
}
class D {
C c;
}

View file

@ -0,0 +1,5 @@
class Main {
void main() {
write(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15);
}
}

View file

@ -0,0 +1,30 @@
// Access inherited and hidden fields (general check)
class Other {
int a;
int b;
Object c;
Other o;
}
class Main extends Other {
int a, b;
void main() {
Other o;
o = (Other) this;
a = 1;
b = 2;
o.a = -1;
o.b = -2;
write(a);
write(b);
write(o.a);
write(o.b);
if (c != null) {
if (o.o != null) {
write(0);
}
}
}
}

View file

@ -0,0 +1,34 @@
/* testing assign statements*/
class Main {
int methodCall() { return 0; }
int methodCall2(int a, int b) {
if (a >= b) {
return 0;
} else {
if (b <= a) {
return 1;
}
}
return a * b;
}
void main() {
int a, b, c, e;
Ast d;
int[] g;
Object f;
a = read();
b = methodCall();
c = methodCall2(a, b);
d = new Ast();
e = d.field;
g = new int[a];
f = new Object[a];
f = g;
g = (int[]) f;
}
}
class Ast {
int field;
}

View file

@ -0,0 +1 @@
1

View file

@ -0,0 +1,24 @@
/* testing conditions*/
class Main {
void main() {
boolean a;
int b,c, d, e;
b = 10;
c = 3;
d = 40;
e = 1;
while ( b >= c ) {
write(b);
b = b-1;
}
a = c <= (b+4-5/2);
if (a){
write(13);}
if (100/2>d*e){
while(e<d){
d = d/2;}}
}
}

View file

@ -0,0 +1,17 @@
// call an method from a superclass
class Main {
void main() {
C2 c;
c = new C2();
c.aux();
}
}
class C1{
void aux(){
write(2);
}
}
class C2 extends C1 {}

View file

@ -0,0 +1,16 @@
/* testing casting as well as creating new Objects*/
class Main
{
void main()
{
int a;
int b;
Object c;
Object d;
c = null;
d = new Object();
a = 10;
c = (Object) d;
}
}

View file

@ -0,0 +1,10 @@
class Main {
void main() {
while(false) {}
if(false) {
return;
}
if (false) {} else {}
if (false) {} else {return;}
}
}

View file

@ -0,0 +1,5 @@
class Main {
void main() {
while(false) {}
}
}

View file

@ -0,0 +1,16 @@
// Hide a field from a superclass (type doesn't need to match)
class Parent {
int a;
}
class Main extends Parent {
Object a;
Main b;
void main() {
Parent c;
b = new Main();
b.a = new Object();
c = new Parent();
c.a = 10;
}
}

View file

@ -0,0 +1,16 @@
/* testing null references:
assigning null to an int or a boolean results in an error
write(null) results in a parser failure
*/
class Main {
void main() {
int a;
boolean b;
Object c;
c = null;
write(a);
writeln();
}
}

View file

@ -0,0 +1,14 @@
/* test overwritten main method */
class OtherMain {
void main () {
int a;
}
}
class Main extends OtherMain {
void main () {
int b;
}
}

View file

@ -0,0 +1,32 @@
/* this test forces the compiler to push registers to the stack */
class Main {
void main() {
int u,v,x,y,z;
boolean e,f,g,h,i,j;
B b1,b2;
e = true;
f = true;
g = true;
z = 0;
while (z < 30) {
if (f){
if (g){
u = 5;
v = 8;
x = u/v;
b1 = new B();
e = false;
}
}
else {
j = true;
}
z = z + 1;
}
}
}
class B {}

View file

@ -0,0 +1,15 @@
class Main {
void main() {
int a, b;
a = 1;
b = 2;
write(+a); writeln();
write(-a); writeln();
write(+a --b); writeln();
write(-a--b); writeln();
write(-----a-----5--b); writeln();
write(-----a*---------b);writeln();
}
}

View file

@ -0,0 +1,29 @@
/* Test expression evaluation with more than 8 variables
Test Register use
*/
class Main {
void main() {
int r1, r2, r3;
int i0, i1, i2, i3, i4, i5, i6, i7;
int a,b,c,d,e;
i0 = 0;
i1 = 1;
i2 = 2;
i3 = 3;
i4 = 4;
i5 = 5;
i6 = 6;
i7 = 7;
a = 5;
b = 5;
c = 5;
d = 5;
e = 5;
write(i0 + (i1 + ( i2 + ( i3 + (a+b+c+d+e) + ( i4 + (i5 + (i6 + (i7 + i0)))))))); writeln();
write(((((((i0 + i1) + i2) + i3) + i4 + (a*b*c*d*e)) + i5) + i6) + i7); writeln();
write(((i0 + i1) + (i2 + i3)) + ((i4 + i5) + (i6 + i7))); writeln();
}
}

View file

@ -0,0 +1,9 @@
/* Test that variables are zero initialized */
class Main {
void main() {
int a;
write(a);
}
}

View file

@ -0,0 +1,79 @@
// A if/else block is valid return statment if all possible branches return some value
// Also serves to check subtyping
// The return logic leads to nonsensical programs with statements after the return, but that's ok
class Main {
// All returns must be empty, but we don't care for their existence
void main() {
int a, b, c;
if (a == b) {
return;
} else {
while (a > b) {
return;
}
if (c > b) {
return;
}
}
}
// All returns must be null, Main (all subtypes of Main), and there must be an unconditional return
Main action1() {
Main a;
Object b;
boolean x, y;
if (x) {
if (y) {
if (x || y) {
if (x == y && x != y) {
return null;
} else {
return a;
}
} else {
if (b == null) {
return a;
} else {
return a;
}
}
} else {
if (b == a || a == null) {
return null;
} else {
return a;
}
}
} else {
return null;
}
}
// All returns must be null, Main[] (all subtypes of Main[]), and there must be an unconditional return
Main[] action2() {
Main[] a, b;
if (a == b) {
return a;
} else {
if (b != null) {
return b;
} else {
return null;
}
}
}
// All returns must be int (no subtypes), and there must be an unconditional return
int action3() {
int a, b, c, d;
if (a > b) {
return c - d;
while (a != a) {
return b;
}
} else {
return c * d / a + b % c - a;
}
}
}

View file

@ -0,0 +1,9 @@
class Main {
int n() {
return 0;
}
void main() {
n();
}
}

View file

@ -0,0 +1,21 @@
// The return statement can be placed in the middle of the method, and no warning will be thrown about unexecuted
// statements
class Main {
void main() {
write(things());
writeln();
}
int things() {
int a;
a = 10;
return a;
// These statements won't be reached
// but there is a complete return statement
a = 20;
writeln();
writeln();
}
}

View file

@ -0,0 +1,22 @@
/* test method that returns an object */
class Main {
void main() {
int x;
B b;
b = aux();
x = b.a;
write(x);
}
B aux(){
B b;
b = new B();
b.a = 10;
return b;
}
}
class B {
int a;
}

View file

@ -0,0 +1,24 @@
// A if/else with a return in both branches is valid
class Main {
void main() {
write(things());
writeln();
}
int things() {
int a;
int b;
Object c, d;
a = 10;
b = 100;
c = new Object();
d = new Object();
if (c == d) {
return 10;
} else {
return 5;
}
}
}

View file

@ -0,0 +1,22 @@
// test that call-by-value is used
class Main {
void main() {
int x;
A a;
a = new A();
a.a = 50;
aux(a.a);
x = a.a;
write(x);
}
void aux (int arg){
arg = arg + 1;
}
}
class A {
int a;
}

View file

@ -0,0 +1,18 @@
// test that call-by-value is used
class Main {
void main() {
int[] y;
int x;
y = new int[5];
y[2] = 50;
aux(y[2]);
x = y[2];
write(x);
}
void aux (int arg){
arg = arg + 1;
}
}

View file

@ -0,0 +1,18 @@
// test that call-by-value is used
class Main {
void main() {
int[] y;
int x;
y = new int[5];
y[2] = 50;
aux(y);
x = y[2];
write(x);
}
void aux (int[] arg){
arg[2] = 100;
}
}

View file

@ -0,0 +1,17 @@
// call an method from a superclass
class Main {
void main() {
C2 c;
c = new C2();
c.aux();
}
}
class C1{
void aux(){
write(2);
}
}
class C2 extends C1 {}

View file

@ -0,0 +1,19 @@
// call a simple method and use its return value
class Main {
void main() {
int a,b;
C c;
c = new C();
a = 5;
b = c.aux(a);
}
}
class C{
int aux(int arg){
return arg*2;
}
}

View file

@ -0,0 +1,35 @@
// call a method with many parameters and use its return value
// also Test Register use, by allocating memory
class Main {
void main() {
int a,b,c,d,e,f,g,h,x;
a = 5;
b = 5;
c = 5;
d = 5;
e = 5;
f = 5;
g = 5;
h = 5;
x = aux(a,b,c,d,e,f,g,h);
}
int aux(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8){
int[] x;
int i;
D d;
x = new int[20];
i = arg1 + arg2 + arg3 + arg4 - arg5 - arg6 + arg7 - arg8;
d = new D();
return i;
}
}
class D{
int[] x;
}