FIX: java.lang.Object should be java/lang/Object.
[jpf-core.git] / src / tests / gov / nasa / jpf / test / java / lang / BoxObjectCacheTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18 package gov.nasa.jpf.test.java.lang;
19
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22
23 import gov.nasa.jpf.util.test.TestJPF;
24
25 import org.junit.Test;
26
27 /**
28  * regression test for java.lang.Integer
29  */
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"}; 
40
41   @Test
42   public void testIntCache(){
43     if (verifyNoPropertyViolation(JPF_ARGS)){
44       Integer i1 = Integer.valueOf( 1);        // should be cached
45       assertTrue( i1.intValue() == 1);
46       
47       Integer i2 = Integer.parseInt("1");
48       assertTrue( i1 == i2);
49       
50       i1 = Integer.valueOf(110); // should be too large for cache
51       assertTrue( i1.intValue() == 110);
52       
53       i2 = Integer.parseInt("110");
54       assertTrue(i1 != i2);
55     }
56   }
57   
58   @Test
59   public void testCharacterCache(){
60     if (verifyNoPropertyViolation(JPF_ARGS)){
61       Character c1 = Character.valueOf( '?');        // should be cached
62       assertTrue( c1.charValue() == '?');
63       
64       Character c2 = '?'; // compiler does the boxing
65       assertTrue( c1 == c2);
66       
67       c1 = Character.valueOf( '\u2107' ); // epsilon, if I'm not mistaken
68       assertTrue( c1.charValue() == '\u2107');
69       
70       c2 = '\u2107'; // compiler does the boxing
71       assertTrue(c1 != c2);
72     }
73   }
74
75   @Test
76   public void testByteCache(){
77     if (verifyNoPropertyViolation(JPF_ARGS)){
78       Byte b1 = Byte.valueOf( (byte)1);        // should be cached
79       assertTrue( b1.byteValue() == 1);
80       
81       Byte b2 = Byte.parseByte("1");
82       assertTrue( b1 == b2);
83     }
84   }
85
86   @Test
87   public void testShortCache(){
88    if (verifyNoPropertyViolation(JPF_ARGS)){
89      Short s1 = Short.valueOf((short)1);        // should be cached
90      assertTrue( s1.shortValue() == 1);
91      
92      Short s2 = Short.parseShort("1");
93      assertTrue( s1 == s2);
94       
95      s1 = Short.valueOf((short)110); // should be too large for cache
96      assertTrue( s1.shortValue() == (short)110);
97       
98      s2 = Short.parseShort("110");
99      assertTrue(s1 != s2);
100     }
101   }
102
103   @Test
104   public void testLongCache(){
105    if (verifyNoPropertyViolation(JPF_ARGS)){
106      Long l1 = Long.valueOf(1);        // should be cached
107      assertTrue( l1.longValue() == 1);
108      
109      Long l2 = Long.parseLong("1");
110      assertTrue( l1 == l2);
111       
112      l1 = Long.valueOf(110); // should be too large for cache
113      assertTrue( l1.longValue() == 110);
114       
115      l2 = Long.parseLong("110");
116      assertTrue(l1 != l2);
117     }
118   }  
119
120   @Test
121   public void testBooleanCache(){
122     if (verifyNoPropertyViolation(JPF_ARGS)){
123       Boolean b1 = Boolean.valueOf(true);        // should be cached
124       assertTrue( b1.booleanValue() == true);
125       
126       Boolean b2 = Boolean.parseBoolean("true");
127       assertTrue( b1 == b2);
128     }
129   }
130
131   @Test
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);
136       
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);
141     }
142   }
143
144   @Test
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() == '?');
149       
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);
154     }
155   }
156
157   @Test
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);
162       
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);
167     }
168   }
169
170   @Test
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);
175       
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);
180     }
181   }
182
183   @Test
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);
188       
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);
193     }
194   }
195
196   @Test
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);
201       
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);
206     }
207   }
208 }