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