1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17
18package org.apache.harmony.tests.java.lang;
19
20import java.io.Serializable;
21import java.util.Arrays;
22
23import junit.framework.TestCase;
24
25// import org.apache.harmony.testframework.serialization.SerializationTest;
26// import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
27
28public class StringBuilderTest extends TestCase {
29
30    /**
31     * java.lang.StringBuilder.StringBuilder()
32     */
33    public void test_Constructor() {
34        StringBuilder sb = new StringBuilder();
35        assertNotNull(sb);
36        assertEquals(16, sb.capacity());
37    }
38
39    /**
40     * java.lang.StringBuilder.StringBuilder(int)
41     */
42    public void test_ConstructorI() {
43        StringBuilder sb = new StringBuilder(24);
44        assertNotNull(sb);
45        assertEquals(24, sb.capacity());
46
47        try {
48            new StringBuilder(-1);
49            fail("no exception");
50        } catch (NegativeArraySizeException e) {
51            // Expected
52        }
53
54        assertNotNull(new StringBuilder(0));
55    }
56
57    /**
58     * java.lang.StringBuilder.StringBuilder(CharSequence)
59     */
60    @SuppressWarnings("cast")
61    public void test_ConstructorLjava_lang_CharSequence() {
62        StringBuilder sb = new StringBuilder((CharSequence) "fixture");
63        assertEquals("fixture", sb.toString());
64        assertEquals("fixture".length() + 16, sb.capacity());
65
66        sb = new StringBuilder((CharSequence) new StringBuffer("fixture"));
67        assertEquals("fixture", sb.toString());
68        assertEquals("fixture".length() + 16, sb.capacity());
69
70        try {
71            new StringBuilder((CharSequence) null);
72            fail("no NPE");
73        } catch (NullPointerException e) {
74            // Expected
75        }
76    }
77
78    /**
79     * java.lang.StringBuilder.StringBuilder(String)
80     */
81    public void test_ConstructorLjava_lang_String() {
82        StringBuilder sb = new StringBuilder("fixture");
83        assertEquals("fixture", sb.toString());
84        assertEquals("fixture".length() + 16, sb.capacity());
85
86        try {
87            new StringBuilder((String) null);
88            fail("no NPE");
89        } catch (NullPointerException e) {
90        }
91    }
92
93    /**
94     * java.lang.StringBuilder.append(boolean)
95     */
96    public void test_appendZ() {
97        StringBuilder sb = new StringBuilder();
98        assertSame(sb, sb.append(true));
99        assertEquals("true", sb.toString());
100        sb.setLength(0);
101        assertSame(sb, sb.append(false));
102        assertEquals("false", sb.toString());
103    }
104
105    /**
106     * java.lang.StringBuilder.append(char)
107     */
108    public void test_appendC() {
109        StringBuilder sb = new StringBuilder();
110        assertSame(sb, sb.append('a'));
111        assertEquals("a", sb.toString());
112        sb.setLength(0);
113        assertSame(sb, sb.append('b'));
114        assertEquals("b", sb.toString());
115    }
116
117    /**
118     * java.lang.StringBuilder.append(char[])
119     */
120    public void test_append$C() {
121        StringBuilder sb = new StringBuilder();
122        assertSame(sb, sb.append(new char[] { 'a', 'b' }));
123        assertEquals("ab", sb.toString());
124        sb.setLength(0);
125        assertSame(sb, sb.append(new char[] { 'c', 'd' }));
126        assertEquals("cd", sb.toString());
127        try {
128            sb.append((char[]) null);
129            fail("no NPE");
130        } catch (NullPointerException e) {
131            // Expected
132        }
133    }
134
135    /**
136     * java.lang.StringBuilder.append(char[], int, int)
137     */
138    public void test_append$CII() {
139        StringBuilder sb = new StringBuilder();
140        assertSame(sb, sb.append(new char[] { 'a', 'b' }, 0, 2));
141        assertEquals("ab", sb.toString());
142        sb.setLength(0);
143        assertSame(sb, sb.append(new char[] { 'c', 'd' }, 0, 2));
144        assertEquals("cd", sb.toString());
145
146        sb.setLength(0);
147        assertSame(sb, sb.append(new char[] { 'a', 'b', 'c', 'd' }, 0, 2));
148        assertEquals("ab", sb.toString());
149
150        sb.setLength(0);
151        assertSame(sb, sb.append(new char[] { 'a', 'b', 'c', 'd' }, 2, 2));
152        assertEquals("cd", sb.toString());
153
154        sb.setLength(0);
155        assertSame(sb, sb.append(new char[] { 'a', 'b', 'c', 'd' }, 2, 0));
156        assertEquals("", sb.toString());
157
158        try {
159            sb.append((char[]) null, 0, 2);
160            fail("no NPE");
161        } catch (NullPointerException e) {
162            // Expected
163        }
164
165        try {
166            sb.append(new char[] { 'a', 'b', 'c', 'd' }, -1, 2);
167            fail("no IOOBE, negative offset");
168        } catch (IndexOutOfBoundsException e) {
169            // Expected
170        }
171
172        try {
173            sb.append(new char[] { 'a', 'b', 'c', 'd' }, 0, -1);
174            fail("no IOOBE, negative length");
175        } catch (IndexOutOfBoundsException e) {
176            // Expected
177        }
178
179        try {
180            sb.append(new char[] { 'a', 'b', 'c', 'd' }, 2, 3);
181            fail("no IOOBE, offset and length overflow");
182        } catch (IndexOutOfBoundsException e) {
183            // Expected
184        }
185    }
186
187    /**
188     * java.lang.StringBuilder.append(CharSequence)
189     */
190    public void test_appendLjava_lang_CharSequence() {
191        StringBuilder sb = new StringBuilder();
192        assertSame(sb, sb.append((CharSequence) "ab"));
193        assertEquals("ab", sb.toString());
194        sb.setLength(0);
195        assertSame(sb, sb.append((CharSequence) "cd"));
196        assertEquals("cd", sb.toString());
197        sb.setLength(0);
198        assertSame(sb, sb.append((CharSequence) null));
199        assertEquals("null", sb.toString());
200    }
201
202    /**
203     * java.lang.StringBuilder.append(CharSequence, int, int)
204     */
205    @SuppressWarnings("cast")
206    public void test_appendLjava_lang_CharSequenceII() {
207        StringBuilder sb = new StringBuilder();
208        assertSame(sb, sb.append((CharSequence) "ab", 0, 2));
209        assertEquals("ab", sb.toString());
210        sb.setLength(0);
211        assertSame(sb, sb.append((CharSequence) "cd", 0, 2));
212        assertEquals("cd", sb.toString());
213        sb.setLength(0);
214        assertSame(sb, sb.append((CharSequence) "abcd", 0, 2));
215        assertEquals("ab", sb.toString());
216        sb.setLength(0);
217        assertSame(sb, sb.append((CharSequence) "abcd", 2, 4));
218        assertEquals("cd", sb.toString());
219        sb.setLength(0);
220        assertSame(sb, sb.append((CharSequence) null, 0, 2));
221        assertEquals("nu", sb.toString());
222    }
223
224    /**
225     * java.lang.StringBuilder.append(double)
226     */
227    public void test_appendD() {
228        StringBuilder sb = new StringBuilder();
229        assertSame(sb, sb.append(1D));
230        assertEquals(String.valueOf(1D), sb.toString());
231        sb.setLength(0);
232        assertSame(sb, sb.append(0D));
233        assertEquals(String.valueOf(0D), sb.toString());
234        sb.setLength(0);
235        assertSame(sb, sb.append(-1D));
236        assertEquals(String.valueOf(-1D), sb.toString());
237        sb.setLength(0);
238        assertSame(sb, sb.append(Double.NaN));
239        assertEquals(String.valueOf(Double.NaN), sb.toString());
240        sb.setLength(0);
241        assertSame(sb, sb.append(Double.NEGATIVE_INFINITY));
242        assertEquals(String.valueOf(Double.NEGATIVE_INFINITY), sb.toString());
243        sb.setLength(0);
244        assertSame(sb, sb.append(Double.POSITIVE_INFINITY));
245        assertEquals(String.valueOf(Double.POSITIVE_INFINITY), sb.toString());
246        sb.setLength(0);
247        assertSame(sb, sb.append(Double.MIN_VALUE));
248        assertEquals(String.valueOf(Double.MIN_VALUE), sb.toString());
249        sb.setLength(0);
250        assertSame(sb, sb.append(Double.MAX_VALUE));
251        assertEquals(String.valueOf(Double.MAX_VALUE), sb.toString());
252    }
253
254    /**
255     * java.lang.StringBuilder.append(float)
256     */
257    public void test_appendF() {
258        StringBuilder sb = new StringBuilder();
259        assertSame(sb, sb.append(1F));
260        assertEquals(String.valueOf(1F), sb.toString());
261        sb.setLength(0);
262        assertSame(sb, sb.append(0F));
263        assertEquals(String.valueOf(0F), sb.toString());
264        sb.setLength(0);
265        assertSame(sb, sb.append(-1F));
266        assertEquals(String.valueOf(-1F), sb.toString());
267        sb.setLength(0);
268        assertSame(sb, sb.append(Float.NaN));
269        assertEquals(String.valueOf(Float.NaN), sb.toString());
270        sb.setLength(0);
271        assertSame(sb, sb.append(Float.NEGATIVE_INFINITY));
272        assertEquals(String.valueOf(Float.NEGATIVE_INFINITY), sb.toString());
273        sb.setLength(0);
274        assertSame(sb, sb.append(Float.POSITIVE_INFINITY));
275        assertEquals(String.valueOf(Float.POSITIVE_INFINITY), sb.toString());
276        sb.setLength(0);
277        assertSame(sb, sb.append(Float.MIN_VALUE));
278        assertEquals(String.valueOf(Float.MIN_VALUE), sb.toString());
279        sb.setLength(0);
280        assertSame(sb, sb.append(Float.MAX_VALUE));
281        assertEquals(String.valueOf(Float.MAX_VALUE), sb.toString());
282    }
283
284    /**
285     * java.lang.StringBuilder.append(int)
286     */
287    public void test_appendI() {
288        StringBuilder sb = new StringBuilder();
289        assertSame(sb, sb.append(1));
290        assertEquals(String.valueOf(1), sb.toString());
291        sb.setLength(0);
292        assertSame(sb, sb.append(0));
293        assertEquals(String.valueOf(0), sb.toString());
294        sb.setLength(0);
295        assertSame(sb, sb.append(-1));
296        assertEquals(String.valueOf(-1), sb.toString());
297        sb.setLength(0);
298        assertSame(sb, sb.append(Integer.MIN_VALUE));
299        assertEquals(String.valueOf(Integer.MIN_VALUE), sb.toString());
300        sb.setLength(0);
301        assertSame(sb, sb.append(Integer.MAX_VALUE));
302        assertEquals(String.valueOf(Integer.MAX_VALUE), sb.toString());
303    }
304
305    /**
306     * java.lang.StringBuilder.append(long)
307     */
308    public void test_appendL() {
309        StringBuilder sb = new StringBuilder();
310        assertSame(sb, sb.append(1L));
311        assertEquals(String.valueOf(1L), sb.toString());
312        sb.setLength(0);
313        assertSame(sb, sb.append(0L));
314        assertEquals(String.valueOf(0L), sb.toString());
315        sb.setLength(0);
316        assertSame(sb, sb.append(-1L));
317        assertEquals(String.valueOf(-1L), sb.toString());
318        sb.setLength(0);
319        assertSame(sb, sb.append(Integer.MIN_VALUE));
320        assertEquals(String.valueOf(Integer.MIN_VALUE), sb.toString());
321        sb.setLength(0);
322        assertSame(sb, sb.append(Integer.MAX_VALUE));
323        assertEquals(String.valueOf(Integer.MAX_VALUE), sb.toString());
324    }
325
326    /**
327     * java.lang.StringBuilder.append(Object)'
328     */
329    public void test_appendLjava_lang_Object() {
330        StringBuilder sb = new StringBuilder();
331        assertSame(sb, sb.append(Fixture.INSTANCE));
332        assertEquals(Fixture.INSTANCE.toString(), sb.toString());
333
334        sb.setLength(0);
335        assertSame(sb, sb.append((Object) null));
336        assertEquals("null", sb.toString());
337    }
338
339    /**
340     * java.lang.StringBuilder.append(String)
341     */
342    public void test_appendLjava_lang_String() {
343        StringBuilder sb = new StringBuilder();
344        assertSame(sb, sb.append("ab"));
345        assertEquals("ab", sb.toString());
346        sb.setLength(0);
347        assertSame(sb, sb.append("cd"));
348        assertEquals("cd", sb.toString());
349        sb.setLength(0);
350        assertSame(sb, sb.append((String) null));
351        assertEquals("null", sb.toString());
352    }
353
354    /**
355     * java.lang.StringBuilder.append(StringBuffer)
356     */
357    public void test_appendLjava_lang_StringBuffer() {
358        StringBuilder sb = new StringBuilder();
359        assertSame(sb, sb.append(new StringBuffer("ab")));
360        assertEquals("ab", sb.toString());
361        sb.setLength(0);
362        assertSame(sb, sb.append(new StringBuffer("cd")));
363        assertEquals("cd", sb.toString());
364        sb.setLength(0);
365        assertSame(sb, sb.append((StringBuffer) null));
366        assertEquals("null", sb.toString());
367    }
368
369    /**
370     * java.lang.StringBuilder.appendCodePoint(int)'
371     */
372    public void test_appendCodePointI() {
373        StringBuilder sb = new StringBuilder();
374        sb.appendCodePoint(0x10000);
375        assertEquals("\uD800\uDC00", sb.toString());
376        sb.append("fixture");
377        assertEquals("\uD800\uDC00fixture", sb.toString());
378        sb.appendCodePoint(0x00010FFFF);
379        assertEquals("\uD800\uDC00fixture\uDBFF\uDFFF", sb.toString());
380    }
381
382    /**
383     * java.lang.StringBuilder.capacity()'
384     */
385    public void test_capacity() {
386        StringBuilder sb = new StringBuilder();
387        assertEquals(16, sb.capacity());
388        sb.append("0123456789ABCDEF0123456789ABCDEF");
389        assertTrue(sb.capacity() > 16);
390    }
391
392    /**
393     * java.lang.StringBuilder.charAt(int)'
394     */
395    public void test_charAtI() {
396        final String fixture = "0123456789";
397        StringBuilder sb = new StringBuilder(fixture);
398        for (int i = 0; i < fixture.length(); i++) {
399            assertEquals((char) ('0' + i), sb.charAt(i));
400        }
401
402        try {
403            sb.charAt(-1);
404            fail("no IOOBE, negative index");
405        } catch (IndexOutOfBoundsException e) {
406            // Expected
407        }
408
409        try {
410            sb.charAt(fixture.length());
411            fail("no IOOBE, equal to length");
412        } catch (IndexOutOfBoundsException e) {
413        }
414
415        try {
416            sb.charAt(fixture.length() + 1);
417            fail("no IOOBE, greater than length");
418        } catch (IndexOutOfBoundsException e) {
419        }
420    }
421
422    /**
423     * java.lang.StringBuilder.codePointAt(int)
424     */
425    public void test_codePointAtI() {
426        StringBuilder sb = new StringBuilder("abc");
427        assertEquals('a', sb.codePointAt(0));
428        assertEquals('b', sb.codePointAt(1));
429        assertEquals('c', sb.codePointAt(2));
430
431        sb = new StringBuilder("\uD800\uDC00");
432        assertEquals(0x10000, sb.codePointAt(0));
433        assertEquals('\uDC00', sb.codePointAt(1));
434
435        sb = new StringBuilder();
436        sb.append("abc");
437        try {
438            sb.codePointAt(-1);
439            fail("No IOOBE on negative index.");
440        } catch (IndexOutOfBoundsException e) {
441
442        }
443
444        try {
445            sb.codePointAt(sb.length());
446            fail("No IOOBE on index equal to length.");
447        } catch (IndexOutOfBoundsException e) {
448
449        }
450
451        try {
452            sb.codePointAt(sb.length() + 1);
453            fail("No IOOBE on index greater than length.");
454        } catch (IndexOutOfBoundsException e) {
455
456        }
457    }
458
459    /**
460     * java.lang.StringBuilder.codePointBefore(int)
461     */
462    public void test_codePointBeforeI() {
463        StringBuilder sb = new StringBuilder("abc");
464        assertEquals('a', sb.codePointBefore(1));
465        assertEquals('b', sb.codePointBefore(2));
466        assertEquals('c', sb.codePointBefore(3));
467
468        sb = new StringBuilder("\uD800\uDC00");
469        assertEquals(0x10000, sb.codePointBefore(2));
470        assertEquals('\uD800', sb.codePointBefore(1));
471
472        sb = new StringBuilder();
473        sb.append("abc");
474
475        try {
476            sb.codePointBefore(0);
477            fail("No IOOBE on zero index.");
478        } catch (IndexOutOfBoundsException e) {
479
480        }
481
482        try {
483            sb.codePointBefore(-1);
484            fail("No IOOBE on negative index.");
485        } catch (IndexOutOfBoundsException e) {
486
487        }
488
489        try {
490            sb.codePointBefore(sb.length() + 1);
491            fail("No IOOBE on index greater than length.");
492        } catch (IndexOutOfBoundsException e) {
493
494        }
495    }
496
497    /**
498     * java.lang.StringBuilder.codePointCount(int, int)
499     */
500    public void test_codePointCountII() {
501        assertEquals(1, new StringBuilder("\uD800\uDC00").codePointCount(0, 2));
502        assertEquals(1, new StringBuilder("\uD800\uDC01").codePointCount(0, 2));
503        assertEquals(1, new StringBuilder("\uD801\uDC01").codePointCount(0, 2));
504        assertEquals(1, new StringBuilder("\uDBFF\uDFFF").codePointCount(0, 2));
505
506        assertEquals(3, new StringBuilder("a\uD800\uDC00b").codePointCount(0, 4));
507        assertEquals(4, new StringBuilder("a\uD800\uDC00b\uD800").codePointCount(0, 5));
508
509        StringBuilder sb = new StringBuilder();
510        sb.append("abc");
511        try {
512            sb.codePointCount(-1, 2);
513            fail("No IOOBE for negative begin index.");
514        } catch (IndexOutOfBoundsException e) {
515
516        }
517
518        try {
519            sb.codePointCount(0, 4);
520            fail("No IOOBE for end index that's too large.");
521        } catch (IndexOutOfBoundsException e) {
522
523        }
524
525        try {
526            sb.codePointCount(3, 2);
527            fail("No IOOBE for begin index larger than end index.");
528        } catch (IndexOutOfBoundsException e) {
529
530        }
531    }
532
533    /**
534     * java.lang.StringBuilder.delete(int, int)
535     */
536    public void test_deleteII() {
537        final String fixture = "0123456789";
538        StringBuilder sb = new StringBuilder(fixture);
539        assertSame(sb, sb.delete(0, 0));
540        assertEquals(fixture, sb.toString());
541        assertSame(sb, sb.delete(5, 5));
542        assertEquals(fixture, sb.toString());
543        assertSame(sb, sb.delete(0, 1));
544        assertEquals("123456789", sb.toString());
545        assertEquals(9, sb.length());
546        assertSame(sb, sb.delete(0, sb.length()));
547        assertEquals("", sb.toString());
548        assertEquals(0, sb.length());
549
550        sb = new StringBuilder(fixture);
551        assertSame(sb, sb.delete(0, 11));
552        assertEquals("", sb.toString());
553        assertEquals(0, sb.length());
554
555        try {
556            new StringBuilder(fixture).delete(-1, 2);
557            fail("no SIOOBE, negative start");
558        } catch (StringIndexOutOfBoundsException e) {
559            // Expected
560        }
561
562        try {
563            new StringBuilder(fixture).delete(11, 12);
564            fail("no SIOOBE, start too far");
565        } catch (StringIndexOutOfBoundsException e) {
566            // Expected
567        }
568
569        try {
570            new StringBuilder(fixture).delete(13, 12);
571            fail("no SIOOBE, start larger than end");
572        } catch (StringIndexOutOfBoundsException e) {
573            // Expected
574        }
575
576        // HARMONY 6212
577        sb = new StringBuilder();
578        sb.append("abcde");
579        String str = sb.toString();
580        sb.delete(0, sb.length());
581        sb.append("YY");
582        assertEquals("abcde", str);
583        assertEquals("YY", sb.toString());
584    }
585
586    /**
587     * java.lang.StringBuilder.deleteCharAt(int)
588     */
589    public void test_deleteCharAtI() {
590        final String fixture = "0123456789";
591        StringBuilder sb = new StringBuilder(fixture);
592        assertSame(sb, sb.deleteCharAt(0));
593        assertEquals("123456789", sb.toString());
594        assertEquals(9, sb.length());
595        sb = new StringBuilder(fixture);
596        assertSame(sb, sb.deleteCharAt(5));
597        assertEquals("012346789", sb.toString());
598        assertEquals(9, sb.length());
599        sb = new StringBuilder(fixture);
600        assertSame(sb, sb.deleteCharAt(9));
601        assertEquals("012345678", sb.toString());
602        assertEquals(9, sb.length());
603
604        try {
605            new StringBuilder(fixture).deleteCharAt(-1);
606            fail("no SIOOBE, negative index");
607        } catch (StringIndexOutOfBoundsException e) {
608            // Expected
609        }
610
611        try {
612            new StringBuilder(fixture).deleteCharAt(fixture.length());
613            fail("no SIOOBE, index equals length");
614        } catch (StringIndexOutOfBoundsException e) {
615            // Expected
616        }
617
618        try {
619            new StringBuilder(fixture).deleteCharAt(fixture.length() + 1);
620            fail("no SIOOBE, index exceeds length");
621        } catch (StringIndexOutOfBoundsException e) {
622            // Expected
623        }
624    }
625
626    /**
627     * java.lang.StringBuilder.ensureCapacity(int)'
628     */
629    public void test_ensureCapacityI() {
630        StringBuilder sb = new StringBuilder(5);
631        assertEquals(5, sb.capacity());
632        sb.ensureCapacity(10);
633        assertEquals(12, sb.capacity());
634        sb.ensureCapacity(26);
635        assertEquals(26, sb.capacity());
636        sb.ensureCapacity(55);
637        assertEquals(55, sb.capacity());
638    }
639
640    /**
641     * java.lang.StringBuilder.getChars(int, int, char[], int)'
642     */
643    public void test_getCharsII$CI() {
644        final String fixture = "0123456789";
645        StringBuilder sb = new StringBuilder(fixture);
646        char[] dst = new char[10];
647        sb.getChars(0, 10, dst, 0);
648        assertTrue(Arrays.equals(fixture.toCharArray(), dst));
649
650        Arrays.fill(dst, '\0');
651        sb.getChars(0, 5, dst, 0);
652        char[] fixtureChars = new char[10];
653        fixture.getChars(0, 5, fixtureChars, 0);
654        assertTrue(Arrays.equals(fixtureChars, dst));
655
656        Arrays.fill(dst, '\0');
657        Arrays.fill(fixtureChars, '\0');
658        sb.getChars(0, 5, dst, 5);
659        fixture.getChars(0, 5, fixtureChars, 5);
660        assertTrue(Arrays.equals(fixtureChars, dst));
661
662        Arrays.fill(dst, '\0');
663        Arrays.fill(fixtureChars, '\0');
664        sb.getChars(5, 10, dst, 1);
665        fixture.getChars(5, 10, fixtureChars, 1);
666        assertTrue(Arrays.equals(fixtureChars, dst));
667
668        try {
669            sb.getChars(0, 10, null, 0);
670            fail("no NPE");
671        } catch (NullPointerException e) {
672            // Expected
673        }
674
675        try {
676            sb.getChars(-1, 10, dst, 0);
677            fail("no IOOBE, srcBegin negative");
678        } catch (IndexOutOfBoundsException e) {
679            // Expected
680        }
681
682        try {
683            sb.getChars(0, 10, dst, -1);
684            fail("no IOOBE, dstBegin negative");
685        } catch (IndexOutOfBoundsException e) {
686            // Expected
687        }
688
689        try {
690            sb.getChars(5, 4, dst, 0);
691            fail("no IOOBE, srcBegin > srcEnd");
692        } catch (IndexOutOfBoundsException e) {
693            // Expected
694        }
695
696        try {
697            sb.getChars(0, 11, dst, 0);
698            fail("no IOOBE, srcEnd > length");
699        } catch (IndexOutOfBoundsException e) {
700            // Expected
701        }
702
703        try {
704            sb.getChars(0, 10, dst, 5);
705            fail("no IOOBE, dstBegin and src size too large for what's left in dst");
706        } catch (IndexOutOfBoundsException e) {
707            // Expected
708        }
709    }
710
711    /**
712     * java.lang.StringBuilder.indexOf(String)
713     */
714    public void test_indexOfLjava_lang_String() {
715        final String fixture = "0123456789";
716        StringBuilder sb = new StringBuilder(fixture);
717        assertEquals(0, sb.indexOf("0"));
718        assertEquals(0, sb.indexOf("012"));
719        assertEquals(-1, sb.indexOf("02"));
720        assertEquals(8, sb.indexOf("89"));
721
722        try {
723            sb.indexOf(null);
724            fail("no NPE");
725        } catch (NullPointerException e) {
726            // Expected
727        }
728    }
729
730    /**
731     * java.lang.StringBuilder.indexOf(String, int)
732     */
733    public void test_IndexOfStringInt() {
734        final String fixture = "0123456789";
735        StringBuilder sb = new StringBuilder(fixture);
736        assertEquals(0, sb.indexOf("0"));
737        assertEquals(0, sb.indexOf("012"));
738        assertEquals(-1, sb.indexOf("02"));
739        assertEquals(8, sb.indexOf("89"));
740
741        assertEquals(0, sb.indexOf("0"), 0);
742        assertEquals(0, sb.indexOf("012"), 0);
743        assertEquals(-1, sb.indexOf("02"), 0);
744        assertEquals(8, sb.indexOf("89"), 0);
745
746        assertEquals(-1, sb.indexOf("0"), 5);
747        assertEquals(-1, sb.indexOf("012"), 5);
748        assertEquals(-1, sb.indexOf("02"), 0);
749        assertEquals(8, sb.indexOf("89"), 5);
750
751        try {
752            sb.indexOf(null, 0);
753            fail("no NPE");
754        } catch (NullPointerException e) {
755            // Expected
756        }
757    }
758
759    /**
760     * java.lang.StringBuilder.insert(int, boolean)
761     */
762    public void test_insertIZ() {
763        final String fixture = "0000";
764        StringBuilder sb = new StringBuilder(fixture);
765        assertSame(sb, sb.insert(0, true));
766        assertEquals("true0000", sb.toString());
767        assertEquals(8, sb.length());
768
769        sb = new StringBuilder(fixture);
770        assertSame(sb, sb.insert(0, false));
771        assertEquals("false0000", sb.toString());
772        assertEquals(9, sb.length());
773
774        sb = new StringBuilder(fixture);
775        assertSame(sb, sb.insert(2, false));
776        assertEquals("00false00", sb.toString());
777        assertEquals(9, sb.length());
778
779        sb = new StringBuilder(fixture);
780        assertSame(sb, sb.insert(4, false));
781        assertEquals("0000false", sb.toString());
782        assertEquals(9, sb.length());
783
784        try {
785            sb = new StringBuilder(fixture);
786            sb.insert(-1, false);
787            fail("no SIOOBE, negative index");
788        } catch (StringIndexOutOfBoundsException e) {
789            // Expected
790        }
791
792        try {
793            sb = new StringBuilder(fixture);
794            sb.insert(5, false);
795            fail("no SIOOBE, index too large index");
796        } catch (StringIndexOutOfBoundsException e) {
797            // Expected
798        }
799    }
800
801    /**
802     * java.lang.StringBuilder.insert(int, char)
803     */
804    public void test_insertIC() {
805        final String fixture = "0000";
806        StringBuilder sb = new StringBuilder(fixture);
807        assertSame(sb, sb.insert(0, 'a'));
808        assertEquals("a0000", sb.toString());
809        assertEquals(5, sb.length());
810
811        sb = new StringBuilder(fixture);
812        assertSame(sb, sb.insert(0, 'b'));
813        assertEquals("b0000", sb.toString());
814        assertEquals(5, sb.length());
815
816        sb = new StringBuilder(fixture);
817        assertSame(sb, sb.insert(2, 'b'));
818        assertEquals("00b00", sb.toString());
819        assertEquals(5, sb.length());
820
821        sb = new StringBuilder(fixture);
822        assertSame(sb, sb.insert(4, 'b'));
823        assertEquals("0000b", sb.toString());
824        assertEquals(5, sb.length());
825
826        // FIXME this fails on Sun JRE 5.0_5
827//		try {
828//			sb = new StringBuilder(fixture);
829//			sb.insert(-1, 'a');
830//			fail("no SIOOBE, negative index");
831//		} catch (StringIndexOutOfBoundsException e) {
832//			// Expected
833//		}
834
835        /*
836           * FIXME This fails on Sun JRE 5.0_5, but that seems like a bug, since
837           * the 'insert(int, char[]) behaves this way.
838           */
839//		try {
840//			sb = new StringBuilder(fixture);
841//			sb.insert(5, 'a');
842//			fail("no SIOOBE, index too large index");
843//		} catch (StringIndexOutOfBoundsException e) {
844//			// Expected
845//		}
846    }
847
848    /**
849     * java.lang.StringBuilder.insert(int, char)
850     */
851    public void test_insertIC_2() {
852        StringBuilder obj = new StringBuilder();
853        try {
854            obj.insert(-1, '?');
855            fail("ArrayIndexOutOfBoundsException expected");
856        } catch (ArrayIndexOutOfBoundsException e) {
857            // expected
858        }
859    }
860
861    /**
862     * java.lang.StringBuilder.insert(int, char[])'
863     */
864    public void test_insertI$C() {
865        final String fixture = "0000";
866        StringBuilder sb = new StringBuilder(fixture);
867        assertSame(sb, sb.insert(0, new char[] { 'a', 'b' }));
868        assertEquals("ab0000", sb.toString());
869        assertEquals(6, sb.length());
870
871        sb = new StringBuilder(fixture);
872        assertSame(sb, sb.insert(2, new char[] { 'a', 'b' }));
873        assertEquals("00ab00", sb.toString());
874        assertEquals(6, sb.length());
875
876        sb = new StringBuilder(fixture);
877        assertSame(sb, sb.insert(4, new char[] { 'a', 'b' }));
878        assertEquals("0000ab", sb.toString());
879        assertEquals(6, sb.length());
880
881        /*
882           * TODO This NPE is the behavior on Sun's JRE 5.0_5, but it's
883           * undocumented. The assumption is that this method behaves like
884           * String.valueOf(char[]), which does throw a NPE too, but that is also
885           * undocumented.
886           */
887
888        try {
889            sb.insert(0, (char[]) null);
890            fail("no NPE");
891        } catch (NullPointerException e) {
892            // Expected
893        }
894
895        try {
896            sb = new StringBuilder(fixture);
897            sb.insert(-1, new char[] { 'a', 'b' });
898            fail("no SIOOBE, negative index");
899        } catch (StringIndexOutOfBoundsException e) {
900            // Expected
901        }
902
903        try {
904            sb = new StringBuilder(fixture);
905            sb.insert(5, new char[] { 'a', 'b' });
906            fail("no SIOOBE, index too large index");
907        } catch (StringIndexOutOfBoundsException e) {
908            // Expected
909        }
910    }
911
912    /**
913     * java.lang.StringBuilder.insert(int, char[], int, int)
914     */
915    public void test_insertI$CII() {
916        final String fixture = "0000";
917        StringBuilder sb = new StringBuilder(fixture);
918        assertSame(sb, sb.insert(0, new char[] { 'a', 'b' }, 0, 2));
919        assertEquals("ab0000", sb.toString());
920        assertEquals(6, sb.length());
921
922        sb = new StringBuilder(fixture);
923        assertSame(sb, sb.insert(0, new char[] { 'a', 'b' }, 0, 1));
924        assertEquals("a0000", sb.toString());
925        assertEquals(5, sb.length());
926
927        sb = new StringBuilder(fixture);
928        assertSame(sb, sb.insert(2, new char[] { 'a', 'b' }, 0, 2));
929        assertEquals("00ab00", sb.toString());
930        assertEquals(6, sb.length());
931
932        sb = new StringBuilder(fixture);
933        assertSame(sb, sb.insert(2, new char[] { 'a', 'b' }, 0, 1));
934        assertEquals("00a00", sb.toString());
935        assertEquals(5, sb.length());
936
937        sb = new StringBuilder(fixture);
938        assertSame(sb, sb.insert(4, new char[] { 'a', 'b' }, 0, 2));
939        assertEquals("0000ab", sb.toString());
940        assertEquals(6, sb.length());
941
942        sb = new StringBuilder(fixture);
943        assertSame(sb, sb.insert(4, new char[] { 'a', 'b' }, 0, 1));
944        assertEquals("0000a", sb.toString());
945        assertEquals(5, sb.length());
946
947        /*
948           * TODO This NPE is the behavior on Sun's JRE 5.0_5, but it's
949           * undocumented. The assumption is that this method behaves like
950           * String.valueOf(char[]), which does throw a NPE too, but that is also
951           * undocumented.
952           */
953
954        try {
955            sb.insert(0, (char[]) null, 0, 2);
956            fail("no NPE");
957        } catch (NullPointerException e) {
958            // Expected
959        }
960
961        try {
962            sb = new StringBuilder(fixture);
963            sb.insert(-1, new char[] { 'a', 'b' }, 0, 2);
964            fail("no SIOOBE, negative index");
965        } catch (StringIndexOutOfBoundsException e) {
966            // Expected
967        }
968
969        try {
970            sb = new StringBuilder(fixture);
971            sb.insert(5, new char[] { 'a', 'b' }, 0, 2);
972            fail("no SIOOBE, index too large index");
973        } catch (StringIndexOutOfBoundsException e) {
974            // Expected
975        }
976
977        try {
978            sb = new StringBuilder(fixture);
979            sb.insert(5, new char[] { 'a', 'b' }, -1, 2);
980            fail("no SIOOBE, negative offset");
981        } catch (StringIndexOutOfBoundsException e) {
982            // Expected
983        }
984
985        try {
986            sb = new StringBuilder(fixture);
987            sb.insert(5, new char[] { 'a', 'b' }, 0, -1);
988            fail("no SIOOBE, negative length");
989        } catch (StringIndexOutOfBoundsException e) {
990            // Expected
991        }
992
993        try {
994            sb = new StringBuilder(fixture);
995            sb.insert(5, new char[] { 'a', 'b' }, 0, 3);
996            fail("no SIOOBE, too long");
997        } catch (StringIndexOutOfBoundsException e) {
998            // Expected
999        }
1000    }
1001
1002    /**
1003     * java.lang.StringBuilder.insert(int, CharSequence)
1004     */
1005    public void test_insertILjava_lang_CharSequence() {
1006        final String fixture = "0000";
1007        StringBuilder sb = new StringBuilder(fixture);
1008        assertSame(sb, sb.insert(0, (CharSequence) "ab"));
1009        assertEquals("ab0000", sb.toString());
1010        assertEquals(6, sb.length());
1011
1012        sb = new StringBuilder(fixture);
1013        assertSame(sb, sb.insert(2, (CharSequence) "ab"));
1014        assertEquals("00ab00", sb.toString());
1015        assertEquals(6, sb.length());
1016
1017        sb = new StringBuilder(fixture);
1018        assertSame(sb, sb.insert(4, (CharSequence) "ab"));
1019        assertEquals("0000ab", sb.toString());
1020        assertEquals(6, sb.length());
1021
1022        sb = new StringBuilder(fixture);
1023        assertSame(sb, sb.insert(4, (CharSequence) null));
1024        assertEquals("0000null", sb.toString());
1025        assertEquals(8, sb.length());
1026
1027        try {
1028            sb = new StringBuilder(fixture);
1029            sb.insert(-1, (CharSequence) "ab");
1030            fail("no IOOBE, negative index");
1031        } catch (IndexOutOfBoundsException e) {
1032            // Expected
1033        }
1034
1035        try {
1036            sb = new StringBuilder(fixture);
1037            sb.insert(5, (CharSequence) "ab");
1038            fail("no IOOBE, index too large index");
1039        } catch (IndexOutOfBoundsException e) {
1040            // Expected
1041        }
1042    }
1043
1044    /**
1045     * java.lang.StringBuilder.insert(int, CharSequence, int, int)
1046     */
1047    @SuppressWarnings("cast")
1048    public void test_insertILjava_lang_CharSequenceII() {
1049        final String fixture = "0000";
1050        StringBuilder sb = new StringBuilder(fixture);
1051        assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 2));
1052        assertEquals("ab0000", sb.toString());
1053        assertEquals(6, sb.length());
1054
1055        sb = new StringBuilder(fixture);
1056        assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 1));
1057        assertEquals("a0000", sb.toString());
1058        assertEquals(5, sb.length());
1059
1060        sb = new StringBuilder(fixture);
1061        assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 2));
1062        assertEquals("00ab00", sb.toString());
1063        assertEquals(6, sb.length());
1064
1065        sb = new StringBuilder(fixture);
1066        assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 1));
1067        assertEquals("00a00", sb.toString());
1068        assertEquals(5, sb.length());
1069
1070        sb = new StringBuilder(fixture);
1071        assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 2));
1072        assertEquals("0000ab", sb.toString());
1073        assertEquals(6, sb.length());
1074
1075        sb = new StringBuilder(fixture);
1076        assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 1));
1077        assertEquals("0000a", sb.toString());
1078        assertEquals(5, sb.length());
1079
1080        sb = new StringBuilder(fixture);
1081        assertSame(sb, sb.insert(4, (CharSequence) null, 0, 2));
1082        assertEquals("0000nu", sb.toString());
1083        assertEquals(6, sb.length());
1084
1085        try {
1086            sb = new StringBuilder(fixture);
1087            sb.insert(-1, (CharSequence) "ab", 0, 2);
1088            fail("no IOOBE, negative index");
1089        } catch (IndexOutOfBoundsException e) {
1090            // Expected
1091        }
1092
1093        try {
1094            sb = new StringBuilder(fixture);
1095            sb.insert(5, (CharSequence) "ab", 0, 2);
1096            fail("no IOOBE, index too large index");
1097        } catch (IndexOutOfBoundsException e) {
1098            // Expected
1099        }
1100
1101        try {
1102            sb = new StringBuilder(fixture);
1103            sb.insert(5, (CharSequence) "ab", -1, 2);
1104            fail("no IOOBE, negative offset");
1105        } catch (IndexOutOfBoundsException e) {
1106            // Expected
1107        }
1108
1109        try {
1110            sb = new StringBuilder(fixture);
1111            sb.insert(5, new char[] { 'a', 'b' }, 0, -1);
1112            fail("no IOOBE, negative length");
1113        } catch (IndexOutOfBoundsException e) {
1114            // Expected
1115        }
1116
1117        try {
1118            sb = new StringBuilder(fixture);
1119            sb.insert(5, new char[] { 'a', 'b' }, 0, 3);
1120            fail("no IOOBE, too long");
1121        } catch (IndexOutOfBoundsException e) {
1122            // Expected
1123        }
1124    }
1125
1126    /**
1127     * java.lang.StringBuilder.insert(int, double)
1128     */
1129    public void test_insertID() {
1130        final String fixture = "0000";
1131        StringBuilder sb = new StringBuilder(fixture);
1132        assertSame(sb, sb.insert(0, -1D));
1133        assertEquals("-1.00000", sb.toString());
1134        assertEquals(8, sb.length());
1135
1136        sb = new StringBuilder(fixture);
1137        assertSame(sb, sb.insert(0, 0D));
1138        assertEquals("0.00000", sb.toString());
1139        assertEquals(7, sb.length());
1140
1141        sb = new StringBuilder(fixture);
1142        assertSame(sb, sb.insert(2, 1D));
1143        assertEquals("001.000", sb.toString());
1144        assertEquals(7, sb.length());
1145
1146        sb = new StringBuilder(fixture);
1147        assertSame(sb, sb.insert(4, 2D));
1148        assertEquals("00002.0", sb.toString());
1149        assertEquals(7, sb.length());
1150
1151        try {
1152            sb = new StringBuilder(fixture);
1153            sb.insert(-1, 1D);
1154            fail("no IOOBE, negative index");
1155        } catch (IndexOutOfBoundsException e) {
1156            // Expected
1157        }
1158
1159        try {
1160            sb = new StringBuilder(fixture);
1161            sb.insert(5, 1D);
1162            fail("no IOOBE, index too large index");
1163        } catch (IndexOutOfBoundsException e) {
1164            // Expected
1165        }
1166    }
1167
1168    /**
1169     * java.lang.StringBuilder.insert(int, float)
1170     */
1171    public void test_insertIF() {
1172        final String fixture = "0000";
1173        StringBuilder sb = new StringBuilder(fixture);
1174        assertSame(sb, sb.insert(0, -1F));
1175        assertEquals("-1.00000", sb.toString());
1176        assertEquals(8, sb.length());
1177
1178        sb = new StringBuilder(fixture);
1179        assertSame(sb, sb.insert(0, 0F));
1180        assertEquals("0.00000", sb.toString());
1181        assertEquals(7, sb.length());
1182
1183        sb = new StringBuilder(fixture);
1184        assertSame(sb, sb.insert(2, 1F));
1185        assertEquals("001.000", sb.toString());
1186        assertEquals(7, sb.length());
1187
1188        sb = new StringBuilder(fixture);
1189        assertSame(sb, sb.insert(4, 2F));
1190        assertEquals("00002.0", sb.toString());
1191        assertEquals(7, sb.length());
1192
1193        try {
1194            sb = new StringBuilder(fixture);
1195            sb.insert(-1, 1F);
1196            fail("no IOOBE, negative index");
1197        } catch (IndexOutOfBoundsException e) {
1198            // Expected
1199        }
1200
1201        try {
1202            sb = new StringBuilder(fixture);
1203            sb.insert(5, 1F);
1204            fail("no IOOBE, index too large index");
1205        } catch (IndexOutOfBoundsException e) {
1206            // Expected
1207        }
1208    }
1209
1210    /**
1211     * java.lang.StringBuilder.insert(int, int)
1212     */
1213    public void test_insertII() {
1214        final String fixture = "0000";
1215        StringBuilder sb = new StringBuilder(fixture);
1216        assertSame(sb, sb.insert(0, -1));
1217        assertEquals("-10000", sb.toString());
1218        assertEquals(6, sb.length());
1219
1220        sb = new StringBuilder(fixture);
1221        assertSame(sb, sb.insert(0, 0));
1222        assertEquals("00000", sb.toString());
1223        assertEquals(5, sb.length());
1224
1225        sb = new StringBuilder(fixture);
1226        assertSame(sb, sb.insert(2, 1));
1227        assertEquals("00100", sb.toString());
1228        assertEquals(5, sb.length());
1229
1230        sb = new StringBuilder(fixture);
1231        assertSame(sb, sb.insert(4, 2));
1232        assertEquals("00002", sb.toString());
1233        assertEquals(5, sb.length());
1234
1235        try {
1236            sb = new StringBuilder(fixture);
1237            sb.insert(-1, 1);
1238            fail("no IOOBE, negative index");
1239        } catch (IndexOutOfBoundsException e) {
1240            // Expected
1241        }
1242
1243        try {
1244            sb = new StringBuilder(fixture);
1245            sb.insert(5, 1);
1246            fail("no IOOBE, index too large index");
1247        } catch (IndexOutOfBoundsException e) {
1248            // Expected
1249        }
1250    }
1251
1252    /**
1253     * java.lang.StringBuilder.insert(int, long)
1254     */
1255    public void test_insertIJ() {
1256        final String fixture = "0000";
1257        StringBuilder sb = new StringBuilder(fixture);
1258        assertSame(sb, sb.insert(0, -1L));
1259        assertEquals("-10000", sb.toString());
1260        assertEquals(6, sb.length());
1261
1262        sb = new StringBuilder(fixture);
1263        assertSame(sb, sb.insert(0, 0L));
1264        assertEquals("00000", sb.toString());
1265        assertEquals(5, sb.length());
1266
1267        sb = new StringBuilder(fixture);
1268        assertSame(sb, sb.insert(2, 1L));
1269        assertEquals("00100", sb.toString());
1270        assertEquals(5, sb.length());
1271
1272        sb = new StringBuilder(fixture);
1273        assertSame(sb, sb.insert(4, 2L));
1274        assertEquals("00002", sb.toString());
1275        assertEquals(5, sb.length());
1276
1277        try {
1278            sb = new StringBuilder(fixture);
1279            sb.insert(-1, 1L);
1280            fail("no IOOBE, negative index");
1281        } catch (IndexOutOfBoundsException e) {
1282            // Expected
1283        }
1284
1285        try {
1286            sb = new StringBuilder(fixture);
1287            sb.insert(5, 1L);
1288            fail("no IOOBE, index too large index");
1289        } catch (IndexOutOfBoundsException e) {
1290            // Expected
1291        }
1292    }
1293
1294    /**
1295     * java.lang.StringBuilder.insert(int, Object)
1296     */
1297    public void test_insertILjava_lang_Object() {
1298        final String fixture = "0000";
1299        StringBuilder sb = new StringBuilder(fixture);
1300        assertSame(sb, sb.insert(0, Fixture.INSTANCE));
1301        assertEquals("fixture0000", sb.toString());
1302        assertEquals(11, sb.length());
1303
1304        sb = new StringBuilder(fixture);
1305        assertSame(sb, sb.insert(2, Fixture.INSTANCE));
1306        assertEquals("00fixture00", sb.toString());
1307        assertEquals(11, sb.length());
1308
1309        sb = new StringBuilder(fixture);
1310        assertSame(sb, sb.insert(4, Fixture.INSTANCE));
1311        assertEquals("0000fixture", sb.toString());
1312        assertEquals(11, sb.length());
1313
1314        sb = new StringBuilder(fixture);
1315        assertSame(sb, sb.insert(4, (Object) null));
1316        assertEquals("0000null", sb.toString());
1317        assertEquals(8, sb.length());
1318
1319        try {
1320            sb = new StringBuilder(fixture);
1321            sb.insert(-1, Fixture.INSTANCE);
1322            fail("no IOOBE, negative index");
1323        } catch (IndexOutOfBoundsException e) {
1324            // Expected
1325        }
1326
1327        try {
1328            sb = new StringBuilder(fixture);
1329            sb.insert(5, Fixture.INSTANCE);
1330            fail("no IOOBE, index too large index");
1331        } catch (IndexOutOfBoundsException e) {
1332            // Expected
1333        }
1334    }
1335
1336    /**
1337     * java.lang.StringBuilder.insert(int, String)
1338     */
1339    public void test_insertILjava_lang_String() {
1340        final String fixture = "0000";
1341        StringBuilder sb = new StringBuilder(fixture);
1342        assertSame(sb, sb.insert(0, "fixture"));
1343        assertEquals("fixture0000", sb.toString());
1344        assertEquals(11, sb.length());
1345
1346        sb = new StringBuilder(fixture);
1347        assertSame(sb, sb.insert(2, "fixture"));
1348        assertEquals("00fixture00", sb.toString());
1349        assertEquals(11, sb.length());
1350
1351        sb = new StringBuilder(fixture);
1352        assertSame(sb, sb.insert(4, "fixture"));
1353        assertEquals("0000fixture", sb.toString());
1354        assertEquals(11, sb.length());
1355
1356        sb = new StringBuilder(fixture);
1357        assertSame(sb, sb.insert(4, (Object) null));
1358        assertEquals("0000null", sb.toString());
1359        assertEquals(8, sb.length());
1360
1361        try {
1362            sb = new StringBuilder(fixture);
1363            sb.insert(-1, "fixture");
1364            fail("no IOOBE, negative index");
1365        } catch (IndexOutOfBoundsException e) {
1366            // Expected
1367        }
1368
1369        try {
1370            sb = new StringBuilder(fixture);
1371            sb.insert(5, "fixture");
1372            fail("no IOOBE, index too large index");
1373        } catch (IndexOutOfBoundsException e) {
1374            // Expected
1375        }
1376    }
1377
1378    /**
1379     * java.lang.StringBuilder.lastIndexOf(String)
1380     */
1381    public void test_lastIndexOfLjava_lang_String() {
1382        final String fixture = "0123456789";
1383        StringBuilder sb = new StringBuilder(fixture);
1384        assertEquals(0, sb.lastIndexOf("0"));
1385        assertEquals(0, sb.lastIndexOf("012"));
1386        assertEquals(-1, sb.lastIndexOf("02"));
1387        assertEquals(8, sb.lastIndexOf("89"));
1388
1389        try {
1390            sb.lastIndexOf(null);
1391            fail("no NPE");
1392        } catch (NullPointerException e) {
1393            // Expected
1394        }
1395    }
1396
1397    /**
1398     * java.lang.StringBuilder.lastIndexOf(String, int)
1399     */
1400    public void test_lastIndexOfLjava_lang_StringI() {
1401        final String fixture = "0123456789";
1402        StringBuilder sb = new StringBuilder(fixture);
1403        assertEquals(0, sb.lastIndexOf("0"));
1404        assertEquals(0, sb.lastIndexOf("012"));
1405        assertEquals(-1, sb.lastIndexOf("02"));
1406        assertEquals(8, sb.lastIndexOf("89"));
1407
1408        assertEquals(0, sb.lastIndexOf("0"), 0);
1409        assertEquals(0, sb.lastIndexOf("012"), 0);
1410        assertEquals(-1, sb.lastIndexOf("02"), 0);
1411        assertEquals(8, sb.lastIndexOf("89"), 0);
1412
1413        assertEquals(-1, sb.lastIndexOf("0"), 5);
1414        assertEquals(-1, sb.lastIndexOf("012"), 5);
1415        assertEquals(-1, sb.lastIndexOf("02"), 0);
1416        assertEquals(8, sb.lastIndexOf("89"), 5);
1417
1418        try {
1419            sb.lastIndexOf(null, 0);
1420            fail("no NPE");
1421        } catch (NullPointerException e) {
1422            // Expected
1423        }
1424    }
1425
1426    /**
1427     * java.lang.StringBuilder.length()
1428     */
1429    public void test_length() {
1430        StringBuilder sb = new StringBuilder();
1431        assertEquals(0, sb.length());
1432        sb.append("0000");
1433        assertEquals(4, sb.length());
1434    }
1435
1436    /**
1437     * java.lang.StringBuilder.offsetByCodePoints(int, int)'
1438     */
1439    public void test_offsetByCodePointsII() {
1440        int result = new StringBuilder("a\uD800\uDC00b").offsetByCodePoints(0, 2);
1441        assertEquals(3, result);
1442
1443        result = new StringBuilder("abcd").offsetByCodePoints(3, -1);
1444        assertEquals(2, result);
1445
1446        result = new StringBuilder("a\uD800\uDC00b").offsetByCodePoints(0, 3);
1447        assertEquals(4, result);
1448
1449        result = new StringBuilder("a\uD800\uDC00b").offsetByCodePoints(3, -1);
1450        assertEquals(1, result);
1451
1452        result = new StringBuilder("a\uD800\uDC00b").offsetByCodePoints(3, 0);
1453        assertEquals(3, result);
1454
1455        result = new StringBuilder("\uD800\uDC00bc").offsetByCodePoints(3, 0);
1456        assertEquals(3, result);
1457
1458        result = new StringBuilder("a\uDC00bc").offsetByCodePoints(3, -1);
1459        assertEquals(2, result);
1460
1461        result = new StringBuilder("a\uD800bc").offsetByCodePoints(3, -1);
1462        assertEquals(2, result);
1463
1464        StringBuilder sb = new StringBuilder();
1465        sb.append("abc");
1466        try {
1467            sb.offsetByCodePoints(-1, 1);
1468            fail("No IOOBE for negative index.");
1469        } catch (IndexOutOfBoundsException e) {
1470
1471        }
1472
1473        try {
1474            sb.offsetByCodePoints(0, 4);
1475            fail("No IOOBE for offset that's too large.");
1476        } catch (IndexOutOfBoundsException e) {
1477
1478        }
1479
1480        try {
1481            sb.offsetByCodePoints(3, -4);
1482            fail("No IOOBE for offset that's too small.");
1483        } catch (IndexOutOfBoundsException e) {
1484
1485        }
1486
1487        try {
1488            sb.offsetByCodePoints(3, 1);
1489            fail("No IOOBE for index that's too large.");
1490        } catch (IndexOutOfBoundsException e) {
1491
1492        }
1493
1494        try {
1495            sb.offsetByCodePoints(4, -1);
1496            fail("No IOOBE for index that's too large.");
1497        } catch (IndexOutOfBoundsException e) {
1498
1499        }
1500    }
1501
1502    /**
1503     * java.lang.StringBuilder.replace(int, int, String)'
1504     */
1505    public void test_replaceIILjava_lang_String() {
1506        final String fixture = "0000";
1507        StringBuilder sb = new StringBuilder(fixture);
1508        assertSame(sb, sb.replace(1, 3, "11"));
1509        assertEquals("0110", sb.toString());
1510        assertEquals(4, sb.length());
1511
1512        sb = new StringBuilder(fixture);
1513        assertSame(sb, sb.replace(1, 2, "11"));
1514        assertEquals("01100", sb.toString());
1515        assertEquals(5, sb.length());
1516
1517        sb = new StringBuilder(fixture);
1518        assertSame(sb, sb.replace(4, 5, "11"));
1519        assertEquals("000011", sb.toString());
1520        assertEquals(6, sb.length());
1521
1522        sb = new StringBuilder(fixture);
1523        assertSame(sb, sb.replace(4, 6, "11"));
1524        assertEquals("000011", sb.toString());
1525        assertEquals(6, sb.length());
1526
1527        // FIXME Undocumented NPE in Sun's JRE 5.0_5
1528        try {
1529            sb.replace(1, 2, null);
1530            fail("No NPE");
1531        } catch (NullPointerException e) {
1532            // Expected
1533        }
1534
1535        try {
1536            sb = new StringBuilder(fixture);
1537            sb.replace(-1, 2, "11");
1538            fail("No SIOOBE, negative start");
1539        } catch (StringIndexOutOfBoundsException e) {
1540            // Expected
1541        }
1542
1543        try {
1544            sb = new StringBuilder(fixture);
1545            sb.replace(5, 2, "11");
1546            fail("No SIOOBE, start > length");
1547        } catch (StringIndexOutOfBoundsException e) {
1548            // Expected
1549        }
1550
1551        try {
1552            sb = new StringBuilder(fixture);
1553            sb.replace(3, 2, "11");
1554            fail("No SIOOBE, start > end");
1555        } catch (StringIndexOutOfBoundsException e) {
1556            // Expected
1557        }
1558
1559        // Regression for HARMONY-348
1560        StringBuilder buffer = new StringBuilder("1234567");
1561        buffer.replace(2, 6, "XXX");
1562        assertEquals("12XXX7", buffer.toString());
1563    }
1564
1565    private void reverseTest(String org, String rev, String back) {
1566        // create non-shared StringBuilder
1567        StringBuilder sb = new StringBuilder(org);
1568        sb.reverse();
1569        String reversed = sb.toString();
1570        assertEquals(rev, reversed);
1571        // create non-shared StringBuilder
1572        sb = new StringBuilder(reversed);
1573        sb.reverse();
1574        reversed = sb.toString();
1575        assertEquals(back, reversed);
1576
1577        // test algorithm when StringBuilder is shared
1578        sb = new StringBuilder(org);
1579        String copy = sb.toString();
1580        assertEquals(org, copy);
1581        sb.reverse();
1582        reversed = sb.toString();
1583        assertEquals(rev, reversed);
1584        sb = new StringBuilder(reversed);
1585        copy = sb.toString();
1586        assertEquals(rev, copy);
1587        sb.reverse();
1588        reversed = sb.toString();
1589        assertEquals(back, reversed);
1590    }
1591
1592    /**
1593     * java.lang.StringBuilder.reverse()
1594     */
1595    public void test_reverse() {
1596        final String fixture = "0123456789";
1597        StringBuilder sb = new StringBuilder(fixture);
1598        assertSame(sb, sb.reverse());
1599        assertEquals("9876543210", sb.toString());
1600
1601        sb = new StringBuilder("012345678");
1602        assertSame(sb, sb.reverse());
1603        assertEquals("876543210", sb.toString());
1604
1605        sb.setLength(1);
1606        assertSame(sb, sb.reverse());
1607        assertEquals("8", sb.toString());
1608
1609        sb.setLength(0);
1610        assertSame(sb, sb.reverse());
1611        assertEquals("", sb.toString());
1612
1613        String str;
1614        str = "a";
1615        reverseTest(str, str, str);
1616
1617        str = "ab";
1618        reverseTest(str, "ba", str);
1619
1620        str = "abcdef";
1621        reverseTest(str, "fedcba", str);
1622
1623        str = "abcdefg";
1624        reverseTest(str, "gfedcba", str);
1625
1626        str = "\ud800\udc00";
1627        reverseTest(str, str, str);
1628
1629        str = "\udc00\ud800";
1630        reverseTest(str, "\ud800\udc00", "\ud800\udc00");
1631
1632        str = "a\ud800\udc00";
1633        reverseTest(str, "\ud800\udc00a", str);
1634
1635        str = "ab\ud800\udc00";
1636        reverseTest(str, "\ud800\udc00ba", str);
1637
1638        str = "abc\ud800\udc00";
1639        reverseTest(str, "\ud800\udc00cba", str);
1640
1641        str = "\ud800\udc00\udc01\ud801\ud802\udc02";
1642        reverseTest(str, "\ud802\udc02\ud801\udc01\ud800\udc00",
1643                "\ud800\udc00\ud801\udc01\ud802\udc02");
1644
1645        str = "\ud800\udc00\ud801\udc01\ud802\udc02";
1646        reverseTest(str, "\ud802\udc02\ud801\udc01\ud800\udc00", str);
1647
1648        str = "\ud800\udc00\udc01\ud801a";
1649        reverseTest(str, "a\ud801\udc01\ud800\udc00",
1650                "\ud800\udc00\ud801\udc01a");
1651
1652        str = "a\ud800\udc00\ud801\udc01";
1653        reverseTest(str, "\ud801\udc01\ud800\udc00a", str);
1654
1655        str = "\ud800\udc00\udc01\ud801ab";
1656        reverseTest(str, "ba\ud801\udc01\ud800\udc00",
1657                "\ud800\udc00\ud801\udc01ab");
1658
1659        str = "ab\ud800\udc00\ud801\udc01";
1660        reverseTest(str, "\ud801\udc01\ud800\udc00ba", str);
1661
1662        str = "\ud800\udc00\ud801\udc01";
1663        reverseTest(str, "\ud801\udc01\ud800\udc00", str);
1664
1665        str = "a\ud800\udc00z\ud801\udc01";
1666        reverseTest(str, "\ud801\udc01z\ud800\udc00a", str);
1667
1668        str = "a\ud800\udc00bz\ud801\udc01";
1669        reverseTest(str, "\ud801\udc01zb\ud800\udc00a", str);
1670
1671        str = "abc\ud802\udc02\ud801\udc01\ud800\udc00";
1672        reverseTest(str, "\ud800\udc00\ud801\udc01\ud802\udc02cba", str);
1673
1674        str = "abcd\ud802\udc02\ud801\udc01\ud800\udc00";
1675        reverseTest(str, "\ud800\udc00\ud801\udc01\ud802\udc02dcba", str);
1676    }
1677
1678    /**
1679     * java.lang.StringBuilder.setCharAt(int, char)
1680     */
1681    public void test_setCharAtIC() {
1682        final String fixture = "0000";
1683        StringBuilder sb = new StringBuilder(fixture);
1684        sb.setCharAt(0, 'A');
1685        assertEquals("A000", sb.toString());
1686        sb.setCharAt(1, 'B');
1687        assertEquals("AB00", sb.toString());
1688        sb.setCharAt(2, 'C');
1689        assertEquals("ABC0", sb.toString());
1690        sb.setCharAt(3, 'D');
1691        assertEquals("ABCD", sb.toString());
1692
1693        try {
1694            sb.setCharAt(-1, 'A');
1695            fail("No IOOBE, negative index");
1696        } catch (IndexOutOfBoundsException e) {
1697            // Expected
1698        }
1699
1700        try {
1701            sb.setCharAt(4, 'A');
1702            fail("No IOOBE, index == length");
1703        } catch (IndexOutOfBoundsException e) {
1704            // Expected
1705        }
1706
1707        try {
1708            sb.setCharAt(5, 'A');
1709            fail("No IOOBE, index > length");
1710        } catch (IndexOutOfBoundsException e) {
1711            // Expected
1712        }
1713    }
1714
1715    /**
1716     * java.lang.StringBuilder.setLength(int)'
1717     */
1718    public void test_setLengthI() {
1719        final String fixture = "0123456789";
1720        StringBuilder sb = new StringBuilder(fixture);
1721        sb.setLength(5);
1722        assertEquals(5, sb.length());
1723        assertEquals("01234", sb.toString());
1724        sb.setLength(6);
1725        assertEquals(6, sb.length());
1726        assertEquals("01234\0", sb.toString());
1727        sb.setLength(0);
1728        assertEquals(0, sb.length());
1729        assertEquals("", sb.toString());
1730
1731        try {
1732            sb.setLength(-1);
1733            fail("No IOOBE, negative length.");
1734        } catch (IndexOutOfBoundsException e) {
1735            // Expected
1736        }
1737
1738        sb = new StringBuilder("abcde");
1739        assertEquals("abcde", sb.toString());
1740        sb.setLength(1);
1741        sb.append('g');
1742        assertEquals("ag", sb.toString());
1743
1744        sb = new StringBuilder("abcde");
1745        sb.setLength(3);
1746        sb.append('g');
1747        assertEquals("abcg", sb.toString());
1748
1749        sb = new StringBuilder("abcde");
1750        sb.setLength(2);
1751        try {
1752            sb.charAt(3);
1753            fail("should throw IndexOutOfBoundsException");
1754        } catch (IndexOutOfBoundsException e) {
1755            // Expected
1756        }
1757
1758        sb = new StringBuilder();
1759        sb.append("abcdefg");
1760        sb.setLength(2);
1761        sb.setLength(5);
1762        for (int i = 2; i < 5; i++) {
1763            assertEquals(0, sb.charAt(i));
1764        }
1765
1766        sb = new StringBuilder();
1767        sb.append("abcdefg");
1768        sb.delete(2, 4);
1769        sb.setLength(7);
1770        assertEquals('a', sb.charAt(0));
1771        assertEquals('b', sb.charAt(1));
1772        assertEquals('e', sb.charAt(2));
1773        assertEquals('f', sb.charAt(3));
1774        assertEquals('g', sb.charAt(4));
1775        for (int i = 5; i < 7; i++) {
1776            assertEquals(0, sb.charAt(i));
1777        }
1778
1779        sb = new StringBuilder();
1780        sb.append("abcdefg");
1781        sb.replace(2, 5, "z");
1782        sb.setLength(7);
1783        for (int i = 5; i < 7; i++) {
1784            assertEquals(0, sb.charAt(i));
1785        }
1786    }
1787
1788    /**
1789     * java.lang.StringBuilder.subSequence(int, int)
1790     */
1791    public void test_subSequenceII() {
1792        final String fixture = "0123456789";
1793        StringBuilder sb = new StringBuilder(fixture);
1794        CharSequence ss = sb.subSequence(0, 5);
1795        assertEquals("01234", ss.toString());
1796
1797        ss = sb.subSequence(0, 0);
1798        assertEquals("", ss.toString());
1799
1800        try {
1801            sb.subSequence(-1, 1);
1802            fail("No IOOBE, negative start.");
1803        } catch (IndexOutOfBoundsException e) {
1804            // Expected
1805        }
1806
1807        try {
1808            sb.subSequence(0, -1);
1809            fail("No IOOBE, negative end.");
1810        } catch (IndexOutOfBoundsException e) {
1811            // Expected
1812        }
1813
1814        try {
1815            sb.subSequence(0, fixture.length() + 1);
1816            fail("No IOOBE, end > length.");
1817        } catch (IndexOutOfBoundsException e) {
1818            // Expected
1819        }
1820
1821        try {
1822            sb.subSequence(3, 2);
1823            fail("No IOOBE, start > end.");
1824        } catch (IndexOutOfBoundsException e) {
1825            // Expected
1826        }
1827    }
1828
1829    /**
1830     * java.lang.StringBuilder.substring(int)
1831     */
1832    public void test_substringI() {
1833        final String fixture = "0123456789";
1834        StringBuilder sb = new StringBuilder(fixture);
1835        String ss = sb.substring(0);
1836        assertEquals(fixture, ss);
1837
1838        ss = sb.substring(10);
1839        assertEquals("", ss);
1840
1841        try {
1842            sb.substring(-1);
1843            fail("No SIOOBE, negative start.");
1844        } catch (StringIndexOutOfBoundsException e) {
1845            // Expected
1846        }
1847
1848        try {
1849            sb.substring(0, -1);
1850            fail("No SIOOBE, negative end.");
1851        } catch (StringIndexOutOfBoundsException e) {
1852            // Expected
1853        }
1854
1855        try {
1856            sb.substring(fixture.length() + 1);
1857            fail("No SIOOBE, start > length.");
1858        } catch (StringIndexOutOfBoundsException e) {
1859            // Expected
1860        }
1861    }
1862
1863    /**
1864     * java.lang.StringBuilder.substring(int, int)
1865     */
1866    public void test_substringII() {
1867        final String fixture = "0123456789";
1868        StringBuilder sb = new StringBuilder(fixture);
1869        String ss = sb.substring(0, 5);
1870        assertEquals("01234", ss);
1871
1872        ss = sb.substring(0, 0);
1873        assertEquals("", ss);
1874
1875        try {
1876            sb.substring(-1, 1);
1877            fail("No SIOOBE, negative start.");
1878        } catch (StringIndexOutOfBoundsException e) {
1879            // Expected
1880        }
1881
1882        try {
1883            sb.substring(0, -1);
1884            fail("No SIOOBE, negative end.");
1885        } catch (StringIndexOutOfBoundsException e) {
1886            // Expected
1887        }
1888
1889        try {
1890            sb.substring(0, fixture.length() + 1);
1891            fail("No SIOOBE, end > length.");
1892        } catch (StringIndexOutOfBoundsException e) {
1893            // Expected
1894        }
1895
1896        try {
1897            sb.substring(3, 2);
1898            fail("No SIOOBE, start > end.");
1899        } catch (StringIndexOutOfBoundsException e) {
1900            // Expected
1901        }
1902    }
1903
1904    /**
1905     * java.lang.StringBuilder.toString()'
1906     */
1907    public void test_toString() throws Exception {
1908        final String fixture = "0123456789";
1909        StringBuilder sb = new StringBuilder(fixture);
1910        assertEquals(fixture, sb.toString());
1911
1912        sb.setLength(0);
1913        sb.append("abcde");
1914        assertEquals("abcde", sb.toString());
1915        sb.setLength(1000);
1916        byte[] bytes = sb.toString().getBytes("GB18030");
1917        for (int i = 5; i < bytes.length; i++) {
1918            assertEquals(0, bytes[i]);
1919        }
1920
1921        sb.setLength(5);
1922        sb.append("fghij");
1923        assertEquals("abcdefghij", sb.toString());
1924    }
1925
1926    /**
1927     * java.lang.StringBuilder.trimToSize()'
1928     */
1929    public void test_trimToSize() {
1930        final String fixture = "0123456789";
1931        StringBuilder sb = new StringBuilder(fixture);
1932        assertTrue(sb.capacity() > fixture.length());
1933        assertEquals(fixture.length(), sb.length());
1934        assertEquals(fixture, sb.toString());
1935        int prevCapacity = sb.capacity();
1936        sb.trimToSize();
1937        assertTrue(prevCapacity > sb.capacity());
1938        assertEquals(fixture.length(), sb.length());
1939        assertEquals(fixture, sb.toString());
1940    }
1941
1942    // comparator for StringBuilder objects
1943    /*
1944    private static final SerializableAssert STRING_BILDER_COMPARATOR = new SerializableAssert() {
1945        public void assertDeserialized(Serializable initial,
1946                Serializable deserialized) {
1947
1948            StringBuilder init = (StringBuilder) initial;
1949            StringBuilder desr = (StringBuilder) deserialized;
1950
1951            assertEquals("toString", init.toString(), desr.toString());
1952        }
1953    };
1954    */
1955
1956    /**
1957     * serialization/deserialization.
1958     */
1959    public void testSerializationSelf() throws Exception {
1960
1961        // SerializationTest.verifySelf(new StringBuilder("0123456789"),
1962        //        STRING_BILDER_COMPARATOR);
1963    }
1964
1965    /**
1966     * serialization/deserialization compatibility with RI.
1967     */
1968    public void testSerializationCompatibility() throws Exception {
1969
1970        //SerializationTest.verifyGolden(this, new StringBuilder("0123456789"),
1971        //        STRING_BILDER_COMPARATOR);
1972    }
1973
1974    private static final class Fixture {
1975        static final Fixture INSTANCE = new Fixture();
1976
1977        private Fixture() {
1978            super();
1979        }
1980
1981        @Override
1982        public String toString() {
1983            return "fixture";
1984        }
1985    }
1986}
1987