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 gov.nasa.jpf.util.test.TestJPF;
21 import gov.nasa.jpf.vm.Verify;
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;
29 import org.junit.Test;
32 * test of java.lang.String APIs
34 public class StringTest extends TestJPF {
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");
44 assert (a != b) : "'new String(intern) != intern' failed";
46 boolean c2 = Verify.getBoolean(); // to do some more storing / backtracking
48 String c = b.intern();
50 assert (a == c) : "'(new String(intern)).intern() == intern' failed";
55 public void testToCharArray() {
56 if (verifyNoPropertyViolation()) {
58 char[] c = s.toCharArray();
67 public void testEquals() {
68 if (verifyNoPropertyViolation()) {
70 String b = "one" + " two";
71 String c = "one three";
79 public void testIndexOf() {
80 if (verifyNoPropertyViolation()) {
82 int i1 = a.indexOf('.');
83 int i2 = a.indexOf('@');
91 public void testCompareTo() {
92 if (verifyNoPropertyViolation()) {
96 assert a.compareTo(b) < 0;
97 assert b.compareTo(a) > 0;
98 assert a.compareTo(a) == 0;
100 String longer = "aaaaaa";
102 assert a.compareTo(longer) < 0;
107 public void testStartsWith() {
108 if (verifyNoPropertyViolation()) {
109 String str = "TestString";
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;
120 public void testEndsWith() {
121 if (verifyNoPropertyViolation()) {
122 String str = "TestString";
124 assert str.endsWith("String") == true;
125 assert str.endsWith("") == true;
126 assert str.endsWith("StrUng") == false;
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("");
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;
153 public void testNullChar(){
154 if (verifyNoPropertyViolation()){
156 assertTrue( s.length() == 1);
157 char c = s.charAt(0);
158 assertTrue( Character.isISOControl(c));
163 public void testCharAtOutOfBounds() {
164 if (verifyNoPropertyViolation()){
167 } catch (StringIndexOutOfBoundsException e) {
175 public void testCharAtOutOfBoundsNeg() {
176 boolean passed = false;
177 if (verifyNoPropertyViolation()){
179 assert(" ".charAt(0) == ' ');
182 } catch (StringIndexOutOfBoundsException e) {
191 public void testSubstringOutOfBounds0() {
192 boolean passed = false;
193 if (verifyNoPropertyViolation()){
198 } catch (StringIndexOutOfBoundsException e) {
207 public void testSubstringOutOfBounds1() {
208 boolean passed = false;
209 if (verifyNoPropertyViolation()){
214 } catch (StringIndexOutOfBoundsException e) {
223 public void testSubstringOutOfBoundsNeg() {
224 boolean passed = false;
225 if (verifyNoPropertyViolation()){
227 assert("".substring(0).equals(""));
230 } catch (StringIndexOutOfBoundsException e) {
239 @SuppressWarnings("deprecation")
240 public void testConstructors(){
241 if (verifyNoPropertyViolation()){
242 String s=new String();
244 assertTrue("empty test",s.isEmpty());
245 char[]value=new char[]{'a','b','c','d','e'};
246 assertTrue("String([]abcde=abcde","abcde".equals(new String(value)));
247 assertTrue("String([]abcde,2,3)=cde","cde".equals(new String(value,2,3)));
248 int[]codePoints=new int[]{48,49,50,51,52,53,54,55,56,57};
249 assertTrue("codePoints0,4=0123","0123".equals(new String(codePoints,0,4)));
250 byte[]bytes=new byte[]{65,66,67,68};
251 byte[]data=new byte[]{69,70,71,72};
252 byte[]more=new byte[]{73,74,75,76};
253 byte[]yow=new byte[]{77,78,79};
254 assertTrue("bytes0,1,3=BCD","BCD".equals(new String(bytes,0,1,3)));
255 assertTrue("bytes=ABCD","ABCD".equals(new String(bytes,0)));
257 Charset d = Charset.forName("ISO-8859-1");
258 String dname=d.name();
259 assertTrue("bytes1,2,ISO=BC","BC".equals(new String(bytes,1,2,dname)));
260 assertTrue("bytes2,2,ISO=CD","CD".equals(new String(bytes,2,2,d)));
262 assertTrue("data,ISO=EFGH","EFGH".equals(new String(data,dname)));
263 assertTrue("more,ISO=IJKL","IJKL".equals(new String(more,d)));
264 } catch (UnsupportedEncodingException e) {
265 // TODO Auto-generated catch block
266 fail("default encoding failure");
268 assertTrue("more1,3=JKL","JKL".equals(new String(more,1,3)));
269 assertTrue("yow=MNO","MNO".equals(new String(yow)));
270 StringBuffer buf=new StringBuffer();
272 assertTrue("buf=yogi","yogi".equals(new String(buf)));
273 StringBuilder build=new StringBuilder();
274 build.append("boo-boo");
275 assertTrue("build=boo-boo","boo-boo".equals(new String(build)));
280 // Test new modelled methods
282 @SuppressWarnings("deprecation")
283 public void testMethods() {
284 if (verifyNoPropertyViolation()) {
286 String x = new String("abcdefg");
287 assertTrue("abcdefg[3]=100", x.codePointAt(3) == 100);
288 assertTrue("abcdefg[2]=99", x.codePointBefore(3) == 99);
289 assertTrue("abcdefg(2,4)", x.codePointCount(2, 4) == 2);
290 assertTrue("abcdefg(2+2)", x.offsetByCodePoints(2, 2) == 4);
291 char[] dst = new char[]{0, 0, 0, 0, 0};
292 x.getChars(4, 7, dst, 1);
293 assertTrue("abcdefg(4,7->1)=0efg0", dst[0] == 0);
294 assertTrue("abcdefg(4,7->1)=0efg0", dst[1] == 'e');
295 assertTrue("abcdefg(4,7->1)=0efg0", dst[2] == 'f');
296 assertTrue("abcdefg(4,7->1)=0efg0", dst[3] == 'g');
297 assertTrue("abcdefg(4,7->1)=0efg0", dst[4] == 0);
298 byte[] bdst = new byte[]{0, 0, 0, 0, 0};
299 x.getBytes(3, 6, bdst, 1);
300 assertTrue("abcdefg(4,7->1)=0efg0", bdst[0] == 0);
301 assertTrue("abcdefg(4,7->1)=0efg0", bdst[1] == 'd');
302 assertTrue("abcdefg(4,7->1)=0efg0", bdst[2] == 'e');
303 assertTrue("abcdefg(4,7->1)=0efg0", bdst[3] == 'f');
304 assertTrue("abcdefg(4,7->1)=0efg0", bdst[4] == 0);
305 String y = new String("qrs");
307 bdst = y.getBytes("ISO-8859-1");
308 } catch (UnsupportedEncodingException e) {
309 fail("getBytes failed: " + e);
311 assertTrue("qrs 0", bdst[0] == 'q');
312 assertTrue("qrs 1", bdst[1] == 'r');
313 assertTrue("qrs 2", bdst[2] == 's');
314 Charset charSet = Charset.forName("ISO-8859-1");
315 String z = new String("tuv");
316 bdst = z.getBytes(charSet);
317 assertTrue("tuv 0", bdst[0] == 't');
318 assertTrue("tuv 1", bdst[1] == 'u');
319 assertTrue("tuv 2", bdst[2] == 'v');
320 String s = new String("wxy");
322 assertTrue("wxy 0", bdst[0] == 'w');
323 assertTrue("wxy 1", bdst[1] == 'x');
324 assertTrue("wxy 2", bdst[2] == 'y');
325 StringBuffer buf = new StringBuffer();
327 assertTrue("xyz".contentEquals(buf));
328 assertTrue("a<b", "a".compareTo("b") < 0);
329 TreeSet<String> set = new TreeSet<String>();
332 Iterator<String> iter = set.iterator();
333 String a = iter.next();
334 assertTrue("set0=a", a.equals("a"));
335 String b = iter.next();
336 assertTrue("set1=b", b.equals("b"));
337 assertTrue("AaBb=aabb", "AaBb".compareToIgnoreCase("aabb") == 0);
338 assertTrue("abcdef1,3-->bcdef", "abcde".regionMatches(2, "bcdef", 1, 3));
339 assertTrue("abcdef1,3-->BCDEF", "abcde".regionMatches(true, 2, "BCDEF", 1, 3));
340 assertTrue("xyz->yz", "xyz".startsWith("yz", 1));
341 assertTrue("abc->a", "abc".startsWith("a"));
342 assertTrue("xyz->yz", "xyz".endsWith("yz"));
343 buf.delete(0, buf.length());
345 assertTrue("hashCode", "hash".hashCode() == buf.toString().hashCode());
346 assertTrue("abc->1", "abc".indexOf('b') == 1);
347 assertTrue("ababa->3", "ababa".indexOf('b', 2) == 3);
348 assertTrue("ababa(z)->-1", "ababa".lastIndexOf('z') == -1);
349 assertTrue("aacdabcd(a,3)", "aacdabcd".lastIndexOf('a', 3) == 1);
350 assertTrue("abcabca->bca so 1", "abcabca".indexOf("bca") == 1);
351 assertTrue("abcabca->bca 2, so 4", "abcabca".indexOf("bca", 2) == 4);
352 assertTrue("lovelovelove->8", "lovelovelove".lastIndexOf("love") == 8);
353 assertTrue("crazycrazy->0", "crazycrazy".lastIndexOf("crazy", 4) == 0);
354 assertTrue("yellowblue->6", "yellowblue".substring(6).equals("blue"));
355 assertTrue("yellowbluebluegreen->6,14", "yellowbluebluegreen".substring(6, 14).equals("blueblue"));
356 assertTrue("x+y", "x".concat("y").equals("xy"));
357 assertTrue("xyz(xy)", "xyz".contains("xy"));
358 assertTrue("abcabc->abaaba", "abcabc".replace("c", "a").equals("abaaba"));
359 Locale l = Locale.ENGLISH;
360 String result = String.format(l, "Gimme the %s and save my soul", "FORTRAN");
361 assertTrue("fortran", result.equals("Gimme the FORTRAN and save my soul"));
362 assertTrue("1", String.valueOf(1).equals("1"));
363 assertTrue("1111111111111111111L", String.valueOf(1111111111111111111L).equals("1111111111111111111"));
364 assertTrue("3.14159", String.valueOf(3.14159).equals("3.14159"));
365 System.out.println(String.valueOf(3.14159265358979324D));
366 assertTrue("3.141592653589793", String.valueOf(3.141592653589793D).equals("3.141592653589793"));
371 public void testContentEquals(){
372 if (verifyNoPropertyViolation()){
373 String s = "fortyTwo";
374 StringBuilder sb = new StringBuilder();
375 sb.append("fortyTwo");
377 assertTrue( s.contentEquals(sb));