/**
 **/
package test;

/**
 * Test exception handling
 * @author Christian Grothoff
 * @author Hiroshi Yamauchi
 **/
public class TestExceptions
    extends TestBase {

    boolean doOverflow;
    
    public TestExceptions(Harness domain, boolean doOverflow) {
	super("Exceptions",domain);
	this.doOverflow = doOverflow;
    }

    public TestExceptions(Harness domain) {
	this(domain, true);
    }

    public TestExceptions(Harness domain, long disable) {
	this(domain, (disable & TestSuite.DISABLE_STACK_OVERFLOW) == 0);
    }

    public void run() {
	testSimpleTryCatch();
	testSubtypedCatch();
	testMultiCatch();
	testCallerCatch();
	testCallerCatchWithNoUnreachable();
	testNestedTryCatch();
	testNullDereference();
        testNPE();
	testArithmeticException();
	testArrayStore();
	testTrace();
	if (doOverflow)
	    testOverflow();
	testArrayIndex();
    }

    private int id(int x) { return x; }

    public void testArithmeticException() {
	int divisor = id(0);
	int foo = 3;
	try {
	    foo /= divisor;
	    id(foo);
	    COREfail("arithmetic exception not thrown");
	} catch (ArithmeticException e) {
	}
    }

    private void doStore(Object [] arr, Object o, boolean expected)
    {
	try {
	    arr[0] = o;
	    COREassert(!expected, "missing array store exception");
	} catch (ArrayStoreException _) {
	    COREassert(expected, "unexpected array store exception");
	}
    }

    public void testArrayStore() {
	doStore(new String[1], new Object(), true);
	doStore(new Object[1], new String("a string"), false);
	doStore(new Object[1], new Object(), false);
    }

    // Note that we return Object, then cast to Object[].  This is yet
    // another j2c workaround.  If returnNull where known to return an
    // Object[], j2c would devirtualize the toString() call, but omit
    // the check for null receiver.
    private static Object returnNull() { return null; }
    
    public void testNullDereference() {
	setModule("null dereference");
        Object o = returnNull();
	try {
	    o.toString();
	    COREfail("null pointer exception not thrown");
	} catch (NullPointerException npe) {
	    // npe.printStackTrace();
	}
    }

    public void testNPE() {
	setModule("null reference checks");
	Object o = returnNull();
	Object[] oa = (Object[]) returnNull();
	try {
	    synchronized(o) {
                COREfail("NPE check missing");
	    }
	    COREfail("NPE check missing");
	} catch (NullPointerException npe) {
	}
	try {
	    o.toString();
	    COREfail("NPE check missing");
	} catch (NullPointerException npe) {
	}
	try {
	    if (oa[1] == o)
                COREfail("NPE check missing");

	    COREfail("NPE check missing");
	} catch (NullPointerException npe) {
	}
    }


    public void testSimpleTryCatch() {
	setModule("simpleTryCatch");
	AException ae = new AException();
	try {	    
	    if (true)  /* make compiler happy! */
		throw ae;
	    COREfail("this code should be unreachable!");
	} catch (AException aeII) {
	    COREassert(aeII == ae,
		       "exception caught is not exception thrown!");
	}
    }

    public void testSubtypedCatch() {
	setModule("subtypedTryCatch");
	AException ae = new AException();
	try {	    
	    if (true)  /* make compiler happy! */
		throw ae;
	    COREfail("this code should be unreachable!");
	} catch (Exception aeII) {
	    COREassert(aeII == ae,
		       "exception caught is not exception thrown!");
	}
    }

    public void testMultiCatch() {
	setModule("multiCatch");
	BException be = new BException();
	try {	    
	    if (true)  /* make compiler happy! */
		throw be;
	    else
		if (false) 
		    throw new CException();
		else
		    COREfail("this code should be unreachable!");
	} catch (CException ce) {
	    COREfail("wrong catch clause!");
	} catch (BException beII) {
	    COREassert(beII == be,
		       "exception caught is not exception thrown!");
	}
    }

    public void testCallerCatch() {
	setModule("callerCatch");
	try {	    
	    m();
	    COREfail("this code should be unreachable!");
	} catch (CException ce) {
	    COREfail("wrong catch clause!");
	} catch (AException beII) {
	} catch (Exception e) {
	    COREfail("wrong catch clause!");
	}
    }

    public void testCallerCatchWithNoUnreachable() {
	setModule("callerCatchWithNoUnreachable");
	try {	    
	    m();
	    //  method call is last to test if PC updates are handled correctly
	} catch (CException ce) {
	    COREfail("wrong catch clause!");
	} catch (AException beII) {
	} catch (Exception e) {
	    COREfail("wrong catch clause!");
	}
    }



    public void testNestedTryCatch() {
	setModule("nestedTryCatch");
	try {	    
	    try {
		try {
		    m();
		    COREfail("this code should be unreachable!");
		} catch (CException ce) {
		    COREfail("wrong catch clause!");
		}
	    } catch (CException ce) {
		COREfail("wrong catch clause!");
	    }
	} catch (AException beII) {
	} catch (Exception e) {
	    COREfail("wrong catch clause!");
	}
    }

   
    public void testTrace() {
	try {
	    n();
	} catch (Exception e) {
	    StackTraceElement[] ste = e.getStackTrace();
	    for (int i = 0; i < ste.length; i++) {
		ste[i].toString(); // just test if it crashes ...
	    }
	}
    }

    private void overflow() {
	overflow();
    }

    public void testOverflow() {
	setModule("stack overflow");
	try {
	    overflow();
            COREfail("No StackOverFlowError");
	} catch (StackOverflowError sof) {
	}
    }

    public void testArrayIndex() {
        setModule("array range checks");
	int[] ia = new int[5];
	ia[0] = 1;
	try {
	    ia[5] = 1;
	    COREfail("ArrayIndex check bad - index too big");
	} catch (ArrayIndexOutOfBoundsException a) {
	}
	try {
	    ia[-1] = 1;
	    COREfail("ArrayIndex check bad - negative index");
	} catch (ArrayIndexOutOfBoundsException a) {
	}
	try {
	    ia[6] = 1;
	    COREfail("ArrayIndex check bad - index too big ");
	} catch (ArrayIndexOutOfBoundsException a) {
	}
	Object[] oa = new Object[5];
	try {
	    oa[5] = null;
	    COREfail("ArrayIndex check bad - index too big");
	} catch (ArrayIndexOutOfBoundsException a) {
	}
	try {
	    oa[-1] = null;
	    COREfail("ArrayIndex check bad - negative index");
	} catch (ArrayIndexOutOfBoundsException a) {
	}
	try {
	    oa[6] = null;
	    COREfail("ArrayIndex check bad - index too big");
	} catch (ArrayIndexOutOfBoundsException a) {
	}
    }


    /* ************** helper methods ********************** */

    private void n() throws Exception {
	m();
    }


    private void m() throws Exception {
	if (true)
	    throw new BException();
	COREfail("this code should be unreachable!");
    }
    
    /* ************** helper classes ********************** */

    static class AException extends Exception {
	AException() {}
    }

    static class BException extends AException {
	BException() {}
    }

    static class CException extends Exception {
 	CException() {}
   }


} // end of TestException
