View Javadoc
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,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.text;
19  
20  import org.junit.Test;
21  import static org.junit.Assert.*;
22  import java.io.Reader;
23  import java.io.Writer;
24  import java.util.Arrays;
25  
26  import org.apache.commons.lang3.ArrayUtils;
27  
28  /**
29   * Unit tests for {@link org.apache.commons.lang3.text.StrBuilder}.
30   * 
31   * @version $Id: StrBuilderTest.java 1436770 2013-01-22 07:09:45Z ggregory $
32   */
33  public class StrBuilderTest {
34  
35      //-----------------------------------------------------------------------
36      @Test
37      public void testConstructors() {
38          final StrBuilder sb0 = new StrBuilder();
39          assertEquals(32, sb0.capacity());
40          assertEquals(0, sb0.length());
41          assertEquals(0, sb0.size());
42  
43          final StrBuilder sb1 = new StrBuilder(32);
44          assertEquals(32, sb1.capacity());
45          assertEquals(0, sb1.length());
46          assertEquals(0, sb1.size());
47  
48          final StrBuilder sb2 = new StrBuilder(0);
49          assertEquals(32, sb2.capacity());
50          assertEquals(0, sb2.length());
51          assertEquals(0, sb2.size());
52  
53          final StrBuilder sb3 = new StrBuilder(-1);
54          assertEquals(32, sb3.capacity());
55          assertEquals(0, sb3.length());
56          assertEquals(0, sb3.size());
57  
58          final StrBuilder sb4 = new StrBuilder(1);
59          assertEquals(1, sb4.capacity());
60          assertEquals(0, sb4.length());
61          assertEquals(0, sb4.size());
62  
63          final StrBuilder sb5 = new StrBuilder((String) null);
64          assertEquals(32, sb5.capacity());
65          assertEquals(0, sb5.length());
66          assertEquals(0, sb5.size());
67  
68          final StrBuilder sb6 = new StrBuilder("");
69          assertEquals(32, sb6.capacity());
70          assertEquals(0, sb6.length());
71          assertEquals(0, sb6.size());
72  
73          final StrBuilder sb7 = new StrBuilder("foo");
74          assertEquals(35, sb7.capacity());
75          assertEquals(3, sb7.length());
76          assertEquals(3, sb7.size());
77      }
78  
79      //-----------------------------------------------------------------------
80      @Test
81      public void testChaining() {
82          final StrBuilder sb = new StrBuilder();
83          assertSame(sb, sb.setNewLineText(null));
84          assertSame(sb, sb.setNullText(null));
85          assertSame(sb, sb.setLength(1));
86          assertSame(sb, sb.setCharAt(0, 'a'));
87          assertSame(sb, sb.ensureCapacity(0));
88          assertSame(sb, sb.minimizeCapacity());
89          assertSame(sb, sb.clear());
90          assertSame(sb, sb.reverse());
91          assertSame(sb, sb.trim());
92      }
93  
94      //-----------------------------------------------------------------------
95      @Test
96      public void testGetSetNewLineText() {
97          final StrBuilder sb = new StrBuilder();
98          assertEquals(null, sb.getNewLineText());
99  
100         sb.setNewLineText("#");
101         assertEquals("#", sb.getNewLineText());
102 
103         sb.setNewLineText("");
104         assertEquals("", sb.getNewLineText());
105 
106         sb.setNewLineText((String) null);
107         assertEquals(null, sb.getNewLineText());
108     }
109 
110     //-----------------------------------------------------------------------
111     @Test
112     public void testGetSetNullText() {
113         final StrBuilder sb = new StrBuilder();
114         assertEquals(null, sb.getNullText());
115 
116         sb.setNullText("null");
117         assertEquals("null", sb.getNullText());
118 
119         sb.setNullText("");
120         assertEquals(null, sb.getNullText());
121 
122         sb.setNullText("NULL");
123         assertEquals("NULL", sb.getNullText());
124 
125         sb.setNullText((String) null);
126         assertEquals(null, sb.getNullText());
127     }
128 
129     //-----------------------------------------------------------------------
130     @Test
131     public void testCapacityAndLength() {
132         final StrBuilder sb = new StrBuilder();
133         assertEquals(32, sb.capacity());
134         assertEquals(0, sb.length());
135         assertEquals(0, sb.size());
136         assertTrue(sb.isEmpty());
137 
138         sb.minimizeCapacity();
139         assertEquals(0, sb.capacity());
140         assertEquals(0, sb.length());
141         assertEquals(0, sb.size());
142         assertTrue(sb.isEmpty());
143 
144         sb.ensureCapacity(32);
145         assertTrue(sb.capacity() >= 32);
146         assertEquals(0, sb.length());
147         assertEquals(0, sb.size());
148         assertTrue(sb.isEmpty());
149 
150         sb.append("foo");
151         assertTrue(sb.capacity() >= 32);
152         assertEquals(3, sb.length());
153         assertEquals(3, sb.size());
154         assertTrue(sb.isEmpty() == false);
155 
156         sb.clear();
157         assertTrue(sb.capacity() >= 32);
158         assertEquals(0, sb.length());
159         assertEquals(0, sb.size());
160         assertTrue(sb.isEmpty());
161 
162         sb.append("123456789012345678901234567890123");
163         assertTrue(sb.capacity() > 32);
164         assertEquals(33, sb.length());
165         assertEquals(33, sb.size());
166         assertTrue(sb.isEmpty() == false);
167 
168         sb.ensureCapacity(16);
169         assertTrue(sb.capacity() > 16);
170         assertEquals(33, sb.length());
171         assertEquals(33, sb.size());
172         assertTrue(sb.isEmpty() == false);
173 
174         sb.minimizeCapacity();
175         assertEquals(33, sb.capacity());
176         assertEquals(33, sb.length());
177         assertEquals(33, sb.size());
178         assertTrue(sb.isEmpty() == false);
179 
180         try {
181             sb.setLength(-1);
182             fail("setLength(-1) expected StringIndexOutOfBoundsException");
183         } catch (final IndexOutOfBoundsException e) {
184             // expected
185         }
186 
187         sb.setLength(33);
188         assertEquals(33, sb.capacity());
189         assertEquals(33, sb.length());
190         assertEquals(33, sb.size());
191         assertTrue(sb.isEmpty() == false);
192 
193         sb.setLength(16);
194         assertTrue(sb.capacity() >= 16);
195         assertEquals(16, sb.length());
196         assertEquals(16, sb.size());
197         assertEquals("1234567890123456", sb.toString());
198         assertTrue(sb.isEmpty() == false);
199 
200         sb.setLength(32);
201         assertTrue(sb.capacity() >= 32);
202         assertEquals(32, sb.length());
203         assertEquals(32, sb.size());
204         assertEquals("1234567890123456\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", sb.toString());
205         assertTrue(sb.isEmpty() == false);
206 
207         sb.setLength(0);
208         assertTrue(sb.capacity() >= 32);
209         assertEquals(0, sb.length());
210         assertEquals(0, sb.size());
211         assertTrue(sb.isEmpty());
212     }
213 
214     //-----------------------------------------------------------------------
215     @Test
216     public void testLength() {
217         final StrBuilder sb = new StrBuilder();
218         assertEquals(0, sb.length());
219         
220         sb.append("Hello");
221         assertEquals(5, sb.length());
222     }
223 
224     @Test
225     public void testSetLength() {
226         final StrBuilder sb = new StrBuilder();
227         sb.append("Hello");
228         sb.setLength(2);  // shorten
229         assertEquals("He", sb.toString());
230         sb.setLength(2);  // no change
231         assertEquals("He", sb.toString());
232         sb.setLength(3);  // lengthen
233         assertEquals("He\0", sb.toString());
234 
235         try {
236             sb.setLength(-1);
237             fail("setLength(-1) expected StringIndexOutOfBoundsException");
238         } catch (final IndexOutOfBoundsException e) {
239             // expected
240         }
241     }
242 
243     //-----------------------------------------------------------------------
244     @Test
245     public void testCapacity() {
246         final StrBuilder sb = new StrBuilder();
247         assertEquals(sb.buffer.length, sb.capacity());
248         
249         sb.append("HelloWorldHelloWorldHelloWorldHelloWorld");
250         assertEquals(sb.buffer.length, sb.capacity());
251     }
252 
253     @Test
254     public void testEnsureCapacity() {
255         final StrBuilder sb = new StrBuilder();
256         sb.ensureCapacity(2);
257         assertTrue(sb.capacity() >= 2);
258         
259         sb.ensureCapacity(-1);
260         assertTrue(sb.capacity() >= 0);
261         
262         sb.append("HelloWorld");
263         sb.ensureCapacity(40);
264         assertTrue(sb.capacity() >= 40);
265     }
266 
267     @Test
268     public void testMinimizeCapacity() {
269         final StrBuilder sb = new StrBuilder();
270         sb.minimizeCapacity();
271         assertEquals(0, sb.capacity());
272         
273         sb.append("HelloWorld");
274         sb.minimizeCapacity();
275         assertEquals(10, sb.capacity());
276     }
277 
278     //-----------------------------------------------------------------------
279     @Test
280     public void testSize() {
281         final StrBuilder sb = new StrBuilder();
282         assertEquals(0, sb.size());
283         
284         sb.append("Hello");
285         assertEquals(5, sb.size());
286     }
287 
288     @Test
289     public void testIsEmpty() {
290         final StrBuilder sb = new StrBuilder();
291         assertTrue(sb.isEmpty());
292         
293         sb.append("Hello");
294         assertFalse(sb.isEmpty());
295         
296         sb.clear();
297         assertTrue(sb.isEmpty());
298     }
299 
300     @Test
301     public void testClear() {
302         final StrBuilder sb = new StrBuilder();
303         sb.append("Hello");
304         sb.clear();
305         assertEquals(0, sb.length());
306         assertTrue(sb.buffer.length >= 5);
307     }
308 
309     //-----------------------------------------------------------------------
310     @Test
311     public void testCharAt() {
312         final StrBuilder sb = new StrBuilder();
313         try {
314             sb.charAt(0);
315             fail("charAt(0) expected IndexOutOfBoundsException");
316         } catch (final IndexOutOfBoundsException e) {
317             // expected
318         }
319         try {
320             sb.charAt(-1);
321             fail("charAt(-1) expected IndexOutOfBoundsException");
322         } catch (final IndexOutOfBoundsException e) {
323             // expected
324         }
325         sb.append("foo");
326         assertEquals('f', sb.charAt(0));
327         assertEquals('o', sb.charAt(1));
328         assertEquals('o', sb.charAt(2));
329         try {
330             sb.charAt(-1);
331             fail("charAt(-1) expected IndexOutOfBoundsException");
332         } catch (final IndexOutOfBoundsException e) {
333             // expected
334         }
335         try {
336             sb.charAt(3);
337             fail("charAt(3) expected IndexOutOfBoundsException");
338         } catch (final IndexOutOfBoundsException e) {
339             // expected
340         }
341     }
342 
343     //-----------------------------------------------------------------------
344     @Test
345     public void testSetCharAt() {
346         final StrBuilder sb = new StrBuilder();
347         try {
348             sb.setCharAt(0, 'f');
349             fail("setCharAt(0,) expected IndexOutOfBoundsException");
350         } catch (final IndexOutOfBoundsException e) {
351             // expected
352         }
353         try {
354             sb.setCharAt(-1, 'f');
355             fail("setCharAt(-1,) expected IndexOutOfBoundsException");
356         } catch (final IndexOutOfBoundsException e) {
357             // expected
358         }
359         sb.append("foo");
360         sb.setCharAt(0, 'b');
361         sb.setCharAt(1, 'a');
362         sb.setCharAt(2, 'r');
363         try {
364             sb.setCharAt(3, '!');
365             fail("setCharAt(3,) expected IndexOutOfBoundsException");
366         } catch (final IndexOutOfBoundsException e) {
367             // expected
368         }
369         assertEquals("bar", sb.toString());
370     }
371 
372     //-----------------------------------------------------------------------
373     @Test
374     public void testDeleteCharAt() {
375         final StrBuilder sb = new StrBuilder("abc");
376         sb.deleteCharAt(0);
377         assertEquals("bc", sb.toString()); 
378         
379         try {
380             sb.deleteCharAt(1000);
381             fail("Expected IndexOutOfBoundsException");
382         } catch (final IndexOutOfBoundsException e) {}
383     }
384 
385     //-----------------------------------------------------------------------
386     @Test
387     public void testToCharArray() {
388         final StrBuilder sb = new StrBuilder();
389         assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, sb.toCharArray());
390 
391         char[] a = sb.toCharArray();
392         assertNotNull("toCharArray() result is null", a);
393         assertEquals("toCharArray() result is too large", 0, a.length);
394 
395         sb.append("junit");
396         a = sb.toCharArray();
397         assertEquals("toCharArray() result incorrect length", 5, a.length);
398         assertTrue("toCharArray() result does not match", Arrays.equals("junit".toCharArray(), a));
399     }
400 
401     @Test
402     public void testToCharArrayIntInt() {
403         final StrBuilder sb = new StrBuilder();
404         assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, sb.toCharArray(0, 0));
405 
406         sb.append("junit");
407         char[] a = sb.toCharArray(0, 20); // too large test
408         assertEquals("toCharArray(int,int) result incorrect length", 5, a.length);
409         assertTrue("toCharArray(int,int) result does not match", Arrays.equals("junit".toCharArray(), a));
410 
411         a = sb.toCharArray(0, 4);
412         assertEquals("toCharArray(int,int) result incorrect length", 4, a.length);
413         assertTrue("toCharArray(int,int) result does not match", Arrays.equals("juni".toCharArray(), a));
414 
415         a = sb.toCharArray(0, 4);
416         assertEquals("toCharArray(int,int) result incorrect length", 4, a.length);
417         assertTrue("toCharArray(int,int) result does not match", Arrays.equals("juni".toCharArray(), a));
418 
419         a = sb.toCharArray(0, 1);
420         assertNotNull("toCharArray(int,int) result is null", a);
421 
422         try {
423             sb.toCharArray(-1, 5);
424             fail("no string index out of bound on -1");
425         } catch (final IndexOutOfBoundsException e) {
426         }
427 
428         try {
429             sb.toCharArray(6, 5);
430             fail("no string index out of bound on -1");
431         } catch (final IndexOutOfBoundsException e) {
432         }
433     }
434 
435     @Test
436     public void testGetChars ( ) {
437         final StrBuilder sb = new StrBuilder();
438         
439         char[] input = new char[10];
440         char[] a = sb.getChars(input);
441         assertSame (input, a);
442         assertTrue(Arrays.equals(new char[10], a));
443         
444         sb.append("junit");
445         a = sb.getChars(input);
446         assertSame(input, a);
447         assertTrue(Arrays.equals(new char[] {'j','u','n','i','t',0,0,0,0,0},a));
448         
449         a = sb.getChars(null);
450         assertNotSame(input,a);
451         assertEquals(5,a.length);
452         assertTrue(Arrays.equals("junit".toCharArray(),a));
453         
454         input = new char[5];
455         a = sb.getChars(input);
456         assertSame(input, a);
457         
458         input = new char[4];
459         a = sb.getChars(input);
460         assertNotSame(input, a);
461     }
462 
463     @Test
464     public void testGetCharsIntIntCharArrayInt( ) {
465         final StrBuilder sb = new StrBuilder();
466                
467         sb.append("junit");
468         char[] a = new char[5];
469         sb.getChars(0,5,a,0);
470         assertTrue(Arrays.equals(new char[] {'j','u','n','i','t'},a));
471         
472         a = new char[5];
473         sb.getChars(0,2,a,3);
474         assertTrue(Arrays.equals(new char[] {0,0,0,'j','u'},a));
475         
476         try {
477             sb.getChars(-1,0,a,0);
478             fail("no exception");
479         }
480         catch (final IndexOutOfBoundsException e) {
481         }
482         
483         try {
484             sb.getChars(0,-1,a,0);
485             fail("no exception");
486         }
487         catch (final IndexOutOfBoundsException e) {
488         }
489         
490         try {
491             sb.getChars(0,20,a,0);
492             fail("no exception");
493         }
494         catch (final IndexOutOfBoundsException e) {
495         }
496         
497         try {
498             sb.getChars(4,2,a,0);
499             fail("no exception");
500         }
501         catch (final IndexOutOfBoundsException e) {
502         }
503     }
504 
505     //-----------------------------------------------------------------------
506     @Test
507     public void testDeleteIntInt() {
508         StrBuilder sb = new StrBuilder("abc");
509         sb.delete(0, 1);
510         assertEquals("bc", sb.toString()); 
511         sb.delete(1, 2);
512         assertEquals("b", sb.toString());
513         sb.delete(0, 1);
514         assertEquals("", sb.toString()); 
515         sb.delete(0, 1000);
516         assertEquals("", sb.toString()); 
517         
518         try {
519             sb.delete(1, 2);
520             fail("Expected IndexOutOfBoundsException");
521         } catch (final IndexOutOfBoundsException e) {}
522         try {
523             sb.delete(-1, 1);
524             fail("Expected IndexOutOfBoundsException");
525         } catch (final IndexOutOfBoundsException e) {}
526         
527         sb = new StrBuilder("anything");
528         try {
529             sb.delete(2, 1);
530             fail("Expected IndexOutOfBoundsException");
531         } catch (final IndexOutOfBoundsException e) {}
532     }
533 
534     //-----------------------------------------------------------------------
535     @Test
536     public void testDeleteAll_char() {
537         StrBuilder sb = new StrBuilder("abcbccba");
538         sb.deleteAll('X');
539         assertEquals("abcbccba", sb.toString());
540         sb.deleteAll('a');
541         assertEquals("bcbccb", sb.toString());
542         sb.deleteAll('c');
543         assertEquals("bbb", sb.toString());
544         sb.deleteAll('b');
545         assertEquals("", sb.toString());
546 
547         sb = new StrBuilder("");
548         sb.deleteAll('b');
549         assertEquals("", sb.toString());
550     }
551 
552     @Test
553     public void testDeleteFirst_char() {
554         StrBuilder sb = new StrBuilder("abcba");
555         sb.deleteFirst('X');
556         assertEquals("abcba", sb.toString());
557         sb.deleteFirst('a');
558         assertEquals("bcba", sb.toString());
559         sb.deleteFirst('c');
560         assertEquals("bba", sb.toString());
561         sb.deleteFirst('b');
562         assertEquals("ba", sb.toString());
563 
564         sb = new StrBuilder("");
565         sb.deleteFirst('b');
566         assertEquals("", sb.toString());
567     }
568 
569     // -----------------------------------------------------------------------
570     @Test
571     public void testDeleteAll_String() {
572         StrBuilder sb = new StrBuilder("abcbccba");
573         sb.deleteAll((String) null);
574         assertEquals("abcbccba", sb.toString());
575         sb.deleteAll("");
576         assertEquals("abcbccba", sb.toString());
577         
578         sb.deleteAll("X");
579         assertEquals("abcbccba", sb.toString());
580         sb.deleteAll("a");
581         assertEquals("bcbccb", sb.toString());
582         sb.deleteAll("c");
583         assertEquals("bbb", sb.toString());
584         sb.deleteAll("b");
585         assertEquals("", sb.toString());
586 
587         sb = new StrBuilder("abcbccba");
588         sb.deleteAll("bc");
589         assertEquals("acba", sb.toString());
590 
591         sb = new StrBuilder("");
592         sb.deleteAll("bc");
593         assertEquals("", sb.toString());
594     }
595 
596     @Test
597     public void testDeleteFirst_String() {
598         StrBuilder sb = new StrBuilder("abcbccba");
599         sb.deleteFirst((String) null);
600         assertEquals("abcbccba", sb.toString());
601         sb.deleteFirst("");
602         assertEquals("abcbccba", sb.toString());
603 
604         sb.deleteFirst("X");
605         assertEquals("abcbccba", sb.toString());
606         sb.deleteFirst("a");
607         assertEquals("bcbccba", sb.toString());
608         sb.deleteFirst("c");
609         assertEquals("bbccba", sb.toString());
610         sb.deleteFirst("b");
611         assertEquals("bccba", sb.toString());
612 
613         sb = new StrBuilder("abcbccba");
614         sb.deleteFirst("bc");
615         assertEquals("abccba", sb.toString());
616 
617         sb = new StrBuilder("");
618         sb.deleteFirst("bc");
619         assertEquals("", sb.toString());
620     }
621 
622     // -----------------------------------------------------------------------
623     @Test
624     public void testDeleteAll_StrMatcher() {
625         StrBuilder sb = new StrBuilder("A0xA1A2yA3");
626         sb.deleteAll((StrMatcher) null);
627         assertEquals("A0xA1A2yA3", sb.toString());
628         sb.deleteAll(A_NUMBER_MATCHER);
629         assertEquals("xy", sb.toString());
630 
631         sb = new StrBuilder("Ax1");
632         sb.deleteAll(A_NUMBER_MATCHER);
633         assertEquals("Ax1", sb.toString());
634 
635         sb = new StrBuilder("");
636         sb.deleteAll(A_NUMBER_MATCHER);
637         assertEquals("", sb.toString());
638     }
639 
640     @Test
641     public void testDeleteFirst_StrMatcher() {
642         StrBuilder sb = new StrBuilder("A0xA1A2yA3");
643         sb.deleteFirst((StrMatcher) null);
644         assertEquals("A0xA1A2yA3", sb.toString());
645         sb.deleteFirst(A_NUMBER_MATCHER);
646         assertEquals("xA1A2yA3", sb.toString());
647 
648         sb = new StrBuilder("Ax1");
649         sb.deleteFirst(A_NUMBER_MATCHER);
650         assertEquals("Ax1", sb.toString());
651 
652         sb = new StrBuilder("");
653         sb.deleteFirst(A_NUMBER_MATCHER);
654         assertEquals("", sb.toString());
655     }
656 
657     // -----------------------------------------------------------------------
658     @Test
659     public void testReplace_int_int_String() {
660         StrBuilder sb = new StrBuilder("abc");
661         sb.replace(0, 1, "d");
662         assertEquals("dbc", sb.toString());
663         sb.replace(0, 1, "aaa");
664         assertEquals("aaabc", sb.toString());
665         sb.replace(0, 3, "");
666         assertEquals("bc", sb.toString());
667         sb.replace(1, 2, (String) null);
668         assertEquals("b", sb.toString());
669         sb.replace(1, 1000, "text");
670         assertEquals("btext", sb.toString());
671         sb.replace(0, 1000, "text");
672         assertEquals("text", sb.toString());
673         
674         sb = new StrBuilder("atext");
675         sb.replace(1, 1, "ny");
676         assertEquals("anytext", sb.toString());
677         try {
678             sb.replace(2, 1, "anything");
679             fail("Expected IndexOutOfBoundsException");
680         } catch (final IndexOutOfBoundsException e) {}
681         
682         sb = new StrBuilder();
683         try {
684             sb.replace(1, 2, "anything");
685             fail("Expected IndexOutOfBoundsException");
686         } catch (final IndexOutOfBoundsException e) {}
687         try {
688             sb.replace(-1, 1, "anything");
689             fail("Expected IndexOutOfBoundsException");
690         } catch (final IndexOutOfBoundsException e) {}
691     }
692 
693     //-----------------------------------------------------------------------
694     @Test
695     public void testReplaceAll_char_char() {
696         final StrBuilder sb = new StrBuilder("abcbccba");
697         sb.replaceAll('x', 'y');
698         assertEquals("abcbccba", sb.toString());
699         sb.replaceAll('a', 'd');
700         assertEquals("dbcbccbd", sb.toString());
701         sb.replaceAll('b', 'e');
702         assertEquals("dececced", sb.toString());
703         sb.replaceAll('c', 'f');
704         assertEquals("defeffed", sb.toString());
705         sb.replaceAll('d', 'd');
706         assertEquals("defeffed", sb.toString());
707     }
708 
709     //-----------------------------------------------------------------------
710     @Test
711     public void testReplaceFirst_char_char() {
712         final StrBuilder sb = new StrBuilder("abcbccba");
713         sb.replaceFirst('x', 'y');
714         assertEquals("abcbccba", sb.toString());
715         sb.replaceFirst('a', 'd');
716         assertEquals("dbcbccba", sb.toString());
717         sb.replaceFirst('b', 'e');
718         assertEquals("decbccba", sb.toString());
719         sb.replaceFirst('c', 'f');
720         assertEquals("defbccba", sb.toString());
721         sb.replaceFirst('d', 'd');
722         assertEquals("defbccba", sb.toString());
723     }
724 
725     //-----------------------------------------------------------------------
726     @Test
727     public void testReplaceAll_String_String() {
728         StrBuilder sb = new StrBuilder("abcbccba");
729         sb.replaceAll((String) null, null);
730         assertEquals("abcbccba", sb.toString());
731         sb.replaceAll((String) null, "anything");
732         assertEquals("abcbccba", sb.toString());
733         sb.replaceAll("", null);
734         assertEquals("abcbccba", sb.toString());
735         sb.replaceAll("", "anything");
736         assertEquals("abcbccba", sb.toString());
737         
738         sb.replaceAll("x", "y");
739         assertEquals("abcbccba", sb.toString());
740         sb.replaceAll("a", "d");
741         assertEquals("dbcbccbd", sb.toString());
742         sb.replaceAll("d", null);
743         assertEquals("bcbccb", sb.toString());
744         sb.replaceAll("cb", "-");
745         assertEquals("b-c-", sb.toString());
746         
747         sb = new StrBuilder("abcba");
748         sb.replaceAll("b", "xbx");
749         assertEquals("axbxcxbxa", sb.toString());
750         
751         sb = new StrBuilder("bb");
752         sb.replaceAll("b", "xbx");
753         assertEquals("xbxxbx", sb.toString());
754     }
755 
756     @Test
757     public void testReplaceFirst_String_String() {
758         StrBuilder sb = new StrBuilder("abcbccba");
759         sb.replaceFirst((String) null, null);
760         assertEquals("abcbccba", sb.toString());
761         sb.replaceFirst((String) null, "anything");
762         assertEquals("abcbccba", sb.toString());
763         sb.replaceFirst("", null);
764         assertEquals("abcbccba", sb.toString());
765         sb.replaceFirst("", "anything");
766         assertEquals("abcbccba", sb.toString());
767         
768         sb.replaceFirst("x", "y");
769         assertEquals("abcbccba", sb.toString());
770         sb.replaceFirst("a", "d");
771         assertEquals("dbcbccba", sb.toString());
772         sb.replaceFirst("d", null);
773         assertEquals("bcbccba", sb.toString());
774         sb.replaceFirst("cb", "-");
775         assertEquals("b-ccba", sb.toString());
776         
777         sb = new StrBuilder("abcba");
778         sb.replaceFirst("b", "xbx");
779         assertEquals("axbxcba", sb.toString());
780         
781         sb = new StrBuilder("bb");
782         sb.replaceFirst("b", "xbx");
783         assertEquals("xbxb", sb.toString());
784     }
785 
786     //-----------------------------------------------------------------------
787     @Test
788     public void testReplaceAll_StrMatcher_String() {
789         StrBuilder sb = new StrBuilder("abcbccba");
790         sb.replaceAll((StrMatcher) null, null);
791         assertEquals("abcbccba", sb.toString());
792         sb.replaceAll((StrMatcher) null, "anything");
793         assertEquals("abcbccba", sb.toString());
794         sb.replaceAll(StrMatcher.noneMatcher(), null);
795         assertEquals("abcbccba", sb.toString());
796         sb.replaceAll(StrMatcher.noneMatcher(), "anything");
797         assertEquals("abcbccba", sb.toString());
798         
799         sb.replaceAll(StrMatcher.charMatcher('x'), "y");
800         assertEquals("abcbccba", sb.toString());
801         sb.replaceAll(StrMatcher.charMatcher('a'), "d");
802         assertEquals("dbcbccbd", sb.toString());
803         sb.replaceAll(StrMatcher.charMatcher('d'), null);
804         assertEquals("bcbccb", sb.toString());
805         sb.replaceAll(StrMatcher.stringMatcher("cb"), "-");
806         assertEquals("b-c-", sb.toString());
807         
808         sb = new StrBuilder("abcba");
809         sb.replaceAll(StrMatcher.charMatcher('b'), "xbx");
810         assertEquals("axbxcxbxa", sb.toString());
811         
812         sb = new StrBuilder("bb");
813         sb.replaceAll(StrMatcher.charMatcher('b'), "xbx");
814         assertEquals("xbxxbx", sb.toString());
815         
816         sb = new StrBuilder("A1-A2A3-A4");
817         sb.replaceAll(A_NUMBER_MATCHER, "***");
818         assertEquals("***-******-***", sb.toString());
819     }
820 
821     @Test
822     public void testReplaceFirst_StrMatcher_String() {
823         StrBuilder sb = new StrBuilder("abcbccba");
824         sb.replaceFirst((StrMatcher) null, null);
825         assertEquals("abcbccba", sb.toString());
826         sb.replaceFirst((StrMatcher) null, "anything");
827         assertEquals("abcbccba", sb.toString());
828         sb.replaceFirst(StrMatcher.noneMatcher(), null);
829         assertEquals("abcbccba", sb.toString());
830         sb.replaceFirst(StrMatcher.noneMatcher(), "anything");
831         assertEquals("abcbccba", sb.toString());
832         
833         sb.replaceFirst(StrMatcher.charMatcher('x'), "y");
834         assertEquals("abcbccba", sb.toString());
835         sb.replaceFirst(StrMatcher.charMatcher('a'), "d");
836         assertEquals("dbcbccba", sb.toString());
837         sb.replaceFirst(StrMatcher.charMatcher('d'), null);
838         assertEquals("bcbccba", sb.toString());
839         sb.replaceFirst(StrMatcher.stringMatcher("cb"), "-");
840         assertEquals("b-ccba", sb.toString());
841         
842         sb = new StrBuilder("abcba");
843         sb.replaceFirst(StrMatcher.charMatcher('b'), "xbx");
844         assertEquals("axbxcba", sb.toString());
845         
846         sb = new StrBuilder("bb");
847         sb.replaceFirst(StrMatcher.charMatcher('b'), "xbx");
848         assertEquals("xbxb", sb.toString());
849         
850         sb = new StrBuilder("A1-A2A3-A4");
851         sb.replaceFirst(A_NUMBER_MATCHER, "***");
852         assertEquals("***-A2A3-A4", sb.toString());
853     }
854 
855     //-----------------------------------------------------------------------
856     @Test
857     public void testReplace_StrMatcher_String_int_int_int_VaryMatcher() {
858         StrBuilder sb = new StrBuilder("abcbccba");
859         sb.replace((StrMatcher) null, "x", 0, sb.length(), -1);
860         assertEquals("abcbccba", sb.toString());
861         
862         sb.replace(StrMatcher.charMatcher('a'), "x", 0, sb.length(), -1);
863         assertEquals("xbcbccbx", sb.toString());
864         
865         sb.replace(StrMatcher.stringMatcher("cb"), "x", 0, sb.length(), -1);
866         assertEquals("xbxcxx", sb.toString());
867         
868         sb = new StrBuilder("A1-A2A3-A4");
869         sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1);
870         assertEquals("***-******-***", sb.toString());
871         
872         sb = new StrBuilder();
873         sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1);
874         assertEquals("", sb.toString());
875     }
876 
877     @Test
878     public void testReplace_StrMatcher_String_int_int_int_VaryReplace() {
879         StrBuilder sb = new StrBuilder("abcbccba");
880         sb.replace(StrMatcher.stringMatcher("cb"), "cb", 0, sb.length(), -1);
881         assertEquals("abcbccba", sb.toString());
882         
883         sb = new StrBuilder("abcbccba");
884         sb.replace(StrMatcher.stringMatcher("cb"), "-", 0, sb.length(), -1);
885         assertEquals("ab-c-a", sb.toString());
886         
887         sb = new StrBuilder("abcbccba");
888         sb.replace(StrMatcher.stringMatcher("cb"), "+++", 0, sb.length(), -1);
889         assertEquals("ab+++c+++a", sb.toString());
890         
891         sb = new StrBuilder("abcbccba");
892         sb.replace(StrMatcher.stringMatcher("cb"), "", 0, sb.length(), -1);
893         assertEquals("abca", sb.toString());
894         
895         sb = new StrBuilder("abcbccba");
896         sb.replace(StrMatcher.stringMatcher("cb"), null, 0, sb.length(), -1);
897         assertEquals("abca", sb.toString());
898     }
899 
900     @Test
901     public void testReplace_StrMatcher_String_int_int_int_VaryStartIndex() {
902         StrBuilder sb = new StrBuilder("aaxaaaayaa");
903         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, sb.length(), -1);
904         assertEquals("-x--y-", sb.toString());
905         
906         sb = new StrBuilder("aaxaaaayaa");
907         sb.replace(StrMatcher.stringMatcher("aa"), "-", 1, sb.length(), -1);
908         assertEquals("aax--y-", sb.toString());
909         
910         sb = new StrBuilder("aaxaaaayaa");
911         sb.replace(StrMatcher.stringMatcher("aa"), "-", 2, sb.length(), -1);
912         assertEquals("aax--y-", sb.toString());
913         
914         sb = new StrBuilder("aaxaaaayaa");
915         sb.replace(StrMatcher.stringMatcher("aa"), "-", 3, sb.length(), -1);
916         assertEquals("aax--y-", sb.toString());
917         
918         sb = new StrBuilder("aaxaaaayaa");
919         sb.replace(StrMatcher.stringMatcher("aa"), "-", 4, sb.length(), -1);
920         assertEquals("aaxa-ay-", sb.toString());
921         
922         sb = new StrBuilder("aaxaaaayaa");
923         sb.replace(StrMatcher.stringMatcher("aa"), "-", 5, sb.length(), -1);
924         assertEquals("aaxaa-y-", sb.toString());
925         
926         sb = new StrBuilder("aaxaaaayaa");
927         sb.replace(StrMatcher.stringMatcher("aa"), "-", 6, sb.length(), -1);
928         assertEquals("aaxaaaay-", sb.toString());
929         
930         sb = new StrBuilder("aaxaaaayaa");
931         sb.replace(StrMatcher.stringMatcher("aa"), "-", 7, sb.length(), -1);
932         assertEquals("aaxaaaay-", sb.toString());
933         
934         sb = new StrBuilder("aaxaaaayaa");
935         sb.replace(StrMatcher.stringMatcher("aa"), "-", 8, sb.length(), -1);
936         assertEquals("aaxaaaay-", sb.toString());
937         
938         sb = new StrBuilder("aaxaaaayaa");
939         sb.replace(StrMatcher.stringMatcher("aa"), "-", 9, sb.length(), -1);
940         assertEquals("aaxaaaayaa", sb.toString());
941         
942         sb = new StrBuilder("aaxaaaayaa");
943         sb.replace(StrMatcher.stringMatcher("aa"), "-", 10, sb.length(), -1);
944         assertEquals("aaxaaaayaa", sb.toString());
945         
946         sb = new StrBuilder("aaxaaaayaa");
947         try {
948             sb.replace(StrMatcher.stringMatcher("aa"), "-", 11, sb.length(), -1);
949             fail();
950         } catch (final IndexOutOfBoundsException ex) {}
951         assertEquals("aaxaaaayaa", sb.toString());
952         
953         sb = new StrBuilder("aaxaaaayaa");
954         try {
955             sb.replace(StrMatcher.stringMatcher("aa"), "-", -1, sb.length(), -1);
956             fail();
957         } catch (final IndexOutOfBoundsException ex) {}
958         assertEquals("aaxaaaayaa", sb.toString());
959     }
960 
961     @Test
962     public void testReplace_StrMatcher_String_int_int_int_VaryEndIndex() {
963         StrBuilder sb = new StrBuilder("aaxaaaayaa");
964         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 0, -1);
965         assertEquals("aaxaaaayaa", sb.toString());
966         
967         sb = new StrBuilder("aaxaaaayaa");
968         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 2, -1);
969         assertEquals("-xaaaayaa", sb.toString());
970         
971         sb = new StrBuilder("aaxaaaayaa");
972         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 3, -1);
973         assertEquals("-xaaaayaa", sb.toString());
974         
975         sb = new StrBuilder("aaxaaaayaa");
976         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 4, -1);
977         assertEquals("-xaaaayaa", sb.toString());
978         
979         sb = new StrBuilder("aaxaaaayaa");
980         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 5, -1);
981         assertEquals("-x-aayaa", sb.toString());
982         
983         sb = new StrBuilder("aaxaaaayaa");
984         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 6, -1);
985         assertEquals("-x-aayaa", sb.toString());
986         
987         sb = new StrBuilder("aaxaaaayaa");
988         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 7, -1);
989         assertEquals("-x--yaa", sb.toString());
990         
991         sb = new StrBuilder("aaxaaaayaa");
992         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 8, -1);
993         assertEquals("-x--yaa", sb.toString());
994         
995         sb = new StrBuilder("aaxaaaayaa");
996         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 9, -1);
997         assertEquals("-x--yaa", sb.toString());
998         
999         sb = new StrBuilder("aaxaaaayaa");
1000         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, -1);
1001         assertEquals("-x--y-", sb.toString());
1002         
1003         sb = new StrBuilder("aaxaaaayaa");
1004         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 1000, -1);
1005         assertEquals("-x--y-", sb.toString());
1006         
1007         sb = new StrBuilder("aaxaaaayaa");
1008         try {
1009             sb.replace(StrMatcher.stringMatcher("aa"), "-", 2, 1, -1);
1010             fail();
1011         } catch (final IndexOutOfBoundsException ex) {}
1012         assertEquals("aaxaaaayaa", sb.toString());
1013     }
1014 
1015     @Test
1016     public void testReplace_StrMatcher_String_int_int_int_VaryCount() {
1017         StrBuilder sb = new StrBuilder("aaxaaaayaa");
1018         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, -1);
1019         assertEquals("-x--y-", sb.toString());
1020         
1021         sb = new StrBuilder("aaxaaaayaa");
1022         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 0);
1023         assertEquals("aaxaaaayaa", sb.toString());
1024         
1025         sb = new StrBuilder("aaxaaaayaa");
1026         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 1);
1027         assertEquals("-xaaaayaa", sb.toString());
1028         
1029         sb = new StrBuilder("aaxaaaayaa");
1030         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 2);
1031         assertEquals("-x-aayaa", sb.toString());
1032         
1033         sb = new StrBuilder("aaxaaaayaa");
1034         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 3);
1035         assertEquals("-x--yaa", sb.toString());
1036         
1037         sb = new StrBuilder("aaxaaaayaa");
1038         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 4);
1039         assertEquals("-x--y-", sb.toString());
1040         
1041         sb = new StrBuilder("aaxaaaayaa");
1042         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 5);
1043         assertEquals("-x--y-", sb.toString());
1044     }
1045 
1046     //-----------------------------------------------------------------------
1047     @Test
1048     public void testReverse() {
1049         final StrBuilder sb = new StrBuilder();
1050         assertEquals("", sb.reverse().toString());
1051         
1052         sb.clear().append(true);
1053         assertEquals("eurt", sb.reverse().toString());
1054         assertEquals("true", sb.reverse().toString());
1055     }
1056 
1057     //-----------------------------------------------------------------------
1058     @Test
1059     public void testTrim() {
1060         final StrBuilder sb = new StrBuilder();
1061         assertEquals("", sb.reverse().toString());
1062         
1063         sb.clear().append(" \u0000 ");
1064         assertEquals("", sb.trim().toString());
1065         
1066         sb.clear().append(" \u0000 a b c");
1067         assertEquals("a b c", sb.trim().toString());
1068         
1069         sb.clear().append("a b c \u0000 ");
1070         assertEquals("a b c", sb.trim().toString());
1071         
1072         sb.clear().append(" \u0000 a b c \u0000 ");
1073         assertEquals("a b c", sb.trim().toString());
1074         
1075         sb.clear().append("a b c");
1076         assertEquals("a b c", sb.trim().toString());
1077     }
1078 
1079     //-----------------------------------------------------------------------
1080     @Test
1081     public void testStartsWith() {
1082         final StrBuilder sb = new StrBuilder();
1083         assertFalse(sb.startsWith("a"));
1084         assertFalse(sb.startsWith(null));
1085         assertTrue(sb.startsWith(""));
1086         sb.append("abc");
1087         assertTrue(sb.startsWith("a"));
1088         assertTrue(sb.startsWith("ab"));
1089         assertTrue(sb.startsWith("abc"));
1090         assertFalse(sb.startsWith("cba"));
1091     }
1092 
1093     @Test
1094     public void testEndsWith() {
1095         final StrBuilder sb = new StrBuilder();
1096         assertFalse(sb.endsWith("a"));
1097         assertFalse(sb.endsWith("c"));
1098         assertTrue(sb.endsWith(""));
1099         assertFalse(sb.endsWith(null));
1100         sb.append("abc");
1101         assertTrue(sb.endsWith("c"));
1102         assertTrue(sb.endsWith("bc"));
1103         assertTrue(sb.endsWith("abc"));
1104         assertFalse(sb.endsWith("cba"));
1105         assertFalse(sb.endsWith("abcd"));
1106         assertFalse(sb.endsWith(" abc"));
1107         assertFalse(sb.endsWith("abc "));
1108     }
1109 
1110     //-----------------------------------------------------------------------
1111     @Test
1112     public void testSubSequenceIntInt() {
1113        final StrBuilder sb = new StrBuilder ("hello goodbye");
1114        // Start index is negative
1115        try {
1116             sb.subSequence(-1, 5);
1117             fail();
1118         } catch (final IndexOutOfBoundsException e) {}
1119         
1120         // End index is negative
1121        try {
1122             sb.subSequence(2, -1);
1123             fail();
1124         } catch (final IndexOutOfBoundsException e) {}
1125         
1126         // End index greater than length()
1127         try {
1128             sb.subSequence(2, sb.length() + 1);
1129             fail();
1130         } catch (final IndexOutOfBoundsException e) {}
1131         
1132         // Start index greater then end index
1133         try {
1134             sb.subSequence(3, 2);
1135             fail();
1136         } catch (final IndexOutOfBoundsException e) {}
1137         
1138         // Normal cases
1139         assertEquals ("hello", sb.subSequence(0, 5));
1140         assertEquals ("hello goodbye".subSequence(0, 6), sb.subSequence(0, 6));
1141         assertEquals ("goodbye", sb.subSequence(6, 13));
1142         assertEquals ("hello goodbye".subSequence(6,13), sb.subSequence(6, 13));
1143     }
1144 
1145     @Test
1146     public void testSubstringInt() {
1147         final StrBuilder sb = new StrBuilder ("hello goodbye");
1148         assertEquals ("goodbye", sb.substring(6));
1149         assertEquals ("hello goodbye".substring(6), sb.substring(6));
1150         assertEquals ("hello goodbye", sb.substring(0));
1151         assertEquals ("hello goodbye".substring(0), sb.substring(0));
1152         try {
1153             sb.substring(-1);
1154             fail ();
1155         } catch (final IndexOutOfBoundsException e) {}
1156         
1157         try {
1158             sb.substring(15);
1159             fail ();
1160         } catch (final IndexOutOfBoundsException e) {}
1161     
1162     }
1163     
1164     @Test
1165     public void testSubstringIntInt() {
1166         final StrBuilder sb = new StrBuilder ("hello goodbye");
1167         assertEquals ("hello", sb.substring(0, 5));
1168         assertEquals ("hello goodbye".substring(0, 6), sb.substring(0, 6));
1169         
1170         assertEquals ("goodbye", sb.substring(6, 13));
1171         assertEquals ("hello goodbye".substring(6,13), sb.substring(6, 13));
1172         
1173         assertEquals ("goodbye", sb.substring(6, 20));
1174         
1175         try {
1176             sb.substring(-1, 5);
1177             fail();
1178         } catch (final IndexOutOfBoundsException e) {}
1179         
1180         try {
1181             sb.substring(15, 20);
1182             fail();
1183         } catch (final IndexOutOfBoundsException e) {}
1184     }
1185 
1186     // -----------------------------------------------------------------------
1187     @Test
1188     public void testMidString() {
1189         final StrBuilder sb = new StrBuilder("hello goodbye hello");
1190         assertEquals("goodbye", sb.midString(6, 7));
1191         assertEquals("hello", sb.midString(0, 5));
1192         assertEquals("hello", sb.midString(-5, 5));
1193         assertEquals("", sb.midString(0, -1));
1194         assertEquals("", sb.midString(20, 2));
1195         assertEquals("hello", sb.midString(14, 22));
1196     }
1197 
1198     @Test
1199     public void testRightString() {
1200         final StrBuilder sb = new StrBuilder("left right");
1201         assertEquals("right", sb.rightString(5));
1202         assertEquals("", sb.rightString(0));
1203         assertEquals("", sb.rightString(-5));
1204         assertEquals("left right", sb.rightString(15));
1205     }
1206 
1207     @Test
1208     public void testLeftString() {
1209         final StrBuilder sb = new StrBuilder("left right");
1210         assertEquals("left", sb.leftString(4));
1211         assertEquals("", sb.leftString(0));
1212         assertEquals("", sb.leftString(-5));
1213         assertEquals("left right", sb.leftString(15));
1214     }
1215 
1216     // -----------------------------------------------------------------------
1217     @Test
1218     public void testContains_char() {
1219         final StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
1220         assertTrue(sb.contains('a'));
1221         assertTrue(sb.contains('o'));
1222         assertTrue(sb.contains('z'));
1223         assertFalse(sb.contains('1'));
1224     }
1225 
1226     @Test
1227     public void testContains_String() {
1228         final StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
1229         assertTrue(sb.contains("a"));
1230         assertTrue(sb.contains("pq"));
1231         assertTrue(sb.contains("z"));
1232         assertFalse(sb.contains("zyx"));
1233         assertFalse(sb.contains((String) null));
1234     }
1235 
1236     @Test
1237     public void testContains_StrMatcher() {
1238         StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
1239         assertTrue(sb.contains(StrMatcher.charMatcher('a')));
1240         assertTrue(sb.contains(StrMatcher.stringMatcher("pq")));
1241         assertTrue(sb.contains(StrMatcher.charMatcher('z')));
1242         assertFalse(sb.contains(StrMatcher.stringMatcher("zy")));
1243         assertFalse(sb.contains((StrMatcher) null));
1244 
1245         sb = new StrBuilder();
1246         assertFalse(sb.contains(A_NUMBER_MATCHER));
1247         sb.append("B A1 C");
1248         assertTrue(sb.contains(A_NUMBER_MATCHER));
1249     }
1250 
1251     // -----------------------------------------------------------------------
1252     @Test
1253     public void testIndexOf_char() {
1254         final StrBuilder sb = new StrBuilder("abab");
1255         assertEquals(0, sb.indexOf('a'));
1256         
1257         // should work like String#indexOf
1258         assertEquals("abab".indexOf('a'), sb.indexOf('a'));
1259 
1260         assertEquals(1, sb.indexOf('b'));
1261         assertEquals("abab".indexOf('b'), sb.indexOf('b'));
1262 
1263         assertEquals(-1, sb.indexOf('z'));
1264     }
1265 
1266     @Test
1267     public void testIndexOf_char_int() {
1268         StrBuilder sb = new StrBuilder("abab");
1269         assertEquals(0, sb.indexOf('a', -1));
1270         assertEquals(0, sb.indexOf('a', 0));
1271         assertEquals(2, sb.indexOf('a', 1));
1272         assertEquals(-1, sb.indexOf('a', 4));
1273         assertEquals(-1, sb.indexOf('a', 5));
1274 
1275         // should work like String#indexOf
1276         assertEquals("abab".indexOf('a', 1), sb.indexOf('a', 1));
1277 
1278         assertEquals(3, sb.indexOf('b', 2));
1279         assertEquals("abab".indexOf('b', 2), sb.indexOf('b', 2));
1280 
1281         assertEquals(-1, sb.indexOf('z', 2));
1282 
1283         sb = new StrBuilder("xyzabc");
1284         assertEquals(2, sb.indexOf('z', 0));
1285         assertEquals(-1, sb.indexOf('z', 3));
1286     }
1287 
1288     @Test
1289     public void testLastIndexOf_char() {
1290         final StrBuilder sb = new StrBuilder("abab");
1291         
1292         assertEquals (2, sb.lastIndexOf('a'));
1293         //should work like String#lastIndexOf
1294         assertEquals ("abab".lastIndexOf('a'), sb.lastIndexOf('a'));
1295         
1296         assertEquals(3, sb.lastIndexOf('b'));
1297         assertEquals ("abab".lastIndexOf('b'), sb.lastIndexOf('b'));
1298         
1299         assertEquals (-1, sb.lastIndexOf('z'));
1300     }
1301 
1302     @Test
1303     public void testLastIndexOf_char_int() {
1304         StrBuilder sb = new StrBuilder("abab");
1305         assertEquals(-1, sb.lastIndexOf('a', -1));
1306         assertEquals(0, sb.lastIndexOf('a', 0));
1307         assertEquals(0, sb.lastIndexOf('a', 1));
1308 
1309         // should work like String#lastIndexOf
1310         assertEquals("abab".lastIndexOf('a', 1), sb.lastIndexOf('a', 1));
1311 
1312         assertEquals(1, sb.lastIndexOf('b', 2));
1313         assertEquals("abab".lastIndexOf('b', 2), sb.lastIndexOf('b', 2));
1314 
1315         assertEquals(-1, sb.lastIndexOf('z', 2));
1316 
1317         sb = new StrBuilder("xyzabc");
1318         assertEquals(2, sb.lastIndexOf('z', sb.length()));
1319         assertEquals(-1, sb.lastIndexOf('z', 1));
1320     }
1321 
1322     // -----------------------------------------------------------------------
1323     @Test
1324     public void testIndexOf_String() {
1325         final StrBuilder sb = new StrBuilder("abab");
1326         
1327         assertEquals(0, sb.indexOf("a"));
1328         //should work like String#indexOf
1329         assertEquals("abab".indexOf("a"), sb.indexOf("a"));
1330         
1331         assertEquals(0, sb.indexOf("ab"));
1332         //should work like String#indexOf
1333         assertEquals("abab".indexOf("ab"), sb.indexOf("ab"));
1334         
1335         assertEquals(1, sb.indexOf("b"));
1336         assertEquals("abab".indexOf("b"), sb.indexOf("b"));
1337         
1338         assertEquals(1, sb.indexOf("ba"));
1339         assertEquals("abab".indexOf("ba"), sb.indexOf("ba"));
1340         
1341         assertEquals(-1, sb.indexOf("z"));
1342         
1343         assertEquals(-1, sb.indexOf((String) null));
1344     }
1345 
1346     @Test
1347     public void testIndexOf_String_int() {
1348         StrBuilder sb = new StrBuilder("abab");
1349         assertEquals(0, sb.indexOf("a", -1));
1350         assertEquals(0, sb.indexOf("a", 0));
1351         assertEquals(2, sb.indexOf("a", 1));
1352         assertEquals(2, sb.indexOf("a", 2));
1353         assertEquals(-1, sb.indexOf("a", 3));
1354         assertEquals(-1, sb.indexOf("a", 4));
1355         assertEquals(-1, sb.indexOf("a", 5));
1356         
1357         assertEquals(-1, sb.indexOf("abcdef", 0));
1358         assertEquals(0, sb.indexOf("", 0));
1359         assertEquals(1, sb.indexOf("", 1));
1360         
1361         //should work like String#indexOf
1362         assertEquals ("abab".indexOf("a", 1), sb.indexOf("a", 1));
1363         
1364         assertEquals(2, sb.indexOf("ab", 1));
1365         //should work like String#indexOf
1366         assertEquals("abab".indexOf("ab", 1), sb.indexOf("ab", 1));
1367         
1368         assertEquals(3, sb.indexOf("b", 2));
1369         assertEquals("abab".indexOf("b", 2), sb.indexOf("b", 2));
1370         
1371         assertEquals(1, sb.indexOf("ba", 1));
1372         assertEquals("abab".indexOf("ba", 2), sb.indexOf("ba", 2));
1373         
1374         assertEquals(-1, sb.indexOf("z", 2));
1375         
1376         sb = new StrBuilder("xyzabc");
1377         assertEquals(2, sb.indexOf("za", 0));
1378         assertEquals(-1, sb.indexOf("za", 3));
1379         
1380         assertEquals(-1, sb.indexOf((String) null, 2));
1381     }
1382 
1383     @Test
1384     public void testLastIndexOf_String() {
1385         final StrBuilder sb = new StrBuilder("abab");
1386         
1387         assertEquals(2, sb.lastIndexOf("a"));
1388         //should work like String#lastIndexOf
1389         assertEquals("abab".lastIndexOf("a"), sb.lastIndexOf("a"));
1390         
1391         assertEquals(2, sb.lastIndexOf("ab"));
1392         //should work like String#lastIndexOf
1393         assertEquals("abab".lastIndexOf("ab"), sb.lastIndexOf("ab"));
1394         
1395         assertEquals(3, sb.lastIndexOf("b"));
1396         assertEquals("abab".lastIndexOf("b"), sb.lastIndexOf("b"));
1397         
1398         assertEquals(1, sb.lastIndexOf("ba"));
1399         assertEquals("abab".lastIndexOf("ba"), sb.lastIndexOf("ba"));
1400         
1401         assertEquals(-1, sb.lastIndexOf("z"));
1402         
1403         assertEquals(-1, sb.lastIndexOf((String) null));
1404     }
1405 
1406     @Test
1407     public void testLastIndexOf_String_int() {
1408         StrBuilder sb = new StrBuilder("abab");
1409         assertEquals(-1, sb.lastIndexOf("a", -1));
1410         assertEquals(0, sb.lastIndexOf("a", 0));
1411         assertEquals(0, sb.lastIndexOf("a", 1));
1412         assertEquals(2, sb.lastIndexOf("a", 2));
1413         assertEquals(2, sb.lastIndexOf("a", 3));
1414         assertEquals(2, sb.lastIndexOf("a", 4));
1415         assertEquals(2, sb.lastIndexOf("a", 5));
1416         
1417         assertEquals(-1, sb.lastIndexOf("abcdef", 3));
1418         assertEquals("abab".lastIndexOf("", 3), sb.lastIndexOf("", 3));
1419         assertEquals("abab".lastIndexOf("", 1), sb.lastIndexOf("", 1));
1420         
1421         //should work like String#lastIndexOf
1422         assertEquals("abab".lastIndexOf("a", 1), sb.lastIndexOf("a", 1));
1423         
1424         assertEquals(0, sb.lastIndexOf("ab", 1));
1425         //should work like String#lastIndexOf
1426         assertEquals("abab".lastIndexOf("ab", 1), sb.lastIndexOf("ab", 1));
1427         
1428         assertEquals(1, sb.lastIndexOf("b", 2));
1429         assertEquals("abab".lastIndexOf("b", 2), sb.lastIndexOf("b", 2));
1430         
1431         assertEquals(1, sb.lastIndexOf("ba", 2));
1432         assertEquals("abab".lastIndexOf("ba", 2), sb.lastIndexOf("ba", 2));
1433         
1434         assertEquals(-1, sb.lastIndexOf("z", 2));
1435         
1436         sb = new StrBuilder("xyzabc");
1437         assertEquals(2, sb.lastIndexOf("za", sb.length()));
1438         assertEquals(-1, sb.lastIndexOf("za", 1));
1439         
1440         assertEquals(-1, sb.lastIndexOf((String) null, 2));
1441     }
1442 
1443     // -----------------------------------------------------------------------
1444     @Test
1445     public void testIndexOf_StrMatcher() {
1446         final StrBuilder sb = new StrBuilder();
1447         assertEquals(-1, sb.indexOf((StrMatcher) null));
1448         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a')));
1449         
1450         sb.append("ab bd");
1451         assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a')));
1452         assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b')));
1453         assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher()));
1454         assertEquals(4, sb.indexOf(StrMatcher.charMatcher('d')));
1455         assertEquals(-1, sb.indexOf(StrMatcher.noneMatcher()));
1456         assertEquals(-1, sb.indexOf((StrMatcher) null));
1457         
1458         sb.append(" A1 junction");
1459         assertEquals(6, sb.indexOf(A_NUMBER_MATCHER));
1460     }
1461 
1462     @Test
1463     public void testIndexOf_StrMatcher_int() {
1464         final StrBuilder sb = new StrBuilder();
1465         assertEquals(-1, sb.indexOf((StrMatcher) null, 2));
1466         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 2));
1467         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 0));
1468         
1469         sb.append("ab bd");
1470         assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'), -2));
1471         assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'), 0));
1472         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 2));
1473         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 20));
1474         
1475         assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), -1));
1476         assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), 0));
1477         assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), 1));
1478         assertEquals(3, sb.indexOf(StrMatcher.charMatcher('b'), 2));
1479         assertEquals(3, sb.indexOf(StrMatcher.charMatcher('b'), 3));
1480         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 4));
1481         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 5));
1482         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 6));
1483         
1484         assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), -2));
1485         assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), 0));
1486         assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), 2));
1487         assertEquals(-1, sb.indexOf(StrMatcher.spaceMatcher(), 4));
1488         assertEquals(-1, sb.indexOf(StrMatcher.spaceMatcher(), 20));
1489         
1490         assertEquals(-1, sb.indexOf(StrMatcher.noneMatcher(), 0));
1491         assertEquals(-1, sb.indexOf((StrMatcher) null, 0));
1492         
1493         sb.append(" A1 junction with A2");
1494         assertEquals(6, sb.indexOf(A_NUMBER_MATCHER, 5));
1495         assertEquals(6, sb.indexOf(A_NUMBER_MATCHER, 6));
1496         assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 7));
1497         assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 22));
1498         assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 23));
1499         assertEquals(-1, sb.indexOf(A_NUMBER_MATCHER, 24));
1500     }
1501 
1502     @Test
1503     public void testLastIndexOf_StrMatcher() {
1504         final StrBuilder sb = new StrBuilder();
1505         assertEquals(-1, sb.lastIndexOf((StrMatcher) null));
1506         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a')));
1507         
1508         sb.append("ab bd");
1509         assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a')));
1510         assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b')));
1511         assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher()));
1512         assertEquals(4, sb.lastIndexOf(StrMatcher.charMatcher('d')));
1513         assertEquals(-1, sb.lastIndexOf(StrMatcher.noneMatcher()));
1514         assertEquals(-1, sb.lastIndexOf((StrMatcher) null));
1515         
1516         sb.append(" A1 junction");
1517         assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER));
1518     }
1519 
1520     @Test
1521     public void testLastIndexOf_StrMatcher_int() {
1522         final StrBuilder sb = new StrBuilder();
1523         assertEquals(-1, sb.lastIndexOf((StrMatcher) null, 2));
1524         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), 2));
1525         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), 0));
1526         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), -1));
1527         
1528         sb.append("ab bd");
1529         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), -2));
1530         assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 0));
1531         assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 2));
1532         assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 20));
1533         
1534         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('b'), -1));
1535         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 0));
1536         assertEquals(1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 1));
1537         assertEquals(1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 2));
1538         assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 3));
1539         assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 4));
1540         assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 5));
1541         assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 6));
1542         
1543         assertEquals(-1, sb.lastIndexOf(StrMatcher.spaceMatcher(), -2));
1544         assertEquals(-1, sb.lastIndexOf(StrMatcher.spaceMatcher(), 0));
1545         assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 2));
1546         assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 4));
1547         assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 20));
1548         
1549         assertEquals(-1, sb.lastIndexOf(StrMatcher.noneMatcher(), 0));
1550         assertEquals(-1, sb.lastIndexOf((StrMatcher) null, 0));
1551         
1552         sb.append(" A1 junction with A2");
1553         assertEquals(-1, sb.lastIndexOf(A_NUMBER_MATCHER, 5));
1554         assertEquals(-1, sb.lastIndexOf(A_NUMBER_MATCHER, 6)); // A matches, 1 is outside bounds
1555         assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 7));
1556         assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 22));
1557         assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 23)); // A matches, 2 is outside bounds
1558         assertEquals(23, sb.lastIndexOf(A_NUMBER_MATCHER, 24));
1559     }
1560 
1561     static final StrMatcher A_NUMBER_MATCHER = new StrMatcher() {
1562         @Override
1563         public int isMatch(final char[] buffer, int pos, final int bufferStart, final int bufferEnd) {
1564             if (buffer[pos] == 'A') {
1565                 pos++;
1566                 if (pos < bufferEnd && buffer[pos] >= '0' && buffer[pos] <= '9') {
1567                     return 2;
1568                 }
1569             }
1570             return 0;
1571         }
1572     };
1573 
1574     //-----------------------------------------------------------------------
1575     @Test
1576     public void testAsTokenizer() throws Exception {
1577         // from Javadoc
1578         final StrBuilder b = new StrBuilder();
1579         b.append("a b ");
1580         final StrTokenizer t = b.asTokenizer();
1581         
1582         final String[] tokens1 = t.getTokenArray();
1583         assertEquals(2, tokens1.length);
1584         assertEquals("a", tokens1[0]);
1585         assertEquals("b", tokens1[1]);
1586         assertEquals(2, t.size());
1587         
1588         b.append("c d ");
1589         final String[] tokens2 = t.getTokenArray();
1590         assertEquals(2, tokens2.length);
1591         assertEquals("a", tokens2[0]);
1592         assertEquals("b", tokens2[1]);
1593         assertEquals(2, t.size());
1594         assertEquals("a", t.next());
1595         assertEquals("b", t.next());
1596         
1597         t.reset();
1598         final String[] tokens3 = t.getTokenArray();
1599         assertEquals(4, tokens3.length);
1600         assertEquals("a", tokens3[0]);
1601         assertEquals("b", tokens3[1]);
1602         assertEquals("c", tokens3[2]);
1603         assertEquals("d", tokens3[3]);
1604         assertEquals(4, t.size());
1605         assertEquals("a", t.next());
1606         assertEquals("b", t.next());
1607         assertEquals("c", t.next());
1608         assertEquals("d", t.next());
1609         
1610         assertEquals("a b c d ", t.getContent());
1611     }
1612 
1613     // -----------------------------------------------------------------------
1614     @Test
1615     public void testAsReader() throws Exception {
1616         final StrBuilder sb = new StrBuilder("some text");
1617         Reader reader = sb.asReader();
1618         assertTrue(reader.ready());
1619         final char[] buf = new char[40];
1620         assertEquals(9, reader.read(buf));
1621         assertEquals("some text", new String(buf, 0, 9));
1622         
1623         assertEquals(-1, reader.read());
1624         assertFalse(reader.ready());
1625         assertEquals(0, reader.skip(2));
1626         assertEquals(0, reader.skip(-1));
1627         
1628         assertTrue(reader.markSupported());
1629         reader = sb.asReader();
1630         assertEquals('s', reader.read());
1631         reader.mark(-1);
1632         char[] array = new char[3];
1633         assertEquals(3, reader.read(array, 0, 3));
1634         assertEquals('o', array[0]);
1635         assertEquals('m', array[1]);
1636         assertEquals('e', array[2]);
1637         reader.reset();
1638         assertEquals(1, reader.read(array, 1, 1));
1639         assertEquals('o', array[0]);
1640         assertEquals('o', array[1]);
1641         assertEquals('e', array[2]);
1642         assertEquals(2, reader.skip(2));
1643         assertEquals(' ', reader.read());
1644         
1645         assertTrue(reader.ready());
1646         reader.close();
1647         assertTrue(reader.ready());
1648         
1649         reader = sb.asReader();
1650         array = new char[3];
1651         try {
1652             reader.read(array, -1, 0);
1653             fail();
1654         } catch (final IndexOutOfBoundsException ex) {}
1655         try {
1656             reader.read(array, 0, -1);
1657             fail();
1658         } catch (final IndexOutOfBoundsException ex) {}
1659         try {
1660             reader.read(array, 100, 1);
1661             fail();
1662         } catch (final IndexOutOfBoundsException ex) {}
1663         try {
1664             reader.read(array, 0, 100);
1665             fail();
1666         } catch (final IndexOutOfBoundsException ex) {}
1667         try {
1668             reader.read(array, Integer.MAX_VALUE, Integer.MAX_VALUE);
1669             fail();
1670         } catch (final IndexOutOfBoundsException ex) {}
1671         
1672         assertEquals(0, reader.read(array, 0, 0));
1673         assertEquals(0, array[0]);
1674         assertEquals(0, array[1]);
1675         assertEquals(0, array[2]);
1676         
1677         reader.skip(9);
1678         assertEquals(-1, reader.read(array, 0, 1));
1679         
1680         reader.reset();
1681         array = new char[30];
1682         assertEquals(9, reader.read(array, 0, 30));
1683     }
1684 
1685     //-----------------------------------------------------------------------
1686     @Test
1687     public void testAsWriter() throws Exception {
1688         final StrBuilder sb = new StrBuilder("base");
1689         final Writer writer = sb.asWriter();
1690         
1691         writer.write('l');
1692         assertEquals("basel", sb.toString());
1693         
1694         writer.write(new char[] {'i', 'n'});
1695         assertEquals("baselin", sb.toString());
1696         
1697         writer.write(new char[] {'n', 'e', 'r'}, 1, 2);
1698         assertEquals("baseliner", sb.toString());
1699         
1700         writer.write(" rout");
1701         assertEquals("baseliner rout", sb.toString());
1702         
1703         writer.write("ping that server", 1, 3);
1704         assertEquals("baseliner routing", sb.toString());
1705         
1706         writer.flush();  // no effect
1707         assertEquals("baseliner routing", sb.toString());
1708         
1709         writer.close();  // no effect
1710         assertEquals("baseliner routing", sb.toString());
1711         
1712         writer.write(" hi");  // works after close
1713         assertEquals("baseliner routing hi", sb.toString());
1714         
1715         sb.setLength(4);  // mix and match
1716         writer.write('d');
1717         assertEquals("based", sb.toString());
1718     }
1719 
1720     //-----------------------------------------------------------------------
1721     @Test
1722     public void testEqualsIgnoreCase() {
1723         final StrBuilder sb1 = new StrBuilder();
1724         final StrBuilder sb2 = new StrBuilder();
1725         assertTrue(sb1.equalsIgnoreCase(sb1));
1726         assertTrue(sb1.equalsIgnoreCase(sb2));
1727         assertTrue(sb2.equalsIgnoreCase(sb2));
1728         
1729         sb1.append("abc");
1730         assertFalse(sb1.equalsIgnoreCase(sb2));
1731         
1732         sb2.append("ABC");
1733         assertTrue(sb1.equalsIgnoreCase(sb2));
1734         
1735         sb2.clear().append("abc");
1736         assertTrue(sb1.equalsIgnoreCase(sb2));
1737         assertTrue(sb1.equalsIgnoreCase(sb1));
1738         assertTrue(sb2.equalsIgnoreCase(sb2));
1739         
1740         sb2.clear().append("aBc");
1741         assertTrue(sb1.equalsIgnoreCase(sb2));
1742     }
1743 
1744     //-----------------------------------------------------------------------
1745     @Test
1746     public void testEquals() {
1747         final StrBuilder sb1 = new StrBuilder();
1748         final StrBuilder sb2 = new StrBuilder();
1749         assertTrue(sb1.equals(sb2));
1750         assertTrue(sb1.equals(sb1));
1751         assertTrue(sb2.equals(sb2));
1752         assertTrue(sb1.equals((Object) sb2));
1753         
1754         sb1.append("abc");
1755         assertFalse(sb1.equals(sb2));
1756         assertFalse(sb1.equals((Object) sb2));
1757         
1758         sb2.append("ABC");
1759         assertFalse(sb1.equals(sb2));
1760         assertFalse(sb1.equals((Object) sb2));
1761         
1762         sb2.clear().append("abc");
1763         assertTrue(sb1.equals(sb2));
1764         assertTrue(sb1.equals((Object) sb2));
1765         
1766         assertFalse(sb1.equals(Integer.valueOf(1)));
1767         assertFalse(sb1.equals("abc"));
1768     }
1769 
1770     //-----------------------------------------------------------------------
1771     @Test
1772     public void testHashCode() {
1773         final StrBuilder sb = new StrBuilder();
1774         final int hc1a = sb.hashCode();
1775         final int hc1b = sb.hashCode();
1776         assertEquals(0, hc1a);
1777         assertEquals(hc1a, hc1b);
1778         
1779         sb.append("abc");
1780         final int hc2a = sb.hashCode();
1781         final int hc2b = sb.hashCode();
1782         assertTrue(hc2a != 0);
1783         assertEquals(hc2a, hc2b);
1784     }
1785 
1786     //-----------------------------------------------------------------------
1787     @Test
1788     public void testToString() {
1789         final StrBuilder sb = new StrBuilder("abc");
1790         assertEquals("abc", sb.toString());
1791     }
1792 
1793     //-----------------------------------------------------------------------
1794     @Test
1795     public void testToStringBuffer() {
1796         final StrBuilder sb = new StrBuilder();
1797         assertEquals(new StringBuffer().toString(), sb.toStringBuffer().toString());
1798         
1799         sb.append("junit");
1800         assertEquals(new StringBuffer("junit").toString(), sb.toStringBuffer().toString());
1801     }
1802 
1803     //-----------------------------------------------------------------------
1804     @Test
1805     public void testToStringBuilder() {
1806         final StrBuilder sb = new StrBuilder();
1807         assertEquals(new StringBuilder().toString(), sb.toStringBuilder().toString());
1808         
1809         sb.append("junit");
1810         assertEquals(new StringBuilder("junit").toString(), sb.toStringBuilder().toString());
1811     }
1812 
1813     //-----------------------------------------------------------------------
1814     @Test
1815     public void testLang294() {
1816         final StrBuilder sb = new StrBuilder("\n%BLAH%\nDo more stuff\neven more stuff\n%BLAH%\n");
1817         sb.deleteAll("\n%BLAH%");
1818         assertEquals("\nDo more stuff\neven more stuff\n", sb.toString()); 
1819     }
1820 
1821     @Test
1822     public void testIndexOfLang294() {
1823         final StrBuilder sb = new StrBuilder("onetwothree");
1824         sb.deleteFirst("three");
1825         assertEquals(-1, sb.indexOf("three"));
1826     }
1827 
1828     //-----------------------------------------------------------------------
1829     @Test
1830     public void testLang295() {
1831         final StrBuilder sb = new StrBuilder("onetwothree");
1832         sb.deleteFirst("three");
1833         assertFalse( "The contains(char) method is looking beyond the end of the string", sb.contains('h'));
1834         assertEquals( "The indexOf(char) method is looking beyond the end of the string", -1, sb.indexOf('h'));
1835     }
1836 
1837     //-----------------------------------------------------------------------
1838     @Test
1839     public void testLang412Right() {
1840         final StrBuilder sb = new StrBuilder();
1841         sb.appendFixedWidthPadRight(null, 10, '*');
1842         assertEquals( "Failed to invoke appendFixedWidthPadRight correctly", "**********", sb.toString());
1843     }
1844 
1845     @Test
1846     public void testLang412Left() {
1847         final StrBuilder sb = new StrBuilder();
1848         sb.appendFixedWidthPadLeft(null, 10, '*');
1849         assertEquals( "Failed to invoke appendFixedWidthPadLeft correctly", "**********", sb.toString());
1850     }
1851 
1852     @Test
1853     public void testAsBuilder() {
1854         final StrBuilder sb = new StrBuilder().appendAll("Lorem", " ", "ipsum", " ", "dolor");
1855         assertEquals(sb.toString(), sb.build());
1856     }
1857 
1858 }