1 // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 // StringBuilderTest.dll - NUnit Test Cases for the System.Text.StringBuilder class
5 // Author: Marcin Szczepanski (marcins@zipworld.com.au)
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
11 // TODO: Add tests for the AppendFormat methods once the AppendFormat methods
12 // are implemented in the StringBuilder class itself
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 :)
23 using NUnit.Framework;
27 namespace MonoTests.System.Text {
30 public class StringBuilderTest : Assertion {
32 private StringBuilder sb;
34 public void TestConstructor1()
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);
43 public void TestConstructor2()
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);
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);
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);
68 public void TestConstructor4()
70 // check for exception in ctor that specifies the capacity & maxCapacity
72 sb = new StringBuilder(9999, 15);
74 catch (ArgumentOutOfRangeException) {
77 // if we didn't catch an exception, then we have a problem Houston.
78 NUnit.Framework.Assertion.Fail("Capacity exeeds MaxCapacity");
81 public void TestConstructor5() {
82 String someString = null;
83 sb = new StringBuilder(someString);
84 AssertEquals("Should be empty string", String.Empty, sb.ToString());
87 public void TestConstructor6() {
88 // check for exception in ctor that prevents startIndex less than zero
90 String someString = "someString";
91 sb = new StringBuilder(someString, -1, 3, 18);
93 catch (ArgumentOutOfRangeException) {
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.");
100 public void TestConstructor7() {
101 // check for exception in ctor that prevents length less than zero
103 String someString = "someString";
104 sb = new StringBuilder(someString, 2, -222, 18);
106 catch (ArgumentOutOfRangeException) {
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.");
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
117 String someString = "someString";
118 sb = new StringBuilder(someString, 10000, 4, 18);
120 catch (ArgumentOutOfRangeException) {
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.");
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
131 String someString = "someString";
132 sb = new StringBuilder(someString, 4, 4000, 18);
134 catch (ArgumentOutOfRangeException) {
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.");
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());
149 [ExpectedException(typeof(ArgumentOutOfRangeException))]
150 public void TestConstructor11 () {
151 new StringBuilder (-1);
154 public void TestAppend() {
155 StringBuilder sb = new StringBuilder( "Foo" );
157 string expected = "FooTwo";
158 AssertEquals( expected, sb.ToString() );
161 public void TestInsert() {
162 StringBuilder sb = new StringBuilder();
164 AssertEquals( String.Empty, sb.ToString() );
165 /* Test empty StringBuilder conforms to spec */
167 sb.Insert( 0, "Foo" ); /* Test insert at start of empty string */
169 AssertEquals( "Foo", sb.ToString() );
171 sb.Insert( 1, "!!" ); /* Test insert not at start of string */
173 AssertEquals( "F!!oo", sb.ToString() );
175 sb.Insert( 5, ".." ); /* Test insert at end of string */
177 AssertEquals( "F!!oo..", sb.ToString() );
179 sb.Insert( 0, 1234 ); /* Test insert of a number (at start of string) */
181 // FIX: Why does this test fail?
182 //AssertEquals( "1234F!!oo..", sb.ToString() );
184 sb.Insert( 5, 1.5 ); /* Test insert of a decimal (and end of string) */
186 // FIX: Why does this test fail?
187 //AssertEquals( "1234F1.5!!oo..", sb.ToString() );
189 sb.Insert( 4, 'A' ); /* Test char insert in middle of string */
191 // FIX: Why does this test fail?
192 //AssertEquals( "1234AF1.5!!oo..", sb.ToString() );
196 public void TestReplace() {
197 StringBuilder sb = new StringBuilder( "Foobarbaz" );
199 sb.Replace( "bar", "!!!" ); /* Test same length replace in middle of string */
201 AssertEquals( "Foo!!!baz", sb.ToString() );
203 sb.Replace( "Foo", "ABcD" ); /* Test longer replace at start of string */
205 AssertEquals( "ABcD!!!baz", sb.ToString() );
207 sb.Replace( "baz", "00" ); /* Test shorter replace at end of string */
209 AssertEquals( "ABcD!!!00", sb.ToString() );
211 sb.Replace( sb.ToString(), null ); /* Test string clear as in spec */
213 AssertEquals( String.Empty, sb.ToString() );
216 /* |0123456789012345678901234567890| */
217 sb.Append( "abc this is testing abc the abc abc partial replace abc" );
219 sb.Replace( "abc", "!!!", 0, 31 ); /* Partial replace at start of string */
221 AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
223 sb.Replace( "testing", "", 0, 15 ); /* Test replace across boundary */
225 AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
227 sb.Replace( "!!!", "" ); /* Test replace with empty string */
229 AssertEquals(" this is testing the abc partial replace abc", sb.ToString() );
232 public void TestAppendFormat() {
236 public void MoreReplace ()
238 StringBuilder sb = new StringBuilder ();
240 sb.Append ("Second");
242 sb.Replace ("Second", "Gone", 2, sb.Length-2);
243 AssertEquals ("#01", "FirstGoneThird", sb.ToString ());
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 ());
252 public void Insert0 ()
254 StringBuilder sb = new StringBuilder();
255 sb.Append("testtesttest");
257 AssertEquals ("#01", "^testtesttest", sb.ToString ());
261 public void AppendToEmpty ()
263 StringBuilder sb = new StringBuilder();
264 char [] ca = new char [] { 'c' };
266 AssertEquals ("#01", "c", sb.ToString ());
271 public void TestRemove ()
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 ());
280 public void Insert1 ()
282 StringBuilder sb = new StringBuilder();
284 AssertEquals ("#01", "aa", sb.ToString ());
286 char [] charArr = new char [] { 'b', 'c', 'd' };
287 sb.Insert(1, charArr, 1, 1);
288 AssertEquals ("#02", "aca", sb.ToString ());
290 sb.Insert (1, null, 0, 0);
291 AssertEquals ("#03", "aca", sb.ToString ());
294 sb.Insert (1, null, 1, 1);
295 Assertion.Fail ("#04: Value must not be null if startIndex and charCount > 0");
296 } catch (ArgumentNullException) {}
300 [ExpectedException (typeof (ArgumentOutOfRangeException))]
301 public void Constructor_StartIndexOverflow ()
303 new StringBuilder ("Mono", Int32.MaxValue, 1, 0);
307 [ExpectedException (typeof (ArgumentOutOfRangeException))]
308 public void Constructor_LengthOverflow ()
310 new StringBuilder ("Mono", 1, Int32.MaxValue, 0);
314 [ExpectedException (typeof (ArgumentOutOfRangeException))]
315 public void ToString_StartIndexOverflow ()
317 StringBuilder sb = new StringBuilder ("Mono");
318 sb.ToString (Int32.MaxValue, 1);
322 [ExpectedException (typeof (ArgumentOutOfRangeException))]
323 public void ToString_LengthOverflow ()
325 StringBuilder sb = new StringBuilder ("Mono");
326 sb.ToString (1, Int32.MaxValue);
330 [ExpectedException (typeof (ArgumentOutOfRangeException))]
331 public void Remove_StartIndexOverflow ()
333 StringBuilder sb = new StringBuilder ("Mono");
334 sb.Remove (Int32.MaxValue, 1);
338 [ExpectedException (typeof (ArgumentOutOfRangeException))]
339 public void Remove_LengthOverflow ()
341 StringBuilder sb = new StringBuilder ("Mono");
342 sb.Remove (1, Int32.MaxValue);
346 [ExpectedException (typeof (ArgumentOutOfRangeException))]
347 public void ReplaceChar_StartIndexOverflow ()
349 StringBuilder sb = new StringBuilder ("Mono");
350 sb.Replace ('o', '0', Int32.MaxValue, 1);
354 [ExpectedException (typeof (ArgumentOutOfRangeException))]
355 public void ReplaceChar_CountOverflow ()
357 StringBuilder sb = new StringBuilder ("Mono");
358 sb.Replace ('0', '0', 1, Int32.MaxValue);
362 [ExpectedException (typeof (ArgumentOutOfRangeException))]
363 public void ReplaceString_StartIndexOverflow ()
365 StringBuilder sb = new StringBuilder ("Mono");
366 sb.Replace ("o", "0", Int32.MaxValue, 1);
370 [ExpectedException (typeof (ArgumentOutOfRangeException))]
371 public void ReplaceString_CountOverflow ()
373 StringBuilder sb = new StringBuilder ("Mono");
374 sb.Replace ("o", "0", 1, Int32.MaxValue);
378 [ExpectedException (typeof (ArgumentOutOfRangeException))]
379 public void AppendCharArray_StartIndexOverflow ()
381 StringBuilder sb = new StringBuilder ("Mono");
382 sb.Append (new char[2], Int32.MaxValue, 1);
386 [ExpectedException (typeof (ArgumentOutOfRangeException))]
387 public void AppendCharArray_CharCountOverflow ()
389 StringBuilder sb = new StringBuilder ("Mono");
390 sb.Append (new char[2], 1, Int32.MaxValue);
394 [ExpectedException (typeof (ArgumentOutOfRangeException))]
395 public void AppendString_StartIndexOverflow ()
397 StringBuilder sb = new StringBuilder ("Mono");
398 sb.Append ("!", Int32.MaxValue, 1);
402 [ExpectedException (typeof (ArgumentOutOfRangeException))]
403 public void AppendString_CountOverflow ()
405 StringBuilder sb = new StringBuilder ("Mono");
406 sb.Append ("!", 1, Int32.MaxValue);
410 [ExpectedException (typeof (ArgumentOutOfRangeException))]
411 public void InsertCharArray_StartIndexOverflow ()
413 StringBuilder sb = new StringBuilder ("Mono");
414 sb.Insert (0, new char[2], Int32.MaxValue, 1);
418 [ExpectedException (typeof (ArgumentOutOfRangeException))]
419 public void InsertCharArray_CharCountOverflow ()
421 StringBuilder sb = new StringBuilder ("Mono");
422 sb.Insert (0, new char[2], 1, Int32.MaxValue);
426 [ExpectedException (typeof (ArgumentOutOfRangeException))]
427 public void MaxCapacity_Overflow1 ()
429 StringBuilder sb = new StringBuilder (2, 3);
434 public void MaxCapacity_Overflow2 ()
436 StringBuilder sb = new StringBuilder (2, 3);
439 } catch (ArgumentOutOfRangeException) {
442 AssertEquals (2, sb.Capacity);
443 AssertEquals (3, sb.MaxCapacity);
448 [ExpectedException (typeof (ArgumentOutOfRangeException))]
449 [Category ("NotWorking")] // Mono follows 2.0 behaviour in this case
451 public void MaxCapacity_Overflow3 ()
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.
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
464 public void CapacityFromString ()
466 StringBuilder sb = new StringBuilder ("hola").Append ("lala");
467 AssertEquals ("#01", "holalala", sb.ToString ());
471 public void ReplaceWithLargerString ()
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 ());
480 public void SetLength ()
482 StringBuilder sb = new StringBuilder ("Text");
483 AssertEquals ("#1", 4, sb.Length);
484 AssertEquals ("#2", "Text", sb.ToString ());
486 AssertEquals ("#3", 8, sb.Length);
487 AssertEquals ("#4", "Text\0\0\0\0", sb.ToString ());