New test.
[mono.git] / mcs / class / corlib / Test / System.Text / StringBuilderTest.cs
1 // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 //
3 // StringBuilderTest.dll - NUnit Test Cases for the System.Text.StringBuilder class
4 // 
5 // Author: Marcin Szczepanski (marcins@zipworld.com.au)
6 //
7 // NOTES: I've also run all these tests against the MS implementation of 
8 // System.Text.StringBuilder to confirm that they return the same results
9 // and they do.
10 //
11 // TODO: Add tests for the AppendFormat methods once the AppendFormat methods
12 // are implemented in the StringBuilder class itself
13 //
14 // TODO: Potentially add more variations on Insert / Append tests for all the
15 // possible types.  I don't really think that's necessary as they all
16 // pretty much just do .ToString().ToCharArray() and then use the Append / Insert
17 // CharArray function.  The ToString() bit for each type should be in the unit
18 // tests for those types, and the unit test for ToCharArray should be in the 
19 // string test type.  If someone wants to add those tests here for completness 
20 // (and some double checking) then feel free :)
21 //
22
23 using NUnit.Framework;
24 using System.Text;
25 using System;
26
27 namespace MonoTests.System.Text {
28
29         [TestFixture]
30         public class StringBuilderTest : Assertion {
31
32                 private StringBuilder sb;
33
34                 public void TestConstructor1() 
35                 {
36                         // check the parameterless ctor
37                         sb = new StringBuilder();
38                         AssertEquals(String.Empty, sb.ToString());
39                         AssertEquals(0, sb.Length);
40                         AssertEquals(16, sb.Capacity);
41                 }
42
43                 public void TestConstructor2() 
44                 {
45                         // check ctor that specifies the capacity
46                         sb = new StringBuilder(10);
47                         AssertEquals(String.Empty, sb.ToString());
48                         AssertEquals(0, sb.Length);
49                         // check that capacity is not less than default
50                         AssertEquals(10, sb.Capacity);
51
52                         sb = new StringBuilder(42);
53                         AssertEquals(String.Empty, sb.ToString());
54                         AssertEquals(0, sb.Length);
55                         // check that capacity is set
56                         AssertEquals(42, sb.Capacity);
57                 }
58                 
59                 public void TestConstructor3() {
60                         // check ctor that specifies the capacity & maxCapacity
61                         sb = new StringBuilder(444, 1234);
62                         AssertEquals(String.Empty, sb.ToString());
63                         AssertEquals(0, sb.Length);
64                         AssertEquals(444, sb.Capacity);
65                         AssertEquals(1234, sb.MaxCapacity);
66                 }
67
68                 public void TestConstructor4() 
69                 {
70                         // check for exception in ctor that specifies the capacity & maxCapacity
71                         try {
72                                 sb = new StringBuilder(9999, 15);
73                         }
74                         catch (ArgumentOutOfRangeException) {
75                                 return;
76                         }
77                         // if we didn't catch an exception, then we have a problem Houston.
78                         NUnit.Framework.Assertion.Fail("Capacity exeeds MaxCapacity");
79                 }
80
81         public void TestConstructor5() {
82                 String someString = null;
83                 sb = new StringBuilder(someString);
84                 AssertEquals("Should be empty string", String.Empty, sb.ToString());
85         }
86
87         public void TestConstructor6() {
88                 // check for exception in ctor that prevents startIndex less than zero
89                 try {
90                         String someString = "someString";
91                         sb = new StringBuilder(someString, -1, 3, 18);
92                 }
93                 catch (ArgumentOutOfRangeException) {
94                         return;
95                 }
96                 // if we didn't catch an exception, then we have a problem Houston.
97                 NUnit.Framework.Assertion.Fail("StartIndex not allowed to be less than zero.");
98         }
99
100         public void TestConstructor7() {
101                 // check for exception in ctor that prevents length less than zero
102                 try {
103                         String someString = "someString";
104                         sb = new StringBuilder(someString, 2, -222, 18);
105                 }
106                 catch (ArgumentOutOfRangeException) {
107                         return;
108                 }
109                 // if we didn't catch an exception, then we have a problem Houston.
110                 NUnit.Framework.Assertion.Fail("Length not allowed to be less than zero.");
111         }
112
113         public void TestConstructor8() {
114                 // check for exception in ctor that ensures substring is contained in given string
115                 // check that startIndex is not too big
116                 try {
117                         String someString = "someString";
118                         sb = new StringBuilder(someString, 10000, 4, 18);
119                 }
120                 catch (ArgumentOutOfRangeException) {
121                         return;
122                 }
123                 // if we didn't catch an exception, then we have a problem Houston.
124                 NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");
125         }
126
127         public void TestConstructor9() {
128                 // check for exception in ctor that ensures substring is contained in given string
129                 // check that length doesn't go beyond end of string
130                 try {
131                         String someString = "someString";
132                         sb = new StringBuilder(someString, 4, 4000, 18);
133                 }
134                 catch (ArgumentOutOfRangeException) {
135                         return;
136                 }
137                 // if we didn't catch an exception, then we have a problem Houston.
138                 NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");
139         }
140
141         public void TestConstructor10() {
142                 // check that substring is taken properly and made into a StringBuilder
143                 String someString = "someString";
144                 sb = new StringBuilder(someString, 4, 6, 18);
145                 string expected = "String";
146                 AssertEquals( expected, sb.ToString());
147         }
148
149         [ExpectedException(typeof(ArgumentOutOfRangeException))]
150         public void TestConstructor11 () {
151                 new StringBuilder (-1);
152         }
153                 
154         public void TestAppend() {
155                 StringBuilder sb = new StringBuilder( "Foo" );
156                 sb.Append( "Two" );
157                 string expected = "FooTwo";
158                 AssertEquals( expected, sb.ToString() );
159         }
160
161         public void TestInsert() {
162                 StringBuilder sb = new StringBuilder();
163
164                 AssertEquals( String.Empty, sb.ToString() ); 
165                         /* Test empty StringBuilder conforms to spec */
166
167                 sb.Insert( 0, "Foo" ); /* Test insert at start of empty string */
168
169                 AssertEquals( "Foo", sb.ToString() );
170
171                 sb.Insert( 1, "!!" ); /* Test insert not at start of string */
172
173                 AssertEquals( "F!!oo", sb.ToString() );
174
175                 sb.Insert( 5, ".." ); /* Test insert at end of string */
176
177                 AssertEquals( "F!!oo..", sb.ToString() );
178         
179                 sb.Insert( 0, 1234 ); /* Test insert of a number (at start of string) */
180                 
181                                 // FIX: Why does this test fail?
182                 //AssertEquals( "1234F!!oo..", sb.ToString() );
183                 
184                 sb.Insert( 5, 1.5 ); /* Test insert of a decimal (and end of string) */
185                 
186                                 // FIX: Why does this test fail?
187                                 //AssertEquals( "1234F1.5!!oo..", sb.ToString() );
188
189                 sb.Insert( 4, 'A' ); /* Test char insert in middle of string */
190
191                                 // FIX: Why does this test fail?
192                                 //AssertEquals( "1234AF1.5!!oo..", sb.ToString() );
193
194         }
195
196         public void TestReplace() {
197                 StringBuilder sb = new StringBuilder( "Foobarbaz" );
198
199                 sb.Replace( "bar", "!!!" );             /* Test same length replace in middle of string */
200                 
201                 AssertEquals( "Foo!!!baz", sb.ToString() );
202
203                 sb.Replace( "Foo", "ABcD" );            /* Test longer replace at start of string */
204
205                 AssertEquals( "ABcD!!!baz", sb.ToString() );
206
207                 sb.Replace( "baz", "00" );              /* Test shorter replace at end of string */
208                         
209                 AssertEquals( "ABcD!!!00", sb.ToString() );
210
211                 sb.Replace( sb.ToString(), null );      /* Test string clear as in spec */
212
213                 AssertEquals( String.Empty, sb.ToString() );
214
215                 /*           |         10        20        30
216                 /*         |0123456789012345678901234567890| */
217                 sb.Append( "abc this is testing abc the abc abc partial replace abc" );
218
219                 sb.Replace( "abc", "!!!", 0, 31 ); /* Partial replace at start of string */
220
221                 AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
222
223                 sb.Replace( "testing", "", 0, 15 ); /* Test replace across boundary */
224
225                 AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
226
227                 sb.Replace( "!!!", "" ); /* Test replace with empty string */
228
229                 AssertEquals(" this is testing  the  abc partial replace abc", sb.ToString() );
230         }
231
232         public void TestAppendFormat() {
233         }
234
235         [Test]
236         public void MoreReplace ()
237         {
238                 StringBuilder sb = new StringBuilder ();
239                 sb.Append ("First");
240                 sb.Append ("Second");
241                 sb.Append ("Third");
242                 sb.Replace ("Second", "Gone", 2, sb.Length-2);
243                 AssertEquals ("#01", "FirstGoneThird", sb.ToString ());
244
245                 sb.Length = 0;
246                 sb.Append ("This, is, a, list");
247                 sb.Replace (",", "comma-separated", 11, sb.Length-11);
248                 AssertEquals ("#02", "This, is, acomma-separated list", sb.ToString ());
249         }
250
251         [Test]
252         public void Insert0 ()
253         {
254                 StringBuilder sb = new StringBuilder();
255                 sb.Append("testtesttest");
256                 sb.Insert(0, '^');
257                 AssertEquals ("#01", "^testtesttest", sb.ToString ());
258         }
259
260         [Test]
261         public void AppendToEmpty ()
262         {
263                 StringBuilder sb = new StringBuilder();
264                 char [] ca = new char [] { 'c' };
265                 sb.Append (ca);
266                 AssertEquals ("#01", "c", sb.ToString ());
267         }
268
269
270         [Test]
271         public void TestRemove ()
272         {
273                 StringBuilder b = new StringBuilder ();
274                 b.Append ("Hello, I am a StringBuilder");
275                 b.Remove (0, 7);  // Should remove "Hello, "
276                 AssertEquals ("#01", "I am a StringBuilder", b.ToString ());
277         }
278
279         [Test]
280         public void Insert1 ()
281         {
282                 StringBuilder sb = new StringBuilder();
283                 sb.Insert(0, "aa");
284                 AssertEquals ("#01", "aa", sb.ToString ());
285
286                 char [] charArr = new char [] { 'b', 'c', 'd' };
287                 sb.Insert(1, charArr, 1, 1);
288                 AssertEquals ("#02", "aca", sb.ToString ());
289
290                 sb.Insert (1, null, 0, 0);
291                 AssertEquals ("#03", "aca", sb.ToString ());
292                 
293                 try {
294                         sb.Insert (1, null, 1, 1);
295                         Assertion.Fail ("#04: Value must not be null if startIndex and charCount > 0");
296                 } catch (ArgumentNullException) {}
297         }
298
299         [Test]
300         [ExpectedException (typeof (ArgumentOutOfRangeException))]
301         public void Constructor_StartIndexOverflow () 
302         {
303                 new StringBuilder ("Mono", Int32.MaxValue, 1, 0);
304         }
305
306         [Test]
307         [ExpectedException (typeof (ArgumentOutOfRangeException))]
308         public void Constructor_LengthOverflow () 
309         {
310                 new StringBuilder ("Mono", 1, Int32.MaxValue, 0);
311         }
312
313         [Test]
314         [ExpectedException (typeof (ArgumentOutOfRangeException))]
315         public void ToString_StartIndexOverflow () 
316         {
317                 StringBuilder sb = new StringBuilder ("Mono");
318                 sb.ToString (Int32.MaxValue, 1);
319         }
320
321         [Test]
322         [ExpectedException (typeof (ArgumentOutOfRangeException))]
323         public void ToString_LengthOverflow () 
324         {
325                 StringBuilder sb = new StringBuilder ("Mono");
326                 sb.ToString (1, Int32.MaxValue);
327         }
328
329         [Test]
330         [ExpectedException (typeof (ArgumentOutOfRangeException))]
331         public void Remove_StartIndexOverflow () 
332         {
333                 StringBuilder sb = new StringBuilder ("Mono");
334                 sb.Remove (Int32.MaxValue, 1);
335         }
336
337         [Test]
338         [ExpectedException (typeof (ArgumentOutOfRangeException))]
339         public void Remove_LengthOverflow () 
340         {
341                 StringBuilder sb = new StringBuilder ("Mono");
342                 sb.Remove (1, Int32.MaxValue);
343         }
344
345         [Test]
346         [ExpectedException (typeof (ArgumentOutOfRangeException))]
347         public void ReplaceChar_StartIndexOverflow () 
348         {
349                 StringBuilder sb = new StringBuilder ("Mono");
350                 sb.Replace ('o', '0', Int32.MaxValue, 1);
351         }
352
353         [Test]
354         [ExpectedException (typeof (ArgumentOutOfRangeException))]
355         public void ReplaceChar_CountOverflow () 
356         {
357                 StringBuilder sb = new StringBuilder ("Mono");
358                 sb.Replace ('0', '0', 1, Int32.MaxValue);
359         }
360
361         [Test]
362         [ExpectedException (typeof (ArgumentOutOfRangeException))]
363         public void ReplaceString_StartIndexOverflow () 
364         {
365                 StringBuilder sb = new StringBuilder ("Mono");
366                 sb.Replace ("o", "0", Int32.MaxValue, 1);
367         }
368
369         [Test]
370         [ExpectedException (typeof (ArgumentOutOfRangeException))]
371         public void ReplaceString_CountOverflow () 
372         {
373                 StringBuilder sb = new StringBuilder ("Mono");
374                 sb.Replace ("o", "0", 1, Int32.MaxValue);
375         }
376
377         [Test]
378         [ExpectedException (typeof (ArgumentOutOfRangeException))]
379         public void AppendCharArray_StartIndexOverflow () 
380         {
381                 StringBuilder sb = new StringBuilder ("Mono");
382                 sb.Append (new char[2], Int32.MaxValue, 1);
383         }
384
385         [Test]
386         [ExpectedException (typeof (ArgumentOutOfRangeException))]
387         public void AppendCharArray_CharCountOverflow () 
388         {
389                 StringBuilder sb = new StringBuilder ("Mono");
390                 sb.Append (new char[2], 1, Int32.MaxValue);
391         }
392
393         [Test]
394         [ExpectedException (typeof (ArgumentOutOfRangeException))]
395         public void AppendString_StartIndexOverflow () 
396         {
397                 StringBuilder sb = new StringBuilder ("Mono");
398                 sb.Append ("!", Int32.MaxValue, 1);
399         }
400
401         [Test]
402         [ExpectedException (typeof (ArgumentOutOfRangeException))]
403         public void AppendString_CountOverflow () 
404         {
405                 StringBuilder sb = new StringBuilder ("Mono");
406                 sb.Append ("!", 1, Int32.MaxValue);
407         }
408
409         [Test]
410         [ExpectedException (typeof (ArgumentOutOfRangeException))]
411         public void InsertCharArray_StartIndexOverflow () 
412         {
413                 StringBuilder sb = new StringBuilder ("Mono");
414                 sb.Insert (0, new char[2], Int32.MaxValue, 1);
415         }
416
417         [Test]
418         [ExpectedException (typeof (ArgumentOutOfRangeException))]
419         public void InsertCharArray_CharCountOverflow () 
420         {
421                 StringBuilder sb = new StringBuilder ("Mono");
422                 sb.Insert (0, new char[2], 1, Int32.MaxValue);
423         }
424
425         [Test]
426         [ExpectedException (typeof (ArgumentOutOfRangeException))]
427         public void MaxCapacity_Overflow1 ()
428         {
429                 StringBuilder sb = new StringBuilder (2, 3);
430                 sb.Append ("Mono");
431         }
432
433         [Test]
434         public void MaxCapacity_Overflow2 ()
435         {
436                 StringBuilder sb = new StringBuilder (2, 3);
437                 try {
438                         sb.Append ("Mono");
439                 } catch (ArgumentOutOfRangeException) {
440                 }
441
442                 AssertEquals (2, sb.Capacity);
443                 AssertEquals (3, sb.MaxCapacity);
444         }
445         
446         [Test]
447 #if ONLY_1_1
448         [ExpectedException (typeof (ArgumentOutOfRangeException))]
449         [Category ("NotWorking")] // Mono follows 2.0 behaviour in this case
450 #endif
451         public void MaxCapacity_Overflow3 ()
452         {
453                 //
454                 // When the capacity (4) gets doubled, it is greater than the
455                 // max capacity. This makes sure that before throwing an exception
456                 // we first attempt to go for a smaller size.
457                 //
458                 new StringBuilder(4, 7).Append ("foo").Append ("bar");
459                 new StringBuilder(4, 6).Append ("foo").Append ("bar");
460                 // this throws ArgumentOutOfRangeException on MS 1.1 SP1
461         }
462
463         [Test]
464         public void CapacityFromString ()
465         {
466                 StringBuilder sb = new StringBuilder ("hola").Append ("lala");
467                 AssertEquals ("#01", "holalala", sb.ToString ());
468         }
469
470         [Test]
471         public void ReplaceWithLargerString ()
472         {
473                 StringBuilder sb = new StringBuilder ("ABCDE");
474                 AssertEquals ("#1", "ABCDE", sb.ToString ());
475                 sb.Replace ("ABC", "abcaa", 0, 3);
476                 AssertEquals ("#2", "abcaaDE", sb.ToString ());
477         }
478
479         [Test]
480         public void SetLength ()
481         {
482                 StringBuilder sb = new StringBuilder ("Text");
483                 AssertEquals ("#1", 4, sb.Length);
484                 AssertEquals ("#2", "Text", sb.ToString ());
485                 sb.Length = 8;
486                 AssertEquals ("#3", 8, sb.Length);
487                 AssertEquals ("#4", "Text\0\0\0\0", sb.ToString ());
488         }
489 }
490
491 }