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 @SuppressWarnings("deprecation")
192 public void testConstructors(){
193 if (verifyNoPropertyViolation()){
194 String s=new String();
196 assertTrue("empty test",s.isEmpty());
197 char[]value=new char[]{'a','b','c','d','e'};
198 assertTrue("String([]abcde=abcde","abcde".equals(new String(value)));
199 assertTrue("String([]abcde,2,3)=cde","cde".equals(new String(value,2,3)));
200 int[]codePoints=new int[]{48,49,50,51,52,53,54,55,56,57};
201 assertTrue("codePoints0,4=0123","0123".equals(new String(codePoints,0,4)));
202 byte[]bytes=new byte[]{65,66,67,68};
203 byte[]data=new byte[]{69,70,71,72};
204 byte[]more=new byte[]{73,74,75,76};
205 byte[]yow=new byte[]{77,78,79};
206 assertTrue("bytes0,1,3=BCD","BCD".equals(new String(bytes,0,1,3)));
207 assertTrue("bytes=ABCD","ABCD".equals(new String(bytes,0)));
209 Charset d = Charset.forName("ISO-8859-1");
210 String dname=d.name();
211 assertTrue("bytes1,2,ISO=BC","BC".equals(new String(bytes,1,2,dname)));
212 assertTrue("bytes2,2,ISO=CD","CD".equals(new String(bytes,2,2,d)));
214 assertTrue("data,ISO=EFGH","EFGH".equals(new String(data,dname)));
215 assertTrue("more,ISO=IJKL","IJKL".equals(new String(more,d)));
216 } catch (UnsupportedEncodingException e) {
217 // TODO Auto-generated catch block
218 fail("default encoding failure");
220 assertTrue("more1,3=JKL","JKL".equals(new String(more,1,3)));
221 assertTrue("yow=MNO","MNO".equals(new String(yow)));
222 StringBuffer buf=new StringBuffer();
224 assertTrue("buf=yogi","yogi".equals(new String(buf)));
225 StringBuilder build=new StringBuilder();
226 build.append("boo-boo");
227 assertTrue("build=boo-boo","boo-boo".equals(new String(build)));
232 // Test new modelled methods
234 @SuppressWarnings("deprecation")
235 public void testMethods() {
236 if (verifyNoPropertyViolation()) {
238 String x = new String("abcdefg");
239 assertTrue("abcdefg[3]=100", x.codePointAt(3) == 100);
240 assertTrue("abcdefg[2]=99", x.codePointBefore(3) == 99);
241 assertTrue("abcdefg(2,4)", x.codePointCount(2, 4) == 2);
242 assertTrue("abcdefg(2+2)", x.offsetByCodePoints(2, 2) == 4);
243 char[] dst = new char[]{0, 0, 0, 0, 0};
244 x.getChars(4, 7, dst, 1);
245 assertTrue("abcdefg(4,7->1)=0efg0", dst[0] == 0);
246 assertTrue("abcdefg(4,7->1)=0efg0", dst[1] == 'e');
247 assertTrue("abcdefg(4,7->1)=0efg0", dst[2] == 'f');
248 assertTrue("abcdefg(4,7->1)=0efg0", dst[3] == 'g');
249 assertTrue("abcdefg(4,7->1)=0efg0", dst[4] == 0);
250 byte[] bdst = new byte[]{0, 0, 0, 0, 0};
251 x.getBytes(3, 6, bdst, 1);
252 assertTrue("abcdefg(4,7->1)=0efg0", bdst[0] == 0);
253 assertTrue("abcdefg(4,7->1)=0efg0", bdst[1] == 'd');
254 assertTrue("abcdefg(4,7->1)=0efg0", bdst[2] == 'e');
255 assertTrue("abcdefg(4,7->1)=0efg0", bdst[3] == 'f');
256 assertTrue("abcdefg(4,7->1)=0efg0", bdst[4] == 0);
257 String y = new String("qrs");
259 bdst = y.getBytes("ISO-8859-1");
260 } catch (UnsupportedEncodingException e) {
261 fail("getBytes failed: " + e);
263 assertTrue("qrs 0", bdst[0] == 'q');
264 assertTrue("qrs 1", bdst[1] == 'r');
265 assertTrue("qrs 2", bdst[2] == 's');
266 Charset charSet = Charset.forName("ISO-8859-1");
267 String z = new String("tuv");
268 bdst = z.getBytes(charSet);
269 assertTrue("tuv 0", bdst[0] == 't');
270 assertTrue("tuv 1", bdst[1] == 'u');
271 assertTrue("tuv 2", bdst[2] == 'v');
272 String s = new String("wxy");
274 assertTrue("wxy 0", bdst[0] == 'w');
275 assertTrue("wxy 1", bdst[1] == 'x');
276 assertTrue("wxy 2", bdst[2] == 'y');
277 StringBuffer buf = new StringBuffer();
279 assertTrue("xyz".contentEquals(buf));
280 assertTrue("a<b", "a".compareTo("b") < 0);
281 TreeSet<String> set = new TreeSet<String>();
284 Iterator<String> iter = set.iterator();
285 String a = iter.next();
286 assertTrue("set0=a", a.equals("a"));
287 String b = iter.next();
288 assertTrue("set1=b", b.equals("b"));
289 assertTrue("AaBb=aabb", "AaBb".compareToIgnoreCase("aabb") == 0);
290 assertTrue("abcdef1,3-->bcdef", "abcde".regionMatches(2, "bcdef", 1, 3));
291 assertTrue("abcdef1,3-->BCDEF", "abcde".regionMatches(true, 2, "BCDEF", 1, 3));
292 assertTrue("xyz->yz", "xyz".startsWith("yz", 1));
293 assertTrue("abc->a", "abc".startsWith("a"));
294 assertTrue("xyz->yz", "xyz".endsWith("yz"));
295 buf.delete(0, buf.length());
297 assertTrue("hashCode", "hash".hashCode() == buf.toString().hashCode());
298 assertTrue("abc->1", "abc".indexOf('b') == 1);
299 assertTrue("ababa->3", "ababa".indexOf('b', 2) == 3);
300 assertTrue("ababa(z)->-1", "ababa".lastIndexOf('z') == -1);
301 assertTrue("aacdabcd(a,3)", "aacdabcd".lastIndexOf('a', 3) == 1);
302 assertTrue("abcabca->bca so 1", "abcabca".indexOf("bca") == 1);
303 assertTrue("abcabca->bca 2, so 4", "abcabca".indexOf("bca", 2) == 4);
304 assertTrue("lovelovelove->8", "lovelovelove".lastIndexOf("love") == 8);
305 assertTrue("crazycrazy->0", "crazycrazy".lastIndexOf("crazy", 4) == 0);
306 assertTrue("yellowblue->6", "yellowblue".substring(6).equals("blue"));
307 assertTrue("yellowbluebluegreen->6,14", "yellowbluebluegreen".substring(6, 14).equals("blueblue"));
308 assertTrue("x+y", "x".concat("y").equals("xy"));
309 assertTrue("xyz(xy)", "xyz".contains("xy"));
310 assertTrue("abcabc->abaaba", "abcabc".replace("c", "a").equals("abaaba"));
311 Locale l = Locale.ENGLISH;
312 String result = String.format(l, "Gimme the %s and save my soul", "FORTRAN");
313 assertTrue("fortran", result.equals("Gimme the FORTRAN and save my soul"));
314 assertTrue("1", String.valueOf(1).equals("1"));
315 assertTrue("1111111111111111111L", String.valueOf(1111111111111111111L).equals("1111111111111111111"));
316 assertTrue("3.14159", String.valueOf(3.14159).equals("3.14159"));
317 System.out.println(String.valueOf(3.14159265358979324D));
318 assertTrue("3.141592653589793", String.valueOf(3.141592653589793D).equals("3.141592653589793"));
323 public void testContentEquals(){
324 if (verifyNoPropertyViolation()){
325 String s = "fortyTwo";
326 StringBuilder sb = new StringBuilder();
327 sb.append("fortyTwo");
329 assertTrue( s.contentEquals(sb));