diff options
Diffstat (limited to 'test/414-static-fields/src')
| -rw-r--r-- | test/414-static-fields/src/Main.java | 92 | ||||
| -rw-r--r-- | test/414-static-fields/src/Other.java | 19 | ||||
| -rw-r--r-- | test/414-static-fields/src/OtherWithClinit.java | 23 | ||||
| -rw-r--r-- | test/414-static-fields/src/TestCase.java | 199 |
4 files changed, 333 insertions, 0 deletions
diff --git a/test/414-static-fields/src/Main.java b/test/414-static-fields/src/Main.java new file mode 100644 index 0000000000..9c5cf133a0 --- /dev/null +++ b/test/414-static-fields/src/Main.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2014 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 extends TestCase { + public static void main(String[] args) { + $opt$TestThisClassStaticField(); + $opt$TestOtherClassStaticField(); + $opt$TestAddThisClassStaticField(); + $opt$TestAddOtherClassStaticField(); + $opt$TestOtherClassWithClinitStaticField(); + $opt$TestAccess(); + } + + static int staticField = 42; + + static int getInt() { + return 33; + } + + static void $opt$TestThisClassStaticField() { + assertEquals(42, staticField); + } + + static void $opt$TestOtherClassStaticField() { + assertEquals(41, Other.staticField); + } + + static void $opt$TestAddThisClassStaticField() { + int a = getInt(); + assertEquals(a + 42, a + staticField); + } + + static void $opt$TestAddOtherClassStaticField() { + int a = getInt(); + assertEquals(a + 41, a + Other.staticField); + } + + static void $opt$TestOtherClassWithClinitStaticField() { + assertEquals(40, OtherWithClinit.staticField); + } + + static void $opt$TestAccess() { + assertEquals(false, sZ); + assertEquals(0, sB); + assertEquals(0, sC); + assertEquals(0, sI); + assertEquals(0, sJ); + assertEquals(0, sS); + assertNull(sObject); + + long longValue = -1122198787987987987L; + Object o = new Object(); + sZ = true; + sB = -2; + sC = 'c'; + sI = 42; + sJ = longValue; + sS = 68; + sObject = o; + + assertEquals(true, sZ); + assertEquals(-2, sB); + assertEquals('c', sC); + assertEquals(42, sI); + assertEquals(longValue, sJ); + assertEquals(68, sS); + assertEquals(o, sObject); + } + + static boolean sZ; + static byte sB; + static char sC; + static double sD; + static float sF; + static int sI; + static long sJ; + static short sS; + static Object sObject; +} diff --git a/test/414-static-fields/src/Other.java b/test/414-static-fields/src/Other.java new file mode 100644 index 0000000000..aede930311 --- /dev/null +++ b/test/414-static-fields/src/Other.java @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2014 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 Other { + static int staticField = 41; +} diff --git a/test/414-static-fields/src/OtherWithClinit.java b/test/414-static-fields/src/OtherWithClinit.java new file mode 100644 index 0000000000..eafbd2b97e --- /dev/null +++ b/test/414-static-fields/src/OtherWithClinit.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2014 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 OtherWithClinit { + static int staticField = 39; + + static { + staticField = 40; + } +} diff --git a/test/414-static-fields/src/TestCase.java b/test/414-static-fields/src/TestCase.java new file mode 100644 index 0000000000..ef77f71f3f --- /dev/null +++ b/test/414-static-fields/src/TestCase.java @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2014 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. + */ + +/** + * Common superclass for test cases. + */ + +import java.util.Arrays; + +public abstract class TestCase { + public static void assertSame(Object expected, Object value) { + if (expected != value) { + throw new AssertionError("Objects are not the same: expected " + + String.valueOf(expected) + ", got " + String.valueOf(value)); + } + } + + public static void assertNotSame(Object expected, Object value) { + if (expected == value) { + throw new AssertionError( + "Objects are the same: " + String.valueOf(expected)); + } + } + + public static void assertEquals(String message, int expected, int actual) { + if (expected != actual) { + throw new AssertionError(message); + } + } + + public static void assertEquals(int expected, int actual) { + if (expected != actual) { + throw new AssertionError("Expected " + expected + " got " + actual); + } + } + + public static void assertTrue(String message, boolean condition) { + if (!condition) { + throw new AssertionError(message); + } + } + + public static void assertTrue(boolean condition) { + assertTrue("Expected true", condition); + } + + public static void assertFalse(String message, boolean condition) { + if (condition) { + throw new AssertionError(message); + } + } + + public static void assertFalse(boolean condition) { + assertFalse("Expected false", condition); + } + + public static void assertEquals(Object expected, Object actual) { + if (!expected.equals(actual)) { + String msg = "Expected \"" + expected + "\" but got \"" + actual + "\""; + throw new AssertionError(msg); + } + } + + public static void assertNotEquals(int expected, int actual) { + if (expected == actual) { + throw new AssertionError("Expected " + expected + " got " + actual); + } + } + + public static void assertNotEquals(Object expected, Object actual) { + if (expected.equals(actual)) { + String msg = "Objects are the same: " + String.valueOf(expected); + throw new AssertionError(msg); + } + } + + public static <T> void assertArrayEquals(T[] actual, T... expected) { + assertTrue(Arrays.equals(expected, actual)); + } + + public static void assertEquals( + String message, Object expected, Object actual) { + if (!expected.equals(actual)) { + throw new AssertionError(message); + } + } + + public static void assertEquals( + String message, long expected, long actual) { + if (expected != actual) { + throw new AssertionError(message); + } + } + + public static void assertEquals(long expected, long actual) { + if (expected != actual) { + throw new AssertionError("Expected " + expected + " got " + actual); + } + } + + public static void assertEquals( + String message, boolean expected, boolean actual) { + if (expected != actual) { + throw new AssertionError(message); + } + } + + public static void assertEquals(boolean expected, boolean actual) { + if (expected != actual) { + throw new AssertionError("Expected " + expected + " got " + actual); + } + } + + public static void assertEquals( + String message, float expected, float actual) { + if (expected != actual) { + throw new AssertionError(message); + } + } + + public static void assertEquals(float expected, float actual) { + if (expected != actual) { + throw new AssertionError("Expected " + expected + " got " + actual); + } + } + + public static void assertEquals(float expected, float actual, + float tolerance) { + if ((actual < expected - tolerance) || (expected + tolerance < actual)) { + throw new AssertionError("Expected " + expected + " got " + actual + + " tolerance " + tolerance); + } + } + + public static void assertEquals( + String message, double expected, double actual) { + if (expected != actual) { + throw new AssertionError(message); + } + } + + public static void assertEquals(double expected, double actual) { + if (expected != actual) { + throw new AssertionError("Expected " + expected + " got " + actual); + } + } + + public static void assertEquals(double expected, double actual, + double tolerance) { + if ((actual < expected - tolerance) || (expected + tolerance < actual)) { + throw new AssertionError("Expected " + expected + " got " + actual + + " tolerance " + tolerance); + } + } + + public static void assertSame( + String message, Object expected, Object actual) { + if (expected != actual) { + throw new AssertionError(message); + } + } + + public static void assertNull(String message, Object object) { + if (object != null) { + throw new AssertionError(message); + } + } + + public static void assertNull(Object object) { + assertNull("Expected null", object); + } + + public static void assertNotNull(String message, Object object) { + if (object == null) { + throw new AssertionError(message); + } + } + + public static void assertNotNull(Object object) { + assertNotNull("Expected non-null", object); + } + + public static void fail(String msg) { + throw new AssertionError(msg); + } +} |