Showing the changes to java (11 of 11 files). Show all
Unified · Split
| @@ -0,0 +1,56 @@ | |||
| 1 | |||
| 2 | import static utils.Utils.*; | ||
| 3 | |||
| 4 | public class InnerClassTest { | ||
| 5 | |||
| 6 | class TestSuperClass { | ||
| 7 | private long privateField; | ||
| 8 | protected int protectedField = 13; | ||
| 9 | public float publicField; | ||
| 10 | |||
| 11 | private void privateFunction() { | ||
| 12 | publicField = protectedField + privateField; | ||
| 13 | } | ||
| 14 | protected int protectedFunction() { | ||
| 15 | privateField = 42; | ||
| 16 | return protectedField; | ||
| 17 | } | ||
| 18 | public boolean publicFunction() { | ||
| 19 | protectedFunction(); | ||
| 20 | privateFunction(); | ||
| 21 | return true; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | class TestDerivedClass extends TestSuperClass { | ||
| 26 | protected int protectedFunction() { | ||
| 27 | protectedField = 44; | ||
| 28 | return protectedField; | ||
| 29 | } | ||
| 30 | public boolean publicFunction() { | ||
| 31 | super.publicFunction(); | ||
| 32 | return false; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | public void runTests() { | ||
| 37 | TestSuperClass test1 = new TestSuperClass(); | ||
| 38 | assertEquals(0.0, test1.publicField); | ||
| 39 | test1.publicField = 6.0f * 9.0f; | ||
| 40 | assertEquals(54.0, test1.publicField); | ||
| 41 | assertEquals(true, test1.publicFunction()); | ||
| 42 | assertEquals(55.0, test1.publicField); | ||
| 43 | |||
| 44 | TestDerivedClass test2 = new TestDerivedClass(); | ||
| 45 | assertEquals(0.0, test2.publicField); | ||
| 46 | test2.publicField = 6.0f * 8.0f; | ||
| 47 | assertEquals(48.0, test2.publicField); | ||
| 48 | assertEquals(false, test2.publicFunction()); | ||
| 49 | assertEquals(44.0, test2.publicField); | ||
| 50 | } | ||
| 51 | |||
| 52 | public static void main(String[] args) { | ||
| 53 | InnerClassTest test = new InnerClassTest(); | ||
| 54 | test.runTests(); | ||
| 55 | } | ||
| 56 | } | ||
| @@ -0,0 +1,14 @@ | |||
| 1 | |||
| 2 | import static utils.Utils.*; | ||
| 3 | import java.lang.Math; | ||
| 4 | |||
| 5 | public class MathTest { | ||
| 6 | |||
| 7 | public static void main(String[] args) { | ||
| 8 | assertEquals(42, 6 * 7); | ||
| 9 | assertEquals(0.5, 1.0 / 2); | ||
| 10 | assertEquals(1, Math.sqrt(1)); | ||
| 11 | assertEquals(3, Math.sqrt(9)); | ||
| 12 | assertEquals(82.5, 6 * 7 / 1 + 45 - 9.0 / 2); | ||
| 13 | } | ||
| 14 | } | ||
| @@ -0,0 +1,13 @@ | |||
| 1 | This folder contains some jvm tests. | ||
| 2 | |||
| 3 | These simple pieces of code try to check some basic jvm | ||
| 4 | and jcl features. They are currently using mkfiles as | ||
| 5 | tests platform, but may later use Inferno's itest. | ||
| 6 | |||
| 7 | Currently tests are defined in mkfile, in TESTS var. | ||
| 8 | Every test consists of runnable java class. | ||
| 9 | |||
| 10 | Useful commands: | ||
| 11 | mk, or mk tests: compiles and runs all the tests | ||
| 12 | mk test-%test name%: run some specific test | ||
| 13 | mk clean: clean-up all binary and debug files | ||
| @@ -0,0 +1,8 @@ | |||
| 1 | |||
| 2 | public class SystemIOTest { | ||
| 3 | |||
| 4 | public static void main(String[] args) { | ||
| 5 | System.out.println("System.out stream test"); | ||
| 6 | System.err.println("System.err stream test"); | ||
| 7 | } | ||
| 8 | } | ||
| @@ -0,0 +1,61 @@ | |||
| 1 | |||
| 2 | import static utils.Utils.*; | ||
| 3 | |||
| 4 | public class ThreadTest { | ||
| 5 | |||
| 6 | private static class Integer { | ||
| 7 | private int data; | ||
| 8 | |||
| 9 | Integer(int data) { | ||
| 10 | this.data = data; | ||
| 11 | } | ||
| 12 | |||
| 13 | int getData() { | ||
| 14 | return data; | ||
| 15 | } | ||
| 16 | |||
| 17 | void setData(int data) { | ||
| 18 | this.data = data; | ||
| 19 | } | ||
| 20 | |||
| 21 | } | ||
| 22 | |||
| 23 | public static void main(String[] args) throws InterruptedException { | ||
| 24 | final Integer var = new Integer(0); | ||
| 25 | Thread testThread = new Thread(new Runnable() { | ||
| 26 | @Override | ||
| 27 | public void run() { | ||
| 28 | var.setData(42); | ||
| 29 | } | ||
| 30 | }); | ||
| 31 | testThread.start(); | ||
| 32 | testThread.join(); | ||
| 33 | assertEquals(42, var.getData()); | ||
| 34 | |||
| 35 | Thread testThread1 = new Thread(new Runnable() { | ||
| 36 | @Override | ||
| 37 | public void run() { | ||
| 38 | for (int i = 0; i < 2000; i++) { | ||
| 39 | synchronized (var) { | ||
| 40 | var.setData(var.getData() + 1); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | }); | ||
| 45 | Thread testThread2 = new Thread(new Runnable() { | ||
| 46 | @Override | ||
| 47 | public void run() { | ||
| 48 | for (int i = 0; i < 1000; i++) { | ||
| 49 | synchronized (var) { | ||
| 50 | var.setData(var.getData() - 1); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | }); | ||
| 55 | testThread1.start(); | ||
| 56 | testThread2.start(); | ||
| 57 | testThread1.join(); | ||
| 58 | testThread2.join(); | ||
| 59 | assertEquals(1042, var.getData()); | ||
| 60 | } | ||
| 61 | } | ||
| @@ -0,0 +1,28 @@ | |||
| 1 | #!/dis/sh | ||
| 2 | |||
| 3 | load std test | ||
| 4 | |||
| 5 | ROOT=/Users/zubr/dev/distributed/inferno-java | ||
| 6 | JAVAC=os -d $ROOT`{pwd} javac | ||
| 7 | |||
| 8 | subfn test-name { | ||
| 9 | result=SystemIOTest | ||
| 10 | } | ||
| 11 | |||
| 12 | fn compile-test { | ||
| 13 | report INF 7 'Compiling test $1' | ||
| 14 | $JAVAC $1.java | ||
| 15 | for c in $1^*.class {java/j2d -g $c} | ||
| 16 | } | ||
| 17 | fn clean-test { | ||
| 18 | report INF 7 'Cleaning test $1' | ||
| 19 | rm *.dis *.s *.class | ||
| 20 | } | ||
| 21 | fn run-test { | ||
| 22 | report INF 7 'Running test $1' | ||
| 23 | java/jvm $1 | ||
| 24 | } | ||
| 25 | |||
| 26 | test := ${test-name} | ||
| 27 | compile-test $test | ||
| 28 | #run-test $test | ||
| @@ -0,0 +1,4 @@ | |||
| 1 | #!/dis/sh | ||
| 2 | |||
| 3 | # Common init, compile and run test | ||
| 4 | run ../common.sh | ||
| @@ -0,0 +1 @@ | |||
| 1 | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | <../../mkconfig | ||
| 2 | ROOT=/Users/zubr/dev/distributed/inferno-java | ||
| 3 | |||
| 4 | TESTS= SystemIOTest\ | ||
| 5 | MathTest\ | ||
| 6 | InnerClassTest\ | ||
| 7 | ThreadTest\ | ||
| 8 | |||
| 9 | OBJ=${TESTS:%=test-%} | ||
| 10 | JAVAC=os -d $ROOT`{pwd} javac | ||
| 11 | |||
| 12 | tests:QV: utils $OBJ | ||
| 13 | |||
| 14 | utils:QV: | ||
| 15 | @{builtin cd utils; mk $MKFLAGS} | ||
| 16 | |||
| 17 | test-%:QV: | ||
| 18 | $JAVAC $stem.java | ||
| 19 | for c in $stem*.class {java/j2d -g $c} | ||
| 20 | java/jvm $stem | ||
| 21 | echo $stem passed! | ||
| 22 | |||
| 23 | clean:QV: | ||
| 24 | rm *.dis *.class *.sbl | ||
| @@ -0,0 +1,61 @@ | |||
| 1 | |||
| 2 | package utils; | ||
| 3 | |||
| 4 | public class Utils { | ||
| 5 | |||
| 6 | public static class AssertionFailedException extends java.lang.RuntimeException { | ||
| 7 | public AssertionFailedException(String msg) { | ||
| 8 | super(msg); | ||
| 9 | System.err.println("Asserion failed: " + msg); | ||
| 10 | } | ||
| 11 | } | ||
| 12 | |||
| 13 | private Utils() { | ||
| 14 | } | ||
| 15 | |||
| 16 | public static void assertEquals(int expected, int found) throws AssertionFailedException { | ||
| 17 | assertEquals(expected, found, "expected " + expected + " but found " + found); | ||
| 18 | } | ||
| 19 | public static void assertEquals(int expected, int found, String msg) throws AssertionFailedException { | ||
| 20 | if (expected != found) | ||
| 21 | throw new AssertionFailedException(msg); | ||
| 22 | } | ||
| 23 | |||
| 24 | |||
| 25 | public static void assertEquals(long expected, long found) throws AssertionFailedException { | ||
| 26 | assertEquals(expected, found, "expected " + expected + " but found " + found); | ||
| 27 | } | ||
| 28 | public static void assertEquals(long expected, long found, String msg) throws AssertionFailedException { | ||
| 29 | if (expected != found) | ||
| 30 | throw new AssertionFailedException(msg); | ||
| 31 | } | ||
| 32 | |||
| 33 | public static void assertEquals(float expected, float found) throws AssertionFailedException { | ||
| 34 | assertEquals(expected, found, "expected " + expected + " but found " + found); | ||
| 35 | } | ||
| 36 | public static void assertEquals(float expected, float found, String msg) throws AssertionFailedException { | ||
| 37 | if (expected != found) | ||
| 38 | throw new AssertionFailedException(msg); | ||
| 39 | } | ||
| 40 | |||
| 41 | public static void assertEquals(double expected, double found) throws AssertionFailedException { | ||
| 42 | assertEquals(expected, found, "expected " + expected + " but found " + found); | ||
| 43 | } | ||
| 44 | public static void assertEquals(double expected, double found, String msg) throws AssertionFailedException { | ||
| 45 | if (expected != found) | ||
| 46 | throw new AssertionFailedException(msg); | ||
| 47 | } | ||
| 48 | |||
| 49 | public static void assertEquals(Object expected, Object found) throws AssertionFailedException { | ||
| 50 | assertEquals(expected, found, "expected " + expected + " but found " + found); | ||
| 51 | } | ||
| 52 | public static void assertEquals(Object expected, Object found, String msg) throws AssertionFailedException { | ||
| 53 | if (!expected.equals(found)) | ||
| 54 | throw new AssertionFailedException(msg); | ||
| 55 | } | ||
| 56 | |||
| 57 | public static void main(String[] args) { | ||
| 58 | // This is only for convenience, so that | ||
| 59 | // test system may assume this is also a test | ||
| 60 | } | ||
| 61 | } | ||
| @@ -0,0 +1,19 @@ | |||
| 1 | <../../../mkconfig | ||
| 2 | ROOT=/Users/zubr/dev/distributed/inferno-java | ||
| 3 | |||
| 4 | UTILS= Utils\ | ||
| 5 | |||
| 6 | OBJ=${UTILS:%=%.dis} | ||
| 7 | JAVAC=os -d $ROOT`{pwd} javac | ||
| 8 | |||
| 9 | all:QV: $OBJ | ||
| 10 | echo Compiled test utils | ||
| 11 | |||
| 12 | %.dis:QE: %.class | ||
| 13 | for c in $stem*.class {java/j2d -g $c} | ||
| 14 | |||
| 15 | %.class:Q: %.java | ||
| 16 | $JAVAC $stem.java | ||
| 17 | |||
| 18 | clean:QV: | ||
| 19 | rm *.dis *.class *.sbl | ||