2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.test.java.lang;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
23 import gov.nasa.jpf.util.test.TestJPF;
25 import org.junit.Test;
28 * regression test for java.lang.Integer
30 public class BoxObjectCacheTest extends TestJPF {
31 private final static String[] JPF_ARGS = { "+vm.cache.low_byte=-100",
32 "+vm.cache.high_byte=100",
33 "+vm.cache.high_char=100",
34 "+vm.cache.low_short=-100",
35 "+vm.cache.high_short=100",
36 "+vm.cache.low_int=-100",
37 "+vm.cache.high_int=100",
38 "+vm.cache.low_long=-100",
39 "+vm.cache.high_long=100"};
42 public void testIntCache(){
43 if (verifyNoPropertyViolation(JPF_ARGS)){
44 Integer i1 = Integer.valueOf( 1); // should be cached
45 assertTrue( i1.intValue() == 1);
47 Integer i2 = Integer.parseInt("1");
48 assertTrue( i1 == i2);
50 i1 = Integer.valueOf(110); // should be too large for cache
51 assertTrue( i1.intValue() == 110);
53 i2 = Integer.parseInt("110");
59 public void testCharacterCache(){
60 if (verifyNoPropertyViolation(JPF_ARGS)){
61 Character c1 = Character.valueOf( '?'); // should be cached
62 assertTrue( c1.charValue() == '?');
64 Character c2 = '?'; // compiler does the boxing
65 assertTrue( c1 == c2);
67 c1 = Character.valueOf( '\u2107' ); // epsilon, if I'm not mistaken
68 assertTrue( c1.charValue() == '\u2107');
70 c2 = '\u2107'; // compiler does the boxing
76 public void testByteCache(){
77 if (verifyNoPropertyViolation(JPF_ARGS)){
78 Byte b1 = Byte.valueOf( (byte)1); // should be cached
79 assertTrue( b1.byteValue() == 1);
81 Byte b2 = Byte.parseByte("1");
82 assertTrue( b1 == b2);
87 public void testShortCache(){
88 if (verifyNoPropertyViolation(JPF_ARGS)){
89 Short s1 = Short.valueOf((short)1); // should be cached
90 assertTrue( s1.shortValue() == 1);
92 Short s2 = Short.parseShort("1");
93 assertTrue( s1 == s2);
95 s1 = Short.valueOf((short)110); // should be too large for cache
96 assertTrue( s1.shortValue() == (short)110);
98 s2 = Short.parseShort("110");
104 public void testLongCache(){
105 if (verifyNoPropertyViolation(JPF_ARGS)){
106 Long l1 = Long.valueOf(1); // should be cached
107 assertTrue( l1.longValue() == 1);
109 Long l2 = Long.parseLong("1");
110 assertTrue( l1 == l2);
112 l1 = Long.valueOf(110); // should be too large for cache
113 assertTrue( l1.longValue() == 110);
115 l2 = Long.parseLong("110");
116 assertTrue(l1 != l2);
121 public void testBooleanCache(){
122 if (verifyNoPropertyViolation(JPF_ARGS)){
123 Boolean b1 = Boolean.valueOf(true); // should be cached
124 assertTrue( b1.booleanValue() == true);
126 Boolean b2 = Boolean.parseBoolean("true");
127 assertTrue( b1 == b2);
132 public void testIntCacheBoxObject() throws SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
133 if (verifyNoPropertyViolation(JPF_ARGS)){
134 Integer i1 = Integer.valueOf( 1); // should be cached
135 assertTrue( i1.intValue() == 1);
137 Integer i2 = new Integer(1);
138 Method m = Integer.class.getMethod("intValue", new Class[0]);
139 Integer i3 = (Integer) m.invoke(i2, new Object[0]);
140 assertTrue( i1 == i3);
145 public void testCharacterCacheBoxObject() throws SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
146 if (verifyNoPropertyViolation(JPF_ARGS)){
147 Character c1 = Character.valueOf( '?'); // should be cached
148 assertTrue( c1.charValue() == '?');
150 Character c2 = new Character('?');
151 Method m = Character.class.getMethod("charValue", new Class[0]);
152 Character c3 = (Character) m.invoke(c2, new Object[0]);
153 assertTrue( c1 == c3);
158 public void testByteCacheBoxObject() throws SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
159 if (verifyNoPropertyViolation(JPF_ARGS)){
160 Byte b1 = Byte.valueOf( (byte)1); // should be cached
161 assertTrue( b1.byteValue() == 1);
163 Byte b2 = new Byte((byte)1);
164 Method m = Byte.class.getMethod("byteValue", new Class[0]);
165 Byte b3 = (Byte) m.invoke(b2, new Object[0]);
166 assertTrue( b1 == b3);
171 public void testShortCacheBoxObject() throws SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
172 if (verifyNoPropertyViolation(JPF_ARGS)){
173 Short s1 = Short.valueOf((short)1); // should be cached
174 assertTrue( s1.shortValue() == 1);
176 Short s2 = new Short((short)1);
177 Method m = Short.class.getMethod("shortValue", new Class[0]);
178 Short s3 = (Short) m.invoke(s2, new Object[0]);
179 assertTrue( s1 == s3);
184 public void testLongCacheBoxObject() throws SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
185 if (verifyNoPropertyViolation(JPF_ARGS)){
186 Long l1 = Long.valueOf(1); // should be cached
187 assertTrue( l1.longValue() == 1);
189 Long l2 = new Long(1);
190 Method m = Long.class.getMethod("longValue", new Class[0]);
191 Long l3 = (Long) m.invoke(l2, new Object[0]);
192 assertTrue( l1 == l3);
197 public void testBooleanCacheBoxObject() throws SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
198 if (verifyNoPropertyViolation(JPF_ARGS)){
199 Boolean b1 = Boolean.valueOf(true); // should be cached
200 assertTrue( b1.booleanValue() == true);
202 Boolean b2 = new Boolean(true);
203 Method m = Boolean.class.getMethod("booleanValue", new Class[0]);
204 Boolean b3 = (Boolean) m.invoke(b2, new Object[0]);
205 assertTrue( b1 == b3);