Added more tests for NEWARRAY, ANEWARRAY and ARRAYLENGTH instructions
authorJan Vrany <jan.vrany@fit.cvut.cz>
Wed, 24 Jun 2015 06:16:28 +0100
changeset 3464 d90e187e80fd
parent 3463 8f3a7fb2ed7f
child 3465 aaa023d2bb5d
Added more tests for NEWARRAY, ANEWARRAY and ARRAYLENGTH instructions ...especially to test that PC is properly set when an exception occur.
tests/java/src/stx/libjava/tests/vm/ANEWARRAY.java
tests/java/src/stx/libjava/tests/vm/ARRAYLENGTH.java
tests/java/src/stx/libjava/tests/vm/NEWARRAY.java
tools/java/src/stx/libjava/tools/compiler/CompileError.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/java/src/stx/libjava/tests/vm/ANEWARRAY.java	Wed Jun 24 06:16:28 2015 +0100
@@ -0,0 +1,65 @@
+package stx.libjava.tests.vm;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class ANEWARRAY {
+
+	@Test
+	public void test_bounds_01() {
+		Object[] arr;
+		Object o;		
+		arr = new Object[10];
+		
+		try {
+			o = arr[-1];
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		arr = new Object[10];
+		try {
+			arr[-1] = new Object();
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		try {
+			o = arr[11];
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		arr = new Object[10];
+		try {
+			arr[11] = new Object();
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+	}
+	
+	@Test
+	public void test_negative_size_01() {
+		Object[] arr;
+		try {
+			arr = new Object[-1];
+		} catch (NegativeArraySizeException nase) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/java/src/stx/libjava/tests/vm/ARRAYLENGTH.java	Wed Jun 24 06:16:28 2015 +0100
@@ -0,0 +1,36 @@
+package stx.libjava.tests.vm;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class ARRAYLENGTH {
+
+	@Test
+	public void test() {
+		int l;
+		int[] i_arr = null;
+		
+		try {
+			l = i_arr.length;
+			fail();
+		} catch (NullPointerException npe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		byte[] b_arr = null;
+		
+		try {
+			l = b_arr.length;
+			fail();
+		} catch (NullPointerException npe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/java/src/stx/libjava/tests/vm/NEWARRAY.java	Wed Jun 24 06:16:28 2015 +0100
@@ -0,0 +1,472 @@
+package stx.libjava.tests.vm;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class NEWARRAY {
+
+	@Test
+	public void test_bounds_boolean() {
+		boolean[] arr;
+		boolean o;		
+		arr = new boolean[10];
+		
+		try {
+			o = arr[-1];
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		arr = new boolean[10];
+		try {
+			arr[-1] = false;
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		try {
+			o = arr[11];
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		arr = new boolean[10];
+		try {
+			arr[11] = false;
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+	}
+	
+	@Test
+	public void test_negative_size_boolean() {
+		boolean[] arr;
+		try {
+			arr = new boolean[-1];
+		} catch (NegativeArraySizeException nase) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+	}
+	
+	
+	// === char ====
+	@Test
+	public void test_bounds_char() {
+		char[] arr;
+		char o;		
+		arr = new char[10];
+		
+		try {
+			o = arr[-1];
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		arr = new char[10];
+		try {
+			arr[-1] = '0';
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		try {
+			o = arr[11];
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		arr = new char[10];
+		try {
+			arr[11] = '0';
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+	}
+	
+	@Test
+	public void test_negative_size_char() {
+		char[] arr;
+		try {
+			arr = new char[-1];
+		} catch (NegativeArraySizeException nase) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+	}
+	
+	// === byte ====
+	@Test
+	public void test_bounds_byte() {
+		byte[] arr;
+		byte o;		
+		arr = new byte[10];
+		
+		try {
+			o = arr[-1];
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		arr = new byte[10];
+		try {
+			arr[-1] = 0;
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		try {
+			o = arr[11];
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+		
+		arr = new byte[10];
+		try {
+			arr[11] = 0;
+			fail();
+		} catch (ArrayIndexOutOfBoundsException abe) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+	}
+	
+	@Test
+	public void test_negative_size_byte() {
+		byte[] arr;
+		try {
+			arr = new byte[-1];
+		} catch (NegativeArraySizeException nase) {
+			// OK
+		} catch (Exception e) {
+			fail();
+		}
+	}
+	
+	// === short ====
+		@Test
+		public void test_bounds_short() {
+			short[] arr;
+			short o;		
+			arr = new short[10];
+			
+			try {
+				o = arr[-1];
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			arr = new short[10];
+			try {
+				arr[-1] = 0;
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			try {
+				o = arr[11];
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			arr = new short[10];
+			try {
+				arr[11] = 0;
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+		}
+		
+		@Test
+		public void test_negative_size_short() {
+			short[] arr;
+			try {
+				arr = new short[-1];
+			} catch (NegativeArraySizeException nase) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+		}
+		
+		// === int ====
+		@Test
+		public void test_bounds_int() {
+			int[] arr;
+			int o;		
+			arr = new int[10];
+			
+			try {
+				o = arr[-1];
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			arr = new int[10];
+			try {
+				arr[-1] = 0;
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			try {
+				o = arr[11];
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			arr = new int[10];
+			try {
+				arr[11] = 0;
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+		}
+		
+		@Test
+		public void test_negative_size_int() {
+			int[] arr;
+			try {
+				arr = new int[-1];
+			} catch (NegativeArraySizeException nase) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+		}
+		
+		// === long ====
+		@Test
+		public void test_bounds_long() {
+			long[] arr;
+			long o;		
+			arr = new long[10];
+			
+			try {
+				o = arr[-1];
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			arr = new long[10];
+			try {
+				arr[-1] = 0;
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			try {
+				o = arr[11];
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			arr = new long[10];
+			try {
+				arr[11] = 0;
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+		}
+		
+		@Test
+		public void test_negative_size_long() {
+			long[] arr;
+			try {
+				arr = new long[-1];
+			} catch (NegativeArraySizeException nase) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+		}
+		
+		// === float ====
+		@Test
+		public void test_bounds_float() {
+			float[] arr;
+			float o;		
+			arr = new float[10];
+			
+			try {
+				o = arr[-1];
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			arr = new float[10];
+			try {
+				arr[-1] = 0;
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			try {
+				o = arr[11];
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			arr = new float[10];
+			try {
+				arr[11] = 0;
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+		}
+		
+		@Test
+		public void test_negative_size_float() {
+			float[] arr;
+			try {
+				arr = new float[-1];
+			} catch (NegativeArraySizeException nase) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+		}
+		
+		// === double ====
+		@Test
+		public void test_bounds_double() {
+			double[] arr;
+			double o;		
+			arr = new double[10];
+			
+			try {
+				o = arr[-1];
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			arr = new double[10];
+			try {
+				arr[-1] = 0;
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			try {
+				o = arr[11];
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+			
+			arr = new double[10];
+			try {
+				arr[11] = 0;
+				fail();
+			} catch (ArrayIndexOutOfBoundsException abe) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+		}
+		
+		@Test
+		public void test_negative_size_double() {
+			double[] arr;
+			try {
+				arr = new double[-1];
+			} catch (NegativeArraySizeException nase) {
+				// OK
+			} catch (Exception e) {
+				fail();
+			}
+		}		
+}
--- a/tools/java/src/stx/libjava/tools/compiler/CompileError.java	Mon Jun 22 15:51:58 2015 +0100
+++ b/tools/java/src/stx/libjava/tools/compiler/CompileError.java	Wed Jun 24 06:16:28 2015 +0100
@@ -55,7 +55,7 @@
             
             field.set(null, "stx/libjava/tools/compiler/CompileError".toCharArray());
         } catch (SecurityException e) {
-            throw new RuntimeException("Failed to modify ConstantPool.JavaLangErrorConstantPoolName field!",e);            
+            throw new RuntimeException("Failed to modify ConstantPool.JavaLangErrorConstantPoolName field!",e);  bt          
         } catch (NoSuchFieldException e) {
             throw new RuntimeException("Failed to modify ConstantPool.JavaLangErrorConstantPoolName field!",e);
         } catch (IllegalArgumentException e) {