StringBuilderTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 ([email protected])
  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. using NUnit.Framework;
  23. using System.Text;
  24. using System;
  25. namespace MonoTests.System.Text {
  26. [TestFixture]
  27. public class StringBuilderTest : Assertion {
  28. private StringBuilder sb;
  29. public void TestConstructor1()
  30. {
  31. // check the parameterless ctor
  32. sb = new StringBuilder();
  33. AssertEquals(String.Empty, sb.ToString());
  34. AssertEquals(0, sb.Length);
  35. AssertEquals(16, sb.Capacity);
  36. }
  37. public void TestConstructor2()
  38. {
  39. // check ctor that specifies the capacity
  40. sb = new StringBuilder(10);
  41. AssertEquals(String.Empty, sb.ToString());
  42. AssertEquals(0, sb.Length);
  43. // check that capacity is not less than default
  44. AssertEquals(10, sb.Capacity);
  45. sb = new StringBuilder(42);
  46. AssertEquals(String.Empty, sb.ToString());
  47. AssertEquals(0, sb.Length);
  48. // check that capacity is set
  49. AssertEquals(42, sb.Capacity);
  50. }
  51. public void TestConstructor3() {
  52. // check ctor that specifies the capacity & maxCapacity
  53. sb = new StringBuilder(444, 1234);
  54. AssertEquals(String.Empty, sb.ToString());
  55. AssertEquals(0, sb.Length);
  56. AssertEquals(444, sb.Capacity);
  57. AssertEquals(1234, sb.MaxCapacity);
  58. }
  59. public void TestConstructor4()
  60. {
  61. // check for exception in ctor that specifies the capacity & maxCapacity
  62. try {
  63. sb = new StringBuilder(9999, 15);
  64. }
  65. catch (ArgumentOutOfRangeException) {
  66. return;
  67. }
  68. // if we didn't catch an exception, then we have a problem Houston.
  69. NUnit.Framework.Assertion.Fail("Capacity exeeds MaxCapacity");
  70. }
  71. public void TestConstructor5() {
  72. String someString = null;
  73. sb = new StringBuilder(someString);
  74. AssertEquals("Should be empty string", String.Empty, sb.ToString());
  75. }
  76. public void TestConstructor6() {
  77. // check for exception in ctor that prevents startIndex less than zero
  78. try {
  79. String someString = "someString";
  80. sb = new StringBuilder(someString, -1, 3, 18);
  81. }
  82. catch (ArgumentOutOfRangeException) {
  83. return;
  84. }
  85. // if we didn't catch an exception, then we have a problem Houston.
  86. NUnit.Framework.Assertion.Fail("StartIndex not allowed to be less than zero.");
  87. }
  88. public void TestConstructor7() {
  89. // check for exception in ctor that prevents length less than zero
  90. try {
  91. String someString = "someString";
  92. sb = new StringBuilder(someString, 2, -222, 18);
  93. }
  94. catch (ArgumentOutOfRangeException) {
  95. return;
  96. }
  97. // if we didn't catch an exception, then we have a problem Houston.
  98. NUnit.Framework.Assertion.Fail("Length not allowed to be less than zero.");
  99. }
  100. public void TestConstructor8() {
  101. // check for exception in ctor that ensures substring is contained in given string
  102. // check that startIndex is not too big
  103. try {
  104. String someString = "someString";
  105. sb = new StringBuilder(someString, 10000, 4, 18);
  106. }
  107. catch (ArgumentOutOfRangeException) {
  108. return;
  109. }
  110. // if we didn't catch an exception, then we have a problem Houston.
  111. NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");
  112. }
  113. public void TestConstructor9() {
  114. // check for exception in ctor that ensures substring is contained in given string
  115. // check that length doesn't go beyond end of string
  116. try {
  117. String someString = "someString";
  118. sb = new StringBuilder(someString, 4, 4000, 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. public void TestConstructor10() {
  127. // check that substring is taken properly and made into a StringBuilder
  128. String someString = "someString";
  129. sb = new StringBuilder(someString, 4, 6, 18);
  130. string expected = "String";
  131. AssertEquals( expected, sb.ToString());
  132. }
  133. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  134. public void TestConstructor11 () {
  135. new StringBuilder (-1);
  136. }
  137. public void TestAppend() {
  138. StringBuilder sb = new StringBuilder( "Foo" );
  139. sb.Append( "Two" );
  140. string expected = "FooTwo";
  141. AssertEquals( expected, sb.ToString() );
  142. }
  143. public void TestInsert() {
  144. StringBuilder sb = new StringBuilder();
  145. AssertEquals( String.Empty, sb.ToString() );
  146. /* Test empty StringBuilder conforms to spec */
  147. sb.Insert( 0, "Foo" ); /* Test insert at start of empty string */
  148. AssertEquals( "Foo", sb.ToString() );
  149. sb.Insert( 1, "!!" ); /* Test insert not at start of string */
  150. AssertEquals( "F!!oo", sb.ToString() );
  151. sb.Insert( 5, ".." ); /* Test insert at end of string */
  152. AssertEquals( "F!!oo..", sb.ToString() );
  153. sb.Insert( 0, 1234 ); /* Test insert of a number (at start of string) */
  154. // FIX: Why does this test fail?
  155. //AssertEquals( "1234F!!oo..", sb.ToString() );
  156. sb.Insert( 5, 1.5 ); /* Test insert of a decimal (and end of string) */
  157. // FIX: Why does this test fail?
  158. //AssertEquals( "1234F1.5!!oo..", sb.ToString() );
  159. sb.Insert( 4, 'A' ); /* Test char insert in middle of string */
  160. // FIX: Why does this test fail?
  161. //AssertEquals( "1234AF1.5!!oo..", sb.ToString() );
  162. }
  163. public void TestReplace() {
  164. StringBuilder sb = new StringBuilder( "Foobarbaz" );
  165. sb.Replace( "bar", "!!!" ); /* Test same length replace in middle of string */
  166. AssertEquals( "Foo!!!baz", sb.ToString() );
  167. sb.Replace( "Foo", "ABcD" ); /* Test longer replace at start of string */
  168. AssertEquals( "ABcD!!!baz", sb.ToString() );
  169. sb.Replace( "baz", "00" ); /* Test shorter replace at end of string */
  170. AssertEquals( "ABcD!!!00", sb.ToString() );
  171. sb.Replace( sb.ToString(), null ); /* Test string clear as in spec */
  172. AssertEquals( String.Empty, sb.ToString() );
  173. /* | 10 20 30
  174. /* |0123456789012345678901234567890| */
  175. sb.Append( "abc this is testing abc the abc abc partial replace abc" );
  176. sb.Replace( "abc", "!!!", 0, 31 ); /* Partial replace at start of string */
  177. AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
  178. sb.Replace( "testing", "", 0, 15 ); /* Test replace across boundary */
  179. AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
  180. sb.Replace( "!!!", "" ); /* Test replace with empty string */
  181. AssertEquals(" this is testing the abc partial replace abc", sb.ToString() );
  182. }
  183. public void TestAppendFormat() {
  184. }
  185. [Test]
  186. public void MoreReplace ()
  187. {
  188. StringBuilder sb = new StringBuilder ();
  189. sb.Append ("First");
  190. sb.Append ("Second");
  191. sb.Append ("Third");
  192. sb.Replace ("Second", "Gone", 2, sb.Length-2);
  193. AssertEquals ("#01", "FirstGoneThird", sb.ToString ());
  194. sb.Length = 0;
  195. sb.Append ("This, is, a, list");
  196. sb.Replace (",", "comma-separated", 11, sb.Length-11);
  197. AssertEquals ("#02", "This, is, acomma-separated list", sb.ToString ());
  198. }
  199. [Test]
  200. public void Insert0 ()
  201. {
  202. StringBuilder sb = new StringBuilder();
  203. sb.Append("testtesttest");
  204. sb.Insert(0, '^');
  205. AssertEquals ("#01", "^testtesttest", sb.ToString ());
  206. }
  207. [Test]
  208. public void AppendToEmpty ()
  209. {
  210. StringBuilder sb = new StringBuilder();
  211. char [] ca = new char [] { 'c' };
  212. sb.Append (ca);
  213. AssertEquals ("#01", "c", sb.ToString ());
  214. }
  215. [Test]
  216. public void TestRemove ()
  217. {
  218. StringBuilder b = new StringBuilder ();
  219. b.Append ("Hello, I am a StringBuilder");
  220. b.Remove (0, 7); // Should remove "Hello, "
  221. AssertEquals ("#01", "I am a StringBuilder", b.ToString ());
  222. }
  223. [Test]
  224. public void Insert1 ()
  225. {
  226. StringBuilder sb = new StringBuilder();
  227. sb.Insert(0, "aa");
  228. AssertEquals ("#01", "aa", sb.ToString ());
  229. char [] charArr = new char [] { 'b', 'c', 'd' };
  230. sb.Insert(1, charArr, 1, 1);
  231. AssertEquals ("#02", "aca", sb.ToString ());
  232. sb.Insert (1, null, 0, 0);
  233. AssertEquals ("#03", "aca", sb.ToString ());
  234. try {
  235. sb.Insert (1, null, 1, 1);
  236. Assertion.Fail ("#04: Value must not be null if startIndex and charCount > 0");
  237. } catch (ArgumentNullException) {}
  238. }
  239. [Test]
  240. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  241. public void Constructor_StartIndexOverflow ()
  242. {
  243. new StringBuilder ("Mono", Int32.MaxValue, 1, 0);
  244. }
  245. [Test]
  246. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  247. public void Constructor_LengthOverflow ()
  248. {
  249. new StringBuilder ("Mono", 1, Int32.MaxValue, 0);
  250. }
  251. [Test]
  252. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  253. public void ToString_StartIndexOverflow ()
  254. {
  255. StringBuilder sb = new StringBuilder ("Mono");
  256. sb.ToString (Int32.MaxValue, 1);
  257. }
  258. [Test]
  259. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  260. public void ToString_LengthOverflow ()
  261. {
  262. StringBuilder sb = new StringBuilder ("Mono");
  263. sb.ToString (1, Int32.MaxValue);
  264. }
  265. [Test]
  266. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  267. public void Remove_StartIndexOverflow ()
  268. {
  269. StringBuilder sb = new StringBuilder ("Mono");
  270. sb.Remove (Int32.MaxValue, 1);
  271. }
  272. [Test]
  273. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  274. public void Remove_LengthOverflow ()
  275. {
  276. StringBuilder sb = new StringBuilder ("Mono");
  277. sb.Remove (1, Int32.MaxValue);
  278. }
  279. [Test]
  280. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  281. public void ReplaceChar_StartIndexOverflow ()
  282. {
  283. StringBuilder sb = new StringBuilder ("Mono");
  284. sb.Replace ('o', '0', Int32.MaxValue, 1);
  285. }
  286. [Test]
  287. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  288. public void ReplaceChar_CountOverflow ()
  289. {
  290. StringBuilder sb = new StringBuilder ("Mono");
  291. sb.Replace ('0', '0', 1, Int32.MaxValue);
  292. }
  293. [Test]
  294. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  295. public void ReplaceString_StartIndexOverflow ()
  296. {
  297. StringBuilder sb = new StringBuilder ("Mono");
  298. sb.Replace ("o", "0", Int32.MaxValue, 1);
  299. }
  300. [Test]
  301. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  302. public void ReplaceString_CountOverflow ()
  303. {
  304. StringBuilder sb = new StringBuilder ("Mono");
  305. sb.Replace ("o", "0", 1, Int32.MaxValue);
  306. }
  307. [Test]
  308. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  309. public void AppendCharArray_StartIndexOverflow ()
  310. {
  311. StringBuilder sb = new StringBuilder ("Mono");
  312. sb.Append (new char[2], Int32.MaxValue, 1);
  313. }
  314. [Test]
  315. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  316. public void AppendCharArray_CharCountOverflow ()
  317. {
  318. StringBuilder sb = new StringBuilder ("Mono");
  319. sb.Append (new char[2], 1, Int32.MaxValue);
  320. }
  321. [Test]
  322. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  323. public void AppendString_StartIndexOverflow ()
  324. {
  325. StringBuilder sb = new StringBuilder ("Mono");
  326. sb.Append ("!", Int32.MaxValue, 1);
  327. }
  328. [Test]
  329. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  330. public void AppendString_CountOverflow ()
  331. {
  332. StringBuilder sb = new StringBuilder ("Mono");
  333. sb.Append ("!", 1, Int32.MaxValue);
  334. }
  335. [Test]
  336. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  337. public void InsertCharArray_StartIndexOverflow ()
  338. {
  339. StringBuilder sb = new StringBuilder ("Mono");
  340. sb.Insert (0, new char[2], Int32.MaxValue, 1);
  341. }
  342. [Test]
  343. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  344. public void InsertCharArray_CharCountOverflow ()
  345. {
  346. StringBuilder sb = new StringBuilder ("Mono");
  347. sb.Insert (0, new char[2], 1, Int32.MaxValue);
  348. }
  349. [Test]
  350. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  351. public void MaxCapacity_Overflow1 ()
  352. {
  353. StringBuilder sb = new StringBuilder (2, 3);
  354. sb.Append ("Mono");
  355. }
  356. [Test]
  357. public void MaxCapacity_Overflow2 ()
  358. {
  359. StringBuilder sb = new StringBuilder (2, 3);
  360. try {
  361. sb.Append ("Mono");
  362. } catch (ArgumentOutOfRangeException) {
  363. }
  364. AssertEquals (2, sb.Capacity);
  365. AssertEquals (3, sb.MaxCapacity);
  366. }
  367. [Test]
  368. public void MaxCapacity_Overflow3 ()
  369. {
  370. //
  371. // When the capacity (4) gets doubled, it is greater than the
  372. // max capacity. This makes sure that before throwing an exception
  373. // we first attempt to go for a smaller size.
  374. //
  375. new StringBuilder(4, 7).Append ("foo").Append ("bar");
  376. new StringBuilder(4, 6).Append ("foo").Append ("bar");
  377. }
  378. [Test]
  379. public void CapacityFromString ()
  380. {
  381. StringBuilder sb = new StringBuilder ("hola").Append ("lala");
  382. AssertEquals ("#01", "holalala", sb.ToString ());
  383. }
  384. [Test]
  385. public void ReplaceWithLargerString ()
  386. {
  387. StringBuilder sb = new StringBuilder ("ABCDE");
  388. AssertEquals ("#1", "ABCDE", sb.ToString ());
  389. sb.Replace ("ABC", "abcaa", 0, 3);
  390. AssertEquals ("#2", "abcaaDE", sb.ToString ());
  391. }
  392. }
  393. }