2003-05-10 Gonzalo Paniagua Javier <gonzalo@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         public class StringBuilderTest : TestCase {\r
30 \r
31                 private StringBuilder sb;\r
32 \r
33                 public void TestConstructor1() \r
34                 {\r
35                         // check the parameterless ctor\r
36                         sb = new StringBuilder();\r
37                         AssertEquals(String.Empty, sb.ToString());\r
38                         AssertEquals(0, sb.Length);\r
39                         AssertEquals(16, sb.Capacity);\r
40                 }\r
41 \r
42                 public void TestConstructor2() \r
43                 {\r
44                         // check ctor that specifies the capacity\r
45                         sb = new StringBuilder(10);\r
46                         AssertEquals(String.Empty, sb.ToString());\r
47                         AssertEquals(0, sb.Length);\r
48                         // check that capacity is not less than default\r
49                         AssertEquals(10, sb.Capacity);\r
50 \r
51                         sb = new StringBuilder(42);\r
52                         AssertEquals(String.Empty, sb.ToString());\r
53                         AssertEquals(0, sb.Length);\r
54                         // check that capacity is set\r
55                         AssertEquals(42, sb.Capacity);\r
56                 }\r
57                 \r
58                 public void TestConstructor3() {\r
59                         // check ctor that specifies the capacity & maxCapacity\r
60                         sb = new StringBuilder(444, 1234);\r
61                         AssertEquals(String.Empty, sb.ToString());\r
62                         AssertEquals(0, sb.Length);\r
63                         AssertEquals(444, sb.Capacity);\r
64                         AssertEquals(1234, sb.MaxCapacity);\r
65                 }\r
66 \r
67                 public void TestConstructor4() \r
68                 {\r
69                         // check for exception in ctor that specifies the capacity & maxCapacity\r
70                         try {\r
71                                 sb = new StringBuilder(9999, 15);\r
72                         }\r
73                         catch (ArgumentOutOfRangeException) {\r
74                                 return;\r
75                         }\r
76                         // if we didn't catch an exception, then we have a problem Houston.\r
77                         NUnit.Framework.Assertion.Fail("Capacity exeeds MaxCapacity");\r
78                 }\r
79 \r
80         public void TestConstructor5() {\r
81                 String someString = null;\r
82                 sb = new StringBuilder(someString);\r
83                 AssertEquals("Should be empty string", String.Empty, sb.ToString());\r
84         }\r
85 \r
86         public void TestConstructor6() {\r
87                 // check for exception in ctor that prevents startIndex less than zero\r
88                 try {\r
89                         String someString = "someString";\r
90                         sb = new StringBuilder(someString, -1, 3, 18);\r
91                 }\r
92                 catch (ArgumentOutOfRangeException) {\r
93                         return;\r
94                 }\r
95                 // if we didn't catch an exception, then we have a problem Houston.\r
96                 NUnit.Framework.Assertion.Fail("StartIndex not allowed to be less than zero.");\r
97         }\r
98 \r
99         public void TestConstructor7() {\r
100                 // check for exception in ctor that prevents length less than zero\r
101                 try {\r
102                         String someString = "someString";\r
103                         sb = new StringBuilder(someString, 2, -222, 18);\r
104                 }\r
105                 catch (ArgumentOutOfRangeException) {\r
106                         return;\r
107                 }\r
108                 // if we didn't catch an exception, then we have a problem Houston.\r
109                 NUnit.Framework.Assertion.Fail("Length not allowed to be less than zero.");\r
110         }\r
111 \r
112         public void TestConstructor8() {\r
113                 // check for exception in ctor that ensures substring is contained in given string\r
114                 // check that startIndex is not too big\r
115                 try {\r
116                         String someString = "someString";\r
117                         sb = new StringBuilder(someString, 10000, 4, 18);\r
118                 }\r
119                 catch (ArgumentOutOfRangeException) {\r
120                         return;\r
121                 }\r
122                 // if we didn't catch an exception, then we have a problem Houston.\r
123                 NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");\r
124         }\r
125 \r
126         public void TestConstructor9() {\r
127                 // check for exception in ctor that ensures substring is contained in given string\r
128                 // check that length doesn't go beyond end of string\r
129                 try {\r
130                         String someString = "someString";\r
131                         sb = new StringBuilder(someString, 4, 4000, 18);\r
132                 }\r
133                 catch (ArgumentOutOfRangeException) {\r
134                         return;\r
135                 }\r
136                 // if we didn't catch an exception, then we have a problem Houston.\r
137                 NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");\r
138         }\r
139 \r
140         public void TestConstructor10() {\r
141                 // check that substring is taken properly and made into a StringBuilder\r
142                 String someString = "someString";\r
143                 sb = new StringBuilder(someString, 4, 6, 18);\r
144                 string expected = "String";\r
145                 AssertEquals( expected, sb.ToString());\r
146         }\r
147 \r
148         [ExpectedException(typeof(ArgumentOutOfRangeException))]\r
149         public void TestConstructor11 () {\r
150                 new StringBuilder (-1);\r
151         }\r
152                 \r
153         public void TestAppend() {\r
154                 StringBuilder sb = new StringBuilder( "Foo" );\r
155                 sb.Append( "Two" );\r
156                 string expected = "FooTwo";\r
157                 AssertEquals( expected, sb.ToString() );\r
158         }\r
159 \r
160         public void TestInsert() {\r
161                 StringBuilder sb = new StringBuilder();\r
162 \r
163                 AssertEquals( String.Empty, sb.ToString() ); \r
164                         /* Test empty StringBuilder conforms to spec */\r
165 \r
166                 sb.Insert( 0, "Foo" ); /* Test insert at start of empty string */\r
167 \r
168                 AssertEquals( "Foo", sb.ToString() );\r
169 \r
170                 sb.Insert( 1, "!!" ); /* Test insert not at start of string */\r
171 \r
172                 AssertEquals( "F!!oo", sb.ToString() );\r
173 \r
174                 sb.Insert( 5, ".." ); /* Test insert at end of string */\r
175 \r
176                 AssertEquals( "F!!oo..", sb.ToString() );\r
177         \r
178                 sb.Insert( 0, 1234 ); /* Test insert of a number (at start of string) */\r
179                 \r
180                                 // FIX: Why does this test fail?\r
181                 //AssertEquals( "1234F!!oo..", sb.ToString() );\r
182                 \r
183                 sb.Insert( 5, 1.5 ); /* Test insert of a decimal (and end of string) */\r
184                 \r
185                                 // FIX: Why does this test fail?\r
186                                 //AssertEquals( "1234F1.5!!oo..", sb.ToString() );\r
187 \r
188                 sb.Insert( 4, 'A' ); /* Test char insert in middle of string */\r
189 \r
190                                 // FIX: Why does this test fail?\r
191                                 //AssertEquals( "1234AF1.5!!oo..", sb.ToString() );\r
192 \r
193         }\r
194 \r
195         public void TestReplace() {\r
196                 StringBuilder sb = new StringBuilder( "Foobarbaz" );\r
197 \r
198                 sb.Replace( "bar", "!!!" );             /* Test same length replace in middle of string */\r
199                 \r
200                 AssertEquals( "Foo!!!baz", sb.ToString() );\r
201 \r
202                 sb.Replace( "Foo", "ABcD" );            /* Test longer replace at start of string */\r
203 \r
204                 AssertEquals( "ABcD!!!baz", sb.ToString() );\r
205 \r
206                 sb.Replace( "baz", "00" );              /* Test shorter replace at end of string */\r
207                         \r
208                 AssertEquals( "ABcD!!!00", sb.ToString() );\r
209 \r
210                 sb.Replace( sb.ToString(), null );      /* Test string clear as in spec */\r
211 \r
212                 AssertEquals( String.Empty, sb.ToString() );\r
213 \r
214                 /*           |         10        20        30\r
215                 /*         |0123456789012345678901234567890| */\r
216                 sb.Append( "abc this is testing abc the abc abc partial replace abc" );\r
217 \r
218                 sb.Replace( "abc", "!!!", 0, 31 ); /* Partial replace at start of string */\r
219 \r
220                 AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );\r
221 \r
222                 sb.Replace( "testing", "", 0, 15 ); /* Test replace across boundary */\r
223 \r
224                 AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );\r
225 \r
226                 sb.Replace( "!!!", "" ); /* Test replace with empty string */\r
227 \r
228                 AssertEquals(" this is testing  the  abc partial replace abc", sb.ToString() );\r
229         }\r
230 \r
231         public void TestAppendFormat() {\r
232         }\r
233 \r
234         [Test]\r
235         public void MoreReplace ()\r
236         {\r
237                 StringBuilder sb = new StringBuilder ();\r
238                 sb.Append ("First");\r
239                 sb.Append ("Second");\r
240                 sb.Append ("Third");\r
241                 sb.Replace ("Second", "Gone", 2, sb.Length-2);\r
242                 AssertEquals ("#01", "FirstGoneThird", sb.ToString ());\r
243 \r
244                 sb.Length = 0;\r
245                 sb.Append ("This, is, a, list");\r
246                 sb.Replace (",", "comma-separated", 11, sb.Length-11);\r
247                 AssertEquals ("#02", "This, is, acomma-separated list", sb.ToString ());\r
248         }\r
249 }\r
250 \r
251 }\r