Manually merge my AOSP update to the VM tests.

Original change: https://android-review.googlesource.com/32051

Bug: http://code.google.com/p/android/issues/detail?id=21599
Bug: http://code.google.com/p/android/issues/detail?id=21597
Change-Id: I31e440b66b720647afab54ca39fd6eb1bbb0cb60
diff --git a/test/045-reflect-array/src/Main.java b/test/045-reflect-array/src/Main.java
index c70e291..36f8ac3 100644
--- a/test/045-reflect-array/src/Main.java
+++ b/test/045-reflect-array/src/Main.java
@@ -10,6 +10,9 @@
 public class Main {
     public static void main(String[] args) {
         testSingleInt();
+        testSingleChar();
+        testSingleShort();
+        testSingleLong();
         testSingle();
         testMultiInt();
         testMulti();
@@ -33,22 +36,31 @@
         try {
             array[2] = 27;
             throw new RuntimeException("store should have failed");
-        }
-        catch (ArrayIndexOutOfBoundsException abe) {
-        }
+        } catch (ArrayIndexOutOfBoundsException abe) { }
+        try {
+            Array.setInt(intArray, 2, 27);
+            throw new RuntimeException("store should have failed");
+        } catch (ArrayIndexOutOfBoundsException abe) { }
         if (array.length != Array.getLength(intArray) ||
             array.length != 2)
         {
             throw new RuntimeException("bad len");
         }
 
+        Integer x123 = Integer.valueOf(123);
+        Integer x456 = Integer.valueOf(456);
+
+        Array.set(intArray, 0, x123);
+        Array.set(intArray, 1, x456);
+        if (!Array.get(intArray, 0).equals(x123) || !Array.get(intArray, 1).equals(x456)) {
+            throw new RuntimeException("bad 123 or 456");
+        }
+
         int[][] wrongArray;
         try {
             wrongArray = (int[][]) intArray;
             throw new RuntimeException("cast should have failed");
-        }
-        catch (ClassCastException cce) {
-        }
+        } catch (ClassCastException cce) { }
 
         intArray = Array.newInstance(Integer.TYPE, 0);
         if (Array.getLength(intArray) != 0)
@@ -56,6 +68,96 @@
         System.out.println("ReflectArrayTest.testSingleInt passed");
     }
 
+    static void testSingleChar() {
+        Object charArray = Array.newInstance(Character.TYPE, 7);
+
+        char[] array = (char[]) charArray;
+        array[0] = '0';
+        array[1] = 'W';
+        array[2] = '2';
+        array[3] = '3';
+        array[4] = 'X';
+        array[5] = '5';
+        array[6] = '6';
+        Array.setChar(charArray, 1, '1');
+        Array.setChar(charArray, 4, '4');
+        try {
+            Array.setShort(charArray, 3, (short) 'Y');
+            throw new RuntimeException("shouldn't allow short in char array");
+        } catch (IllegalArgumentException iae) {}
+        try {
+            Array.setInt(charArray, 5, 'Z');
+            throw new RuntimeException("shouldn't allow int in char array");
+        } catch (IllegalArgumentException iae) {}
+
+        try {
+            for (int i = 0; i < array.length; i++) {
+                if (Array.getInt(charArray, i) - '0' != i) {
+                    throw new RuntimeException("mismatch: " + i + " is " + array[i]);
+                }
+            }
+
+            if (Array.getInt(charArray, 4) != '4') {
+                throw new RuntimeException("load should have worked");
+            }
+        } catch (IllegalArgumentException iae) {
+            System.err.println("Couldn't Array.getInt(charArray)");
+        }
+        try {
+            Array.getByte(charArray, 2);
+            throw new RuntimeException("shouldn't allow read of char as byte");
+        } catch (IllegalArgumentException iae) {}
+
+        Array.setChar(charArray, 3, (char) 0xffff);
+        try {
+            if (Array.getInt(charArray, 3) != 0xffff) {
+                throw new RuntimeException("char got sign-extended? "
+                    + Array.getInt(charArray, 3));
+            }
+        } catch (IllegalArgumentException iae) {
+            System.err.println("Couldn't Array.getInt(charArray)");
+        }
+
+        System.out.println("ReflectArrayTest.testSingleChar passed");
+    }
+
+    static void testSingleShort() {
+        Object shortArray = Array.newInstance(Short.TYPE, 1);
+        Array.setShort(shortArray, 0, (short) -1);
+        if (Array.getInt(shortArray, 0) != -1) {
+            throw new RuntimeException("short didn't get sign-extended");
+        }
+
+        Short box = (Short) Array.get(shortArray, 0);
+
+        System.out.println("ReflectArrayTest.testSingleShort passed");
+    }
+
+    static void testSingleLong() {
+        Object longArray = Array.newInstance(Long.TYPE, 2);
+        Array.setInt(longArray, 0, 123);
+        Array.setLong(longArray, 1, 0x1122334455667788L);
+        try {
+            Array.getInt(longArray, 0);
+            throw new RuntimeException("shouldn't allow read of long as int");
+        } catch (IllegalArgumentException iae) {}
+
+        long[] array = (long[]) longArray;
+        if (array[0] != 123 || array[1] != 0x1122334455667788L) {
+            throw new RuntimeException();
+        }
+
+        float f = Array.getFloat(longArray, 0);
+        if (f < 122.9 || f > 123.1) {
+            throw new RuntimeException("long-as-float failed - " + f);
+        }
+        if (Array.getLong(longArray, 1) != 0x1122334455667788L) {
+            throw new RuntimeException("long1 failed");
+        }
+
+        System.out.println("ReflectArrayTest.testSingleLong passed");
+    }
+
     static void testSingle() {
         Object strArray;
 
@@ -64,6 +166,10 @@
         String[] array = (String[]) strArray;
         array[0] = "entry zero";
         Array.set(strArray, 1, "entry one");
+        try {
+            Array.set(strArray, 2, "entry two");
+            throw new RuntimeException("store should have failed");
+        } catch (ArrayIndexOutOfBoundsException abe) { }
 
         //System.out.println("array: " + array);
 
@@ -77,6 +183,11 @@
         {
             throw new RuntimeException("bad len");
         }
+
+        try {
+            Array.set(strArray, 0, new Integer(5));
+            throw new RuntimeException("store of Integer should have failed");
+        } catch (IllegalArgumentException iae) {}
         System.out.println("ReflectArrayTest.testSingle passed");
     }