diff options
Diffstat (limited to 'jack-tests/tests/com/android/jack/trycatch')
9 files changed, 501 insertions, 0 deletions
diff --git a/jack-tests/tests/com/android/jack/trycatch/test001/dx/Tests.java b/jack-tests/tests/com/android/jack/trycatch/test001/dx/Tests.java new file mode 100644 index 0000000..79f8bb8 --- /dev/null +++ b/jack-tests/tests/com/android/jack/trycatch/test001/dx/Tests.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.trycatch.test001.dx; + +import org.junit.Assert; +import org.junit.Test; + +import com.android.jack.trycatch.test001.jack.TryCatch; + +/** + * Tests about try/catch. + */ +public class Tests { + + @Test + public void testDiv() { + Assert.assertEquals(0, TryCatch.div(0)); + } + + @Test + public void testNestedDiv() { + Assert.assertEquals(0, TryCatch.nestedDiv(0, 0)); + Assert.assertEquals(-1, TryCatch.nestedDiv(1, 0)); + Assert.assertEquals(-2, TryCatch.nestedDiv(0, 1)); + } + + @Test + public void emptyCatch() { + Assert.assertEquals(0, TryCatch.emptyCatch(0)); + } + + @Test + public void nestedTry() { + Assert.assertEquals(1, TryCatch.nestedTry(0)); + } + + @Test + public void nestedTry2() { + Assert.assertEquals(5, TryCatch.nestedTry2(0)); + } + + @Test + public void nestedTryWithFallThrough() { + Assert.assertEquals(8, TryCatch.nestedTryWithFallThrough(0)); + } + + @Test + public void similarCatches() { + Assert.assertEquals(1, TryCatch.similarCatches()); + } +} diff --git a/jack-tests/tests/com/android/jack/trycatch/test001/jack/TryCatch.java b/jack-tests/tests/com/android/jack/trycatch/test001/jack/TryCatch.java new file mode 100644 index 0000000..501d092 --- /dev/null +++ b/jack-tests/tests/com/android/jack/trycatch/test001/jack/TryCatch.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.trycatch.test001.jack; + +public class TryCatch { + + public static int div(int i) { + try { + int r = 1 / i; + return -1; + } catch (Throwable t) { + return 0; + } + } + + public static int nestedDiv(int i, int j) { + try { + int r1 = 1 / i; + return -1; + } catch (Throwable t1) { + try { + int r2 = 1 / j; + return -2; + } catch (Throwable t2) { + return 0; + } + } + } + + public static int emptyCatch(int i) { + try { + int r = 1 / i; + return -1; + } catch (Throwable t) { + } + return 0; + } + + public static int nestedTry(int i) { + try { + try { + int r = 1 / i; + return -1; + } catch (Throwable t) { + } + } catch (Throwable t) { + return 0; + } + return 1; + } + + public static int nestedTry2(int i) { + int result = 1; + try { + try { + int r = 1 / i; + return -1; + } catch (Throwable t) { + result = 5; + } + } catch (Throwable t) { + return result; + } + return result; + } + + public static int nestedTryWithFallThrough(int i) { + int result = 0; + try { + try { + result = result + 2; + } catch (Throwable t) { + result = 0; + } + result = result + 2; + int r = 1 / i; + } catch (Throwable t) { + result = result + 2; + } + result = result + 2; + return result; + } + + public static int similarCatches() { + int result = 0; + try { + try { + throw new AssertionError(); + } catch (AssertionError t) { + result += 1; + } + } catch (AssertionError t) { + result += 2; + } + return result; + } +} diff --git a/jack-tests/tests/com/android/jack/trycatch/test002/dx/Tests.java b/jack-tests/tests/com/android/jack/trycatch/test002/dx/Tests.java new file mode 100644 index 0000000..ca491e6 --- /dev/null +++ b/jack-tests/tests/com/android/jack/trycatch/test002/dx/Tests.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.trycatch.test002.dx; + +import com.android.jack.trycatch.test002.jack.TryCatch; + +import junit.framework.Assert; + +import org.junit.Test; + +public class Tests { + @Test + public void test001() { + Assert.assertNull(TryCatch.tryWithLoop1()); + } + @Test + public void test002() { + Assert.assertNull(TryCatch.tryWithLoop2()); + } + @Test + public void test003() { + Assert.assertNull(TryCatch.tryWithLoop3()); + } + @Test + public void test004() { + Assert.assertNull(TryCatch.tryWithLoop4()); + } + @Test + public void test005() { + Assert.assertEquals(1, TryCatch.tryWithLoop5()); + } +} diff --git a/jack-tests/tests/com/android/jack/trycatch/test002/jack/TryCatch.java b/jack-tests/tests/com/android/jack/trycatch/test002/jack/TryCatch.java new file mode 100644 index 0000000..dc7c69e --- /dev/null +++ b/jack-tests/tests/com/android/jack/trycatch/test002/jack/TryCatch.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.trycatch.test002.jack; + +import java.util.LinkedList; +import java.util.List; + + +public class TryCatch { + + public static Object tryWithLoop1() { + try { + for(int i = 0; i<1; i++) { + i++; + } + + } catch (RuntimeException e) { + return new Object(); + } + return null; + } + + public static Object tryWithLoop2() { + try { + for(int i = 0; i<1; i++) { + m(i); + } + + } catch (RuntimeException e) { + return null; + } + return new Object(); + } + + + public static Object tryWithLoop3() { + try { + for(int i = 0; i<1; i++) { + i++; + } + + } catch (RuntimeException e) { + } + return null; + } + + public static Object tryWithLoop4() { + try { + for(int i = 0; i<1; i++) { + m(i); + } + + } catch (RuntimeException e) { + } + return null; + } + + private static void m(int i) { + throw new NullPointerException(); + } + + public static int tryWithLoop5() { + List<Integer> values = new LinkedList<Integer>(); + values.add(new Integer(1)); + values.add(new Integer(2)); + values.add(new Integer(3)); + try { + try { + return 1; + } catch (Throwable ex) { + return 2; + } + } finally { + for (Integer i : values) + i.intValue(); + } + } + +} diff --git a/jack-tests/tests/com/android/jack/trycatch/test003/dx/Tests.java b/jack-tests/tests/com/android/jack/trycatch/test003/dx/Tests.java new file mode 100644 index 0000000..84b6f98 --- /dev/null +++ b/jack-tests/tests/com/android/jack/trycatch/test003/dx/Tests.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.trycatch.test003.dx; + +import com.android.jack.trycatch.test003.jack.TryCatch; + +import junit.framework.Assert; + +import org.junit.Test; + +public class Tests { + @Test + public void test001() { + try { + TryCatch.noThrow(); + } catch (Throwable t) { + Assert.fail("No throwable should have been thrown"); + } + } +} diff --git a/jack-tests/tests/com/android/jack/trycatch/test003/jack/TryCatch.java b/jack-tests/tests/com/android/jack/trycatch/test003/jack/TryCatch.java new file mode 100644 index 0000000..69b7d06 --- /dev/null +++ b/jack-tests/tests/com/android/jack/trycatch/test003/jack/TryCatch.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.trycatch.test003.jack; + +public class TryCatch { + + public static void noThrow() { + try { + } catch (Throwable t) { + } + } + +} diff --git a/jack-tests/tests/com/android/jack/trycatch/test004/jack/TryCatch.java b/jack-tests/tests/com/android/jack/trycatch/test004/jack/TryCatch.java new file mode 100644 index 0000000..95bef45 --- /dev/null +++ b/jack-tests/tests/com/android/jack/trycatch/test004/jack/TryCatch.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.trycatch.test004.jack; + +public class TryCatch { + Object mCurApp; + + private class Drawable { + } + + private String loadLabel(Object p) { + return null; + } + + private Drawable loadIcon(Object p) { + return null; + } + + private void setDrawable(int iconId, Drawable d) {} + + private void setText(int actionId, String text) {} + + private String getString(int actionStr, CharSequence appName) { + return null; + } + + // The generated dex should not contain useless 'mov' instructions into empty catch. + void setIconAndText(int iconId, int actionId, int descriptionId, String packageName, + int actionStr, int descriptionStr) { + CharSequence appName = ""; + Drawable appIcon = null; + + try { + appName = loadLabel(null); + appIcon = loadIcon(null); + } catch (Exception e) { + } + + + setDrawable(iconId, appIcon); + setText(actionId, getString(actionStr, appName)); + } +} diff --git a/jack-tests/tests/com/android/jack/trycatch/test005/dx/Tests.java b/jack-tests/tests/com/android/jack/trycatch/test005/dx/Tests.java new file mode 100644 index 0000000..c420202 --- /dev/null +++ b/jack-tests/tests/com/android/jack/trycatch/test005/dx/Tests.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.trycatch.test005.dx; + + +import junit.framework.Assert; + +import org.junit.Test; + +import com.android.jack.trycatch.test005.jack.TryCatch; + +public class Tests { + @Test + public void test001() { + try { + new TryCatch(); + } catch (NullPointerException npe) { + Assert.fail("Npe not caught"); + } + } +} diff --git a/jack-tests/tests/com/android/jack/trycatch/test005/jack/TryCatch.java b/jack-tests/tests/com/android/jack/trycatch/test005/jack/TryCatch.java new file mode 100644 index 0000000..d1a0e3f --- /dev/null +++ b/jack-tests/tests/com/android/jack/trycatch/test005/jack/TryCatch.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.trycatch.test005.jack; + +/** + * Test checking that exception is catched in init block. + */ +public class TryCatch { + { + try { + m(); + } catch (NullPointerException e) { + } + } + + public void m() { + throw new NullPointerException(); + } +} |