1 // TestConvert.cs - NUnit Test Cases for System.Convert class
3 // Krister Hansson (ds99krha@thn.htu.se)
4 // Andreas Jonsson (ds99anjn@thn.htu.se)
6 // (C) Krister Hansson & Andreas Jonsson
11 using System.Globalization;
13 namespace MonoTests.System {
16 public class ConvertTest {
47 tryDT = new DateTime(2002,1,1);
52 tryInt64 = 123456789012;
53 tryObj = new Object();
55 tryFloat = 1234.2345f;
56 falseString = "false";
63 ci = new CultureInfo("en-US");
64 ci.NumberFormat.NumberDecimalDigits = 3;
67 public void TestChangeType() {
70 Assert.AreEqual ((short)12345, Convert.ChangeType(tryInt32, typeof(short)), "#A01");
72 Assert.AreEqual ('A', Convert.ChangeType(65, typeof(char)), "#A02");
74 Assert.AreEqual (66, Convert.ChangeType('B', typeof(int)), "#A03");
76 Assert.AreEqual (((ulong)12345), Convert.ChangeType(tryInt32, typeof(ulong)), "#A04");
79 Assert.AreEqual (true, Convert.ChangeType(tryDec, TypeCode.Boolean), "#A05");
81 Assert.AreEqual ('f', Convert.ChangeType("f", TypeCode.Char), "#A06");
83 Assert.AreEqual ((decimal)123456789012, Convert.ChangeType(tryInt64, TypeCode.Decimal), "#A07");
85 Assert.AreEqual ((int)34567, Convert.ChangeType(tryUI16, TypeCode.Int32), "#A08");
88 Assert.AreEqual ((double)567891234, Convert.ChangeType(tryUI32, typeof(double), ci), "#A09");
90 Assert.AreEqual ((ushort)0, Convert.ChangeType(tryByte, typeof(ushort), ci), "#A10");
92 Assert.AreEqual ((decimal)567891234, Convert.ChangeType(tryUI32, typeof(decimal), ci), "#A11");
94 Assert.AreEqual ((float)1234, Convert.ChangeType(tryInt16, typeof(float), ci), "#A12");
96 Assert.AreEqual (null, Convert.ChangeType(null, null, ci), "#A13");
99 Assert.AreEqual ((decimal)0, Convert.ChangeType(tryByte, TypeCode.Decimal, ci), "#A14");
101 Assert.AreEqual ("f", Convert.ChangeType('f', TypeCode.String, ci), "#A15");
103 Assert.AreEqual ('D', Convert.ChangeType(68, TypeCode.Char, ci), "#A16");
105 Assert.AreEqual ((long)34567, Convert.ChangeType(tryUI16, TypeCode.Int64, ci), "#A17");
107 Assert.AreEqual (null, Convert.ChangeType(null, TypeCode.Empty, ci), "#A18");
108 } catch (Exception e) {
109 Assert.Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
113 Convert.ChangeType(boolTrue, typeof(char));
116 catch (Exception e) {
117 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A25");
121 Convert.ChangeType(tryChar, typeof(DateTime));
124 catch (Exception e) {
125 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A26");
129 Convert.ChangeType(ci, TypeCode.String);
132 catch (Exception e) {
133 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A27");
137 Convert.ChangeType(tryInt32, null);
140 catch (Exception e) {
141 Assert.AreEqual (typeof(ArgumentNullException), e.GetType(), "#A28");
146 Convert.ChangeType(boolTrue, typeof(DateTime), ci);
149 catch (Exception e) {
150 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A29");
154 Convert.ChangeType(ci, typeof(DateTime), ci);
157 catch (Exception e) {
158 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A30");
161 /* Should throw ArgumentException but throws InvalidCastException
163 Convert.ChangeType(tryUI32, typeof(FormatException), ci);
166 catch (Exception e) {
167 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#A??");
171 Convert.ChangeType(tryUI32, TypeCode.Byte, ci);
174 catch (Exception e) {
175 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#A31");
179 Convert.ChangeType(boolTrue, TypeCode.Char, ci);
182 catch (Exception e) {
183 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A32");
187 Convert.ChangeType(boolTrue, null, ci);
190 catch (Exception e) {
191 Assert.AreEqual (typeof(ArgumentNullException), e.GetType(), "#A33");
195 /* should fail to convert string to any enumeration type. */
196 Convert.ChangeType("random string", typeof(DayOfWeek));
199 catch (Exception e) {
200 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A34");
205 public void TestGetTypeCode() {
208 Assert.AreEqual (TypeCode.String, Convert.GetTypeCode(tryStr), "#B01");
210 Assert.AreEqual (TypeCode.UInt16, Convert.GetTypeCode(tryUI16), "#B02");
212 Assert.AreEqual (TypeCode.UInt32, Convert.GetTypeCode(tryUI32), "#B03");
214 Assert.AreEqual (TypeCode.UInt64, Convert.GetTypeCode(tryUI64), "#B04");
216 Assert.AreEqual (TypeCode.Double, Convert.GetTypeCode(tryDbl), "#B05");
218 Assert.AreEqual (TypeCode.Int16, Convert.GetTypeCode(tryInt16), "#B06");
220 Assert.AreEqual (TypeCode.Int64, Convert.GetTypeCode(tryInt64), "#B07");
222 Assert.AreEqual (TypeCode.Object, Convert.GetTypeCode(tryObj), "#B08");
224 Assert.AreEqual (TypeCode.SByte, Convert.GetTypeCode(trySByte), "#B09");
226 Assert.AreEqual (TypeCode.Single, Convert.GetTypeCode(tryFloat), "#B10");
228 Assert.AreEqual (TypeCode.Byte, Convert.GetTypeCode(tryByte), "#B11");
230 Assert.AreEqual (TypeCode.Char, Convert.GetTypeCode(tryChar), "#B12");
232 // Assert.AreEqual (TypeCode.DateTime, Convert.GetTypeCode(tryDT), "#B13");
234 Assert.AreEqual (TypeCode.Decimal, Convert.GetTypeCode(tryDec), "#B14");
236 Assert.AreEqual (TypeCode.Int32, Convert.GetTypeCode(tryInt32), "#B15");
238 Assert.AreEqual (TypeCode.Boolean, Convert.GetTypeCode(boolTrue), "#B16");
239 } catch (Exception e) {
240 Assert.Fail ("Unexpected exception at " + marker + ": " + e);
244 public void TestIsDBNull() {
245 Assert.AreEqual (false, Convert.IsDBNull(tryInt32), "#C01");
246 Assert.AreEqual (true, Convert.IsDBNull(Convert.DBNull), "#C02");
247 Assert.AreEqual (false, Convert.IsDBNull(boolTrue), "#C03");
248 Assert.AreEqual (false, Convert.IsDBNull(tryChar), "#C04");
249 Assert.AreEqual (false, Convert.IsDBNull(tryFloat), "#C05");
252 public void TestToBoolean() {
253 tryObj = (object)tryDbl;
255 Assert.AreEqual (true, Convert.ToBoolean(boolTrue), "#D01");
256 Assert.AreEqual (false, Convert.ToBoolean(tryByte), "#D02");
257 Assert.AreEqual (true, Convert.ToBoolean(tryDec), "#D03");
258 Assert.AreEqual (false, Convert.ToBoolean(tryDbl), "#D04");
259 Assert.AreEqual (true, Convert.ToBoolean(tryInt16), "#D05");
260 Assert.AreEqual (true, Convert.ToBoolean(tryInt32), "#D06");
261 Assert.AreEqual (true, Convert.ToBoolean(tryInt64), "#D07");
262 Assert.AreEqual (false, Convert.ToBoolean(tryObj), "#D08");
263 Assert.AreEqual (true, Convert.ToBoolean(trySByte), "#D09");
264 Assert.AreEqual (true, Convert.ToBoolean(tryFloat), "#D10");
265 Assert.AreEqual (true, Convert.ToBoolean(trueString), "#D11");
266 Assert.AreEqual (false, Convert.ToBoolean(falseString), "#D12");
267 Assert.AreEqual (true, Convert.ToBoolean(tryUI16), "#D13");
268 Assert.AreEqual (true, Convert.ToBoolean(tryUI32), "#D14");
269 Assert.AreEqual (false, Convert.ToBoolean(tryUI64), "#D15");
270 Assert.AreEqual (false, Convert.ToBoolean(tryObj, ci), "#D16");
271 Assert.AreEqual (true, Convert.ToBoolean(trueString, ci), "#D17");
272 Assert.AreEqual (false, Convert.ToBoolean(falseString, ci), "#D18");
275 Convert.ToBoolean(tryChar);
278 catch (Exception e) {
279 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#D20");
283 Convert.ToBoolean(tryDT);
286 catch (Exception e) {
287 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#D21");
291 Convert.ToBoolean(tryStr);
294 catch (Exception e) {
295 Assert.AreEqual (typeof(FormatException), e.GetType(), "#D22");
299 Convert.ToBoolean(nullString);
302 catch (Exception e) {
303 Assert.AreEqual (typeof(FormatException), e.GetType(), "#D23");
307 public void TestToByte() {
309 Assert.AreEqual ((byte)1, Convert.ToByte(boolTrue), "#E01");
310 Assert.AreEqual ((byte)0, Convert.ToByte(boolFalse), "#E02");
311 Assert.AreEqual (tryByte, Convert.ToByte(tryByte), "#E03");
312 Assert.AreEqual ((byte)114, Convert.ToByte('r'), "#E04");
313 Assert.AreEqual ((byte)201, Convert.ToByte((decimal)200.6), "#E05");
314 Assert.AreEqual ((byte)125, Convert.ToByte((double)125.4), "#E06");
315 Assert.AreEqual ((byte)255, Convert.ToByte((short)255), "#E07");
316 Assert.AreEqual ((byte)254, Convert.ToByte((int)254), "#E08");
317 Assert.AreEqual ((byte)34, Convert.ToByte((long)34), "#E09");
318 Assert.AreEqual ((byte)1, Convert.ToByte((object)boolTrue), "#E10");
319 Assert.AreEqual ((byte)123, Convert.ToByte((float)123.49f), "#E11");
320 Assert.AreEqual ((byte)57, Convert.ToByte("57"), "#E12");
321 Assert.AreEqual ((byte)75, Convert.ToByte((ushort)75), "#E13");
322 Assert.AreEqual ((byte)184, Convert.ToByte((uint)184), "#E14");
323 Assert.AreEqual ((byte)241, Convert.ToByte((ulong)241), "#E15");
324 Assert.AreEqual ((byte)123, Convert.ToByte(trySByte, ci), "#E16");
325 Assert.AreEqual ((byte)27, Convert.ToByte("011011", 2), "#E17");
326 Assert.AreEqual ((byte)13, Convert.ToByte("15", 8), "#E18");
327 Assert.AreEqual ((byte)27, Convert.ToByte("27", 10), "#E19");
328 Assert.AreEqual ((byte)250, Convert.ToByte("FA", 16), "#E20");
331 Convert.ToByte('\u03A9'); // sign of Omega on Win2k
334 catch (Exception e) {
335 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E25");
339 Convert.ToByte(tryDT);
342 catch (Exception e) {
343 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#D26");
347 Convert.ToByte((decimal)22000);
350 catch (Exception e) {
351 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E27");
355 Convert.ToByte((double)255.5);
358 catch (Exception e) {
359 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E28");
363 Convert.ToByte(-tryInt16);
366 catch (Exception e) {
367 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E29");
371 Convert.ToByte((int)-256);
374 catch (Exception e) {
375 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E30");
379 Convert.ToByte(tryInt64);
382 catch (Exception e) {
383 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E31");
387 Convert.ToByte((object)ci);
390 catch (Exception e) {
391 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#E32");
395 Convert.ToByte((sbyte)-1);
398 catch (Exception e) {
399 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E33");
403 Convert.ToByte((float)-0.6f);
406 catch (Exception e) {
407 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E34");
411 Convert.ToByte("1a1");
414 catch (Exception e) {
415 Assert.AreEqual (typeof(FormatException), e.GetType(), "#E35");
419 Convert.ToByte("457");
422 catch (Exception e) {
423 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E36");
427 Convert.ToByte((ushort)30000);
430 catch (Exception e) {
431 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E37");
435 Convert.ToByte((uint)300);
438 catch (Exception e) {
439 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E38");
443 Convert.ToByte((ulong)987654321321);
446 catch (Exception e) {
447 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E39");
451 Convert.ToByte("10010111", 3);
454 catch (Exception e) {
455 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#E40");
459 Convert.ToByte("3F3", 16);
462 catch (Exception e) {
463 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E41");
467 public void TestToChar(){
469 Assert.AreEqual (':', Convert.ToChar(tryByte), "#F01");
470 Assert.AreEqual ('a', Convert.ToChar(tryChar), "#F02");
471 Assert.AreEqual ('A', Convert.ToChar((short)65), "#F03");
472 Assert.AreEqual ('x', Convert.ToChar((int)120), "#F04");
473 Assert.AreEqual ('"', Convert.ToChar((long)34), "#F05");
474 Assert.AreEqual ('-', Convert.ToChar((sbyte)45), "#F06");
475 Assert.AreEqual ('@', Convert.ToChar("@"), "#F07");
476 Assert.AreEqual ('K', Convert.ToChar((ushort)75), "#F08");
477 Assert.AreEqual ('=', Convert.ToChar((uint)61), "#F09");
478 // Assert.AreEqual ('E', Convert.ToChar((ulong)200), "#F10");
479 Assert.AreEqual ('{', Convert.ToChar((object)trySByte, ci), "#F11");
480 Assert.AreEqual ('o', Convert.ToChar(tryStr.Substring(1, 1), ci), "#F12");
483 Convert.ToChar(boolTrue);
486 catch (Exception e) {
487 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F20");
491 Convert.ToChar(tryDT);
494 catch (Exception e) {
495 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F21");
499 Convert.ToChar(tryDec);
502 catch (Exception e) {
503 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F22");
507 Convert.ToChar(tryDbl);
510 catch (Exception e) {
511 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F23");
515 Convert.ToChar((short)-1);
518 catch (Exception e) {
519 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F24");
523 Convert.ToChar(Int32.MinValue);
526 catch (Exception e) {
527 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F25");
531 Convert.ToChar(Int32.MaxValue);
534 catch (Exception e) {
535 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F26");
539 Convert.ToChar(tryInt64);
542 catch (Exception e) {
543 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F27");
547 Convert.ToChar((long)-123);
550 catch (Exception e) {
551 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F28");
558 catch (Exception e) {
559 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F29");
563 Convert.ToChar(-trySByte);
566 catch (Exception e) {
567 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F30");
571 Convert.ToChar(tryFloat);
574 catch (Exception e) {
575 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F31");
579 Convert.ToChar("foo");
582 catch (Exception e) {
583 Assert.AreEqual (typeof(FormatException), e.GetType(), "#F32");
587 Convert.ToChar(null);
590 catch (Exception e) {
591 Assert.AreEqual (typeof(ArgumentNullException), e.GetType(), "#F33");
595 Convert.ToChar(new Exception(), ci);
598 catch (Exception e) {
599 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F34");
603 Convert.ToChar(null, ci);
606 catch (Exception e) {
607 Assert.AreEqual (typeof(ArgumentNullException), e.GetType(), "#F35");
611 Convert.ToChar("", ci);
614 catch (Exception e) {
615 Assert.AreEqual (typeof(FormatException), e.GetType(), "#F36");
619 Convert.ToChar(tryStr, ci);
622 catch (Exception e) {
623 Assert.AreEqual (typeof(FormatException), e.GetType(), "#F37");
627 /*[Ignore ("http://bugzilla.ximian.com/show_bug.cgi?id=45286")]
629 [ExpectedException (typeof (ArgumentOutOfRangeException))]
631 Convert.ToDateTime("20002-25-01");
634 public void TestToDateTime() {
635 string dateString = "01/01/2002";
638 Assert.AreEqual (tryDT, Convert.ToDateTime(tryDT), "#G01");
640 Assert.AreEqual (tryDT, Convert.ToDateTime(dateString), "#G02");
642 Assert.AreEqual (tryDT, Convert.ToDateTime(dateString, ci), "#G03");
643 } catch (Exception e) {
644 Assert.Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
648 Convert.ToDateTime(boolTrue);
651 catch (Exception e) {
652 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G10");
656 Convert.ToDateTime(tryByte);
659 catch (Exception e) {
660 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G11");
664 Convert.ToDateTime(tryChar);
667 catch (Exception e) {
668 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G12");
672 Convert.ToDateTime(tryDec);
675 catch (Exception e) {
676 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G13");
680 Convert.ToDateTime(tryDbl);
683 catch (Exception e) {
684 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G14");
688 Convert.ToDateTime(tryInt16);
691 catch (Exception e) {
692 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G15");
696 Convert.ToDateTime(tryInt32);
699 catch (Exception e) {
700 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G16");
704 Convert.ToDateTime(tryInt64);
707 catch (Exception e) {
708 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G17");
712 Convert.ToDateTime(ci);
715 catch (Exception e) {
716 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G18");
720 Convert.ToDateTime(trySByte);
723 catch (Exception e) {
724 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G19");
728 Convert.ToDateTime(tryFloat);
731 catch (Exception e) {
732 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G20");
736 Convert.ToDateTime("20a2-01-01");
739 catch (Exception e) {
740 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G21");
744 Convert.ToDateTime(tryUI16);
747 catch (Exception e) {
748 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G23");
752 Convert.ToDateTime(tryUI32);
755 catch (Exception e) {
756 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G24");
760 Convert.ToDateTime(tryUI64);
763 catch (Exception e) {
764 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G25");
768 Convert.ToDateTime(ci, ci);
771 catch (Exception e) {
772 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G26");
776 Convert.ToDateTime("20a2-01-01", ci);
779 catch (Exception e) {
780 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G27");
783 // this is supported by .net 1.1 (defect 41845)
785 Convert.ToDateTime("20022-01-01");
788 catch (Exception e) {
790 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G28");
792 Assert.AreEqual (typeof(ArgumentOutOfRangeException), e.GetType(), "#G28");
797 Convert.ToDateTime("2002-21-01");
800 catch (Exception e) {
801 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G29");
805 Convert.ToDateTime("2002-111-01");
808 catch (Exception e) {
809 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G30");
813 Convert.ToDateTime("2002-01-41");
816 catch (Exception e) {
817 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G31");
821 Convert.ToDateTime("2002-01-111");
824 catch (Exception e) {
825 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G32");
829 Assert.AreEqual (tryDT, Convert.ToDateTime("2002-01-01"), "#G33");
830 } catch (Exception e) {
831 Assert.Fail ("Unexpected exception at #G33 " + e);
835 Convert.ToDateTime("2002-01-11 34:11:11");
838 catch (Exception e) {
839 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G34");
843 Convert.ToDateTime("2002-01-11 11:70:11");
846 catch (Exception e) {
847 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G35");
851 Convert.ToDateTime("2002-01-11 11:11:70");
854 catch (Exception e) {
855 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G36");
860 public void TestToDecimal() {
861 Assert.AreEqual ((decimal)1, Convert.ToDecimal(boolTrue), "#H01");
862 Assert.AreEqual ((decimal)0, Convert.ToDecimal(boolFalse), "#H02");
863 Assert.AreEqual ((decimal)tryByte, Convert.ToDecimal(tryByte), "#H03");
864 Assert.AreEqual (tryDec, Convert.ToDecimal(tryDec), "#H04");
865 Assert.AreEqual ((decimal)tryDbl, Convert.ToDecimal(tryDbl), "#H05");
866 Assert.AreEqual ((decimal)tryInt16, Convert.ToDecimal(tryInt16), "#H06");
867 Assert.AreEqual ((decimal)tryInt32, Convert.ToDecimal(tryInt32), "#H07");
868 Assert.AreEqual ((decimal)tryInt64, Convert.ToDecimal(tryInt64), "#H08");
869 Assert.AreEqual ((decimal)trySByte, Convert.ToDecimal(trySByte), "#H09");
870 Assert.AreEqual ((decimal)tryFloat, Convert.ToDecimal(tryFloat), "#H10");
871 string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
872 // Assert.AreEqual ((decimal)23456.432, Convert.ToDecimal("23456" + sep + "432"), "#H11");
873 // Note: changed because the number were the same but with a different base
874 // and this isn't a Convert bug (but a Decimal bug). See #60227 for more details.
875 // http://bugzilla.ximian.com/show_bug.cgi?id=60227
876 Assert.IsTrue (Decimal.Equals (23456.432m, Convert.ToDecimal ("23456" + sep + "432")), "#H11");
877 Assert.AreEqual ((decimal)tryUI16, Convert.ToDecimal(tryUI16), "#H12");
878 Assert.AreEqual ((decimal)tryUI32, Convert.ToDecimal(tryUI32), "#H13");
879 Assert.AreEqual ((decimal)tryUI64, Convert.ToDecimal(tryUI64), "#H14");
880 Assert.AreEqual ((decimal)63784, Convert.ToDecimal("63784", ci), "#H15");
883 Convert.ToDecimal(tryChar);
886 catch (Exception e) {
887 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#H20");
891 Convert.ToDecimal(tryDT);
894 catch (Exception e) {
895 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#H21");
899 Convert.ToDecimal(double.MaxValue);
902 catch (Exception e) {
903 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#H22");
907 Convert.ToDecimal(double.MinValue);
910 catch (Exception e) {
911 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#H23");
915 Convert.ToDecimal(ci);
918 catch (Exception e) {
919 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#H24");
923 Convert.ToDecimal(tryStr);
926 catch (Exception e) {
927 Assert.AreEqual (typeof(FormatException), e.GetType(), "#H25");
931 string maxDec = decimal.MaxValue.ToString();
932 maxDec = maxDec + "1";
933 Convert.ToDecimal(maxDec);
936 catch (Exception e) {
937 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#H26");
941 Convert.ToDecimal(ci, ci);
944 catch (Exception e) {
945 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#H27");
949 Convert.ToDecimal(tryStr, ci);
952 catch (Exception e) {
953 Assert.AreEqual (typeof(FormatException), e.GetType(), "#H28");
957 string maxDec = decimal.MaxValue.ToString();
958 maxDec = maxDec + "1";
959 Convert.ToDecimal(maxDec, ci);
962 catch (Exception e) {
963 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#H29");
967 public void TestToDouble() {
970 Assert.AreEqual ((double)1, Convert.ToDouble(boolTrue), "#I01");
972 Assert.AreEqual ((double)0, Convert.ToDouble(boolFalse), "#I02");
974 Assert.AreEqual ((double)tryByte, Convert.ToDouble(tryByte), "#I03");
976 Assert.AreEqual (tryDbl, Convert.ToDouble(tryDbl), "#I04");
978 Assert.AreEqual ((double)tryDec, Convert.ToDouble(tryDec), "#I05");
980 Assert.AreEqual ((double)tryInt16, Convert.ToDouble(tryInt16), "#I06");
982 Assert.AreEqual ((double)tryInt32, Convert.ToDouble(tryInt32), "#I07");
984 Assert.AreEqual ((double)tryInt64, Convert.ToDouble(tryInt64), "#I08");
986 Assert.AreEqual ((double)trySByte, Convert.ToDouble(trySByte), "#I09");
988 Assert.AreEqual ((double)tryFloat, Convert.ToDouble(tryFloat), "#I10");
990 string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
991 Assert.AreEqual ((double)23456.432, Convert.ToDouble("23456" + sep + "432"), "#I11");
993 Assert.AreEqual ((double)tryUI16, Convert.ToDouble(tryUI16), "#I12");
995 Assert.AreEqual ((double)tryUI32, Convert.ToDouble(tryUI32), "#I13");
997 Assert.AreEqual ((double)tryUI64, Convert.ToDouble(tryUI64), "#I14");
999 Assert.AreEqual ((double)63784, Convert.ToDouble("63784", ci), "#H15");
1000 } catch (Exception e) {
1001 Assert.Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1005 Convert.ToDouble(tryChar);
1008 catch (Exception e) {
1009 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#I20");
1013 Convert.ToDouble(tryDT);
1016 catch (Exception e) {
1017 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#I21");
1021 Convert.ToDouble(ci);
1024 catch (Exception e) {
1025 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#I22");
1029 Convert.ToDouble(tryStr);
1032 catch (Exception e) {
1033 Assert.AreEqual (typeof(FormatException), e.GetType(), "#I23");
1037 string maxDec = double.MaxValue.ToString();
1038 maxDec = maxDec + "1";
1039 Convert.ToDouble(maxDec);
1042 catch (Exception e) {
1043 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#I24");
1047 Convert.ToDouble(ci, ci);
1050 catch (Exception e) {
1051 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#I25");
1055 Convert.ToDouble(tryStr, ci);
1058 catch (Exception e) {
1059 Assert.AreEqual (typeof(FormatException), e.GetType(), "#I26");
1063 string maxDec = double.MaxValue.ToString();
1064 maxDec = maxDec + "1";
1065 Convert.ToDouble(maxDec, ci);
1068 catch (Exception e) {
1069 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#I27");
1073 Convert.ToDouble(tryObj, ci);
1076 catch (Exception e) {
1077 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#I28");
1081 public void TestToInt16() {
1082 Assert.AreEqual ((short)0, Convert.ToInt16(boolFalse), "#J01");
1083 Assert.AreEqual ((short)1, Convert.ToInt16(boolTrue), "#J02");
1084 Assert.AreEqual ((short)97, Convert.ToInt16(tryChar), "#J03");
1085 Assert.AreEqual ((short)1234, Convert.ToInt16(tryDec), "#J04");
1086 Assert.AreEqual ((short)0, Convert.ToInt16(tryDbl), "#J05");
1087 Assert.AreEqual ((short)1234, Convert.ToInt16(tryInt16), "#J06");
1088 Assert.AreEqual ((short)12345, Convert.ToInt16(tryInt32), "#J07");
1089 Assert.AreEqual ((short)30000, Convert.ToInt16((long)30000), "#J08");
1090 Assert.AreEqual ((short)123, Convert.ToInt16(trySByte), "#J09");
1091 Assert.AreEqual ((short)1234, Convert.ToInt16(tryFloat), "#J10");
1092 Assert.AreEqual ((short)578, Convert.ToInt16("578"), "#J11");
1093 Assert.AreEqual ((short)15500, Convert.ToInt16((ushort)15500), "#J12");
1094 Assert.AreEqual ((short)5489, Convert.ToInt16((uint)5489), "#J13");
1095 Assert.AreEqual ((short)9876, Convert.ToInt16((ulong)9876), "#J14");
1096 Assert.AreEqual ((short)14, Convert.ToInt16("14", ci), "#J15");
1097 Assert.AreEqual ((short)11, Convert.ToInt16("01011", 2), "#J16");
1098 Assert.AreEqual ((short)1540, Convert.ToInt16("3004", 8), "#J17");
1099 Assert.AreEqual ((short)321, Convert.ToInt16("321", 10), "#J18");
1100 Assert.AreEqual ((short)2748, Convert.ToInt16("ABC", 16), "#J19");
1103 Convert.ToInt16(char.MaxValue);
1106 catch (Exception e) {
1107 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J25");
1111 Convert.ToInt16(tryDT);
1114 catch (Exception e) {
1115 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#J26");
1119 Convert.ToInt16((decimal)(short.MaxValue + 1));
1122 catch (Exception e) {
1123 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J27");
1127 Convert.ToInt16((decimal)(short.MinValue - 1));
1130 catch (Exception e) {
1131 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J28");
1135 Convert.ToInt16((double)(short.MaxValue + 1));
1138 catch (Exception e) {
1139 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J29");
1143 Convert.ToInt16((double)(short.MinValue - 1));
1146 catch (Exception e) {
1147 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J30");
1151 Convert.ToInt16(50000);
1154 catch (Exception e) {
1155 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J31");
1159 Convert.ToInt16(-50000);
1162 catch (Exception e) {
1163 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J32");
1167 Convert.ToInt16(tryInt64);
1170 catch (Exception e) {
1171 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J33");
1175 Convert.ToInt16(-tryInt64);
1178 catch (Exception e) {
1179 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J34");
1183 Convert.ToInt16(tryObj);
1186 catch (Exception e) {
1187 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#J35");
1191 Convert.ToInt16((float)32767.5);
1194 catch (Exception e) {
1195 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J36");
1199 Convert.ToInt16((float)-33000.54);
1202 catch (Exception e) {
1203 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J37");
1207 Convert.ToInt16(tryStr);
1210 catch (Exception e) {
1211 Assert.AreEqual (typeof(FormatException), e.GetType(), "#J38");
1215 Convert.ToInt16("-33000");
1218 catch (Exception e) {
1219 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J39");
1223 Convert.ToInt16(ushort.MaxValue);
1226 catch (Exception e) {
1227 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J40");
1231 Convert.ToInt16(uint.MaxValue);
1234 catch (Exception e) {
1235 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J41");
1239 Convert.ToInt16(ulong.MaxValue);
1242 catch (Exception e) {
1243 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J42");
1247 Convert.ToInt16(tryObj, ci);
1250 catch (Exception e) {
1251 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#J43");
1255 Convert.ToInt16(tryStr, ci);
1258 catch (Exception e) {
1259 Assert.AreEqual (typeof(FormatException), e.GetType(), "#J44");
1263 Convert.ToInt16("-33000", ci);
1266 catch (Exception e) {
1267 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J45");
1271 Convert.ToInt16("321", 11);
1274 catch (Exception e) {
1275 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#J46");
1279 Convert.ToInt16("D8BF1", 16);
1282 catch (Exception e) {
1283 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J47");
1287 public void TestToInt32() {
1288 long tryMax = long.MaxValue;
1289 long tryMin = long.MinValue;
1290 Assert.AreEqual ((int)0, Convert.ToInt32(boolFalse), "#K01");
1291 Assert.AreEqual ((int)1, Convert.ToInt32(boolTrue), "#K02");
1292 Assert.AreEqual ((int)0, Convert.ToInt32(tryByte), "#K03");
1293 Assert.AreEqual ((int)97, Convert.ToInt32(tryChar), "#K04");
1294 Assert.AreEqual ((int)1234, Convert.ToInt32(tryDec), "#K05");
1295 Assert.AreEqual ((int)0, Convert.ToInt32(tryDbl), "#K06");
1296 Assert.AreEqual ((int)1234, Convert.ToInt32(tryInt16), "#K07");
1297 Assert.AreEqual ((int)12345, Convert.ToInt32(tryInt32), "#K08");
1298 Assert.AreEqual ((int)60000, Convert.ToInt32((long)60000), "#K09");
1299 Assert.AreEqual ((int)123, Convert.ToInt32(trySByte), "#K10");
1300 Assert.AreEqual ((int)1234, Convert.ToInt32(tryFloat), "#K11");
1301 Assert.AreEqual ((int)9876, Convert.ToInt32((string)"9876"), "#K12");
1302 Assert.AreEqual ((int)34567, Convert.ToInt32(tryUI16), "#K13");
1303 Assert.AreEqual ((int)567891234, Convert.ToInt32(tryUI32), "#K14");
1304 Assert.AreEqual ((int)0, Convert.ToInt32(tryUI64), "#K15");
1305 Assert.AreEqual ((int)123, Convert.ToInt32("123", ci), "#K16");
1306 Assert.AreEqual ((int)128, Convert.ToInt32("10000000", 2), "#K17");
1307 Assert.AreEqual ((int)302, Convert.ToInt32("456", 8), "#K18");
1308 Assert.AreEqual ((int)456, Convert.ToInt32("456", 10), "#K19");
1309 Assert.AreEqual ((int)1110, Convert.ToInt32("456", 16), "#K20");
1312 Convert.ToInt32(tryDT);
1315 catch (Exception e) {
1316 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#K25");
1320 Convert.ToInt32((decimal)tryMax);
1323 catch (Exception e) {
1324 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K26");
1328 Convert.ToInt32((decimal)tryMin);
1331 catch (Exception e) {
1332 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K27");
1336 Convert.ToInt32((double)tryMax);
1339 catch (Exception e) {
1340 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K28");
1344 Convert.ToInt32((double)tryMin);
1347 catch (Exception e) {
1348 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K29");
1352 Convert.ToInt32(tryInt64);
1355 catch (Exception e) {
1356 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K30");
1360 Convert.ToInt32(-tryInt64);
1363 catch (Exception e) {
1364 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K31");
1368 Convert.ToInt32(tryObj);
1371 catch (Exception e) {
1372 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#K32");
1376 Convert.ToInt32((float)tryMax);
1379 catch (Exception e) {
1380 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K33");
1384 Convert.ToInt32((float)tryMin);
1387 catch (Exception e) {
1388 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K34");
1392 Convert.ToInt32(tryStr, ci);
1395 catch (Exception e) {
1396 Assert.AreEqual (typeof(FormatException), e.GetType(), "#K35");
1400 Convert.ToInt32("-46565465123");
1403 catch (Exception e) {
1404 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K36");
1408 Convert.ToInt32("46565465123");
1411 catch (Exception e) {
1412 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K37");
1416 Convert.ToInt32((uint)tryMax);
1419 catch (Exception e) {
1420 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K38");
1424 Convert.ToInt32((ulong)tryMax);
1427 catch (Exception e) {
1428 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K39");
1432 Convert.ToInt32(tryObj, ci);
1435 catch (Exception e) {
1436 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#K40");
1440 Convert.ToInt32(tryStr, ci);
1443 catch (Exception e) {
1444 Assert.AreEqual (typeof(FormatException), e.GetType(), "#K41");
1448 Convert.ToInt32("-46565465123", ci);
1451 catch (Exception e) {
1452 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K42");
1456 Convert.ToInt32("654", 9);
1459 catch (Exception e) {
1460 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#K43");
1464 public void TestToInt64() {
1465 decimal longMax = long.MaxValue;
1467 decimal longMin = long.MinValue;
1470 Assert.AreEqual ((long)0, Convert.ToInt64(boolFalse), "#L01");
1471 Assert.AreEqual ((long)1, Convert.ToInt64(boolTrue), "#L02");
1472 Assert.AreEqual ((long)97, Convert.ToInt64(tryChar), "#L03");
1473 Assert.AreEqual ((long)1234, Convert.ToInt64(tryDec), "#L04");
1474 Assert.AreEqual ((long)0, Convert.ToInt64(tryDbl), "#L05");
1475 Assert.AreEqual ((long)1234, Convert.ToInt64(tryInt16), "#L06");
1476 Assert.AreEqual ((long)12345, Convert.ToInt64(tryInt32), "#L07");
1477 Assert.AreEqual ((long)123456789012, Convert.ToInt64(tryInt64), "#L08");
1478 Assert.AreEqual ((long)123, Convert.ToInt64(trySByte), "#L09");
1479 Assert.AreEqual ((long)1234, Convert.ToInt64(tryFloat), "#L10");
1480 Assert.AreEqual ((long)564897, Convert.ToInt64("564897"), "#L11");
1481 Assert.AreEqual ((long)34567, Convert.ToInt64(tryUI16), "#L12");
1482 Assert.AreEqual ((long)567891234, Convert.ToInt64(tryUI32), "#L13");
1483 Assert.AreEqual ((long)0, Convert.ToInt64(tryUI64), "#L14");
1484 Assert.AreEqual ((long)-2548751, Convert.ToInt64("-2548751", ci), "#L15");
1485 Assert.AreEqual ((long)24987562, Convert.ToInt64("1011111010100011110101010", 2), "#L16");
1486 Assert.AreEqual ((long)-24578965, Convert.ToInt64("1777777777777642172153", 8), "#L17");
1487 Assert.AreEqual ((long)248759757, Convert.ToInt64("248759757", 10), "#L18");
1488 Assert.AreEqual ((long)256, Convert.ToInt64("100", 16), "#L19");
1491 Convert.ToInt64(tryDT);
1494 catch (Exception e) {
1495 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#L20");
1499 Convert.ToInt64((decimal)longMax + 1);
1502 catch (Exception e) {
1503 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L21");
1507 Convert.ToInt64((decimal)longMin);
1510 catch (Exception e) {
1511 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L24");
1515 Convert.ToInt64((double)longMax);
1518 catch (Exception e) {
1519 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L25:"+longMax);
1523 Convert.ToInt64((double)longMin);
1526 catch (Exception e) {
1527 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L26");
1531 Convert.ToInt64(new Exception());
1534 catch (Exception e) {
1535 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#L27");
1539 Convert.ToInt64(((float)longMax)*100);
1542 catch (Exception e) {
1543 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L28:"+longMax);
1547 Convert.ToInt64(((float)longMin)*100);
1550 catch (Exception e) {
1551 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L29");
1555 Convert.ToInt64("-567b3");
1558 catch (Exception e) {
1559 Assert.AreEqual (typeof(FormatException), e.GetType(), "#L30");
1563 Convert.ToInt64(longMax.ToString());
1566 catch (Exception e) {
1567 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L31:");
1571 Convert.ToInt64(ulong.MaxValue);
1574 catch (Exception e) {
1575 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L32");
1579 Convert.ToInt64(tryStr, ci);
1582 catch (Exception e) {
1583 Assert.AreEqual (typeof(FormatException), e.GetType(), "#L32b");
1587 Convert.ToInt64(longMin.ToString(), ci);
1590 catch (Exception e) {
1591 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L33");
1595 Convert.ToInt64("321", 11);
1598 catch (Exception e) {
1599 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#L34");
1603 public void TestToSByte() {
1606 Assert.AreEqual ((sbyte)0, Convert.ToSByte(boolFalse), "#M01");
1608 Assert.AreEqual ((sbyte)1, Convert.ToSByte(boolTrue), "#M02");
1610 Assert.AreEqual ((sbyte)97, Convert.ToSByte(tryChar), "#M03");
1612 Assert.AreEqual ((sbyte)15, Convert.ToSByte((decimal)15), "#M04");
1614 Assert.AreEqual ((sbyte)0, Convert.ToSByte(tryDbl), "#M05");
1616 Assert.AreEqual ((sbyte)127, Convert.ToSByte((short)127), "#M06");
1618 Assert.AreEqual ((sbyte)-128, Convert.ToSByte((int)-128), "#M07");
1620 Assert.AreEqual ((sbyte)30, Convert.ToSByte((long)30), "#M08");
1622 Assert.AreEqual ((sbyte)123, Convert.ToSByte(trySByte), "#M09");
1624 Assert.AreEqual ((sbyte)12, Convert.ToSByte((float)12.46987f), "#M10");
1626 Assert.AreEqual ((sbyte)1, Convert.ToSByte("1"), "#M11");
1628 Assert.AreEqual ((sbyte)99, Convert.ToSByte((ushort)99), "#M12");
1630 Assert.AreEqual ((sbyte)54, Convert.ToSByte((uint)54), "#M13");
1632 Assert.AreEqual ((sbyte)127, Convert.ToSByte((ulong)127), "#M14");
1634 Assert.AreEqual ((sbyte)14, Convert.ToSByte("14", ci), "#M15");
1636 Assert.AreEqual ((sbyte)11, Convert.ToSByte("01011", 2), "#M16");
1638 Assert.AreEqual ((sbyte)5, Convert.ToSByte("5", 8), "#M17");
1640 Assert.AreEqual ((sbyte)100, Convert.ToSByte("100", 10), "#M18");
1642 Assert.AreEqual ((sbyte)-1, Convert.ToSByte("FF", 16), "#M19");
1643 } catch (Exception e) {
1644 Assert.Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1648 Convert.ToSByte((byte)200);
1651 catch (Exception e) {
1652 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M25");
1656 Convert.ToSByte((char)130);
1659 catch (Exception e) {
1660 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M26");
1664 Convert.ToSByte(tryDT);
1667 catch (Exception e) {
1668 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#M27");
1672 Convert.ToSByte((decimal)127.5m);
1675 catch (Exception e) {
1676 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M28");
1680 Convert.ToSByte((decimal)-200m);
1683 catch (Exception e) {
1684 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M29");
1688 Convert.ToSByte((double)150);
1691 catch (Exception e) {
1692 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M30");
1696 Convert.ToSByte((double)-128.6);
1699 catch (Exception e) {
1700 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M31");
1704 Convert.ToSByte((short)150);
1707 catch (Exception e) {
1708 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M32");
1712 Convert.ToSByte((short)-300);
1715 catch (Exception e) {
1716 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M33");
1720 Convert.ToSByte((int)1500);
1723 catch (Exception e) {
1724 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M34");
1728 Convert.ToSByte((int)-1286);
1731 catch (Exception e) {
1732 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M35");
1736 Convert.ToSByte((long)128);
1739 catch (Exception e) {
1740 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M36");
1744 Convert.ToSByte((long)-129);
1747 catch (Exception e) {
1748 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M37");
1752 Convert.ToSByte(new NumberFormatInfo());
1755 catch (Exception e) {
1756 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#M38");
1760 Convert.ToSByte((float)333);
1763 catch (Exception e) {
1764 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M39");
1768 Convert.ToSByte((float)-666);
1771 catch (Exception e) {
1772 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M40");
1776 Convert.ToSByte("B3");
1779 catch (Exception e) {
1780 Assert.AreEqual (typeof(FormatException), e.GetType(), "#M41");
1784 Convert.ToSByte("251");
1787 catch (Exception e) {
1788 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M42");
1792 Convert.ToSByte(ushort.MaxValue);
1795 catch (Exception e) {
1796 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M43");
1800 Convert.ToSByte((uint)600);
1803 catch (Exception e) {
1804 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M44");
1808 Convert.ToSByte(ulong.MaxValue);
1811 catch (Exception e) {
1812 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M45");
1816 Convert.ToSByte(ci, ci);
1819 catch (Exception e) {
1820 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#M46");
1824 Convert.ToSByte(tryStr, ci);
1827 catch (Exception e) {
1828 Assert.AreEqual (typeof(FormatException), e.GetType(), "#M47");
1832 Convert.ToSByte("325", ci);
1835 catch (Exception e) {
1836 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M48");
1840 Convert.ToSByte("5D", 15);
1843 catch (Exception e) {
1844 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#M49");
1848 Convert.ToSByte("111111111", 2);
1851 catch (Exception e) {
1852 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M50");
1856 public void TestToSingle() {
1859 Assert.AreEqual ((float)0, Convert.ToSingle(boolFalse), "#N01");
1861 Assert.AreEqual ((float)1, Convert.ToSingle(boolTrue), "#N02");
1863 Assert.AreEqual ((float)0, Convert.ToSingle(tryByte), "#N03");
1865 Assert.AreEqual ((float)1234, 234, Convert.ToSingle(tryDec), "#N04");
1867 Assert.AreEqual ((float)0, Convert.ToSingle(tryDbl), "#N05");
1869 Assert.AreEqual ((float)1234, Convert.ToSingle(tryInt16), "#N06");
1871 Assert.AreEqual ((float)12345, Convert.ToSingle(tryInt32), "#N07");
1873 Assert.AreEqual ((float)123456789012, Convert.ToSingle(tryInt64), "#N08");
1875 Assert.AreEqual ((float)123, Convert.ToSingle(trySByte), "#N09");
1877 Assert.AreEqual ((float)1234, 2345, Convert.ToSingle(tryFloat), "#N10");
1879 Assert.AreEqual ((float)987, Convert.ToSingle("987"), "#N11");
1881 Assert.AreEqual ((float)34567, Convert.ToSingle(tryUI16), "#N12");
1883 Assert.AreEqual ((float)567891234, Convert.ToSingle(tryUI32), "#N13");
1885 Assert.AreEqual ((float)0, Convert.ToSingle(tryUI64), "#N14");
1887 Assert.AreEqual ((float)654.234, Convert.ToSingle("654.234", ci), "#N15");
1888 } catch (Exception e) {
1889 Assert.Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1893 Convert.ToSingle(tryChar);
1896 catch (Exception e) {
1897 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#N25");
1901 Convert.ToSingle(tryDT);
1904 catch (Exception e) {
1905 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#N26");
1909 Convert.ToSingle(tryObj);
1912 catch (Exception e) {
1913 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#N27");
1917 Convert.ToSingle("A345H");
1920 catch (Exception e) {
1921 Assert.AreEqual (typeof(FormatException), e.GetType(), "#N28");
1925 Convert.ToSingle(double.MaxValue.ToString());
1928 catch (Exception e) {
1929 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#N29");
1933 Convert.ToSingle(tryObj, ci);
1936 catch (Exception e) {
1937 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#N30");
1941 Convert.ToSingle("J345K", ci);
1944 catch (Exception e) {
1945 Assert.AreEqual (typeof(FormatException), e.GetType(), "#N31");
1949 Convert.ToSingle("11000000000000000000000000000000000000000000000", ci);
1952 catch (Exception e) {
1953 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#N32");
1957 public void TestToString() {
1960 Assert.AreEqual ("False", Convert.ToString(boolFalse), "#O01");
1961 Assert.AreEqual ("True", Convert.ToString(boolTrue), "#O02");
1962 Assert.AreEqual ("123", Convert.ToString(tryByte), "#O03");
1963 Assert.AreEqual ("a", Convert.ToString(tryChar), "#O04");
1964 Assert.AreEqual (tryDT.ToString(), Convert.ToString(tryDT), "#O05");
1965 Assert.AreEqual (tryDec.ToString(), Convert.ToString(tryDec), "#O06");
1966 Assert.AreEqual (tryDbl.ToString(), Convert.ToString(tryDbl), "#O07");
1967 Assert.AreEqual ("1234", Convert.ToString(tryInt16), "#O08");
1968 Assert.AreEqual ("12345", Convert.ToString(tryInt32), "#O09");
1969 Assert.AreEqual ("123456789012", Convert.ToString(tryInt64), "#O10");
1970 Assert.AreEqual ("123", Convert.ToString(trySByte), "#O11");
1971 Assert.AreEqual (tryFloat.ToString(), Convert.ToString(tryFloat), "#O12");
1972 Assert.AreEqual ("foobar", Convert.ToString(tryStr), "#O13");
1973 Assert.AreEqual ("34567", Convert.ToString(tryUI16), "#O14");
1974 Assert.AreEqual ("567891234", Convert.ToString(tryUI32), "#O15");
1975 Assert.AreEqual ("True", Convert.ToString(boolTrue, ci), "#O16");
1976 Assert.AreEqual ("False", Convert.ToString(boolFalse, ci), "#O17");
1977 Assert.AreEqual ("123", Convert.ToString(tryByte, ci), "#O18");
1978 Assert.AreEqual ("1111011", Convert.ToString(tryByte, 2), "#O19");
1979 Assert.AreEqual ("173", Convert.ToString(tryByte, 8), "#O20");
1980 Assert.AreEqual ("123", Convert.ToString(tryByte, 10), "#O21");
1981 Assert.AreEqual ("7b", Convert.ToString(tryByte, 16), "#O22");
1982 Assert.AreEqual ("a", Convert.ToString(tryChar, ci), "#O23");
1983 Assert.AreEqual (tryDT.ToString(ci), Convert.ToString(tryDT, ci), "#O24");
1984 Assert.AreEqual (tryDec.ToString(ci), Convert.ToString(tryDec, ci), "#O25");
1985 Assert.AreEqual (tryDbl.ToString(ci), Convert.ToString(tryDbl, ci), "#O26");
1986 Assert.AreEqual ("1234", Convert.ToString(tryInt16, ci), "#O27");
1987 Assert.AreEqual ("10011010010", Convert.ToString(tryInt16, 2), "#O28");
1988 Assert.AreEqual ("2322", Convert.ToString(tryInt16, 8), "#O29");
1989 Assert.AreEqual ("1234", Convert.ToString(tryInt16, 10), "#O30");
1990 Assert.AreEqual ("4d2", Convert.ToString(tryInt16, 16), "#O31");
1991 Assert.AreEqual ("12345", Convert.ToString(tryInt32, ci), "#O32");
1992 Assert.AreEqual ("11000000111001", Convert.ToString(tryInt32, 2), "#O33");
1993 Assert.AreEqual ("30071", Convert.ToString(tryInt32, 8), "#O34");
1994 Assert.AreEqual ("12345", Convert.ToString(tryInt32, 10), "#O35");
1995 Assert.AreEqual ("3039", Convert.ToString(tryInt32, 16), "#O36");
1996 Assert.AreEqual ("123456789012", Convert.ToString(tryInt64, ci), "#O37");
1997 Assert.AreEqual ("1110010111110100110010001101000010100", Convert.ToString(tryInt64, 2), "#O38");
1998 Assert.AreEqual ("1627646215024", Convert.ToString(tryInt64, 8), "#O39");
1999 Assert.AreEqual ("123456789012", Convert.ToString(tryInt64, 10), "#O40");
2000 Assert.AreEqual ("1cbe991a14", Convert.ToString(tryInt64, 16), "#O41");
2001 Assert.AreEqual ("123", Convert.ToString((trySByte), ci), "#O42");
2002 Assert.AreEqual (tryFloat.ToString(ci), Convert.ToString((tryFloat), ci), "#O43");
2003 Assert.AreEqual ("foobar", Convert.ToString((tryStr), ci), "#O44");
2004 Assert.AreEqual ("34567", Convert.ToString((tryUI16), ci), "#O45");
2005 Assert.AreEqual ("567891234", Convert.ToString((tryUI32), ci), "#O46");
2006 Assert.AreEqual ("0", Convert.ToString(tryUI64), "#O47");
2007 Assert.AreEqual ("0", Convert.ToString((tryUI64), ci), "#O48");
2010 Convert.ToString(tryInt16, 5);
2013 catch (Exception e) {
2014 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#O55");
2018 Convert.ToString(tryInt32, 17);
2021 catch (Exception e) {
2022 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#O56");
2026 Convert.ToString(tryInt64, 1);
2029 catch (Exception e) {
2030 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#O57");
2034 public void TestToUInt16() {
2035 Assert.AreEqual ((ushort)0, Convert.ToUInt16(boolFalse), "#P01");
2036 Assert.AreEqual ((ushort)1, Convert.ToUInt16(boolTrue), "#P02");
2037 Assert.AreEqual ((ushort)0, Convert.ToUInt16(tryByte), "#P03");
2038 Assert.AreEqual ((ushort)97, Convert.ToUInt16(tryChar), "#P04");
2039 Assert.AreEqual ((ushort)1234, Convert.ToUInt16(tryDec), "#P05");
2040 Assert.AreEqual ((ushort)0, Convert.ToUInt16(tryDbl), "#P06");
2041 Assert.AreEqual ((ushort)1234, Convert.ToUInt16(tryInt16), "#P07");
2042 Assert.AreEqual ((ushort)12345, Convert.ToUInt16(tryInt32), "#P08");
2043 Assert.AreEqual ((ushort)43752, Convert.ToUInt16((long)43752), "#P09");
2044 Assert.AreEqual ((ushort)123, Convert.ToUInt16(trySByte), "#P10");
2045 Assert.AreEqual ((ushort)1234, Convert.ToUInt16(tryFloat), "#P11");
2046 Assert.AreEqual ((ushort)123, Convert.ToUInt16((string)"123"), "#P12");
2047 Assert.AreEqual ((ushort)34567, Convert.ToUInt16(tryUI16), "#P13");
2048 Assert.AreEqual ((ushort)56789, Convert.ToUInt16((uint)56789), "#P14");
2049 Assert.AreEqual ((ushort)0, Convert.ToUInt16(tryUI64), "#P15");
2050 Assert.AreEqual ((ushort)31, Convert.ToUInt16("31", ci), "#P16");
2051 Assert.AreEqual ((ushort)14, Convert.ToUInt16("1110", 2), "#P17");
2052 Assert.AreEqual ((ushort)32, Convert.ToUInt16("40", 8), "#P18");
2053 Assert.AreEqual ((ushort)40, Convert.ToUInt16("40", 10), "#P19");
2054 Assert.AreEqual ((ushort)64, Convert.ToUInt16("40", 16), "#P20");
2058 Convert.ToUInt16(tryDT);
2061 catch (Exception e) {
2062 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#P25");
2066 Convert.ToUInt16(decimal.MaxValue);
2069 catch (Exception e) {
2070 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P26");
2074 Convert.ToUInt16(decimal.MinValue);
2077 catch (Exception e) {
2078 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P27");
2082 Convert.ToUInt16(double.MaxValue);
2085 catch (Exception e) {
2086 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P28");
2090 Convert.ToUInt16(double.MinValue);
2093 catch (Exception e) {
2094 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P29");
2098 Convert.ToUInt16(short.MinValue);
2101 catch (Exception e) {
2102 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P30");
2106 Convert.ToUInt16(int.MaxValue);
2109 catch (Exception e) {
2110 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P31");
2114 Convert.ToUInt16(int.MinValue);
2117 catch (Exception e) {
2118 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P32");
2122 Convert.ToUInt16(long.MaxValue);
2125 catch (Exception e) {
2126 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P33");
2130 Convert.ToUInt16(long.MinValue);
2133 catch (Exception e) {
2134 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P34");
2138 Convert.ToUInt16(tryObj);
2141 catch (Exception e) {
2142 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#P35");
2146 Convert.ToUInt16(sbyte.MinValue);
2149 catch (Exception e) {
2150 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P36");
2154 Convert.ToUInt16(float.MaxValue);
2157 catch (Exception e) {
2158 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P37");
2162 Convert.ToUInt16(float.MinValue);
2165 catch (Exception e) {
2166 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P38");
2170 Convert.ToUInt16("1A2");
2173 catch (Exception e) {
2174 Assert.AreEqual (typeof(FormatException), e.GetType(), "#P39");
2178 Convert.ToUInt16("-32800");
2181 catch (Exception e) {
2182 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P40");
2186 Convert.ToUInt16(int.MaxValue.ToString());
2189 catch (Exception e) {
2190 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P41");
2194 Convert.ToUInt16(ulong.MaxValue);
2197 catch (Exception e) {
2198 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P42");
2202 Convert.ToUInt16("1A2", ci);
2205 catch (Exception e) {
2206 Assert.AreEqual (typeof(FormatException), e.GetType(), "#P43");
2210 Convert.ToUInt16("-32800", ci);
2213 catch (Exception e) {
2214 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P44");
2218 Convert.ToUInt16("456987", ci);
2221 catch (Exception e) {
2222 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P45");
2226 Convert.ToUInt16("40", 9);
2229 catch (Exception e) {
2230 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#P46");
2234 Convert.ToUInt16 ("abcde", 16);
2237 catch (Exception e) {
2238 Assert.AreEqual (typeof (OverflowException), e.GetType (), "#P47");
2242 public void TestSignedToInt() {
2243 // String cannot contain a minus sign if the base is not 10.
2244 // But can if it is ten, and + is allowed everywhere.
2245 Assert.AreEqual (-1, Convert.ToInt32 ("-1", 10), "Signed0");
2246 Assert.AreEqual (1, Convert.ToInt32 ("+1", 10), "Signed1");
2247 Assert.AreEqual (1, Convert.ToInt32 ("+1", 2), "Signed2");
2248 Assert.AreEqual (1, Convert.ToInt32 ("+1", 8), "Signed3");
2249 Assert.AreEqual (1, Convert.ToInt32 ("+1", 16), "Signed4");
2252 Convert.ToInt32("-1", 2);
2258 Convert.ToInt32("-1", 8);
2264 Convert.ToInt32("-1", 16);
2273 public void TestToUInt32() {
2274 Assert.AreEqual ((uint)1, Convert.ToUInt32(boolTrue), "#Q01");
2275 Assert.AreEqual ((uint)0, Convert.ToUInt32(boolFalse), "#Q02");
2276 Assert.AreEqual ((uint)0, Convert.ToUInt32(tryByte), "#Q03");
2277 Assert.AreEqual ((uint)97, Convert.ToUInt32(tryChar), "#Q04");
2278 Assert.AreEqual ((uint)1234, Convert.ToUInt32(tryDec), "#Q05");
2279 Assert.AreEqual ((uint)0, Convert.ToUInt32(tryDbl), "#Q06");
2280 Assert.AreEqual ((uint)1234, Convert.ToUInt32(tryInt16), "#Q07");
2281 Assert.AreEqual ((uint)12345, Convert.ToUInt32(tryInt32), "#Q08");
2282 Assert.AreEqual ((uint)1234567890, Convert.ToUInt32((long)1234567890), "#Q09");
2283 Assert.AreEqual ((uint)123, Convert.ToUInt32(trySByte), "#Q10");
2284 Assert.AreEqual ((uint)1234, Convert.ToUInt32(tryFloat), "#Q11");
2285 Assert.AreEqual ((uint)3456789, Convert.ToUInt32("3456789"), "#Q12");
2286 Assert.AreEqual ((uint)34567, Convert.ToUInt32(tryUI16), "#Q13");
2287 Assert.AreEqual ((uint)567891234, Convert.ToUInt32(tryUI32), "#Q14");
2288 Assert.AreEqual ((uint)0, Convert.ToUInt32(tryUI64), "#Q15");
2289 Assert.AreEqual ((uint)415, Convert.ToUInt32("110011111", 2), "#Q16");
2290 Assert.AreEqual ((uint)156, Convert.ToUInt32("234", 8), "#Q17");
2291 Assert.AreEqual ((uint)234, Convert.ToUInt32("234", 10), "#Q18");
2292 Assert.AreEqual ((uint)564, Convert.ToUInt32("234", 16), "#Q19");
2296 Convert.ToUInt32(tryDT);
2299 catch (Exception e) {
2300 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#Q25");
2304 Convert.ToUInt32(decimal.MaxValue);
2307 catch (Exception e) {
2308 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q26");
2312 Convert.ToUInt32((decimal)-150);
2315 catch (Exception e) {
2316 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q27");
2320 Convert.ToUInt32(double.MaxValue);
2323 catch (Exception e) {
2324 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q28");
2328 Convert.ToUInt32((double)-1);
2331 catch (Exception e) {
2332 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q29");
2336 Convert.ToUInt32(short.MinValue);
2339 catch (Exception e) {
2340 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q30");
2344 Convert.ToUInt32(int.MinValue);
2347 catch (Exception e) {
2348 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q31");
2352 Convert.ToUInt32(long.MaxValue);
2355 catch (Exception e) {
2356 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q32");
2360 Convert.ToUInt32((long)-50000);
2363 catch (Exception e) {
2364 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q33");
2368 Convert.ToUInt32(new Exception());
2371 catch (Exception e) {
2372 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#Q34");
2376 Convert.ToUInt32(sbyte.MinValue);
2379 catch (Exception e) {
2380 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q35");
2384 Convert.ToUInt32(float.MaxValue);
2387 catch (Exception e) {
2388 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q36");
2392 Convert.ToUInt32(float.MinValue);
2395 catch (Exception e) {
2396 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q37");
2400 Convert.ToUInt32("45t54");
2403 catch (Exception e) {
2404 Assert.AreEqual (typeof(FormatException), e.GetType(), "#Q38");
2408 Convert.ToUInt32("-55");
2411 catch (Exception e) {
2412 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q39");
2416 Convert.ToUInt32(ulong.MaxValue);
2419 catch (Exception e) {
2420 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q40");
2424 Convert.ToUInt32(new Exception(), ci);
2427 catch (Exception e) {
2428 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#Q41");
2432 Convert.ToUInt32(tryStr, ci);
2435 catch (Exception e) {
2436 Assert.AreEqual (typeof(FormatException), e.GetType(), "#Q42");
2440 Convert.ToUInt32("-50", ci);
2443 catch (Exception e) {
2444 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q43");
2448 Convert.ToUInt32(decimal.MaxValue.ToString(), ci);
2451 catch (Exception e) {
2452 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q44");
2456 Convert.ToUInt32("1001110", 1);
2459 catch (Exception e) {
2460 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#Q45");
2464 public void TestToUInt64()
2468 Assert.AreEqual ((ulong)1, Convert.ToUInt64(boolTrue), "#R01");
2470 Assert.AreEqual ((ulong)0, Convert.ToUInt64(boolFalse), "#R02");
2472 Assert.AreEqual ((ulong)0, Convert.ToUInt64(tryByte), "#R03");
2474 Assert.AreEqual ((ulong)97, Convert.ToUInt64(tryChar), "#R04");
2476 Assert.AreEqual ((ulong)1234, Convert.ToUInt64(tryDec), "#R05");
2478 Assert.AreEqual ((ulong)0, Convert.ToUInt64(tryDbl), "#R06");
2480 Assert.AreEqual ((ulong)1234, Convert.ToUInt64(tryInt16), "#R07");
2482 Assert.AreEqual ((ulong)12345, Convert.ToUInt64(tryInt32), "#R08");
2484 Assert.AreEqual ((ulong)123456789012, Convert.ToUInt64(tryInt64), "#R09");
2486 Assert.AreEqual ((ulong)123, Convert.ToUInt64(trySByte), "#R10");
2488 Assert.AreEqual ((ulong)1234, Convert.ToUInt64(tryFloat), "#R11");
2490 Assert.AreEqual ((ulong)345678, Convert.ToUInt64("345678"), "#R12");
2492 Assert.AreEqual ((ulong)34567, Convert.ToUInt64(tryUI16), "#R13");
2494 Assert.AreEqual ((ulong)567891234, Convert.ToUInt64(tryUI32), "#R14");
2496 Assert.AreEqual ((ulong)0, Convert.ToUInt64(tryUI64), "#R15");
2498 Assert.AreEqual ((ulong)123, Convert.ToUInt64("123", ci), "#R16");
2500 Assert.AreEqual ((ulong)4, Convert.ToUInt64("100", 2), "#R17");
2502 Assert.AreEqual ((ulong)64, Convert.ToUInt64("100", 8), "#R18");
2504 Assert.AreEqual ((ulong)100, Convert.ToUInt64("100", 10), "#R19");
2506 Assert.AreEqual ((ulong)256, Convert.ToUInt64("100", 16), "#R20");
2507 } catch (Exception e) {
2508 Assert.Fail ("Unexpected exception caught when iTest = " + iTest + ": e = " + e);
2512 Convert.ToUInt64(tryDT);
2515 catch (Exception e) {
2516 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#R25");
2520 Convert.ToUInt64(decimal.MaxValue);
2523 catch (Exception e) {
2524 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R26");
2528 Convert.ToUInt64((decimal)-140);
2531 catch (Exception e) {
2532 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R27");
2536 Convert.ToUInt64(double.MaxValue);
2539 catch (Exception e) {
2540 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R28");
2544 Convert.ToUInt64((double)-1);
2547 catch (Exception e) {
2548 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R29");
2552 Convert.ToUInt64(short.MinValue);
2555 catch (Exception e) {
2556 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R30");
2560 Convert.ToUInt64(int.MinValue);
2563 catch (Exception e) {
2564 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R31");
2568 Convert.ToUInt64(long.MinValue);
2571 catch (Exception e) {
2572 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R32");
2576 Convert.ToUInt64(tryObj);
2579 catch (Exception e) {
2580 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#R33");
2584 Convert.ToUInt64(sbyte.MinValue);
2587 catch (Exception e) {
2588 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R34");
2592 Convert.ToUInt64(float.MinValue);
2595 catch (Exception e) {
2596 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R35");
2600 Convert.ToUInt64(float.MaxValue);
2603 catch (Exception e) {
2604 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R36");
2608 Convert.ToUInt64("234rt78");
2611 catch (Exception e) {
2612 Assert.AreEqual (typeof(FormatException), e.GetType(), "#R37");
2616 Convert.ToUInt64("-68");
2619 catch (Exception e) {
2620 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R38");
2624 Convert.ToUInt64(decimal.MaxValue.ToString());
2627 catch (Exception e) {
2628 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R39");
2632 Convert.ToUInt64("23rd2", ci);
2635 catch (Exception e) {
2636 Assert.AreEqual (typeof(FormatException), e.GetType(), "#R40");
2640 Convert.ToUInt64(decimal.MinValue.ToString(), ci);
2643 catch (Exception e) {
2644 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R41");
2648 Convert.ToUInt64(decimal.MaxValue.ToString(), ci);
2651 catch (Exception e) {
2652 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R42");
2656 Convert.ToUInt64("132", 9);
2659 catch (Exception e) {
2660 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#R43");
2664 Assert.AreEqual ((ulong) 256, Convert.ToUInt64 ("0x100", 16), "#L35");
2665 Assert.AreEqual ((ulong) 256, Convert.ToUInt64 ("0X100", 16), "#L36");
2666 Assert.AreEqual (ulong.MaxValue, Convert.ToUInt64 ("0xFFFFFFFFFFFFFFFF", 16), "#L37");
2670 [ExpectedException (typeof (FormatException))]
2671 public void TestInvalidBase64() {
2672 // This has to be a multiple of 4 characters, otherwise you
2673 // are testing something else. Ideally one will become a byte
2676 // This test is designed to see what happens with invalid bytes
2677 string brokenB64 = "AB~\u00a3";
2678 Convert.FromBase64String(brokenB64);
2682 [ExpectedException (typeof (FormatException))]
2683 public void TestInvalidBase64_Bug5464 ()
2685 Convert.FromBase64String ("dGVzdA==DQo=");
2689 public void TestValidBase64_Bug5464 ()
2691 byte[] result = Convert.FromBase64String ("dGVzdA==");
2692 Assert.AreEqual(4, result.Length, "Array.Length expected to be 4.");
2693 Assert.AreEqual(116, result[0], "#A01");
2694 Assert.AreEqual(101, result[1], "#A02");
2695 Assert.AreEqual(115, result[2], "#A03");
2696 Assert.AreEqual(116, result[3], "#A04");
2700 [ExpectedException (typeof (FormatException))]
2701 public void TestInvalidBase64_TooManyPaddings ()
2703 Convert.FromBase64String ("dGVzd===");
2707 public void TestBeginWithSpaces ()
2709 byte[] bb = new byte[] { 1, 2, 3};
2710 string s = Convert.ToBase64String (bb);
2711 byte [] b2 = Convert.FromBase64String (" " + s);
2712 Assert.AreEqual (3, b2.Length, "#01");
2713 for (int i = 0; i < 3; i++)
2714 Assert.AreEqual (bb [i], b2 [i], "#0" + (i + 2));
2717 public void TestToBase64CharArray ()
2719 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2721 char[] expectedCharArr = {'I', 'X', '/', '/', 'b', 'a', 'o', '2'};
2722 char[] result = new Char[8];
2724 Convert.ToBase64CharArray(byteArr, 0, byteArr.Length, result, 0);
2726 for (int i = 0; i < expectedCharArr.Length; i++) {
2727 Assert.AreEqual (expectedCharArr[i], result[i], "#S0" + i);
2732 [ExpectedException (typeof(ArgumentNullException))]
2733 public void ToBase64CharArray_InNull ()
2735 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2736 char[] result = new Char[8];
2737 Convert.ToBase64CharArray (null, 0, byteArr.Length, result, 0);
2741 [ExpectedException (typeof(ArgumentNullException))]
2742 public void ToBase64CharArray_OutNull ()
2744 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2745 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, null, 0);
2749 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2750 public void ToBase64CharArray_OffsetInNegative ()
2752 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2753 char[] result = new Char[8];
2754 Convert.ToBase64CharArray (byteArr, -1, byteArr.Length, result, 0);
2758 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2759 public void ToBase64CharArray_LengthNegative ()
2761 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2762 char[] result = new Char[8];
2763 Convert.ToBase64CharArray (byteArr, 0, -5, result, 0);
2767 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2768 public void ToBase64CharArray_OffsetOutNegative ()
2770 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2771 char[] result = new Char[8];
2772 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, -2);
2776 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2777 public void ToBase64CharArray_TotalIn ()
2779 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2780 char[] result = new Char[8];
2781 Convert.ToBase64CharArray (byteArr, 4, byteArr.Length, result, 0);
2785 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2786 public void ToBase64CharArray_TotalInOverflow ()
2788 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2789 char[] result = new Char[8];
2790 Convert.ToBase64CharArray (byteArr, Int32.MaxValue, byteArr.Length, result, 0);
2794 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2795 public void ToBase64CharArray_TotalOut ()
2797 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2798 char[] result = new Char[8];
2799 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, 2);
2803 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2804 public void ToBase64CharArray_TotalOutOverflow ()
2806 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2807 char[] result = new Char[8];
2808 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, Int32.MaxValue);
2811 public void TestToBase64String() {
2812 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2813 string expectedStr = "IX//bao2";
2817 result1 = Convert.ToBase64String(byteArr);
2818 result2 = Convert.ToBase64String(byteArr, 0, byteArr.Length);
2820 Assert.AreEqual (expectedStr, result1, "#T01");
2821 Assert.AreEqual (expectedStr, result2, "#T02");
2824 Convert.ToBase64String(null);
2827 catch (Exception e) {
2828 Assert.AreEqual (typeof(ArgumentNullException), e.GetType(), "#T05");
2832 Convert.ToBase64String(byteArr, -1, byteArr.Length);
2835 catch (Exception e) {
2836 Assert.AreEqual (typeof(ArgumentOutOfRangeException), e.GetType(), "#T06");
2840 Convert.ToBase64String(byteArr, 0, -10);
2843 catch (Exception e) {
2844 Assert.AreEqual (typeof(ArgumentOutOfRangeException), e.GetType(), "#T07");
2848 Convert.ToBase64String(byteArr, 4, byteArr.Length);
2851 catch (Exception e) {
2852 Assert.AreEqual (typeof(ArgumentOutOfRangeException), e.GetType(), "#T08");
2857 public void ToBase64String_Bug76876 ()
2859 byte[] bs = Convert.FromBase64String ("ZuBZ7PESb3VRXgrl/KSRJd/hTGBvaEvEplqH3izPomDv5nBjS9MzcD1h8tOWzS7/wYGnaip8\nbhBfCrpWxivi8G7R08oBcADIiclpZeqRxai9kG4WoBUzJ6MCbxuvb1k757q+D9nqoL0p9Rer\n+5+vNodYkHYwqnKKyMBSQ11sspw=\n");
2860 string s = Convert.ToBase64String (bs, Base64FormattingOptions.None);
2861 Assert.IsTrue (!s.Contains ("\n"), "no new line");
2864 static string ToBase64 (int len, Base64FormattingOptions options)
2866 return Convert.ToBase64String (new byte [len], options);
2870 public void Base64String_LineEnds_InsertLineBreaks ()
2872 string base64 = ToBase64 (0, Base64FormattingOptions.InsertLineBreaks);
2873 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "0-le");
2874 Assert.AreEqual (String.Empty, base64, "0");
2876 base64 = ToBase64 (1, Base64FormattingOptions.InsertLineBreaks);
2877 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "1-le");
2878 Assert.AreEqual ("AA==", base64, "1");
2880 base64 = ToBase64 (57, Base64FormattingOptions.InsertLineBreaks);
2881 Assert.IsFalse (base64.Contains (Environment.NewLine), "57-nl"); // one lines
2882 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "57-le");
2883 Assert.AreEqual ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", base64, "55");
2885 base64 = ToBase64 (58, Base64FormattingOptions.InsertLineBreaks);
2886 Assert.IsTrue (base64.Contains (Environment.NewLine), "58-nl"); // two lines
2887 Assert.IsTrue (base64.EndsWith ("AA=="), "58-le"); // no NewLine
2891 public void Base64String_LineEnds_None ()
2893 string base64 = ToBase64 (0, Base64FormattingOptions.None);
2894 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "0-le");
2895 Assert.AreEqual (String.Empty, base64, "0");
2897 base64 = ToBase64 (1, Base64FormattingOptions.None);
2898 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "1-le");
2899 Assert.AreEqual ("AA==", base64, "1");
2901 base64 = ToBase64 (57, Base64FormattingOptions.None);
2902 Assert.IsFalse (base64.Contains (Environment.NewLine), "57-nl"); // one lines
2903 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "57-le");
2904 Assert.AreEqual ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", base64, "55");
2906 base64 = ToBase64 (58, Base64FormattingOptions.None);
2907 Assert.IsFalse (base64.Contains (Environment.NewLine), "58-nl"); // one lines
2908 Assert.IsTrue (base64.EndsWith ("AA=="), "58-le"); // no NewLine
2911 /* Have experienced some problems with FromBase64CharArray using mono. Something
2912 * about error in a unicode file.
2914 * However the test seems to run fine using mono in a cygwin environment
2918 [ExpectedException (typeof (ArgumentNullException))]
2919 public void FromBase64CharArray_Null ()
2921 Convert.FromBase64CharArray (null, 1, 5);
2925 [ExpectedException (typeof (FormatException))]
2926 public void FromBase64CharArray_Empty ()
2928 Convert.FromBase64CharArray (new char[0], 0, 0);
2932 [ExpectedException (typeof (FormatException))]
2933 public void FormatBase64CharArray_OnlyWhitespace ()
2935 Convert.FromBase64CharArray (new char[3] {' ',
2940 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2941 public void FromBase64CharArray_OutOfRangeStart ()
2943 Convert.FromBase64CharArray (new char [4], -1, 4);
2947 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2948 public void FromBase64CharArray_OutOfRangeLength ()
2950 Convert.FromBase64CharArray (new char [4], 2, 4);
2954 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2955 public void FromBase64CharArray_Overflow ()
2957 Convert.FromBase64CharArray (new char [4], Int32.MaxValue, 4);
2961 [ExpectedException (typeof (FormatException))]
2962 public void FromBase64CharArray_InvalidLength ()
2964 Convert.FromBase64CharArray (new char [4], 0, 3);
2968 [ExpectedException (typeof (FormatException))]
2969 public void FromBase64CharArray_WideChar ()
2971 char[] c = new char [4] { 'A', 'A', 'A', (char) Char.MaxValue };
2972 Convert.FromBase64CharArray (c, 0, 4);
2976 public void FromBase64CharArray ()
2978 char[] charArr = {'M','o','n','o','m','o','n','o'};
2979 byte[] expectedByteArr = {50, 137, 232, 154, 137, 232};
2981 byte[] fromCharArr = Convert.FromBase64CharArray(charArr, 0, 8);
2983 for (int i = 0; i < fromCharArr.Length; i++){
2984 Assert.AreEqual (expectedByteArr[i], fromCharArr[i], "#U0" + i);
2988 /* Have experienced some problems with FromBase64String using mono. Something about
2989 * error in a unicode file.
2991 * However the test seems to run fine using mono in a cygwin environment
2995 [ExpectedException (typeof (ArgumentNullException))]
2996 public void FromBase64String_Null ()
2998 Convert.FromBase64String (null);
3002 [ExpectedException (typeof (FormatException))]
3003 public void FromBase64String_InvalidLength ()
3005 Convert.FromBase64String ("foo");
3009 [ExpectedException (typeof (FormatException))]
3010 public void FromBase64String_InvalidLength2 ()
3012 Convert.FromBase64String (tryStr);
3016 public void FromBase64String_InvalidLengthBecauseOfIgnoredChars ()
3018 byte[] result = Convert.FromBase64String ("AAAA\t");
3019 Assert.AreEqual (3, result.Length, "InvalidLengthBecauseOfIgnoredChars");
3022 private const string ignored = "\t\r\n ";
3023 private const string base64data = "AAAAAAAAAAAAAAAAAAAA"; // 15 bytes 0x00
3026 public void FromBase64_IgnoreCharsBefore ()
3028 string s = ignored + base64data;
3029 byte[] data = Convert.FromBase64String (s);
3030 Assert.AreEqual (15, data.Length, "String-IgnoreCharsBefore-Ignored");
3032 char[] c = s.ToCharArray ();
3033 data = Convert.FromBase64CharArray (c, 0, c.Length);
3034 Assert.AreEqual (15, data.Length, "CharArray-IgnoreCharsBefore-Ignored");
3038 public void FromBase64_IgnoreCharsInside ()
3040 string s = base64data + ignored + base64data;
3041 byte[] data = Convert.FromBase64String (s);
3042 Assert.AreEqual (30, data.Length, "String-IgnoreCharsInside-Ignored");
3044 char[] c = s.ToCharArray ();
3045 data = Convert.FromBase64CharArray (c, 0, c.Length);
3046 Assert.AreEqual (30, data.Length, "CharArray-IgnoreCharsInside-Ignored");
3050 public void FromBase64_IgnoreCharsAfter ()
3052 string s = base64data + ignored;
3053 byte[] data = Convert.FromBase64String (s);
3054 Assert.AreEqual (15, data.Length, "String-IgnoreCharsAfter-Ignored");
3056 char[] c = s.ToCharArray ();
3057 data = Convert.FromBase64CharArray (c, 0, c.Length);
3058 Assert.AreEqual (15, data.Length, "CharArray-IgnoreCharsAfter-Ignored");
3062 public void FromBase64_Empty ()
3064 Assert.AreEqual (new byte[0], Convert.FromBase64String (string.Empty));
3068 public void FromBase64_OnlyWhiteSpace ()
3070 Assert.AreEqual (new byte[0], Convert.FromBase64String (" \r\t"));
3074 [ExpectedException (typeof (FormatException))]
3075 public void FromBase64_InvalidChar ()
3077 Convert.FromBase64String ("amVsb3U=\u0100");
3081 [ExpectedException (typeof (FormatException))]
3082 public void FromBase64_Min ()
3084 Convert.FromBase64String ("amVsb3U= \r \n\u007B");
3088 public void FromBase64_TrailingEqualAndSpaces () // From bug #75840.
3090 string base64 = "\n fdy6S2NLpnT4fMdokUHSHsmpcvo= ";
3091 byte [] bytes = Convert.FromBase64String (base64);
3092 Assert.AreEqual (20, bytes.Length, "#01");
3093 byte [] target = new byte [] { 0x7D, 0xDC, 0xBA, 0x4B, 0x63, 0x4B, 0xA6, 0x74, 0xF8, 0x7C, 0xC7,
3094 0x68, 0x91, 0x41, 0xD2, 0x1E, 0xC9, 0xA9, 0x72, 0xFA };
3096 for (int i = 0; i < 20; i++) {
3097 if (bytes [i] != target [i])
3098 Assert.Fail ("Item #" + i);
3103 public void TestConvertFromNull() {
3105 Assert.AreEqual (false, Convert.ToBoolean (null as object), "#W1");
3106 Assert.AreEqual (0, Convert.ToByte (null as object), "#W2");
3107 Assert.AreEqual (0, Convert.ToChar (null as object), "#W3");
3108 Assert.AreEqual (new DateTime (1, 1, 1, 0, 0, 0), Convert.ToDateTime (null as object), "#W4");
3109 Assert.AreEqual (0, Convert.ToDecimal (null as object), "#W5");
3110 Assert.AreEqual (0, Convert.ToDouble (null as object), "#W6");
3111 Assert.AreEqual (0, Convert.ToInt16 (null as object), "#W7");
3112 Assert.AreEqual (0, Convert.ToInt32 (null as object), "#W8");
3113 Assert.AreEqual (0, Convert.ToInt64 (null as object), "#W9");
3114 Assert.AreEqual (0, Convert.ToSByte (null as object), "#W10");
3115 Assert.AreEqual (0, Convert.ToSingle (null as object), "#W11");
3116 Assert.AreEqual ("", Convert.ToString (null as object), "#W12");
3117 Assert.AreEqual (0, Convert.ToUInt16 (null as object), "#W13");
3118 Assert.AreEqual (0, Convert.ToUInt32 (null as object), "#W14");
3119 Assert.AreEqual (0, Convert.ToUInt64 (null as object), "#W15");
3120 Assert.AreEqual (false, Convert.ToBoolean (null as string), "#W16");
3121 Assert.AreEqual (0, Convert.ToByte (null as string), "#W17");
3124 Convert.ToChar (null as string);
3126 } catch (Exception e) {
3127 Assert.AreEqual (typeof (ArgumentNullException), e.GetType (), "#W18");
3130 Assert.AreEqual (new DateTime (1, 1, 1, 0, 0, 0), Convert.ToDateTime (null as string), "#W19");
3131 Assert.AreEqual (0, Convert.ToDecimal (null as string), "#W20");
3132 Assert.AreEqual (0, Convert.ToDouble (null as string), "#W21");
3133 Assert.AreEqual (0, Convert.ToInt16 (null as string), "#W22");
3134 Assert.AreEqual (0, Convert.ToInt32 (null as string), "#W23");
3135 Assert.AreEqual (0, Convert.ToInt64 (null as string), "#W24");
3136 Assert.AreEqual (0, Convert.ToSByte (null as string), "#W25");
3137 Assert.AreEqual (0, Convert.ToSingle (null as string), "#W26");
3138 Assert.AreEqual (null, Convert.ToString (null as string), "#W27");
3139 Assert.AreEqual (0, Convert.ToUInt16 (null as string), "#W28");
3140 Assert.AreEqual (0, Convert.ToUInt32 (null as string), "#W29");
3141 Assert.AreEqual (0, Convert.ToUInt64 (null as string), "#W30");
3145 [ExpectedException (typeof (FormatException))]
3146 public void FromBase64StringInvalidFormat ()
3148 Convert.FromBase64String ("Tgtm+DBN====");
3152 [ExpectedException (typeof (FormatException))]
3153 public void FromBase64StringInvalidFormat2 ()
3155 Convert.FromBase64String ("Tgtm+DBN========");
3159 public void ToByte_PrefixedHexStringInBase16 ()
3161 Assert.AreEqual (255, Convert.ToByte ("0xff", 16), "0xff");
3162 Assert.AreEqual (255, Convert.ToByte ("0xfF", 16), "0xfF");
3163 Assert.AreEqual (255, Convert.ToByte ("0xFf", 16), "0xFf");
3164 Assert.AreEqual (255, Convert.ToByte ("0xFF", 16), "0xFF");
3166 Assert.AreEqual (255, Convert.ToByte ("0Xff", 16), "0Xff");
3167 Assert.AreEqual (255, Convert.ToByte ("0XfF", 16), "0XfF");
3168 Assert.AreEqual (255, Convert.ToByte ("0XFf", 16), "0XFf");
3169 Assert.AreEqual (255, Convert.ToByte ("0XFF", 16), "0XFF");
3171 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0x0", 16), "0x0");
3175 [ExpectedException (typeof (OverflowException))]
3176 public void ToByte_NegativeString ()
3178 Convert.ToByte ("-1");
3182 [ExpectedException (typeof (ArgumentException))]
3183 public void ToByte_NegativeStringNonBase10 ()
3185 Convert.ToByte ("-0", 16);
3189 [ExpectedException (typeof (OverflowException))]
3190 public void ToByte_NegativeString_Base10 ()
3192 Convert.ToByte ("-0", 10);
3196 public void ToByte_NegativeZeroString ()
3198 Convert.ToByte ("-0");
3199 Convert.ToByte ("-0", null);
3203 [ExpectedException (typeof (OverflowException))]
3204 public void ToUInt16_NegativeString ()
3206 Convert.ToUInt16 ("-1");
3210 [ExpectedException (typeof (ArgumentException))]
3211 public void ToUInt16_NegativeStringNonBase10 ()
3213 Convert.ToUInt16 ("-0", 16);
3217 [ExpectedException (typeof (OverflowException))]
3218 public void ToUInt16_NegativeString_Base10 ()
3220 Convert.ToUInt16 ("-0", 10);
3224 public void ToUInt16_NegativeZeroString ()
3226 Convert.ToUInt16 ("-0");
3227 Convert.ToUInt16 ("-0", null);
3231 [ExpectedException (typeof (OverflowException))]
3232 public void ToUInt32_NegativeString ()
3234 Convert.ToUInt32 ("-1");
3238 [ExpectedException (typeof (ArgumentException))]
3239 public void ToUInt32_NegativeStringNonBase10 ()
3241 Convert.ToUInt32 ("-0", 16);
3245 [ExpectedException (typeof (OverflowException))]
3246 public void ToUInt32_NegativeString_Base10 ()
3248 Convert.ToUInt32 ("-0", 10);
3252 public void ToUInt32_NegativeZeroString ()
3254 Convert.ToUInt32 ("-0");
3255 Convert.ToUInt32 ("-0", null);
3259 [ExpectedException (typeof (OverflowException))]
3260 public void ToUInt64_NegativeString ()
3262 Convert.ToUInt64 ("-1");
3266 [ExpectedException (typeof (ArgumentException))]
3267 public void ToUInt64_NegativeStringNonBase10 ()
3269 Convert.ToUInt64 ("-0", 16);
3273 [ExpectedException (typeof (OverflowException))]
3274 public void ToUInt64_NegativeString_Base10 ()
3276 Convert.ToUInt64 ("-0", 10);
3280 public void ToUInt64_NegativeZeroString ()
3282 Convert.ToUInt64 ("-0");
3283 Convert.ToUInt64 ("-0", null);
3289 public void ToByte_MaxValue ()
3291 Assert.AreEqual (Byte.MaxValue, Convert.ToByte ("ff", 16), "ff,16");
3292 Assert.AreEqual (Byte.MaxValue, Convert.ToByte ("0XFF", 16), "0xFF,16");
3293 Assert.AreEqual (Byte.MaxValue, Convert.ToByte ("255", 10), "255,10");
3294 Assert.AreEqual (Byte.MaxValue, Convert.ToByte ("377", 8), "377,8");
3295 Assert.AreEqual (Byte.MaxValue, Convert.ToByte ("11111111", 2), "11111111,2");
3299 public void ToByte_MinValue ()
3301 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0", 16), "0,16");
3302 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0x0", 16), "0x0,16");
3303 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0", 10), "0,10");
3304 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0", 8), "0,8");
3305 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0", 2), "0,2");
3309 public void ToUInt16_MaxValue ()
3311 Assert.AreEqual (UInt16.MaxValue, Convert.ToUInt16 ("ffff", 16), "ffff,16");
3312 Assert.AreEqual (UInt16.MaxValue, Convert.ToUInt16 ("0XFFFF", 16), "0XFFFF,16");
3313 Assert.AreEqual (UInt16.MaxValue, Convert.ToUInt16 ("65535", 10), "65535,10");
3314 Assert.AreEqual (UInt16.MaxValue, Convert.ToUInt16 ("177777", 8), "177777,8");
3315 Assert.AreEqual (UInt16.MaxValue, Convert.ToUInt16 ("1111111111111111", 2), "1111111111111111,2");
3319 public void ToUInt16_MinValue ()
3321 Assert.AreEqual (UInt16.MinValue, Convert.ToUInt16 ("0", 16), "0,16");
3322 Assert.AreEqual (UInt16.MinValue, Convert.ToUInt16 ("0x0", 16), "0x0,16");
3323 Assert.AreEqual (UInt16.MinValue, Convert.ToUInt16 ("0", 10), "0,10");
3324 Assert.AreEqual (UInt16.MinValue, Convert.ToUInt16 ("0", 8), "0,8");
3325 Assert.AreEqual (UInt16.MinValue, Convert.ToUInt16 ("0", 2), "0,2");
3329 public void ToUInt32_MaxValue ()
3331 Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 ("ffffffff", 16), "ffffffff,16");
3332 Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 ("0XFFFFFFFF", 16), "0XFFFFFFFF,16");
3333 Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 ("4294967295", 10), "4294967295,10");
3334 Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 ("37777777777", 8), "37777777777,8");
3335 Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 ("11111111111111111111111111111111", 2), "11111111111111111111111111111111,2");
3339 public void ToUInt32_MinValue ()
3341 Assert.AreEqual (UInt32.MinValue, Convert.ToUInt32 ("0", 16), "0,16");
3342 Assert.AreEqual (UInt32.MinValue, Convert.ToUInt32 ("0x0", 16), "0x0,16");
3343 Assert.AreEqual (UInt32.MinValue, Convert.ToUInt32 ("0", 10), "0,10");
3344 Assert.AreEqual (UInt32.MinValue, Convert.ToUInt32 ("0", 8), "0,8");
3345 Assert.AreEqual (UInt32.MinValue, Convert.ToUInt32 ("0", 2), "0,2");
3349 public void ToUInt64_MaxValue ()
3351 Assert.AreEqual (UInt64.MaxValue, Convert.ToUInt64 ("ffffffffffffffff", 16), "ffffffffffffffff,16");
3352 Assert.AreEqual (UInt64.MaxValue, Convert.ToUInt64 ("0XFFFFFFFFFFFFFFFF", 16), "0XFFFFFFFFFFFFFFFF,16");
3353 Assert.AreEqual (UInt64.MaxValue, Convert.ToUInt64 ("18446744073709551615", 10), "18446744073709551615,10");
3354 Assert.AreEqual (UInt64.MaxValue, Convert.ToUInt64 ("1777777777777777777777", 8), "1777777777777777777777,8");
3355 Assert.AreEqual (UInt64.MaxValue, Convert.ToUInt64 ("1111111111111111111111111111111111111111111111111111111111111111", 2), "1111111111111111111111111111111111111111111111111111111111111111,2");
3359 public void ToUInt64_MinValue ()
3361 Assert.AreEqual (UInt64.MinValue, Convert.ToUInt64 ("0", 16), "0,16");
3362 Assert.AreEqual (UInt64.MinValue, Convert.ToUInt64 ("0x0", 16), "0x0,16");
3363 Assert.AreEqual (UInt64.MinValue, Convert.ToUInt64 ("0", 10), "0,10");
3364 Assert.AreEqual (UInt64.MinValue, Convert.ToUInt64 ("0", 8), "0,8");
3365 Assert.AreEqual (UInt64.MinValue, Convert.ToUInt64 ("0", 2), "0,2");
3371 public void ToSByte_MaxValue ()
3373 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("7f", 16), "7F,16");
3374 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("0X7F", 16), "0X7F,16");
3375 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("127", 10), "127,10");
3376 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("177", 8), "177,8");
3377 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("1111111", 2), "1111111,2");
3381 public void ToSByte_MinValue ()
3383 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("80", 16), "80,16");
3384 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("-128", 10), "-128,10");
3385 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("200", 8), "200,8");
3386 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("10000000", 2), "10000000,2");
3390 public void ToInt16_MaxValue ()
3392 Assert.AreEqual (Int16.MaxValue, Convert.ToInt16 ("7fff", 16), "7FFF,16");
3393 Assert.AreEqual (Int16.MaxValue, Convert.ToInt16 ("0X7FFF", 16), "0X7FFF,16");
3394 Assert.AreEqual (Int16.MaxValue, Convert.ToInt16 ("32767", 10), "32767,10");
3395 Assert.AreEqual (Int16.MaxValue, Convert.ToInt16 ("77777", 8), "77777,8");
3396 Assert.AreEqual (Int16.MaxValue, Convert.ToInt16 ("111111111111111", 2), "111111111111111,2");
3400 public void ToInt16_MinValue ()
3402 Assert.AreEqual (Int16.MinValue, Convert.ToInt16 ("8000", 16), "8000,16");
3403 Assert.AreEqual (Int16.MinValue, Convert.ToInt16 ("-32768", 10), "-32768,10");
3404 Assert.AreEqual (Int16.MinValue, Convert.ToInt16 ("100000", 8), "100000,8");
3405 Assert.AreEqual (Int16.MinValue, Convert.ToInt16 ("1000000000000000", 2), "1000000000000000,2");
3409 public void ToInt32_MaxValue ()
3411 Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 ("7fffffff", 16), "7fffffff,16");
3412 Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 ("0X7FFFFFFF", 16), "0X7FFFFFFF,16");
3413 Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 ("2147483647", 10), "2147483647,10");
3414 Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 ("17777777777", 8), "17777777777,8");
3415 Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 ("1111111111111111111111111111111", 2), "1111111111111111111111111111111,2");
3419 public void ToInt32_MinValue ()
3421 Assert.AreEqual (Int32.MinValue, Convert.ToInt32 ("80000000", 16), "80000000,16");
3422 Assert.AreEqual (Int32.MinValue, Convert.ToInt32 ("-2147483648", 10), "-2147483648,10");
3423 Assert.AreEqual (Int32.MinValue, Convert.ToInt32 ("20000000000", 8), "20000000000,8");
3424 Assert.AreEqual (Int32.MinValue, Convert.ToInt32 ("10000000000000000000000000000000", 2), "10000000000000000000000000000000,2");
3428 public void ToInt64_MaxValue ()
3430 Assert.AreEqual (Int64.MaxValue, Convert.ToInt64 ("7fffffffffffffff", 16), "7fffffffffffffff,16");
3431 Assert.AreEqual (Int64.MaxValue, Convert.ToInt64 ("0X7FFFFFFFFFFFFFFF", 16), "0X7FFFFFFFFFFFFFFF,16");
3432 Assert.AreEqual (Int64.MaxValue, Convert.ToInt64 ("9223372036854775807", 10), "9223372036854775807,10");
3433 Assert.AreEqual (Int64.MaxValue, Convert.ToInt64 ("777777777777777777777", 8), "777777777777777777777,8");
3434 Assert.AreEqual (Int64.MaxValue, Convert.ToInt64 ("111111111111111111111111111111111111111111111111111111111111111", 2), "111111111111111111111111111111111111111111111111111111111111111,2");
3438 public void ToInt64_MinValue ()
3440 Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("8000000000000000", 16), "8000000000000000,16");
3441 Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("0x8000000000000000", 16), "0x8000000000000000,16");
3442 Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("-9223372036854775808", 10), "-9223372036854775808,10");
3443 Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("1000000000000000000000", 8), "1000000000000000000000,8");
3444 Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("1000000000000000000000000000000000000000000000000000000000000000", 2), "1000000000000000000000000000000000000000000000000000000000000000,2");
3450 [ExpectedException (typeof (OverflowException))]
3451 public void ToSByte_OverMaxValue ()
3453 string max_plus1 = "128";
3454 Convert.ToSByte (max_plus1);
3458 [ExpectedException (typeof (OverflowException))]
3459 public void ToSByte_OverMinValue ()
3461 string min_minus1 = "-129";
3462 Convert.ToSByte (min_minus1);
3466 [ExpectedException (typeof (OverflowException))]
3467 public void ToInt16_OverMaxValue ()
3469 string max_plus1 = "32768";
3470 Convert.ToInt16 (max_plus1);
3474 [ExpectedException (typeof (OverflowException))]
3475 public void ToInt16_OverMinValue ()
3477 string min_minus1 = "-32769";
3478 Convert.ToInt16 (min_minus1);
3482 [ExpectedException (typeof (OverflowException))]
3483 public void ToInt32_OverMaxValue ()
3485 string max_plus1 = "2147483648";
3486 Convert.ToInt32 (max_plus1);
3490 [ExpectedException (typeof (OverflowException))]
3491 public void ToInt32_OverMinValue ()
3493 string min_minus1 = "-2147483649";
3494 Convert.ToInt32 (min_minus1);
3498 [ExpectedException (typeof (OverflowException))]
3499 public void ToInt64_OverMaxValue ()
3501 string max_plus1 = "9223372036854775808";
3502 Convert.ToInt64 (max_plus1);
3506 [ExpectedException (typeof (OverflowException))]
3507 public void ToInt64_OverMinValue ()
3509 string min_minus1 = "-9223372036854775809";
3510 Convert.ToInt64 (min_minus1);
3516 [ExpectedException (typeof (OverflowException))]
3517 public void ToByte_OverMaxValue ()
3519 string max_plus1 = "257";
3520 Convert.ToByte (max_plus1);
3524 [ExpectedException (typeof (OverflowException))]
3525 public void ToByte_OverMinValue ()
3527 string min_minus1 = "-1";
3528 Convert.ToByte (min_minus1);
3532 [ExpectedException (typeof (OverflowException))]
3533 public void ToUInt16_OverMaxValue ()
3535 string max_plus1 = "65536";
3536 Convert.ToUInt16 (max_plus1);
3540 [ExpectedException (typeof (OverflowException))]
3541 public void ToUInt16_OverMinValue ()
3543 string min_minus1 = "-1";
3544 Convert.ToUInt16 (min_minus1);
3548 [ExpectedException (typeof (OverflowException))]
3549 public void ToUInt32_OverMaxValue ()
3551 string max_plus1 = "4294967296";
3552 Convert.ToUInt32 (max_plus1);
3556 [ExpectedException (typeof (OverflowException))]
3557 public void ToUInt32_OverMinValue ()
3559 string min_minus1 = "-1";
3560 Convert.ToUInt32 (min_minus1);
3564 [ExpectedException (typeof (OverflowException))]
3565 public void ToUInt64_OverMaxValue ()
3567 string max_plus1 = "18446744073709551616";
3568 Convert.ToUInt64 (max_plus1);
3572 [ExpectedException (typeof (OverflowException))]
3573 public void ToUInt64_OverMinValue ()
3575 string min_minus1 = "-1";
3576 Convert.ToUInt64 (min_minus1);
3580 public void To_NullString ()
3584 Assert.AreEqual (0, Convert.ToSByte (s), "ToSByte");
3585 Assert.AreEqual (0, Convert.ToSByte (s, 10), "ToSByte+base");
3586 Assert.AreEqual (0, Convert.ToInt16 (s), "ToInt16");
3587 Assert.AreEqual (0, Convert.ToInt16 (s, 10), "ToInt16+base");
3588 Assert.AreEqual (0, Convert.ToInt32 (s), "ToInt32");
3589 Assert.AreEqual (0, Convert.ToInt32 (s, 10), "ToInt32+base");
3590 Assert.AreEqual (0, Convert.ToInt64 (s), "ToInt64");
3591 Assert.AreEqual (0, Convert.ToInt64 (s, 10), "ToInt64+base");
3593 Assert.AreEqual (0, Convert.ToByte (s), "ToByte");
3594 Assert.AreEqual (0, Convert.ToByte (s, 10), "ToByte+base");
3595 Assert.AreEqual (0, Convert.ToUInt16 (s), "ToUInt16");
3596 Assert.AreEqual (0, Convert.ToUInt16 (s, 10), "ToUInt16+base");
3597 Assert.AreEqual (0, Convert.ToUInt32 (s), "ToUInt32");
3598 Assert.AreEqual (0, Convert.ToUInt32 (s, 10), "ToUInt32+base");
3599 Assert.AreEqual (0, Convert.ToUInt64 (s), "ToUInt64");
3600 Assert.AreEqual (0, Convert.ToUInt64 (s, 10), "ToUInt64+base");
3604 public void To_NullObject ()
3608 Assert.AreEqual (0, Convert.ToSByte (o), "ToSByte");
3609 Assert.AreEqual (0, Convert.ToInt16 (o), "ToInt16");
3610 Assert.AreEqual (0, Convert.ToInt32 (o), "ToInt32");
3611 Assert.AreEqual (0, Convert.ToInt64 (o), "ToInt64");
3613 Assert.AreEqual (0, Convert.ToByte (o), "ToByte");
3614 Assert.AreEqual (0, Convert.ToUInt16 (o), "ToUInt16");
3615 Assert.AreEqual (0, Convert.ToUInt32 (o), "ToUInt32");
3616 Assert.AreEqual (0, Convert.ToUInt64 (o), "ToUInt64");
3620 public void To_NullObjectFormatProvider ()
3623 IFormatProvider fp = (IFormatProvider) new NumberFormatInfo ();
3625 Assert.AreEqual (0, Convert.ToSByte (o, fp), "ToSByte");
3626 Assert.AreEqual (0, Convert.ToInt16 (o, fp), "ToInt16");
3627 Assert.AreEqual (0, Convert.ToInt32 (o, fp), "ToInt32");
3628 Assert.AreEqual (0, Convert.ToInt64 (o, fp), "ToInt64");
3630 Assert.AreEqual (0, Convert.ToByte (o, fp), "ToByte");
3631 Assert.AreEqual (0, Convert.ToUInt16 (o, fp), "ToUInt16");
3632 Assert.AreEqual (0, Convert.ToUInt32 (o, fp), "ToUInt32");
3633 Assert.AreEqual (0, Convert.ToUInt64 (o, fp), "ToUInt64");
3637 [ExpectedException (typeof (ArgumentNullException))]
3638 public void ToSByte_NullStringFormatProvider ()
3641 // SByte is a "special" case ???
3642 Convert.ToSByte (s, new NumberFormatInfo ());
3646 public void To_NullStringFormatProvider ()
3649 IFormatProvider fp = (IFormatProvider) new NumberFormatInfo ();
3652 Assert.AreEqual (0, Convert.ToInt16 (s, fp), "ToInt16");
3653 Assert.AreEqual (0, Convert.ToInt32 (s, fp), "ToInt32");
3654 Assert.AreEqual (0, Convert.ToInt64 (s, fp), "ToInt64");
3656 Assert.AreEqual (0, Convert.ToByte (s, fp), "ToByte");
3657 Assert.AreEqual (0, Convert.ToUInt16 (s, fp), "ToUInt16");
3658 Assert.AreEqual (0, Convert.ToUInt32 (s, fp), "ToUInt32");
3659 Assert.AreEqual (0, Convert.ToUInt64 (s, fp), "ToUInt64");
3663 [ExpectedException (typeof (InvalidCastException))]
3664 public void ChangeTypeToTypeCodeEmpty ()
3666 Convert.ChangeType (true, TypeCode.Empty);
3670 [ExpectedException (typeof (ArgumentNullException))]
3671 public void CharChangeTypeNullNull ()
3674 IConvertible convert = a;
3675 convert.ToType (null, null);
3679 [ExpectedException (typeof (ArgumentNullException))]
3680 public void StringChangeTypeNullNull ()
3683 IConvertible convert = a;
3684 convert.ToType (null, null);
3688 [ExpectedException (typeof (InvalidCastException))]
3689 public void ChangeTypeNullToValuetype ()
3691 Convert.ChangeType (null, typeof (int));
3695 public void ToString_MinMax_WithBase ()
3697 Assert.AreEqual ("0", Convert.ToString (Byte.MinValue, 2), "Byte.MinValue base 2");
3698 Assert.AreEqual ("0", Convert.ToString (Byte.MinValue, 8), "Byte.MinValue base 8");
3699 Assert.AreEqual ("0", Convert.ToString (Byte.MinValue, 10), "Byte.MinValue base 10");
3700 Assert.AreEqual ("0", Convert.ToString (Byte.MinValue, 16), "Byte.MinValue base 16");
3702 Assert.AreEqual ("11111111", Convert.ToString (Byte.MaxValue, 2), "Byte.MaxValue base 2");
3703 Assert.AreEqual ("377", Convert.ToString (Byte.MaxValue, 8), "Byte.MaxValue base 8");
3704 Assert.AreEqual ("255", Convert.ToString (Byte.MaxValue, 10), "Byte.MaxValue base 10");
3705 Assert.AreEqual ("ff", Convert.ToString (Byte.MaxValue, 16), "Byte.MaxValue base 16");
3707 Assert.AreEqual ("1000000000000000", Convert.ToString (Int16.MinValue, 2), "Int16.MinValue base 2");
3708 Assert.AreEqual ("100000", Convert.ToString (Int16.MinValue, 8), "Int16.MinValue base 8");
3709 Assert.AreEqual ("-32768", Convert.ToString (Int16.MinValue, 10), "Int16.MinValue base 10");
3710 Assert.AreEqual ("8000", Convert.ToString (Int16.MinValue, 16), "Int16.MinValue base 16");
3712 Assert.AreEqual ("111111111111111", Convert.ToString (Int16.MaxValue, 2), "Int16.MaxValue base 2");
3713 Assert.AreEqual ("77777", Convert.ToString (Int16.MaxValue, 8), "Int16.MaxValue base 8");
3714 Assert.AreEqual ("32767", Convert.ToString (Int16.MaxValue, 10), "Int16.MaxValue base 10");
3715 Assert.AreEqual ("7fff", Convert.ToString (Int16.MaxValue, 16), "Int16.MaxValue base 16");
3717 Assert.AreEqual ("10000000000000000000000000000000", Convert.ToString (Int32.MinValue, 2), "Int32.MinValue base 2");
3718 Assert.AreEqual ("20000000000", Convert.ToString (Int32.MinValue, 8), "Int32.MinValue base 8");
3719 Assert.AreEqual ("-2147483648", Convert.ToString (Int32.MinValue, 10), "Int32.MinValue base 10");
3720 Assert.AreEqual ("80000000", Convert.ToString (Int32.MinValue, 16), "Int32.MinValue base 16");
3722 Assert.AreEqual ("1111111111111111111111111111111", Convert.ToString (Int32.MaxValue, 2), "Int32.MaxValue base 2");
3723 Assert.AreEqual ("17777777777", Convert.ToString (Int32.MaxValue, 8), "Int32.MaxValue base 8");
3724 Assert.AreEqual ("2147483647", Convert.ToString (Int32.MaxValue, 10), "Int32.MaxValue base 10");
3725 Assert.AreEqual ("7fffffff", Convert.ToString (Int32.MaxValue, 16), "Int32.MaxValue base 16");
3727 Assert.AreEqual ("1000000000000000000000000000000000000000000000000000000000000000", Convert.ToString (Int64.MinValue, 2), "Int64.MinValue base 2");
3728 Assert.AreEqual ("1000000000000000000000", Convert.ToString (Int64.MinValue, 8), "Int64.MinValue base 8");
3729 Assert.AreEqual ("-9223372036854775808", Convert.ToString (Int64.MinValue, 10), "Int64.MinValue base 10");
3730 Assert.AreEqual ("8000000000000000", Convert.ToString (Int64.MinValue, 16), "Int64.MinValue base 16");
3732 Assert.AreEqual ("111111111111111111111111111111111111111111111111111111111111111", Convert.ToString (Int64.MaxValue, 2), "Int64.MaxValue base 2");
3733 Assert.AreEqual ("777777777777777777777", Convert.ToString (Int64.MaxValue, 8), "Int64.MaxValue base 8");
3734 Assert.AreEqual ("9223372036854775807", Convert.ToString (Int64.MaxValue, 10), "Int64.MaxValue base 10");
3735 Assert.AreEqual ("7fffffffffffffff", Convert.ToString (Int64.MaxValue, 16), "Int64.MaxValue base 16");
3739 [ExpectedException (typeof (FormatException))]
3740 public void ToByte_BadHexPrefix1 ()
3742 Convert.ToByte ("#10", 16);
3746 [ExpectedException (typeof (FormatException))]
3747 public void ToByte_BadHexPrefix2 ()
3749 Convert.ToByte ("&H10", 16);
3753 [ExpectedException (typeof (FormatException))]
3754 public void ToByte_BadHexPrefix3 ()
3756 Convert.ToByte ("&h10", 16);
3760 [ExpectedException (typeof (FormatException))]
3761 public void ToInt16_BadHexPrefix1 ()
3763 Convert.ToInt16 ("#10", 16);
3767 [ExpectedException (typeof (FormatException))]
3768 public void ToInt16_BadHexPrefix2 ()
3770 Convert.ToInt16 ("&H10", 16);
3774 [ExpectedException (typeof (FormatException))]
3775 public void ToInt16_BadHexPrefix3 ()
3777 Convert.ToInt16 ("&h10", 16);
3781 [ExpectedException (typeof (FormatException))]
3782 public void ToInt32_BadHexPrefix1 ()
3784 Convert.ToInt32 ("#10", 16);
3788 [ExpectedException (typeof (FormatException))]
3789 public void ToInt32_BadHexPrefix2 ()
3791 Convert.ToInt32 ("&H10", 16);
3795 [ExpectedException (typeof (FormatException))]
3796 public void ToInt32_BadHexPrefix3 ()
3798 Convert.ToInt32 ("&h10", 16);
3802 [ExpectedException (typeof (FormatException))]
3803 public void ToInt64_BadHexPrefix1 ()
3805 Convert.ToInt64 ("#10", 16);
3809 [ExpectedException (typeof (FormatException))]
3810 public void ToInt64_BadHexPrefix2 ()
3812 Convert.ToInt64 ("&H10", 16);
3816 [ExpectedException (typeof (FormatException))]
3817 public void ToInt64_BadHexPrefix3 ()
3819 Convert.ToInt64 ("&h10", 16);
3823 [ExpectedException (typeof (FormatException))]
3824 public void ToSByte_BadHexPrefix1 ()
3826 Convert.ToSByte ("#10", 16);
3830 [ExpectedException (typeof (FormatException))]
3831 public void ToSByte_BadHexPrefix2 ()
3833 Convert.ToSByte ("&H10", 16);
3837 [ExpectedException (typeof (FormatException))]
3838 public void ToSByte_BadHexPrefix3 ()
3840 Convert.ToSByte ("&h10", 16);
3844 [ExpectedException (typeof (FormatException))]
3845 public void ToUInt16_BadHexPrefix1 ()
3847 Convert.ToUInt16 ("#10", 16);
3851 [ExpectedException (typeof (FormatException))]
3852 public void ToUInt16_BadHexPrefix2 ()
3854 Convert.ToUInt16 ("&H10", 16);
3858 [ExpectedException (typeof (FormatException))]
3859 public void ToUInt16_BadHexPrefix3 ()
3861 Convert.ToUInt16 ("&h10", 16);
3865 [ExpectedException (typeof (FormatException))]
3866 public void ToUInt32_BadHexPrefix1 ()
3868 Convert.ToUInt32 ("#10", 16);
3872 [ExpectedException (typeof (FormatException))]
3873 public void ToUInt32_BadHexPrefix2 ()
3875 Convert.ToUInt32 ("&H10", 16);
3879 [ExpectedException (typeof (FormatException))]
3880 public void ToUInt32_BadHexPrefix3 ()
3882 Convert.ToUInt32 ("&h10", 16);
3886 [ExpectedException (typeof (FormatException))]
3887 public void ToUInt64_BadHexPrefix1 ()
3889 Convert.ToUInt64 ("#10", 16);
3893 [ExpectedException (typeof (FormatException))]
3894 public void ToUInt64_BadHexPrefix2 ()
3896 Convert.ToUInt64 ("&H10", 16);
3900 [ExpectedException (typeof (FormatException))]
3901 public void ToUInt64_BadHexPrefix3 ()
3903 Convert.ToUInt64 ("&h10", 16);
3907 public void ToSByte_Base16_MinMax ()
3909 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("80", 16), "80,16");
3910 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("0x80", 16), "0x80,16");
3911 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("0X80", 16), "0X80,16");
3913 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("7f", 16), "7f,16");
3914 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("7F", 16), "7F,16");
3915 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("0x7f", 16), "0x7f,16");
3916 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("0X7F", 16), "0X7F,16");
3920 public void ToInt16_Base16_MinMax ()
3922 Assert.AreEqual (short.MinValue, Convert.ToInt16 ("8000", 16), "8000,16");
3923 Assert.AreEqual (short.MinValue, Convert.ToInt16 ("0x8000", 16), "0x8000,16");
3924 Assert.AreEqual (short.MinValue, Convert.ToInt16 ("0X8000", 16), "0X8000,16");
3926 Assert.AreEqual (short.MaxValue, Convert.ToInt16 ("7fff", 16), "7fff,16");
3927 Assert.AreEqual (short.MaxValue, Convert.ToInt16 ("7FFF", 16), "7FFF,16");
3928 Assert.AreEqual (short.MaxValue, Convert.ToInt16 ("0x7fff", 16), "0x7fff,16");
3929 Assert.AreEqual (short.MaxValue, Convert.ToInt16 ("0X7FFF", 16), "0X7FFF,16");
3933 public void ToInt32_Base16_MinMax ()
3935 Assert.AreEqual (int.MinValue, Convert.ToInt32 ("80000000", 16), "80000000,16");
3936 Assert.AreEqual (int.MinValue, Convert.ToInt32 ("0x80000000", 16), "0x80000000,16");
3937 Assert.AreEqual (int.MinValue, Convert.ToInt32 ("0X80000000", 16), "0X80000000,16");
3939 Assert.AreEqual (int.MaxValue, Convert.ToInt32 ("7fffffff", 16), "7fffffff,16");
3940 Assert.AreEqual (int.MaxValue, Convert.ToInt32 ("7FFFFFFF", 16), "7FFFFFFF,16");
3941 Assert.AreEqual (int.MaxValue, Convert.ToInt32 ("0x7fffffff", 16), "0x7fffffff,16");
3942 Assert.AreEqual (int.MaxValue, Convert.ToInt32 ("0X7FFFFFFF", 16), "0X7FFFFFFF,16");
3946 [ExpectedException (typeof (FormatException))]
3947 public void ToByte_Base10_InvalidChars1 ()
3949 Convert.ToByte ("0-1", 10);
3953 [ExpectedException (typeof (FormatException))]
3954 public void ToByte_Base10_InvalidChars2 ()
3956 Convert.ToByte ("FF", 10);
3960 [ExpectedException (typeof (FormatException))]
3961 public void ToInt16_Base10_InvalidChars1 ()
3963 Convert.ToInt16 ("0-1", 10);
3967 [ExpectedException (typeof (FormatException))]
3968 public void ToInt16_Base10_InvalidChars2 ()
3970 Convert.ToInt16 ("FF", 10);
3974 [ExpectedException (typeof (FormatException))]
3975 public void ToInt32_Base10_InvalidChars1 ()
3977 Convert.ToInt32 ("0-1", 10);
3981 [ExpectedException (typeof (FormatException))]
3982 public void ToInt32_Base10_InvalidChars2 ()
3984 Convert.ToInt32 ("FF", 10);
3988 [ExpectedException (typeof (FormatException))]
3989 public void ToInt64_Base10_InvalidChars1 ()
3991 Convert.ToInt64 ("0-1", 10);
3995 [ExpectedException (typeof (FormatException))]
3996 public void ToInt64_Base10_InvalidChars2 ()
3998 Convert.ToInt64 ("FF", 10);
4002 [ExpectedException (typeof (FormatException))]
4003 public void ToSByte_Base10_InvalidChars1 ()
4005 Convert.ToSByte ("0-1", 10);
4009 [ExpectedException (typeof (FormatException))]
4010 public void ToSByte_Base10_InvalidChars2 ()
4012 Convert.ToSByte ("FF", 10);
4016 [ExpectedException (typeof (FormatException))]
4017 public void ToUInt16_Base10_InvalidChars1 ()
4019 Convert.ToUInt16 ("0-1", 10);
4023 [ExpectedException (typeof (FormatException))]
4024 public void ToUInt16_Base10_InvalidChars2 ()
4026 Convert.ToUInt16 ("FF", 10);
4030 [ExpectedException (typeof (FormatException))]
4031 public void ToUInt32_Base10_InvalidChars1 ()
4033 Convert.ToUInt32 ("0-1", 10);
4037 [ExpectedException (typeof (FormatException))]
4038 public void ToUInt32_Base10_InvalidChars2 ()
4040 Convert.ToUInt32 ("FF", 10);
4044 [ExpectedException (typeof (FormatException))]
4045 public void ToUInt64_Base10_InvalidChars1 ()
4047 Convert.ToUInt64 ("0-1", 10);
4051 [ExpectedException (typeof (FormatException))]
4052 public void ToUInt64_Base10_InvalidChars2 ()
4054 Convert.ToUInt64 ("FF", 10);
4058 [ExpectedException (typeof (FormatException))]
4059 public void ToByte_Base16_InvalidChars1 ()
4061 Convert.ToByte ("0-1", 16);
4065 [ExpectedException (typeof (FormatException))]
4066 public void ToByte_Base16_InvalidChars2 ()
4068 Convert.ToByte ("GG", 16);
4072 [ExpectedException (typeof (FormatException))]
4073 public void ToInt16_Base16_InvalidChars1 ()
4075 Convert.ToInt16 ("0-1", 16);
4079 [ExpectedException (typeof (FormatException))]
4080 public void ToInt16_Base16_InvalidChars2 ()
4082 Convert.ToInt16 ("GG", 16);
4086 [ExpectedException (typeof (FormatException))]
4087 public void ToInt32_Base16_InvalidChars1 ()
4089 Convert.ToInt32 ("0-1", 16);
4093 [ExpectedException (typeof (FormatException))]
4094 public void ToInt32_Base16_InvalidChars2 ()
4096 Convert.ToInt32 ("GG", 16);
4100 [ExpectedException (typeof (FormatException))]
4101 public void ToInt64_Base16_InvalidChars1 ()
4103 Convert.ToInt64 ("0-1", 16);
4107 [ExpectedException (typeof (FormatException))]
4108 public void ToInt64_Base16_InvalidChars2 ()
4110 Convert.ToInt64 ("GG", 16);
4114 [ExpectedException (typeof (FormatException))]
4115 public void ToSByte_Base16_InvalidChars1 ()
4117 Convert.ToSByte ("0-1", 16);
4121 [ExpectedException (typeof (FormatException))]
4122 public void ToSByte_Base16_InvalidChars2 ()
4124 Convert.ToSByte ("GG", 16);
4128 [ExpectedException (typeof (FormatException))]
4129 public void ToUInt16_Base16_InvalidChars1 ()
4131 Convert.ToUInt16 ("0-1", 16);
4135 [ExpectedException (typeof (FormatException))]
4136 public void ToUInt16_Base16_InvalidChars2 ()
4138 Convert.ToUInt16 ("GG", 16);
4142 [ExpectedException (typeof (FormatException))]
4143 public void ToUInt32_Base16_InvalidChars1 ()
4145 Convert.ToUInt32 ("0-1", 16);
4149 [ExpectedException (typeof (FormatException))]
4150 public void ToUInt32_Base16_InvalidChars2 ()
4152 Convert.ToUInt32 ("GG", 16);
4156 [ExpectedException (typeof (FormatException))]
4157 public void ToUInt64_Base16_InvalidChars1 ()
4159 Convert.ToUInt64 ("0-1", 16);
4163 [ExpectedException (typeof (FormatException))]
4164 public void ToUInt64_Base16_InvalidChars2 ()
4166 Convert.ToUInt64 ("GG", 16);
4170 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4171 public void ToByte_Base2_Empty ()
4173 Convert.ToByte ("", 2);
4177 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4178 public void ToByte_Base8_Empty ()
4180 Convert.ToByte ("", 8);
4184 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4185 public void ToByte_Base10_Empty ()
4187 Convert.ToByte ("", 10);
4191 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4192 public void ToByte_Base16_Empty ()
4194 Convert.ToByte ("", 16);
4198 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4199 public void ToInt16_Base2_Empty ()
4201 Convert.ToInt16 ("", 2);
4205 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4206 public void ToInt16_Base8_Empty ()
4208 Convert.ToInt16 ("", 8);
4212 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4213 public void ToInt16_Base10_Empty ()
4215 Convert.ToInt16 ("", 10);
4219 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4220 public void ToInt16_Base16_Empty ()
4222 Convert.ToInt16 ("", 16);
4226 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4227 public void ToInt32_Base2_Empty ()
4229 Convert.ToInt32 ("", 2);
4233 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4234 public void ToInt32_Base8_Empty ()
4236 Convert.ToInt32 ("", 8);
4240 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4241 public void ToInt32_Base10_Empty ()
4243 Convert.ToInt32 ("", 10);
4247 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4248 public void ToInt32_Base16_Empty ()
4250 Convert.ToInt32 ("", 16);
4254 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4255 public void ToInt64_Base2_Empty ()
4257 Convert.ToInt64 ("", 2);
4261 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4262 public void ToInt64_Base8_Empty ()
4264 Convert.ToInt64 ("", 8);
4268 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4269 public void ToInt64_Base10_Empty ()
4271 Convert.ToInt64 ("", 10);
4275 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4276 public void ToInt64_Base16_Empty ()
4278 Convert.ToInt64 ("", 16);
4282 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4283 public void ToSByte_Base2_Empty ()
4285 Convert.ToSByte ("", 2);
4289 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4290 public void ToSByte_Base8_Empty ()
4292 Convert.ToSByte ("", 8);
4296 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4297 public void ToSByte_Base10_Empty ()
4299 Convert.ToSByte ("", 10);
4303 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4304 public void ToSByte_Base16_Empty ()
4306 Convert.ToSByte ("", 16);
4310 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4311 public void ToUInt16_Base2_Empty ()
4313 Convert.ToUInt16 ("", 2);
4317 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4318 public void ToUInt16_Base8_Empty ()
4320 Convert.ToUInt16 ("", 8);
4324 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4325 public void ToUInt16_Base10_Empty ()
4327 Convert.ToUInt16 ("", 10);
4331 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4332 public void ToUInt16_Base16_Empty ()
4334 Convert.ToUInt16 ("", 16);
4338 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4339 public void ToUInt32_Base2_Empty ()
4341 Convert.ToUInt32 ("", 2);
4345 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4346 public void ToUInt32_Base8_Empty ()
4348 Convert.ToUInt32 ("", 8);
4352 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4353 public void ToUInt32_Base10_Empty ()
4355 Convert.ToUInt32 ("", 10);
4359 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4360 public void ToUInt32_Base16_Empty ()
4362 Convert.ToUInt32 ("", 16);
4366 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4367 public void ToUInt64_Base2_Empty ()
4369 Convert.ToUInt64 ("", 2);
4373 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4374 public void ToUInt64_Base8_Empty ()
4376 Convert.ToUInt64 ("", 8);
4380 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4381 public void ToUInt64_Base10_Empty ()
4383 Convert.ToUInt64 ("", 10);
4387 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4388 public void ToUInt64_Base16_Empty ()
4390 Convert.ToUInt64 ("", 16);
4394 [ExpectedException (typeof (FormatException))]
4395 public void ToByte_HexPrefixOnly ()
4397 Convert.ToByte ("0x", 16);
4401 [ExpectedException (typeof (FormatException))]
4402 public void ToInt16_HexPrefixOnly ()
4404 Convert.ToInt16 ("0x", 16);
4408 [ExpectedException (typeof (FormatException))]
4409 public void ToInt32_HexPrefixOnly ()
4411 Convert.ToInt32 ("0x", 16);
4415 [ExpectedException (typeof (FormatException))]
4416 public void ToInt64_HexPrefixOnly ()
4418 Convert.ToInt64 ("0x", 16);
4422 [ExpectedException (typeof (FormatException))]
4423 public void ToSByte_HexPrefixOnly ()
4425 Convert.ToSByte ("0x", 16);
4429 [ExpectedException (typeof (FormatException))]
4430 public void ToUInt16_HexPrefixOnly ()
4432 Convert.ToUInt16 ("0x", 16);
4436 [ExpectedException (typeof (FormatException))]
4437 public void ToUInt32_HexPrefixOnly ()
4439 Convert.ToUInt32 ("0x", 16);
4443 [ExpectedException (typeof (FormatException))]
4444 public void ToUInt64_HexPrefixOnly ()
4446 Convert.ToUInt64 ("0x", 16);
4450 [ExpectedException (typeof (ArgumentException))]
4451 public void ToByte_Base2_NegativeSignOnly ()
4453 Convert.ToByte ("-", 2);
4457 [ExpectedException (typeof (ArgumentException))]
4458 public void ToByte_Base8_NegativeSignOnly ()
4460 Convert.ToByte ("-", 8);
4464 [ExpectedException (typeof (OverflowException))]
4465 public void ToByte_Base10_NegativeSignOnly ()
4467 Convert.ToByte ("-", 10);
4471 [ExpectedException (typeof (ArgumentException))]
4472 public void ToByte_Base16_NegativeSignOnly ()
4474 Convert.ToByte ("-", 16);
4478 [ExpectedException (typeof (ArgumentException))]
4479 public void ToInt16_Base2_NegativeSignOnly ()
4481 Convert.ToInt16 ("-", 2);
4485 [ExpectedException (typeof (ArgumentException))]
4486 public void ToInt16_Base8_NegativeSignOnly ()
4488 Convert.ToInt16 ("-", 8);
4492 [ExpectedException (typeof (FormatException))]
4493 public void ToInt16_Base10_NegativeSignOnly ()
4495 Convert.ToInt16 ("-", 10);
4499 [ExpectedException (typeof (ArgumentException))]
4500 public void ToInt16_Base16_NegativeSignOnly ()
4502 Convert.ToInt16 ("-", 16);
4506 [ExpectedException (typeof (ArgumentException))]
4507 public void ToInt32_Base2_NegativeSignOnly ()
4509 Convert.ToInt32 ("-", 2);
4513 [ExpectedException (typeof (ArgumentException))]
4514 public void ToInt32_Base8_NegativeSignOnly ()
4516 Convert.ToInt32 ("-", 8);
4520 [ExpectedException (typeof (FormatException))]
4521 public void ToInt32_Base10_NegativeSignOnly ()
4523 Convert.ToInt32 ("-", 10);
4527 [ExpectedException (typeof (ArgumentException))]
4528 public void ToInt32_Base16_NegativeSignOnly ()
4530 Convert.ToInt32 ("-", 16);
4534 [ExpectedException (typeof (ArgumentException))]
4535 public void ToInt64_Base2_NegativeSignOnly ()
4537 Convert.ToInt64 ("-", 2);
4541 [ExpectedException (typeof (ArgumentException))]
4542 public void ToInt64_Base8_NegativeSignOnly ()
4544 Convert.ToInt64 ("-", 8);
4548 [ExpectedException (typeof (FormatException))]
4549 public void ToInt64_Base10_NegativeSignOnly ()
4551 Convert.ToInt64 ("-", 10);
4555 [ExpectedException (typeof (ArgumentException))]
4556 public void ToInt64_Base16_NegativeSignOnly ()
4558 Convert.ToInt64 ("-", 16);
4562 [ExpectedException (typeof (ArgumentException))]
4563 public void ToSByte_Base2_NegativeSignOnly ()
4565 Convert.ToSByte ("-", 2);
4569 [ExpectedException (typeof (ArgumentException))]
4570 public void ToSByte_Base8_NegativeSignOnly ()
4572 Convert.ToSByte ("-", 8);
4576 [ExpectedException (typeof (FormatException))]
4577 public void ToSByte_Base10_NegativeSignOnly ()
4579 Convert.ToSByte ("-", 10);
4583 [ExpectedException (typeof (ArgumentException))]
4584 public void ToSByte_Base16_NegativeSignOnly ()
4586 Convert.ToSByte ("-", 16);
4590 [ExpectedException (typeof (ArgumentException))]
4591 public void ToUInt16_Base2_NegativeSignOnly ()
4593 Convert.ToUInt16 ("-", 2);
4597 [ExpectedException (typeof (ArgumentException))]
4598 public void ToUInt16_Base8_NegativeSignOnly ()
4600 Convert.ToUInt16 ("-", 8);
4604 [ExpectedException (typeof (OverflowException))]
4605 public void ToUInt16_Base10_NegativeSignOnly ()
4607 Convert.ToUInt16 ("-", 10);
4611 [ExpectedException (typeof (ArgumentException))]
4612 public void ToUInt16_Base16_NegativeSignOnly ()
4614 Convert.ToUInt16 ("-", 16);
4618 [ExpectedException (typeof (ArgumentException))]
4619 public void ToUInt32_Base2_NegativeSignOnly ()
4621 Convert.ToUInt32 ("-", 2);
4625 [ExpectedException (typeof (ArgumentException))]
4626 public void ToUInt32_Base8_NegativeSignOnly ()
4628 Convert.ToUInt32 ("-", 8);
4632 [ExpectedException (typeof (OverflowException))]
4633 public void ToUInt32_Base10_NegativeSignOnly ()
4635 Convert.ToUInt32 ("-", 10);
4639 [ExpectedException (typeof (ArgumentException))]
4640 public void ToUInt32_Base16_NegativeSignOnly ()
4642 Convert.ToUInt32 ("-", 16);
4646 [ExpectedException (typeof (ArgumentException))]
4647 public void ToUInt64_Base2_NegativeSignOnly ()
4649 Convert.ToUInt64 ("-", 2);
4653 [ExpectedException (typeof (ArgumentException))]
4654 public void ToUInt64_Base8_NegativeSignOnly ()
4656 Convert.ToUInt64 ("-", 8);
4660 [ExpectedException (typeof (OverflowException))]
4661 public void ToUInt64_Base10_NegativeSignOnly ()
4663 Convert.ToUInt64 ("-", 10);
4667 [ExpectedException (typeof (ArgumentException))]
4668 public void ToUInt64_Base16_NegativeSignOnly ()
4670 Convert.ToUInt64 ("-", 16);
4673 [Test] // bug #481687
4674 public void ChangeType_Value_IConvertible ()
4676 BitmapStatus bitmapStatus = new BitmapStatus (3);
4677 Image c1 = Convert.ChangeType (bitmapStatus, typeof (Image)) as Image;
4678 Assert.IsNotNull (c1, "#A1");
4679 Assert.AreEqual (32, c1.ColorDepth, "#A2");
4681 bitmapStatus.ConvertToImage = false;
4682 object c2 = Convert.ChangeType (bitmapStatus, typeof (Image));
4683 Assert.IsNull (c2, "#B");
4685 object c3 = Convert.ChangeType (bitmapStatus, typeof (int));
4686 Assert.IsNotNull (c3, "#C1");
4687 Assert.AreEqual (3, c3, "#C2");
4690 // This is a simple and happy struct.
4695 [ExpectedException (typeof (InvalidCastException))]
4696 public void ChangeType_ShouldThrowOnString ()
4698 Convert.ChangeType ("this-is-a-string", typeof (Foo));
4702 [ExpectedException (typeof (OverflowException))]
4703 public void ToInt32_NaN ()
4705 Convert.ToInt32 (Double.NaN);
4709 public void ChangeTypeFromInvalidDouble ()
4711 // types which should generate OverflowException from double.NaN, etc.
4712 Type[] types = new Type []{
4713 typeof (byte), typeof (sbyte), typeof (decimal),
4714 typeof (short), typeof (int), typeof (long),
4715 typeof (ushort), typeof (uint), typeof (ulong),
4718 CheckChangeTypeOverflow ("double.NaN", double.NaN, types);
4719 CheckChangeTypeOverflow ("double.PositiveInfinity", double.PositiveInfinity, types);
4720 CheckChangeTypeOverflow ("double.NegativeInfinity", double.NegativeInfinity, types);
4721 CheckChangeTypeOverflow ("float.NaN", float.NaN, types);
4722 CheckChangeTypeOverflow ("float.PositiveInfinity", float.PositiveInfinity, types);
4723 CheckChangeTypeOverflow ("float.NegativeInfinity", float.NegativeInfinity, types);
4726 static void CheckChangeTypeOverflow (string svalue, object value, Type[] types)
4728 foreach (Type type in types) {
4729 string message = string.Format (" when converting {0} to {1}", svalue, type.FullName);
4731 Convert.ChangeType (value, type);
4732 Assert.Fail ("Expected System.OverflowException " + message);
4734 catch (OverflowException) {
4737 catch (Exception e) {
4738 Assert.Fail ("Unexpected exception type " + e.GetType ().FullName + message);
4744 public void ToInt32_InvalidFormatProvider ()
4746 Assert.AreEqual (5, Convert.ToInt32 ("5", new InvalidFormatProvider ()));
4752 private int colorDepth;
4759 public Image (int colorDepth)
4761 this.colorDepth = colorDepth;
4764 public int ColorDepth {
4765 get { return colorDepth; }
4769 public class BitmapStatus : IConvertible
4771 protected int m_Status;
4772 private bool convertToImage;
4774 public BitmapStatus (int status)
4777 convertToImage = true;
4780 public bool ConvertToImage {
4781 get { return convertToImage; }
4782 set { convertToImage = value; }
4785 TypeCode IConvertible.GetTypeCode ()
4787 return TypeCode.Int32;
4790 bool IConvertible.ToBoolean (IFormatProvider provider)
4792 return (bool)((IConvertible)this).ToType (typeof (bool), provider);
4795 byte IConvertible.ToByte (IFormatProvider provider)
4797 return (byte)((IConvertible)this).ToType (typeof (byte), provider);
4800 char IConvertible.ToChar (IFormatProvider provider)
4802 return (char)((IConvertible)this).ToType (typeof (char), provider);
4805 DateTime IConvertible.ToDateTime (IFormatProvider provider)
4807 return (DateTime)((IConvertible)this).ToType (typeof (DateTime), provider);
4810 decimal IConvertible.ToDecimal (IFormatProvider provider)
4812 return (decimal)((IConvertible)this).ToType (typeof (decimal), provider);
4815 double IConvertible.ToDouble (IFormatProvider provider)
4817 return (double)((IConvertible)this).ToType (typeof (double), provider);
4820 short IConvertible.ToInt16 (IFormatProvider provider)
4822 return (short)((IConvertible)this).ToType (typeof (short), provider);
4825 int IConvertible.ToInt32 (IFormatProvider provider)
4827 return (int)m_Status;
4830 long IConvertible.ToInt64 (IFormatProvider provider)
4832 return (long)((IConvertible)this).ToType (typeof (long), provider);
4835 sbyte IConvertible.ToSByte (IFormatProvider provider)
4837 return (sbyte)((IConvertible)this).ToType (typeof (sbyte), provider);
4840 float IConvertible.ToSingle (IFormatProvider provider)
4842 return (float)((IConvertible)this).ToType (typeof (float), provider);
4845 string IConvertible.ToString (IFormatProvider provider)
4847 return (string)((IConvertible)this).ToType (typeof (string), provider);
4850 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
4852 if (ConvertToImage && conversionType == typeof (Image))
4853 return new Image (32);
4854 else if (conversionType.IsAssignableFrom (typeof (int)))
4855 return Convert.ChangeType (1, conversionType, provider);
4859 ushort IConvertible.ToUInt16 (IFormatProvider provider)
4861 return (ushort)((IConvertible)this).ToType (typeof (ushort), provider);
4864 uint IConvertible.ToUInt32 (IFormatProvider provider)
4866 return (uint)((IConvertible)this).ToType (typeof (uint), provider);
4869 ulong IConvertible.ToUInt64 (IFormatProvider provider)
4871 return (ulong)((IConvertible)this).ToType (typeof (ulong), provider);
4875 class InvalidFormatProvider : IFormatProvider
4877 public object GetFormat (Type formatType)