836a16944cbae643fe19b36cdf7d106d5f8a4bf2
[jpf-core.git] / src / tests / gov / nasa / jpf / test / java / lang / SystemTest.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 gov.nasa.jpf.util.test.TestJPF;
21 import gov.nasa.jpf.vm.Verify;
22
23 import org.junit.Test;
24
25 /**
26  * raw test for java.lang.System functionality
27  */
28 public class SystemTest extends TestJPF {
29
30   final Object lock = new Object(); // need a shared object
31   boolean exitCalled;
32
33   @Test
34   public void testExit() {
35     if (verifyNoPropertyViolation()) {
36       Thread t = new Thread(new Runnable() {
37
38         @Override
39                 public void run() {
40           while (true) {
41             assert !exitCalled : "thread not stopped by System.exit()";
42
43             synchronized (lock) {
44               try {
45                 lock.wait();
46               } catch (InterruptedException x) {
47                 System.out.println("wait interrupted");
48               }
49             }
50           }
51         }
52       });
53
54       t.start();
55
56       synchronized (lock) {
57         exitCalled = true;
58         System.out.println("calling System.exit(0)");
59         System.exit(0);
60       }
61
62       assert false : "main not stopped by System.exit()";
63     }
64   }
65
66   /**
67    * just needs a gazillion more cases (different sizes, slices, overruns,
68    * incompatible types etc.)
69    */
70   @Test
71   public void testSimpleArrayCopy() {
72     if (verifyNoPropertyViolation()) {
73       int[] a = {0, 1, 2, 3, 4, 5, 6, 7};
74       int[] b = new int[a.length];
75
76       System.arraycopy(a, 0, b, 0, a.length);
77
78       for (int i = 0; i < a.length; i++) {
79         assert b[i] == i;
80       }
81     }
82   }
83
84   @Test
85   public void testSelfArrayCopy(){
86     if (verifyNoPropertyViolation()){
87       int[] a = {0, 1, 2, 3, 4, 5, 6, 7};
88
89       System.arraycopy(a, 3, a, 0, 5);
90
91       // the overwritten ones
92       assertTrue(a[0] == 3);
93       assertTrue(a[1] == 4);
94       assertTrue(a[2] == 5);
95       assertTrue(a[3] == 6);
96       assertTrue(a[4] == 7);
97
98       // the old ones
99       assertTrue(a[5] == 5);
100       assertTrue(a[6] == 6);
101       assertTrue(a[7] == 7);
102     }
103   }
104
105   @Test
106   public void testOverlappingSelfArrayCopy(){
107     if (verifyNoPropertyViolation()){
108       int[] a = {0, 1, 2, 3, 4, 5, 6, 7};
109
110       System.arraycopy(a, 0, a, 2, 3);
111
112       // copying should proceed as if using a temporary destination
113       assertTrue(a[0] == 0);
114       assertTrue(a[1] == 1);
115       assertTrue(a[2] == 0);
116       assertTrue(a[3] == 1);
117       assertTrue(a[4] == 2);
118       assertTrue(a[5] == 5);
119       assertTrue(a[6] == 6);
120       assertTrue(a[7] == 7);
121     }
122   }
123
124   @Test
125   public void testIncompatibleReferencesArrayCopy(){
126     if (verifyUnhandledException("java.lang.ArrayStoreException")){
127       String[] dst = new String[2];
128       Object[] src = { "one", new Integer(2) };
129
130       System.arraycopy(src,0,dst,0,src.length);
131     }
132   }
133
134   @Test
135   public void testRestoredArrayCopy(){
136     if (verifyNoPropertyViolation()){
137       Object[] src = { "one", "two" };
138       Object[] dst = new Object[2];
139
140       int n = Verify.getInt(0, 1);
141       System.out.println("processing choice: " + n);
142
143       if (n == 0){
144         System.out.println("copying array");
145         System.arraycopy(src,0,dst,0,src.length);
146       } else if (n == 1){
147         System.out.println("checking if non-copied dst[0] is still null");
148         assertTrue( dst[0] == null);
149       }
150     }
151   }
152 }