⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 This is a resource based on j2me embedded,if you dont understand,you can connection with me .
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)Test.java	1.61 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * *//* * Test.java 00/04/25 * Please keep TestExpectedResult in sync. */import sun.misc.CVM;import sun.misc.GC;public class Test {    static int sfoo = 3;    static final int sbar = 6;    static int ssumbar;    static int sprodbar;    static int srecursebar;    int nTrailers;    Test next;    static int nFailure;    static int nSuccess;    static int refUninitIndicator;    static int clinitIndicator;    public static boolean assert0( boolean val, String expr ){	if ( val ){	    nSuccess += 1;	    return true;	} else {	    nFailure += 1;	    System.out.print("*TEST FAILURE: ");	    System.out.println( expr );	    return false;	}    }    /* flags for refUninitIndicator */    static final int finishedTry = 1;    static final int caughtStackOverflow = 2;    static final int didFinally     = 4;    static final int caughtThrowable = 8;    /* flags for clinitIndicator */    static final int clinitA	 = 1;    static final int clinitB	 = 2;    static final int clinitC	 = 3;    static final int clinitD	 = 4;    static final int clinitE	 = 5;    static final int clinitF	 = 6;    static final int clinitG	 = 7;    static final int clinitShift = 3;    /** This class takes as arguments the names of System properties	to get and print out, thereby testing the command line parsing	in CVM.initialize() as well as the passing of arguments into	main(). */    public static void main(String argv[]) {	nFailure = 0;	nSuccess = 0;	refUninitIndicator = 0;	clinitIndicator = 0;	System.out.println("*Number of command line arguments: " + argv.length);	for (int i = 0; i < argv.length; i++) {	    System.out.println("*"+ i + ": System.getProperty(\"" +			       argv[i] + "\") = " +			       System.getProperty(argv[i]));	}	String a = "foobar";	a = a + "bar";	assert0( a.intern() == "foobarbar", "Trivial string intern");	System.out.print("Starting test1\n");	int oldvalue = CVM.setDebugFlags(CVM.DEBUGFLAG_TRACE_METHOD |					 CVM.DEBUGFLAG_TRACE_GCALLOC);	test1(false);	CVM.restoreDebugFlags(CVM.DEBUGFLAG_TRACE_METHOD |			      CVM.DEBUGFLAG_TRACE_GCALLOC, oldvalue);	/* Now test out the java.lang.System version of the method tracing */	System.out.print("Starting test1 again\n");	Runtime.getRuntime().traceMethodCalls(true);	test1(true);	Runtime.getRuntime().traceMethodCalls(false);	test3();	internTest();	test5();	test6();	testArrayCopy();	testStaticInitalizers();	assert0( clinitIndicator ==	    ( (((((((clinitD<<clinitShift)+clinitC)<<clinitShift)+clinitE)<<clinitShift)+clinitF)<<clinitShift)+clinitG ),	    "<clinit> test unexpected result" );	testArrayClassAccess();	testCloning();	testFloatBits();	testDeepArrayConstruction();	/*	 * This had better run flawlessly -- we implement code rewriting now	 */	refUninitConflictTest();	assert0( refUninitIndicator == (didFinally|caughtThrowable),	    "refUninitConflictTest unexpected path "+refUninitIndicator );		/*	 * Here is a method with a ref-val conflict which we don't yet	 * know how to handle.	 *	 */	refValConflictTest(5);	testSunMiscGC(); 	testManyFieldsAndMethods();	if ( nFailure == 0 ){	    System.out.print("\n*CONGRATULATIONS: ");	} else {	    System.out.print("\n*TOO BAD: ");	}	System.out.print("test Test completed with ");	System.out.print( nSuccess );	System.out.print( " tests passed and " );	System.out.print( nFailure );	System.out.println( " failures" );	System.out.println("*Output lines starting with a * should be checked for correctness");	System.out.println("*They can be compared to src/share/javavm/test/TestExpectedResult" );    }    /*     * Test the sun.misc.GC class, and check it by issuing a latency     * request on GC. We should be seeing full GC's happening.     */    static void testSunMiscGC() {	System.err.println("Requesting GC with a latency request of 2 seconds");	System.err.println("(Turning GC tracing on)");	int oldvalue = 0;	GC.LatencyRequest latencyRequest = GC.requestLatency((long)2000);	System.err.println("Sleeping 5 seconds, and waiting for GC's");	try {	    oldvalue = CVM.setDebugFlags(CVM.DEBUGFLAG_TRACE_GCSTARTSTOP);	    Thread.sleep((long)5000);	} catch (InterruptedException e) {	}	CVM.restoreDebugFlags(CVM.DEBUGFLAG_TRACE_GCSTARTSTOP, oldvalue);	System.err.println("Woke up! Cancelling latency request");	latencyRequest.cancel();    }	    /*     * This method involves a Ref-uninit conflict. Use it to test     * handling of conflicts.     */    static void refUninitConflictTest() {	try {	    Object[] o1 = new Object[5];	    Object[] o2 = new Object[7];	    int flag;	    try {		int i = 4;		o1[8] = null;		System.arraycopy(o2, 2, o1, 3, 8);		refUninitIndicator = finishedTry;	    } catch (StackOverflowError e) {		//System.out.println("caught!");		flag = 1;		e.printStackTrace();		refUninitIndicator |= caughtStackOverflow;	    } finally {		flag = 2;		Object o = new Object();		refUninitIndicator |= didFinally;		System.out.println("finally!");	    }	} catch (Throwable e) {	    // ignore.	    refUninitIndicator |= caughtThrowable;	}    }    static void refValConflictTest(int param) {	if (param == 0) {	    int val = 5; /* Integer use of a local */	}	/* The reference use of the local above is in the 'any'	   exception handler. The exception object is stored in the same	   local variable assigned to val above. */	try {	    Object o = new int[4];	} finally {	    /* A GC point within a jsr block. Has ref-val conflict. */	    Object o2 = new int[4]; 	}    }    /*     * The following is a refVal conflict in a <clinit> method.     * Since theses are separately allocated, they can be      * separately deallocated, too.     */    private static Object oWhatever = null;    static {	if (oWhatever == null) {	    int val = 5; /* Integer use of a local */	}	/* The reference use of the local above is in the 'any'	   exception handler. The exception object is stored in the same	   local variable assigned to val above. */	try {	    Object o = null;	    o = new int[4];	} finally {	    /* A GC point within a jsr block. Has ref-val conflict. */	    oWhatever = new int[4]; 	}    }    static final int expectedPlusResult = 7;    static final int expectedMultResult = 10;    static final int expectedRecurseResult = 15;    static final int expectedStaticPlusResult = 9;    static final int expectedStaticMultResult = 18;    static final int expectedStaticRecurseResult = 18*19/2;    static final int expectedLinkResult = 3;    // If userRuntime = true, use the java.lang.Runtime version of the opcode    // and method tracing. Otherwise, use the sun.misc.CVM version.    static void test1(boolean useRuntime) {	int foo = 5;	int bar = 2;	int sumbar;	int prodbar;	int recursebar;	int linkval = 0;	sumbar = foo + bar;	prodbar = mult(foo,bar);	recursebar = recurse(5);	ssumbar = sfoo + sbar;	int oldvalue = 0;	if (useRuntime) {	    Runtime.getRuntime().traceInstructions(true);	} else {	    oldvalue = CVM.setDebugFlags(CVM.DEBUGFLAG_TRACE_OPCODE);	}	sprodbar = mult(sfoo,sbar);	if (useRuntime) {	    Runtime.getRuntime().traceInstructions(false);	} else {	    CVM.restoreDebugFlags(CVM.DEBUGFLAG_TRACE_OPCODE, oldvalue);	}	srecursebar = recurse(sprodbar);	Test t = new Test();	linkval = t.link( new Test() );	linkval += t.link( new Test() );	int x = 5;	x += -1;	x += 1;	oldvalue = CVM.clearDebugFlags(-1);	assert0( sumbar == expectedPlusResult, "test1: sumbar = foo+bar" );	assert0( prodbar == expectedMultResult, "test1: prodbar = mult(foo,bar)" );	assert0( recursebar == expectedRecurseResult, "test1: recursebar = recurse(5)" );	assert0( ssumbar == expectedStaticPlusResult, "test1: ssumbar = sfoo+sbar" );	assert0( sprodbar == expectedStaticMultResult, "test1: sprodbar = mult(sfoo,sbar)" );	assert0( srecursebar == expectedStaticRecurseResult, "test1: srecursebar = recurse(sprodbar)" );	assert0( linkval == expectedLinkResult, "test1: number of t.link calls" );	assert0( x == 5, "test1: 5 +/- 1");	CVM.restoreDebugFlags(-1, oldvalue);    }    /*     * A test of basic and multi-dimensional     * array types: read, write and allocation.     */    static void test3() {	char[]    carr = new char[3];    char c;	float[]   farr = new float[3];   float f;	double[]  darr = new double[3];  double d;	int[]     iarr = new int[3];     int i;	long[]    larr = new long[3];    long l;	boolean[] zarr = new boolean[3]; boolean z;	short[]   sarr = new short[3];   short s;	byte[]    barr = new byte[3];    byte b;	Object[]  oarr = new Object[3];  Object o;		char[][][] c3arr = new char[2][5][4];	for (int j = 0; j < 2; j++) {	    for (int k = 0; k < 5; k++) {		for (int m = 0; m < 4; m++) {		    assert0(c3arr[j][k][m] == (char)0,			"test3: Non-zero element in multiarray!!!");		}	    }	}		Object[][] o2arr = new Object[4][];	oarr[0] = carr;	oarr[1] = barr;	oarr[2] = iarr;	o2arr[0] = oarr;	o2arr[1] = oarr;	o2arr[2] = oarr;	o2arr[3] = oarr;		char[][] c2arr = new char[2][1];	c2arr[0] = carr;	c2arr[1] = carr;	c3arr[0] = new char[10][10];	c3arr[1] = new char[10][10];	for (int k = 0; k < 4; k++) {	    for (int j = 0; j < 2; j++) {		o2arr[k][j] = carr;	    }	}	for(int k = 0; k < 3; k++) {	    carr[k] = 'a';	    c = carr[k];	}	for(int k = 0; k < 3; k++) {	    farr[k] = k;	    f = farr[k];	}	for(int k = 0; k < 3; k++) {	    darr[k] = k;	    d = darr[k];	}	for(int k = 0; k < 3; k++) {	    iarr[k] = k;	    i = iarr[k];	}	for(int k = 0; k < 3; k++) {	    larr[k] = k;	    l = larr[k];	}	for(int k = 0; k < 3; k++) {	    zarr[k] = (k == 2);	    z = zarr[k];	}	for(int k = 0; k < 3; k++) {	    sarr[k] = (short)k;	    s = sarr[k];	}	for(int k = 0; k < 3; k++) {	    barr[k] = (byte)k;	    b = barr[k];	}    }    /*     * Simple string testing. Construction, interning.     */    static void internSelf( char v[] ){	String vString = new String( v );	String newV    = vString.intern();	// true will not intern to itself, as there already is one.	// other random strings will intern to themselves.	// but they should all compare equal to their interned selves.	assert0( vString.equals("true") != (vString == newV),	    "internTest: #1 of \"" + vString + "\""  );	assert0( vString.equals(newV),	    "internTest: #2 of \"" + vString + "\"" );    }    static void internTest(){	char tarray[] = new char[4];	tarray[0] = 't';	tarray[1] = 'r';	tarray[2] = 'u';	tarray[3] = 'e';	internSelf( tarray );	tarray[3] = 'X';	internSelf( tarray );	assert0("UniqueStringXYZ" == "UniqueStringXYZ",	    "internTest: #3");	assert0(InternStringHelper1.getUniqueString() == 	    InternStringHelper2.getUniqueString(),	    "internTest: #4");    }    static void callA( simpleA a ){	assert0( a.aMethod() == C.Avalue, "test5: a.aMethod()");    }    static void callB( simpleB b ){	assert0( b.bMethod() == C.Bvalue, "test5: b.bMethod()");    }    static void test5(){	C cObject = new C();	callA( cObject );	callB( cObject );    }        static int mult(int a, int b) {	int z=0;	try {	    z =  a * b;	} catch ( Exception e ){	    z = -1;	}	return z;    }    static int recurse(int count) {	int oldvalue = CVM.clearDebugFlags(-1);	System.out.print("...recurse\n");	CVM.restoreDebugFlags(-1, oldvalue);	if (count > 0)	    return count + recurse(count - 1);	return 0;    }    /*     * Put t on the end of the linked list.     * Increment nTrailers for each element     * already on the list.     */    int link( Test t ){	int oldvalue = CVM.clearDebugFlags(-1);	System.out.print("...link\n");	CVM.restoreDebugFlags(-1, oldvalue);	nTrailers += 1;	if ( next == null ){	    next = t;	    return 1;	} else {	    return 1+next.link( t );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -