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