Initial revision

From-SVN: r102074
This commit is contained in:
Tom Tromey 2005-07-16 00:30:23 +00:00
parent 6f4434b39b
commit f911ba985a
4557 changed files with 1000262 additions and 0 deletions

View file

@ -0,0 +1,100 @@
public class ArrayTest {
public static void main (String args[])
{
BooleanArrayInit();
ByteArrayInit();
CharArrayInit();
ShortArrayInit();
IntArrayInit();
ArrayName(args);
}
public static void BooleanArrayInit()
{
try {
boolean val = true;
boolean [] x = { true };
if (x[0] == val)
passed("BooleanArrayInit() boolean[] x = {"+val+"}");
else
failed("BooleanArrayInit() boolean[] x = {"+val+"}");
} catch (Exception e) {
failed("BooleanArrayInit() "+e);
}
}
public static void ByteArrayInit()
{
try {
byte val = 42;
byte [] x = { 42 };
if (x[0] == val)
passed("ByteArrayInit() byte[] x = {"+val+"}");
else
failed("ByteArrayInit() byte[] x = {"+val+"}");
} catch (Exception e) {
failed("ByteArrayInit() "+e);
}
}
public static void CharArrayInit()
{
try {
char val = 'X';
char [] x = { 'X' };
if (x[0] == val)
passed("CharArrayInit() char[] x = {'"+val+"'}");
else
failed("CharArrayInit() char[] x = {'"+val+"'}");
} catch (Exception e) {
failed("CharArrayInit() "+e);
}
}
public static void ShortArrayInit()
{
try {
short val = 42;
short [] x = { 42 };
if (x[0] == val)
passed("ShortArrayInit() short[] x = {"+val+"}");
else
failed("ShortArrayInit() short[] x = {"+val+"}");
} catch (Exception e) {
failed("ShortArrayInit() "+e);
}
}
public static void IntArrayInit()
{
try {
int val = 42;
int [] x = { 42 };
if (x[0] == val)
passed("IntArrayInit() int[] x = {"+val+"}");
else
failed("IntArrayInit() int[] x = {"+val+"}");
} catch (Exception e) {
failed("IntArrayInit() "+e);
}
}
public static void failed(String s)
{
if (s != null)
System.out.println("FAILED: " + s);
else
System.out.println("FAILED: ");
}
public static void passed(String s)
{
if (s != null)
System.out.println("PASSED: " + s);
else
System.out.println("PASSED: ");
}
public static void ArrayName(String args[])
{
try {
String name = args.getClass().getName();
passed("ArrayName() name="+name);
} catch (Exception e) {
failed("ArrayName() "+e);
}
}
}

View file

@ -0,0 +1,177 @@
/**
* Test the Boolean object wrapper class.
*
* @author Brian Jones (brian.jones@oryxsoft.com)
*/
public class BooleanTest
{
Boolean j;
String x;
public static void main (String[] argv)
{
BooleanTest test = new BooleanTest();
test.constructorsTest();
test.booleanValueTest();
test.equalsTest();
test.getBooleanTest();
test.hashCodeTest();
test.toStringTest();
test.valueOfTest();
test.variablesTest();
}
public void constructorsTest()
{
j = new Boolean(true); // is true
if (j.booleanValue() != true)
failed("Boolean(true)");
else
passed("Boolean(true)");
j = new Boolean(false); // is false
if (j.booleanValue() != false)
failed("Boolean(false)");
else
passed("Boolean(false)");
j = new Boolean("tRuE"); // is true
if (j.booleanValue() != true)
failed("Boolean(\"tRuE\")");
else
passed("Boolean(String)");
j = new Boolean("brian"); // is false
if (j.booleanValue() != false)
failed("Boolean(\"brian\")");
else
passed("Boolean(String)");
j = new Boolean(null); // is false
if (j.booleanValue() != false)
failed("Boolean(null)");
else
passed("Boolean(String)");
}
public void booleanValueTest()
{
if (Boolean.TRUE.booleanValue() != true)
failed("Boolean.booleanValue()");
else
passed("Boolean.booleanValue()");
}
public void equalsTest()
{
j = new Boolean("false");
if (j.equals(Boolean.FALSE) != true)
failed("Boolean.equals(Object)");
else
passed("Boolean.equals(Object)");
}
public void getBooleanTest()
{
if (Boolean.getBoolean("BIG_DAWG_TEST"))
failed("Boolean.getBoolean(String)");
else
passed("Boolean.getBoolean(String)");
}
public void hashCodeTest()
{
j = new Boolean(null); // is false
boolean caught = false;
try
{
int i = j.hashCode();
}
catch (Exception e)
{
caught = true;
failed("Boolean.hashCode()");
}
if (!caught)
passed("Boolean.hashCode()");
}
public void toStringTest()
{
j = Boolean.TRUE;
String x = j.toString();
if (x.equals("true") != true)
failed("j.toString() where j is Boolean.TRUE");
else
passed("Boolean.toString()");
j = Boolean.FALSE;
x = j.toString();
if (x.equals("false") != true)
failed("j.toString() where j is Boolean.FALSE");
else
passed("Boolean.toString()");
}
public void valueOfTest()
{
j = Boolean.valueOf("tRUe"); // true
if (j.booleanValue() != true)
failed("Boolean.valueOf(String)");
else
passed("Boolean.valueOf(String)");
j = Boolean.valueOf(null); // false
if (j.booleanValue() != false)
failed("Boolean.valueOf(null)");
else
passed("Boolean.valueOf(null)");
j = Boolean.valueOf("lc"); // false
if (j.booleanValue() != false)
failed("Boolean.valueOf(String)");
else
passed("Boolean.valueOf(String)");
}
public void variablesTest()
{
if (Boolean.TRUE.booleanValue() != true)
failed("Boolean.TRUE");
else
passed("Boolean.TRUE");
if (Boolean.FALSE.booleanValue() != false)
failed("Boolean.FALSE");
else
passed("Boolean.FALSE");
x = Boolean.TYPE.getName();
if (x.equals("boolean") != true)
failed("Boolean.TYPE.getName() is " + x + " != boolean");
else
passed("Boolean.TYPE.getName() is boolean");
}
public void failed(String s)
{
if (s != null)
System.out.println("FAILED: " + s);
else
System.out.println("FAILED: ");
}
public void passed(String s)
{
if (s != null)
System.out.println("PASSED: " + s);
else
System.out.println("PASSED: ");
}
}

View file

@ -0,0 +1,380 @@
import gnu.test.*;
/**
* Test the Byte object wrapper class.
*
* @author Brian Jones (cbj@gnu.org)
*/
public class ByteTest
{
public static class constructorTest1 implements Test
{
byte b = 1;
public String getName() {
return "Byte(byte)";
}
public Result test() {
try {
Byte byteObject = new Byte(b);
} catch (Exception e) {
return new Fail(e.getMessage());
} catch (Error err) {
return new Fail(err.getMessage());
}
return new Pass();
}
}
public static class constructorTest2 implements Test
{
Byte byteObject = null;
public String getName() {
return "Byte(String)";
}
public Result test() {
try {
byteObject = new Byte("1");
} catch (Exception e) {
return new Fail(e.getMessage());
} catch (Error err) {
return new Fail(err.getMessage());
}
return new Pass();
}
}
public static class byteValueTest implements Test
{
public String getName() {
return "Byte.byteValue()";
}
public Result test() {
byte b = 1;
Byte byteObject = new Byte(b);
if (byteObject.byteValue() == b)
return new Pass();
else
return new Fail();
}
}
public static class decodeTest implements Test
{
public String getName() {
return "Byte.decode(String)";
}
public Result test() {
Byte obj = Byte.decode("1");
if (obj.byteValue() == 1)
return new Pass();
else
return new Fail();
}
}
public static class doubleValueTest implements Test
{
public String getName() {
return "Byte.doubleValue()";
}
public Result test() {
byte b = 4;
double d = b;
Byte obj = new Byte(b);
if (obj.doubleValue() == d)
return new Pass();
else
return new Fail();
}
}
public static class equalsTest1 implements Test
{
public String getName() {
return "Byte.equals(Object)";
}
public Result test() {
Byte obj1 = null, obj2 = null;
obj1 = new Byte((byte)1);
obj2 = new Byte((byte)2);
if (obj1.equals(obj2))
return new Fail("1 != 2");
else
return new Pass("1 != 2");
}
}
public static class equalsTest2 implements Test
{
public String getName() {
return "Byte.equals(Object)";
}
public Result test() {
Byte obj1 = null, obj2 = null;
obj1 = new Byte((byte)1);
obj2 = new Byte((byte)2);
obj2 = obj1;
if (obj1.equals(obj2))
return new Pass("1 == 1");
else
return new Fail("1 == 1");
}
}
public static class floatValueTest implements Test
{
public String getName() {
return "Byte.floatValue()";
}
public Result test() {
byte b = 4;
float f = b;
Byte obj = new Byte(b);
if (obj.floatValue() == f)
return new Pass();
else
return new Fail();
}
}
public static class hashCodeTest implements Test
{
public String getName() {
return "Byte.hashCode()";
}
public Result test() {
boolean caught = false;
Byte obj = new Byte((byte)1);
int i = obj.hashCode();
if (i == 1)
return new Pass();
else
return new Fail("hash is " + i + ". It should be 1.");
}
}
public static class intValueTest implements Test
{
public String getName() {
return "Byte.intValue()";
}
public Result test() {
byte b = 4;
int i = b;
Byte obj = new Byte(b);
if (obj.intValue() == i)
return new Pass();
else
return new Fail();
}
}
public static class longValueTest implements Test
{
public String getName() {
return "Byte.longValue()";
}
public Result test() {
byte b = 4;
long l = b;
Byte obj = new Byte(b);
if (obj.longValue() == l)
return new Pass();
else
return new Fail();
}
}
public static class parseByteTest1 implements Test
{
public String getName() {
return "Byte.parseByte(String)";
}
public Result test() {
byte b = Byte.parseByte("1");
if (b == (byte)1)
return new Pass();
else
return new Fail();
}
}
public static class parseByteTest2 implements Test
{
public String getName() {
return "Byte.parseByte(String, int)";
}
public Result test() {
byte b = Byte.parseByte("-4", 10);
if (b == (byte)-4)
return new Pass();
else
return new Fail();
}
}
public static class shortValueTest implements Test
{
public String getName() {
return "Byte.shortValue()";
}
public Result test() {
byte b = 4;
short s = b;
Byte obj = new Byte(b);
if (obj.shortValue() == s)
return new Pass();
else
return new Fail();
}
}
public static class toStringTest1 implements Test
{
public String getName() {
return "Byte.toString()";
}
public Result test() {
Byte obj = new Byte((byte)-2);
String x = obj.toString();
if (x.equals("-2"))
return new Pass();
else
return new Fail();
}
}
public static class toStringTest2 implements Test
{
public String getName() {
return "Byte.toString(byte)";
}
public Result test() {
String x = Byte.toString((byte)-2);
if (x.equals("-2"))
return new Pass();
else
return new Fail();
}
}
public static class valueOfTest1 implements Test
{
public String getName() {
return "Byte.valueOf(String, int)";
}
public Result test() {
Byte obj1 = Byte.valueOf("2",10);
Byte obj2 = new Byte((byte)2);
if (obj1.intValue() == obj2.intValue())
return new Pass();
else
return new Fail();
}
}
public static class valueOfTest2 implements Test
{
public String getName() {
return "Byte.valueOf(String)";
}
public Result test() {
Byte obj1 = Byte.valueOf("2");
if (obj1.intValue() == 2)
return new Pass();
else
return new Fail();
}
}
public static class variablesTest1 implements Test
{
public String getName() {
return "Byte.MIN_VALUE";
}
public Result test() {
byte min = Byte.MIN_VALUE;
byte max = Byte.MAX_VALUE;
if (min == (byte)-128)
return new Pass("Byte.MIN_VALUE is -128");
else
return new Fail("Byte.MIN_VALUE is " + min + " != -128");
}
}
public static class variablesTest2 implements Test
{
public String getName() {
return "Byte.MAX_VALUE";
}
public Result test() {
byte min = Byte.MIN_VALUE;
byte max = Byte.MAX_VALUE;
if (max == (byte)127)
return new Pass("Byte.MAX_VALUE is 127");
else
return new Fail("Byte.MAX_VALUE is " + max + " != 127");
}
}
public static class variablesTest3 implements Test
{
public String getName() {
return "Byte.TYPE.getName()";
}
public Result test() {
String x = Byte.TYPE.getName();
if (x.equals("byte") != true)
return new Fail("Byte.TYPE.getName() is " + x + " != byte");
else
return new Pass("Byte.TYPE.getName() is byte");
}
}
public static class typeInstance implements Test
{
public String getName() {
return "Byte.TYPE.newInstance()";
}
public Result test() {
try {
Object b = Byte.TYPE.newInstance();
return new Fail("Byte.TYPE.newInstance succeeded.");
}
catch (InstantiationException e) {
return new Pass("Byte.TYPE.newInstance failed with exception '" +
e.toString() + "'");
}
catch (Exception ex) {
return new Fail("Byte.TYPE.newInstance threw incorrect exception '"
+ ex.toString() + "'");
}
}
}
}

View file

@ -0,0 +1,170 @@
public class CastTest
{
public static void main(String args[])
{
d2d();
l2d2l();
d2l2d();
f2d2f();
d2f2d();
i2f2i();
l2f2l();
f2l2f();
}
static void d2d()
{
String msg = "double -> double ";
try {
double dvalue1 = 4.2;
double dvalue2 = (double)dvalue1;
if (dvalue1 != dvalue2)
failed(msg + dvalue1 + " != " + dvalue2);
else
passed(msg + dvalue1 + " == " + dvalue2);
}
catch (Exception e)
{
failed(msg + " : exception " + e.toString());
}
}
static void l2f2l()
{
String msg = "long -> float -> long ";
try {
long lvalue = 123;
float fvalue = (float)lvalue;
long lvalue2 = (long)fvalue;
if (lvalue != lvalue2)
failed(msg + lvalue + " != " + lvalue2 + " (float)" + fvalue);
else
passed(msg + lvalue + " == " + lvalue2 + " (float)" + fvalue);
}
catch (Exception e)
{
failed(msg + " : exception " + e.toString());
}
}
static void i2f2i()
{
String msg = "int -> float -> int ";
try {
int ivalue = 123;
float fvalue = (float)ivalue;
int ivalue2 = (int)fvalue;
if (ivalue != ivalue2)
failed(msg + ivalue + " != " + ivalue2 + " (float)" + fvalue);
else
passed(msg + ivalue + " == " + ivalue2 + " (float)" + fvalue);
}
catch (Exception e)
{
failed(msg + " : exception " + e.toString());
}
}
static void f2d2f()
{
String msg = "float -> double -> float ";
try {
float fvalue = 123.0f;
double dvalue = (double)fvalue;
float fvalue2 = (float)dvalue;
if (fvalue != fvalue2)
failed(msg + fvalue + " != " + fvalue2 + " (double)" + dvalue);
else
passed(msg + fvalue + " == " + fvalue2 + " (double)" + dvalue);
}
catch (Exception e)
{
failed(msg + " : exception " + e.toString());
}
}
static void f2l2f()
{
String msg = "float -> long -> float ";
try {
float fvalue = 123.0f;
long lvalue = (long)fvalue;
float fvalue2 = (float)lvalue;
if (fvalue != fvalue2)
failed(msg + fvalue + " != " + fvalue2 + " (long)" + lvalue);
else
passed(msg + fvalue + " == " + fvalue2 + " (long)" + lvalue);
}
catch (Exception e)
{
failed(msg + " : exception " + e.toString());
}
}
static void d2f2d()
{
String msg = "double -> float -> double ";
try {
double dvalue = 123.0;
float fvalue = (float)dvalue;
double dvalue2 = (double)fvalue;
if (dvalue != dvalue2)
failed(msg + dvalue + " != " + dvalue2 + " (float)" + fvalue);
else
passed(msg + dvalue + " == " + dvalue2 + " (float)" + fvalue);
}
catch (Exception e)
{
failed(msg + " : exception " + e.toString());
}
}
static void l2d2l()
{
String msg = "long -> double -> long ";
try {
long lvalue = 1023;
double dvalue = (double)lvalue;
long lvalue2 = (long)dvalue;
if (lvalue != lvalue2)
failed(msg + lvalue + " != " + lvalue2 + " (double)" + dvalue);
else
passed(msg + lvalue + " == " + lvalue2 + " (double)" + dvalue);
}
catch (Exception e)
{
failed(msg + " : exception " + e.toString());
}
}
static void d2l2d()
{
String msg = "double -> long -> double ";
try {
double dvalue = 123.0;
long lvalue = (long)dvalue;
double dvalue2 = (double)lvalue;
if (dvalue != dvalue2)
failed(msg + dvalue + " != " + dvalue2 + " (long)" + lvalue);
else
passed(msg + dvalue + " == " + dvalue2 + " (long)" + lvalue);
}
catch (Exception e)
{
failed(msg + " : exception " + e.toString());
}
}
static void passed(String msg)
{
System.out.println("PASSED: "+msg);
}
static void failed(String msg)
{
System.out.println("FAILED: "+msg);
}
}

View file

@ -0,0 +1,25 @@
public class ClassForNameTest
{
public static void main(String args[]) {
Class c;
/* test for both success and failure */
try {
c = Class.forName("ClassForNameTest");
}
catch (Exception e) {
System.out.println("FAILED: Couldn't find ClassForNameTest.");
System.exit(0);
}
try {
c = Class.forName("ClazzForNameT3st");
}
catch (Exception e) {
System.out.println("PASSED: passed both success and failure cases for Class.forName");
System.exit(0);
}
System.out.println("FAILED: Didn't raise exception for incorrect class name.");
}
}

View file

@ -0,0 +1,21 @@
public class ExceptionTest
{
static int foo() throws ArrayIndexOutOfBoundsException {
int f[] = new int[10];
return f[26];
}
public static void main (String args[]) {
int f;
try {
f = foo();
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("PASSED: " + e.toString());
} catch (Exception e) {
System.out.println("FAILED: " + e.toString());
}
}
}

View file

@ -0,0 +1,19 @@
public class FloatingDecimalTest
{
public static void main(String args[]) {
/*
try {
*/
double d = 1.0;
String result;
result = "Double is " +d + " and kicking";
System.out.println("PASSED: "+result);
/*
}
catch (Exception e)
{
System.out.println("FAILED: exception " + e.toString());
}
*/
}
}

View file

@ -0,0 +1,32 @@
class IsInstanceTest extends Thread implements Cloneable
{
static void main(String args[])
{
IsInstanceTest test = new IsInstanceTest();
if (test instanceof java.lang.Object)
pass("IsInstanceTest is instance of java.lang.Object");
else
fail("IsInstanceTest is not instance of java.lang.Object");
if (test instanceof java.lang.Cloneable)
pass("IsInstanceTest is instance of java.lang.Cloneable");
else
fail("IsInstanceTest is not instance of java.lang.Cloneable");
if (test instanceof java.lang.Runnable)
pass("IsInstanceTest is instance of java.lang.Runnable");
else
fail("IsInstanceTest is not instance of java.lang.Runnable");
}
static void pass(String message)
{
System.out.println("PASSED: "+message);
}
static void fail(String message)
{
System.out.println("FAILED: "+message);
}
}

View file

@ -0,0 +1,77 @@
public class JoinTest
implements Runnable
{
public static int count = 0;
void send()
throws Exception
{
Thread.sleep(2000);
System.out.println("PASSED: Sender completed");
}
void receive()
throws Exception
{
synchronized(this) {
notifyAll();
}
Thread.sleep(5000);
count++;
System.out.println("PASSED: Receiver completed");
}
public void run()
{
String name = Thread.currentThread().getName();
if (name.equals("timer")) {
try {
Thread.sleep(10000);
} catch (InterruptedException e){}
System.out.println("FAILED: timer triggered");
System.exit(1);
}
try {
receive();
} catch (Exception e) {
System.out.println("FAILED: receiver: " + e);
System.exit(1);
}
}
public static void main(String args[])
{
try {
JoinTest sender =
new JoinTest();
JoinTest receiver =
new JoinTest();
Thread receiver_thread = new Thread(receiver);
/* Make sure the test terminates even if it hangs on network */
JoinTest timer = new JoinTest();
Thread timer_thread = new Thread(timer, "timer");
timer_thread.start();
synchronized(receiver) {
receiver_thread.start();
receiver.wait();
}
try {
sender.send();
} catch (Exception e) {
System.out.println("FAILED: sender: " + e);
System.exit(1);
}
receiver_thread.join();
if (0 == count)
throw new Exception("Nothing received");
System.out.println("PASSED: Join send/receive count="+count);
System.exit(0);
} catch (Exception e) {
System.out.println("FAILED: " + e);
System.exit(1);
}
}
}

View file

@ -0,0 +1,12 @@
public class LongFieldTest
{
static long field;
public static void main(String args[])
{
field = 1L;
if (field == 1)
System.out.println("PASSED: field = " + field);
}
}

View file

@ -0,0 +1,24 @@
public class NewInstanceTest
{
public NewInstanceTest() {
static_field = 1;
}
public static void main(String args[]) {
try {
Class cls = Class.forName("NewInstanceTest");
Object instance = cls.newInstance();
if (static_field == 1)
System.out.println("PASSED: static_field = " + static_field);
else
System.out.println("FAILED: static_field = " + static_field);
}
catch (Exception e)
{
System.out.println("FAILED: exception " + e.toString());
}
}
public static int static_field;
}

View file

@ -0,0 +1,20 @@
import java.lang.*;
public class NullcastTest
{
static String retString(String str1, String str2)
{
return str1;
}
public static void main (String args[]) {
try {
String tmp = retString((String) null, (String)null);
System.out.println("PASSED: (String)null");
System.exit(0);
} catch (Exception e) {
System.out.println("FAILED: "+e);
}
}
}

View file

@ -0,0 +1,64 @@
import java.util.Vector;
/**
* Under JavaSoft's VM they arbitarily limit the amount of memory
* a Java application can use (though this can be overridden). The
* point here is to check to see whether or not an application being
* run by Japhar will ever get the OutOfMemoryError or not when resources
* are scarce. --brian
*/
public class OutOfMemoryErrorTest
{
public static void main(String[] argv)
{
Vector v = null;
Runtime r = null;
long free = 0, total = 0;
// quickly approach memory limit 1M at a time
try {
r = Runtime.getRuntime();
v = new Vector();
while(true)
{
v.addElement(new byte[1048576]);
}
}
// out of memory error
catch (OutOfMemoryError oomerr1)
{
// slowly encroach on memory limit 2 bytes+ at a time
try {
while(true)
{
v.addElement(new byte[2]);
}
}
// out of memory error
catch (OutOfMemoryError oomerr2)
{
if (r != null)
{
free = r.freeMemory();
total = r.totalMemory();
v = null;
r.gc();
// System.out.println("free = " + free);
// System.out.println("total = " + total);
System.out.println("PASSED: ");
}
else
System.out.println("FAILED: runtime unknown");
}
}
// generic error
catch (Error err)
{
System.out.println("FAILED: unexpected error");
}
// generic exception
catch (Exception e)
{
System.out.println("FAILED: unexpected exception");
}
}
}

View file

@ -0,0 +1,24 @@
/*
* Aaron M. Renn reported a bug in Japhar having string length 17 for
* this string
*/
public class StringTest
{
public static void
main(String[] argv)
{
UnicodeStringLength();
}
static void UnicodeStringLength()
{
String str = "a-->\u01FF\uA000\u6666\u0200RRR";
int len = str.length();
if (11 == len) {
System.out.println("PASSED: " + str + " has len=" +str.length());
} else {
System.out.println("FAILED: " + str +
" has len=" +str.length() + " != 11");
}
}
}

View file

@ -0,0 +1,59 @@
public class SyncronizedTest
implements Runnable
{
public static int count = 0;
String _name;
public SyncronizedTest(String name)
{
_name = name;
}
public void run()
{
if (_name.equals("timer")) {
try {
Thread.sleep(10000);
} catch (InterruptedException e){}
System.out.println("FAILED: timer triggered");
System.exit(1);
}
try {
count++;
synchronized(this) {
notifyAll();
}
} catch (Exception e) {
System.out.println("FAILED: receiver: " + e);
System.exit(1);
}
}
public static void main(String args[])
{
try {
SyncronizedTest tester = new SyncronizedTest("tester");
Thread tester_thread = new Thread(tester);
SyncronizedTest timer = new SyncronizedTest("timer");
Thread timer_thread = new Thread(timer);
timer_thread.start();
synchronized(tester) {
tester_thread.start();
tester.wait();
}
if (0 == count)
throw new Exception("Thread did not run.");
tester_thread.join();
System.out.println("PASSED: count="+count);
System.exit(0);
} catch (Exception e) {
System.out.println("FAILED: " + e);
System.exit(1);
}
}
}

View file

@ -0,0 +1,477 @@
/* Written by Artur Biesiadowski <abies@pg.gda.pl> */
/*
This class test basic 4 conversion types and compares results to ready ones, done
on sure VM (suns JDK). Conversions are
(obj instanceof clazz)
(clazz)obj
clazz.isInstance(obj)
clazz1.isAssignableFrom(clazz2);
Hopefully all needed cases are covered. If you want to add object just put it
into objs table. If you want to add class, you need to add it to both cls and to
testCode method. Of course you need to regenerate results after that.
*/
/*
You can copy/modify/use this file for any purposes, as long as you do not delete
my name from top of that file. Of course you can add your own below that :)
*/
import java.io.*;
interface I1 {}
interface I2 {}
interface I3 extends I2{}
class A1 implements I1 {}
class AB12 extends A1 implements I2 {}
class ABC12 extends AB12 {}
class D3 implements I3 {}
public class TestCasts
{
public Object objs[] =
{
null,
new Object(),
new A1(),
new AB12(),
new ABC12(),
new D3(),
new A1[1],
new AB12[1],
new ABC12[1],
new D3[1],
new I1[1],
new I2[1],
new I3[1],
new int[1],
new A1[1][1],
new AB12[1][1],
new I1[1][1]
};
public Class cls[] =
{
Object.class,
A1.class,
AB12.class,
ABC12.class,
D3.class,
I1.class,
I2.class,
I3.class,
Cloneable.class,
Serializable.class,
A1[].class,
AB12[].class,
ABC12[].class,
D3[].class,
I1[].class,
I2[].class,
I3[].class,
int[].class,
A1[][].class,
AB12[][].class,
I1[][].class
};
java.util.Vector results = new java.util.Vector(1000);
boolean verbose = false;
boolean generate = false;
String filename = "TestCasts-results.txt";
public static void main(String argv[] )
{
TestCasts tc = new TestCasts();
if ( argv.length > 0 )
{
int i;
for ( i =0; i < argv.length;i++ )
{
if ( argv[i].equals("-g") )
{
tc.generate = true;
}
else if ( argv[i].equals("-v") )
{
tc.verbose = true;
}
else if ( argv[i].equals("-f") )
{
i++;
if ( i > argv.length )
{
System.out.println("You need to specify filename after -f");
System.exit(1);
}
tc.filename = argv[i];
}
else
{
System.out.println( "Options are: -v -g -f file");
System.out.println( "[-v] verbose ");
System.out.println( "[-g] generate result table");
System.out.println( "[-f file] read/write tests from/to file (default "+tc.filename+")");
System.exit(1);
}
}
}
tc.test();
//System.out.println(tc.results);
System.out.println( "Performed " + tc.counter + " tests");
if ( tc.generate )
System.out.println( "True: " + tc.genTrue + "\tfalse: " + tc.genFalse);
else
{
System.out.println( "Passed: " + tc.passed + "\tfailed: " + tc.failed);
if (tc.failed == 0 )
System.out.println("PASSED: all cast tests");
}
}
public final void test()
{
if (!generate)
readResultsFromFile();
int i;
int j;
for ( i=0; i < objs.length; i++ )
{
for ( j=0; j < cls.length; j++ )
{
reportClIsInst(objs[i], cls[j], cls[j].isInstance(objs[i]) );
}
}
for (i=0; i < objs.length; i++ )
{
testCode(objs[i]);
}
for ( i=0; i < cls.length; i++ )
{
for ( j=0; j < cls.length; j++ )
{
reportClIsAssign(cls[i], cls[j], cls[i].isAssignableFrom(cls[j]));
}
}
if ( generate )
writeResultsToFile();
}
public final void testCode(Object o)
{
reportInstanceof(o, Object.class, (o instanceof Object) );
try
{
Object r1 = (Object) o;
reportCast(o, Object.class, true );
} catch (ClassCastException e) {
reportCast(o,Object.class, false );
}
reportInstanceof(o, A1.class, (o instanceof A1) );
try
{
A1 r1 = (A1) o;
reportCast(o, A1.class, true );
} catch (ClassCastException e) {
reportCast(o,A1.class, false );
}
reportInstanceof(o, AB12.class, (o instanceof AB12) );
try
{
AB12 r1 = (AB12) o;
reportCast(o, AB12.class, true );
} catch (ClassCastException e) {
reportCast(o,AB12.class, false );
}
reportInstanceof(o, ABC12.class, (o instanceof ABC12) );
try
{
ABC12 r1 = (ABC12) o;
reportCast(o, ABC12.class, true );
} catch (ClassCastException e) {
reportCast(o,ABC12.class, false );
}
reportInstanceof(o, D3.class, (o instanceof D3) );
try
{
D3 r1 = (D3) o;
reportCast(o, D3.class, true );
} catch (ClassCastException e) {
reportCast(o,D3.class, false );
}
reportInstanceof(o, I1.class, (o instanceof I1) );
try
{
I1 r1 = (I1) o;
reportCast(o, I1.class, true );
} catch (ClassCastException e) {
reportCast(o,I1.class, false );
}
reportInstanceof(o, I2.class, (o instanceof I2) );
try
{
I2 r1 = (I2) o;
reportCast(o, I2.class, true );
} catch (ClassCastException e) {
reportCast(o,I2.class, false );
}
reportInstanceof(o, I3.class, (o instanceof I3) );
try
{
I3 r1 = (I3) o;
reportCast(o, I3.class, true );
} catch (ClassCastException e) {
reportCast(o,I3.class, false );
}
reportInstanceof(o, Cloneable.class, (o instanceof Cloneable) );
try
{
Cloneable r1 = (Cloneable) o;
reportCast(o, Cloneable.class, true );
} catch (ClassCastException e) {
reportCast(o,Cloneable.class, false );
}
reportInstanceof(o, Serializable.class, (o instanceof Serializable) );
try
{
Serializable r1 = (Serializable) o;
reportCast(o, Serializable.class, true );
} catch (ClassCastException e) {
reportCast(o,Serializable.class, false );
}
reportInstanceof(o, A1[].class, (o instanceof A1[]) );
try
{
A1[] r1 = (A1[]) o;
reportCast(o, A1[].class, true );
} catch (ClassCastException e) {
reportCast(o,A1[].class, false );
}
reportInstanceof(o, AB12[].class, (o instanceof AB12[]) );
try
{
AB12[] r1 = (AB12[]) o;
reportCast(o, AB12[].class, true );
} catch (ClassCastException e) {
reportCast(o,AB12[].class, false );
}
reportInstanceof(o, ABC12[].class, (o instanceof ABC12[]) );
try
{
ABC12[] r1 = (ABC12[]) o;
reportCast(o, ABC12[].class, true );
} catch (ClassCastException e) {
reportCast(o,ABC12[].class, false );
}
reportInstanceof(o, D3[].class, (o instanceof D3[]) );
try
{
D3[] r1 = (D3[]) o;
reportCast(o, D3[].class, true );
} catch (ClassCastException e) {
reportCast(o,D3[].class, false );
}
reportInstanceof(o, I1[].class, (o instanceof I1[]) );
try
{
I1[] r1 = (I1[]) o;
reportCast(o, I1[].class, true );
} catch (ClassCastException e) {
reportCast(o,I1[].class, false );
}
reportInstanceof(o, I2[].class, (o instanceof I2[]) );
try
{
I2[] r1 = (I2[]) o;
reportCast(o, I2[].class, true );
} catch (ClassCastException e) {
reportCast(o,I2[].class, false );
}
reportInstanceof(o, I3[].class, (o instanceof I3[]) );
try
{
I3[] r1 = (I3[]) o;
reportCast(o, I3[].class, true );
} catch (ClassCastException e) {
reportCast(o,I3[].class, false );
}
reportInstanceof(o, int[].class, (o instanceof int[]) );
try
{
int[] r1 = (int[]) o;
reportCast(o, int[].class, true );
} catch (ClassCastException e) {
reportCast(o,int[].class, false );
}
reportInstanceof(o, A1[][].class, (o instanceof A1[][]) );
try
{
A1[][] r1 = (A1[][]) o;
reportCast(o, A1[][].class, true );
} catch (ClassCastException e) {
reportCast(o,A1[][].class, false );
}
reportInstanceof(o, AB12[][].class, (o instanceof AB12[][]) );
try
{
AB12[][] r1 = (AB12[][]) o;
reportCast(o, AB12[][].class, true );
} catch (ClassCastException e) {
reportCast(o,AB12[][].class, false );
}
reportInstanceof(o, I1[][].class, (o instanceof I1[][]) );
try
{
I1[][] r1 = (I1[][]) o;
reportCast(o, I1[][].class, true );
} catch (ClassCastException e) {
reportCast(o,I1[][].class, false );
}
}
int counter = 0;
int passed = 0;
int failed = 0;
int genTrue = 0;
int genFalse =0;
public final boolean result(boolean b )
{
counter++;
if ( generate )
{
if (b )
{
genTrue++;
results.addElement(Boolean.TRUE);
}
else
{
genFalse++;
results.addElement(Boolean.FALSE);
}
return true;
}
else
{
if ( ((Boolean)results.elementAt(counter-1)).booleanValue() != b )
{
failed++;
return false;
}
else
{
passed++;
return true;
}
}
}
public final void reportClIsInst(Object obj, Class cl, boolean b )
{
if ( result(b) )
{
if ( verbose )
System.out.println("PASSED: "+obj +"\tis\t"+ cl + "\t?" + b);
}
else
{
System.out.println("FAILED: " + cl + ".isInstance(" + obj + ") is\t" + b );
}
}
public final void reportClIsAssign( Class c1, Class c2, boolean b )
{
if ( result(b) )
{
if (verbose)
System.out.println("PASSED: "+c1 + "\tisAssignableFrom\t" + c2 + "\t?\t" + b);
}
else
{
System.out.println("FAILED: " + c1 + ".isAssigableFrom(" + c2 + ") is " + b);
}
}
public final void reportInstanceof( Object obj, Class cl, boolean b )
{
if ( result(b) )
{
if ( verbose )
System.out.println("PASSED: "+obj +"\tinstanceof\t"+ cl + "\t?" + b);
}
else
{
System.out.println("FAILED: (" + obj + "instanceof\t" + cl + ")\tis\t" + b );
}
}
public final void reportCast( Object obj, Class cl, boolean b )
{
if ( result(b) )
{
if ( verbose )
System.out.println("PASSED: "+obj +"\tcastto \t"+ cl + "\t?" + b);
}
else
{
System.out.println("FAILED: " + obj + "\tcastto \t" + cl + "\tis\t" + b );
}
}
public final void readResultsFromFile()
{
try{
int i;
FileInputStream fin = new FileInputStream(filename);
while ( (i=fin.read()) != -1 )
{
results.addElement( i==1 ? Boolean.TRUE : Boolean.FALSE );
}
} catch (IOException e )
{
System.out.println("Cannot read from file " + filename);
System.out.println(e);
System.exit(1);
}
}
public final void writeResultsToFile()
{
try{
int i;
FileOutputStream fos = new FileOutputStream(filename);
for ( i=0; i < counter; i++ )
{
fos.write( ((Boolean)results.elementAt(i)).booleanValue() ? 1 : 0 );
}
fos.close();
} catch (IOException e )
{
System.out.println("Cannot read from file " + filename);
System.out.println(e);
System.exit(1);
}
}
}

View file

@ -0,0 +1,48 @@
import java.lang.*;
/* Simple producer/consumer thread test. */
public class ThreadTest implements Runnable {
static String threadName = "Running thread";
static int count = 0;
static int max = 4; // XXX Seem to fail when >4 on kaffe 0.9.0
public void run() {
if (! Thread.currentThread().isAlive() ) {
System.out.println("FAILED: isAlive() false in new thread!");
} else {
System.out.println("PASSED: isAlive() working in new thread");
}
while (0 <= count && count <= max) {
count ++;
}
}
public static void main (String args[]) {
try {
if (! Thread.currentThread().isAlive() ) {
System.out.println("FAILED: isAlive() false in initial thread!");
} else {
System.out.println("PASSED: isAlive() working in initial thread");
}
ThreadTest test = new ThreadTest();
Thread testThread = new Thread(test, threadName);
testThread.setDaemon(true);
testThread.start();
Thread.currentThread().sleep(3000);
if (count < max) {
System.out.println("FAILED: unable to run new thread");
} else {
System.out.println("PASSED: Theads worked");
}
System.exit(0);
} catch (Exception e) {
System.out.println("FAILED: "+e);
}
}
}

View file

@ -0,0 +1,7 @@
#
# Author: Petter Reinholdtsen <pere@td.org.uit.no>
# Load support procs
load_lib java.exp
test-java-source