StringBuilderTest.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // StringBuilderTest.dll - NUnit Test Cases for the System.Text.StringBuilder class
  3. //
  4. // Author: Marcin Szczepanski ([email protected])
  5. //
  6. // NOTES: I've also run all these tests against the MS implementation of
  7. // System.Text.StringBuilder to confirm that they return the same results
  8. // and they do.
  9. //
  10. // TODO: Add tests for the AppendFormat methods once the AppendFormat methods
  11. // are implemented in the StringBuilder class itself
  12. //
  13. // TODO: Potentially add more variations on Insert / Append tests for all the
  14. // possible types. I don't really think that's necessary as they all
  15. // pretty much just do .ToString().ToCharArray() and then use the Append / Insert
  16. // CharArray function. The ToString() bit for each type should be in the unit
  17. // tests for those types, and the unit test for ToCharArray should be in the
  18. // string test type. If someone wants to add those tests here for completness
  19. // (and some double checking) then feel free :)
  20. //
  21. using NUnit.Framework;
  22. using System.Text;
  23. using System;
  24. namespace MonoTests.System.Text {
  25. public class StringBuilderTest : TestCase {
  26. private StringBuilder sb;
  27. public void TestConstructor1()
  28. {
  29. // check the parameterless ctor
  30. sb = new StringBuilder();
  31. AssertEquals(String.Empty, sb.ToString());
  32. AssertEquals(0, sb.Length);
  33. AssertEquals(16, sb.Capacity);
  34. }
  35. public void TestConstructor2()
  36. {
  37. // check ctor that specifies the capacity
  38. sb = new StringBuilder(10);
  39. AssertEquals(String.Empty, sb.ToString());
  40. AssertEquals(0, sb.Length);
  41. // check that capacity is not less than default
  42. AssertEquals(10, sb.Capacity);
  43. sb = new StringBuilder(42);
  44. AssertEquals(String.Empty, sb.ToString());
  45. AssertEquals(0, sb.Length);
  46. // check that capacity is set
  47. AssertEquals(42, sb.Capacity);
  48. }
  49. public void TestConstructor3() {
  50. // check ctor that specifies the capacity & maxCapacity
  51. sb = new StringBuilder(444, 1234);
  52. AssertEquals(String.Empty, sb.ToString());
  53. AssertEquals(0, sb.Length);
  54. AssertEquals(444, sb.Capacity);
  55. AssertEquals(1234, sb.MaxCapacity);
  56. }
  57. public void TestConstructor4()
  58. {
  59. // check for exception in ctor that specifies the capacity & maxCapacity
  60. try {
  61. sb = new StringBuilder(9999, 15);
  62. }
  63. catch (ArgumentOutOfRangeException) {
  64. return;
  65. }
  66. // if we didn't catch an exception, then we have a problem Houston.
  67. NUnit.Framework.Assertion.Fail("Capacity exeeds MaxCapacity");
  68. }
  69. public void TestConstructor5() {
  70. String someString = null;
  71. sb = new StringBuilder(someString);
  72. AssertEquals("Should be empty string", String.Empty, sb.ToString());
  73. }
  74. public void TestConstructor6() {
  75. // check for exception in ctor that prevents startIndex less than zero
  76. try {
  77. String someString = "someString";
  78. sb = new StringBuilder(someString, -1, 3, 18);
  79. }
  80. catch (ArgumentOutOfRangeException) {
  81. return;
  82. }
  83. // if we didn't catch an exception, then we have a problem Houston.
  84. NUnit.Framework.Assertion.Fail("StartIndex not allowed to be less than zero.");
  85. }
  86. public void TestConstructor7() {
  87. // check for exception in ctor that prevents length less than zero
  88. try {
  89. String someString = "someString";
  90. sb = new StringBuilder(someString, 2, -222, 18);
  91. }
  92. catch (ArgumentOutOfRangeException) {
  93. return;
  94. }
  95. // if we didn't catch an exception, then we have a problem Houston.
  96. NUnit.Framework.Assertion.Fail("Length not allowed to be less than zero.");
  97. }
  98. public void TestConstructor8() {
  99. // check for exception in ctor that ensures substring is contained in given string
  100. // check that startIndex is not too big
  101. try {
  102. String someString = "someString";
  103. sb = new StringBuilder(someString, 10000, 4, 18);
  104. }
  105. catch (ArgumentOutOfRangeException) {
  106. return;
  107. }
  108. // if we didn't catch an exception, then we have a problem Houston.
  109. NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");
  110. }
  111. public void TestConstructor9() {
  112. // check for exception in ctor that ensures substring is contained in given string
  113. // check that length doesn't go beyond end of string
  114. try {
  115. String someString = "someString";
  116. sb = new StringBuilder(someString, 4, 4000, 18);
  117. }
  118. catch (ArgumentOutOfRangeException) {
  119. return;
  120. }
  121. // if we didn't catch an exception, then we have a problem Houston.
  122. NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");
  123. }
  124. public void TestConstructor10() {
  125. // check that substring is taken properly and made into a StringBuilder
  126. String someString = "someString";
  127. sb = new StringBuilder(someString, 4, 6, 18);
  128. string expected = "String";
  129. AssertEquals( expected, sb.ToString());
  130. }
  131. public void TestAppend() {
  132. StringBuilder sb = new StringBuilder( "Foo" );
  133. sb.Append( "Two" );
  134. string expected = "FooTwo";
  135. AssertEquals( expected, sb.ToString() );
  136. }
  137. public void TestInsert() {
  138. StringBuilder sb = new StringBuilder();
  139. AssertEquals( String.Empty, sb.ToString() );
  140. /* Test empty StringBuilder conforms to spec */
  141. sb.Insert( 0, "Foo" ); /* Test insert at start of empty string */
  142. AssertEquals( "Foo", sb.ToString() );
  143. sb.Insert( 1, "!!" ); /* Test insert not at start of string */
  144. AssertEquals( "F!!oo", sb.ToString() );
  145. sb.Insert( 5, ".." ); /* Test insert at end of string */
  146. AssertEquals( "F!!oo..", sb.ToString() );
  147. sb.Insert( 0, 1234 ); /* Test insert of a number (at start of string) */
  148. // FIX: Why does this test fail?
  149. //AssertEquals( "1234F!!oo..", sb.ToString() );
  150. sb.Insert( 5, 1.5 ); /* Test insert of a decimal (and end of string) */
  151. // FIX: Why does this test fail?
  152. //AssertEquals( "1234F1.5!!oo..", sb.ToString() );
  153. sb.Insert( 4, 'A' ); /* Test char insert in middle of string */
  154. // FIX: Why does this test fail?
  155. //AssertEquals( "1234AF1.5!!oo..", sb.ToString() );
  156. }
  157. public void TestReplace() {
  158. StringBuilder sb = new StringBuilder( "Foobarbaz" );
  159. sb.Replace( "bar", "!!!" ); /* Test same length replace in middle of string */
  160. AssertEquals( "Foo!!!baz", sb.ToString() );
  161. sb.Replace( "Foo", "ABcD" ); /* Test longer replace at start of string */
  162. AssertEquals( "ABcD!!!baz", sb.ToString() );
  163. sb.Replace( "baz", "00" ); /* Test shorter replace at end of string */
  164. AssertEquals( "ABcD!!!00", sb.ToString() );
  165. sb.Replace( sb.ToString(), null ); /* Test string clear as in spec */
  166. AssertEquals( String.Empty, sb.ToString() );
  167. /* | 10 20 30
  168. /* |0123456789012345678901234567890| */
  169. sb.Append( "abc this is testing abc the abc abc partial replace abc" );
  170. sb.Replace( "abc", "!!!", 0, 31 ); /* Partial replace at start of string */
  171. AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
  172. sb.Replace( "testing", "", 0, 15 ); /* Test replace across boundary */
  173. AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
  174. sb.Replace( "!!!", "" ); /* Test replace with empty string */
  175. AssertEquals(" this is testing the abc partial replace abc", sb.ToString() );
  176. }
  177. public void TestAppendFormat() {
  178. }
  179. }
  180. }