StringBuilderTest.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. public class StringBuilderTest : TestCase {
  27. private StringBuilder sb;
  28. public void TestConstructor1()
  29. {
  30. // check the parameterless ctor
  31. sb = new StringBuilder();
  32. AssertEquals(String.Empty, sb.ToString());
  33. AssertEquals(0, sb.Length);
  34. AssertEquals(16, sb.Capacity);
  35. }
  36. public void TestConstructor2()
  37. {
  38. // check ctor that specifies the capacity
  39. sb = new StringBuilder(10);
  40. AssertEquals(String.Empty, sb.ToString());
  41. AssertEquals(0, sb.Length);
  42. // check that capacity is not less than default
  43. AssertEquals(10, sb.Capacity);
  44. sb = new StringBuilder(42);
  45. AssertEquals(String.Empty, sb.ToString());
  46. AssertEquals(0, sb.Length);
  47. // check that capacity is set
  48. AssertEquals(42, sb.Capacity);
  49. }
  50. public void TestConstructor3() {
  51. // check ctor that specifies the capacity & maxCapacity
  52. sb = new StringBuilder(444, 1234);
  53. AssertEquals(String.Empty, sb.ToString());
  54. AssertEquals(0, sb.Length);
  55. AssertEquals(444, sb.Capacity);
  56. AssertEquals(1234, sb.MaxCapacity);
  57. }
  58. public void TestConstructor4()
  59. {
  60. // check for exception in ctor that specifies the capacity & maxCapacity
  61. try {
  62. sb = new StringBuilder(9999, 15);
  63. }
  64. catch (ArgumentOutOfRangeException) {
  65. return;
  66. }
  67. // if we didn't catch an exception, then we have a problem Houston.
  68. NUnit.Framework.Assertion.Fail("Capacity exeeds MaxCapacity");
  69. }
  70. public void TestConstructor5() {
  71. String someString = null;
  72. sb = new StringBuilder(someString);
  73. AssertEquals("Should be empty string", String.Empty, sb.ToString());
  74. }
  75. public void TestConstructor6() {
  76. // check for exception in ctor that prevents startIndex less than zero
  77. try {
  78. String someString = "someString";
  79. sb = new StringBuilder(someString, -1, 3, 18);
  80. }
  81. catch (ArgumentOutOfRangeException) {
  82. return;
  83. }
  84. // if we didn't catch an exception, then we have a problem Houston.
  85. NUnit.Framework.Assertion.Fail("StartIndex not allowed to be less than zero.");
  86. }
  87. public void TestConstructor7() {
  88. // check for exception in ctor that prevents length less than zero
  89. try {
  90. String someString = "someString";
  91. sb = new StringBuilder(someString, 2, -222, 18);
  92. }
  93. catch (ArgumentOutOfRangeException) {
  94. return;
  95. }
  96. // if we didn't catch an exception, then we have a problem Houston.
  97. NUnit.Framework.Assertion.Fail("Length not allowed to be less than zero.");
  98. }
  99. public void TestConstructor8() {
  100. // check for exception in ctor that ensures substring is contained in given string
  101. // check that startIndex is not too big
  102. try {
  103. String someString = "someString";
  104. sb = new StringBuilder(someString, 10000, 4, 18);
  105. }
  106. catch (ArgumentOutOfRangeException) {
  107. return;
  108. }
  109. // if we didn't catch an exception, then we have a problem Houston.
  110. NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");
  111. }
  112. public void TestConstructor9() {
  113. // check for exception in ctor that ensures substring is contained in given string
  114. // check that length doesn't go beyond end of string
  115. try {
  116. String someString = "someString";
  117. sb = new StringBuilder(someString, 4, 4000, 18);
  118. }
  119. catch (ArgumentOutOfRangeException) {
  120. return;
  121. }
  122. // if we didn't catch an exception, then we have a problem Houston.
  123. NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");
  124. }
  125. public void TestConstructor10() {
  126. // check that substring is taken properly and made into a StringBuilder
  127. String someString = "someString";
  128. sb = new StringBuilder(someString, 4, 6, 18);
  129. string expected = "String";
  130. AssertEquals( expected, sb.ToString());
  131. }
  132. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  133. public void TestConstructor11 () {
  134. new StringBuilder (-1);
  135. }
  136. public void TestAppend() {
  137. StringBuilder sb = new StringBuilder( "Foo" );
  138. sb.Append( "Two" );
  139. string expected = "FooTwo";
  140. AssertEquals( expected, sb.ToString() );
  141. }
  142. public void TestInsert() {
  143. StringBuilder sb = new StringBuilder();
  144. AssertEquals( String.Empty, sb.ToString() );
  145. /* Test empty StringBuilder conforms to spec */
  146. sb.Insert( 0, "Foo" ); /* Test insert at start of empty string */
  147. AssertEquals( "Foo", sb.ToString() );
  148. sb.Insert( 1, "!!" ); /* Test insert not at start of string */
  149. AssertEquals( "F!!oo", sb.ToString() );
  150. sb.Insert( 5, ".." ); /* Test insert at end of string */
  151. AssertEquals( "F!!oo..", sb.ToString() );
  152. sb.Insert( 0, 1234 ); /* Test insert of a number (at start of string) */
  153. // FIX: Why does this test fail?
  154. //AssertEquals( "1234F!!oo..", sb.ToString() );
  155. sb.Insert( 5, 1.5 ); /* Test insert of a decimal (and end of string) */
  156. // FIX: Why does this test fail?
  157. //AssertEquals( "1234F1.5!!oo..", sb.ToString() );
  158. sb.Insert( 4, 'A' ); /* Test char insert in middle of string */
  159. // FIX: Why does this test fail?
  160. //AssertEquals( "1234AF1.5!!oo..", sb.ToString() );
  161. }
  162. public void TestReplace() {
  163. StringBuilder sb = new StringBuilder( "Foobarbaz" );
  164. sb.Replace( "bar", "!!!" ); /* Test same length replace in middle of string */
  165. AssertEquals( "Foo!!!baz", sb.ToString() );
  166. sb.Replace( "Foo", "ABcD" ); /* Test longer replace at start of string */
  167. AssertEquals( "ABcD!!!baz", sb.ToString() );
  168. sb.Replace( "baz", "00" ); /* Test shorter replace at end of string */
  169. AssertEquals( "ABcD!!!00", sb.ToString() );
  170. sb.Replace( sb.ToString(), null ); /* Test string clear as in spec */
  171. AssertEquals( String.Empty, sb.ToString() );
  172. /* | 10 20 30
  173. /* |0123456789012345678901234567890| */
  174. sb.Append( "abc this is testing abc the abc abc partial replace abc" );
  175. sb.Replace( "abc", "!!!", 0, 31 ); /* Partial replace at start of string */
  176. AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
  177. sb.Replace( "testing", "", 0, 15 ); /* Test replace across boundary */
  178. AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
  179. sb.Replace( "!!!", "" ); /* Test replace with empty string */
  180. AssertEquals(" this is testing the abc partial replace abc", sb.ToString() );
  181. }
  182. public void TestAppendFormat() {
  183. }
  184. }
  185. }