1 // StringTest.cs - NUnit Test Cases for the System.String class
4 // Jeffrey Stedfast <fejj@ximian.com>
5 // David Brandt <bucky@keystreams.com>
6 // Kornel Pal <http://www.kornelpal.hu/>
8 // (C) Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2006 Kornel Pal
10 // Copyright (C) 2006 Novell (http://www.novell.com)
15 using System.Globalization;
16 using System.Reflection;
17 using System.Threading;
19 using NUnit.Framework;
21 namespace MonoTests.System
25 public class StringTest : TestCase
27 private CultureInfo orgCulture;
29 protected override void SetUp ()
31 // save current culture
32 orgCulture = CultureInfo.CurrentCulture;
35 protected override void TearDown ()
37 // restore original culture
38 Thread.CurrentThread.CurrentCulture = orgCulture;
43 [Test] // ctor (Char [])
44 public unsafe void Constructor2 ()
46 AssertEquals ("#1", String.Empty, new String ((char[]) null));
47 AssertEquals ("#2", String.Empty, new String (new Char [0]));
48 AssertEquals ("#3", "A", new String (new Char [1] {'A'}));
52 [Test] // ctor (Char, Int32)
53 public void Constructor4 ()
55 AssertEquals (string.Empty, new String ('A', 0));
56 AssertEquals ("AAA", new String ('A', 3));
59 [Test] // ctor (Char, Int32)
60 public void Constructor4_Count_Negative ()
65 } catch (ArgumentOutOfRangeException ex) {
66 // 'count' must be non-negative
67 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
68 AssertNull ("#3", ex.InnerException);
69 AssertNotNull ("#4", ex.Message);
70 AssertEquals ("#5", "count", ex.ParamName);
74 [Test] // ctor (Char [], Int32, Int32)
75 public void Constructor6 ()
77 char [] arr = new char [3] { 'A', 'B', 'C' };
78 AssertEquals ("#1", "ABC", new String (arr, 0, arr.Length));
79 AssertEquals ("#2", "BC", new String (arr, 1, 2));
80 AssertEquals ("#3", string.Empty, new String (arr, 2, 0));
83 [Test] // ctor (Char [], Int32, Int32)
84 public void Constructor6_Length_Negative ()
86 char [] arr = new char [3] { 'A', 'B', 'C' };
89 new String (arr, 0, -1);
91 } catch (ArgumentOutOfRangeException ex) {
92 // Length cannot be less than zero
93 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
94 AssertNull ("#3", ex.InnerException);
95 AssertNotNull ("#4", ex.Message);
96 AssertEquals ("#5", "length", ex.ParamName);
100 [Test] // ctor (Char [], Int32, Int32)
101 public void Constructor6_Length_Overflow ()
103 char [] arr = new char [3] { 'A', 'B', 'C' };
106 new String (arr, 1, 3);
108 } catch (ArgumentOutOfRangeException ex) {
109 // Index was out of range. Must be non-negative and
110 // less than the size of the collection
111 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
112 AssertNull ("#3", ex.InnerException);
113 AssertNotNull ("#4", ex.Message);
114 AssertEquals ("#5", "startIndex", ex.ParamName);
118 [Test] // ctor (Char [], Int32, Int32)
119 public void Constructor6_StartIndex_Negative ()
121 char [] arr = new char [3] { 'A', 'B', 'C' };
124 new String (arr, -1, 0);
126 } catch (ArgumentOutOfRangeException ex) {
127 // StartIndex cannot be less than zero
128 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
129 AssertNull ("#3", ex.InnerException);
130 AssertNotNull ("#4", ex.Message);
131 AssertEquals ("#5", "startIndex", ex.ParamName);
135 [Test] // ctor (Char [], Int32, Int32)
136 public void Constructor6_Value_Null ()
139 new String ((char []) null, 0, 0);
141 } catch (ArgumentNullException ex) {
142 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
143 AssertNull ("#3", ex.InnerException);
144 AssertNotNull ("#4", ex.Message);
145 AssertEquals ("#5", "value", ex.ParamName);
151 public unsafe void CharPtrConstructor ()
153 AssertEquals ("char*", String.Empty, new String ((char*) null));
154 AssertEquals ("char*,int,int", String.Empty, new String ((char*) null, 0, 0));
158 public unsafe void TestSbytePtrConstructorASCII ()
160 Encoding encoding = Encoding.ASCII;
161 String s = "ASCII*\0";
162 byte[] bytes = encoding.GetBytes (s);
164 fixed (byte* bytePtr = bytes)
165 AssertEquals (s, new String ((sbyte*) bytePtr, 0, bytes.Length, encoding));
169 public unsafe void TestSbytePtrConstructorDefault ()
171 Encoding encoding = Encoding.Default;
172 byte [] bytes = new byte [256];
174 for (int i = 0; i < 255; i++)
175 bytes [i] = (byte) (i + 1);
176 bytes [255] = (byte) 0;
178 // Ensure that bytes are valid for Encoding.Default
179 bytes = encoding.GetBytes (encoding.GetChars (bytes));
180 String s = encoding.GetString(bytes);
182 // Ensure null terminated array
183 bytes [bytes.Length - 1] = (byte) 0;
185 fixed (byte* bytePtr = bytes)
187 AssertEquals (s.Substring (0, s.Length - 1), new String ((sbyte*) bytePtr));
188 AssertEquals (s, new String ((sbyte*) bytePtr, 0, bytes.Length));
189 AssertEquals (s, new String ((sbyte*) bytePtr, 0, bytes.Length, null));
190 AssertEquals (s, new String ((sbyte*) bytePtr, 0, bytes.Length, encoding));
194 [Test] // ctor (SByte*)
195 public unsafe void Constructor3_Value_Null ()
197 AssertEquals (String.Empty, new String ((sbyte*) null));
200 [Test] // ctor (SByte*)
201 public unsafe void Constructor3_Value_Invalid ()
204 new String ((sbyte*) (-1));
206 } catch (ArgumentOutOfRangeException ex) {
207 // Pointer startIndex and length do not refer to a
209 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
210 AssertNull ("#3", ex.InnerException);
211 AssertNotNull ("#4", ex.Message);
212 AssertEquals ("#5", "ptr", ex.ParamName);
216 [Test] // ctor (SByte*, Int32, Int32)
217 public unsafe void Constructor7_Length_Negative ()
220 new String ((sbyte*) null, 0, -1);
222 } catch (ArgumentOutOfRangeException ex) {
223 // Length cannot be less than zero
224 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
225 AssertNull ("#3", ex.InnerException);
226 AssertNotNull ("#4", ex.Message);
227 AssertEquals ("#5", "length", ex.ParamName);
231 [Test] // ctor (SByte*, Int32, Int32)
232 public unsafe void Constructor7_StartIndex_Negative ()
235 new String ((sbyte*) null, -1, 0);
237 } catch (ArgumentOutOfRangeException ex) {
238 // StartIndex cannot be less than zero
239 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
240 AssertNull ("#3", ex.InnerException);
241 AssertNotNull ("#4", ex.Message);
242 AssertEquals ("#5", "startIndex", ex.ParamName);
247 public unsafe void Constructor7_StartIndex_Overflow ()
250 new String ((sbyte*) (-1), 1, 0);
252 } catch (ArgumentOutOfRangeException ex) {
253 // Pointer startIndex and length do not refer to a
255 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
256 AssertNull ("#A3", ex.InnerException);
257 AssertNotNull ("#A4", ex.Message);
258 AssertEquals ("#A5", "startIndex", ex.ParamName);
262 new String ((sbyte*) (-1), 1, 1);
264 } catch (ArgumentOutOfRangeException ex) {
265 // Pointer startIndex and length do not refer to a
267 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
268 AssertNull ("#B3", ex.InnerException);
269 AssertNotNull ("#B4", ex.Message);
270 AssertEquals ("#B5", "startIndex", ex.ParamName);
274 [Test] // ctor (SByte*, Int32, Int32)
275 public unsafe void Constructor7_Value_Invalid ()
278 new String ((sbyte*) (-1), 0, 1);
280 } catch (ArgumentOutOfRangeException ex) {
281 // Pointer startIndex and length do not refer to a
283 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
284 AssertNull ("#3", ex.InnerException);
285 AssertNotNull ("#4", ex.Message);
286 AssertEquals ("#5", "ptr", ex.ParamName);
290 [Test] // ctor (SByte*, Int32, Int32)
291 public unsafe void Constructor7_Value_Null ()
295 new String ((sbyte*) null, 0, 0);
297 } catch (ArgumentNullException ex) {
298 AssertEquals ("#A2", typeof (ArgumentNullException), ex.GetType ());
299 AssertNull ("#A3", ex.InnerException);
300 AssertNotNull ("#A4", ex.Message);
301 AssertEquals ("#A5", "value", ex.ParamName);
304 AssertEquals ("#A", String.Empty, new String ((sbyte*) null, 0, 0));
309 new String ((sbyte*) null, 0, 1);
311 } catch (ArgumentNullException ex) {
312 AssertEquals ("#B2", typeof (ArgumentNullException), ex.GetType ());
313 AssertNull ("#B3", ex.InnerException);
314 AssertNotNull ("#B4", ex.Message);
315 AssertEquals ("#B5", "value", ex.ParamName);
318 AssertEquals ("#B", String.Empty, new String ((sbyte*) null, 0, 1));
323 new String ((sbyte*) null, 1, 0);
325 } catch (ArgumentNullException ex) {
326 AssertEquals ("#C2", typeof (ArgumentNullException), ex.GetType ());
327 AssertNull ("#C3", ex.InnerException);
328 AssertNotNull ("#C4", ex.Message);
329 AssertEquals ("#C5", "value", ex.ParamName);
332 AssertEquals ("#C", String.Empty, new String ((sbyte*) null, 1, 0));
336 [Test] // ctor (SByte*, Int32, Int32, Encoding)
337 public unsafe void Constructor8_Length_Negative ()
340 new String ((sbyte*) null, 0, -1, null);
342 } catch (ArgumentOutOfRangeException ex) {
343 // Length cannot be less than zero
344 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
345 AssertNull ("#A3", ex.InnerException);
346 AssertNotNull ("#A4", ex.Message);
347 AssertEquals ("#A5", "length", ex.ParamName);
351 new String ((sbyte*) null, 0, -1, Encoding.Default);
353 } catch (ArgumentOutOfRangeException ex) {
354 // Non-negative number required
355 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
356 AssertNull ("#B3", ex.InnerException);
357 AssertNotNull ("#B4", ex.Message);
358 AssertEquals ("#B5", "length", ex.ParamName);
362 [Test] // ctor (SByte*, Int32, Int32, Encoding)
363 public unsafe void Constructor8_StartIndex_Negative ()
366 new String ((sbyte*) null, -1, 0, null);
368 } catch (ArgumentOutOfRangeException ex) {
369 // StartIndex cannot be less than zero
370 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
371 AssertNull ("#A3", ex.InnerException);
372 AssertNotNull ("#A4", ex.Message);
373 AssertEquals ("#A5", "startIndex", ex.ParamName);
377 new String ((sbyte*) null, -1, 0, Encoding.Default);
379 } catch (ArgumentOutOfRangeException ex) {
380 // StartIndex cannot be less than zero
381 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
382 AssertNull ("#B3", ex.InnerException);
383 AssertNotNull ("#B4", ex.Message);
384 AssertEquals ("#B5", "startIndex", ex.ParamName);
389 public unsafe void Constructor8_StartIndex_Overflow ()
392 new String ((sbyte*) (-1), 1, 0, null);
394 } catch (ArgumentOutOfRangeException ex) {
395 // Pointer startIndex and length do not refer to a
397 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
398 AssertNull ("#A3", ex.InnerException);
399 AssertNotNull ("#A4", ex.Message);
400 AssertEquals ("#A5", "startIndex", ex.ParamName);
404 new String ((sbyte*) (-1), 1, 1, null);
406 } catch (ArgumentOutOfRangeException ex) {
407 // Pointer startIndex and length do not refer to a
409 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
410 AssertNull ("#B3", ex.InnerException);
411 AssertNotNull ("#B4", ex.Message);
412 AssertEquals ("#B5", "startIndex", ex.ParamName);
416 new String ((sbyte*) (-1), 1, 0, Encoding.Default);
418 } catch (ArgumentOutOfRangeException ex) {
419 // Pointer startIndex and length do not refer to a
421 AssertEquals ("#C2", typeof (ArgumentOutOfRangeException), ex.GetType ());
422 AssertNull ("#C3", ex.InnerException);
423 AssertNotNull ("#C4", ex.Message);
424 AssertEquals ("#C5", "startIndex", ex.ParamName);
428 new String ((sbyte*) (-1), 1, 1, Encoding.Default);
430 } catch (ArgumentOutOfRangeException ex) {
431 // Pointer startIndex and length do not refer to a
433 AssertEquals ("#D2", typeof (ArgumentOutOfRangeException), ex.GetType ());
434 AssertNull ("#D3", ex.InnerException);
435 AssertNotNull ("#D4", ex.Message);
436 AssertEquals ("#D5", "startIndex", ex.ParamName);
440 [Test] // ctor (SByte*, Int32, Int32, Encoding)
441 public unsafe void Constructor8_Value_Invalid ()
444 new String ((sbyte*) (-1), 0, 1, null);
446 } catch (ArgumentOutOfRangeException ex) {
447 // Pointer startIndex and length do not refer to a
449 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
450 AssertNull ("#3", ex.InnerException);
451 AssertNotNull ("#4", ex.Message);
452 AssertEquals ("#5", "ptr", ex.ParamName);
458 [Ignore ("Runtime throws NullReferenceException instead of AccessViolationException")]
459 [ExpectedException (typeof (AccessViolationException))]
461 [ExpectedException (typeof (NullReferenceException))]
463 public unsafe void Constructor8_Value_Invalid2 ()
465 new String ((sbyte*) (-1), 0, 1, Encoding.Default);
468 [Test] // ctor (SByte*, Int32, Int32, Encoding)
469 public unsafe void Constructor8_Value_Null ()
473 new String ((sbyte*) null, 0, 0, null);
475 } catch (ArgumentNullException ex) {
476 AssertEquals ("#A2", typeof (ArgumentNullException), ex.GetType ());
477 AssertNull ("#A3", ex.InnerException);
478 AssertNotNull ("#A4", ex.Message);
479 AssertEquals ("#A5", "value", ex.ParamName);
482 AssertEquals ("#A", String.Empty, new String ((sbyte*) null, 0, 0, null));
487 new String ((sbyte*) null, 0, 1, null);
489 } catch (ArgumentNullException ex) {
490 AssertEquals ("#B2", typeof (ArgumentNullException), ex.GetType ());
491 AssertNull ("#B3", ex.InnerException);
492 AssertNotNull ("#B4", ex.Message);
493 AssertEquals ("#B5", "value", ex.ParamName);
496 AssertEquals ("#B", String.Empty, new String ((sbyte*) null, 0, 1, null));
501 new String ((sbyte*) null, 1, 0, null);
503 } catch (ArgumentNullException ex) {
504 AssertEquals ("#C2", typeof (ArgumentNullException), ex.GetType ());
505 AssertNull ("#C3", ex.InnerException);
506 AssertNotNull ("#C4", ex.Message);
507 AssertEquals ("#C5", "value", ex.ParamName);
510 AssertEquals ("#C", String.Empty, new String ((sbyte*) null, 1, 0, null));
513 AssertEquals ("#D", String.Empty, new String ((sbyte*) null, 0, 0, Encoding.Default));
516 new String ((sbyte*) null, 0, 1, Encoding.Default);
519 } catch (ArgumentOutOfRangeException ex) {
520 // Pointer startIndex and length do not refer to a
522 AssertEquals ("#E2", typeof (ArgumentOutOfRangeException), ex.GetType ());
523 AssertNull ("#E3", ex.InnerException);
524 AssertNotNull ("#E4", ex.Message);
525 //AssertEquals ("#E5", "value", ex.ParamName);
528 } catch (NullReferenceException ex) {
529 AssertEquals ("#E2", typeof (NullReferenceException), ex.GetType ());
530 AssertNull ("#E3", ex.InnerException);
531 AssertNotNull ("#E4", ex.Message);
535 AssertEquals ("#F", String.Empty, new String ((sbyte*) null, 1, 0, Encoding.Default));
540 public void Length ()
542 string str = "test string";
544 AssertEquals("wrong length", 11, str.Length);
550 string s1 = "oRiGiNal";
551 AssertEquals ("#A1", s1, s1.Clone ());
552 AssertSame ("#A2", s1, s1.Clone ());
554 string s2 = new DateTime (2000, 6, 3).ToString ();
555 AssertEquals ("#B1", s2, s2.Clone ());
556 AssertSame ("#B2", s2, s2.Clone ());
559 [Test] // bug #316666
560 public void CompareNotWorking ()
562 AssertEquals ("A03", String.Compare ("A", "a"), 1);
563 AssertEquals ("A04", String.Compare ("a", "A"), -1);
567 public void CompareNotWorking2 ()
569 string needle = "ab";
570 string haystack = "abbcbacab";
571 AssertEquals("basic substring check #9", 0,
572 String.Compare(needle, 0, haystack, 0, 2, false));
573 for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
575 AssertEquals("loop substring check #8/" + i, -1, String.Compare(needle, 0, haystack, i, 2, false));
581 public void Compare ()
583 string lesser = "abc";
584 string medium = "abcd";
585 string greater = "xyz";
588 AssertEquals(0, String.Compare (null, null));
589 AssertEquals(1, String.Compare (lesser, null));
591 Assert (String.Compare (lesser, greater) < 0);
592 Assert (String.Compare (greater, lesser) > 0);
593 Assert (String.Compare (lesser, lesser) == 0);
594 Assert (String.Compare (lesser, medium) < 0);
596 Assert (String.Compare (lesser, caps, true) == 0);
597 Assert (String.Compare (lesser, caps, false) != 0);
598 AssertEquals ("A01", String.Compare ("a", "b"), -1);
599 AssertEquals ("A02", String.Compare ("b", "a"), 1);
602 // TODO - test with CultureInfo
604 string needle = "ab";
605 string haystack = "abbcbacab";
606 AssertEquals("basic substring check #1", 0,
607 String.Compare(needle, 0, haystack, 0, 2));
608 AssertEquals("basic substring check #2", -1,
609 String.Compare(needle, 0, haystack, 0, 3));
610 AssertEquals("basic substring check #3", 0,
611 String.Compare("ab", 0, "ab", 0, 2));
612 AssertEquals("basic substring check #4", 0,
613 String.Compare("ab", 0, "ab", 0, 3));
614 AssertEquals("basic substring check #5", 0,
615 String.Compare("abc", 0, "ab", 0, 2));
616 AssertEquals("basic substring check #6", 1,
617 String.Compare("abc", 0, "ab", 0, 5));
618 AssertEquals("basic substring check #7", -1,
619 String.Compare("ab", 0, "abc", 0, 5));
621 for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
623 Assert("loop substring check #1/" + i, String.Compare(needle, 0, haystack, i, 2) != 0);
624 Assert("loop substring check #2/" + i, String.Compare(needle, 0, haystack, i, 3) != 0);
626 AssertEquals("loop substring check #3/" + i, 0, String.Compare(needle, 0, haystack, i, 2));
627 AssertEquals("loop substring check #4/" + i, 0, String.Compare(needle, 0, haystack, i, 3));
632 AssertEquals("basic substring check #8", 0,
633 String.Compare(needle, 0, haystack, 0, 2, true));
634 for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
636 Assert("loop substring check #5/" + i, String.Compare(needle, 0, haystack, i, 2, true) != 0);
637 Assert("loop substring check #6/" + i, String.Compare(needle, 0, haystack, i, 2, false) != 0);
639 AssertEquals("loop substring check #7/" + i, 0, String.Compare(needle, 0, haystack, i, 2, true));
643 AssertEquals ("Compare with 0 length", 0, String.Compare (needle, 0, haystack, 0, 0));
645 // TODO - extended format call with CultureInfo
649 public void CompareOrdinal ()
651 string lesser = "abc";
652 string medium = "abcd";
653 string greater = "xyz";
655 AssertEquals(0, String.CompareOrdinal (null, null));
656 AssertEquals(1, String.CompareOrdinal (lesser, null));
658 Assert ("#1", String.CompareOrdinal (lesser, greater) < 0);
659 Assert ("#2", String.CompareOrdinal (greater, lesser) > 0);
660 Assert ("#3", String.CompareOrdinal (lesser, lesser) == 0);
661 Assert ("#4", String.CompareOrdinal (lesser, medium) < 0);
663 string needle = "ab";
664 string haystack = "abbcbacab";
665 AssertEquals("basic substring check", 0,
666 String.CompareOrdinal(needle, 0, haystack, 0, 2));
667 AssertEquals("basic substring miss", -1,
668 String.CompareOrdinal(needle, 0, haystack, 0, 3));
669 for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
671 Assert("loop substring check " + i, String.CompareOrdinal(needle, 0, haystack, i, 2) != 0);
672 Assert("loop substring check " + i, String.CompareOrdinal(needle, 0, haystack, i, 3) != 0);
674 AssertEquals("loop substring check " + i, 0, String.CompareOrdinal(needle, 0, haystack, i, 2));
675 AssertEquals("loop substring check " + i, 0, String.CompareOrdinal(needle, 0, haystack, i, 3));
681 public void CompareTo ()
683 string lower = "abc";
684 string greater = "xyz";
685 string lesser = "abc";
687 Assert (lower.CompareTo (greater) < 0);
688 Assert (lower.CompareTo (lower) == 0);
689 Assert (greater.CompareTo (lesser) > 0);
694 public override string ToString ()
701 public void Concat ()
703 string string1 = "string1";
704 string string2 = "string2";
705 string concat = "string1string2";
707 Assert (String.Concat (string1, string2) == concat);
709 AssertEquals (string1, String.Concat (string1, null));
710 AssertEquals (string1, String.Concat (null, string1));
711 AssertEquals (string.Empty, String.Concat (null, null));
713 WeirdToString wts = new WeirdToString ();
714 AssertEquals (string1, String.Concat (string1, wts));
715 AssertEquals (string1, String.Concat (wts, string1));
716 AssertEquals (string.Empty, String.Concat (wts, wts));
717 string [] allstr = new string []{ string1, null, string2, concat };
718 object [] allobj = new object []{ string1, null, string2, concat };
719 string astr = String.Concat (allstr);
720 AssertEquals ("string1string2string1string2", astr);
721 string ostr = String.Concat (allobj);
722 AssertEquals (astr, ostr);
728 string s1 = "original";
729 string s2 = String.Copy(s1);
730 AssertEquals("#1", s1, s2);
731 Assert ("#2", !object.ReferenceEquals (s1, s2));
735 public void Copy_Str_Null ()
738 String.Copy ((string) null);
740 } catch (ArgumentNullException ex) {
741 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
742 AssertNull ("#3", ex.InnerException);
743 AssertNotNull ("#4", ex.Message);
744 AssertEquals ("#5", "str", ex.ParamName);
749 public void CopyTo ()
751 string s1 = "original";
752 char[] c1 = new char[s1.Length];
753 string s2 = new String(c1);
754 Assert("#1", !s1.Equals(s2));
755 for (int i = 0; i < s1.Length; i++) {
756 s1.CopyTo(i, c1, i, 1);
759 AssertEquals("#2", s1, s2);
763 public void CopyTo_Count_Negative ()
765 char [] dest = new char [4];
767 "Mono".CopyTo (0, dest, 0, -1);
769 } catch (ArgumentOutOfRangeException ex) {
770 // Count cannot be less than zero
771 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
772 AssertNull ("#3", ex.InnerException);
773 AssertNotNull ("#4", ex.Message);
774 AssertEquals ("#5", "count", ex.ParamName);
779 public void CopyTo_Count_Overflow ()
781 char [] dest = new char [4];
783 "Mono".CopyTo (0, dest, 0, Int32.MaxValue);
785 } catch (ArgumentOutOfRangeException ex) {
786 // Index and count must refer to a location within the
788 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
789 AssertNull ("#3", ex.InnerException);
790 AssertNotNull ("#4", ex.Message);
791 AssertEquals ("#5", "sourceIndex", ex.ParamName);
796 public void CopyTo_Destination_Null ()
798 string s = "original";
801 s.CopyTo (0, (char []) null, 0, s.Length);
803 } catch (ArgumentNullException ex) {
804 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
805 AssertNull ("#3", ex.InnerException);
806 AssertNotNull ("#4", ex.Message);
807 AssertEquals ("#5", "destination", ex.ParamName);
812 public void CopyTo_DestinationIndex_Negative ()
814 char [] dest = new char [4];
816 "Mono".CopyTo (0, dest, -1, 4);
818 } catch (ArgumentOutOfRangeException ex) {
819 // Index and count must refer to a location within the
821 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
822 AssertNull ("#3", ex.InnerException);
823 AssertNotNull ("#4", ex.Message);
824 AssertEquals ("#5", "destinationIndex", ex.ParamName);
829 public void CopyTo_DestinationIndex_Overflow ()
831 char [] dest = new char [4];
833 "Mono".CopyTo (0, dest, Int32.MaxValue, 4);
835 } catch (ArgumentOutOfRangeException ex) {
836 // Index and count must refer to a location within the
838 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
839 AssertNull ("#3", ex.InnerException);
840 AssertNotNull ("#4", ex.Message);
841 AssertEquals ("#5", "destinationIndex", ex.ParamName);
846 public void CopyTo_SourceIndex_Negative ()
848 char [] dest = new char [4];
850 "Mono".CopyTo (-1, dest, 0, 4);
852 } catch (ArgumentOutOfRangeException ex) {
853 // Index was out of range. Must be non-negative and
854 // less than the size of the collection
855 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
856 AssertNull ("#3", ex.InnerException);
857 AssertNotNull ("#4", ex.Message);
858 AssertEquals ("#5", "sourceIndex", ex.ParamName);
863 public void CopyTo_SourceIndex_Overflow ()
865 char[] dest = new char [4];
867 "Mono".CopyTo (Int32.MaxValue, dest, 0, 4);
869 } catch (ArgumentOutOfRangeException ex) {
870 // Index and count must refer to a location within the
872 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
873 AssertNull ("#3", ex.InnerException);
874 AssertNotNull ("#4", ex.Message);
875 AssertEquals ("#5", "sourceIndex", ex.ParamName);
879 [Test] // EndsWith (String)
880 public void EndsWith1 ()
884 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
887 Assert ("#A1", s.EndsWith ("bC"));
888 Assert ("#A1", !s.EndsWith ("bc"));
889 Assert ("#A2", !s.EndsWith ("dc"));
890 Assert ("#A3", !s.EndsWith ("LAbC"));
891 Assert ("#A4", s.EndsWith (string.Empty));
892 Assert ("#A5", !s.EndsWith ("Ab"));
893 Assert ("#A6", !s.EndsWith ("Abc"));
894 Assert ("#A7", s.EndsWith ("AbC"));
898 Assert ("#B1", s.EndsWith ("ai"));
899 Assert ("#B2", !s.EndsWith ("AI"));
900 Assert ("#B3", !s.EndsWith ("LTai"));
901 Assert ("#B4", s.EndsWith (string.Empty));
902 Assert ("#B5", !s.EndsWith ("Ta"));
903 Assert ("#B6", !s.EndsWith ("tai"));
904 Assert ("#B7", s.EndsWith ("Tai"));
906 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
908 Assert ("#C1", s.EndsWith ("ai"));
909 Assert ("#C2", !s.EndsWith ("AI"));
910 Assert ("#C3", !s.EndsWith ("LTai"));
911 Assert ("#C4", s.EndsWith (string.Empty));
912 Assert ("#C5", !s.EndsWith ("Ta"));
913 Assert ("#C6", !s.EndsWith ("tai"));
914 Assert ("#C7", s.EndsWith ("Tai"));
917 [Test] // EndsWith (String)
918 public void EndsWith1_Value_Null ()
921 "ABC".EndsWith ((string) null);
923 } catch (ArgumentNullException ex) {
924 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
925 AssertNull ("#3", ex.InnerException);
926 AssertNotNull ("#4", ex.Message);
927 AssertEquals ("#5", "value", ex.ParamName);
932 [Test] // EndsWith (String, StringComparison)
933 public void EndsWith2_ComparisonType_Invalid ()
936 "ABC".EndsWith ("C", (StringComparison) 80);
938 } catch (ArgumentException ex) {
939 // The string comparison type passed in is currently
941 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
942 AssertNull ("#3", ex.InnerException);
943 AssertNotNull ("#4", ex.Message);
944 AssertEquals ("#5", "comparisonType", ex.ParamName);
948 [Test] // EndsWith (String, StringComparison)
949 public void EndsWith2_Value_Null ()
952 "ABC".EndsWith ((string) null, StringComparison.CurrentCulture);
954 } catch (ArgumentNullException ex) {
955 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
956 AssertNull ("#3", ex.InnerException);
957 AssertNotNull ("#4", ex.Message);
958 AssertEquals ("#5", "value", ex.ParamName);
962 [Test] // EndsWith (String, Boolean, CultureInfo)
963 public void EndsWith3 ()
969 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
974 Assert ("#A1", !s.EndsWith ("bc", ignorecase, culture));
975 Assert ("#A2", !s.EndsWith ("dc", ignorecase, culture));
976 Assert ("#A3", !s.EndsWith ("LAbC", ignorecase, culture));
977 Assert ("#A4", s.EndsWith (string.Empty, ignorecase, culture));
978 Assert ("#A5", !s.EndsWith ("Ab", ignorecase, culture));
979 Assert ("#A6", !s.EndsWith ("Abc", ignorecase, culture));
980 Assert ("#A7", s.EndsWith ("AbC", ignorecase, culture));
983 Assert ("#B1", s.EndsWith ("bc", ignorecase, culture));
984 Assert ("#B2", !s.EndsWith ("dc", ignorecase, culture));
985 Assert ("#B3", !s.EndsWith ("LAbC", ignorecase, culture));
986 Assert ("#B4", s.EndsWith (string.Empty, ignorecase, culture));
987 Assert ("#B5", !s.EndsWith ("Ab", ignorecase, culture));
988 Assert ("#B6", s.EndsWith ("Abc", ignorecase, culture));
989 Assert ("#B7", s.EndsWith ("AbC", ignorecase, culture));
995 Assert ("#C1", s.EndsWith ("ai", ignorecase, culture));
996 Assert ("#C2", !s.EndsWith ("AI", ignorecase, culture));
997 Assert ("#C3", !s.EndsWith ("LTai", ignorecase, culture));
998 Assert ("#C4", s.EndsWith (string.Empty, ignorecase, culture));
999 Assert ("#C5", !s.EndsWith ("Ta", ignorecase, culture));
1000 Assert ("#C6", !s.EndsWith ("tai", ignorecase, culture));
1001 Assert ("#C7", s.EndsWith ("Tai", ignorecase, culture));
1004 Assert ("#D1", s.EndsWith ("ai", ignorecase, culture));
1005 Assert ("#D2", !s.EndsWith ("AI", ignorecase, culture));
1006 Assert ("#D3", !s.EndsWith ("LTai", ignorecase, culture));
1007 Assert ("#D4", s.EndsWith (string.Empty, ignorecase, culture));
1008 Assert ("#D5", !s.EndsWith ("Ta", ignorecase, culture));
1009 Assert ("#D6", s.EndsWith ("tai", ignorecase, culture));
1010 Assert ("#D7", s.EndsWith ("Tai", ignorecase, culture));
1013 culture = new CultureInfo ("en-US");
1016 Assert ("#E1", s.EndsWith ("ai", ignorecase, culture));
1017 Assert ("#E2", !s.EndsWith ("AI", ignorecase, culture));
1018 Assert ("#E3", !s.EndsWith ("LTai", ignorecase, culture));
1019 Assert ("#E4", s.EndsWith (string.Empty, ignorecase, culture));
1020 Assert ("#E5", !s.EndsWith ("Ta", ignorecase, culture));
1021 Assert ("#E6", !s.EndsWith ("tai", ignorecase, culture));
1022 Assert ("#E7", s.EndsWith ("Tai", ignorecase, culture));
1025 Assert ("#F1", s.EndsWith ("ai", ignorecase, culture));
1026 Assert ("#F2", s.EndsWith ("AI", ignorecase, culture));
1027 Assert ("#F3", !s.EndsWith ("LTai", ignorecase, culture));
1028 Assert ("#F4", s.EndsWith (string.Empty, ignorecase, culture));
1029 Assert ("#F5", !s.EndsWith ("Ta", ignorecase, culture));
1030 Assert ("#F6", s.EndsWith ("tai", ignorecase, culture));
1031 Assert ("#F7", s.EndsWith ("Tai", ignorecase, culture));
1033 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
1037 Assert ("#G1", s.EndsWith ("ai", ignorecase, culture));
1038 Assert ("#G2", !s.EndsWith ("AI", ignorecase, culture));
1039 Assert ("#G3", !s.EndsWith ("LTai", ignorecase, culture));
1040 Assert ("#G4", s.EndsWith (string.Empty, ignorecase, culture));
1041 Assert ("#G5", !s.EndsWith ("Ta", ignorecase, culture));
1042 Assert ("#G6", !s.EndsWith ("tai", ignorecase, culture));
1043 Assert ("#G7", s.EndsWith ("Tai", ignorecase, culture));
1046 Assert ("#H1", s.EndsWith ("ai", ignorecase, culture));
1047 Assert ("#H2", s.EndsWith ("AI", ignorecase, culture));
1048 Assert ("#H3", !s.EndsWith ("LTai", ignorecase, culture));
1049 Assert ("#H4", s.EndsWith (string.Empty, ignorecase, culture));
1050 Assert ("#H5", !s.EndsWith ("Ta", ignorecase, culture));
1051 Assert ("#H6", s.EndsWith ("tai", ignorecase, culture));
1052 Assert ("#H7", s.EndsWith ("Tai", ignorecase, culture));
1055 [Test] // EndsWith (String, Boolean, CultureInfo)
1056 public void EndsWith3_Value_Null ()
1059 "ABC".EndsWith ((string) null, true, null);
1061 } catch (ArgumentNullException ex) {
1062 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1063 AssertNull ("#3", ex.InnerException);
1064 AssertNotNull ("#4", ex.Message);
1065 AssertEquals ("#5", "value", ex.ParamName);
1071 public void TestEquals ()
1073 string s1 = "original";
1074 string yes = "original";
1077 string s1s1 = s1 + s1;
1079 Assert("No match for null", !s1.Equals (null));
1080 Assert("Should match object", s1.Equals (y));
1081 Assert("Should match", s1.Equals (yes));
1082 Assert("Shouldn't match", !s1.Equals (no));
1084 Assert("Static nulls should match", String.Equals (null, null));
1085 Assert("Should match", String.Equals (s1, yes));
1086 Assert("Shouldn't match", !String.Equals (s1, no));
1088 AssertEquals ("Equals (object)", false, s1s1.Equals (y));
1092 public void TestFormat ()
1094 AssertEquals ("Empty format string.", string.Empty, String.Format (string.Empty, 0));
1095 AssertEquals ("Single argument.", "100", String.Format ("{0}", 100));
1096 AssertEquals ("Single argument, right justified.", "X 37X", String.Format ("X{0,5}X", 37));
1097 AssertEquals ("Single argument, left justified.", "X37 X", String.Format ("X{0,-5}X", 37));
1098 AssertEquals ("Whitespace in specifier", " 7d", String.Format ("{0, 4:x}", 125));
1099 AssertEquals ("Two arguments.", "The 3 wise men.", String.Format ("The {0} wise {1}.", 3, "men"));
1100 AssertEquals ("Three arguments.", "do re me fa so.", String.Format ("{0} re {1} fa {2}.", "do", "me", "so"));
1101 AssertEquals ("Formatted argument.", "###00c0ffee#", String.Format ("###{0:x8}#", 0xc0ffee));
1102 AssertEquals ("Formatted argument, right justified.", "# 033#", String.Format ("#{0,5:x3}#", 0x33));
1103 AssertEquals ("Formatted argument, left justified.", "#033 #", String.Format ("#{0,-5:x3}#", 0x33));
1104 AssertEquals ("Escaped bracket", "typedef struct _MonoObject { ... } MonoObject;", String.Format ("typedef struct _{0} {{ ... }} MonoObject;", "MonoObject"));
1105 AssertEquals ("With Slash", "Could not find file \"a/b\"", String.Format ("Could not find file \"{0}\"", "a/b"));
1106 AssertEquals ("With BackSlash", "Could not find file \"a\\b\"", String.Format ("Could not find file \"{0}\"", "a\\b"));
1109 [Test] // Format (String, Object)
1110 public void Format1_Format_Null ()
1113 String.Format (null, 1);
1115 } catch (ArgumentNullException ex) {
1116 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1117 AssertNull ("#3", ex.InnerException);
1118 AssertNotNull ("#4", ex.Message);
1119 AssertEquals ("#5", "format", ex.ParamName);
1123 [Test] // Format (String, Object [])
1124 public void Format2_Format_Null ()
1127 String.Format (null, new object [] { 2 });
1129 } catch (ArgumentNullException ex) {
1130 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1131 AssertNull ("#3", ex.InnerException);
1132 AssertNotNull ("#4", ex.Message);
1133 AssertEquals ("#5", "format", ex.ParamName);
1137 [Test] // Format (String, Object [])
1138 public void Format2_Args_Null ()
1141 String.Format ("text", (object []) null);
1143 } catch (ArgumentNullException ex) {
1144 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1145 AssertNull ("#3", ex.InnerException);
1146 AssertNotNull ("#4", ex.Message);
1147 AssertEquals ("#5", "args", ex.ParamName);
1151 [Test] // Format (IFormatProvider, String, Object [])
1152 public void Format3_Format_Null ()
1155 String.Format (CultureInfo.InvariantCulture, null,
1156 new object [] { 3 });
1158 } catch (ArgumentNullException ex) {
1159 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1160 AssertNull ("#3", ex.InnerException);
1161 AssertNotNull ("#4", ex.Message);
1162 AssertEquals ("#5", "format", ex.ParamName);
1166 [Test] // Format (IFormatProvider, String, Object [])
1167 public void Format3_Args_Null ()
1170 String.Format (CultureInfo.InvariantCulture, "text",
1173 } catch (ArgumentNullException ex) {
1174 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1175 AssertNull ("#3", ex.InnerException);
1176 AssertNotNull ("#4", ex.Message);
1177 AssertEquals ("#5", "args", ex.ParamName);
1181 [Test] // Format (String, Object, Object)
1182 public void Format4_Format_Null ()
1185 String.Format (null, 4, 5);
1187 } catch (ArgumentNullException ex) {
1188 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1189 AssertNull ("#3", ex.InnerException);
1190 AssertNotNull ("#4", ex.Message);
1191 AssertEquals ("#5", "format", ex.ParamName);
1195 [Test] // Format (String, Object, Object, Object)
1196 public void Format5_Format_Null ()
1199 String.Format (null, 4, 5, 6);
1201 } catch (ArgumentNullException ex) {
1202 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1203 AssertNull ("#3", ex.InnerException);
1204 AssertNotNull ("#4", ex.Message);
1205 AssertEquals ("#5", "format", ex.ParamName);
1210 public void TestGetEnumerator ()
1212 string s1 = "original";
1213 char[] c1 = new char[s1.Length];
1214 string s2 = new String(c1);
1215 Assert("pre-enumerated string should not match", !s1.Equals(s2));
1216 CharEnumerator en = s1.GetEnumerator();
1217 AssertNotNull("null enumerator", en);
1219 for (int i = 0; i < s1.Length; i++) {
1223 s2 = new String(c1);
1224 AssertEquals("enumerated string should match", s1, s2);
1228 public void TestGetHashCode ()
1230 string s1 = "original";
1231 // TODO - weak test, currently. Just verifies determinicity.
1232 AssertEquals("same string, same hash code",
1233 s1.GetHashCode(), s1.GetHashCode());
1237 public void TestGetType ()
1239 string s1 = "original";
1240 AssertEquals("String type", "System.String", s1.GetType().ToString());
1244 public void TestGetTypeCode ()
1246 string s1 = "original";
1247 Assert(s1.GetTypeCode().Equals(TypeCode.String));
1251 public void IndexOf ()
1253 string s1 = "original";
1256 s1.IndexOf ('q', s1.Length + 1);
1258 } catch (ArgumentOutOfRangeException ex) {
1259 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1260 AssertNull ("#A3", ex.InnerException);
1261 AssertNotNull ("#A4", ex.Message);
1262 AssertEquals ("#A5", "startIndex", ex.ParamName);
1266 s1.IndexOf ('q', s1.Length + 1, 1);
1268 } catch (ArgumentOutOfRangeException ex) {
1269 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1270 AssertNull ("#B3", ex.InnerException);
1271 AssertNotNull ("#B4", ex.Message);
1272 AssertEquals ("#B5", "startIndex", ex.ParamName);
1276 s1.IndexOf ("huh", s1.Length + 1);
1278 } catch (ArgumentOutOfRangeException ex) {
1279 AssertEquals ("#C2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1280 AssertNull ("#C3", ex.InnerException);
1281 AssertNotNull ("#C4", ex.Message);
1282 AssertEquals ("#C5", "startIndex", ex.ParamName);
1285 AssertEquals("basic char index", 1, s1.IndexOf('r'));
1286 AssertEquals("basic char index 2", 2, s1.IndexOf('i'));
1287 AssertEquals("basic char index - no", -1, s1.IndexOf('q'));
1289 AssertEquals("basic string index", 1, s1.IndexOf("rig"));
1290 AssertEquals("basic string index 2", 2, s1.IndexOf("i"));
1291 AssertEquals("basic string index 3", 0, string.Empty.IndexOf(string.Empty));
1292 AssertEquals("basic string index 4", 0, "ABC".IndexOf(string.Empty));
1293 AssertEquals("basic string index - no", -1, s1.IndexOf("rag"));
1295 AssertEquals("stepped char index", 1, s1.IndexOf('r', 1));
1296 AssertEquals("stepped char index 2", 2, s1.IndexOf('i', 1));
1297 AssertEquals("stepped char index 3", 4, s1.IndexOf('i', 3));
1298 AssertEquals("stepped char index 4", -1, s1.IndexOf('i', 5));
1299 AssertEquals("stepped char index 5", -1, s1.IndexOf('l', s1.Length));
1301 AssertEquals("stepped limited char index",
1302 1, s1.IndexOf('r', 1, 1));
1303 AssertEquals("stepped limited char index",
1304 -1, s1.IndexOf('r', 0, 1));
1305 AssertEquals("stepped limited char index",
1306 2, s1.IndexOf('i', 1, 3));
1307 AssertEquals("stepped limited char index",
1308 4, s1.IndexOf('i', 3, 3));
1309 AssertEquals("stepped limited char index",
1310 -1, s1.IndexOf('i', 5, 3));
1312 s1 = "original original";
1313 AssertEquals("stepped string index 1",
1314 0, s1.IndexOf("original", 0));
1315 AssertEquals("stepped string index 2",
1316 9, s1.IndexOf("original", 1));
1317 AssertEquals("stepped string index 3",
1318 -1, s1.IndexOf("original", 10));
1319 AssertEquals("stepped string index 4",
1320 3, s1.IndexOf(string.Empty, 3));
1321 AssertEquals("stepped limited string index 1",
1322 1, s1.IndexOf("rig", 0, 5));
1323 AssertEquals("stepped limited string index 2",
1324 -1, s1.IndexOf("rig", 0, 3));
1325 AssertEquals("stepped limited string index 3",
1326 10, s1.IndexOf("rig", 2, 15));
1327 AssertEquals("stepped limited string index 4",
1328 -1, s1.IndexOf("rig", 2, 3));
1329 AssertEquals("stepped limited string index 5",
1330 2, s1.IndexOf(string.Empty, 2, 3));
1332 string s2 = "QBitArray::bitarr_data";
1333 AssertEquals ("bug #62160", 9, s2.IndexOf ("::"));
1336 [Test] // IndexOf (String)
1337 public void IndexOf2_Value_Null ()
1340 "Mono".IndexOf ((string) null);
1342 } catch (ArgumentNullException ex) {
1343 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1344 AssertNull ("#3", ex.InnerException);
1345 AssertNotNull ("#4", ex.Message);
1347 AssertEquals ("#5", "value", ex.ParamName);
1349 //Fixme: Does it really make sense to check for obsolete
1350 // parameter names. Then case this in string.
1351 //AssertEquals ("#5", "string2", ex.ParamName);
1356 [Test] // IndexOf (Char, Int32)
1357 public void IndexOf3 ()
1359 string s = "testing123456";
1361 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
1363 AssertEquals ("#A1", -1, s.IndexOf ('a', s.Length));
1364 AssertEquals ("#A2", -1, s.IndexOf ('6', s.Length));
1365 AssertEquals ("#A3", -1, s.IndexOf ('t', s.Length));
1366 AssertEquals ("#A4", -1, s.IndexOf ('T', s.Length));
1367 AssertEquals ("#A5", -1, s.IndexOf ('i', s.Length));
1368 AssertEquals ("#A6", -1, s.IndexOf ('I', s.Length));
1369 AssertEquals ("#A7", -1, s.IndexOf ('q', s.Length));
1370 AssertEquals ("#A8", -1, s.IndexOf ('3', s.Length));
1372 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
1374 AssertEquals ("#B1", -1, s.IndexOf ('a', s.Length));
1375 AssertEquals ("#B2", -1, s.IndexOf ('6', s.Length));
1376 AssertEquals ("#B3", -1, s.IndexOf ('t', s.Length));
1377 AssertEquals ("#B4", -1, s.IndexOf ('T', s.Length));
1378 AssertEquals ("#B5", -1, s.IndexOf ('i', s.Length));
1379 AssertEquals ("#B6", -1, s.IndexOf ('I', s.Length));
1380 AssertEquals ("#B7", -1, s.IndexOf ('q', s.Length));
1381 AssertEquals ("#B8", -1, s.IndexOf ('3', s.Length));
1384 [Test] // IndexOf (String, Int32)
1385 public void IndexOf4 ()
1387 string s = "testing123456";
1389 AssertEquals ("#1", -1, s.IndexOf ("IN", 3));
1390 AssertEquals ("#2", 4, s.IndexOf ("in", 3));
1391 AssertEquals ("#3", -1, s.IndexOf ("in", 5));
1392 AssertEquals ("#4", 7, s.IndexOf ("1", 5));
1393 AssertEquals ("#5", 12, s.IndexOf ("6", 12));
1394 AssertEquals ("#6", 0, s.IndexOf ("testing123456", 0));
1395 AssertEquals ("#7", -1, s.IndexOf ("testing123456", 1));
1396 AssertEquals ("#8", 5, s.IndexOf (string.Empty, 5));
1397 AssertEquals ("#9", 0, s.IndexOf (string.Empty, 0));
1400 [Test] // IndexOf (String, Int32)
1401 public void IndexOf4_Value_Null ()
1404 "Mono".IndexOf ((string) null, 1);
1406 } catch (ArgumentNullException ex) {
1407 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1408 AssertNull ("#3", ex.InnerException);
1409 AssertNotNull ("#4", ex.Message);
1411 AssertEquals ("#5", "value", ex.ParamName);
1413 AssertEquals ("#5", "string2", ex.ParamName);
1419 [Test] // IndexOf (String, StringComparison)
1420 public void IndexOf5 ()
1422 string s = "testing123456";
1423 StringComparison comparison_type;
1425 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
1427 comparison_type = StringComparison.CurrentCulture;
1428 AssertEquals ("#A1", 7, s.IndexOf ("123", comparison_type));
1429 AssertEquals ("#A2", -1, s.IndexOf ("NG", comparison_type));
1430 AssertEquals ("#A3", -1, s.IndexOf ("nga", comparison_type));
1431 AssertEquals ("#A4", 0, s.IndexOf ("t", comparison_type));
1432 AssertEquals ("#A5", -1, s.IndexOf ("T", comparison_type));
1433 AssertEquals ("#A6", 12, s.IndexOf ("6", comparison_type));
1434 AssertEquals ("#A7", 3, s.IndexOf ("tin", comparison_type));
1435 AssertEquals ("#A8", -1, s.IndexOf ("TIN", comparison_type));
1436 AssertEquals ("#A9", 0, s.IndexOf (string.Empty, comparison_type));
1438 comparison_type = StringComparison.CurrentCultureIgnoreCase;
1439 AssertEquals ("#B1", 7, s.IndexOf ("123", comparison_type));
1440 AssertEquals ("#B2", 5, s.IndexOf ("NG", comparison_type));
1441 AssertEquals ("#B3", -1, s.IndexOf ("nga", comparison_type));
1442 AssertEquals ("#B4", 0, s.IndexOf ("t", comparison_type));
1443 AssertEquals ("#B5", 0, s.IndexOf ("T", comparison_type));
1444 AssertEquals ("#B6", 12, s.IndexOf ("6", comparison_type));
1445 AssertEquals ("#B7", 3, s.IndexOf ("tin", comparison_type));
1446 AssertEquals ("#B8", -1, s.IndexOf ("TIN", comparison_type));
1447 AssertEquals ("#B9", 0, s.IndexOf (string.Empty, comparison_type));
1449 comparison_type = StringComparison.InvariantCulture;
1450 AssertEquals ("#C1", 7, s.IndexOf ("123", comparison_type));
1451 AssertEquals ("#C2", -1, s.IndexOf ("NG", comparison_type));
1452 AssertEquals ("#C3", -1, s.IndexOf ("nga", comparison_type));
1453 AssertEquals ("#C4", 0, s.IndexOf ("t", comparison_type));
1454 AssertEquals ("#C5", -1, s.IndexOf ("T", comparison_type));
1455 AssertEquals ("#C6", 12, s.IndexOf ("6", comparison_type));
1456 AssertEquals ("#C7", 3, s.IndexOf ("tin", comparison_type));
1457 AssertEquals ("#C8", -1, s.IndexOf ("TIN", comparison_type));
1458 AssertEquals ("#C9", 0, s.IndexOf (string.Empty, comparison_type));
1460 comparison_type = StringComparison.InvariantCultureIgnoreCase;
1461 AssertEquals ("#D1", 7, s.IndexOf ("123", comparison_type));
1462 AssertEquals ("#D2", 5, s.IndexOf ("NG", comparison_type));
1463 AssertEquals ("#D3", -1, s.IndexOf ("nga", comparison_type));
1464 AssertEquals ("#D4", 0, s.IndexOf ("t", comparison_type));
1465 AssertEquals ("#D5", 0, s.IndexOf ("T", comparison_type));
1466 AssertEquals ("#D6", 12, s.IndexOf ("6", comparison_type));
1467 AssertEquals ("#D7", 3, s.IndexOf ("tin", comparison_type));
1468 AssertEquals ("#D8", 3, s.IndexOf ("TIN", comparison_type));
1469 AssertEquals ("#D9", 0, s.IndexOf (string.Empty, comparison_type));
1471 comparison_type = StringComparison.Ordinal;
1472 AssertEquals ("#E1", 7, s.IndexOf ("123", comparison_type));
1473 AssertEquals ("#E2", -1, s.IndexOf ("NG", comparison_type));
1474 AssertEquals ("#E3", -1, s.IndexOf ("nga", comparison_type));
1475 AssertEquals ("#E4", 0, s.IndexOf ("t", comparison_type));
1476 AssertEquals ("#E5", -1, s.IndexOf ("T", comparison_type));
1477 AssertEquals ("#E6", 12, s.IndexOf ("6", comparison_type));
1478 AssertEquals ("#E7", 3, s.IndexOf ("tin", comparison_type));
1479 AssertEquals ("#E8", -1, s.IndexOf ("TIN", comparison_type));
1480 AssertEquals ("#E9", 0, s.IndexOf (string.Empty, comparison_type));
1482 comparison_type = StringComparison.OrdinalIgnoreCase;
1483 AssertEquals ("#F1", 7, s.IndexOf ("123", comparison_type));
1484 AssertEquals ("#F2", 5, s.IndexOf ("NG", comparison_type));
1485 AssertEquals ("#F3", -1, s.IndexOf ("nga", comparison_type));
1486 AssertEquals ("#F4", 0, s.IndexOf ("t", comparison_type));
1487 AssertEquals ("#F5", 0, s.IndexOf ("T", comparison_type));
1488 AssertEquals ("#F6", 12, s.IndexOf ("6", comparison_type));
1489 AssertEquals ("#F7", 3, s.IndexOf ("tin", comparison_type));
1490 AssertEquals ("#F8", 3, s.IndexOf ("TIN", comparison_type));
1491 AssertEquals ("#F9", 0, s.IndexOf (string.Empty, comparison_type));
1493 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
1495 comparison_type = StringComparison.CurrentCulture;
1496 AssertEquals ("#G1", 7, s.IndexOf ("123", comparison_type));
1497 AssertEquals ("#G2", -1, s.IndexOf ("NG", comparison_type));
1498 AssertEquals ("#G3", -1, s.IndexOf ("nga", comparison_type));
1499 AssertEquals ("#G4", 0, s.IndexOf ("t", comparison_type));
1500 AssertEquals ("#G5", -1, s.IndexOf ("T", comparison_type));
1501 AssertEquals ("#G6", 12, s.IndexOf ("6", comparison_type));
1502 AssertEquals ("#G7", 3, s.IndexOf ("tin", comparison_type));
1503 AssertEquals ("#G8", -1, s.IndexOf ("TIN", comparison_type));
1504 AssertEquals ("#G9", 0, s.IndexOf (string.Empty, comparison_type));
1506 comparison_type = StringComparison.CurrentCultureIgnoreCase;
1507 AssertEquals ("#H1", 7, s.IndexOf ("123", comparison_type));
1508 AssertEquals ("#H2", 5, s.IndexOf ("NG", comparison_type));
1509 AssertEquals ("#H3", -1, s.IndexOf ("nga", comparison_type));
1510 AssertEquals ("#H4", 0, s.IndexOf ("t", comparison_type));
1511 AssertEquals ("#H5", 0, s.IndexOf ("T", comparison_type));
1512 AssertEquals ("#H6", 12, s.IndexOf ("6", comparison_type));
1513 AssertEquals ("#H7", 3, s.IndexOf ("tin", comparison_type));
1514 AssertEquals ("#H8", 3, s.IndexOf ("TIN", comparison_type));
1515 AssertEquals ("#H9", 0, s.IndexOf (string.Empty, comparison_type));
1517 comparison_type = StringComparison.InvariantCulture;
1518 AssertEquals ("#I1", 7, s.IndexOf ("123", comparison_type));
1519 AssertEquals ("#I2", -1, s.IndexOf ("NG", comparison_type));
1520 AssertEquals ("#I3", -1, s.IndexOf ("nga", comparison_type));
1521 AssertEquals ("#I4", 0, s.IndexOf ("t", comparison_type));
1522 AssertEquals ("#I5", -1, s.IndexOf ("T", comparison_type));
1523 AssertEquals ("#I6", 12, s.IndexOf ("6", comparison_type));
1524 AssertEquals ("#I7", 3, s.IndexOf ("tin", comparison_type));
1525 AssertEquals ("#I8", -1, s.IndexOf ("TIN", comparison_type));
1526 AssertEquals ("#I9", 0, s.IndexOf (string.Empty, comparison_type));
1528 comparison_type = StringComparison.InvariantCultureIgnoreCase;
1529 AssertEquals ("#J1", 7, s.IndexOf ("123", comparison_type));
1530 AssertEquals ("#J2", 5, s.IndexOf ("NG", comparison_type));
1531 AssertEquals ("#J3", -1, s.IndexOf ("nga", comparison_type));
1532 AssertEquals ("#J4", 0, s.IndexOf ("t", comparison_type));
1533 AssertEquals ("#J5", 0, s.IndexOf ("T", comparison_type));
1534 AssertEquals ("#J6", 12, s.IndexOf ("6", comparison_type));
1535 AssertEquals ("#J7", 3, s.IndexOf ("tin", comparison_type));
1536 AssertEquals ("#J8", 3, s.IndexOf ("TIN", comparison_type));
1537 AssertEquals ("#J9", 0, s.IndexOf (string.Empty, comparison_type));
1539 comparison_type = StringComparison.Ordinal;
1540 AssertEquals ("#K1", 7, s.IndexOf ("123", comparison_type));
1541 AssertEquals ("#K2", -1, s.IndexOf ("NG", comparison_type));
1542 AssertEquals ("#K3", -1, s.IndexOf ("nga", comparison_type));
1543 AssertEquals ("#K4", 0, s.IndexOf ("t", comparison_type));
1544 AssertEquals ("#K5", -1, s.IndexOf ("T", comparison_type));
1545 AssertEquals ("#K6", 12, s.IndexOf ("6", comparison_type));
1546 AssertEquals ("#K7", 3, s.IndexOf ("tin", comparison_type));
1547 AssertEquals ("#K8", -1, s.IndexOf ("TIN", comparison_type));
1548 AssertEquals ("#K9", 0, s.IndexOf (string.Empty, comparison_type));
1550 comparison_type = StringComparison.OrdinalIgnoreCase;
1551 AssertEquals ("#L1", 7, s.IndexOf ("123", comparison_type));
1552 AssertEquals ("#L2", 5, s.IndexOf ("NG", comparison_type));
1553 AssertEquals ("#L3", -1, s.IndexOf ("nga", comparison_type));
1554 AssertEquals ("#L4", 0, s.IndexOf ("t", comparison_type));
1555 AssertEquals ("#L5", 0, s.IndexOf ("T", comparison_type));
1556 AssertEquals ("#L6", 12, s.IndexOf ("6", comparison_type));
1557 AssertEquals ("#L7", 3, s.IndexOf ("tin", comparison_type));
1558 AssertEquals ("#L8", 3, s.IndexOf ("TIN", comparison_type));
1559 AssertEquals ("#L9", 0, s.IndexOf (string.Empty, comparison_type));
1561 AssertEquals ("#M", 0, string.Empty.IndexOf (string.Empty, comparison_type));
1564 [Test] // IndexOf (String, StringComparison)
1565 public void IndexOf5_ComparisonType_Invalid ()
1568 "Mono".IndexOf (string.Empty, (StringComparison) Int32.MinValue);
1570 } catch (ArgumentException ex) {
1571 // The string comparison type passed in is currently
1573 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
1574 AssertNull ("#3", ex.InnerException);
1575 AssertNotNull ("#4", ex.Message);
1576 AssertEquals ("#5", "comparisonType", ex.ParamName);
1580 [Test] // IndexOf (String, StringComparison)
1581 public void IndexOf5_Value_Null ()
1584 "Mono".IndexOf ((string) null, StringComparison.Ordinal);
1586 } catch (ArgumentNullException ex) {
1587 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1588 AssertNull ("#3", ex.InnerException);
1589 AssertNotNull ("#4", ex.Message);
1590 AssertEquals ("#5", "value", ex.ParamName);
1595 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1596 public void IndexOfStringComparisonOrdinalRangeException1 ()
1598 "Mono".IndexOf ("no", 5, StringComparison.Ordinal);
1602 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1603 public void IndexOfStringComparisonOrdinalRangeException2 ()
1605 "Mono".IndexOf ("no", 1, 5, StringComparison.Ordinal);
1609 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1610 public void IndexOfStringComparisonOrdinalIgnoreCaseRangeException1 ()
1612 "Mono".IndexOf ("no", 5, StringComparison.OrdinalIgnoreCase);
1616 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1617 public void IndexOfStringComparisonOrdinalIgnoreCaseRangeException2 ()
1619 "Mono".IndexOf ("no", 1, 5, StringComparison.OrdinalIgnoreCase);
1623 public void IndexOfStringComparison ()
1625 string text = "testing123456";
1626 string text2 = "123";
1627 string text3 = "NG";
1629 AssertEquals ("#1-1", 7, text.IndexOf (text2, StringComparison.Ordinal));
1630 AssertEquals ("#2-1", 5, text.IndexOf (text3, StringComparison.OrdinalIgnoreCase));
1632 AssertEquals ("#1-2", 7, text.IndexOf (text2, 0, StringComparison.Ordinal));
1633 AssertEquals ("#2-2", 5, text.IndexOf (text3, 0, StringComparison.OrdinalIgnoreCase));
1635 AssertEquals ("#1-3", 7, text.IndexOf (text2, 1, StringComparison.Ordinal));
1636 AssertEquals ("#2-3", 5, text.IndexOf (text3, 1, StringComparison.OrdinalIgnoreCase));
1638 AssertEquals ("#1-4", 7, text.IndexOf (text2, 6, StringComparison.Ordinal));
1639 AssertEquals ("#2-4", -1, text.IndexOf (text3, 6, StringComparison.OrdinalIgnoreCase));
1641 AssertEquals ("#1-5", 7, text.IndexOf (text2, 7, 3, StringComparison.Ordinal));
1642 AssertEquals ("#2-5", -1, text.IndexOf (text3, 7, 3, StringComparison.OrdinalIgnoreCase));
1644 AssertEquals ("#1-6", -1, text.IndexOf (text2, 6, 0, StringComparison.Ordinal));
1645 AssertEquals ("#2-6", -1, text.IndexOf (text3, 5, 0, StringComparison.OrdinalIgnoreCase));
1647 AssertEquals ("#1-7", -1, text.IndexOf (text2, 7, 1, StringComparison.Ordinal));
1648 AssertEquals ("#2-7", -1, text.IndexOf (text3, 5, 1, StringComparison.OrdinalIgnoreCase));
1650 AssertEquals ("#3-1", 0, text.IndexOf (text4, 0, StringComparison.Ordinal));
1651 AssertEquals ("#3-2", 0, text.IndexOf (text4, 0, StringComparison.OrdinalIgnoreCase));
1653 AssertEquals ("#4-1", -1, text.IndexOf (text4, 13, StringComparison.Ordinal));
1654 AssertEquals ("#4-2", -1, text.IndexOf (text4, 13, StringComparison.OrdinalIgnoreCase));
1656 AssertEquals ("#4-1", -1, text.IndexOf (text4, 13, 0, StringComparison.Ordinal));
1657 AssertEquals ("#4-2", -1, text.IndexOf (text4, 13, 0, StringComparison.OrdinalIgnoreCase));
1659 AssertEquals ("#5-1", 12, text.IndexOf ("6", 12, 1, StringComparison.Ordinal));
1660 AssertEquals ("#5-2", 12, text.IndexOf ("6", 12, 1, StringComparison.OrdinalIgnoreCase));
1664 public void IndexOfStringComparisonOrdinal ()
1666 string text = "testing123456";
1667 AssertEquals ("#1", 10, text.IndexOf ("456", StringComparison.Ordinal));
1668 AssertEquals ("#2", -1, text.IndexOf ("4567", StringComparison.Ordinal));
1669 AssertEquals ("#3", 0, text.IndexOf ("te", StringComparison.Ordinal));
1670 AssertEquals ("#4", 2, text.IndexOf ("s", StringComparison.Ordinal));
1671 AssertEquals ("#5", -1, text.IndexOf ("ates", StringComparison.Ordinal));
1672 AssertEquals ("#6", -1, text.IndexOf ("S", StringComparison.Ordinal));
1676 public void IndexOfStringComparisonOrdinalIgnoreCase ()
1678 string text = "testing123456";
1679 AssertEquals ("#1", 10, text.IndexOf ("456", StringComparison.OrdinalIgnoreCase));
1680 AssertEquals ("#2", -1, text.IndexOf ("4567", StringComparison.OrdinalIgnoreCase));
1681 AssertEquals ("#3", 0, text.IndexOf ("te", StringComparison.OrdinalIgnoreCase));
1682 AssertEquals ("#4", 2, text.IndexOf ("s", StringComparison.OrdinalIgnoreCase));
1683 AssertEquals ("#5", -1, text.IndexOf ("ates", StringComparison.OrdinalIgnoreCase));
1684 AssertEquals ("#6", 2, text.IndexOf ("S", StringComparison.OrdinalIgnoreCase));
1688 public void IndexOfOrdinalCountSmallerThanValueString ()
1690 AssertEquals ("#1", -1, "Test".IndexOf ("ST", 2, 1, StringComparison.Ordinal));
1691 AssertEquals ("#2", -1, "Test".IndexOf ("ST", 2, 1, StringComparison.OrdinalIgnoreCase));
1692 AssertEquals ("#3", -1, "Test".LastIndexOf ("ST", 2, 1, StringComparison.Ordinal));
1693 AssertEquals ("#4", -1, "Test".LastIndexOf ("ST", 2, 1, StringComparison.OrdinalIgnoreCase));
1697 [Test] // IndexOf (Char, Int32, Int32)
1698 public void IndexOf6_Count_Negative ()
1701 "Mono".IndexOf ('o', 1, -1);
1703 } catch (ArgumentOutOfRangeException ex) {
1704 // Count must be positive and count must refer to a
1705 // location within the string/array/collection
1706 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1707 AssertNull ("#3", ex.InnerException);
1708 AssertNotNull ("#4", ex.Message);
1709 AssertEquals ("#5", "count", ex.ParamName);
1713 [Test] // IndexOf (Char, Int32, Int32)
1714 public void IndexOf6_Count_Overflow ()
1717 "Mono".IndexOf ('o', 1, Int32.MaxValue);
1719 } catch (ArgumentOutOfRangeException ex) {
1720 // Count must be positive and count must refer to a
1721 // location within the string/array/collection
1722 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1723 AssertNull ("#3", ex.InnerException);
1724 AssertNotNull ("#4", ex.Message);
1725 AssertEquals ("#5", "count", ex.ParamName);
1729 [Test] // IndexOf (Char, Int32, Int32)
1730 public void IndexOf6_StartIndex_Negative ()
1733 "Mono".IndexOf ('o', -1, 1);
1735 } catch (ArgumentOutOfRangeException ex) {
1736 // Index was out of range. Must be non-negative and
1737 // less than the size of the collection
1738 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1739 AssertNull ("#3", ex.InnerException);
1740 AssertNotNull ("#4", ex.Message);
1741 AssertEquals ("#5", "startIndex", ex.ParamName);
1745 [Test] // IndexOf (Char, Int32, Int32)
1746 public void IndexOf6_StartIndex_Overflow ()
1748 string s = "testing123456";
1751 s.IndexOf ('o', s.Length + 1, 1);
1753 } catch (ArgumentOutOfRangeException ex) {
1754 // Index was out of range. Must be non-negative and
1755 // less than the size of the collection
1756 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1757 AssertNull ("#3", ex.InnerException);
1758 AssertNotNull ("#4", ex.Message);
1759 AssertEquals ("#5", "startIndex", ex.ParamName);
1763 [Test] // IndexOf (String, Int32, Int32)
1764 public void IndexOf7 ()
1766 string s = "testing123456test";
1768 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
1770 AssertEquals ("#A1", -1, s.IndexOf ("123", 4, 5));
1771 AssertEquals ("#A2", 7, s.IndexOf ("123", 4, 6));
1772 AssertEquals ("#A3", -1, s.IndexOf ("123", 5, 4));
1773 AssertEquals ("#A4", 7, s.IndexOf ("123", 5, 5));
1774 AssertEquals ("#A5", 7, s.IndexOf ("123", 0, s.Length));
1775 AssertEquals ("#A6", -1, s.IndexOf ("123", s.Length, 0));
1777 AssertEquals ("#B1", -1, s.IndexOf ("tin", 2, 3));
1778 AssertEquals ("#B2", 3, s.IndexOf ("tin", 3, 3));
1779 AssertEquals ("#B3", -1, s.IndexOf ("tin", 2, 2));
1780 AssertEquals ("#B4", -1, s.IndexOf ("tin", 1, 4));
1781 AssertEquals ("#B5", 3, s.IndexOf ("tin", 0, s.Length));
1782 AssertEquals ("#B6", -1, s.IndexOf ("tin", s.Length, 0));
1784 AssertEquals ("#C1", 6, s.IndexOf ("g12", 4, 5));
1785 AssertEquals ("#C2", -1, s.IndexOf ("g12", 5, 2));
1786 AssertEquals ("#C3", -1, s.IndexOf ("g12", 5, 3));
1787 AssertEquals ("#C4", 6, s.IndexOf ("g12", 6, 4));
1788 AssertEquals ("#C5", 6, s.IndexOf ("g12", 0, s.Length));
1789 AssertEquals ("#C6", -1, s.IndexOf ("g12", s.Length, 0));
1791 AssertEquals ("#D1", 1, s.IndexOf ("est", 0, 5));
1792 AssertEquals ("#D2", -1, s.IndexOf ("est", 1, 2));
1793 AssertEquals ("#D3", -1, s.IndexOf ("est", 2, 10));
1794 AssertEquals ("#D4", 14, s.IndexOf ("est", 7, 10));
1795 AssertEquals ("#D5", 1, s.IndexOf ("est", 0, s.Length));
1796 AssertEquals ("#D6", -1, s.IndexOf ("est", s.Length, 0));
1798 AssertEquals ("#E1", -1, s.IndexOf ("T", 0, s.Length));
1799 AssertEquals ("#E2", 4, s.IndexOf ("i", 0, s.Length));
1800 AssertEquals ("#E3", -1, s.IndexOf ("I", 0, s.Length));
1801 AssertEquals ("#E4", 12, s.IndexOf ("6", 0, s.Length));
1802 AssertEquals ("#E5", 0, s.IndexOf ("testing123456", 0, s.Length));
1803 AssertEquals ("#E6", -1, s.IndexOf ("testing1234567", 0, s.Length));
1804 AssertEquals ("#E7", 0, s.IndexOf (string.Empty, 0, 0));
1805 AssertEquals ("#E8", 4, s.IndexOf (string.Empty, 4, 3));
1806 AssertEquals ("#E9", 0, string.Empty.IndexOf (string.Empty, 0, 0));
1807 AssertEquals ("#E10", -1, string.Empty.IndexOf ("abc", 0, 0));
1809 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
1811 AssertEquals ("#F1", -1, s.IndexOf ("123", 4, 5));
1812 AssertEquals ("#F2", 7, s.IndexOf ("123", 4, 6));
1813 AssertEquals ("#F3", -1, s.IndexOf ("123", 5, 4));
1814 AssertEquals ("#F4", 7, s.IndexOf ("123", 5, 5));
1815 AssertEquals ("#F5", 7, s.IndexOf ("123", 0, s.Length));
1816 AssertEquals ("#F6", -1, s.IndexOf ("123", s.Length, 0));
1818 AssertEquals ("#G1", -1, s.IndexOf ("tin", 2, 3));
1819 AssertEquals ("#G2", 3, s.IndexOf ("tin", 3, 3));
1820 AssertEquals ("#G3", -1, s.IndexOf ("tin", 2, 2));
1821 AssertEquals ("#G4", -1, s.IndexOf ("tin", 1, 4));
1822 AssertEquals ("#G5", 3, s.IndexOf ("tin", 0, s.Length));
1823 AssertEquals ("#G6", -1, s.IndexOf ("tin", s.Length, 0));
1825 AssertEquals ("#H1", 6, s.IndexOf ("g12", 4, 5));
1826 AssertEquals ("#H2", -1, s.IndexOf ("g12", 5, 2));
1827 AssertEquals ("#H3", -1, s.IndexOf ("g12", 5, 3));
1828 AssertEquals ("#H4", 6, s.IndexOf ("g12", 6, 4));
1829 AssertEquals ("#H5", 6, s.IndexOf ("g12", 0, s.Length));
1830 AssertEquals ("#H6", -1, s.IndexOf ("g12", s.Length, 0));
1832 AssertEquals ("#I1", 1, s.IndexOf ("est", 0, 5));
1833 AssertEquals ("#I2", -1, s.IndexOf ("est", 1, 2));
1834 AssertEquals ("#I3", -1, s.IndexOf ("est", 2, 10));
1835 AssertEquals ("#I4", 14, s.IndexOf ("est", 7, 10));
1836 AssertEquals ("#I5", 1, s.IndexOf ("est", 0, s.Length));
1837 AssertEquals ("#I6", -1, s.IndexOf ("est", s.Length, 0));
1839 AssertEquals ("#J1", -1, s.IndexOf ("T", 0, s.Length));
1840 AssertEquals ("#J2", 4, s.IndexOf ("i", 0, s.Length));
1841 AssertEquals ("#J3", -1, s.IndexOf ("I", 0, s.Length));
1842 AssertEquals ("#J4", 12, s.IndexOf ("6", 0, s.Length));
1843 AssertEquals ("#J5", 0, s.IndexOf ("testing123456", 0, s.Length));
1844 AssertEquals ("#J6", -1, s.IndexOf ("testing1234567", 0, s.Length));
1845 AssertEquals ("#J7", 0, s.IndexOf (string.Empty, 0, 0));
1846 AssertEquals ("#J8", 4, s.IndexOf (string.Empty, 4, 3));
1847 AssertEquals ("#J9", 0, string.Empty.IndexOf (string.Empty, 0, 0));
1848 AssertEquals ("#J10", -1, string.Empty.IndexOf ("abc", 0, 0));
1851 [Test] // IndexOf (String, Int32, Int32)
1852 public void IndexOf7_Count_Negative ()
1855 "Mono".IndexOf ("no", 1, -1);
1857 } catch (ArgumentOutOfRangeException ex) {
1858 // Count must be positive and count must refer to a
1859 // location within the string/array/collection
1860 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1861 AssertNull ("#3", ex.InnerException);
1862 AssertNotNull ("#4", ex.Message);
1863 AssertEquals ("#5", "count", ex.ParamName);
1867 [Test] // IndexOf (String, Int32, Int32)
1868 public void IndexOf7_Count_Overflow ()
1870 string s = "testing123456";
1873 s.IndexOf ("no", 1, s.Length);
1875 } catch (ArgumentOutOfRangeException ex) {
1876 // Count must be positive and count must refer to a
1877 // location within the string/array/collection
1878 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1879 AssertNull ("#A3", ex.InnerException);
1880 AssertNotNull ("#A4", ex.Message);
1881 AssertEquals ("#A5", "count", ex.ParamName);
1885 s.IndexOf ("no", 1, s.Length + 1);
1887 } catch (ArgumentOutOfRangeException ex) {
1888 // Count must be positive and count must refer to a
1889 // location within the string/array/collection
1890 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1891 AssertNull ("#B3", ex.InnerException);
1892 AssertNotNull ("#B4", ex.Message);
1893 AssertEquals ("#B5", "count", ex.ParamName);
1897 s.IndexOf ("no", 1, int.MaxValue);
1899 } catch (ArgumentOutOfRangeException ex) {
1901 // Count must be positive and count must refer to a
1902 // location within the string/array/collection
1903 AssertEquals ("#C2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1904 AssertNull ("#C3", ex.InnerException);
1905 AssertNotNull ("#C4", ex.Message);
1906 AssertEquals ("#C5", "count", ex.ParamName);
1908 // Index was out of range. Must be non-negative and
1909 // less than the size of the collection
1910 AssertEquals ("#C2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1911 AssertNull ("#C3", ex.InnerException);
1912 AssertNotNull ("#C4", ex.Message);
1913 AssertNotNull ("#C5", ex.ParamName);
1914 //AssertEquals ("#C5", "startIndex", ex.ParamName);
1919 [Test] // IndexOf (String, Int32, Int32)
1920 public void IndexOf7_StartIndex_Negative ()
1923 "Mono".IndexOf ("no", -1, 1);
1925 } catch (ArgumentOutOfRangeException ex) {
1926 // Index was out of range. Must be non-negative and
1927 // less than the size of the collection
1928 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1929 AssertNull ("#3", ex.InnerException);
1930 AssertNotNull ("#4", ex.Message);
1931 AssertEquals ("#5", "startIndex", ex.ParamName);
1935 [Test] // IndexOf (String, Int32, Int32)
1936 public void IndexOf7_StartIndex_Overflow ()
1938 string s = "testing123456";
1941 s.IndexOf ("no", s.Length + 1, 1);
1943 } catch (ArgumentOutOfRangeException ex) {
1944 // Index was out of range. Must be non-negative and
1945 // less than the size of the collection
1946 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1947 AssertNull ("#A3", ex.InnerException);
1948 AssertNotNull ("#A4", ex.Message);
1950 AssertEquals ("#A5", "startIndex", ex.ParamName);
1952 AssertNotNull ("#A5", ex.ParamName);
1953 //AssertEquals ("#A5", "count", ex.ParamName);
1958 s.IndexOf ("no", int.MaxValue, 1);
1960 } catch (ArgumentOutOfRangeException ex) {
1961 // Index was out of range. Must be non-negative and
1962 // less than the size of the collection
1963 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
1964 AssertNull ("#B3", ex.InnerException);
1965 AssertNotNull ("#B4", ex.Message);
1966 AssertEquals ("#B5", "startIndex", ex.ParamName);
1970 [Test] // IndexOf (String, Int32, Int32)
1971 public void IndexOf7_Value_Null ()
1974 "Mono".IndexOf ((string) null, 0, 1);
1976 } catch (ArgumentNullException ex) {
1977 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
1978 AssertNull ("#3", ex.InnerException);
1979 AssertNotNull ("#4", ex.Message);
1981 AssertEquals ("#5", "value", ex.ParamName);
1983 AssertEquals ("#5", "string2", ex.ParamName);
1989 [Test] // IndexOf (String, Int32, StringComparison)
1990 public void IndexOf8_ComparisonType_Invalid ()
1993 "Mono".IndexOf (string.Empty, 1, (StringComparison) Int32.MinValue);
1995 } catch (ArgumentException ex) {
1996 // The string comparison type passed in is currently
1998 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
1999 AssertNull ("#3", ex.InnerException);
2000 AssertNotNull ("#4", ex.Message);
2001 AssertEquals ("#5", "comparisonType", ex.ParamName);
2005 [Test] // IndexOf (String, Int32, StringComparison)
2006 public void IndexOf8_StartIndex_Negative ()
2009 "Mono".IndexOf ("o", -1, StringComparison.Ordinal);
2011 } catch (ArgumentOutOfRangeException ex) {
2012 // Index was out of range. Must be non-negative and
2013 // less than the size of the collection
2014 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2015 AssertNull ("#3", ex.InnerException);
2016 AssertNotNull ("#4", ex.Message);
2017 AssertEquals ("#5", "startIndex", ex.ParamName);
2021 [Test] // IndexOf (String, Int32, Int32, StringComparison)
2022 public void IndexOf9_ComparisonType_Invalid ()
2025 "Mono".IndexOf (string.Empty, 0, 1, (StringComparison) Int32.MinValue);
2027 } catch (ArgumentException ex) {
2028 // The string comparison type passed in is currently
2030 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
2031 AssertNull ("#3", ex.InnerException);
2032 AssertNotNull ("#4", ex.Message);
2033 AssertEquals ("#5", "comparisonType", ex.ParamName);
2037 [Test] // IndexOf (String, Int32, Int32, StringComparison)
2038 public void IndexOf9_Count_Negative ()
2041 "Mono".IndexOf ("o", 1, -1, StringComparison.Ordinal);
2044 } catch (ArgumentOutOfRangeException ex) {
2045 // Count must be positive and count must refer to a
2046 // location within the string/array/collection
2047 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2048 AssertNull ("#3", ex.InnerException);
2049 AssertNotNull ("#4", ex.Message);
2050 AssertEquals ("#5", "count", ex.ParamName);
2054 [Test] // IndexOf (String, Int32, Int32, StringComparison)
2055 public void IndexOf9_StartIndex_Negative ()
2058 "Mono".IndexOf ("o", -1, 0, StringComparison.Ordinal);
2060 } catch (ArgumentOutOfRangeException ex) {
2061 // Index was out of range. Must be non-negative and
2062 // less than the size of the collection
2063 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2064 AssertNull ("#3", ex.InnerException);
2065 AssertNotNull ("#4", ex.Message);
2066 AssertEquals ("#5", "startIndex", ex.ParamName);
2072 public void IndexOfAny1 ()
2074 string s = "abcdefghijklmd";
2077 c = new char [] {'a', 'e', 'i', 'o', 'u'};
2078 AssertEquals ("#1", 0, s.IndexOfAny (c));
2079 c = new char [] { 'd', 'z' };
2080 AssertEquals ("#1", 3, s.IndexOfAny (c));
2081 c = new char [] { 'q', 'm', 'z' };
2082 AssertEquals ("#2", 12, s.IndexOfAny (c));
2084 AssertEquals ("#3", -1, s.IndexOfAny (c));
2088 [Test] // IndexOfAny (Char [])
2089 public void IndexOfAny1_AnyOf_Null ()
2092 "mono".IndexOfAny ((char []) null);
2094 } catch (ArgumentNullException ex) {
2095 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
2096 AssertNull ("#3", ex.InnerException);
2097 AssertNotNull ("#4", ex.Message);
2098 AssertNull ("#5", ex.ParamName);
2102 [Test] // IndexOfAny (Char [], Int32)
2103 public void IndexOfAny2 ()
2105 string s = "abcdefghijklmd";
2108 c = new char [] { 'a', 'e', 'i', 'o', 'u' };
2109 AssertEquals ("#A1", 0, s.IndexOfAny (c, 0));
2110 AssertEquals ("#A1", 4, s.IndexOfAny (c, 1));
2111 AssertEquals ("#A2", -1, s.IndexOfAny (c, 9));
2112 AssertEquals ("#A3", -1, s.IndexOfAny (c, s.Length));
2114 c = new char [] { 'd', 'z' };
2115 AssertEquals ("#B1", 3, s.IndexOfAny (c, 0));
2116 AssertEquals ("#B2", 3, s.IndexOfAny (c, 3));
2117 AssertEquals ("#B3", 13, s.IndexOfAny (c, 4));
2118 AssertEquals ("#B4", 13, s.IndexOfAny (c, 9));
2119 AssertEquals ("#B5", -1, s.IndexOfAny (c, s.Length));
2120 AssertEquals ("#B6", 13, s.IndexOfAny (c, s.Length - 1));
2122 c = new char [] { 'q', 'm', 'z' };
2123 AssertEquals ("#C1", 12, s.IndexOfAny (c, 0));
2124 AssertEquals ("#C2", 12, s.IndexOfAny (c, 4));
2125 AssertEquals ("#C3", 12, s.IndexOfAny (c, 12));
2126 AssertEquals ("#C4", -1, s.IndexOfAny (c, s.Length));
2129 AssertEquals ("#D1", -1, s.IndexOfAny (c, 0));
2130 AssertEquals ("#D2", -1, s.IndexOfAny (c, 4));
2131 AssertEquals ("#D3", -1, s.IndexOfAny (c, 9));
2132 AssertEquals ("#D4", -1, s.IndexOfAny (c, s.Length));
2135 [Test] // IndexOfAny (Char [], Int32)
2136 public void IndexOfAny2_AnyOf_Null ()
2139 "mono".IndexOfAny ((char []) null, 0);
2141 } catch (ArgumentNullException ex) {
2142 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
2143 AssertNull ("#3", ex.InnerException);
2144 AssertNotNull ("#4", ex.Message);
2145 AssertNull ("#5", ex.ParamName);
2149 [Test] // IndexOfAny (Char [], Int32)
2150 public void IndexOfAny2_StartIndex_Negative ()
2152 string s = "abcdefghijklm";
2155 s.IndexOfAny (new char [1] { 'd' }, -1, 1);
2157 } catch (ArgumentOutOfRangeException ex) {
2158 // Specified argument was out of the range of valid
2160 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2161 AssertNull ("#3", ex.InnerException);
2162 AssertNotNull ("#4", ex.Message);
2163 AssertNull ("#5", ex.ParamName);
2167 [Test] // IndexOfAny (Char [], Int32, Int32)
2168 public void IndexOfAny2_StartIndex_Overflow ()
2170 string s = "abcdefghijklm";
2173 s.IndexOfAny (new char [1] { 'd' }, s.Length + 1);
2175 } catch (ArgumentOutOfRangeException ex) {
2176 // Specified argument was out of the range of valid
2178 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2179 AssertNull ("#3", ex.InnerException);
2180 AssertNotNull ("#4", ex.Message);
2181 AssertNull ("#5", ex.ParamName);
2185 [Test] // IndexOfAny (Char [], Int32, Int32)
2186 public void IndexOfAny3 ()
2188 string s = "abcdefghijklmd";
2191 c = new char [] { 'a', 'e', 'i', 'o', 'u' };
2192 AssertEquals ("#A1", 0, s.IndexOfAny (c, 0, 2));
2193 AssertEquals ("#A2", -1, s.IndexOfAny (c, 1, 2));
2194 AssertEquals ("#A3", -1, s.IndexOfAny (c, 1, 3));
2195 AssertEquals ("#A3", 4, s.IndexOfAny (c, 1, 4));
2196 AssertEquals ("#A4", 4, s.IndexOfAny (c, 1, s.Length - 1));
2198 c = new char [] { 'd', 'z' };
2199 AssertEquals ("#B1", -1, s.IndexOfAny (c, 0, 2));
2200 AssertEquals ("#B2", -1, s.IndexOfAny (c, 1, 2));
2201 AssertEquals ("#B3", 3, s.IndexOfAny (c, 1, 3));
2202 AssertEquals ("#B4", 3, s.IndexOfAny (c, 0, s.Length));
2203 AssertEquals ("#B5", 3, s.IndexOfAny (c, 1, s.Length - 1));
2204 AssertEquals ("#B6", -1, s.IndexOfAny (c, s.Length, 0));
2206 c = new char [] { 'q', 'm', 'z' };
2207 AssertEquals ("#C1", -1, s.IndexOfAny (c, 0, 10));
2208 AssertEquals ("#C2", 12, s.IndexOfAny (c, 10, 4));
2209 AssertEquals ("#C3", -1, s.IndexOfAny (c, 1, 3));
2210 AssertEquals ("#C4", 12, s.IndexOfAny (c, 0, s.Length));
2211 AssertEquals ("#C5", 12, s.IndexOfAny (c, 1, s.Length - 1));
2214 AssertEquals ("#D1", -1, s.IndexOfAny (c, 0, 3));
2215 AssertEquals ("#D2", -1, s.IndexOfAny (c, 4, 9));
2216 AssertEquals ("#D3", -1, s.IndexOfAny (c, 9, 5));
2217 AssertEquals ("#D4", -1, s.IndexOfAny (c, 13, 1));
2220 [Test] // IndexOfAny (Char [], Int32, Int32)
2221 public void IndexOfAny3_AnyOf_Null ()
2224 "mono".IndexOfAny ((char []) null, 0, 0);
2226 } catch (ArgumentNullException ex) {
2227 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
2228 AssertNull ("#3", ex.InnerException);
2229 AssertNotNull ("#4", ex.Message);
2230 AssertNull ("#5", ex.ParamName);
2234 [Test] // IndexOfAny (Char [], Int32, Int32)
2235 public void IndexOfAny3_Count_Negative ()
2238 "Mono".IndexOfAny (new char [1] { 'o' }, 1, -1);
2240 } catch (ArgumentOutOfRangeException ex) {
2241 // Count must be positive and count must refer to a
2242 // location within the string/array/collection
2243 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2244 AssertNull ("#3", ex.InnerException);
2245 AssertNotNull ("#4", ex.Message);
2246 AssertEquals ("#5", "count", ex.ParamName);
2250 [Test] // IndexOfAny (Char [], Int32, Int32)
2251 public void IndexOfAny3_Length_Overflow ()
2253 string s = "abcdefghijklm";
2256 s.IndexOfAny (new char [1] { 'd' }, 1, s.Length);
2258 } catch (ArgumentOutOfRangeException ex) {
2259 // Count must be positive and count must refer to a
2260 // location within the string/array/collection
2261 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2262 AssertNull ("#3", ex.InnerException);
2263 AssertNotNull ("#4", ex.Message);
2264 AssertEquals ("#5", "count", ex.ParamName);
2268 [Test] // IndexOfAny (Char [], Int32, Int32)
2269 public void IndexOfAny3_StartIndex_Negative ()
2272 "Mono".IndexOfAny (new char [1] { 'o' }, -1, 1);
2274 } catch (ArgumentOutOfRangeException ex) {
2275 // Specified argument was out of the range of valid
2277 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2278 AssertNull ("#3", ex.InnerException);
2279 AssertNotNull ("#4", ex.Message);
2280 AssertNull ("#5", ex.ParamName);
2284 [Test] // IndexOfAny (Char [], Int32, Int32)
2285 public void IndexOfAny3_StartIndex_Overflow ()
2287 string s = "abcdefghijklm";
2290 s.IndexOfAny (new char [1] { 'o' }, s.Length + 1, 1);
2292 } catch (ArgumentOutOfRangeException ex) {
2293 // Specified argument was out of the range of valid
2295 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2296 AssertNull ("#3", ex.InnerException);
2297 AssertNotNull ("#4", ex.Message);
2298 AssertNull ("#5", ex.ParamName);
2304 public void Contains ()
2306 Assert ("ABC".Contains (string.Empty));
2307 Assert ("ABC".Contains ("ABC"));
2308 Assert ("ABC".Contains ("AB"));
2309 Assert (!"ABC".Contains ("AD"));
2313 public void Contains_Value_Null ()
2316 "ABC".Contains (null);
2318 } catch (ArgumentNullException ex) {
2319 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
2320 AssertNull ("#3", ex.InnerException);
2321 AssertNotNull ("#4", ex.Message);
2322 AssertEquals ("#5", "value", ex.ParamName);
2327 public void IsNullOrEmpty ()
2329 Assert (String.IsNullOrEmpty (null));
2330 Assert (String.IsNullOrEmpty (String.Empty));
2331 Assert (String.IsNullOrEmpty (""));
2332 Assert (!String.IsNullOrEmpty ("A"));
2333 Assert (!String.IsNullOrEmpty (" "));
2334 Assert (!String.IsNullOrEmpty ("\t"));
2335 Assert (!String.IsNullOrEmpty ("\n"));
2340 public void TestInsert ()
2342 string s1 = "original";
2345 s1.Insert (0, null);
2347 } catch (ArgumentNullException ex) {
2348 AssertEquals ("#A2", typeof (ArgumentNullException), ex.GetType ());
2349 AssertNull ("#A3", ex.InnerException);
2350 AssertNotNull ("#A4", ex.Message);
2351 AssertEquals ("#A5", "value", ex.ParamName);
2355 s1.Insert (s1.Length + 1, "Hi!");
2357 } catch (ArgumentOutOfRangeException ex) {
2358 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2359 AssertNull ("#B3", ex.InnerException);
2360 AssertNotNull ("#B4", ex.Message);
2361 AssertEquals ("#B5", "startIndex", ex.ParamName);
2364 AssertEquals("#C1", "Hi!original", s1.Insert (0, "Hi!"));
2365 AssertEquals("#C2", "originalHi!", s1.Insert (s1.Length, "Hi!"));
2366 AssertEquals("#C3", "origHi!inal", s1.Insert (4, "Hi!"));
2370 public void Intern ()
2372 string s1 = "original";
2373 AssertSame ("#A1", s1, String.Intern (s1));
2374 AssertSame ("#A2", String.Intern(s1), String.Intern(s1));
2376 string s2 = "originally";
2377 AssertSame ("#B1", s2, String.Intern (s2));
2378 Assert ("#B2", String.Intern(s1) != String.Intern(s2));
2380 string s3 = new DateTime (2000, 3, 7).ToString ();
2381 AssertNull ("#C1", String.IsInterned (s3));
2382 AssertSame ("#C2", s3, String.Intern (s3));
2383 AssertSame ("#C3", s3, String.IsInterned (s3));
2384 AssertSame ("#C4", s3, String.IsInterned (new DateTime (2000, 3, 7).ToString ()));
2385 AssertSame ("#C5", s3, String.Intern (new DateTime (2000, 3, 7).ToString ()));
2389 public void Intern_Str_Null ()
2392 String.Intern (null);
2394 } catch (ArgumentNullException ex) {
2395 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
2396 AssertNull ("#3", ex.InnerException);
2397 AssertNotNull ("#4", ex.Message);
2398 AssertEquals ("#5", "str", ex.ParamName);
2403 public void IsInterned ()
2405 AssertNull ("#1", String.IsInterned (new DateTime (2000, 3, 6).ToString ()));
2406 string s1 = "original";
2407 AssertSame("#2", s1, String.IsInterned (s1));
2411 public void IsInterned_Str_Null ()
2414 String.IsInterned (null);
2416 } catch (ArgumentNullException ex) {
2417 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
2418 AssertNull ("#3", ex.InnerException);
2419 AssertNotNull ("#4", ex.Message);
2420 AssertEquals ("#5", "str", ex.ParamName);
2425 public void TestJoin ()
2428 string s = String.Join(" ", null);
2430 } catch (ArgumentNullException ex) {
2431 AssertEquals ("#A2", typeof (ArgumentNullException), ex.GetType ());
2432 AssertNull ("#A3", ex.InnerException);
2433 AssertNotNull ("#A4", ex.Message);
2434 AssertEquals ("#A5", "value", ex.ParamName);
2437 string[] chunks = {"this", "is", "a", "test"};
2438 AssertEquals("Basic join", "this is a test",
2439 String.Join(" ", chunks));
2440 AssertEquals("Basic join", "this.is.a.test",
2441 String.Join(".", chunks));
2443 AssertEquals("Subset join", "is a",
2444 String.Join(" ", chunks, 1, 2));
2445 AssertEquals("Subset join", "is.a",
2446 String.Join(".", chunks, 1, 2));
2447 AssertEquals("Subset join", "is a test",
2448 String.Join(" ", chunks, 1, 3));
2451 string s = String.Join(" ", chunks, 2, 3);
2453 } catch (ArgumentOutOfRangeException ex) {
2454 AssertEquals ("#C2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2455 AssertNull ("#C3", ex.InnerException);
2456 AssertNotNull ("#C4", ex.Message);
2457 AssertEquals ("#C5", "startIndex", ex.ParamName);
2462 public void Join_SeparatorNull ()
2464 string[] chunks = {"this", "is", "a", "test"};
2465 AssertEquals ("SeparatorNull", "thisisatest", String.Join (null, chunks));
2469 public void Join_ValuesNull ()
2471 string[] chunks1 = {null, "is", "a", null};
2472 AssertEquals ("SomeNull", " is a ", String.Join (" ", chunks1));
2474 string[] chunks2 = {null, "is", "a", null};
2475 AssertEquals ("Some+Sep=Null", "isa", String.Join (null, chunks2));
2477 string[] chunks3 = {null, null, null, null};
2478 AssertEquals ("AllValuesNull", " ", String.Join (" ", chunks3));
2482 public void Join_AllNull ()
2484 string[] chunks = {null, null, null};
2485 AssertEquals ("AllNull", string.Empty, String.Join (null, chunks));
2489 public void Join_StartIndexNegative ()
2491 string[] values = { "Mo", "no" };
2493 String.Join ("o", values, -1, 1);
2495 } catch (ArgumentOutOfRangeException ex) {
2496 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2497 AssertNull ("#3", ex.InnerException);
2498 AssertNotNull ("#4", ex.Message);
2499 AssertEquals ("#5", "startIndex", ex.ParamName);
2504 public void Join_StartIndexOverflow ()
2506 string[] values = { "Mo", "no" };
2508 String.Join ("o", values, Int32.MaxValue, 1);
2510 } catch (ArgumentOutOfRangeException ex) {
2511 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2512 AssertNull ("#3", ex.InnerException);
2513 AssertNotNull ("#4", ex.Message);
2514 AssertEquals ("#5", "startIndex", ex.ParamName);
2519 public void Join_LengthNegative ()
2521 string[] values = { "Mo", "no" };
2523 String.Join ("o", values, 1, -1);
2525 } catch (ArgumentOutOfRangeException ex) {
2526 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2527 AssertNull ("#3", ex.InnerException);
2528 AssertNotNull ("#4", ex.Message);
2529 AssertEquals ("#5", "count", ex.ParamName);
2534 public void Join_LengthOverflow ()
2536 string[] values = { "Mo", "no" };
2538 String.Join ("o", values, 1, Int32.MaxValue);
2540 } catch (ArgumentOutOfRangeException ex) {
2541 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2542 AssertNull ("#3", ex.InnerException);
2543 AssertNotNull ("#4", ex.Message);
2544 AssertEquals ("#5", "startIndex", ex.ParamName);
2549 public void LastIndexOf ()
2551 string s1 = "original";
2554 s1.LastIndexOf ('q', -1);
2556 } catch (ArgumentOutOfRangeException ex) {
2557 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2558 AssertNull ("#A3", ex.InnerException);
2559 AssertNotNull ("#A4", ex.Message);
2560 AssertEquals ("#A5", "startIndex", ex.ParamName);
2564 s1.LastIndexOf ('q', -1, 1);
2566 } catch (ArgumentOutOfRangeException ex) {
2567 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2568 AssertNull ("#B3", ex.InnerException);
2569 AssertNotNull ("#B4", ex.Message);
2570 AssertEquals ("#B5", "startIndex", ex.ParamName);
2574 s1.LastIndexOf ("huh", s1.Length + 1);
2576 } catch (ArgumentOutOfRangeException ex) {
2577 AssertEquals ("#C2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2578 AssertNull ("#C3", ex.InnerException);
2579 AssertNotNull ("#C4", ex.Message);
2580 AssertEquals ("#C5", "startIndex", ex.ParamName);
2584 int i = s1.LastIndexOf ("huh", s1.Length + 1, 3);
2586 } catch (ArgumentOutOfRangeException ex) {
2587 AssertEquals ("#D2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2588 AssertNull ("#D3", ex.InnerException);
2589 AssertNotNull ("#D4", ex.Message);
2590 AssertEquals ("#D5", "startIndex", ex.ParamName);
2594 s1.LastIndexOf (null);
2596 } catch (ArgumentNullException ex) {
2597 AssertEquals ("#E2", typeof (ArgumentNullException), ex.GetType ());
2598 AssertNull ("#E3", ex.InnerException);
2599 AssertNotNull ("#E4", ex.Message);
2601 AssertEquals ("#E5", "value", ex.ParamName);
2603 AssertEquals ("#E5", "string2", ex.ParamName);
2608 s1.LastIndexOf (null, 0);
2610 } catch (ArgumentNullException ex) {
2611 AssertEquals ("#F2", typeof (ArgumentNullException), ex.GetType ());
2612 AssertNull ("#F3", ex.InnerException);
2613 AssertNotNull ("#F4", ex.Message);
2615 AssertEquals ("#F5", "value", ex.ParamName);
2617 AssertEquals ("#F5", "string2", ex.ParamName);
2622 s1.LastIndexOf (null, 0, 1);
2624 } catch (ArgumentNullException ex) {
2625 AssertEquals ("#G2", typeof (ArgumentNullException), ex.GetType ());
2626 AssertNull ("#G3", ex.InnerException);
2627 AssertNotNull ("#G4", ex.Message);
2629 AssertEquals ("#G5", "value", ex.ParamName);
2631 AssertEquals ("#G5", "string2", ex.ParamName);
2635 AssertEquals("basic char index", 1, s1.LastIndexOf('r'));
2636 AssertEquals("basic char index", 4, s1.LastIndexOf('i'));
2637 AssertEquals("basic char index - no", -1, s1.LastIndexOf('q'));
2639 AssertEquals("basic string index", 7, s1.LastIndexOf(string.Empty));
2640 AssertEquals("basic string index", 1, s1.LastIndexOf("rig"));
2641 AssertEquals("basic string index", 4, s1.LastIndexOf("i"));
2642 AssertEquals("basic string index - no", -1,
2643 s1.LastIndexOf("rag"));
2645 AssertEquals("stepped char index", 1,
2646 s1.LastIndexOf('r', s1.Length-1));
2647 AssertEquals("stepped char index", 4,
2648 s1.LastIndexOf('i', s1.Length-1));
2649 AssertEquals("stepped char index", 2,
2650 s1.LastIndexOf('i', 3));
2651 AssertEquals("stepped char index", -1,
2652 s1.LastIndexOf('i', 1));
2654 AssertEquals("stepped limited char index",
2655 1, s1.LastIndexOf('r', 1, 1));
2656 AssertEquals("stepped limited char index",
2657 -1, s1.LastIndexOf('r', 0, 1));
2658 AssertEquals("stepped limited char index",
2659 4, s1.LastIndexOf('i', 6, 3));
2660 AssertEquals("stepped limited char index",
2661 2, s1.LastIndexOf('i', 3, 3));
2662 AssertEquals("stepped limited char index",
2663 -1, s1.LastIndexOf('i', 1, 2));
2665 s1 = "original original";
2666 AssertEquals("stepped string index #1",
2667 9, s1.LastIndexOf("original", s1.Length));
2668 AssertEquals("stepped string index #2",
2669 0, s1.LastIndexOf("original", s1.Length-2));
2670 AssertEquals("stepped string index #3",
2671 -1, s1.LastIndexOf("original", s1.Length-11));
2672 AssertEquals("stepped string index #4",
2673 -1, s1.LastIndexOf("translator", 2));
2674 AssertEquals("stepped string index #5",
2675 0, string.Empty.LastIndexOf(string.Empty, 0));
2677 AssertEquals("stepped string index #6",
2678 -1, string.Empty.LastIndexOf("A", -1));
2680 AssertEquals("stepped limited string index #1",
2681 10, s1.LastIndexOf("rig", s1.Length-1, 10));
2682 AssertEquals("stepped limited string index #2",
2683 -1, s1.LastIndexOf("rig", s1.Length, 3));
2684 AssertEquals("stepped limited string index #3",
2685 10, s1.LastIndexOf("rig", s1.Length-2, 15));
2686 AssertEquals("stepped limited string index #4",
2687 -1, s1.LastIndexOf("rig", s1.Length-2, 3));
2689 string s2 = "QBitArray::bitarr_data";
2690 AssertEquals ("bug #62160", 9, s2.LastIndexOf ("::"));
2692 string s3 = "test123";
2693 AssertEquals ("bug #77412", 0, s3.LastIndexOf ("test123"));
2698 [ExpectedException (typeof (ArgumentException))]
2699 public void LastIndexOf_StringComparison ()
2701 " ".LastIndexOf (string.Empty, 0, 1, (StringComparison)Int32.MinValue);
2705 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2706 public void LastIndexOfStringComparisonOrdinalRangeException1 ()
2708 "Mono".LastIndexOf ("no", 5, StringComparison.Ordinal);
2712 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2713 public void LastIndexOfStringComparisonOrdinalRangeException2 ()
2715 "Mono".LastIndexOf ("no", 1, 3, StringComparison.Ordinal);
2719 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2720 public void LastIndexOfStringComparisonOrdinalIgnoreCaseRangeException1 ()
2722 "Mono".LastIndexOf ("no", 5, StringComparison.OrdinalIgnoreCase);
2726 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2727 public void LastIndexOfStringComparisonOrdinalIgnoreCaseRangeException2 ()
2729 "Mono".LastIndexOf ("no", 1, 3, StringComparison.OrdinalIgnoreCase);
2733 public void LastIndexOfStringComparison ()
2735 string text = "testing123456";
2736 string text2 = "123";
2737 string text3 = "NG";
2739 AssertEquals ("#1-1", 7, text.LastIndexOf (text2, StringComparison.Ordinal));
2740 AssertEquals ("#2-1", 5, text.LastIndexOf (text3, StringComparison.OrdinalIgnoreCase));
2742 AssertEquals ("#1-2", 7, text.LastIndexOf (text2, 12, StringComparison.Ordinal));
2743 AssertEquals ("#2-2", 5, text.LastIndexOf (text3, 12, StringComparison.OrdinalIgnoreCase));
2745 AssertEquals ("#1-3", -1, text.LastIndexOf (text2, 0, StringComparison.Ordinal));
2746 AssertEquals ("#2-3", -1, text.LastIndexOf (text3, 0, StringComparison.OrdinalIgnoreCase));
2748 AssertEquals ("#1-4", -1, text.LastIndexOf (text2, 6, StringComparison.Ordinal));
2749 AssertEquals ("#2-4", 5, text.LastIndexOf (text3, 6, StringComparison.OrdinalIgnoreCase));
2751 AssertEquals ("#1-5", -1, text.LastIndexOf (text2, 7, 3, StringComparison.Ordinal));
2752 AssertEquals ("#2-5", 5, text.LastIndexOf (text3, 7, 3, StringComparison.OrdinalIgnoreCase));
2754 AssertEquals ("#1-6", -1, text.LastIndexOf (text2, 6, 0, StringComparison.Ordinal));
2755 AssertEquals ("#2-6", -1, text.LastIndexOf (text3, 5, 0, StringComparison.OrdinalIgnoreCase));
2757 AssertEquals ("#1-7", -1, text.LastIndexOf (text2, 7, 1, StringComparison.Ordinal));
2758 AssertEquals ("#2-7", -1, text.LastIndexOf (text3, 5, 1, StringComparison.OrdinalIgnoreCase));
2760 AssertEquals ("#3-1", 0, text.LastIndexOf (text4, 0, StringComparison.Ordinal));
2761 AssertEquals ("#3-2", 0, text.LastIndexOf (text4, 0, StringComparison.OrdinalIgnoreCase));
2763 AssertEquals ("#4-1", 3, text.LastIndexOf (text4, 13, StringComparison.Ordinal));
2764 AssertEquals ("#4-2", 3, text.LastIndexOf (text4, 13, StringComparison.OrdinalIgnoreCase));
2766 AssertEquals ("#4-1", 3, text.LastIndexOf (text4, 13, 14, StringComparison.Ordinal));
2767 AssertEquals ("#4-2", 3, text.LastIndexOf (text4, 13, 14, StringComparison.OrdinalIgnoreCase));
2769 AssertEquals ("#5-1", 0, text.LastIndexOf (text4, 1, 2, StringComparison.Ordinal));
2770 AssertEquals ("#5-2", 0, text.LastIndexOf (text4, 1, 2, StringComparison.OrdinalIgnoreCase));
2772 AssertEquals (-1, "".LastIndexOf ("FOO", StringComparison.Ordinal));
2773 AssertEquals (0, "".LastIndexOf ("", StringComparison.Ordinal));
2777 public void LastIndexOfStringComparisonOrdinal ()
2779 string text = "testing123456";
2780 AssertEquals ("#1", 10, text.LastIndexOf ("456", StringComparison.Ordinal));
2781 AssertEquals ("#2", -1, text.LastIndexOf ("4567", StringComparison.Ordinal));
2782 AssertEquals ("#3", 0, text.LastIndexOf ("te", StringComparison.Ordinal));
2783 AssertEquals ("#4", 2, text.LastIndexOf ("s", StringComparison.Ordinal));
2784 AssertEquals ("#5", -1, text.LastIndexOf ("ates", StringComparison.Ordinal));
2785 AssertEquals ("#6", -1, text.LastIndexOf ("S", StringComparison.Ordinal));
2789 public void LastIndexOfStringComparisonOrdinalIgnoreCase ()
2791 string text = "testing123456";
2792 AssertEquals ("#1", 10, text.LastIndexOf ("456", StringComparison.OrdinalIgnoreCase));
2793 AssertEquals ("#2", -1, text.LastIndexOf ("4567", StringComparison.OrdinalIgnoreCase));
2794 AssertEquals ("#3", 0, text.LastIndexOf ("te", StringComparison.OrdinalIgnoreCase));
2795 AssertEquals ("#4", 2, text.LastIndexOf ("s", StringComparison.OrdinalIgnoreCase));
2796 AssertEquals ("#5", -1, text.LastIndexOf ("ates", StringComparison.OrdinalIgnoreCase));
2797 AssertEquals ("#6", 2, text.LastIndexOf ("S", StringComparison.OrdinalIgnoreCase));
2802 public void LastIndexOf_Char_StartIndexStringLength ()
2806 s.LastIndexOf ('n', s.Length, 1);
2808 } catch (ArgumentOutOfRangeException ex) {
2809 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2810 AssertNull ("#3", ex.InnerException);
2811 AssertNotNull ("#4", ex.Message);
2812 AssertEquals ("#5", "startIndex", ex.ParamName);
2814 // this works for string but not for a char
2818 public void LastIndexOf_Char_StartIndexOverflow ()
2821 "Mono".LastIndexOf ('o', Int32.MaxValue, 1);
2823 } catch (ArgumentOutOfRangeException ex) {
2824 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2825 AssertNull ("#3", ex.InnerException);
2826 AssertNotNull ("#4", ex.Message);
2827 AssertEquals ("#5", "startIndex", ex.ParamName);
2832 public void LastIndexOf_Char_LengthOverflow ()
2835 "Mono".LastIndexOf ('o', 1, Int32.MaxValue);
2837 } catch (ArgumentOutOfRangeException ex) {
2838 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2839 AssertNull ("#3", ex.InnerException);
2840 AssertNotNull ("#4", ex.Message);
2841 AssertEquals ("#5", "count", ex.ParamName);
2846 public void LastIndexOf_String_StartIndexStringLength ()
2849 AssertEquals (-1, s.LastIndexOf ("n", s.Length, 1));
2850 // this works for string but not for a char
2854 public void LastIndexOf_String_StartIndexStringLength_Plus1 ()
2858 s.LastIndexOf ("n", s.Length + 1, 1);
2860 } catch (ArgumentOutOfRangeException ex) {
2861 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2862 AssertNull ("#3", ex.InnerException);
2863 AssertNotNull ("#4", ex.Message);
2864 AssertEquals ("#5", "startIndex", ex.ParamName);
2869 public void LastIndexOf_String_StartIndexOverflow ()
2872 "Mono".LastIndexOf ("no", Int32.MaxValue, 1);
2874 } catch (ArgumentOutOfRangeException ex) {
2875 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2876 AssertNull ("#3", ex.InnerException);
2877 AssertNotNull ("#4", ex.Message);
2878 AssertEquals ("#5", "startIndex", ex.ParamName);
2883 public void LastIndexOf_String_LengthOverflow ()
2886 "Mono".LastIndexOf ("no", 1, Int32.MaxValue);
2888 } catch (ArgumentOutOfRangeException ex) {
2889 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2890 AssertNull ("#3", ex.InnerException);
2891 AssertNotNull ("#4", ex.Message);
2892 AssertEquals ("#5", "count", ex.ParamName);
2897 public void LastIndexOfAny ()
2899 string s1 = ".bcdefghijklm";
2902 s1.LastIndexOfAny (null);
2904 } catch (ArgumentNullException ex) {
2905 AssertEquals ("#A2", typeof (ArgumentNullException), ex.GetType ());
2906 AssertNull ("#A3", ex.InnerException);
2907 AssertNotNull ("#A4", ex.Message);
2908 AssertNull ("#A5", ex.ParamName);
2912 s1.LastIndexOfAny (null, s1.Length);
2914 } catch (ArgumentNullException ex) {
2915 AssertEquals ("#B2", typeof (ArgumentNullException), ex.GetType ());
2916 AssertNull ("#B3", ex.InnerException);
2917 AssertNotNull ("#B4", ex.Message);
2918 AssertNull ("#B5", ex.ParamName);
2922 s1.LastIndexOfAny (null, s1.Length, 1);
2924 } catch (ArgumentNullException ex) {
2925 AssertEquals ("#C2", typeof (ArgumentNullException), ex.GetType ());
2926 AssertNull ("#C3", ex.InnerException);
2927 AssertNotNull ("#C4", ex.Message);
2928 AssertNull ("#C5", ex.ParamName);
2931 char[] c1 = {'a', 'e', 'i', 'o', 'u'};
2932 AssertEquals("#D1", 8, s1.LastIndexOfAny (c1));
2933 AssertEquals("#D2", 4, s1.LastIndexOfAny (c1, 7));
2934 AssertEquals("#D3", -1, s1.LastIndexOfAny (c1, 3));
2935 AssertEquals("#D4", 4, s1.LastIndexOfAny (c1, s1.Length - 6, 4));
2936 AssertEquals("#D5", -1, s1.LastIndexOfAny (c1, s1.Length - 6, 3));
2939 s1.LastIndexOfAny (c1, -1);
2941 } catch (ArgumentOutOfRangeException ex) {
2942 // Index was out of range. Must be non-negative and
2943 // less than the size of the collection
2944 AssertEquals ("#E2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2945 AssertNull ("#E3", ex.InnerException);
2946 AssertNotNull ("#E4", ex.Message);
2947 AssertEquals ("#E5", "startIndex", ex.ParamName);
2951 s1.LastIndexOfAny (c1, -1, 1);
2953 } catch (ArgumentOutOfRangeException ex) {
2954 // Index was out of range. Must be non-negative and
2955 // less than the size of the collection
2956 AssertEquals ("#F2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2957 AssertNull ("#F3", ex.InnerException);
2958 AssertNotNull ("#F4", ex.Message);
2959 AssertEquals ("#F5", "startIndex", ex.ParamName);
2964 public void LastIndexOfAny_Length_Overflow ()
2967 "Mono".LastIndexOfAny (new char [1] { 'o' }, 1, Int32.MaxValue);
2969 } catch (ArgumentOutOfRangeException ex) {
2970 // Count must be positive and count must refer to a
2971 // location within the string/array/collection
2972 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2973 AssertNull ("#3", ex.InnerException);
2974 AssertNotNull ("#4", ex.Message);
2975 AssertEquals ("#5", "count", ex.ParamName);
2980 public void LastIndexOfAny_StartIndex_Overflow ()
2983 "Mono".LastIndexOfAny (new char [1] { 'o' }, Int32.MaxValue, 1);
2985 } catch (ArgumentOutOfRangeException ex) {
2986 // Index was out of range. Must be non-negative and
2987 // less than the size of the collection
2988 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
2989 AssertNull ("#3", ex.InnerException);
2990 AssertNotNull ("#4", ex.Message);
2991 AssertEquals ("#5", "startIndex", ex.ParamName);
2995 [Test] // PadLeft (Int32)
2996 public void PadLeft1 ()
3001 result = s1.PadLeft (0);
3002 AssertSame ("#A", s1, result);
3004 result = s1.PadLeft (s1.Length - 1);
3005 AssertSame ("#B", s1, result);
3007 result = s1.PadLeft (s1.Length);
3008 AssertEquals ("#C1", s1, result);
3009 Assert ("#C2", !object.ReferenceEquals (s1, result));
3011 result = s1.PadLeft (s1.Length + 1);
3012 AssertEquals("#D", " Hi!", result);
3015 [Test] // PadLeft (Int32)
3016 public void PadLeft1_TotalWidth_Negative ()
3019 "Mono".PadLeft (-1);
3021 } catch (ArgumentOutOfRangeException ex) {
3022 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3023 AssertNull ("#3", ex.InnerException);
3024 AssertNotNull ("#4", ex.Message);
3025 AssertEquals ("#5", "totalWidth", ex.ParamName);
3029 [Test] // PadRight (Int32)
3030 public void PadRight1 ()
3035 result = s1.PadRight (0);
3036 AssertSame ("#A", s1, result);
3038 result = s1.PadRight (s1.Length - 1);
3039 AssertSame ("#B", s1, result);
3041 result = s1.PadRight (s1.Length);
3042 AssertEquals ("#C1", s1, result);
3043 Assert ("#C2", !object.ReferenceEquals (s1, result));
3045 result = s1.PadRight (s1.Length + 1);
3046 AssertEquals("#D", "Hi! ", result);
3049 [Test] // PadRight1 (Int32)
3050 public void PadRight1_TotalWidth_Negative ()
3053 "Mono".PadRight (-1);
3055 } catch (ArgumentOutOfRangeException ex) {
3056 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3057 AssertNull ("#3", ex.InnerException);
3058 AssertNotNull ("#4", ex.Message);
3059 AssertEquals ("#5", "totalWidth", ex.ParamName);
3063 [Test] // Remove (Int32, Int32)
3064 public void Remove2 ()
3066 string s1 = "original";
3071 } catch (ArgumentOutOfRangeException ex) {
3072 // StartIndex cannot be less than zero
3073 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3074 AssertNull ("#A3", ex.InnerException);
3075 AssertNotNull ("#A4", ex.Message);
3076 AssertEquals ("#A5", "startIndex", ex.ParamName);
3082 } catch (ArgumentOutOfRangeException ex) {
3083 // Count cannot be less than zero
3084 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3085 AssertNull ("#B3", ex.InnerException);
3086 AssertNotNull ("#B4", ex.Message);
3087 AssertEquals ("#B5", "count", ex.ParamName);
3091 s1.Remove (s1.Length, s1.Length);
3093 } catch (ArgumentOutOfRangeException ex) {
3094 // Index and count must refer to a location within the
3096 AssertEquals ("#C2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3097 AssertNull ("#C3", ex.InnerException);
3098 AssertNotNull ("#C4", ex.Message);
3099 AssertEquals ("#C5", "count", ex.ParamName);
3102 AssertEquals ("#D1", "oinal", s1.Remove(1, 3));
3103 AssertEquals ("#D2", s1, s1.Remove (0, 0));
3104 Assert ("#D3", !object.ReferenceEquals (s1, s1.Remove (0, 0)));
3105 AssertEquals ("#D4", "riginal", s1.Remove (0, 1));
3106 AssertEquals ("#D5", "origina", s1.Remove (7, 1));
3109 [Test] // Remove (Int32, Int32)
3110 public void Remove2_Length_Overflow ()
3113 "Mono".Remove (1, Int32.MaxValue);
3115 } catch (ArgumentOutOfRangeException ex) {
3116 // Index and count must refer to a location within the
3118 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3119 AssertNull ("#3", ex.InnerException);
3120 AssertNotNull ("#4", ex.Message);
3121 AssertEquals ("#5", "count", ex.ParamName);
3125 [Test] // Remove (Int32, Int32)
3126 public void Remove2_StartIndex_Overflow ()
3129 "Mono".Remove (Int32.MaxValue, 1);
3131 } catch (ArgumentOutOfRangeException ex) {
3132 // Index and count must refer to a location within the
3134 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3135 AssertNull ("#3", ex.InnerException);
3136 AssertNotNull ("#4", ex.Message);
3137 AssertEquals ("#5", "count", ex.ParamName);
3142 [Test] // Remove (Int32)
3143 public void Remove1_StartIndex_Negative ()
3148 } catch (ArgumentOutOfRangeException ex) {
3149 // StartIndex cannot be less than zero
3150 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3151 AssertNull ("#3", ex.InnerException);
3152 AssertNotNull ("#4", ex.Message);
3153 AssertEquals ("#5", "startIndex", ex.ParamName);
3157 [Test] // Remove (Int32)
3158 public void Remove1_StartIndex_Overflow ()
3163 } catch (ArgumentOutOfRangeException ex) {
3164 // startIndex must be less than length of string
3165 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3166 AssertNull ("#3", ex.InnerException);
3167 AssertNotNull ("#4", ex.Message);
3168 AssertEquals ("#5", "startIndex", ex.ParamName);
3172 [Test] // Remove (Int32)
3173 public void Remove1 ()
3177 AssertEquals ("#1", "AB", s.Remove (2));
3178 AssertEquals ("#2", string.Empty, s.Remove (0));
3179 AssertEquals ("#3", "A", s.Remove (1));
3184 public void Replace()
3186 string s1 = "original";
3188 AssertEquals("non-hit char", s1, s1.Replace('q','s'));
3189 AssertEquals("single char", "oxiginal", s1.Replace('r', 'x'));
3190 AssertEquals("double char", "orxgxnal", s1.Replace('i', 'x'));
3192 bool errorThrown = false;
3194 string s = s1.Replace(null, "feh");
3195 } catch (ArgumentNullException) {
3198 Assert("should get null arg exception", errorThrown);
3200 AssertEquals("replace as remove", "ornal",
3201 s1.Replace("igi", null));
3202 AssertEquals("non-hit string", s1, s1.Replace("spam", "eggs"));
3203 AssertEquals("single string", "orirumal",
3204 s1.Replace("gin", "rum"));
3205 AssertEquals("double string", "oreigeinal",
3206 s1.Replace("i", "ei"));
3208 AssertEquals("start", "ooriginal", s1.Replace("o", "oo"));
3209 AssertEquals("end", "originall", s1.Replace("l", "ll"));
3211 AssertEquals("start empty", "riginal", s1.Replace("o", string.Empty));
3212 AssertEquals("end empty", "origina", s1.Replace("l", string.Empty));
3214 AssertEquals("replace bigger that original", "original", s1.Replace("original2", "original3"));
3216 AssertEquals ("result longer", ":!:", "::".Replace ("::", ":!:"));
3218 // Test overlapping matches (bug #54988)
3219 string s2 = "...aaaaaaa.bbbbbbbbb,............ccccccc.u...";
3220 AssertEquals ("..aaaaaaa.bbbbbbbbb,......ccccccc.u..", s2.Replace("..", "."));
3222 // Test replacing null characters (bug #67395)
3223 #if !TARGET_JVM //bug #7276
3224 AssertEquals ("should not strip content after nullchar",
3225 "is this ok ?", "is \0 ok ?".Replace ("\0", "this"));
3230 public void ReplaceStringBeginEndTest ()
3232 string s1 = "original";
3234 AssertEquals ("#1", "riginal", s1.Replace ("o", ""));
3235 AssertEquals ("#2", "origina", s1.Replace ("l", ""));
3236 AssertEquals ("#3", "ariginal", s1.Replace ("o", "a"));
3237 AssertEquals ("#4", "originaa", s1.Replace ("l", "a"));
3238 AssertEquals ("#5", "aariginal", s1.Replace ("o", "aa"));
3239 AssertEquals ("#6", "originaaa", s1.Replace ("l", "aa"));
3240 AssertEquals ("#7", "original", s1.Replace ("o", "o"));
3241 AssertEquals ("#8", "original", s1.Replace ("l", "l"));
3242 AssertEquals ("#9", "original", s1.Replace ("original", "original"));
3243 AssertEquals ("#10", "", s1.Replace ("original", ""));
3247 public void ReplaceStringBeginEndTestFallback ()
3249 string prev = new String ('o', 300);
3250 string s1 = prev + "riginal";
3252 AssertEquals ("#1", "riginal", s1.Replace ("o", ""));
3253 AssertEquals ("#2", prev + "rigina", s1.Replace ("l", ""));
3254 AssertEquals ("#3", new String ('a', 300) + "riginal", s1.Replace ("o", "a"));
3255 AssertEquals ("#4", prev + "riginaa", s1.Replace ("l", "a"));
3256 AssertEquals ("#5", new String ('a', 600) + "riginal", s1.Replace ("o", "aa"));
3257 AssertEquals ("#6", prev + "riginaaa", s1.Replace ("l", "aa"));
3258 AssertEquals ("#7", s1, s1.Replace ("o", "o"));
3259 AssertEquals ("#8", s1, s1.Replace ("l", "l"));
3260 AssertEquals ("#9", s1, s1.Replace (s1, s1));
3261 AssertEquals ("#10", "", s1.Replace (prev + "riginal", ""));
3265 public void ReplaceStringOffByOne ()
3267 AssertEquals ("#-1", "", new String ('o', 199).Replace ("o", ""));
3268 AssertEquals ("#0", "", new String ('o', 200).Replace ("o", ""));
3269 AssertEquals ("#+1", "", new String ('o', 201).Replace ("o", ""));
3273 public void ReplaceStringCultureTests ()
3275 // LAMESPEC: According to MSDN Replace with String parameter is culture-senstive.
3276 // However this does not currently seem to be the case. Otherwise following code should
3277 // produce "check" instead of "AE"
3279 CultureInfo old = Thread.CurrentThread.CurrentCulture;
3280 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
3281 AssertEquals ("#1", "AE", "AE".Replace ("\u00C6", "check"));
3282 Thread.CurrentThread.CurrentCulture = old;
3285 [Test] // StartsWith (String)
3286 public void StartsWith1 ()
3288 string s1 = "original";
3290 Assert ("#1", s1.StartsWith ("o"));
3291 Assert ("#2", s1.StartsWith ("orig"));
3292 Assert ("#3", !s1.StartsWith ("rig"));
3293 Assert ("#4", s1.StartsWith (String.Empty));
3294 Assert ("#5", String.Empty.StartsWith (String.Empty));
3295 Assert ("#6", !String.Empty.StartsWith ("rig"));
3298 [Test] // StartsWith (String)
3299 public void StartsWith1_Value_Null ()
3302 "A".StartsWith (null);
3304 } catch (ArgumentNullException ex) {
3305 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
3306 AssertNull ("#3", ex.InnerException);
3307 AssertNotNull ("#4", ex.Message);
3308 AssertEquals ("#5", "value", ex.ParamName);
3313 [Test] // StartsWith (String, StringComparison)
3314 public void StartsWith2_ComparisonType_Invalid ()
3317 "ABC".StartsWith ("A", (StringComparison) 80);
3319 } catch (ArgumentException ex) {
3320 // The string comparison type passed in is currently
3322 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
3323 AssertNull ("#3", ex.InnerException);
3324 AssertNotNull ("#4", ex.Message);
3325 AssertEquals ("#5", "comparisonType", ex.ParamName);
3329 [Test] // StartsWith (String, StringComparison)
3330 public void StartsWith2_Value_Null ()
3333 "A".StartsWith (null, StringComparison.CurrentCulture);
3335 } catch (ArgumentNullException ex) {
3336 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
3337 AssertNull ("#3", ex.InnerException);
3338 AssertNotNull ("#4", ex.Message);
3339 AssertEquals ("#5", "value", ex.ParamName);
3343 [Test] // StartsWith (String, Boolean, CultureInfo)
3344 public void StartsWith3_Culture_Null ()
3346 // This should not crash
3349 s.StartsWith ("this", true, null);
3353 [Test] // SubString (Int32)
3354 public void Substring1 ()
3356 string s = "original";
3358 AssertEquals("#1", "inal", s.Substring (4));
3359 AssertEquals ("#2", string.Empty, s.Substring (s.Length));
3361 AssertSame ("#3", s, s.Substring (0));
3363 AssertEquals ("#3a", s, s.Substring (0));
3364 Assert ("#3b", !object.ReferenceEquals (s, s.Substring (0)));
3368 [Test] // SubString (Int32)
3369 public void SubString1_StartIndex_Negative ()
3371 string s = "original";
3376 } catch (ArgumentOutOfRangeException ex) {
3377 // StartIndex cannot be less than zero
3378 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3379 AssertNull ("#3", ex.InnerException);
3380 AssertNotNull ("#4", ex.Message);
3381 AssertEquals ("#5", "startIndex", ex.ParamName);
3385 [Test] // SubString (Int32)
3386 public void SubString1_StartIndex_Overflow ()
3388 string s = "original";
3391 s.Substring (s.Length + 1);
3393 } catch (ArgumentOutOfRangeException ex) {
3394 // startIndex cannot be larger than length of string
3395 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3396 AssertNull ("#3", ex.InnerException);
3397 AssertNotNull ("#4", ex.Message);
3399 AssertEquals ("#5", "startIndex", ex.ParamName);
3401 AssertEquals ("#5", "length", ex.ParamName);
3406 [Test] // SubString (Int32, Int32)
3407 public void Substring2 ()
3409 string s = "original";
3411 AssertEquals ("#1", "igin", s.Substring (2, 4));
3412 AssertEquals ("#2", string.Empty, s.Substring (s.Length, 0));
3413 AssertEquals ("#3", "origina", s.Substring (0, s.Length - 1));
3414 AssertEquals ("#4", s, s.Substring (0, s.Length));
3416 AssertSame ("#5", s, s.Substring (0, s.Length));
3418 Assert ("#5", !object.ReferenceEquals (s, s.Substring (0, s.Length)));
3422 [Test] // SubString (Int32, Int32)
3423 public void SubString2_Length_Negative ()
3425 string s = "original";
3428 s.Substring (1, -1);
3430 } catch (ArgumentOutOfRangeException ex) {
3431 // Length cannot be less than zero
3432 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3433 AssertNull ("#3", ex.InnerException);
3434 AssertNotNull ("#4", ex.Message);
3435 AssertEquals ("#5", "length", ex.ParamName);
3439 [Test] // SubString (Int32, Int32)
3440 public void Substring2_Length_Overflow ()
3442 string s = "original";
3445 s.Substring (s.Length, 1);
3447 } catch (ArgumentOutOfRangeException ex) {
3448 // Index and length must refer to a location within
3450 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3451 AssertNull ("#A3", ex.InnerException);
3452 AssertNotNull ("#A4", ex.Message);
3453 AssertEquals ("#A5", "length", ex.ParamName);
3457 s.Substring (1, s.Length);
3459 } catch (ArgumentOutOfRangeException ex) {
3460 // Index and length must refer to a location within
3462 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3463 AssertNull ("#B3", ex.InnerException);
3464 AssertNotNull ("#B4", ex.Message);
3465 AssertEquals ("#B5", "length", ex.ParamName);
3469 s.Substring (1, Int32.MaxValue);
3471 } catch (ArgumentOutOfRangeException ex) {
3472 // Index and length must refer to a location within
3474 AssertEquals ("#C2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3475 AssertNull ("#C3", ex.InnerException);
3476 AssertNotNull ("#C4", ex.Message);
3477 AssertEquals ("#C5", "length", ex.ParamName);
3481 [Test] // SubString (Int32, Int32)
3482 public void SubString2_StartIndex_Negative ()
3484 string s = "original";
3487 s.Substring (-1, 1);
3489 } catch (ArgumentOutOfRangeException ex) {
3490 // StartIndex cannot be less than zero
3491 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3492 AssertNull ("#3", ex.InnerException);
3493 AssertNotNull ("#4", ex.Message);
3494 AssertEquals ("#5", "startIndex", ex.ParamName);
3498 [Test] // SubString (Int32, Int32)
3499 public void Substring2_StartIndex_Overflow ()
3501 string s = "original";
3504 s.Substring (s.Length + 1, 0);
3506 } catch (ArgumentOutOfRangeException ex) {
3507 // startIndex cannot be larger than length of string
3508 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3509 AssertNull ("#A3", ex.InnerException);
3510 AssertNotNull ("#A4", ex.Message);
3512 AssertEquals ("#A5", "startIndex", ex.ParamName);
3514 AssertEquals ("#A5", "length", ex.ParamName);
3519 "Mono".Substring (Int32.MaxValue, 1);
3521 } catch (ArgumentOutOfRangeException ex) {
3522 // startIndex cannot be larger than length of string
3523 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3524 AssertNull ("#B3", ex.InnerException);
3525 AssertNotNull ("#B4", ex.Message);
3527 AssertEquals ("#B5", "startIndex", ex.ParamName);
3529 AssertEquals ("#B5", "length", ex.ParamName);
3535 public void ToCharArray ()
3537 const string s = "original";
3540 c = s.ToCharArray ();
3541 AssertEquals ("#A1", s.Length, c.Length);
3542 AssertEquals ("#A2", s, new String (c));
3544 c = s.ToCharArray (0, s.Length);
3545 AssertEquals ("#B1", s.Length, c.Length);
3546 AssertEquals ("#B2", s, new String (c));
3548 c = s.ToCharArray (1, s.Length - 1);
3549 AssertEquals ("#C1", 7, c.Length);
3550 AssertEquals ("#C2", "riginal", new String (c));
3552 c = s.ToCharArray (0, 3);
3553 AssertEquals ("#D1", 3, c.Length);
3554 AssertEquals ("#D2", "ori", new String (c));
3556 c = s.ToCharArray (2, 0);
3557 AssertEquals ("#E1", 0, c.Length);
3558 AssertEquals ("#E2", string.Empty, new String (c));
3560 c = s.ToCharArray (3, 2);
3561 AssertEquals ("#F1", 2, c.Length);
3562 AssertEquals ("#F2", "gi", new String (c));
3564 c = s.ToCharArray (s.Length, 0);
3565 AssertEquals ("#G1", 0, c.Length);
3566 AssertEquals ("#G2", string.Empty, new String (c));
3570 public void ToCharArray_Length_Negative ()
3572 const string s = "original";
3575 s.ToCharArray (1, -1);
3577 } catch (ArgumentOutOfRangeException ex) {
3578 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3579 AssertNull ("#3", ex.InnerException);
3580 AssertNotNull ("#4", ex.Message);
3581 AssertEquals ("#5", "length", ex.ParamName);
3586 public void ToCharArray_Length_Overflow ()
3588 const string s = "original";
3591 s.ToCharArray (1, s.Length);
3593 } catch (ArgumentOutOfRangeException ex) {
3594 // Index was out of range. Must be non-negative and
3595 // less than the size of the collection
3596 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3597 AssertNull ("#A3", ex.InnerException);
3598 AssertNotNull ("#A4", ex.Message);
3599 AssertEquals ("#A5", "startIndex", ex.ParamName);
3603 s.ToCharArray (1, Int32.MaxValue);
3605 } catch (ArgumentOutOfRangeException ex) {
3606 // Index was out of range. Must be non-negative and
3607 // less than the size of the collection
3608 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3609 AssertNull ("#B3", ex.InnerException);
3610 AssertNotNull ("#B4", ex.Message);
3611 AssertEquals ("#B5", "startIndex", ex.ParamName);
3616 public void ToCharArray_StartIndex_Negative ()
3618 const string s = "original";
3621 s.ToCharArray (-1, 1);
3623 } catch (ArgumentOutOfRangeException ex) {
3624 // Index was out of range. Must be non-negative and
3625 // less than the size of the collection
3626 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3627 AssertNull ("#3", ex.InnerException);
3628 AssertNotNull ("#4", ex.Message);
3629 AssertEquals ("#5", "startIndex", ex.ParamName);
3634 public void ToCharArray_StartIndex_Overflow ()
3636 const string s = "original";
3639 s.ToCharArray (s.Length, 1);
3641 } catch (ArgumentOutOfRangeException ex) {
3642 // Index was out of range. Must be non-negative and
3643 // less than the size of the collection
3644 AssertEquals ("#A2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3645 AssertNull ("#A3", ex.InnerException);
3646 AssertNotNull ("#A4", ex.Message);
3647 AssertEquals ("#A5", "startIndex", ex.ParamName);
3651 s.ToCharArray (Int32.MaxValue, 1);
3653 } catch (ArgumentOutOfRangeException ex) {
3654 // Index was out of range. Must be non-negative and
3655 // less than the size of the collection
3656 AssertEquals ("#B2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3657 AssertNull ("#B3", ex.InnerException);
3658 AssertNotNull ("#B4", ex.Message);
3659 AssertEquals ("#B5", "startIndex", ex.ParamName);
3663 [Test] // ToLower ()
3664 public void ToLower1 ()
3666 string s = "OrIgInAli";
3668 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
3670 AssertEquals ("#1", "\u006f\u0072\u0131\u0067\u0131\u006e\u0061\u006c\u0069", s.ToLower());
3672 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
3674 AssertEquals ("#2", "originali", s.ToLower ());
3677 [Test] // ToLower (CultureInfo)
3678 public void ToLower2 ()
3680 string s = "OrIgInAli";
3682 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
3684 AssertEquals ("#A1", "originali", s.ToLower (new CultureInfo ("en-US")));
3685 AssertEquals ("#A2", "\u006f\u0072\u0131\u0067\u0131\u006e\u0061\u006c\u0069",
3686 s.ToLower (new CultureInfo ("tr-TR")));
3687 AssertEquals ("#A3", string.Empty, string.Empty.ToLower (new CultureInfo ("en-US")));
3688 AssertEquals ("#A4", string.Empty, string.Empty.ToLower (new CultureInfo ("tr-TR")));
3690 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
3692 AssertEquals ("#B1", "originali", s.ToLower (new CultureInfo ("en-US")));
3693 AssertEquals ("#B2", "\u006f\u0072\u0131\u0067\u0131\u006e\u0061\u006c\u0069",
3694 s.ToLower (new CultureInfo ("tr-TR")));
3695 AssertEquals ("#B3", string.Empty, string.Empty.ToLower (new CultureInfo ("en-US")));
3696 AssertEquals ("#B4", string.Empty, string.Empty.ToLower (new CultureInfo ("tr-TR")));
3699 [Test] // ToLower (CultureInfo)
3700 public void ToLower2_Culture_Null ()
3702 string s = "OrIgInAl";
3705 s.ToLower ((CultureInfo) null);
3707 } catch (ArgumentNullException ex) {
3708 AssertEquals ("#A2", typeof (ArgumentNullException), ex.GetType ());
3709 AssertNull ("#A3", ex.InnerException);
3710 AssertNotNull ("#A4", ex.Message);
3711 AssertEquals ("#A5", "culture", ex.ParamName);
3715 string.Empty.ToLower ((CultureInfo) null);
3717 } catch (ArgumentNullException ex) {
3718 AssertEquals ("#B2", typeof (ArgumentNullException), ex.GetType ());
3719 AssertNull ("#B3", ex.InnerException);
3720 AssertNotNull ("#B4", ex.Message);
3721 AssertEquals ("#B5", "culture", ex.ParamName);
3726 public void TestToString ()
3728 string s1 = "OrIgInAli";
3729 AssertEquals("ToString failed!", s1, s1.ToString());
3732 [Test] // ToUpper ()
3733 public void ToUpper1 ()
3735 string s = "OrIgInAli";
3737 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
3739 AssertEquals ("#1", "ORIGINAL\u0130", s.ToUpper ());
3741 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
3743 AssertEquals ("#2", "ORIGINALI", s.ToUpper ());
3746 [Test] // ToUpper (CultureInfo)
3747 public void ToUpper2 ()
3749 string s = "OrIgInAli";
3751 Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
3753 AssertEquals ("#A1", "ORIGINALI", s.ToUpper (new CultureInfo ("en-US")));
3754 AssertEquals ("#A2", "ORIGINAL\u0130", s.ToUpper (new CultureInfo ("tr-TR")));
3755 AssertEquals ("#A3", string.Empty, string.Empty.ToUpper (new CultureInfo ("en-US")));
3756 AssertEquals ("#A4", string.Empty, string.Empty.ToUpper (new CultureInfo ("tr-TR")));
3758 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
3760 AssertEquals ("#B1", "ORIGINALI", s.ToUpper (new CultureInfo ("en-US")));
3761 AssertEquals ("#B2", "ORIGINAL\u0130", s.ToUpper (new CultureInfo ("tr-TR")));
3762 AssertEquals ("#B3", string.Empty, string.Empty.ToUpper (new CultureInfo ("en-US")));
3763 AssertEquals ("#B4", string.Empty, string.Empty.ToUpper (new CultureInfo ("tr-TR")));
3766 [Test] // ToUpper (CultureInfo)
3767 public void ToUpper2_Culture_Null ()
3769 string s = "OrIgInAl";
3772 s.ToUpper ((CultureInfo) null);
3774 } catch (ArgumentNullException ex) {
3775 AssertEquals ("#A2", typeof (ArgumentNullException), ex.GetType ());
3776 AssertNull ("#A3", ex.InnerException);
3777 AssertNotNull ("#A4", ex.Message);
3778 AssertEquals ("#A5", "culture", ex.ParamName);
3782 string.Empty.ToUpper ((CultureInfo) null);
3784 } catch (ArgumentNullException ex) {
3785 AssertEquals ("#B2", typeof (ArgumentNullException), ex.GetType ());
3786 AssertNull ("#B3", ex.InnerException);
3787 AssertNotNull ("#B4", ex.Message);
3788 AssertEquals ("#B5", "culture", ex.ParamName);
3793 public void TestTrim ()
3795 string s1 = " original\t\n";
3796 AssertEquals("basic trim failed", "original", s1.Trim());
3797 AssertEquals("basic trim failed", "original", s1.Trim(null));
3800 AssertEquals("basic trim failed", "original", s1.Trim());
3801 AssertEquals("basic trim failed", "original", s1.Trim(null));
3804 AssertEquals("empty trim failed", string.Empty, s1.Trim());
3805 AssertEquals("empty trim failed", string.Empty, s1.Trim(null));
3807 s1 = "aaaoriginalbbb";
3808 char[] delims = {'a', 'b'};
3809 AssertEquals("custom trim failed",
3810 "original", s1.Trim(delims));
3813 AssertEquals ("net_2_0 additional char#1", "original", "\u2028original\u2029".Trim ());
3814 AssertEquals ("net_2_0 additional char#2", "original", "\u0085original\u1680".Trim ());
3819 public void TestTrimEnd ()
3821 string s1 = " original\t\n";
3822 AssertEquals("basic TrimEnd failed",
3823 " original", s1.TrimEnd(null));
3826 AssertEquals("basic TrimEnd failed",
3827 " original", s1.TrimEnd(null));
3830 AssertEquals("empty TrimEnd failed",
3831 string.Empty, s1.TrimEnd(null));
3833 s1 = "aaaoriginalbbb";
3834 char[] delims = {'a', 'b'};
3835 AssertEquals("custom TrimEnd failed",
3836 "aaaoriginal", s1.TrimEnd(delims));
3840 public void TestTrimStart ()
3842 string s1 = " original\t\n";
3843 AssertEquals("basic TrimStart failed",
3844 "original\t\n", s1.TrimStart(null));
3846 s1 = "original\t\n";
3847 AssertEquals("basic TrimStart failed",
3848 "original\t\n", s1.TrimStart(null));
3851 AssertEquals("empty TrimStart failed",
3852 string.Empty, s1.TrimStart(null));
3854 s1 = "aaaoriginalbbb";
3855 char[] delims = {'a', 'b'};
3856 AssertEquals("custom TrimStart failed",
3857 "originalbbb", s1.TrimStart(delims));
3861 public void TestChars ()
3869 } catch (IndexOutOfRangeException ex) {
3870 AssertEquals ("#A2", typeof (IndexOutOfRangeException), ex.GetType ());
3871 AssertNull ("#A3", ex.InnerException);
3872 AssertNotNull ("#A4", ex.Message);
3879 } catch (IndexOutOfRangeException ex) {
3880 AssertEquals ("#B2", typeof (IndexOutOfRangeException), ex.GetType ());
3881 AssertNull ("#B3", ex.InnerException);
3882 AssertNotNull ("#B4", ex.Message);
3887 public void TestComparePeriod ()
3889 // according to bug 63981, this behavior is for all cultures
3890 AssertEquals ("#1", -1, String.Compare ("foo.obj", "foobar.obj", false));
3894 public void LastIndexOfAnyBounds1 ()
3896 string mono = "Mono";
3897 char [] k = { 'M' };
3899 mono.LastIndexOfAny (k, mono.Length, 1);
3901 } catch (ArgumentOutOfRangeException ex) {
3902 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
3903 AssertNull ("#3", ex.InnerException);
3904 AssertNotNull ("#4", ex.Message);
3905 AssertEquals ("#5", "startIndex", ex.ParamName);
3910 public void TestSplit ()
3912 string s1 = "abcdefghijklm";
3913 char[] c1 = {'q', 'r'};
3914 AssertEquals("No splitters", s1, (s1.Split(c1))[0]);
3916 char[] c2 = {'a', 'e', 'i', 'o', 'u'};
3917 string[] chunks = s1.Split(c2);
3918 AssertEquals("First chunk", string.Empty, chunks[0]);
3919 AssertEquals("Second chunk", "bcd", chunks[1]);
3920 AssertEquals("Third chunk", "fgh", chunks[2]);
3921 AssertEquals("Fourth chunk", "jklm", chunks[3]);
3924 bool errorThrown = false;
3926 chunks = s1.Split(c2, -1);
3927 } catch (ArgumentOutOfRangeException) {
3930 Assert("Split out of range", errorThrown);
3933 chunks = s1.Split(c2, 2);
3934 AssertEquals("Limited chunk", 2, chunks.Length);
3935 AssertEquals("First limited chunk", string.Empty, chunks[0]);
3936 AssertEquals("Second limited chunk", "bcdefghijklm", chunks[1]);
3940 chunks = s3.Split(c3,2);
3941 AssertEquals("1.0 split length", 2, chunks.Length);
3942 AssertEquals("1.0 split first chunk", "1", chunks[0]);
3943 AssertEquals("1.0 split second chunk", "0", chunks[1]);
3945 string s4 = "1.0.0";
3947 chunks = s4.Split(c4,2);
3948 AssertEquals("1.0.0 split length", 2, chunks.Length);
3949 AssertEquals("1.0.0 split first chunk", "1", chunks[0]);
3950 AssertEquals("1.0.0 split second chunk", "0.0", chunks[1]);
3954 chunks = s5.Split (c5, 2);
3955 AssertEquals(".0.0 split length", 2, chunks.Length);
3956 AssertEquals(".0.0 split first chunk", string.Empty, chunks[0]);
3957 AssertEquals(".0.0 split second chunk", "0.0", chunks[1]);
3961 chunks = s6.Split (c6, 2);
3962 AssertEquals(".0 split length", 2, chunks.Length);
3963 AssertEquals(".0 split first chunk", string.Empty, chunks[0]);
3964 AssertEquals(".0 split second chunk", "0", chunks[1]);
3968 chunks = s7.Split (c7, 2);
3969 AssertEquals("0. split length", 2, chunks.Length);
3970 AssertEquals("0. split first chunk", "0", chunks[0]);
3971 AssertEquals("0. split second chunk", string.Empty, chunks[1]);
3973 string s8 = "0.0000";
3975 chunks = s8.Split (c8, 2);
3976 AssertEquals("0.0000/2 split length", 2, chunks.Length);
3977 AssertEquals("0.0000/2 split first chunk", "0", chunks[0]);
3978 AssertEquals("0.0000/2 split second chunk", "0000", chunks[1]);
3980 chunks = s8.Split (c8, 3);
3981 AssertEquals("0.0000/3 split length", 2, chunks.Length);
3982 AssertEquals("0.0000/3 split first chunk", "0", chunks[0]);
3983 AssertEquals("0.0000/3 split second chunk", "0000", chunks[1]);
3985 chunks = s8.Split (c8, 1);
3986 AssertEquals("0.0000/1 split length", 1, chunks.Length);
3987 AssertEquals("0.0000/1 split first chunk", "0.0000", chunks[0]);
3989 chunks = s1.Split(c2, 1);
3990 AssertEquals("Single split", 1, chunks.Length);
3991 AssertEquals("Single chunk", s1, chunks[0]);
3993 chunks = s1.Split(c2, 0);
3994 AssertEquals("Zero split", 0, chunks.Length);
3998 public void MoreSplit ()
4000 string test = "123 456 789";
4001 string [] st = test.Split ();
4002 AssertEquals ("#01", "123", st [0]);
4003 st = test.Split (null);
4004 AssertEquals ("#02", "123", st [0]);
4008 [Test] // Split (Char [], StringSplitOptions)
4009 public void Split3_Options_Invalid ()
4012 "A B".Split (new Char [] { 'A' }, (StringSplitOptions) 4096);
4014 } catch (ArgumentException ex) {
4015 // Illegal enum value: 4096
4016 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
4017 AssertNull ("#3", ex.InnerException);
4018 AssertNotNull ("#4", ex.Message);
4019 Assert ("#5", ex.Message.IndexOf ("4096") != 1);
4020 AssertNull ("#6", ex.ParamName);
4024 [Test] // Split (Char [], StringSplitOptions)
4025 public void Split4_Options_Invalid ()
4028 "A B".Split (new String [] { "A" }, (StringSplitOptions) 4096);
4030 } catch (ArgumentException ex) {
4031 // Illegal enum value: 4096
4032 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
4033 AssertNull ("#3", ex.InnerException);
4034 AssertNotNull ("#4", ex.Message);
4035 Assert ("#5", ex.Message.IndexOf ("4096") != 1);
4036 AssertNull ("#6", ex.ParamName);
4040 [Test] // Split (Char [], StringSplitOptions)
4041 public void Split5_Options_Invalid ()
4044 "A B".Split (new Char [] { 'A' }, 0, (StringSplitOptions) 4096);
4046 } catch (ArgumentException ex) {
4047 // Illegal enum value: 4096
4048 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
4049 AssertNull ("#3", ex.InnerException);
4050 AssertNotNull ("#4", ex.Message);
4051 Assert ("#5", ex.Message.IndexOf ("4096") != 1);
4052 AssertNull ("#6", ex.ParamName);
4056 [Test] // Split (String [], Int32, StringSplitOptions)
4057 public void Split6_Count_Negative ()
4060 "A B".Split (new String [] { "A" }, -1, StringSplitOptions.None);
4062 } catch (ArgumentOutOfRangeException ex) {
4063 AssertEquals ("#2", typeof (ArgumentOutOfRangeException), ex.GetType ());
4064 AssertNull ("#3", ex.InnerException);
4065 AssertNotNull ("#4", ex.Message);
4066 AssertEquals ("#5", "count", ex.ParamName);
4070 [Test] // Split (String [], Int32, StringSplitOptions)
4071 public void Split6_Options_Invalid ()
4074 "A B".Split (new String [] { "A" }, 0, (StringSplitOptions) 4096);
4076 } catch (ArgumentException ex) {
4077 // Illegal enum value: 4096
4078 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
4079 AssertNull ("#3", ex.InnerException);
4080 AssertNotNull ("#4", ex.Message);
4081 Assert ("#5", ex.Message.IndexOf ("4096") != 1);
4082 AssertNull ("#6", ex.ParamName);
4087 public void SplitString ()
4092 res = "A B C".Split (new String [] { "A" }, 0, StringSplitOptions.None);
4093 AssertEquals (0, res.Length);
4095 // empty and RemoveEmpty
4096 res = string.Empty.Split (new String [] { "A" }, StringSplitOptions.RemoveEmptyEntries);
4097 AssertEquals (0, res.Length);
4100 res = "A B C".Split (new String [] { "D" }, StringSplitOptions.None);
4101 AssertEquals (1, res.Length);
4102 AssertEquals ("A B C", res [0]);
4105 res = "A B C DD E".Split (new String[] { "B", "D" }, StringSplitOptions.None);
4106 AssertEquals (4, res.Length);
4107 AssertEquals ("A ", res [0]);
4108 AssertEquals (" C ", res [1]);
4109 AssertEquals (string.Empty, res [2]);
4110 AssertEquals (" E", res [3]);
4112 // Same with RemoveEmptyEntries
4113 res = "A B C DD E".Split (new String[] { "B", "D" }, StringSplitOptions.RemoveEmptyEntries);
4114 AssertEquals (3, res.Length);
4115 AssertEquals ("A ", res [0]);
4116 AssertEquals (" C ", res [1]);
4117 AssertEquals (" E", res [2]);
4119 // Delimiter matches once at the beginning of the string
4120 res = "A B".Split (new String [] { "A" }, StringSplitOptions.RemoveEmptyEntries);
4121 AssertEquals (1, res.Length);
4122 AssertEquals (" B", res [0]);
4124 // Delimiter at the beginning and at the end
4125 res = "B C DD B".Split (new String[] { "B" }, StringSplitOptions.None);
4126 AssertEquals (3, res.Length);
4127 AssertEquals (string.Empty, res [0]);
4128 AssertEquals (" C DD ", res [1]);
4129 AssertEquals (string.Empty, res [2]);
4131 res = "B C DD B".Split (new String[] { "B" }, StringSplitOptions.RemoveEmptyEntries);
4132 AssertEquals (1, res.Length);
4133 AssertEquals (" C DD ", res [0]);
4136 res = "A B C DD E".Split (new String[] { "B", "D" }, 2, StringSplitOptions.None);
4137 AssertEquals (2, res.Length);
4138 AssertEquals ("A ", res [0]);
4139 AssertEquals (" C DD E", res [1]);
4142 res = "ABCDEF".Split (new String[] { "EF", "BCDE" }, StringSplitOptions.None);
4143 AssertEquals (2, res.Length);
4144 AssertEquals ("A", res [0]);
4145 AssertEquals ("F", res [1]);
4147 res = "ABCDEF".Split (new String[] { "BCD", "BC" }, StringSplitOptions.None);
4148 AssertEquals (2, res.Length);
4149 AssertEquals ("A", res [0]);
4150 AssertEquals ("EF", res [1]);
4153 res = "A B\nC".Split ((String[])null, StringSplitOptions.None);
4154 AssertEquals (3, res.Length);
4155 AssertEquals ("A", res [0]);
4156 AssertEquals ("B", res [1]);
4157 AssertEquals ("C", res [2]);
4159 res = "A B\nC".Split (new String [0], StringSplitOptions.None);
4160 AssertEquals (3, res.Length);
4161 AssertEquals ("A", res [0]);
4162 AssertEquals ("B", res [1]);
4163 AssertEquals ("C", res [2]);
4167 public void SplitStringChars ()
4172 res = "..A..B..".Split (new Char[] { '.' }, 0, StringSplitOptions.None);
4173 AssertEquals ("#01-01", 0, res.Length);
4176 res = "..A..B..".Split (new Char[] { '.' }, 1, StringSplitOptions.None);
4177 AssertEquals ("#02-01", 1, res.Length);
4178 AssertEquals ("#02-02", "..A..B..", res [0]);
4180 // count == 1 + RemoveEmpty
4181 res = "..A..B..".Split (new Char[] { '.' }, 1, StringSplitOptions.RemoveEmptyEntries);
4182 AssertEquals ("#03-01", 1, res.Length);
4183 AssertEquals ("#03-02", "..A..B..", res [0]);
4185 // Strange Case A+B A
4186 res = "...".Split (new Char[] { '.' }, 1, StringSplitOptions.RemoveEmptyEntries);
4187 AssertEquals ("#ABA-01", 1, res.Length);
4188 AssertEquals ("#ABA-02", "...", res [0]);
4190 // Strange Case A+B B
4191 res = "...".Split (new Char[] { '.' }, 2, StringSplitOptions.RemoveEmptyEntries);
4192 AssertEquals ("#ABB-01", 0, res.Length);
4194 // Keeping Empties and multipe split chars
4195 res = "..A;.B.;".Split (new Char[] { '.', ';' }, StringSplitOptions.None);
4196 AssertEquals ("#04-01", 7, res.Length);
4197 AssertEquals ("#04-02", string.Empty, res [0]);
4198 AssertEquals ("#04-03", string.Empty, res [1]);
4199 AssertEquals ("#04-04", "A", res [2]);
4200 AssertEquals ("#04-05", string.Empty, res [3]);
4201 AssertEquals ("#04-06", "B", res [4]);
4202 AssertEquals ("#04-07", string.Empty, res [5]);
4203 AssertEquals ("#04-08", string.Empty, res [6]);
4205 // Trimming (3 tests)
4206 res = "..A".Split (new Char[] { '.' }, 2, StringSplitOptions.RemoveEmptyEntries);
4207 AssertEquals ("#05-01", 1, res.Length);
4208 AssertEquals ("#05-02", "A", res [0]);
4210 res = "A..".Split (new Char[] { '.' }, 2, StringSplitOptions.RemoveEmptyEntries);
4211 AssertEquals ("#06-01", 1, res.Length);
4212 AssertEquals ("#06-02", "A", res [0]);
4214 res = "..A..".Split (new Char[] { '.' }, 2, StringSplitOptions.RemoveEmptyEntries);
4215 AssertEquals ("#07-01", 1, res.Length);
4216 AssertEquals ("#07-02", "A", res [0]);
4219 res = "..A..B..".Split (new Char[] { '.' }, 2, StringSplitOptions.RemoveEmptyEntries);
4220 AssertEquals ("#08-01", 2, res.Length);
4221 AssertEquals ("#08-02", "A", res [0]);
4222 AssertEquals ("#08-03", "B..", res [1]);
4224 // Whitespace and Long split chain (removing empty chars)
4225 res = " A\tBC\n\rDEF GHI ".Split ((Char[])null, StringSplitOptions.RemoveEmptyEntries);
4226 AssertEquals ("#09-01", 4, res.Length);
4227 AssertEquals ("#09-02", "A", res [0]);
4228 AssertEquals ("#09-03", "BC", res [1]);
4229 AssertEquals ("#09-04", "DEF", res [2]);
4230 AssertEquals ("#09-05", "GHI", res [3]);
4232 // Nothing but separators
4233 res = "..,.;.,".Split (new Char[]{'.',',',';'},2,StringSplitOptions.RemoveEmptyEntries);
4234 AssertEquals ("#10-01", 0, res.Length);
4236 // Complete testseries
4237 char[] dash = new Char[] { '/' };
4238 StringSplitOptions o = StringSplitOptions.RemoveEmptyEntries;
4239 AssertEquals ("#11-01", "hi", "hi".Split (dash, o)[0]);
4240 AssertEquals ("#11-02", "hi", "hi/".Split (dash, o)[0]);
4241 AssertEquals ("#11-03", "hi", "/hi".Split (dash, o)[0]);
4243 AssertEquals ("#11-04-1", "hi..", "hi../".Split (dash, o)[0]);
4244 AssertEquals ("#11-04-2", "hi..", "/hi..".Split (dash, o)[0]);
4246 res = "/hi/..".Split (dash, o);
4247 AssertEquals ("#11-05-1", "hi", res[0]);
4248 AssertEquals ("#11-05-2", "..", res[1]);
4249 AssertEquals ("#11-09-3", 2, res.Length);
4251 res = "hi/..".Split (dash, o);
4252 AssertEquals ("#11-06-1", "hi", res[0]);
4253 AssertEquals ("#11-06-2", "..", res[1]);
4254 AssertEquals ("#11-09-3", 2, res.Length);
4256 res = "hi/../".Split (dash, o);
4257 AssertEquals ("#11-07-1", "hi", res[0]);
4258 AssertEquals ("#11-07-2", "..", res[1]);
4259 AssertEquals ("#11-07-3", 2, res.Length);
4261 res = "/hi../".Split (dash, o);
4262 AssertEquals ("#11-08-1", "hi..", res[0]);
4263 AssertEquals ("#11-08-2", 1, res.Length);
4265 res = "/hi/../".Split (dash, o);
4266 AssertEquals ("#11-09-1", "hi", res[0]);
4267 AssertEquals ("#11-09-2", "..", res[1]);
4268 AssertEquals ("#11-09-3", 2, res.Length);
4272 [Category ("NotDotNet")]
4273 public void Normalize1 ()
4275 // .NET does not combine them into U+1F80
4276 // seealso: http://demo.icu-project.org/icu-bin/nbrowser?t=\u03B1\u0313\u0345
4277 string s = "\u03B1\u0313\u0345";
4278 Assert ("#1", !s.IsNormalized (NormalizationForm.FormC));
4279 Assert ("#2", !s.IsNormalized (NormalizationForm.FormKC));
4280 AssertEquals ("#3", "\u1F80", s.Normalize (NormalizationForm.FormC));
4281 AssertEquals ("#4", "\u1F80", s.Normalize (NormalizationForm.FormKC));
4285 [Category ("NotDotNet")]
4286 public void Normalize2 ()
4288 string s1 = "\u0061\u0301bc";
4289 string s2 = "\u00e1bc";
4290 // .NET does not combine \u0061\0301 into \u00E1
4291 // seealso: http://demo.icu-project.org/icu-bin/nbrowser?t=\u0061\u0301bc
4292 AssertEquals ("#1", s2, s1.Normalize (NormalizationForm.FormC));
4293 AssertEquals ("#2", s2, s1.Normalize (NormalizationForm.FormKC));