StringTest.cs 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. // StringTest.cs - NUnit Test Cases for the System.String class
  2. //
  3. // Jeffrey Stedfast <[email protected]>
  4. // David Brandt <[email protected]>
  5. //
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. //
  8. using NUnit.Framework;
  9. using System;
  10. using System.Globalization;
  11. namespace MonoTests.System
  12. {
  13. public class StringTest : TestCase
  14. {
  15. public StringTest() {}
  16. protected override void SetUp ()
  17. {
  18. }
  19. public void TestLength ()
  20. {
  21. string str = "test string";
  22. AssertEquals("wrong length", 11, str.Length);
  23. }
  24. public void TestCompare ()
  25. {
  26. string lesser = "abc";
  27. string medium = "abcd";
  28. string greater = "xyz";
  29. string caps = "ABC";
  30. AssertEquals(0, String.Compare(null, null));
  31. AssertEquals(1, String.Compare(lesser, null));
  32. Assert (String.Compare (lesser, greater) < 0);
  33. Assert (String.Compare (greater, lesser) > 0);
  34. Assert (String.Compare (lesser, lesser) == 0);
  35. Assert (String.Compare (lesser, medium) < 0);
  36. Assert (String.Compare (lesser, caps, true) == 0);
  37. Assert (String.Compare (lesser, caps, false) != 0);
  38. AssertEquals ("A01", String.Compare ("a", "b"), -1);
  39. AssertEquals ("A02", String.Compare ("b", "a"), 1);
  40. AssertEquals ("A03", String.Compare ("A", "a"), 1);
  41. AssertEquals ("A04", String.Compare ("a", "A"), -1);
  42. // TODO - test with CultureInfo
  43. string needle = "ab";
  44. string haystack = "abbcbacab";
  45. AssertEquals("basic substring check #1", 0,
  46. String.Compare(needle, 0, haystack, 0, 2));
  47. AssertEquals("basic substring check #2", -1,
  48. String.Compare(needle, 0, haystack, 0, 3));
  49. AssertEquals("basic substring check #3", 0,
  50. String.Compare("ab", 0, "ab", 0, 2));
  51. AssertEquals("basic substring check #4", 0,
  52. String.Compare("ab", 0, "ab", 0, 3));
  53. AssertEquals("basic substring check #5", 0,
  54. String.Compare("abc", 0, "ab", 0, 2));
  55. AssertEquals("basic substring check #6", 1,
  56. String.Compare("abc", 0, "ab", 0, 5));
  57. AssertEquals("basic substring check #7", -1,
  58. String.Compare("ab", 0, "abc", 0, 5));
  59. for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
  60. if (i != 7) {
  61. Assert("loop substring check #1/" + i, String.Compare(needle, 0, haystack, i, 2) != 0);
  62. Assert("loop substring check #2/" + i, String.Compare(needle, 0, haystack, i, 3) != 0);
  63. } else {
  64. AssertEquals("loop substring check #3/" + i, 0, String.Compare(needle, 0, haystack, i, 2));
  65. AssertEquals("loop substring check #4/" + i, 0, String.Compare(needle, 0, haystack, i, 3));
  66. }
  67. }
  68. needle = "AB";
  69. AssertEquals("basic substring check #8", 0,
  70. String.Compare(needle, 0, haystack, 0, 2, true));
  71. AssertEquals("basic substring check #9", 1,
  72. String.Compare(needle, 0, haystack, 0, 2, false));
  73. for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
  74. if (i != 7) {
  75. Assert("loop substring check #5/" + i, String.Compare(needle, 0, haystack, i, 2, true) != 0);
  76. Assert("loop substring check #6/" + i, String.Compare(needle, 0, haystack, i, 2, false) != 0);
  77. } else {
  78. AssertEquals("loop substring check #7/" + i, 0, String.Compare(needle, 0, haystack, i, 2, true));
  79. AssertEquals("loop substring check #8/" + i, 1, String.Compare(needle, 0, haystack, i, 2, false));
  80. }
  81. }
  82. // TODO - extended format call with CultureInfo
  83. }
  84. public void TestCompareOrdinal ()
  85. {
  86. string lesser = "abc";
  87. string medium = "abcd";
  88. string greater = "xyz";
  89. AssertEquals(0, String.CompareOrdinal(null, null));
  90. AssertEquals(1, String.CompareOrdinal(lesser, null));
  91. Assert (String.CompareOrdinal (lesser, greater) < 0);
  92. Assert (String.CompareOrdinal (greater, lesser) > 0);
  93. Assert (String.CompareOrdinal (lesser, lesser) == 0);
  94. Assert (String.CompareOrdinal (lesser, medium) < 0);
  95. string needle = "ab";
  96. string haystack = "abbcbacab";
  97. AssertEquals("basic substring check", 0,
  98. String.CompareOrdinal(needle, 0, haystack, 0, 2));
  99. AssertEquals("basic substring miss", -1,
  100. String.CompareOrdinal(needle, 0, haystack, 0, 3));
  101. for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
  102. if (i != 7) {
  103. Assert("loop substring check " + i, String.CompareOrdinal(needle, 0, haystack, i, 2) != 0);
  104. Assert("loop substring check " + i, String.CompareOrdinal(needle, 0, haystack, i, 3) != 0);
  105. } else {
  106. AssertEquals("loop substring check " + i, 0, String.CompareOrdinal(needle, 0, haystack, i, 2));
  107. AssertEquals("loop substring check " + i, 0, String.CompareOrdinal(needle, 0, haystack, i, 3));
  108. }
  109. }
  110. }
  111. public void TestCompareTo ()
  112. {
  113. string lower = "abc";
  114. string greater = "xyz";
  115. string lesser = "abc";
  116. Assert (lower.CompareTo (greater) < 0);
  117. Assert (lower.CompareTo (lower) == 0);
  118. Assert (greater.CompareTo (lesser) > 0);
  119. }
  120. public void TestConcat ()
  121. {
  122. string string1 = "string1";
  123. string string2 = "string2";
  124. string concat = "string1string2";
  125. Assert (String.Concat (string1, string2) == concat);
  126. }
  127. public void TestCopy()
  128. {
  129. string s1 = "original";
  130. string s2 = String.Copy(s1);
  131. AssertEquals("String copy no good", s1, s2);
  132. bool errorThrown = false;
  133. try {
  134. string s = String.Copy(null);
  135. } catch (ArgumentNullException) {
  136. errorThrown = true;
  137. }
  138. Assert("null copy shouldn't be good", errorThrown);
  139. }
  140. public void TestCopyTo()
  141. {
  142. string s1 = "original";
  143. bool errorThrown = false;
  144. try {
  145. s1.CopyTo(0, (char[])null, 0, s1.Length);
  146. } catch (ArgumentNullException) {
  147. errorThrown = true;
  148. }
  149. Assert("null CopyTo shouldn't be good", errorThrown);
  150. char[] c1 = new char[s1.Length];
  151. string s2 = new String(c1);
  152. Assert("char string not bad to start", !s1.Equals(s2));
  153. for (int i = 0; i < s1.Length; i++) {
  154. s1.CopyTo(i, c1, i, 1);
  155. }
  156. s2 = new String(c1);
  157. AssertEquals("char-by-char copy bad", s1, s2);
  158. }
  159. public void TestEndsWith()
  160. {
  161. string s1 = "original";
  162. bool errorThrown = false;
  163. try {
  164. bool huh = s1.EndsWith(null);
  165. } catch (ArgumentNullException) {
  166. errorThrown = true;
  167. }
  168. Assert("null EndsWith shouldn't be good", errorThrown);
  169. Assert("should match", s1.EndsWith("l"));
  170. Assert("should match 2", s1.EndsWith("inal"));
  171. Assert("should fail", !s1.EndsWith("ina"));
  172. }
  173. public void TestEquals()
  174. {
  175. string s1 = "original";
  176. string yes = "original";
  177. object y = yes;
  178. string no = "copy";
  179. string s1s1 = s1 + s1;
  180. Assert("No match for null", !s1.Equals(null));
  181. Assert("Should match object", s1.Equals(y));
  182. Assert("Should match", s1.Equals(yes));
  183. Assert("Shouldn't match", !s1.Equals(no));
  184. Assert("Static nulls should match", String.Equals(null, null));
  185. Assert("Should match", String.Equals(s1, yes));
  186. Assert("Shouldn't match", !String.Equals(s1, no));
  187. AssertEquals ("Equals (object)", false, s1s1.Equals (y));
  188. }
  189. public void TestFormat ()
  190. {
  191. AssertEquals ("Empty format string.", "", String.Format ("", 0));
  192. AssertEquals ("Single argument.", "100", String.Format ("{0}", 100));
  193. AssertEquals ("Single argument, right justified.", "X 37X", String.Format ("X{0,5}X", 37));
  194. AssertEquals ("Single argument, left justified.", "X37 X", String.Format ("X{0,-5}X", 37));
  195. AssertEquals ("Two arguments.", "The 3 wise men.", String.Format ("The {0} wise {1}.", 3, "men"));
  196. AssertEquals ("Three arguments.", "do re me fa so.", String.Format ("{0} re {1} fa {2}.", "do", "me", "so"));
  197. AssertEquals ("Formatted argument.", "###00c0ffee#", String.Format ("###{0:x8}#", 0xc0ffee));
  198. AssertEquals ("Formatted argument, right justified.", "# 033#", String.Format ("#{0,5:x3}#", 0x33));
  199. AssertEquals ("Formatted argument, left justified.", "#033 #", String.Format ("#{0,-5:x3}#", 0x33));
  200. AssertEquals ("Escaped bracket", "typedef struct _MonoObject { ... } MonoObject;", String.Format ("typedef struct _{0} {{ ... }} MonoObject;", "MonoObject"));
  201. AssertEquals ("With Slash", "Could not find file \"a/b\"", String.Format ("Could not find file \"{0}\"", "a/b"));
  202. AssertEquals ("With BackSlash", "Could not find file \"a\\b\"", String.Format ("Could not find file \"{0}\"", "a\\b"));
  203. // TODO test format exceptions
  204. // TODO test argument null exceptions
  205. // This should work, but it doesn't currently.
  206. // I think I broke the spec...
  207. //bool errorThrown = false;
  208. //try {
  209. //string s = String.Format(null, " ");
  210. //} catch (ArgumentNullException) {
  211. //errorThrown = true;
  212. //}
  213. //Assert("error not thrown 1", errorThrown);
  214. //errorThrown = false;
  215. //try {
  216. //string s = String.Format(null, " ", " ");
  217. //} catch (ArgumentNullException) {
  218. //errorThrown = true;
  219. //}
  220. //Assert("error not thrown 2", errorThrown);
  221. //errorThrown = false;
  222. //try {
  223. //string s = String.Format(" ", null);
  224. //} catch (ArgumentNullException) {
  225. //errorThrown = true;
  226. //}
  227. //Assert("error not thrown 3", errorThrown);
  228. }
  229. public void TestGetEnumerator()
  230. {
  231. string s1 = "original";
  232. char[] c1 = new char[s1.Length];
  233. string s2 = new String(c1);
  234. Assert("pre-enumerated string should not match", !s1.Equals(s2));
  235. CharEnumerator en = s1.GetEnumerator();
  236. AssertNotNull("null enumerator", en);
  237. for (int i = 0; i < s1.Length; i++) {
  238. en.MoveNext();
  239. c1[i] = en.Current;
  240. }
  241. s2 = new String(c1);
  242. AssertEquals("enumerated string should match", s1, s2);
  243. }
  244. public void TestGetHashCode()
  245. {
  246. string s1 = "original";
  247. // TODO - weak test, currently. Just verifies determinicity.
  248. AssertEquals("same string, same hash code",
  249. s1.GetHashCode(), s1.GetHashCode());
  250. }
  251. public void TestGetType() {
  252. string s1 = "original";
  253. AssertEquals("String type", "System.String", s1.GetType().ToString());
  254. }
  255. public void TestGetTypeCode() {
  256. string s1 = "original";
  257. Assert(s1.GetTypeCode().Equals(TypeCode.String));
  258. }
  259. public void TestIndexOf() {
  260. string s1 = "original";
  261. bool errorThrown = false;
  262. try {
  263. int i = s1.IndexOf('q', s1.Length + 1);
  264. } catch (ArgumentOutOfRangeException) {
  265. errorThrown = true;
  266. }
  267. Assert("out of range error for char", errorThrown);
  268. errorThrown = false;
  269. try {
  270. int i = s1.IndexOf('q', s1.Length + 1, 1);
  271. } catch (ArgumentOutOfRangeException) {
  272. errorThrown = true;
  273. }
  274. Assert("out of range error for char", errorThrown);
  275. errorThrown = false;
  276. try {
  277. int i = s1.IndexOf("huh", s1.Length + 1);
  278. } catch (ArgumentOutOfRangeException) {
  279. errorThrown = true;
  280. }
  281. Assert("out of range for string", errorThrown);
  282. errorThrown = false;
  283. try {
  284. int i = s1.IndexOf("huh", s1.Length + 1, 3);
  285. } catch (ArgumentOutOfRangeException) {
  286. errorThrown = true;
  287. }
  288. Assert("out of range for string", errorThrown);
  289. errorThrown = false;
  290. try {
  291. int i = s1.IndexOf(null);
  292. } catch (ArgumentNullException) {
  293. errorThrown = true;
  294. }
  295. Assert("null string error", errorThrown);
  296. errorThrown = false;
  297. try {
  298. int i = s1.IndexOf(null, 0);
  299. } catch (ArgumentNullException) {
  300. errorThrown = true;
  301. }
  302. Assert("null string error", errorThrown);
  303. errorThrown = false;
  304. try {
  305. int i = s1.IndexOf(null, 0, 1);
  306. } catch (ArgumentNullException) {
  307. errorThrown = true;
  308. }
  309. Assert("null string error", errorThrown);
  310. AssertEquals("basic char index", 1, s1.IndexOf('r'));
  311. AssertEquals("basic char index", 2, s1.IndexOf('i'));
  312. AssertEquals("basic char index - no", -1, s1.IndexOf('q'));
  313. AssertEquals("basic string index", 1, s1.IndexOf("rig"));
  314. AssertEquals("basic string index", 2, s1.IndexOf("i"));
  315. AssertEquals("basic string index - no", -1, s1.IndexOf("rag"));
  316. AssertEquals("stepped char index", 1, s1.IndexOf('r', 1));
  317. AssertEquals("stepped char index", 2, s1.IndexOf('i', 1));
  318. AssertEquals("stepped char index", 4, s1.IndexOf('i', 3));
  319. AssertEquals("stepped char index", -1, s1.IndexOf('i', 5));
  320. AssertEquals("stepped char index", -1, s1.IndexOf('l', s1.Length));
  321. AssertEquals("stepped limited char index",
  322. 1, s1.IndexOf('r', 1, 1));
  323. AssertEquals("stepped limited char index",
  324. -1, s1.IndexOf('r', 0, 1));
  325. AssertEquals("stepped limited char index",
  326. 2, s1.IndexOf('i', 1, 3));
  327. AssertEquals("stepped limited char index",
  328. 4, s1.IndexOf('i', 3, 3));
  329. AssertEquals("stepped limited char index",
  330. -1, s1.IndexOf('i', 5, 3));
  331. s1 = "original original";
  332. AssertEquals("stepped string index 1",
  333. 0, s1.IndexOf("original", 0));
  334. AssertEquals("stepped string index 2",
  335. 9, s1.IndexOf("original", 1));
  336. AssertEquals("stepped string index 3",
  337. -1, s1.IndexOf("original", 10));
  338. AssertEquals("stepped limited string index 1",
  339. 1, s1.IndexOf("rig", 0, 5));
  340. AssertEquals("stepped limited string index 2",
  341. -1, s1.IndexOf("rig", 0, 3));
  342. AssertEquals("stepped limited string index 3",
  343. 10, s1.IndexOf("rig", 2, 15));
  344. AssertEquals("stepped limited string index 4",
  345. -1, s1.IndexOf("rig", 2, 3));
  346. }
  347. public void TestIndexOfAny() {
  348. string s1 = "abcdefghijklm";
  349. bool errorThrown = false;
  350. try {
  351. int i = s1.IndexOfAny(null);
  352. } catch (ArgumentNullException) {
  353. errorThrown = true;
  354. }
  355. Assert("null char[] error", errorThrown);
  356. errorThrown = false;
  357. try {
  358. int i = s1.IndexOfAny(null, 0);
  359. } catch (ArgumentNullException) {
  360. errorThrown = true;
  361. }
  362. Assert("null char[] error", errorThrown);
  363. errorThrown = false;
  364. try {
  365. int i = s1.IndexOfAny(null, 0, 1);
  366. } catch (ArgumentNullException) {
  367. errorThrown = true;
  368. }
  369. Assert("null char[] error", errorThrown);
  370. char[] c1 = {'a', 'e', 'i', 'o', 'u'};
  371. AssertEquals("first vowel", 0, s1.IndexOfAny(c1));
  372. AssertEquals("second vowel", 4, s1.IndexOfAny(c1, 1));
  373. AssertEquals("out of vowels", -1, s1.IndexOfAny(c1, 9));
  374. AssertEquals("second vowel in range",
  375. 4, s1.IndexOfAny(c1, 1, 4));
  376. AssertEquals("second vowel out of range",
  377. -1, s1.IndexOfAny(c1, 1, 3));
  378. errorThrown = false;
  379. try {
  380. int i = s1.IndexOfAny(c1, s1.Length+1);
  381. } catch (ArgumentOutOfRangeException) {
  382. errorThrown = true;
  383. }
  384. Assert("Out of range error", errorThrown);
  385. errorThrown = false;
  386. try {
  387. int i = s1.IndexOfAny(c1, s1.Length+1, 1);
  388. } catch (ArgumentOutOfRangeException) {
  389. errorThrown = true;
  390. }
  391. Assert("Out of range error", errorThrown);
  392. }
  393. public void TestInsert() {
  394. string s1 = "original";
  395. bool errorThrown = false;
  396. try {
  397. string result = s1.Insert(0, null);
  398. } catch (ArgumentNullException) {
  399. errorThrown = true;
  400. }
  401. Assert("Null arg error", errorThrown);
  402. errorThrown = false;
  403. try {
  404. string result = s1.Insert(s1.Length+1, "Hi!");
  405. } catch (ArgumentOutOfRangeException) {
  406. errorThrown = true;
  407. }
  408. Assert("Out of range error", errorThrown);
  409. AssertEquals("front insert",
  410. "Hi!original", s1.Insert(0, "Hi!"));
  411. AssertEquals("back insert",
  412. "originalHi!", s1.Insert(s1.Length, "Hi!"));
  413. AssertEquals("middle insert",
  414. "origHi!inal", s1.Insert(4, "Hi!"));
  415. }
  416. public void TestIntern() {
  417. bool errorThrown = false;
  418. try {
  419. string s = String.Intern(null);
  420. } catch (ArgumentNullException) {
  421. errorThrown = true;
  422. }
  423. Assert("null arg error", errorThrown);
  424. string s1 = "original";
  425. AssertEquals("One string's reps are both the same",
  426. String.Intern(s1), String.Intern(s1));
  427. string s2 = "originally";
  428. Assert("Different strings, different reps",
  429. String.Intern(s1) != String.Intern(s2));
  430. }
  431. public void TestIsInterned() {
  432. bool errorThrown = false;
  433. try {
  434. string s = String.IsInterned(null);
  435. } catch (ArgumentNullException) {
  436. errorThrown = true;
  437. }
  438. Assert("null arg error", errorThrown);
  439. // FIXME - it seems like this should work, but no.
  440. // I don't know how it's possible to get a null
  441. // returned from IsInterned.
  442. //Assert("no intern for regular string",
  443. //String.IsInterned("original") == null);
  444. string s1 = "original";
  445. AssertEquals("is interned", s1, String.IsInterned(s1));
  446. }
  447. public void TestJoin() {
  448. bool errorThrown = false;
  449. try {
  450. string s = String.Join(" ", null);
  451. } catch (ArgumentNullException) {
  452. errorThrown = true;
  453. }
  454. Assert("null arg error", errorThrown);
  455. string[] chunks = {"this", "is", "a", "test"};
  456. AssertEquals("Basic join", "this is a test",
  457. String.Join(" ", chunks));
  458. AssertEquals("Basic join", "this.is.a.test",
  459. String.Join(".", chunks));
  460. AssertEquals("Subset join", "is a",
  461. String.Join(" ", chunks, 1, 2));
  462. AssertEquals("Subset join", "is.a",
  463. String.Join(".", chunks, 1, 2));
  464. AssertEquals("Subset join", "is a test",
  465. String.Join(" ", chunks, 1, 3));
  466. errorThrown = false;
  467. try {
  468. string s = String.Join(" ", chunks, 2, 3);
  469. } catch (ArgumentOutOfRangeException) {
  470. errorThrown = true;
  471. }
  472. Assert("out of range error", errorThrown);
  473. }
  474. public void TestLastIndexOf() {
  475. string s1 = "original";
  476. bool errorThrown = false;
  477. try {
  478. int i = s1.LastIndexOf('q', -1);
  479. } catch (ArgumentOutOfRangeException) {
  480. errorThrown = true;
  481. }
  482. Assert("out of range error for char", errorThrown);
  483. errorThrown = false;
  484. try {
  485. int i = s1.LastIndexOf('q', -1, 1);
  486. } catch (ArgumentOutOfRangeException) {
  487. errorThrown = true;
  488. }
  489. Assert("out of range error for char", errorThrown);
  490. errorThrown = false;
  491. try {
  492. int i = s1.LastIndexOf("huh", s1.Length + 1);
  493. } catch (ArgumentOutOfRangeException) {
  494. errorThrown = true;
  495. }
  496. Assert("out of range for string", errorThrown);
  497. errorThrown = false;
  498. try {
  499. int i = s1.LastIndexOf("huh", s1.Length + 1, 3);
  500. } catch (ArgumentOutOfRangeException) {
  501. errorThrown = true;
  502. }
  503. Assert("out of range for string", errorThrown);
  504. errorThrown = false;
  505. try {
  506. int i = s1.LastIndexOf(null);
  507. } catch (ArgumentNullException) {
  508. errorThrown = true;
  509. }
  510. Assert("null string error", errorThrown);
  511. errorThrown = false;
  512. try {
  513. int i = s1.LastIndexOf(null, 0);
  514. } catch (ArgumentNullException) {
  515. errorThrown = true;
  516. }
  517. Assert("null string error", errorThrown);
  518. errorThrown = false;
  519. try {
  520. int i = s1.LastIndexOf(null, 0, 1);
  521. } catch (ArgumentNullException) {
  522. errorThrown = true;
  523. }
  524. Assert("null string error", errorThrown);
  525. AssertEquals("basic char index", 1, s1.LastIndexOf('r'));
  526. AssertEquals("basic char index", 4, s1.LastIndexOf('i'));
  527. AssertEquals("basic char index - no", -1, s1.LastIndexOf('q'));
  528. AssertEquals("basic string index", 1, s1.LastIndexOf("rig"));
  529. AssertEquals("basic string index", 4, s1.LastIndexOf("i"));
  530. AssertEquals("basic string index - no", -1,
  531. s1.LastIndexOf("rag"));
  532. AssertEquals("stepped char index", 1,
  533. s1.LastIndexOf('r', s1.Length-1));
  534. AssertEquals("stepped char index", 4,
  535. s1.LastIndexOf('i', s1.Length-1));
  536. AssertEquals("stepped char index", 2,
  537. s1.LastIndexOf('i', 3));
  538. AssertEquals("stepped char index", -1,
  539. s1.LastIndexOf('i', 1));
  540. AssertEquals("stepped limited char index",
  541. 1, s1.LastIndexOf('r', 1, 1));
  542. AssertEquals("stepped limited char index",
  543. -1, s1.LastIndexOf('r', 0, 1));
  544. AssertEquals("stepped limited char index",
  545. 4, s1.LastIndexOf('i', 6, 3));
  546. AssertEquals("stepped limited char index",
  547. 2, s1.LastIndexOf('i', 3, 3));
  548. AssertEquals("stepped limited char index",
  549. -1, s1.LastIndexOf('i', 1, 2));
  550. s1 = "original original";
  551. AssertEquals("stepped string index #1",
  552. 9, s1.LastIndexOf("original", s1.Length));
  553. AssertEquals("stepped string index #2",
  554. 0, s1.LastIndexOf("original", s1.Length-2));
  555. AssertEquals("stepped string index #3",
  556. -1, s1.LastIndexOf("original", s1.Length-11));
  557. AssertEquals("stepped string index #4",
  558. -1, s1.LastIndexOf("translator", 2));
  559. AssertEquals("stepped limited string index #1",
  560. 10, s1.LastIndexOf("rig", s1.Length-1, 10));
  561. AssertEquals("stepped limited string index #2",
  562. -1, s1.LastIndexOf("rig", s1.Length, 3));
  563. AssertEquals("stepped limited string index #3",
  564. 10, s1.LastIndexOf("rig", s1.Length-2, 15));
  565. AssertEquals("stepped limited string index #4",
  566. -1, s1.LastIndexOf("rig", s1.Length-2, 3));
  567. }
  568. public void TestLastIndexOfAny() {
  569. string s1 = ".bcdefghijklm";
  570. bool errorThrown = false;
  571. try {
  572. int i = s1.LastIndexOfAny(null);
  573. } catch (ArgumentNullException) {
  574. errorThrown = true;
  575. }
  576. Assert("null char[] error", errorThrown);
  577. errorThrown = false;
  578. try {
  579. int i = s1.LastIndexOfAny(null, s1.Length);
  580. } catch (ArgumentNullException) {
  581. errorThrown = true;
  582. }
  583. Assert("null char[] error", errorThrown);
  584. errorThrown = false;
  585. try {
  586. int i = s1.LastIndexOfAny(null, s1.Length, 1);
  587. } catch (ArgumentNullException) {
  588. errorThrown = true;
  589. }
  590. Assert("null char[] error", errorThrown);
  591. char[] c1 = {'a', 'e', 'i', 'o', 'u'};
  592. AssertEquals("first vowel", 8, s1.LastIndexOfAny(c1));
  593. AssertEquals("second vowel", 4, s1.LastIndexOfAny(c1, 7));
  594. AssertEquals("out of vowels", -1, s1.LastIndexOfAny(c1, 3));
  595. AssertEquals("second vowel in range",
  596. 4, s1.LastIndexOfAny(c1, s1.Length-6, 4));
  597. AssertEquals("second vowel out of range",
  598. -1, s1.LastIndexOfAny(c1, s1.Length-6, 3));
  599. errorThrown = false;
  600. try {
  601. int i = s1.LastIndexOfAny(c1, -1);
  602. } catch (ArgumentOutOfRangeException) {
  603. errorThrown = true;
  604. }
  605. Assert("Out of range error", errorThrown);
  606. errorThrown = false;
  607. try {
  608. int i = s1.LastIndexOfAny(c1, -1, 1);
  609. } catch (ArgumentOutOfRangeException) {
  610. errorThrown = true;
  611. }
  612. Assert("Out of range error", errorThrown);
  613. }
  614. public void TestPadLeft() {
  615. string s1 = "Hi!";
  616. bool errorThrown = false;
  617. try {
  618. string s = s1.PadLeft(-1);
  619. } catch (ArgumentException) {
  620. errorThrown = true;
  621. }
  622. Assert("Bad argument error", errorThrown);
  623. AssertEquals("Too little padding",
  624. s1, s1.PadLeft(s1.Length-1));
  625. AssertEquals("Some padding",
  626. " Hi!", s1.PadLeft(5));
  627. }
  628. public void TestPadRight() {
  629. string s1 = "Hi!";
  630. bool errorThrown = false;
  631. try {
  632. string s = s1.PadRight(-1);
  633. } catch (ArgumentException) {
  634. errorThrown = true;
  635. }
  636. Assert("Bad argument error", errorThrown);
  637. AssertEquals("Too little padding",
  638. s1, s1.PadRight(s1.Length-1));
  639. AssertEquals("Some padding",
  640. "Hi! ", s1.PadRight(5));
  641. }
  642. public void TestRemove() {
  643. string s1 = "original";
  644. bool errorThrown = false;
  645. try {
  646. s1.Remove(-1,1);
  647. } catch (ArgumentOutOfRangeException) {
  648. errorThrown = true;
  649. }
  650. Assert("out of range error", errorThrown);
  651. errorThrown = false;
  652. try {
  653. s1.Remove(1,-1);
  654. } catch (ArgumentOutOfRangeException) {
  655. errorThrown = true;
  656. }
  657. Assert("out of range error", errorThrown);
  658. errorThrown = false;
  659. try {
  660. s1.Remove(s1.Length,s1.Length);
  661. } catch (ArgumentOutOfRangeException) {
  662. errorThrown = true;
  663. }
  664. Assert("out of range error", errorThrown);
  665. AssertEquals("basic remove", "oinal",
  666. s1.Remove(1, 3));
  667. }
  668. public void TestReplace() {
  669. string s1 = "original";
  670. AssertEquals("non-hit char", s1, s1.Replace('q','s'));
  671. AssertEquals("single char", "oxiginal", s1.Replace('r', 'x'));
  672. AssertEquals("double char", "orxgxnal", s1.Replace('i', 'x'));
  673. bool errorThrown = false;
  674. try {
  675. string s = s1.Replace(null, "feh");
  676. } catch (ArgumentNullException) {
  677. errorThrown = true;
  678. }
  679. Assert("should get null arg exception", errorThrown);
  680. AssertEquals("replace as remove", "ornal",
  681. s1.Replace("igi", null));
  682. AssertEquals("non-hit string", s1, s1.Replace("spam", "eggs"));
  683. AssertEquals("single string", "orirumal",
  684. s1.Replace("gin", "rum"));
  685. AssertEquals("double string", "oreigeinal",
  686. s1.Replace("i", "ei"));
  687. AssertEquals ("result longer", ":!:", "::".Replace ("::", ":!:"));
  688. }
  689. public void TestSplit() {
  690. string s1 = "abcdefghijklm";
  691. char[] c1 = {'q', 'r'};
  692. AssertEquals("No splitters", s1, (s1.Split(c1))[0]);
  693. char[] c2 = {'a', 'e', 'i', 'o', 'u'};
  694. string[] chunks = s1.Split(c2);
  695. AssertEquals("First chunk", "", chunks[0]);
  696. AssertEquals("Second chunk", "bcd", chunks[1]);
  697. AssertEquals("Third chunk", "fgh", chunks[2]);
  698. AssertEquals("Fourth chunk", "jklm", chunks[3]);
  699. {
  700. bool errorThrown = false;
  701. try {
  702. chunks = s1.Split(c2, -1);
  703. } catch (ArgumentOutOfRangeException) {
  704. errorThrown = true;
  705. }
  706. Assert("Split out of range", errorThrown);
  707. }
  708. chunks = s1.Split(c2, 2);
  709. AssertEquals("Limited chunk", 2, chunks.Length);
  710. AssertEquals("First limited chunk", "", chunks[0]);
  711. AssertEquals("Second limited chunk", "bcdefghijklm", chunks[1]);
  712. string s3 = "1.0";
  713. char[] c3 = {'.'};
  714. chunks = s3.Split(c3,2);
  715. AssertEquals("1.0 split length", 2, chunks.Length);
  716. AssertEquals("1.0 split first chunk", "1", chunks[0]);
  717. AssertEquals("1.0 split second chunk", "0", chunks[1]);
  718. string s4 = "1.0.0";
  719. char[] c4 = {'.'};
  720. chunks = s4.Split(c4,2);
  721. AssertEquals("1.0.0 split length", 2, chunks.Length);
  722. AssertEquals("1.0.0 split first chunk", "1", chunks[0]);
  723. AssertEquals("1.0.0 split second chunk", "0.0", chunks[1]);
  724. string s5 = ".0.0";
  725. char[] c5 = {'.'};
  726. chunks = s5.Split (c5, 2);
  727. AssertEquals(".0.0 split length", 2, chunks.Length);
  728. AssertEquals(".0.0 split first chunk", "", chunks[0]);
  729. AssertEquals(".0.0 split second chunk", "0.0", chunks[1]);
  730. string s6 = ".0";
  731. char[] c6 = {'.'};
  732. chunks = s6.Split (c6, 2);
  733. AssertEquals(".0 split length", 2, chunks.Length);
  734. AssertEquals(".0 split first chunk", "", chunks[0]);
  735. AssertEquals(".0 split second chunk", "0", chunks[1]);
  736. string s7 = "0.";
  737. char[] c7 = {'.'};
  738. chunks = s7.Split (c7, 2);
  739. AssertEquals("0. split length", 2, chunks.Length);
  740. AssertEquals("0. split first chunk", "0", chunks[0]);
  741. AssertEquals("0. split second chunk", "", chunks[1]);
  742. string s8 = "0.0000";
  743. char[] c8 = {'.'};
  744. chunks = s8.Split (c8, 2);
  745. AssertEquals("0.0000/2 split length", 2, chunks.Length);
  746. AssertEquals("0.0000/2 split first chunk", "0", chunks[0]);
  747. AssertEquals("0.0000/2 split second chunk", "0000", chunks[1]);
  748. chunks = s8.Split (c8, 3);
  749. AssertEquals("0.0000/3 split length", 2, chunks.Length);
  750. AssertEquals("0.0000/3 split first chunk", "0", chunks[0]);
  751. AssertEquals("0.0000/3 split second chunk", "0000", chunks[1]);
  752. chunks = s8.Split (c8, 1);
  753. AssertEquals("0.0000/1 split length", 1, chunks.Length);
  754. AssertEquals("0.0000/1 split first chunk", "0.0000", chunks[0]);
  755. chunks = s1.Split(c2, 1);
  756. AssertEquals("Single split", 1, chunks.Length);
  757. AssertEquals("Single chunk", s1, chunks[0]);
  758. chunks = s1.Split(c2, 0);
  759. AssertEquals("Zero split", 0, chunks.Length);
  760. }
  761. public void TestStartsWith() {
  762. string s1 = "original";
  763. bool errorThrown = false;
  764. try {
  765. bool huh = s1.StartsWith(null);
  766. } catch (ArgumentNullException) {
  767. errorThrown = true;
  768. }
  769. Assert("null StartsWith shouldn't be good", errorThrown);
  770. Assert("should match", s1.StartsWith("o"));
  771. Assert("should match 2", s1.StartsWith("orig"));
  772. Assert("should fail", !s1.StartsWith("rig"));
  773. }
  774. public void TestSubstring() {
  775. string s1 = "original";
  776. bool errorThrown = false;
  777. try {
  778. string s = s1.Substring(s1.Length+1);
  779. } catch (ArgumentOutOfRangeException) {
  780. errorThrown = true;
  781. }
  782. Assert("error not thrown", errorThrown);
  783. errorThrown = false;
  784. try {
  785. string s = s1.Substring(-1);
  786. } catch (ArgumentOutOfRangeException) {
  787. errorThrown = true;
  788. }
  789. Assert("error not thrown", errorThrown);
  790. errorThrown = false;
  791. try {
  792. string s = s1.Substring(1, -1);
  793. } catch (ArgumentOutOfRangeException) {
  794. errorThrown = true;
  795. }
  796. Assert("error not thrown", errorThrown);
  797. errorThrown = false;
  798. try {
  799. string s = s1.Substring(-1, 1);
  800. } catch (ArgumentOutOfRangeException) {
  801. errorThrown = true;
  802. }
  803. Assert("error not thrown", errorThrown);
  804. errorThrown = false;
  805. try {
  806. string s = s1.Substring(s1.Length, 1);
  807. } catch (ArgumentOutOfRangeException) {
  808. errorThrown = true;
  809. }
  810. Assert("error not thrown", errorThrown);
  811. errorThrown = false;
  812. try {
  813. string s = s1.Substring(1, s1.Length);
  814. } catch (ArgumentOutOfRangeException) {
  815. errorThrown = true;
  816. }
  817. Assert("error not thrown", errorThrown);
  818. AssertEquals("basic substring", "inal",
  819. s1.Substring(4));
  820. AssertEquals("midstring", "igin",
  821. s1.Substring(2, 4));
  822. AssertEquals("at end", "",
  823. s1.Substring(s1.Length, 0));
  824. }
  825. public void TestToCharArray() {
  826. string s1 = "original";
  827. char[] c1 = s1.ToCharArray();
  828. AssertEquals("right array size", s1.Length, c1.Length);
  829. AssertEquals("basic char array", s1,
  830. new String(c1));
  831. bool errorThrown = false;
  832. try {
  833. s1.ToCharArray(s1.Length, 1);
  834. } catch (ArgumentOutOfRangeException) {
  835. errorThrown = true;
  836. }
  837. Assert("error not thrown", errorThrown);
  838. errorThrown = false;
  839. try {
  840. s1.ToCharArray(1, s1.Length);
  841. } catch (ArgumentOutOfRangeException) {
  842. errorThrown = true;
  843. }
  844. Assert("error not thrown", errorThrown);
  845. errorThrown = false;
  846. try {
  847. s1.ToCharArray(-1, 1);
  848. } catch (ArgumentOutOfRangeException) {
  849. errorThrown = true;
  850. }
  851. Assert("error not thrown", errorThrown);
  852. errorThrown = false;
  853. try {
  854. s1.ToCharArray(1, -1);
  855. } catch (ArgumentOutOfRangeException) {
  856. errorThrown = true;
  857. }
  858. Assert("error not thrown", errorThrown);
  859. c1 = s1.ToCharArray(0, 3);
  860. AssertEquals("Starting char array", "ori", new String(c1));
  861. }
  862. public void TestToLower() {
  863. string s1 = "OrIgInAl";
  864. AssertEquals("lowercase failed", "original", s1.ToLower());
  865. // TODO - Again, with CultureInfo
  866. }
  867. public void TestToString() {
  868. string s1 = "original";
  869. AssertEquals("ToString failed!", s1, s1.ToString());
  870. }
  871. public void TestToUpper() {
  872. string s1 = "OrIgInAl";
  873. AssertEquals("uppercase failed", "ORIGINAL", s1.ToUpper());
  874. // TODO - Again, with CultureInfo
  875. }
  876. public void TestTrim() {
  877. string s1 = " original\t\n";
  878. AssertEquals("basic trim failed", "original", s1.Trim());
  879. AssertEquals("basic trim failed", "original", s1.Trim(null));
  880. s1 = "original";
  881. AssertEquals("basic trim failed", "original", s1.Trim());
  882. AssertEquals("basic trim failed", "original", s1.Trim(null));
  883. s1 = " \t \n ";
  884. AssertEquals("empty trim failed", "", s1.Trim());
  885. AssertEquals("empty trim failed", "", s1.Trim(null));
  886. s1 = "aaaoriginalbbb";
  887. char[] delims = {'a', 'b'};
  888. AssertEquals("custom trim failed",
  889. "original", s1.Trim(delims));
  890. }
  891. public void TestTrimEnd() {
  892. string s1 = " original\t\n";
  893. AssertEquals("basic TrimEnd failed",
  894. " original", s1.TrimEnd(null));
  895. s1 = " original";
  896. AssertEquals("basic TrimEnd failed",
  897. " original", s1.TrimEnd(null));
  898. s1 = " \t \n \n ";
  899. AssertEquals("empty TrimEnd failed",
  900. "", s1.TrimEnd(null));
  901. s1 = "aaaoriginalbbb";
  902. char[] delims = {'a', 'b'};
  903. AssertEquals("custom TrimEnd failed",
  904. "aaaoriginal", s1.TrimEnd(delims));
  905. }
  906. public void TestTrimStart() {
  907. string s1 = " original\t\n";
  908. AssertEquals("basic TrimStart failed",
  909. "original\t\n", s1.TrimStart(null));
  910. s1 = "original\t\n";
  911. AssertEquals("basic TrimStart failed",
  912. "original\t\n", s1.TrimStart(null));
  913. s1 = " \t \n \n ";
  914. AssertEquals("empty TrimStart failed",
  915. "", s1.TrimStart(null));
  916. s1 = "aaaoriginalbbb";
  917. char[] delims = {'a', 'b'};
  918. AssertEquals("custom TrimStart failed",
  919. "originalbbb", s1.TrimStart(delims));
  920. }
  921. }
  922. }