efbe6d7f545713ac9512939f19c7daaf2eb835e6
[jpf-core.git] / src / tests / gov / nasa / jpf / test / java / lang / StringTest.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.UnsupportedEncodingException;
24 import java.nio.charset.Charset;
25 import java.util.Iterator;
26 import java.util.Locale;
27 import java.util.TreeSet;
28
29 import org.junit.Test;
30
31 /**
32  * test of java.lang.String APIs
33  */
34 public class StringTest extends TestJPF {
35
36
37         @Test
38         public void testIntern() {
39                 if (verifyNoPropertyViolation()) {
40                         boolean c1 = Verify.getBoolean(); // to do some state storing / backtracking
41                         String a = "Blah".intern();
42                         String b = new String("Blah");
43
44                         assert (a != b) : "'new String(intern) != intern' failed";
45
46                         boolean c2 = Verify.getBoolean(); // to do some more storing / backtracking
47
48                         String c = b.intern();
49
50                         assert (a == c) : "'(new String(intern)).intern() == intern' failed";
51                 }
52         }
53
54         @Test
55         public void testToCharArray() {
56                 if (verifyNoPropertyViolation()) {
57                         String s = "42";
58                         char[] c = s.toCharArray();
59
60                         assert c.length == 2;
61                         assert c[0] == '4';
62                         assert c[1] == '2';
63                 }
64         }
65
66         @Test
67         public void testEquals() {
68                 if (verifyNoPropertyViolation()) {
69                         String a = "one two";
70                         String b = "one" + " two";
71                         String c = "one three";
72
73                         assert a.equals(b);
74                         assert !a.equals(c);
75                 }
76         }
77
78         @Test
79         public void testIndexOf() {
80                 if (verifyNoPropertyViolation()) {
81                         String a = "bla.bla";
82                         int i1 = a.indexOf('.');
83                         int i2 = a.indexOf('@');
84
85                         assert i1 == 3;
86                         assert i2 == -1;
87                 }
88         }
89
90         @Test
91         public void testCompareTo() {
92                 if (verifyNoPropertyViolation()) {
93                         String a = "aaa";
94                         String b = "bbb";
95
96                         assert a.compareTo(b) < 0;
97                         assert b.compareTo(a) > 0;
98                         assert a.compareTo(a) == 0;
99
100                         String longer = "aaaaaa";
101
102                         assert a.compareTo(longer) < 0;
103                 }
104         }
105
106         @Test
107         public void testStartsWith() {
108                 if (verifyNoPropertyViolation()) {
109                         String str = "TestString";
110
111                         assert str.startsWith("Test") == true;
112                         assert str.startsWith("String", 4) == true;
113                         assert str.startsWith("StringString", 4) == false;
114                         assert str.startsWith("StrUng", 4) == false;
115                         assert str.startsWith("Test", -5) == false;
116                 }
117         }
118
119         @Test
120         public void testEndsWith() {
121                 if (verifyNoPropertyViolation()) {
122                         String str = "TestString";
123
124                         assert str.endsWith("String") == true;
125                         assert str.endsWith("") == true;
126                         assert str.endsWith("StrUng") == false;
127                 }
128         }
129
130         @Test
131         public void testTrim() {
132                 if (verifyNoPropertyViolation()) {
133                         assert "   Test    ".trim().equals("Test");
134                         assert "   Test".trim().equals("Test");
135                         assert "Test    ".trim().equals("Test");
136                         assert "Test".trim().equals("Test");
137                         assert "       ".trim().equals("");
138
139                 }
140         }
141
142         @Test
143         public void testReplace() {
144                 if (verifyNoPropertyViolation()) {
145                         assert "hello".replace('l', 'a').equals("heaao") == true;
146                         assert "".replace('l', 'a').equals("") == true;
147                         assert "hello".replace('f', 'a').equals("hello") == true;
148                         assert "eve".replace('e', 'a').equals("ava") == true;
149                 }
150         }
151
152         @Test
153         public void testNullChar(){
154                 if (verifyNoPropertyViolation()){
155                         String s = "\u0000";
156                         assertTrue( s.length() == 1);
157                         char c = s.charAt(0);
158                         assertTrue( Character.isISOControl(c));
159                 }
160         }
161
162         @Test
163   @SuppressWarnings("deprecation")
164   public void testConstructors(){
165                 if (verifyNoPropertyViolation()){
166                         String s=new String();
167                         new String(s);
168                         assertTrue("empty test",s.isEmpty());
169                         char[]value=new char[]{'a','b','c','d','e'};
170                         assertTrue("String([]abcde=abcde","abcde".equals(new String(value)));
171                         assertTrue("String([]abcde,2,3)=cde","cde".equals(new String(value,2,3)));
172                         int[]codePoints=new int[]{48,49,50,51,52,53,54,55,56,57};
173                         assertTrue("codePoints0,4=0123","0123".equals(new String(codePoints,0,4)));
174                         byte[]bytes=new byte[]{65,66,67,68};
175                         byte[]data=new byte[]{69,70,71,72};
176                         byte[]more=new byte[]{73,74,75,76};
177                         byte[]yow=new byte[]{77,78,79};
178                         assertTrue("bytes0,1,3=BCD","BCD".equals(new String(bytes,0,1,3)));
179                         assertTrue("bytes=ABCD","ABCD".equals(new String(bytes,0)));
180                         try {
181                                 Charset d = Charset.forName("ISO-8859-1");
182                                 String dname=d.name();
183                                 assertTrue("bytes1,2,ISO=BC","BC".equals(new String(bytes,1,2,dname)));
184                                 assertTrue("bytes2,2,ISO=CD","CD".equals(new String(bytes,2,2,d)));
185
186                                 assertTrue("data,ISO=EFGH","EFGH".equals(new String(data,dname)));
187                                 assertTrue("more,ISO=IJKL","IJKL".equals(new String(more,d)));
188                         } catch (UnsupportedEncodingException e) {
189                                 // TODO Auto-generated catch block
190                                 fail("default encoding failure");
191                         }
192                         assertTrue("more1,3=JKL","JKL".equals(new String(more,1,3)));
193                         assertTrue("yow=MNO","MNO".equals(new String(yow)));
194                         StringBuffer buf=new StringBuffer();
195                         buf.append("yogi");
196                         assertTrue("buf=yogi","yogi".equals(new String(buf)));
197                         StringBuilder build=new StringBuilder();
198                         build.append("boo-boo");
199                         assertTrue("build=boo-boo","boo-boo".equals(new String(build)));
200                 }
201         }
202   
203   
204         // Test new modelled methods
205         @Test
206   @SuppressWarnings("deprecation")
207   public void testMethods() {
208     if (verifyNoPropertyViolation()) {
209       // 97 ...
210       String x = new String("abcdefg");
211       assertTrue("abcdefg[3]=100", x.codePointAt(3) == 100);
212       assertTrue("abcdefg[2]=99", x.codePointBefore(3) == 99);
213       assertTrue("abcdefg(2,4)", x.codePointCount(2, 4) == 2);
214       assertTrue("abcdefg(2+2)", x.offsetByCodePoints(2, 2) == 4);
215       char[] dst = new char[]{0, 0, 0, 0, 0};
216       x.getChars(4, 7, dst, 1);
217       assertTrue("abcdefg(4,7->1)=0efg0", dst[0] == 0);
218       assertTrue("abcdefg(4,7->1)=0efg0", dst[1] == 'e');
219       assertTrue("abcdefg(4,7->1)=0efg0", dst[2] == 'f');
220       assertTrue("abcdefg(4,7->1)=0efg0", dst[3] == 'g');
221       assertTrue("abcdefg(4,7->1)=0efg0", dst[4] == 0);
222       byte[] bdst = new byte[]{0, 0, 0, 0, 0};
223       x.getBytes(3, 6, bdst, 1);
224       assertTrue("abcdefg(4,7->1)=0efg0", bdst[0] == 0);
225       assertTrue("abcdefg(4,7->1)=0efg0", bdst[1] == 'd');
226       assertTrue("abcdefg(4,7->1)=0efg0", bdst[2] == 'e');
227       assertTrue("abcdefg(4,7->1)=0efg0", bdst[3] == 'f');
228       assertTrue("abcdefg(4,7->1)=0efg0", bdst[4] == 0);
229       String y = new String("qrs");
230       try {
231         bdst = y.getBytes("ISO-8859-1");
232       } catch (UnsupportedEncodingException e) {
233         fail("getBytes failed: " + e);
234       }
235       assertTrue("qrs 0", bdst[0] == 'q');
236       assertTrue("qrs 1", bdst[1] == 'r');
237       assertTrue("qrs 2", bdst[2] == 's');
238       Charset charSet = Charset.forName("ISO-8859-1");
239       String z = new String("tuv");
240       bdst = z.getBytes(charSet);
241       assertTrue("tuv 0", bdst[0] == 't');
242       assertTrue("tuv 1", bdst[1] == 'u');
243       assertTrue("tuv 2", bdst[2] == 'v');
244       String s = new String("wxy");
245       bdst = s.getBytes();
246       assertTrue("wxy 0", bdst[0] == 'w');
247       assertTrue("wxy 1", bdst[1] == 'x');
248       assertTrue("wxy 2", bdst[2] == 'y');
249       StringBuffer buf = new StringBuffer();
250       buf.append("xyz");
251       assertTrue("xyz".contentEquals(buf));
252       assertTrue("a<b", "a".compareTo("b") < 0);
253       TreeSet<String> set = new TreeSet<String>();
254       set.add("b");
255       set.add("a");
256       Iterator<String> iter = set.iterator();
257       String a = iter.next();
258       assertTrue("set0=a", a.equals("a"));
259       String b = iter.next();
260       assertTrue("set1=b", b.equals("b"));
261       assertTrue("AaBb=aabb", "AaBb".compareToIgnoreCase("aabb") == 0);
262       assertTrue("abcdef1,3-->bcdef", "abcde".regionMatches(2, "bcdef", 1, 3));
263       assertTrue("abcdef1,3-->BCDEF", "abcde".regionMatches(true, 2, "BCDEF", 1, 3));
264       assertTrue("xyz->yz", "xyz".startsWith("yz", 1));
265       assertTrue("abc->a", "abc".startsWith("a"));
266       assertTrue("xyz->yz", "xyz".endsWith("yz"));
267       buf.delete(0, buf.length());
268       buf.append("hash");
269       assertTrue("hashCode", "hash".hashCode() == buf.toString().hashCode());
270       assertTrue("abc->1", "abc".indexOf('b') == 1);
271       assertTrue("ababa->3", "ababa".indexOf('b', 2) == 3);
272       assertTrue("ababa(z)->-1", "ababa".lastIndexOf('z') == -1);
273       assertTrue("aacdabcd(a,3)", "aacdabcd".lastIndexOf('a', 3) == 1);
274       assertTrue("abcabca->bca so 1", "abcabca".indexOf("bca") == 1);
275       assertTrue("abcabca->bca 2, so 4", "abcabca".indexOf("bca", 2) == 4);
276       assertTrue("lovelovelove->8", "lovelovelove".lastIndexOf("love") == 8);
277       assertTrue("crazycrazy->0", "crazycrazy".lastIndexOf("crazy", 4) == 0);
278       assertTrue("yellowblue->6", "yellowblue".substring(6).equals("blue"));
279       assertTrue("yellowbluebluegreen->6,14", "yellowbluebluegreen".substring(6, 14).equals("blueblue"));
280       assertTrue("x+y", "x".concat("y").equals("xy"));
281       assertTrue("xyz(xy)", "xyz".contains("xy"));
282       assertTrue("abcabc->abaaba", "abcabc".replace("c", "a").equals("abaaba"));
283       Locale l = Locale.ENGLISH;
284       String result = String.format(l, "Gimme the %s and save my soul", "FORTRAN");
285       assertTrue("fortran", result.equals("Gimme the FORTRAN and save my soul"));
286       assertTrue("1", String.valueOf(1).equals("1"));
287       assertTrue("1111111111111111111L", String.valueOf(1111111111111111111L).equals("1111111111111111111"));
288       assertTrue("3.14159", String.valueOf(3.14159).equals("3.14159"));
289       System.out.println(String.valueOf(3.14159265358979324D));
290       assertTrue("3.141592653589793", String.valueOf(3.141592653589793D).equals("3.141592653589793"));
291     }
292   }
293   
294   @Test
295   public void testContentEquals(){
296     if (verifyNoPropertyViolation()){
297       String s = "fortyTwo";
298       StringBuilder sb = new StringBuilder();
299       sb.append("fortyTwo");
300             
301       assertTrue( s.contentEquals(sb));
302     }
303   }
304 }