#!/bin/java bsh.Interpreter

source("TestHarness.bsh");

oa = new Object [] { "foo", "bar" };
assert( oa.length == 2 && oa[0] instanceof String );

oa = new Object [] { };
assert( oa.length == 0 );

int [][] ia = new int [5][4];
ia[2][2] = 5;

Object [][][] foo = new Object [2][2][2];
assert( foo.length == 2 );
assert( foo[0].length == 2 );
assert( foo[0][0].length == 2 );

assert( ia[2][2] == 5 && ia[2][3] != 5 );

// Some tests with null
oa = new Object [] { "foo", "bar", null };
assert( oa instanceof Object [] );
oa = new Object [] { null, "foo", "bar" };
assert( oa instanceof Object [] );
x = new String[] {null, "foo"};

Object[] test = new Object[] { new Long(2000), "MyDisk", new Double(21.4) }; 
assert( test.length == 3);
assert( test[2] instanceof Double );

Object[] test2 = new Object[] { 
	(Object)new Long(2000), (Object)"MyDisk", (Object)new Double(21.4) }; 


assert( isEvalError("a=new int[] { 2, new Object() }") );

// promotion
long [] la = new long [] { 1L, 2, (byte)3 };
float [] fa = new float [] { 1L, 2, (byte)3, (float)3.3, 3.3f };
double [] da = new double [] { 1L, 2, (byte)3, 3.3, 3.3f, 3.3d };
assert( da[1] == 2.0d );
assert( da[1] == 2.0 );

// double check that declared types don't matter
la2 = new long [] { 1L, 2, (byte)3 };
fa2 = new float [] { 1L, 2, (byte)3, (float)3.3, 3.3f };
da2 = new double [] { 1L, 2, (byte)3, 3.3, 3.3f, 3.3d };

String [][] sa = new String [2][2];
String [] d1sa = new String [] { "foo", "bar" };
String [][] d2sa  = new String [][] { d1sa, d1sa };

// multidimensional initializers
String [][] sa2 = new String [][] { new String [] { "foo" }, new String [] { "foo", "bar" } };

String [][][] sa3 = new String [][][] { 
		new String [][] { 
			new String [] { "foo" }, new String [] { "f", "f", } },
		new String [][] { 
			new String [] { "foo", "bar", "flag" } } 
	};

assert( sa3[1][0][2].equals("flag") );

String [][][] sa4 = new String [][][] {
    new String [][] {
        new String [] { "f", "f", }, new String [] { "foo" } },
    new String [][] {
        new String [] { "foo", "bar", "flag" } }
};

assert( sa4[1][0][2].equals("flag") );

String [][][] sa5 = new String [][][] {
    new String [][] {
        new String [] { "foo", "bar", "flag" } },
    new String [][] {
        new String [] { "f", "f", }, new String [] { "foo" } }
};

assert( sa5[0][0][2].equals("flag") );

Object [][] o= new Object [][] { 
	new Object [] {"me",new Integer(5),new Byte((byte)3)}, 
	new Object [] {"you",new Integer(6),new Byte((byte)2)} }; 

// initializers which rely on the declared type of the variable!
// This is quite a coup! ;);)

String [] d1xa = { "foo", "bar" };
String [][] d2xa  = { d1xa, d1xa };

// will not work in assignment of course
// d2xa  = { d1xa, d1xa };

// Wow!  I am so happy ;)
String [][][] sa99 = 
		{ { { "foo", null }, { "f", "f", } }, { { "foo", "bar", "flag" } } };
assert( sa99[1][0][2].equals("flag") );

// a few more
long [] la = { 1L, 2, (byte)3 };
float [] fa =  { 1L, 2, (byte)3, (float)3.3, 3.3f };
double [] da = { 1L, 2, (byte)3, 3.3, 3.3f, 3.3d };
assert( da[1] == 2.0d );
assert( da[1] == 2.0 );

// Some stuff submitted in the original bug report... thought I'd throw it in.
Object[] oo = { new Object(), new Object() };
oo = new Object[] { new Object(), new Object() };
// doesn't work in 1.1
oo = new Object[][] { { new Object() }, { new Object() } };
// doesn't work in 1.1
oo = new Object[][] { new Object[] { new Object() }, new Object[] { new Object() } };
oo = new Object[] { 0, 1 };
oo = new Object[] { "a", "b"};
oo = new Object[] { 0, "a" };
oo = new Object[] { new Integer(0), new String("a") };
oo = new Object[] { (Object) new Integer(0), (Object) new String("a") };
ov = new Vector();
ov.add(0); 
ov.add("a"); 
oo = ov.toArray();

Integer [][] ua1x = new Integer [5][0];
assert( ua1x[0] instanceof Integer[] );

// partial initialization
Integer [][] ua1 = new Integer [5][];
assert( ua1 instanceof Integer [][] );
assert ( ua1[0] == null );
ua1[0] = new Integer[5];

int [][] ua1a = new int [5][];
assert( ua1a instanceof int [][] );
assert ( ua1a[0] == null );
ua1a[0] = new int[5];

ua2x = new Integer [3][3][3];
assert( ua2x instanceof Integer [][][] );

ua2 = new Integer [3][][];
assert( ua2 instanceof Integer [][][] );
ua2[0] = new Integer [1][1] ;
ua2[0][0] = new Integer [1] ;
ua2[1] = new Integer [][] { {1,2}, {1,2} };

String[][] array2dim1 = new String[10][];

int[][][] primi3 = new int [3][][];
assert( primi3 instanceof int[][][] );

complete();
