Commit 85326581 authored by uwdkn's avatar uwdkn

Merge branch 'group1-compile-tests' into 'master'

Add exec tests from group 1

See merge request !43
parents 2eb9e37b 75dfdaa1
import lib.BooleanUtils;
class Test {
public static void main(String[] args) {
new Test().test();
}
public boolean booleanField;
public int intField;
public Test objectField;
public Test[] arrayField;
public void test() {
booleanField = true;
intField = 123;
objectField = new Test();
objectField.booleanField = true;
arrayField = new Test[5];
arrayField[0] = new Test();
arrayField[0].booleanField = true;
BooleanUtils bu = new BooleanUtils();
bu.println(booleanField);
System.out.println(intField);
bu.println(objectField.booleanField);
bu.println(arrayField[0].booleanField);
}
}
20
-1737
9169
7808
8896
-8482
9322
-943
-9196
-7588
-1390
5526
7096
264
-5848
-8359
-5300
-3034
-6372
1936
-7563
import lib.IntArrayUtils;
class InsertionSort {
public IntArray _array;
/* constructor */
public InsertionSort init(IntArray array) {
this._array = array;
return this;
}
public void sortArray() {
int i = 0;
while (i < _array.length) {
int j = i;
while (j > 0 && _array.get(j-1) > _array.get(j)) {
swapArrayElements(j, j-1);
j = j -1;
}
i = i + 1;
}
}
public void swapArrayElements(int indexA, int indexB) {
int tmp = _array.get(indexA);
_array.set(indexA, _array.get(indexB));
_array.set(indexB, tmp);
}
public IntArray getArray() {
return _array;
}
public static void main(String[] args) throws Exception {
IntArrayUtils iau = new IntArrayUtils();
IntArray arrayToSort = iau.readIntArrayFromStdin();
InsertionSort insertionSort = new InsertionSort().init(arrayToSort);
insertionSort.sortArray();
IntArray sortedArray = insertionSort.getArray();
iau.printIntArray(sortedArray);
}
}
10
1
2
3
4
5
6
7
8
9
10
3
-1
2147483647
-2147483648
import lib.IntArrayUtils;
class Test {
public static void main(String[] args) throws Exception {
IntArrayUtils intArrayUtils = new IntArrayUtils();
IntArray intArray = intArrayUtils.readIntArrayFromStdin();
intArrayUtils.printIntArray(intArray);
}
}
...@@ -10,6 +10,10 @@ public class BooleanUtils { ...@@ -10,6 +10,10 @@ public class BooleanUtils {
} }
} }
public boolean toBoolean(int value) {
return value != 0;
}
public BooleanUtils println(boolean value){ public BooleanUtils println(boolean value){
System.out.println(toInt(value)); System.out.println(toInt(value));
return this; return this;
......
package lib;
/*
* Utils to read int arrays from stdin and output them back to stdout.
*
* Methods using System.in.read() throw Exception to be valid Java code.
* java.io.IOException is the only exception thrown, but that is not a valid MiniJava IDENT.
*/
public class IntArrayUtils {
/* Reads a line as an int from stdin */
public int readIntFromStdin() throws Exception {
int result = 0;
int c = System.in.read();
boolean negative = false;
if (c == 45) { /* - */
negative = true;
c = System.in.read();
}
while (c != 10) { /* until newline */
if (c > 57 || c < 48) {
/* ignore non-digits */
} else {
int digit = c - 48; /* convert to digit */
result = result * 10 + digit;
}
c = System.in.read();
}
/* output the final int */
if (negative) {
return -result;
}
return result;
}
/* Reads a complete int array from stdin. First asks how many elements the array has. */
public IntArray readIntArrayFromStdin() throws Exception {
System.out.write(110); /* n */
System.out.write(58); /* : */
System.out.write(32); /* [space] */
System.out.flush();
int n = readIntFromStdin();
System.out.println(n);
int[] result = new int[n];
int i = 0;
while (i < n) {
result[i] = readIntFromStdin();
i = i + 1;
}
IntArray resultObj = new IntArray();
resultObj.array = result;
resultObj.length = n;
return resultObj;
}
/* Prints the int array back to stdout. */
public void printIntArray(IntArray array) {
int i = 0;
while (i < array.length) {
System.out.println(array.array[i]);
i = i + 1;
}
}
}
/*
* Wrapper class for an int array.
* As MiniJava arrays do not have a length attribute, we need to store it separately.
*/
class IntArray {
public int[] array;
public int length;
public IntArray init(int[] array, int length) {
this.array = array;
this.length = length;
return this;
}
public int get(int index) {
return array[index];
}
public void set(int index, int value) {
array[index] = value;
}
}
import lib.BooleanUtils;
class Test {
public static void main(String[] args) {
new Test().test();
}
public void test() {
noParameters();
booleanParameter(true);
this.intParameter(42);
Test test = new Test();
objectParameter(test);
Test[] array = new Test[1];
array[0] = test;
test.arrayParameter(array);
multipleParameters(5, true, 6, false);
}
public void noParameters() {
System.out.println(0);
}
public void booleanParameter(boolean arg) {
BooleanUtils bu = new BooleanUtils();
bu.println(arg);
}
public void intParameter(int arg) {
System.out.println(arg);
}
public void objectParameter(Test test) {
test.noParameters();
}
public void arrayParameter(Test[] test) {
test[0].noParameters();
}
public void multipleParameters(int a, boolean b, int c, boolean d) {
BooleanUtils bu = new BooleanUtils();
System.out.println(a);
bu.println(b);
System.out.println(c);
bu.println(d);
}
}
import lib.BooleanUtils;
class Test {
public static void main(String[] args) {
new Test().test();
}
public void test() {
BooleanUtils bu = new BooleanUtils();
voidMethod();
this.voidMethod();
bu.println(booleanMethod());
bu.println(this.booleanMethod());
System.out.println(intMethod());
System.out.println(this.intMethod());
/* As we create new instances in each method, those two should be unequal. */
bu.println(objectMethod() == objectMethod());
bu.println(arrayMethod() == arrayMethod());
}
public void voidMethod() {
System.out.println(2132);
}
public boolean booleanMethod() {
return false;
}
public int intMethod() {
return 42;
}
public Test objectMethod() {
return new Test();
}
public Test[] arrayMethod() {
return new Test[5];
}
}
import lib.BooleanUtils;
class Test {
public static void main(String[] args) {
new Test().test();
}
public void test() {
BooleanUtils bu = new BooleanUtils();
bu.println(false || sideEffect()); /* side effect executed */
bu.println(true || sideEffect()); /* side effect not executed */
bu.println(false && sideEffect()); /* side effect not executed */
bu.println(true && sideEffect()); /* side effect executed */
}
public boolean sideEffect() {
System.out.println(2);
return true;
}
}
/* uninitialized values should be
boolean: false
int: 0
object: null
array: null
*/
class Test {
public static void main(String[] args) {
new Test().test();
}
public boolean uninitializedBooleanField;
public int uninitializedIntField;
public Test uninitializedObjectField;
public Test[] uninitializedArrayField;
public void test() {
System.out.println(uninitializedIntField);
if (uninitializedBooleanField) {
System.out.println(1);
} else {
System.out.println(0);
}
if (uninitializedObjectField == null) {
System.out.println(1);
} else {
System.out.println(0);
}
if (uninitializedArrayField == null) {
System.out.println(1);
} else {
System.out.println(0);
}
}
}
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