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