Adding the profj tests
svn: r1313
This commit is contained in:
parent
24c6d96c7d
commit
4ebb06b25c
40
collects/tests/profj/advanced-tests.ss
Normal file
40
collects/tests/profj/advanced-tests.ss
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
(module advanced-tests mzscheme
|
||||||
|
(require (lib "profj-testing.ss" "profj"))
|
||||||
|
|
||||||
|
(prepare-for-tests "Advanced")
|
||||||
|
|
||||||
|
(interact-test 'advanced
|
||||||
|
(list "new int[3] instanceof int[] && true"
|
||||||
|
"((int[]) new int[3])[1]" "3/2")
|
||||||
|
(list #t 0 1)
|
||||||
|
"casts and instanceof of array types")
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
"class hasStatic {
|
||||||
|
static int x;
|
||||||
|
static void setX( int x ) {
|
||||||
|
hasStatic.x = x;
|
||||||
|
}
|
||||||
|
}" 'advanced
|
||||||
|
(list "hasStatic.x" "hasStatic.setX(5)" "hasStatic.x")
|
||||||
|
(list 0 '(void) 5)
|
||||||
|
"Using statics")
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
'advanced
|
||||||
|
(list "(new int[2][])[0]" #;"(new int[2][])[1]=new int[2];")
|
||||||
|
(list null #;0)
|
||||||
|
"multi-dimension array - not all intialized")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class TestClass{
|
||||||
|
ALoObj iterFilter(){
|
||||||
|
for (;false;){}
|
||||||
|
}
|
||||||
|
}/end SameAuthor"
|
||||||
|
'advanced
|
||||||
|
#t "Parse error check")
|
||||||
|
|
||||||
|
(report-test-results)
|
||||||
|
|
||||||
|
)
|
6
collects/tests/profj/all-tests.ss
Normal file
6
collects/tests/profj/all-tests.ss
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
(module all-tests mzscheme
|
||||||
|
(require "full-tests.ss")
|
||||||
|
(require "advanced-tests.ss")
|
||||||
|
(require "intermediate-tests.ss")
|
||||||
|
(require "beginner-tests.ss")
|
||||||
|
)
|
446
collects/tests/profj/beginner-tests.ss
Normal file
446
collects/tests/profj/beginner-tests.ss
Normal file
|
@ -0,0 +1,446 @@
|
||||||
|
(module beginner-tests mzscheme
|
||||||
|
(require (lib "profj-testing.ss" "profj"))
|
||||||
|
(require (lib "class.ss")
|
||||||
|
(lib "Object.ss" "profj" "libs" "java" "lang")
|
||||||
|
(lib "String.ss" "profj" "libs" "java" "lang"))
|
||||||
|
|
||||||
|
(prepare-for-tests "Beginner")
|
||||||
|
|
||||||
|
(define language 'beginner)
|
||||||
|
|
||||||
|
;;Execution tests that should pass
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class Simple {
|
||||||
|
Simple() { }
|
||||||
|
}
|
||||||
|
class Simple2 {
|
||||||
|
Simple s;
|
||||||
|
Simple2( Simple s ) {
|
||||||
|
this.s = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Simple3 {
|
||||||
|
Simple2 s;
|
||||||
|
Simple3( Simple2 s ) {
|
||||||
|
this.s = s;
|
||||||
|
}
|
||||||
|
boolean happy() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
language
|
||||||
|
#f
|
||||||
|
"Simple classes, with fields and methods")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface F {
|
||||||
|
int fo();
|
||||||
|
}"
|
||||||
|
'beginner #f "Interface with method")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface F {
|
||||||
|
int foo();
|
||||||
|
}
|
||||||
|
class FP implements F {
|
||||||
|
int x;
|
||||||
|
FP(int x) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
int foo() { return this.x; }
|
||||||
|
}"
|
||||||
|
language
|
||||||
|
#f
|
||||||
|
"Simple implementation of interface")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface F {
|
||||||
|
int lessThan( int x );
|
||||||
|
}
|
||||||
|
class Fp implements F {
|
||||||
|
int x;
|
||||||
|
Fp( int x ) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
int lessThan( int y) {
|
||||||
|
if (y < this.x)
|
||||||
|
return -1;
|
||||||
|
else
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}" language #f "Class & interface, containing if statement")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class A {
|
||||||
|
int a;
|
||||||
|
double b;
|
||||||
|
boolean c;
|
||||||
|
char d;
|
||||||
|
String e;
|
||||||
|
Object f;
|
||||||
|
|
||||||
|
A(int a, double b, boolean c, char d, String e, Object f) {
|
||||||
|
this.a = a;
|
||||||
|
this.b = b; this.c =c; this.d = d; this.e = e; this.f = f;
|
||||||
|
}
|
||||||
|
}" language #f "Class with variables of many primitive types")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class A {
|
||||||
|
B var;
|
||||||
|
A() { this.var = new B(); }
|
||||||
|
}
|
||||||
|
class B {
|
||||||
|
A var;
|
||||||
|
B() { this.var = new A(); }
|
||||||
|
}" language #f "Two classes with cycles: cannot be instantiated")
|
||||||
|
|
||||||
|
;;Execution tests that should produce errors
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface Z {
|
||||||
|
int x();
|
||||||
|
}
|
||||||
|
|
||||||
|
class A {
|
||||||
|
int z = 3;
|
||||||
|
Z y;
|
||||||
|
A(int z) {
|
||||||
|
this.z = z;
|
||||||
|
this.y = new B();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class B implements Z {
|
||||||
|
B() { }
|
||||||
|
int x() { return 3; }
|
||||||
|
int oX() { if (this.x() == 3) return 5; else return 6; }
|
||||||
|
}
|
||||||
|
foo"
|
||||||
|
language #t "Parse-error test, mentioning foo")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class X {
|
||||||
|
X() { super(); }
|
||||||
|
}"
|
||||||
|
language #t "Parse-error test, indicating that super not allowed")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class X {
|
||||||
|
X() { }
|
||||||
|
int x() { return super.x(); }
|
||||||
|
}"
|
||||||
|
language #t "Parse-error test, indicating that super.x() not allowed")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface A {
|
||||||
|
int x;
|
||||||
|
}"
|
||||||
|
language #t "Interface with a field")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface A {
|
||||||
|
int x();
|
||||||
|
boolean x();
|
||||||
|
}" language
|
||||||
|
#t "Two methods in an interface with the same name, and different return types")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface A {
|
||||||
|
int x();
|
||||||
|
int x(int y);
|
||||||
|
}" language
|
||||||
|
#t "Two methods in an interface with different arguments")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"public class B { B() { } }"
|
||||||
|
language #t "Use of public")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface A {
|
||||||
|
A foo();
|
||||||
|
}
|
||||||
|
class B implements A {
|
||||||
|
B() { }
|
||||||
|
A foo( String s) { return new B(); }
|
||||||
|
}"
|
||||||
|
'beginner #t "Method not implemented test, overload error")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class F { }"
|
||||||
|
'beginner #t "No constructor error")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class F {
|
||||||
|
F() { }
|
||||||
|
int x;
|
||||||
|
int x() { return 3; }
|
||||||
|
}" 'beginner #t "Method and field name share names error")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class badCtor {
|
||||||
|
badCtor() c{
|
||||||
|
}
|
||||||
|
}" 'beginner #t "Error message bug")
|
||||||
|
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class IncorrectField {
|
||||||
|
int myField;
|
||||||
|
IncorrectField(int x) {
|
||||||
|
this.myField = myField;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'beginner #t "Field access without this")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class IncorrectMethodCall {
|
||||||
|
IncorrectMethodCall() { }
|
||||||
|
int returner() { return 4; }
|
||||||
|
int adder( int x ) {
|
||||||
|
return returner() + x;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'beginner #t "Method access without this")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class F1 {
|
||||||
|
F1(int x) {
|
||||||
|
x = 4;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'beginner #t "Set non-field")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class F2 {
|
||||||
|
int f;
|
||||||
|
F2() {
|
||||||
|
this.f = this.f;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'beginner #t "Set with field")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface A {
|
||||||
|
A foo();
|
||||||
|
}
|
||||||
|
class B implements A {
|
||||||
|
B() { }
|
||||||
|
B foo() { return new B(); }
|
||||||
|
}"
|
||||||
|
'beginner #t "Incorrect overriding")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class A { }"
|
||||||
|
language #t "Use of abstract class")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class B extends A { }"
|
||||||
|
language #t "Use of extends")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class A { A() { } }
|
||||||
|
class B implements A { B () { } }"
|
||||||
|
language #t "Implementing a class")
|
||||||
|
|
||||||
|
;;Interaction tests: Mix of pass and fail
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
language
|
||||||
|
'("1 + 2 * 3 / 4" "new Object()" "\"Hello World\"" "true" "true && false" "true || false")
|
||||||
|
(list 2 (make-object Object) (make-java-string "Hello World") #t #f #t)
|
||||||
|
"Tests of simple expressions")
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
"class Book {
|
||||||
|
String title;
|
||||||
|
int numPages;
|
||||||
|
Book( String title, int numPages ) {
|
||||||
|
this.title = title;
|
||||||
|
this.numPages = numPages;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
language
|
||||||
|
(list "Book b = new Book(\"HP\", 33);" "b.title" "new Book(\"HP\",33).numPages" "new Book()" "new Book(\"\",0).numPages()")
|
||||||
|
(list (void) (make-java-string "HP") 33 'error 'error)
|
||||||
|
"Tests instantiating a class")
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
"class Book {
|
||||||
|
int numPages;
|
||||||
|
Book( int numPages ) {
|
||||||
|
this.numPages = numPages;
|
||||||
|
}
|
||||||
|
String level() {
|
||||||
|
if ( this.numPages < 10 )
|
||||||
|
return \"Apprentice\";
|
||||||
|
else if (this.numPages < 100)
|
||||||
|
return \"Journeyman\";
|
||||||
|
else
|
||||||
|
return \"Master\";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"
|
||||||
|
language
|
||||||
|
(list "new Book(9).level()" "new Book(10).level()" "new Book(100).level()" "new Book(99).level")
|
||||||
|
(list (make-java-string "Apprentice") (make-java-string "Journeyman") (make-java-string "Master") 'error)
|
||||||
|
"Tests of an if in a method")
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
"interface AShape { }
|
||||||
|
class CartPt {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
CartPt(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Dot implements AShape {
|
||||||
|
CartPt loc;
|
||||||
|
Dot(CartPt loc) {
|
||||||
|
this.loc = loc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Square implements AShape {
|
||||||
|
CartPt loc;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
Square(CartPt loc, int size) {
|
||||||
|
this.loc = loc;
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Circle implements AShape {
|
||||||
|
CartPt loc;
|
||||||
|
int radius;
|
||||||
|
|
||||||
|
Circle(CartPt loc, int radius) {
|
||||||
|
this.loc = loc;
|
||||||
|
this.radius = radius;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
language
|
||||||
|
(list "new Object()" "new AShape()" "new Square(new CartPt(77,22), 300).size" "new CartPt(21,1+21)")
|
||||||
|
(list `(make-object Object) 'error 300 `(let ((obj (make-object CartPt)))
|
||||||
|
(send obj CartPt-constructor-int-int 21 22)
|
||||||
|
obj))
|
||||||
|
"Book Test: AShape")
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
"class Coffee {
|
||||||
|
String kind;
|
||||||
|
int price;
|
||||||
|
int weight;
|
||||||
|
|
||||||
|
Coffee(String kind, int price, int weight) {
|
||||||
|
this.kind = kind;
|
||||||
|
this.price = price;
|
||||||
|
this.weight = weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
// to compute the total cost of this coffee purchase
|
||||||
|
int cost() { return this.price * this.weight; }
|
||||||
|
|
||||||
|
// to determine whether this coffee costs less than the given amount
|
||||||
|
boolean costsLess(int amt) { return this.price < amt; }
|
||||||
|
|
||||||
|
// to determine whether this coffee is cheaper than the given coffee
|
||||||
|
boolean cheaperThan(Coffee that) { return this.price < that.price; }
|
||||||
|
|
||||||
|
}"
|
||||||
|
'beginner
|
||||||
|
(list "new Coffee(\"Kona\", 1595, 100).cost()"
|
||||||
|
"new Coffee(\"Ethiopian\", 800, 1000).cost()"
|
||||||
|
"new Coffee(\"Colombian\", 950, 20).cost()"
|
||||||
|
"new Coffee(\"Kona\", 1595, 100).costsLess(200)"
|
||||||
|
"new Coffee(\"Columbian\", 950, 200).costsLess(1000)"
|
||||||
|
"new Coffee(\"Ethiopian\", 800, 1000).cheaperThan(new Coffee(\"Columbian\", 950, 200))"
|
||||||
|
"new Coffee(\"Kona\", 1595, 100).cheaperThan(new Coffee(\"Columbian\", 950, 200))")
|
||||||
|
(list 159500 800000 19000 #f #t #t #f)
|
||||||
|
"Book Test Coffee")
|
||||||
|
|
||||||
|
(interact-test 'beginner
|
||||||
|
(list "Math.abs(-1)")
|
||||||
|
(list 1)
|
||||||
|
"Library availability test")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"// Exercise 3.1.1
|
||||||
|
// Exercise 5.1.2
|
||||||
|
interface ALoH{}
|
||||||
|
class MTLoH implements ALoH{
|
||||||
|
MTLoH(){}
|
||||||
|
}
|
||||||
|
class ConsHouse implements ALoH{
|
||||||
|
House fst;
|
||||||
|
ALoH rst;
|
||||||
|
|
||||||
|
ConsHouse(House f, ALoH r) {
|
||||||
|
this.fst = f;
|
||||||
|
this.rst = r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Address {
|
||||||
|
int no;
|
||||||
|
String street;
|
||||||
|
String city;
|
||||||
|
|
||||||
|
Address(int no, String street, String city) {
|
||||||
|
this.no = no;
|
||||||
|
this.street = street;
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class House {
|
||||||
|
String kind;
|
||||||
|
int rooms;
|
||||||
|
Address address;
|
||||||
|
int price;
|
||||||
|
|
||||||
|
House(String kind, int rooms, Address address, int price) {
|
||||||
|
|
||||||
|
this.kind = kind;
|
||||||
|
this.rooms = rooms;
|
||||||
|
this.address = address;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
// determine if this house has more rooms (is bigger) than that given house
|
||||||
|
boolean isBigger(House that) { return this.rooms > that.rooms; }
|
||||||
|
// actual: h1.isBigger(h2), expected: false
|
||||||
|
// actual: h2.isBigger(h3), expected: true
|
||||||
|
|
||||||
|
//determine if this house's city as the same as a given city
|
||||||
|
boolean thisCity(String city) { return this.city.equals(city); }
|
||||||
|
|
||||||
|
// h1.thisCity(\"Brookline)\"--true
|
||||||
|
// h2.thisCity(\"Brookline\")--false
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Address a1 = new Address(23, \"Maple Street\", \"Brookline\");
|
||||||
|
Address a2 = new Address(5, \"Joye Road\", \"Newton\");
|
||||||
|
Address a3 = new Address(83, \"Winslow Street\", \"Waltham\");
|
||||||
|
|
||||||
|
House h1 = new House(\"Ranch\", 7, a1, 375000);
|
||||||
|
House h2 = new House(\"Colonial\", 9, a2, 450000);
|
||||||
|
House h3 = new House(\"Cape\", 6, a3, 235000);
|
||||||
|
|
||||||
|
ALoH mtlist = new MTLoH();
|
||||||
|
ALoH list1 = new ConsHouse(h1, mtlist);
|
||||||
|
ALoH list2 = new ConsHouse(h3, list1);
|
||||||
|
*/"
|
||||||
|
'beginner #t "Error message bug")
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
"interface A { }"
|
||||||
|
language
|
||||||
|
(list "new A()")
|
||||||
|
(list 'error)
|
||||||
|
"Trying to create an instance of an interface")
|
||||||
|
|
||||||
|
(report-test-results))
|
||||||
|
|
104
collects/tests/profj/full-tests.ss
Normal file
104
collects/tests/profj/full-tests.ss
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
(module full-tests mzscheme
|
||||||
|
(require (lib "profj-testing.ss" "profj"))
|
||||||
|
|
||||||
|
(prepare-for-tests "Full")
|
||||||
|
|
||||||
|
(execute-test "/* empty */"
|
||||||
|
'full
|
||||||
|
#f
|
||||||
|
"Empty file of comments")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface Bt { int largestNum(); }
|
||||||
|
|
||||||
|
class Leaf implements Bt { int largestNum() { return 1 ;} }
|
||||||
|
|
||||||
|
class Heap implements Bt {
|
||||||
|
Bt left;
|
||||||
|
Bt right;
|
||||||
|
int largestNum(){
|
||||||
|
if(this.left instanceof Heap &&
|
||||||
|
this.right instanceof Heap)
|
||||||
|
return this.right.largestNum();
|
||||||
|
else if(this.left instanceof Heap)
|
||||||
|
return this.right.largestNum();
|
||||||
|
else
|
||||||
|
return this.right.largestNum();
|
||||||
|
}
|
||||||
|
}" 'full #f "Instanceof test")
|
||||||
|
|
||||||
|
|
||||||
|
(execute-test "interface F {
|
||||||
|
int foo(int x);
|
||||||
|
}
|
||||||
|
interface G extends F {
|
||||||
|
int foo(int x);
|
||||||
|
}
|
||||||
|
|
||||||
|
class A implements G {
|
||||||
|
A() { }
|
||||||
|
int foo(int x) { return 3; }
|
||||||
|
}" 'full #f "Extending an interface while overriding a method")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class Foo {
|
||||||
|
private static int getX() { return 5; }
|
||||||
|
public static int x = getX();
|
||||||
|
}"
|
||||||
|
'full #f "Static access and order")
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
"public class hasStatic {
|
||||||
|
private int myId;
|
||||||
|
|
||||||
|
public hasStatic( int id ) {
|
||||||
|
super();
|
||||||
|
this.myId = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int returnId( hasStatic s ) {
|
||||||
|
return s.myId;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'full (list "hasStatic.returnId(new hasStatic(4))") (list 4) "Static use of private field")
|
||||||
|
|
||||||
|
(interact-test 'full
|
||||||
|
(list "int x = 4;" "x")
|
||||||
|
(list `(void) 4)
|
||||||
|
"Use of interactions fields")
|
||||||
|
|
||||||
|
(interact-test 'full
|
||||||
|
(list "String x = 4;")
|
||||||
|
(list 'error)
|
||||||
|
"Incorrect field assignment")
|
||||||
|
|
||||||
|
(interact-test 'full
|
||||||
|
(list "1.0 == 1.0")
|
||||||
|
(list #t)
|
||||||
|
"Floating point ==")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class A {
|
||||||
|
int x;
|
||||||
|
A() {
|
||||||
|
this.x = 4;
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'full #t "Misplaced super call")
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
"class A {
|
||||||
|
static int x= 0;
|
||||||
|
static {
|
||||||
|
for(int i = 0; i< 10; i++)
|
||||||
|
x += i;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'full (list "A.x") (list 45) "for loop in static section")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class A { A() { super.toString(); } }"
|
||||||
|
'full #f "Calling a super method")
|
||||||
|
|
||||||
|
(report-test-results))
|
364
collects/tests/profj/intermediate-tests.ss
Normal file
364
collects/tests/profj/intermediate-tests.ss
Normal file
|
@ -0,0 +1,364 @@
|
||||||
|
(module intermediate-tests mzscheme
|
||||||
|
(require (lib "profj-testing.ss" "profj"))
|
||||||
|
|
||||||
|
(prepare-for-tests "Intermediate")
|
||||||
|
|
||||||
|
;;Execute tests without errors
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class Foo {
|
||||||
|
abstract int f();
|
||||||
|
}"
|
||||||
|
'intermediate
|
||||||
|
#f "Simple abstract class with abstract method")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class Foo {
|
||||||
|
abstract int f();
|
||||||
|
}
|
||||||
|
class FooP extends Foo {
|
||||||
|
int f() { return 3; }
|
||||||
|
}"
|
||||||
|
'intermediate
|
||||||
|
#f "Simple abstract class with extending sub class")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class Foo {
|
||||||
|
abstract int f();
|
||||||
|
int fp() { return 3; }
|
||||||
|
}
|
||||||
|
class FooP extends Foo {
|
||||||
|
int f() { return this.fp(); }
|
||||||
|
}"
|
||||||
|
'intermediate
|
||||||
|
#f "Abstract class with abstract and non abstract methods; implemented")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class Fo {
|
||||||
|
int dist;
|
||||||
|
}"
|
||||||
|
'intermediate #f "Abstract class with field")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class F {
|
||||||
|
abstract int fp();
|
||||||
|
}
|
||||||
|
abstract class G extends F {
|
||||||
|
abstract int gp();
|
||||||
|
}"
|
||||||
|
'intermediate #f "Abstract class extending abstract class")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class first { }
|
||||||
|
class second extends first { }"
|
||||||
|
'intermediate #f "Class extension")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class first { int x() { return 3; } }
|
||||||
|
class second extends first { int x() { return 6; }}"
|
||||||
|
'intermediate #f "Overriding")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class first { int x() { return 3; }}
|
||||||
|
class second extends first { int x() { return super.x() + 3; }}"
|
||||||
|
'intermediate #f "Use of super in the method body")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface f { int fp(); }
|
||||||
|
interface g extends f { int fp(); int gp(); }
|
||||||
|
abstract class F implements g {
|
||||||
|
int fp() { return 2; }
|
||||||
|
abstract boolean i( int c );
|
||||||
|
}
|
||||||
|
class G extends F {
|
||||||
|
int gp() { return 2; }
|
||||||
|
boolean i ( int c ) { return c == this.gp(); }
|
||||||
|
}"
|
||||||
|
'intermediate #f "Abstract class with interface")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class HasMethod {
|
||||||
|
HasMethod() { }
|
||||||
|
int myMethod() {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class HasInheritedMethod extends HasMethod{
|
||||||
|
HasInheritedMethod() { }
|
||||||
|
|
||||||
|
int otherMethod() {
|
||||||
|
return this.myMethod();
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'intermediate
|
||||||
|
#f "Method Inheritance Test")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class ForError {
|
||||||
|
int x;
|
||||||
|
}
|
||||||
|
|
||||||
|
class extendForError extends ForError {
|
||||||
|
}
|
||||||
|
|
||||||
|
class UseError {
|
||||||
|
int f(ForError a) {
|
||||||
|
return a.x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class NoError extends UseError {
|
||||||
|
int fPrime() {
|
||||||
|
return this.f( new extendForError() );
|
||||||
|
}
|
||||||
|
}" 'intermediate #f "Former inheritance error")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class voidMethod {
|
||||||
|
void doNothing() { return; }
|
||||||
|
}" 'intermediate #f "Test of return of no arguments")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class Date {
|
||||||
|
int day;
|
||||||
|
int month;
|
||||||
|
int year;
|
||||||
|
|
||||||
|
Date(int day, int month, int year) {
|
||||||
|
this.day = day;
|
||||||
|
this.month = month;
|
||||||
|
this.year = year;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class ClockTime {
|
||||||
|
int hour;
|
||||||
|
int minute;
|
||||||
|
|
||||||
|
ClockTime(int hour, int minute){
|
||||||
|
this.hour = hour;
|
||||||
|
this.minute = minute;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
abstract class AMuseTicket {
|
||||||
|
Date d;
|
||||||
|
int price;
|
||||||
|
}
|
||||||
|
class MuseAdm extends AMuseTicket {
|
||||||
|
MuseAdm(Date d, int price) {
|
||||||
|
this.d = d;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class OmniMax extends AMuseTicket {
|
||||||
|
ClockTime t;
|
||||||
|
String title;
|
||||||
|
OmniMax(Date d, int price, ClockTime t, String title) {
|
||||||
|
this.d = d;
|
||||||
|
this.price = price;
|
||||||
|
this.t = t;
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class LaserShow extends AMuseTicket {
|
||||||
|
ClockTime t;
|
||||||
|
String row;
|
||||||
|
int seat;
|
||||||
|
|
||||||
|
LaserShow(Date d, int price, ClockTime t, String row, int seat) {
|
||||||
|
this.d = d;
|
||||||
|
this.price = price;
|
||||||
|
this.t = t;
|
||||||
|
this.row = row;
|
||||||
|
this.seat = seat;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'intermediate #f "Book Test Date")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class AZooAnimal{
|
||||||
|
String name;
|
||||||
|
int weight;
|
||||||
|
}
|
||||||
|
class Lion extends AZooAnimal{
|
||||||
|
int meat;
|
||||||
|
|
||||||
|
Lion(String name, int weight, int meat){
|
||||||
|
this.name = name;
|
||||||
|
this.weight = weight;
|
||||||
|
this.meat = meat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Snake extends AZooAnimal{
|
||||||
|
int length;
|
||||||
|
|
||||||
|
Snake(String name, int weight, int length){
|
||||||
|
this.name = name;
|
||||||
|
this.weight = weight;
|
||||||
|
this.length = length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Monkey extends AZooAnimal{
|
||||||
|
String food;;
|
||||||
|
|
||||||
|
Monkey(String name, int weight, String food){
|
||||||
|
this.name = name;
|
||||||
|
this.weight = weight;
|
||||||
|
this.food = food;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'intermediate #f "Book Test: ZooAnimal")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class Foo {
|
||||||
|
Foo() {
|
||||||
|
super();
|
||||||
|
x = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
int x;
|
||||||
|
int adder( int v ) {
|
||||||
|
return v + x;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'intermediate #f "Calling super")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface foo {
|
||||||
|
int size();
|
||||||
|
}
|
||||||
|
class M implements foo {
|
||||||
|
int size() { return 1;}
|
||||||
|
}"
|
||||||
|
'intermediate #f "Interface implemented")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class Path { abstract boolean isOk(); }
|
||||||
|
abstract class Success extends Path {
|
||||||
|
boolean isOk() { return true; }
|
||||||
|
}
|
||||||
|
class Left extends Success {
|
||||||
|
Success rest;
|
||||||
|
Left(Success rest) { this.rest = rest; }
|
||||||
|
}"
|
||||||
|
'intermediate #f "Abstract method implemented, class subclassed")
|
||||||
|
|
||||||
|
;;Execute tests with errors
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class F{ abstract int f(); }
|
||||||
|
class G extends F { }"
|
||||||
|
'intermediate #t "Extending abstract class without method")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class F { abstract int f(); }
|
||||||
|
class g extends F { boolean f() { return true; } }"
|
||||||
|
'intermediate #t "Extending abstract class with incorrect method")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class F { }
|
||||||
|
class G extends F {
|
||||||
|
int f() { return super.f(); }
|
||||||
|
}"
|
||||||
|
'intermediate #t "Super call for non-existing method")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class A { int a; }
|
||||||
|
class B extends A { int a; }
|
||||||
|
" 'intermediate #t "Test of shadowing dissallowed")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface A { }
|
||||||
|
class B extends A { }"
|
||||||
|
'intermediate #t "extending an interface")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface A { }
|
||||||
|
class B implements A { }
|
||||||
|
interface AAA { int f(); }
|
||||||
|
abstract class BBBB extends B implements AAA { }
|
||||||
|
class NotImplementIFace extends BBBB { }"
|
||||||
|
'intermediate #t "Interface not implemented, by inheritance")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"abstract class ALoC
|
||||||
|
{
|
||||||
|
boolean this.isMT;
|
||||||
|
abstract boolean compareTo(ALoC that);
|
||||||
|
abstract Coach getFst();
|
||||||
|
abstract ALoC getRst();
|
||||||
|
}"
|
||||||
|
'intermediate #t "Illegal use of this as a name")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"class ConsList extends List{
|
||||||
|
Swimmer first;
|
||||||
|
List rest;
|
||||||
|
ConsList(Swimmer first, List rest){
|
||||||
|
this.first = first;
|
||||||
|
this.rest = rest;
|
||||||
|
}
|
||||||
|
//the longer method takes a number and returns a list of all of the Swimmers
|
||||||
|
//in the original list that are longer than the given number.
|
||||||
|
List longer(int n){
|
||||||
|
|
||||||
|
if (n <= this.first.length)
|
||||||
|
rest.longer(n);
|
||||||
|
else
|
||||||
|
rest.longer(n)
|
||||||
|
return this.first;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'intermediate #t "Incorrect statement- parse error")
|
||||||
|
|
||||||
|
(execute-test
|
||||||
|
"interface Filter { Object filt(Object o); }
|
||||||
|
class Longer implements Filter{
|
||||||
|
double l;
|
||||||
|
Longer(double l) {
|
||||||
|
this.l=l;
|
||||||
|
}
|
||||||
|
boolean filt(Object o){
|
||||||
|
return (((Swimmer)o).length > l);
|
||||||
|
}
|
||||||
|
}" 'intermediate #t "Incompatible return type from inherited interface")
|
||||||
|
|
||||||
|
|
||||||
|
;;Interact tests
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
"class cycleA {
|
||||||
|
cycleB b;
|
||||||
|
cycleA( cycleB b) {
|
||||||
|
this.b = b;
|
||||||
|
b.addMyA(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class cycleB {
|
||||||
|
cycleA a;
|
||||||
|
cycleB() { }
|
||||||
|
|
||||||
|
void addMyA( cycleA a) {
|
||||||
|
this.a = a;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
'intermediate
|
||||||
|
(list "cycleA a = new cycleA( new cycleB());" "a")
|
||||||
|
(list '(void) 'a~f)
|
||||||
|
"Cyclic class constructors")
|
||||||
|
|
||||||
|
|
||||||
|
(interact-test
|
||||||
|
"class A {
|
||||||
|
int a() { return 2; }
|
||||||
|
}
|
||||||
|
class B extends A {
|
||||||
|
int a() { return 3 + super.a(); }
|
||||||
|
}"
|
||||||
|
'intermediate
|
||||||
|
(list "new B().a()")
|
||||||
|
(list 5)
|
||||||
|
"Calling a super method")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(report-test-results))
|
Loading…
Reference in New Issue
Block a user