diff options
author | jeffhao <jeffhao@google.com> | 2011-09-29 17:41:15 -0700 |
---|---|---|
committer | jeffhao <jeffhao@google.com> | 2011-09-29 17:41:15 -0700 |
commit | 5d1ac920fdaef5d4ec8f66bb734488cd9660b024 (patch) | |
tree | dd372f306ab70f4c86759869b1f74eca62ff6f2b /test/084-class-init | |
parent | c31664f3d82e6cd68275a529a8a73f067a52e8be (diff) | |
download | android_art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.tar.gz android_art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.tar.bz2 android_art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.zip |
Adding old unit tests to test suite.
These tests are copied straight over. They'll still run, but they're
using the old system.
Change-Id: If494519e52ddf858a9febfc55bdae830468cb3c8
Diffstat (limited to 'test/084-class-init')
-rw-r--r-- | test/084-class-init/expected.txt | 8 | ||||
-rw-r--r-- | test/084-class-init/info.txt | 1 | ||||
-rw-r--r-- | test/084-class-init/src/IntHolder.java | 41 | ||||
-rw-r--r-- | test/084-class-init/src/Main.java | 105 | ||||
-rw-r--r-- | test/084-class-init/src/PartialInit.java | 24 | ||||
-rw-r--r-- | test/084-class-init/src/SlowInit.java | 40 |
6 files changed, 219 insertions, 0 deletions
diff --git a/test/084-class-init/expected.txt b/test/084-class-init/expected.txt new file mode 100644 index 0000000000..6e74fbb474 --- /dev/null +++ b/test/084-class-init/expected.txt @@ -0,0 +1,8 @@ +Got expected EIIE for FIELD0 +Got expected NCDFE for FIELD0 +Got expected NCDFE for FIELD1 +SlowInit static block pre-sleep +SlowInit static block post-sleep +MethodThread message +Fields (child thread): 111222333444 +Fields (main thread): 111222333444 diff --git a/test/084-class-init/info.txt b/test/084-class-init/info.txt new file mode 100644 index 0000000000..00fa31b7f8 --- /dev/null +++ b/test/084-class-init/info.txt @@ -0,0 +1 @@ +Test class initialization edge cases and race conditions. diff --git a/test/084-class-init/src/IntHolder.java b/test/084-class-init/src/IntHolder.java new file mode 100644 index 0000000000..4012d6e8b4 --- /dev/null +++ b/test/084-class-init/src/IntHolder.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2010 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. + */ + + +/** + * Holds an int. + */ +public class IntHolder { + private int mValue = 0; + + /** + * Constructs an IntHolder with the specified value. Throws an + * exception if the initial value is less than zero. + */ + public IntHolder(int initialVal) { + if (initialVal < 0) + throw new RuntimeException("negative number"); + + mValue = initialVal; + } + + public int getValue() { + return mValue; + } + public void setValue(int val) { + mValue = val; + } +} diff --git a/test/084-class-init/src/Main.java b/test/084-class-init/src/Main.java new file mode 100644 index 0000000000..f77711302f --- /dev/null +++ b/test/084-class-init/src/Main.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2010 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. + */ + +public class Main { + public static void main(String[] args) { + checkExceptions(); + checkTiming(); + } + + public static void sleep(int msec) { + try { + Thread.sleep(msec); + } catch (InterruptedException ie) { + System.err.println("sleep interrupted"); + } + } + + static void checkExceptions() { + try { + System.out.println(PartialInit.FIELD0); + System.err.println("Construction of PartialInit succeeded unexpectedly"); + } catch (ExceptionInInitializerError eiie) { + System.out.println("Got expected EIIE for FIELD0"); + } + + try { + System.out.println(PartialInit.FIELD0); + System.err.println("Load of FIELD0 succeeded unexpectedly"); + } catch (NoClassDefFoundError ncdfe) { + System.out.println("Got expected NCDFE for FIELD0"); + } + try { + System.out.println(PartialInit.FIELD1); + System.err.println("Load of FIELD1 succeeded unexpectedly"); + } catch (NoClassDefFoundError ncdfe) { + System.out.println("Got expected NCDFE for FIELD1"); + } + } + + static void checkTiming() { + FieldThread fieldThread = new FieldThread(); + MethodThread methodThread = new MethodThread(); + + fieldThread.start(); + methodThread.start(); + + /* start class init */ + IntHolder zero = SlowInit.FIELD0; + + /* wait for children to complete */ + try { + fieldThread.join(); + methodThread.join(); + } catch (InterruptedException ie) { + System.err.println(ie); + } + + /* print all values */ + System.out.println("Fields (main thread): " + + SlowInit.FIELD0.getValue() + SlowInit.FIELD1.getValue() + + SlowInit.FIELD2.getValue() + SlowInit.FIELD3.getValue()); + } + + static class FieldThread extends Thread { + public void run() { + /* allow class init to start */ + Main.sleep(200); + + /* collect fields; should delay until class init completes */ + int field0, field1, field2, field3; + field0 = SlowInit.FIELD0.getValue(); + field1 = SlowInit.FIELD1.getValue(); + field2 = SlowInit.FIELD2.getValue(); + field3 = SlowInit.FIELD3.getValue(); + + /* let MethodThread print first */ + Main.sleep(400); + System.out.println("Fields (child thread): " + + field0 + field1 + field2 + field3); + } + } + + static class MethodThread extends Thread { + public void run() { + /* allow class init to start */ + Main.sleep(200); + + /* use a method that shouldn't be accessible yet */ + SlowInit.printMsg("MethodThread message"); + } + } +} diff --git a/test/084-class-init/src/PartialInit.java b/test/084-class-init/src/PartialInit.java new file mode 100644 index 0000000000..d4c71ffec7 --- /dev/null +++ b/test/084-class-init/src/PartialInit.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2010 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. + */ + + +/** + * Partially-initialized class. + */ +public class PartialInit { + public static final IntHolder FIELD0 = new IntHolder(1); // succeeds + public static final IntHolder FIELD1 = new IntHolder(-2); // throws +} diff --git a/test/084-class-init/src/SlowInit.java b/test/084-class-init/src/SlowInit.java new file mode 100644 index 0000000000..8ac72be81e --- /dev/null +++ b/test/084-class-init/src/SlowInit.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2010 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. + */ + +/** + * Class that initializes with a pause. + */ +public class SlowInit { + + public static final IntHolder FIELD0 = new IntHolder(0); + public static final IntHolder FIELD1 = new IntHolder(0); + public static final IntHolder FIELD2 = new IntHolder(0); + public static final IntHolder FIELD3 = new IntHolder(0); + + public static void printMsg(String msg) { + System.out.println(msg); + } + + static { + FIELD0.setValue(111); + FIELD1.setValue(222); + printMsg("SlowInit static block pre-sleep"); + Main.sleep(600); + printMsg("SlowInit static block post-sleep"); + FIELD2.setValue(333); + FIELD3.setValue(444); + }; +} |