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;
68 public void TestChangeType() {
71 Assert.AreEqual ((short)12345, Convert.ChangeType(tryInt32, typeof(short)), "#A01");
73 Assert.AreEqual ('A', Convert.ChangeType(65, typeof(char)), "#A02");
75 Assert.AreEqual (66, Convert.ChangeType('B', typeof(int)), "#A03");
77 Assert.AreEqual (((ulong)12345), Convert.ChangeType(tryInt32, typeof(ulong)), "#A04");
80 Assert.AreEqual (true, Convert.ChangeType(tryDec, TypeCode.Boolean), "#A05");
82 Assert.AreEqual ('f', Convert.ChangeType("f", TypeCode.Char), "#A06");
84 Assert.AreEqual ((decimal)123456789012, Convert.ChangeType(tryInt64, TypeCode.Decimal), "#A07");
86 Assert.AreEqual ((int)34567, Convert.ChangeType(tryUI16, TypeCode.Int32), "#A08");
89 Assert.AreEqual ((double)567891234, Convert.ChangeType(tryUI32, typeof(double), ci), "#A09");
91 Assert.AreEqual ((ushort)0, Convert.ChangeType(tryByte, typeof(ushort), ci), "#A10");
93 Assert.AreEqual ((decimal)567891234, Convert.ChangeType(tryUI32, typeof(decimal), ci), "#A11");
95 Assert.AreEqual ((float)1234, Convert.ChangeType(tryInt16, typeof(float), ci), "#A12");
98 Assert.AreEqual (null, Convert.ChangeType(null, null, ci), "#A13");
100 } catch (ArgumentNullException) {
104 Assert.AreEqual ((decimal)0, Convert.ChangeType(tryByte, TypeCode.Decimal, ci), "#A14");
106 Assert.AreEqual ("f", Convert.ChangeType('f', TypeCode.String, ci), "#A15");
108 Assert.AreEqual ('D', Convert.ChangeType(68, TypeCode.Char, ci), "#A16");
110 Assert.AreEqual ((long)34567, Convert.ChangeType(tryUI16, TypeCode.Int64, ci), "#A17");
112 Assert.AreEqual (null, Convert.ChangeType(null, TypeCode.Empty, ci), "#A18");
113 } catch (Exception e) {
114 Assert.Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
118 Convert.ChangeType(boolTrue, typeof(char));
121 catch (Exception e) {
122 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A25");
126 Convert.ChangeType(tryChar, typeof(DateTime));
129 catch (Exception e) {
130 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A26");
134 Convert.ChangeType(ci, TypeCode.String);
137 catch (Exception e) {
138 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A27");
142 Convert.ChangeType(tryInt32, null);
145 catch (Exception e) {
146 Assert.AreEqual (typeof(ArgumentNullException), e.GetType(), "#A28");
151 Convert.ChangeType(boolTrue, typeof(DateTime), ci);
154 catch (Exception e) {
155 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A29");
159 Convert.ChangeType(ci, typeof(DateTime), ci);
162 catch (Exception e) {
163 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A30");
166 /* Should throw ArgumentException but throws InvalidCastException
168 Convert.ChangeType(tryUI32, typeof(FormatException), ci);
171 catch (Exception e) {
172 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#A??");
176 Convert.ChangeType(tryUI32, TypeCode.Byte, ci);
179 catch (Exception e) {
180 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#A31");
184 Convert.ChangeType(boolTrue, TypeCode.Char, ci);
187 catch (Exception e) {
188 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A32");
192 Convert.ChangeType(boolTrue, null, ci);
195 catch (Exception e) {
196 Assert.AreEqual (typeof(ArgumentNullException), e.GetType(), "#A33");
200 /* should fail to convert string to any enumeration type. */
201 Convert.ChangeType("random string", typeof(DayOfWeek));
204 catch (Exception e) {
205 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#A34");
211 public void TestGetTypeCode() {
214 Assert.AreEqual (TypeCode.String, Convert.GetTypeCode(tryStr), "#B01");
216 Assert.AreEqual (TypeCode.UInt16, Convert.GetTypeCode(tryUI16), "#B02");
218 Assert.AreEqual (TypeCode.UInt32, Convert.GetTypeCode(tryUI32), "#B03");
220 Assert.AreEqual (TypeCode.UInt64, Convert.GetTypeCode(tryUI64), "#B04");
222 Assert.AreEqual (TypeCode.Double, Convert.GetTypeCode(tryDbl), "#B05");
224 Assert.AreEqual (TypeCode.Int16, Convert.GetTypeCode(tryInt16), "#B06");
226 Assert.AreEqual (TypeCode.Int64, Convert.GetTypeCode(tryInt64), "#B07");
228 Assert.AreEqual (TypeCode.Object, Convert.GetTypeCode(tryObj), "#B08");
230 Assert.AreEqual (TypeCode.SByte, Convert.GetTypeCode(trySByte), "#B09");
232 Assert.AreEqual (TypeCode.Single, Convert.GetTypeCode(tryFloat), "#B10");
234 Assert.AreEqual (TypeCode.Byte, Convert.GetTypeCode(tryByte), "#B11");
236 Assert.AreEqual (TypeCode.Char, Convert.GetTypeCode(tryChar), "#B12");
238 // Assert.AreEqual (TypeCode.DateTime, Convert.GetTypeCode(tryDT), "#B13");
240 Assert.AreEqual (TypeCode.Decimal, Convert.GetTypeCode(tryDec), "#B14");
242 Assert.AreEqual (TypeCode.Int32, Convert.GetTypeCode(tryInt32), "#B15");
244 Assert.AreEqual (TypeCode.Boolean, Convert.GetTypeCode(boolTrue), "#B16");
245 } catch (Exception e) {
246 Assert.Fail ("Unexpected exception at " + marker + ": " + e);
251 public void TestIsDBNull() {
252 Assert.AreEqual (false, Convert.IsDBNull(tryInt32), "#C01");
253 Assert.AreEqual (true, Convert.IsDBNull(Convert.DBNull), "#C02");
254 Assert.AreEqual (false, Convert.IsDBNull(boolTrue), "#C03");
255 Assert.AreEqual (false, Convert.IsDBNull(tryChar), "#C04");
256 Assert.AreEqual (false, Convert.IsDBNull(tryFloat), "#C05");
260 public void TestToBoolean() {
261 tryObj = (object)tryDbl;
263 Assert.AreEqual (true, Convert.ToBoolean(boolTrue), "#D01");
264 Assert.AreEqual (false, Convert.ToBoolean(tryByte), "#D02");
265 Assert.AreEqual (true, Convert.ToBoolean(tryDec), "#D03");
266 Assert.AreEqual (false, Convert.ToBoolean(tryDbl), "#D04");
267 Assert.AreEqual (true, Convert.ToBoolean(tryInt16), "#D05");
268 Assert.AreEqual (true, Convert.ToBoolean(tryInt32), "#D06");
269 Assert.AreEqual (true, Convert.ToBoolean(tryInt64), "#D07");
270 Assert.AreEqual (false, Convert.ToBoolean(tryObj), "#D08");
271 Assert.AreEqual (true, Convert.ToBoolean(trySByte), "#D09");
272 Assert.AreEqual (true, Convert.ToBoolean(tryFloat), "#D10");
273 Assert.AreEqual (true, Convert.ToBoolean(trueString), "#D11");
274 Assert.AreEqual (false, Convert.ToBoolean(falseString), "#D12");
275 Assert.AreEqual (true, Convert.ToBoolean(tryUI16), "#D13");
276 Assert.AreEqual (true, Convert.ToBoolean(tryUI32), "#D14");
277 Assert.AreEqual (false, Convert.ToBoolean(tryUI64), "#D15");
278 Assert.AreEqual (false, Convert.ToBoolean(tryObj, ci), "#D16");
279 Assert.AreEqual (true, Convert.ToBoolean(trueString, ci), "#D17");
280 Assert.AreEqual (false, Convert.ToBoolean(falseString, ci), "#D18");
283 Convert.ToBoolean(tryChar);
286 catch (Exception e) {
287 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#D20");
291 Convert.ToBoolean(tryDT);
294 catch (Exception e) {
295 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#D21");
299 Convert.ToBoolean(tryStr);
302 catch (Exception e) {
303 Assert.AreEqual (typeof(FormatException), e.GetType(), "#D22");
307 Convert.ToBoolean(nullString);
310 catch (Exception e) {
311 Assert.AreEqual (typeof(FormatException), e.GetType(), "#D23");
316 public void TestToByte() {
318 Assert.AreEqual ((byte)1, Convert.ToByte(boolTrue), "#E01");
319 Assert.AreEqual ((byte)0, Convert.ToByte(boolFalse), "#E02");
320 Assert.AreEqual (tryByte, Convert.ToByte(tryByte), "#E03");
321 Assert.AreEqual ((byte)114, Convert.ToByte('r'), "#E04");
322 Assert.AreEqual ((byte)201, Convert.ToByte((decimal)200.6), "#E05");
323 Assert.AreEqual ((byte)125, Convert.ToByte((double)125.4), "#E06");
324 Assert.AreEqual ((byte)255, Convert.ToByte((short)255), "#E07");
325 Assert.AreEqual ((byte)254, Convert.ToByte((int)254), "#E08");
326 Assert.AreEqual ((byte)34, Convert.ToByte((long)34), "#E09");
327 Assert.AreEqual ((byte)1, Convert.ToByte((object)boolTrue), "#E10");
328 Assert.AreEqual ((byte)123, Convert.ToByte((float)123.49f), "#E11");
329 Assert.AreEqual ((byte)57, Convert.ToByte("57"), "#E12");
330 Assert.AreEqual ((byte)75, Convert.ToByte((ushort)75), "#E13");
331 Assert.AreEqual ((byte)184, Convert.ToByte((uint)184), "#E14");
332 Assert.AreEqual ((byte)241, Convert.ToByte((ulong)241), "#E15");
333 Assert.AreEqual ((byte)123, Convert.ToByte(trySByte, ci), "#E16");
334 Assert.AreEqual ((byte)27, Convert.ToByte("011011", 2), "#E17");
335 Assert.AreEqual ((byte)13, Convert.ToByte("15", 8), "#E18");
336 Assert.AreEqual ((byte)27, Convert.ToByte("27", 10), "#E19");
337 Assert.AreEqual ((byte)250, Convert.ToByte("FA", 16), "#E20");
340 Convert.ToByte('\u03A9'); // sign of Omega on Win2k
343 catch (Exception e) {
344 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E25");
348 Convert.ToByte(tryDT);
351 catch (Exception e) {
352 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#D26");
356 Convert.ToByte((decimal)22000);
359 catch (Exception e) {
360 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E27");
364 Convert.ToByte((double)255.5);
367 catch (Exception e) {
368 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E28");
372 Convert.ToByte(-tryInt16);
375 catch (Exception e) {
376 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E29");
380 Convert.ToByte((int)-256);
383 catch (Exception e) {
384 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E30");
388 Convert.ToByte(tryInt64);
391 catch (Exception e) {
392 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E31");
396 Convert.ToByte((object)ci);
399 catch (Exception e) {
400 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#E32");
404 Convert.ToByte((sbyte)-1);
407 catch (Exception e) {
408 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E33");
412 Convert.ToByte((float)-0.6f);
415 catch (Exception e) {
416 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E34");
420 Convert.ToByte("1a1");
423 catch (Exception e) {
424 Assert.AreEqual (typeof(FormatException), e.GetType(), "#E35");
428 Convert.ToByte("457");
431 catch (Exception e) {
432 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E36");
436 Convert.ToByte((ushort)30000);
439 catch (Exception e) {
440 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E37");
444 Convert.ToByte((uint)300);
447 catch (Exception e) {
448 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E38");
452 Convert.ToByte((ulong)987654321321);
455 catch (Exception e) {
456 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E39");
460 Convert.ToByte("10010111", 3);
463 catch (Exception e) {
464 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#E40");
468 Convert.ToByte("3F3", 16);
471 catch (Exception e) {
472 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#E41");
477 public void TestToChar(){
479 Assert.AreEqual (':', Convert.ToChar(tryByte), "#F01");
480 Assert.AreEqual ('a', Convert.ToChar(tryChar), "#F02");
481 Assert.AreEqual ('A', Convert.ToChar((short)65), "#F03");
482 Assert.AreEqual ('x', Convert.ToChar((int)120), "#F04");
483 Assert.AreEqual ('"', Convert.ToChar((long)34), "#F05");
484 Assert.AreEqual ('-', Convert.ToChar((sbyte)45), "#F06");
485 Assert.AreEqual ('@', Convert.ToChar("@"), "#F07");
486 Assert.AreEqual ('K', Convert.ToChar((ushort)75), "#F08");
487 Assert.AreEqual ('=', Convert.ToChar((uint)61), "#F09");
488 // Assert.AreEqual ('E', Convert.ToChar((ulong)200), "#F10");
489 Assert.AreEqual ('{', Convert.ToChar((object)trySByte, ci), "#F11");
490 Assert.AreEqual ('o', Convert.ToChar(tryStr.Substring(1, 1), ci), "#F12");
493 Convert.ToChar(boolTrue);
496 catch (Exception e) {
497 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F20");
501 Convert.ToChar(tryDT);
504 catch (Exception e) {
505 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F21");
509 Convert.ToChar(tryDec);
512 catch (Exception e) {
513 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F22");
517 Convert.ToChar(tryDbl);
520 catch (Exception e) {
521 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F23");
525 Convert.ToChar((short)-1);
528 catch (Exception e) {
529 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F24");
533 Convert.ToChar(Int32.MinValue);
536 catch (Exception e) {
537 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F25");
541 Convert.ToChar(Int32.MaxValue);
544 catch (Exception e) {
545 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F26");
549 Convert.ToChar(tryInt64);
552 catch (Exception e) {
553 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F27");
557 Convert.ToChar((long)-123);
560 catch (Exception e) {
561 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F28");
568 catch (Exception e) {
569 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F29");
573 Convert.ToChar(-trySByte);
576 catch (Exception e) {
577 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#F30");
581 Convert.ToChar(tryFloat);
584 catch (Exception e) {
585 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F31");
589 Convert.ToChar("foo");
592 catch (Exception e) {
593 Assert.AreEqual (typeof(FormatException), e.GetType(), "#F32");
597 Convert.ToChar(null);
600 catch (Exception e) {
601 Assert.AreEqual (typeof(ArgumentNullException), e.GetType(), "#F33");
605 Convert.ToChar(new Exception(), ci);
608 catch (Exception e) {
609 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#F34");
613 Convert.ToChar(null, ci);
616 catch (Exception e) {
617 Assert.AreEqual (typeof(ArgumentNullException), e.GetType(), "#F35");
621 Convert.ToChar("", ci);
624 catch (Exception e) {
625 Assert.AreEqual (typeof(FormatException), e.GetType(), "#F36");
629 Convert.ToChar(tryStr, ci);
632 catch (Exception e) {
633 Assert.AreEqual (typeof(FormatException), e.GetType(), "#F37");
637 /*[Ignore ("http://bugzilla.ximian.com/show_bug.cgi?id=45286")]
639 [ExpectedException (typeof (ArgumentOutOfRangeException))]
641 Convert.ToDateTime("20002-25-01");
645 public void TestToDateTime() {
646 string dateString = "01/01/2002";
649 Assert.AreEqual (tryDT, Convert.ToDateTime(tryDT), "#G01");
651 Assert.AreEqual (tryDT, Convert.ToDateTime(dateString), "#G02");
653 Assert.AreEqual (tryDT, Convert.ToDateTime(dateString, ci), "#G03");
654 } catch (Exception e) {
655 Assert.Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
659 Convert.ToDateTime(boolTrue);
662 catch (Exception e) {
663 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G10");
667 Convert.ToDateTime(tryByte);
670 catch (Exception e) {
671 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G11");
675 Convert.ToDateTime(tryChar);
678 catch (Exception e) {
679 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G12");
683 Convert.ToDateTime(tryDec);
686 catch (Exception e) {
687 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G13");
691 Convert.ToDateTime(tryDbl);
694 catch (Exception e) {
695 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G14");
699 Convert.ToDateTime(tryInt16);
702 catch (Exception e) {
703 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G15");
707 Convert.ToDateTime(tryInt32);
710 catch (Exception e) {
711 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G16");
715 Convert.ToDateTime(tryInt64);
718 catch (Exception e) {
719 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G17");
723 Convert.ToDateTime(ci);
726 catch (Exception e) {
727 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G18");
731 Convert.ToDateTime(trySByte);
734 catch (Exception e) {
735 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G19");
739 Convert.ToDateTime(tryFloat);
742 catch (Exception e) {
743 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G20");
747 Convert.ToDateTime("20a2-01-01");
750 catch (Exception e) {
751 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G21");
755 Convert.ToDateTime(tryUI16);
758 catch (Exception e) {
759 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G23");
763 Convert.ToDateTime(tryUI32);
766 catch (Exception e) {
767 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G24");
771 Convert.ToDateTime(tryUI64);
774 catch (Exception e) {
775 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G25");
779 Convert.ToDateTime(ci, ci);
782 catch (Exception e) {
783 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#G26");
787 Convert.ToDateTime("20a2-01-01", ci);
790 catch (Exception e) {
791 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G27");
794 // this is supported by .net 1.1 (defect 41845)
796 Convert.ToDateTime("20022-01-01");
799 catch (Exception e) {
800 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G28");
804 Convert.ToDateTime("2002-21-01");
807 catch (Exception e) {
808 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G29");
812 Convert.ToDateTime("2002-111-01");
815 catch (Exception e) {
816 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G30");
820 Convert.ToDateTime("2002-01-41");
823 catch (Exception e) {
824 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G31");
828 Convert.ToDateTime("2002-01-111");
831 catch (Exception e) {
832 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G32");
836 Assert.AreEqual (tryDT, Convert.ToDateTime("2002-01-01"), "#G33");
837 } catch (Exception e) {
838 Assert.Fail ("Unexpected exception at #G33 " + e);
842 Convert.ToDateTime("2002-01-11 34:11:11");
845 catch (Exception e) {
846 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G34");
850 Convert.ToDateTime("2002-01-11 11:70:11");
853 catch (Exception e) {
854 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G35");
858 Convert.ToDateTime("2002-01-11 11:11:70");
861 catch (Exception e) {
862 Assert.AreEqual (typeof(FormatException), e.GetType(), "#G36");
868 public void TestToDecimal() {
869 Assert.AreEqual ((decimal)1, Convert.ToDecimal(boolTrue), "#H01");
870 Assert.AreEqual ((decimal)0, Convert.ToDecimal(boolFalse), "#H02");
871 Assert.AreEqual ((decimal)tryByte, Convert.ToDecimal(tryByte), "#H03");
872 Assert.AreEqual (tryDec, Convert.ToDecimal(tryDec), "#H04");
873 Assert.AreEqual ((decimal)tryDbl, Convert.ToDecimal(tryDbl), "#H05");
874 Assert.AreEqual ((decimal)tryInt16, Convert.ToDecimal(tryInt16), "#H06");
875 Assert.AreEqual ((decimal)tryInt32, Convert.ToDecimal(tryInt32), "#H07");
876 Assert.AreEqual ((decimal)tryInt64, Convert.ToDecimal(tryInt64), "#H08");
877 Assert.AreEqual ((decimal)trySByte, Convert.ToDecimal(trySByte), "#H09");
878 Assert.AreEqual ((decimal)tryFloat, Convert.ToDecimal(tryFloat), "#H10");
879 string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
880 // Assert.AreEqual ((decimal)23456.432, Convert.ToDecimal("23456" + sep + "432"), "#H11");
881 // Note: changed because the number were the same but with a different base
882 // and this isn't a Convert bug (but a Decimal bug). See #60227 for more details.
883 // http://bugzilla.ximian.com/show_bug.cgi?id=60227
884 Assert.IsTrue (Decimal.Equals (23456.432m, Convert.ToDecimal ("23456" + sep + "432")), "#H11");
885 Assert.AreEqual ((decimal)tryUI16, Convert.ToDecimal(tryUI16), "#H12");
886 Assert.AreEqual ((decimal)tryUI32, Convert.ToDecimal(tryUI32), "#H13");
887 Assert.AreEqual ((decimal)tryUI64, Convert.ToDecimal(tryUI64), "#H14");
888 Assert.AreEqual ((decimal)63784, Convert.ToDecimal("63784", ci), "#H15");
891 Convert.ToDecimal(tryChar);
894 catch (Exception e) {
895 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#H20");
899 Convert.ToDecimal(tryDT);
902 catch (Exception e) {
903 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#H21");
907 Convert.ToDecimal(double.MaxValue);
910 catch (Exception e) {
911 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#H22");
915 Convert.ToDecimal(double.MinValue);
918 catch (Exception e) {
919 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#H23");
923 Convert.ToDecimal(ci);
926 catch (Exception e) {
927 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#H24");
931 Convert.ToDecimal(tryStr);
934 catch (Exception e) {
935 Assert.AreEqual (typeof(FormatException), e.GetType(), "#H25");
939 string maxDec = decimal.MaxValue.ToString();
940 maxDec = maxDec + "1";
941 Convert.ToDecimal(maxDec);
944 catch (Exception e) {
945 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#H26");
949 Convert.ToDecimal(ci, ci);
952 catch (Exception e) {
953 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#H27");
957 Convert.ToDecimal(tryStr, ci);
960 catch (Exception e) {
961 Assert.AreEqual (typeof(FormatException), e.GetType(), "#H28");
965 string maxDec = decimal.MaxValue.ToString();
966 maxDec = maxDec + "1";
967 Convert.ToDecimal(maxDec, ci);
970 catch (Exception e) {
971 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#H29");
976 public void TestToDouble() {
979 Assert.AreEqual ((double)1, Convert.ToDouble(boolTrue), "#I01");
981 Assert.AreEqual ((double)0, Convert.ToDouble(boolFalse), "#I02");
983 Assert.AreEqual ((double)tryByte, Convert.ToDouble(tryByte), "#I03");
985 Assert.AreEqual (tryDbl, Convert.ToDouble(tryDbl), "#I04");
987 Assert.AreEqual ((double)tryDec, Convert.ToDouble(tryDec), "#I05");
989 Assert.AreEqual ((double)tryInt16, Convert.ToDouble(tryInt16), "#I06");
991 Assert.AreEqual ((double)tryInt32, Convert.ToDouble(tryInt32), "#I07");
993 Assert.AreEqual ((double)tryInt64, Convert.ToDouble(tryInt64), "#I08");
995 Assert.AreEqual ((double)trySByte, Convert.ToDouble(trySByte), "#I09");
997 Assert.AreEqual ((double)tryFloat, Convert.ToDouble(tryFloat), "#I10");
999 string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
1000 Assert.AreEqual ((double)23456.432, Convert.ToDouble("23456" + sep + "432"), "#I11");
1002 Assert.AreEqual ((double)tryUI16, Convert.ToDouble(tryUI16), "#I12");
1004 Assert.AreEqual ((double)tryUI32, Convert.ToDouble(tryUI32), "#I13");
1006 Assert.AreEqual ((double)tryUI64, Convert.ToDouble(tryUI64), "#I14");
1008 Assert.AreEqual ((double)63784, Convert.ToDouble("63784", ci), "#H15");
1009 } catch (Exception e) {
1010 Assert.Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1014 Convert.ToDouble(tryChar);
1017 catch (Exception e) {
1018 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#I20");
1022 Convert.ToDouble(tryDT);
1025 catch (Exception e) {
1026 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#I21");
1030 Convert.ToDouble(ci);
1033 catch (Exception e) {
1034 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#I22");
1038 Convert.ToDouble(tryStr);
1041 catch (Exception e) {
1042 Assert.AreEqual (typeof(FormatException), e.GetType(), "#I23");
1046 string maxDec = double.MaxValue.ToString();
1047 maxDec = maxDec + "1";
1048 Convert.ToDouble(maxDec);
1051 catch (Exception e) {
1052 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#I24");
1056 Convert.ToDouble(ci, ci);
1059 catch (Exception e) {
1060 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#I25");
1064 Convert.ToDouble(tryStr, ci);
1067 catch (Exception e) {
1068 Assert.AreEqual (typeof(FormatException), e.GetType(), "#I26");
1072 string maxDec = double.MaxValue.ToString();
1073 maxDec = maxDec + "1";
1074 Convert.ToDouble(maxDec, ci);
1077 catch (Exception e) {
1078 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#I27");
1082 Convert.ToDouble(tryObj, ci);
1085 catch (Exception e) {
1086 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#I28");
1091 public void TestToInt16() {
1092 Assert.AreEqual ((short)0, Convert.ToInt16(boolFalse), "#J01");
1093 Assert.AreEqual ((short)1, Convert.ToInt16(boolTrue), "#J02");
1094 Assert.AreEqual ((short)97, Convert.ToInt16(tryChar), "#J03");
1095 Assert.AreEqual ((short)1234, Convert.ToInt16(tryDec), "#J04");
1096 Assert.AreEqual ((short)0, Convert.ToInt16(tryDbl), "#J05");
1097 Assert.AreEqual ((short)1234, Convert.ToInt16(tryInt16), "#J06");
1098 Assert.AreEqual ((short)12345, Convert.ToInt16(tryInt32), "#J07");
1099 Assert.AreEqual ((short)30000, Convert.ToInt16((long)30000), "#J08");
1100 Assert.AreEqual ((short)123, Convert.ToInt16(trySByte), "#J09");
1101 Assert.AreEqual ((short)1234, Convert.ToInt16(tryFloat), "#J10");
1102 Assert.AreEqual ((short)578, Convert.ToInt16("578"), "#J11");
1103 Assert.AreEqual ((short)15500, Convert.ToInt16((ushort)15500), "#J12");
1104 Assert.AreEqual ((short)5489, Convert.ToInt16((uint)5489), "#J13");
1105 Assert.AreEqual ((short)9876, Convert.ToInt16((ulong)9876), "#J14");
1106 Assert.AreEqual ((short)14, Convert.ToInt16("14", ci), "#J15");
1107 Assert.AreEqual ((short)11, Convert.ToInt16("01011", 2), "#J16");
1108 Assert.AreEqual ((short)1540, Convert.ToInt16("3004", 8), "#J17");
1109 Assert.AreEqual ((short)321, Convert.ToInt16("321", 10), "#J18");
1110 Assert.AreEqual ((short)2748, Convert.ToInt16("ABC", 16), "#J19");
1113 Convert.ToInt16(char.MaxValue);
1116 catch (Exception e) {
1117 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J25");
1121 Convert.ToInt16(tryDT);
1124 catch (Exception e) {
1125 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#J26");
1129 Convert.ToInt16((decimal)(short.MaxValue + 1));
1132 catch (Exception e) {
1133 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J27");
1137 Convert.ToInt16((decimal)(short.MinValue - 1));
1140 catch (Exception e) {
1141 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J28");
1145 Convert.ToInt16((double)(short.MaxValue + 1));
1148 catch (Exception e) {
1149 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J29");
1153 Convert.ToInt16((double)(short.MinValue - 1));
1156 catch (Exception e) {
1157 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J30");
1161 Convert.ToInt16(50000);
1164 catch (Exception e) {
1165 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J31");
1169 Convert.ToInt16(-50000);
1172 catch (Exception e) {
1173 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J32");
1177 Convert.ToInt16(tryInt64);
1180 catch (Exception e) {
1181 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J33");
1185 Convert.ToInt16(-tryInt64);
1188 catch (Exception e) {
1189 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J34");
1193 Convert.ToInt16(tryObj);
1196 catch (Exception e) {
1197 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#J35");
1201 Convert.ToInt16((float)32767.5);
1204 catch (Exception e) {
1205 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J36");
1209 Convert.ToInt16((float)-33000.54);
1212 catch (Exception e) {
1213 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J37");
1217 Convert.ToInt16(tryStr);
1220 catch (Exception e) {
1221 Assert.AreEqual (typeof(FormatException), e.GetType(), "#J38");
1225 Convert.ToInt16("-33000");
1228 catch (Exception e) {
1229 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J39");
1233 Convert.ToInt16(ushort.MaxValue);
1236 catch (Exception e) {
1237 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J40");
1241 Convert.ToInt16(uint.MaxValue);
1244 catch (Exception e) {
1245 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J41");
1249 Convert.ToInt16(ulong.MaxValue);
1252 catch (Exception e) {
1253 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J42");
1257 Convert.ToInt16(tryObj, ci);
1260 catch (Exception e) {
1261 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#J43");
1265 Convert.ToInt16(tryStr, ci);
1268 catch (Exception e) {
1269 Assert.AreEqual (typeof(FormatException), e.GetType(), "#J44");
1273 Convert.ToInt16("-33000", ci);
1276 catch (Exception e) {
1277 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J45");
1281 Convert.ToInt16("321", 11);
1284 catch (Exception e) {
1285 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#J46");
1289 Convert.ToInt16("D8BF1", 16);
1292 catch (Exception e) {
1293 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#J47");
1298 public void TestToInt32() {
1299 long tryMax = long.MaxValue;
1300 long tryMin = long.MinValue;
1301 Assert.AreEqual ((int)0, Convert.ToInt32(boolFalse), "#K01");
1302 Assert.AreEqual ((int)1, Convert.ToInt32(boolTrue), "#K02");
1303 Assert.AreEqual ((int)0, Convert.ToInt32(tryByte), "#K03");
1304 Assert.AreEqual ((int)97, Convert.ToInt32(tryChar), "#K04");
1305 Assert.AreEqual ((int)1234, Convert.ToInt32(tryDec), "#K05");
1306 Assert.AreEqual ((int)0, Convert.ToInt32(tryDbl), "#K06");
1307 Assert.AreEqual ((int)1234, Convert.ToInt32(tryInt16), "#K07");
1308 Assert.AreEqual ((int)12345, Convert.ToInt32(tryInt32), "#K08");
1309 Assert.AreEqual ((int)60000, Convert.ToInt32((long)60000), "#K09");
1310 Assert.AreEqual ((int)123, Convert.ToInt32(trySByte), "#K10");
1311 Assert.AreEqual ((int)1234, Convert.ToInt32(tryFloat), "#K11");
1312 Assert.AreEqual ((int)9876, Convert.ToInt32((string)"9876"), "#K12");
1313 Assert.AreEqual ((int)34567, Convert.ToInt32(tryUI16), "#K13");
1314 Assert.AreEqual ((int)567891234, Convert.ToInt32(tryUI32), "#K14");
1315 Assert.AreEqual ((int)0, Convert.ToInt32(tryUI64), "#K15");
1316 Assert.AreEqual ((int)123, Convert.ToInt32("123", ci), "#K16");
1317 Assert.AreEqual ((int)128, Convert.ToInt32("10000000", 2), "#K17");
1318 Assert.AreEqual ((int)302, Convert.ToInt32("456", 8), "#K18");
1319 Assert.AreEqual ((int)456, Convert.ToInt32("456", 10), "#K19");
1320 Assert.AreEqual ((int)1110, Convert.ToInt32("456", 16), "#K20");
1323 Convert.ToInt32(tryDT);
1326 catch (Exception e) {
1327 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#K25");
1331 Convert.ToInt32((decimal)tryMax);
1334 catch (Exception e) {
1335 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K26");
1339 Convert.ToInt32((decimal)tryMin);
1342 catch (Exception e) {
1343 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K27");
1347 Convert.ToInt32((double)tryMax);
1350 catch (Exception e) {
1351 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K28");
1355 Convert.ToInt32((double)tryMin);
1358 catch (Exception e) {
1359 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K29");
1363 Convert.ToInt32(tryInt64);
1366 catch (Exception e) {
1367 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K30");
1371 Convert.ToInt32(-tryInt64);
1374 catch (Exception e) {
1375 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K31");
1379 Convert.ToInt32(tryObj);
1382 catch (Exception e) {
1383 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#K32");
1387 Convert.ToInt32((float)tryMax);
1390 catch (Exception e) {
1391 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K33");
1395 Convert.ToInt32((float)tryMin);
1398 catch (Exception e) {
1399 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K34");
1403 Convert.ToInt32(tryStr, ci);
1406 catch (Exception e) {
1407 Assert.AreEqual (typeof(FormatException), e.GetType(), "#K35");
1411 Convert.ToInt32("-46565465123");
1414 catch (Exception e) {
1415 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K36");
1419 Convert.ToInt32("46565465123");
1422 catch (Exception e) {
1423 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K37");
1427 Convert.ToInt32((uint)tryMax);
1430 catch (Exception e) {
1431 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K38");
1435 Convert.ToInt32((ulong)tryMax);
1438 catch (Exception e) {
1439 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K39");
1443 Convert.ToInt32(tryObj, ci);
1446 catch (Exception e) {
1447 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#K40");
1451 Convert.ToInt32(tryStr, ci);
1454 catch (Exception e) {
1455 Assert.AreEqual (typeof(FormatException), e.GetType(), "#K41");
1459 Convert.ToInt32("-46565465123", ci);
1462 catch (Exception e) {
1463 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#K42");
1467 Convert.ToInt32("654", 9);
1470 catch (Exception e) {
1471 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#K43");
1476 public void TestToInt64() {
1477 decimal longMax = long.MaxValue;
1479 decimal longMin = long.MinValue;
1482 Assert.AreEqual ((long)0, Convert.ToInt64(boolFalse), "#L01");
1483 Assert.AreEqual ((long)1, Convert.ToInt64(boolTrue), "#L02");
1484 Assert.AreEqual ((long)97, Convert.ToInt64(tryChar), "#L03");
1485 Assert.AreEqual ((long)1234, Convert.ToInt64(tryDec), "#L04");
1486 Assert.AreEqual ((long)0, Convert.ToInt64(tryDbl), "#L05");
1487 Assert.AreEqual ((long)1234, Convert.ToInt64(tryInt16), "#L06");
1488 Assert.AreEqual ((long)12345, Convert.ToInt64(tryInt32), "#L07");
1489 Assert.AreEqual ((long)123456789012, Convert.ToInt64(tryInt64), "#L08");
1490 Assert.AreEqual ((long)123, Convert.ToInt64(trySByte), "#L09");
1491 Assert.AreEqual ((long)1234, Convert.ToInt64(tryFloat), "#L10");
1492 Assert.AreEqual ((long)564897, Convert.ToInt64("564897"), "#L11");
1493 Assert.AreEqual ((long)34567, Convert.ToInt64(tryUI16), "#L12");
1494 Assert.AreEqual ((long)567891234, Convert.ToInt64(tryUI32), "#L13");
1495 Assert.AreEqual ((long)0, Convert.ToInt64(tryUI64), "#L14");
1496 Assert.AreEqual ((long)-2548751, Convert.ToInt64("-2548751", ci), "#L15");
1497 Assert.AreEqual ((long)24987562, Convert.ToInt64("1011111010100011110101010", 2), "#L16");
1498 Assert.AreEqual ((long)-24578965, Convert.ToInt64("1777777777777642172153", 8), "#L17");
1499 Assert.AreEqual ((long)248759757, Convert.ToInt64("248759757", 10), "#L18");
1500 Assert.AreEqual ((long)256, Convert.ToInt64("100", 16), "#L19");
1503 Convert.ToInt64(tryDT);
1506 catch (Exception e) {
1507 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#L20");
1511 Convert.ToInt64((decimal)longMax + 1);
1514 catch (Exception e) {
1515 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L21");
1519 Convert.ToInt64((decimal)longMin);
1522 catch (Exception e) {
1523 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L24");
1527 Convert.ToInt64((double)longMax);
1530 catch (Exception e) {
1531 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L25:"+longMax);
1535 Convert.ToInt64((double)longMin);
1538 catch (Exception e) {
1539 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L26");
1543 Convert.ToInt64(new Exception());
1546 catch (Exception e) {
1547 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#L27");
1551 Convert.ToInt64(((float)longMax)*100);
1554 catch (Exception e) {
1555 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L28:"+longMax);
1559 Convert.ToInt64(((float)longMin)*100);
1562 catch (Exception e) {
1563 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L29");
1567 Convert.ToInt64("-567b3");
1570 catch (Exception e) {
1571 Assert.AreEqual (typeof(FormatException), e.GetType(), "#L30");
1575 Convert.ToInt64(longMax.ToString());
1578 catch (Exception e) {
1579 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L31:");
1583 Convert.ToInt64(ulong.MaxValue);
1586 catch (Exception e) {
1587 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L32");
1591 Convert.ToInt64(tryStr, ci);
1594 catch (Exception e) {
1595 Assert.AreEqual (typeof(FormatException), e.GetType(), "#L32b");
1599 Convert.ToInt64(longMin.ToString(), ci);
1602 catch (Exception e) {
1603 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#L33");
1607 Convert.ToInt64("321", 11);
1610 catch (Exception e) {
1611 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#L34");
1616 public void TestToSByte() {
1619 Assert.AreEqual ((sbyte)0, Convert.ToSByte(boolFalse), "#M01");
1621 Assert.AreEqual ((sbyte)1, Convert.ToSByte(boolTrue), "#M02");
1623 Assert.AreEqual ((sbyte)97, Convert.ToSByte(tryChar), "#M03");
1625 Assert.AreEqual ((sbyte)15, Convert.ToSByte((decimal)15), "#M04");
1627 Assert.AreEqual ((sbyte)0, Convert.ToSByte(tryDbl), "#M05");
1629 Assert.AreEqual ((sbyte)127, Convert.ToSByte((short)127), "#M06");
1631 Assert.AreEqual ((sbyte)-128, Convert.ToSByte((int)-128), "#M07");
1633 Assert.AreEqual ((sbyte)30, Convert.ToSByte((long)30), "#M08");
1635 Assert.AreEqual ((sbyte)123, Convert.ToSByte(trySByte), "#M09");
1637 Assert.AreEqual ((sbyte)12, Convert.ToSByte((float)12.46987f), "#M10");
1639 Assert.AreEqual ((sbyte)1, Convert.ToSByte("1"), "#M11");
1641 Assert.AreEqual ((sbyte)99, Convert.ToSByte((ushort)99), "#M12");
1643 Assert.AreEqual ((sbyte)54, Convert.ToSByte((uint)54), "#M13");
1645 Assert.AreEqual ((sbyte)127, Convert.ToSByte((ulong)127), "#M14");
1647 Assert.AreEqual ((sbyte)14, Convert.ToSByte("14", ci), "#M15");
1649 Assert.AreEqual ((sbyte)11, Convert.ToSByte("01011", 2), "#M16");
1651 Assert.AreEqual ((sbyte)5, Convert.ToSByte("5", 8), "#M17");
1653 Assert.AreEqual ((sbyte)100, Convert.ToSByte("100", 10), "#M18");
1655 Assert.AreEqual ((sbyte)-1, Convert.ToSByte("FF", 16), "#M19");
1656 } catch (Exception e) {
1657 Assert.Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1661 Convert.ToSByte((byte)200);
1664 catch (Exception e) {
1665 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M25");
1669 Convert.ToSByte((char)130);
1672 catch (Exception e) {
1673 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M26");
1677 Convert.ToSByte(tryDT);
1680 catch (Exception e) {
1681 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#M27");
1685 Convert.ToSByte((decimal)127.5m);
1688 catch (Exception e) {
1689 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M28");
1693 Convert.ToSByte((decimal)-200m);
1696 catch (Exception e) {
1697 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M29");
1701 Convert.ToSByte((double)150);
1704 catch (Exception e) {
1705 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M30");
1709 Convert.ToSByte((double)-128.6);
1712 catch (Exception e) {
1713 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M31");
1717 Convert.ToSByte((short)150);
1720 catch (Exception e) {
1721 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M32");
1725 Convert.ToSByte((short)-300);
1728 catch (Exception e) {
1729 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M33");
1733 Convert.ToSByte((int)1500);
1736 catch (Exception e) {
1737 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M34");
1741 Convert.ToSByte((int)-1286);
1744 catch (Exception e) {
1745 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M35");
1749 Convert.ToSByte((long)128);
1752 catch (Exception e) {
1753 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M36");
1757 Convert.ToSByte((long)-129);
1760 catch (Exception e) {
1761 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M37");
1765 Convert.ToSByte(new NumberFormatInfo());
1768 catch (Exception e) {
1769 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#M38");
1773 Convert.ToSByte((float)333);
1776 catch (Exception e) {
1777 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M39");
1781 Convert.ToSByte((float)-666);
1784 catch (Exception e) {
1785 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M40");
1789 Convert.ToSByte("B3");
1792 catch (Exception e) {
1793 Assert.AreEqual (typeof(FormatException), e.GetType(), "#M41");
1797 Convert.ToSByte("251");
1800 catch (Exception e) {
1801 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M42");
1805 Convert.ToSByte(ushort.MaxValue);
1808 catch (Exception e) {
1809 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M43");
1813 Convert.ToSByte((uint)600);
1816 catch (Exception e) {
1817 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M44");
1821 Convert.ToSByte(ulong.MaxValue);
1824 catch (Exception e) {
1825 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M45");
1829 Convert.ToSByte(ci, ci);
1832 catch (Exception e) {
1833 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#M46");
1837 Convert.ToSByte(tryStr, ci);
1840 catch (Exception e) {
1841 Assert.AreEqual (typeof(FormatException), e.GetType(), "#M47");
1845 Convert.ToSByte("325", ci);
1848 catch (Exception e) {
1849 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M48");
1853 Convert.ToSByte("5D", 15);
1856 catch (Exception e) {
1857 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#M49");
1861 Convert.ToSByte("111111111", 2);
1864 catch (Exception e) {
1865 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#M50");
1870 public void TestToSingle() {
1873 Assert.AreEqual ((float)0, Convert.ToSingle(boolFalse), "#N01");
1875 Assert.AreEqual ((float)1, Convert.ToSingle(boolTrue), "#N02");
1877 Assert.AreEqual ((float)0, Convert.ToSingle(tryByte), "#N03");
1879 Assert.AreEqual ((float)1234, 234, Convert.ToSingle(tryDec), "#N04");
1881 Assert.AreEqual ((float)0, Convert.ToSingle(tryDbl), "#N05");
1883 Assert.AreEqual ((float)1234, Convert.ToSingle(tryInt16), "#N06");
1885 Assert.AreEqual ((float)12345, Convert.ToSingle(tryInt32), "#N07");
1887 Assert.AreEqual ((float)123456789012, Convert.ToSingle(tryInt64), "#N08");
1889 Assert.AreEqual ((float)123, Convert.ToSingle(trySByte), "#N09");
1891 Assert.AreEqual ((float)1234, 2345, Convert.ToSingle(tryFloat), "#N10");
1893 Assert.AreEqual ((float)987, Convert.ToSingle("987"), "#N11");
1895 Assert.AreEqual ((float)34567, Convert.ToSingle(tryUI16), "#N12");
1897 Assert.AreEqual ((float)567891234, Convert.ToSingle(tryUI32), "#N13");
1899 Assert.AreEqual ((float)0, Convert.ToSingle(tryUI64), "#N14");
1901 Assert.AreEqual ((float)654.234, Convert.ToSingle("654.234", ci), "#N15");
1902 } catch (Exception e) {
1903 Assert.Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1907 Convert.ToSingle(tryChar);
1910 catch (Exception e) {
1911 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#N25");
1915 Convert.ToSingle(tryDT);
1918 catch (Exception e) {
1919 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#N26");
1923 Convert.ToSingle(tryObj);
1926 catch (Exception e) {
1927 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#N27");
1931 Convert.ToSingle("A345H");
1934 catch (Exception e) {
1935 Assert.AreEqual (typeof(FormatException), e.GetType(), "#N28");
1939 Convert.ToSingle(double.MaxValue.ToString());
1942 catch (Exception e) {
1943 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#N29");
1947 Convert.ToSingle(tryObj, ci);
1950 catch (Exception e) {
1951 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#N30");
1955 Convert.ToSingle("J345K", ci);
1958 catch (Exception e) {
1959 Assert.AreEqual (typeof(FormatException), e.GetType(), "#N31");
1963 Convert.ToSingle("11000000000000000000000000000000000000000000000", ci);
1966 catch (Exception e) {
1967 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#N32");
1972 public void TestToString() {
1975 Assert.AreEqual ("False", Convert.ToString(boolFalse), "#O01");
1976 Assert.AreEqual ("True", Convert.ToString(boolTrue), "#O02");
1977 Assert.AreEqual ("123", Convert.ToString(tryByte), "#O03");
1978 Assert.AreEqual ("a", Convert.ToString(tryChar), "#O04");
1979 Assert.AreEqual (tryDT.ToString(), Convert.ToString(tryDT), "#O05");
1980 Assert.AreEqual (tryDec.ToString(), Convert.ToString(tryDec), "#O06");
1981 Assert.AreEqual (tryDbl.ToString(), Convert.ToString(tryDbl), "#O07");
1982 Assert.AreEqual ("1234", Convert.ToString(tryInt16), "#O08");
1983 Assert.AreEqual ("12345", Convert.ToString(tryInt32), "#O09");
1984 Assert.AreEqual ("123456789012", Convert.ToString(tryInt64), "#O10");
1985 Assert.AreEqual ("123", Convert.ToString(trySByte), "#O11");
1986 Assert.AreEqual (tryFloat.ToString(), Convert.ToString(tryFloat), "#O12");
1987 Assert.AreEqual ("foobar", Convert.ToString(tryStr), "#O13");
1988 Assert.AreEqual ("34567", Convert.ToString(tryUI16), "#O14");
1989 Assert.AreEqual ("567891234", Convert.ToString(tryUI32), "#O15");
1990 Assert.AreEqual ("True", Convert.ToString(boolTrue, ci), "#O16");
1991 Assert.AreEqual ("False", Convert.ToString(boolFalse, ci), "#O17");
1992 Assert.AreEqual ("123", Convert.ToString(tryByte, ci), "#O18");
1993 Assert.AreEqual ("1111011", Convert.ToString(tryByte, 2), "#O19");
1994 Assert.AreEqual ("173", Convert.ToString(tryByte, 8), "#O20");
1995 Assert.AreEqual ("123", Convert.ToString(tryByte, 10), "#O21");
1996 Assert.AreEqual ("7b", Convert.ToString(tryByte, 16), "#O22");
1997 Assert.AreEqual ("a", Convert.ToString(tryChar, ci), "#O23");
1998 Assert.AreEqual (tryDT.ToString(ci), Convert.ToString(tryDT, ci), "#O24");
1999 Assert.AreEqual (tryDec.ToString(ci), Convert.ToString(tryDec, ci), "#O25");
2000 Assert.AreEqual (tryDbl.ToString(ci), Convert.ToString(tryDbl, ci), "#O26");
2001 Assert.AreEqual ("1234", Convert.ToString(tryInt16, ci), "#O27");
2002 Assert.AreEqual ("10011010010", Convert.ToString(tryInt16, 2), "#O28");
2003 Assert.AreEqual ("2322", Convert.ToString(tryInt16, 8), "#O29");
2004 Assert.AreEqual ("1234", Convert.ToString(tryInt16, 10), "#O30");
2005 Assert.AreEqual ("4d2", Convert.ToString(tryInt16, 16), "#O31");
2006 Assert.AreEqual ("12345", Convert.ToString(tryInt32, ci), "#O32");
2007 Assert.AreEqual ("11000000111001", Convert.ToString(tryInt32, 2), "#O33");
2008 Assert.AreEqual ("30071", Convert.ToString(tryInt32, 8), "#O34");
2009 Assert.AreEqual ("12345", Convert.ToString(tryInt32, 10), "#O35");
2010 Assert.AreEqual ("3039", Convert.ToString(tryInt32, 16), "#O36");
2011 Assert.AreEqual ("123456789012", Convert.ToString(tryInt64, ci), "#O37");
2012 Assert.AreEqual ("1110010111110100110010001101000010100", Convert.ToString(tryInt64, 2), "#O38");
2013 Assert.AreEqual ("1627646215024", Convert.ToString(tryInt64, 8), "#O39");
2014 Assert.AreEqual ("123456789012", Convert.ToString(tryInt64, 10), "#O40");
2015 Assert.AreEqual ("1cbe991a14", Convert.ToString(tryInt64, 16), "#O41");
2016 Assert.AreEqual ("123", Convert.ToString((trySByte), ci), "#O42");
2017 Assert.AreEqual (tryFloat.ToString(ci), Convert.ToString((tryFloat), ci), "#O43");
2018 Assert.AreEqual ("foobar", Convert.ToString((tryStr), ci), "#O44");
2019 Assert.AreEqual ("34567", Convert.ToString((tryUI16), ci), "#O45");
2020 Assert.AreEqual ("567891234", Convert.ToString((tryUI32), ci), "#O46");
2021 Assert.AreEqual ("0", Convert.ToString(tryUI64), "#O47");
2022 Assert.AreEqual ("0", Convert.ToString((tryUI64), ci), "#O48");
2025 Convert.ToString(tryInt16, 5);
2028 catch (Exception e) {
2029 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#O55");
2033 Convert.ToString(tryInt32, 17);
2036 catch (Exception e) {
2037 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#O56");
2041 Convert.ToString(tryInt64, 1);
2044 catch (Exception e) {
2045 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#O57");
2050 public void TestToUInt16() {
2051 Assert.AreEqual ((ushort)0, Convert.ToUInt16(boolFalse), "#P01");
2052 Assert.AreEqual ((ushort)1, Convert.ToUInt16(boolTrue), "#P02");
2053 Assert.AreEqual ((ushort)0, Convert.ToUInt16(tryByte), "#P03");
2054 Assert.AreEqual ((ushort)97, Convert.ToUInt16(tryChar), "#P04");
2055 Assert.AreEqual ((ushort)1234, Convert.ToUInt16(tryDec), "#P05");
2056 Assert.AreEqual ((ushort)0, Convert.ToUInt16(tryDbl), "#P06");
2057 Assert.AreEqual ((ushort)1234, Convert.ToUInt16(tryInt16), "#P07");
2058 Assert.AreEqual ((ushort)12345, Convert.ToUInt16(tryInt32), "#P08");
2059 Assert.AreEqual ((ushort)43752, Convert.ToUInt16((long)43752), "#P09");
2060 Assert.AreEqual ((ushort)123, Convert.ToUInt16(trySByte), "#P10");
2061 Assert.AreEqual ((ushort)1234, Convert.ToUInt16(tryFloat), "#P11");
2062 Assert.AreEqual ((ushort)123, Convert.ToUInt16((string)"123"), "#P12");
2063 Assert.AreEqual ((ushort)34567, Convert.ToUInt16(tryUI16), "#P13");
2064 Assert.AreEqual ((ushort)56789, Convert.ToUInt16((uint)56789), "#P14");
2065 Assert.AreEqual ((ushort)0, Convert.ToUInt16(tryUI64), "#P15");
2066 Assert.AreEqual ((ushort)31, Convert.ToUInt16("31", ci), "#P16");
2067 Assert.AreEqual ((ushort)14, Convert.ToUInt16("1110", 2), "#P17");
2068 Assert.AreEqual ((ushort)32, Convert.ToUInt16("40", 8), "#P18");
2069 Assert.AreEqual ((ushort)40, Convert.ToUInt16("40", 10), "#P19");
2070 Assert.AreEqual ((ushort)64, Convert.ToUInt16("40", 16), "#P20");
2074 Convert.ToUInt16(tryDT);
2077 catch (Exception e) {
2078 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#P25");
2082 Convert.ToUInt16(decimal.MaxValue);
2085 catch (Exception e) {
2086 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P26");
2090 Convert.ToUInt16(decimal.MinValue);
2093 catch (Exception e) {
2094 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P27");
2098 Convert.ToUInt16(double.MaxValue);
2101 catch (Exception e) {
2102 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P28");
2106 Convert.ToUInt16(double.MinValue);
2109 catch (Exception e) {
2110 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P29");
2114 Convert.ToUInt16(short.MinValue);
2117 catch (Exception e) {
2118 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P30");
2122 Convert.ToUInt16(int.MaxValue);
2125 catch (Exception e) {
2126 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P31");
2130 Convert.ToUInt16(int.MinValue);
2133 catch (Exception e) {
2134 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P32");
2138 Convert.ToUInt16(long.MaxValue);
2141 catch (Exception e) {
2142 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P33");
2146 Convert.ToUInt16(long.MinValue);
2149 catch (Exception e) {
2150 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P34");
2154 Convert.ToUInt16(tryObj);
2157 catch (Exception e) {
2158 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#P35");
2162 Convert.ToUInt16(sbyte.MinValue);
2165 catch (Exception e) {
2166 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P36");
2170 Convert.ToUInt16(float.MaxValue);
2173 catch (Exception e) {
2174 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P37");
2178 Convert.ToUInt16(float.MinValue);
2181 catch (Exception e) {
2182 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P38");
2186 Convert.ToUInt16("1A2");
2189 catch (Exception e) {
2190 Assert.AreEqual (typeof(FormatException), e.GetType(), "#P39");
2194 Convert.ToUInt16("-32800");
2197 catch (Exception e) {
2198 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P40");
2202 Convert.ToUInt16(int.MaxValue.ToString());
2205 catch (Exception e) {
2206 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P41");
2210 Convert.ToUInt16(ulong.MaxValue);
2213 catch (Exception e) {
2214 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P42");
2218 Convert.ToUInt16("1A2", ci);
2221 catch (Exception e) {
2222 Assert.AreEqual (typeof(FormatException), e.GetType(), "#P43");
2226 Convert.ToUInt16("-32800", ci);
2229 catch (Exception e) {
2230 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P44");
2234 Convert.ToUInt16("456987", ci);
2237 catch (Exception e) {
2238 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#P45");
2242 Convert.ToUInt16("40", 9);
2245 catch (Exception e) {
2246 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#P46");
2250 Convert.ToUInt16 ("abcde", 16);
2253 catch (Exception e) {
2254 Assert.AreEqual (typeof (OverflowException), e.GetType (), "#P47");
2259 public void TestSignedToInt() {
2260 // String cannot contain a minus sign if the base is not 10.
2261 // But can if it is ten, and + is allowed everywhere.
2262 Assert.AreEqual (-1, Convert.ToInt32 ("-1", 10), "Signed0");
2263 Assert.AreEqual (1, Convert.ToInt32 ("+1", 10), "Signed1");
2264 Assert.AreEqual (1, Convert.ToInt32 ("+1", 2), "Signed2");
2265 Assert.AreEqual (1, Convert.ToInt32 ("+1", 8), "Signed3");
2266 Assert.AreEqual (1, Convert.ToInt32 ("+1", 16), "Signed4");
2269 Convert.ToInt32("-1", 2);
2275 Convert.ToInt32("-1", 8);
2281 Convert.ToInt32("-1", 16);
2291 public void TestToUInt32() {
2292 Assert.AreEqual ((uint)1, Convert.ToUInt32(boolTrue), "#Q01");
2293 Assert.AreEqual ((uint)0, Convert.ToUInt32(boolFalse), "#Q02");
2294 Assert.AreEqual ((uint)0, Convert.ToUInt32(tryByte), "#Q03");
2295 Assert.AreEqual ((uint)97, Convert.ToUInt32(tryChar), "#Q04");
2296 Assert.AreEqual ((uint)1234, Convert.ToUInt32(tryDec), "#Q05");
2297 Assert.AreEqual ((uint)0, Convert.ToUInt32(tryDbl), "#Q06");
2298 Assert.AreEqual ((uint)1234, Convert.ToUInt32(tryInt16), "#Q07");
2299 Assert.AreEqual ((uint)12345, Convert.ToUInt32(tryInt32), "#Q08");
2300 Assert.AreEqual ((uint)1234567890, Convert.ToUInt32((long)1234567890), "#Q09");
2301 Assert.AreEqual ((uint)123, Convert.ToUInt32(trySByte), "#Q10");
2302 Assert.AreEqual ((uint)1234, Convert.ToUInt32(tryFloat), "#Q11");
2303 Assert.AreEqual ((uint)3456789, Convert.ToUInt32("3456789"), "#Q12");
2304 Assert.AreEqual ((uint)34567, Convert.ToUInt32(tryUI16), "#Q13");
2305 Assert.AreEqual ((uint)567891234, Convert.ToUInt32(tryUI32), "#Q14");
2306 Assert.AreEqual ((uint)0, Convert.ToUInt32(tryUI64), "#Q15");
2307 Assert.AreEqual ((uint)415, Convert.ToUInt32("110011111", 2), "#Q16");
2308 Assert.AreEqual ((uint)156, Convert.ToUInt32("234", 8), "#Q17");
2309 Assert.AreEqual ((uint)234, Convert.ToUInt32("234", 10), "#Q18");
2310 Assert.AreEqual ((uint)564, Convert.ToUInt32("234", 16), "#Q19");
2314 Convert.ToUInt32(tryDT);
2317 catch (Exception e) {
2318 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#Q25");
2322 Convert.ToUInt32(decimal.MaxValue);
2325 catch (Exception e) {
2326 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q26");
2330 Convert.ToUInt32((decimal)-150);
2333 catch (Exception e) {
2334 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q27");
2338 Convert.ToUInt32(double.MaxValue);
2341 catch (Exception e) {
2342 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q28");
2346 Convert.ToUInt32((double)-1);
2349 catch (Exception e) {
2350 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q29");
2354 Convert.ToUInt32(short.MinValue);
2357 catch (Exception e) {
2358 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q30");
2362 Convert.ToUInt32(int.MinValue);
2365 catch (Exception e) {
2366 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q31");
2370 Convert.ToUInt32(long.MaxValue);
2373 catch (Exception e) {
2374 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q32");
2378 Convert.ToUInt32((long)-50000);
2381 catch (Exception e) {
2382 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q33");
2386 Convert.ToUInt32(new Exception());
2389 catch (Exception e) {
2390 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#Q34");
2394 Convert.ToUInt32(sbyte.MinValue);
2397 catch (Exception e) {
2398 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q35");
2402 Convert.ToUInt32(float.MaxValue);
2405 catch (Exception e) {
2406 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q36");
2410 Convert.ToUInt32(float.MinValue);
2413 catch (Exception e) {
2414 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q37");
2418 Convert.ToUInt32("45t54");
2421 catch (Exception e) {
2422 Assert.AreEqual (typeof(FormatException), e.GetType(), "#Q38");
2426 Convert.ToUInt32("-55");
2429 catch (Exception e) {
2430 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q39");
2434 Convert.ToUInt32(ulong.MaxValue);
2437 catch (Exception e) {
2438 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q40");
2442 Convert.ToUInt32(new Exception(), ci);
2445 catch (Exception e) {
2446 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#Q41");
2450 Convert.ToUInt32(tryStr, ci);
2453 catch (Exception e) {
2454 Assert.AreEqual (typeof(FormatException), e.GetType(), "#Q42");
2458 Convert.ToUInt32("-50", ci);
2461 catch (Exception e) {
2462 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q43");
2466 Convert.ToUInt32(decimal.MaxValue.ToString(), ci);
2469 catch (Exception e) {
2470 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#Q44");
2474 Convert.ToUInt32("1001110", 1);
2477 catch (Exception e) {
2478 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#Q45");
2483 public void TestToUInt64()
2487 Assert.AreEqual ((ulong)1, Convert.ToUInt64(boolTrue), "#R01");
2489 Assert.AreEqual ((ulong)0, Convert.ToUInt64(boolFalse), "#R02");
2491 Assert.AreEqual ((ulong)0, Convert.ToUInt64(tryByte), "#R03");
2493 Assert.AreEqual ((ulong)97, Convert.ToUInt64(tryChar), "#R04");
2495 Assert.AreEqual ((ulong)1234, Convert.ToUInt64(tryDec), "#R05");
2497 Assert.AreEqual ((ulong)0, Convert.ToUInt64(tryDbl), "#R06");
2499 Assert.AreEqual ((ulong)1234, Convert.ToUInt64(tryInt16), "#R07");
2501 Assert.AreEqual ((ulong)12345, Convert.ToUInt64(tryInt32), "#R08");
2503 Assert.AreEqual ((ulong)123456789012, Convert.ToUInt64(tryInt64), "#R09");
2505 Assert.AreEqual ((ulong)123, Convert.ToUInt64(trySByte), "#R10");
2507 Assert.AreEqual ((ulong)1234, Convert.ToUInt64(tryFloat), "#R11");
2509 Assert.AreEqual ((ulong)345678, Convert.ToUInt64("345678"), "#R12");
2511 Assert.AreEqual ((ulong)34567, Convert.ToUInt64(tryUI16), "#R13");
2513 Assert.AreEqual ((ulong)567891234, Convert.ToUInt64(tryUI32), "#R14");
2515 Assert.AreEqual ((ulong)0, Convert.ToUInt64(tryUI64), "#R15");
2517 Assert.AreEqual ((ulong)123, Convert.ToUInt64("123", ci), "#R16");
2519 Assert.AreEqual ((ulong)4, Convert.ToUInt64("100", 2), "#R17");
2521 Assert.AreEqual ((ulong)64, Convert.ToUInt64("100", 8), "#R18");
2523 Assert.AreEqual ((ulong)100, Convert.ToUInt64("100", 10), "#R19");
2525 Assert.AreEqual ((ulong)256, Convert.ToUInt64("100", 16), "#R20");
2526 } catch (Exception e) {
2527 Assert.Fail ("Unexpected exception caught when iTest = " + iTest + ": e = " + e);
2531 Convert.ToUInt64(tryDT);
2534 catch (Exception e) {
2535 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#R25");
2539 Convert.ToUInt64(decimal.MaxValue);
2542 catch (Exception e) {
2543 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R26");
2547 Convert.ToUInt64((decimal)-140);
2550 catch (Exception e) {
2551 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R27");
2555 Convert.ToUInt64(double.MaxValue);
2558 catch (Exception e) {
2559 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R28");
2563 Convert.ToUInt64((double)-1);
2566 catch (Exception e) {
2567 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R29");
2571 Convert.ToUInt64(short.MinValue);
2574 catch (Exception e) {
2575 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R30");
2579 Convert.ToUInt64(int.MinValue);
2582 catch (Exception e) {
2583 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R31");
2587 Convert.ToUInt64(long.MinValue);
2590 catch (Exception e) {
2591 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R32");
2595 Convert.ToUInt64(tryObj);
2598 catch (Exception e) {
2599 Assert.AreEqual (typeof(InvalidCastException), e.GetType(), "#R33");
2603 Convert.ToUInt64(sbyte.MinValue);
2606 catch (Exception e) {
2607 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R34");
2611 Convert.ToUInt64(float.MinValue);
2614 catch (Exception e) {
2615 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R35");
2619 Convert.ToUInt64(float.MaxValue);
2622 catch (Exception e) {
2623 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R36");
2627 Convert.ToUInt64("234rt78");
2630 catch (Exception e) {
2631 Assert.AreEqual (typeof(FormatException), e.GetType(), "#R37");
2635 Convert.ToUInt64("-68");
2638 catch (Exception e) {
2639 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R38");
2643 Convert.ToUInt64(decimal.MaxValue.ToString());
2646 catch (Exception e) {
2647 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R39");
2651 Convert.ToUInt64("23rd2", ci);
2654 catch (Exception e) {
2655 Assert.AreEqual (typeof(FormatException), e.GetType(), "#R40");
2659 Convert.ToUInt64(decimal.MinValue.ToString(), ci);
2662 catch (Exception e) {
2663 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R41");
2667 Convert.ToUInt64(decimal.MaxValue.ToString(), ci);
2670 catch (Exception e) {
2671 Assert.AreEqual (typeof(OverflowException), e.GetType(), "#R42");
2675 Convert.ToUInt64("132", 9);
2678 catch (Exception e) {
2679 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "#R43");
2683 Assert.AreEqual ((ulong) 256, Convert.ToUInt64 ("0x100", 16), "#L35");
2684 Assert.AreEqual ((ulong) 256, Convert.ToUInt64 ("0X100", 16), "#L36");
2685 Assert.AreEqual (ulong.MaxValue, Convert.ToUInt64 ("0xFFFFFFFFFFFFFFFF", 16), "#L37");
2689 [ExpectedException (typeof (FormatException))]
2690 public void TestInvalidBase64() {
2691 // This has to be a multiple of 4 characters, otherwise you
2692 // are testing something else. Ideally one will become a byte
2695 // This test is designed to see what happens with invalid bytes
2696 string brokenB64 = "AB~\u00a3";
2697 Convert.FromBase64String(brokenB64);
2701 [ExpectedException (typeof (FormatException))]
2702 public void TestInvalidBase64_Bug5464 ()
2704 Convert.FromBase64String ("dGVzdA==DQo=");
2708 public void TestValidBase64_Bug5464 ()
2710 byte[] result = Convert.FromBase64String ("dGVzdA==");
2711 Assert.AreEqual(4, result.Length, "Array.Length expected to be 4.");
2712 Assert.AreEqual(116, result[0], "#A01");
2713 Assert.AreEqual(101, result[1], "#A02");
2714 Assert.AreEqual(115, result[2], "#A03");
2715 Assert.AreEqual(116, result[3], "#A04");
2719 [ExpectedException (typeof (FormatException))]
2720 public void TestInvalidBase64_TooManyPaddings ()
2722 Convert.FromBase64String ("dGVzd===");
2726 public void TestBeginWithSpaces ()
2728 byte[] bb = new byte[] { 1, 2, 3};
2729 string s = Convert.ToBase64String (bb);
2730 byte [] b2 = Convert.FromBase64String (" " + s);
2731 Assert.AreEqual (3, b2.Length, "#01");
2732 for (int i = 0; i < 3; i++)
2733 Assert.AreEqual (bb [i], b2 [i], "#0" + (i + 2));
2737 public void TestToBase64CharArray ()
2739 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2741 char[] expectedCharArr = {'I', 'X', '/', '/', 'b', 'a', 'o', '2'};
2742 char[] result = new Char[8];
2744 Convert.ToBase64CharArray(byteArr, 0, byteArr.Length, result, 0);
2746 for (int i = 0; i < expectedCharArr.Length; i++) {
2747 Assert.AreEqual (expectedCharArr[i], result[i], "#S0" + i);
2752 [ExpectedException (typeof(ArgumentNullException))]
2753 public void ToBase64CharArray_InNull ()
2755 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2756 char[] result = new Char[8];
2757 Convert.ToBase64CharArray (null, 0, byteArr.Length, result, 0);
2761 [ExpectedException (typeof(ArgumentNullException))]
2762 public void ToBase64CharArray_OutNull ()
2764 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2765 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, null, 0);
2769 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2770 public void ToBase64CharArray_OffsetInNegative ()
2772 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2773 char[] result = new Char[8];
2774 Convert.ToBase64CharArray (byteArr, -1, byteArr.Length, result, 0);
2778 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2779 public void ToBase64CharArray_LengthNegative ()
2781 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2782 char[] result = new Char[8];
2783 Convert.ToBase64CharArray (byteArr, 0, -5, result, 0);
2787 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2788 public void ToBase64CharArray_OffsetOutNegative ()
2790 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2791 char[] result = new Char[8];
2792 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, -2);
2796 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2797 public void ToBase64CharArray_TotalIn ()
2799 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2800 char[] result = new Char[8];
2801 Convert.ToBase64CharArray (byteArr, 4, byteArr.Length, result, 0);
2805 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2806 public void ToBase64CharArray_TotalInOverflow ()
2808 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2809 char[] result = new Char[8];
2810 Convert.ToBase64CharArray (byteArr, Int32.MaxValue, byteArr.Length, result, 0);
2814 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2815 public void ToBase64CharArray_TotalOut ()
2817 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2818 char[] result = new Char[8];
2819 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, 2);
2823 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2824 public void ToBase64CharArray_TotalOutOverflow ()
2826 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2827 char[] result = new Char[8];
2828 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, Int32.MaxValue);
2832 public void TestToBase64String() {
2833 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2834 string expectedStr = "IX//bao2";
2838 result1 = Convert.ToBase64String(byteArr);
2839 result2 = Convert.ToBase64String(byteArr, 0, byteArr.Length);
2841 Assert.AreEqual (expectedStr, result1, "#T01");
2842 Assert.AreEqual (expectedStr, result2, "#T02");
2845 Convert.ToBase64String(null);
2848 catch (Exception e) {
2849 Assert.AreEqual (typeof(ArgumentNullException), e.GetType(), "#T05");
2853 Convert.ToBase64String(byteArr, -1, byteArr.Length);
2856 catch (Exception e) {
2857 Assert.AreEqual (typeof(ArgumentOutOfRangeException), e.GetType(), "#T06");
2861 Convert.ToBase64String(byteArr, 0, -10);
2864 catch (Exception e) {
2865 Assert.AreEqual (typeof(ArgumentOutOfRangeException), e.GetType(), "#T07");
2869 Convert.ToBase64String(byteArr, 4, byteArr.Length);
2872 catch (Exception e) {
2873 Assert.AreEqual (typeof(ArgumentOutOfRangeException), e.GetType(), "#T08");
2878 public void ToBase64String_Bug76876 ()
2880 byte[] bs = Convert.FromBase64String ("ZuBZ7PESb3VRXgrl/KSRJd/hTGBvaEvEplqH3izPomDv5nBjS9MzcD1h8tOWzS7/wYGnaip8\nbhBfCrpWxivi8G7R08oBcADIiclpZeqRxai9kG4WoBUzJ6MCbxuvb1k757q+D9nqoL0p9Rer\n+5+vNodYkHYwqnKKyMBSQ11sspw=\n");
2881 string s = Convert.ToBase64String (bs, Base64FormattingOptions.None);
2882 Assert.IsTrue (!s.Contains ("\n"), "no new line");
2885 static string ToBase64 (int len, Base64FormattingOptions options)
2887 return Convert.ToBase64String (new byte [len], options);
2891 public void Base64String_LineEnds_InsertLineBreaks ()
2893 string base64 = ToBase64 (0, Base64FormattingOptions.InsertLineBreaks);
2894 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "0-le");
2895 Assert.AreEqual (String.Empty, base64, "0");
2897 base64 = ToBase64 (1, Base64FormattingOptions.InsertLineBreaks);
2898 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "1-le");
2899 Assert.AreEqual ("AA==", base64, "1");
2901 base64 = ToBase64 (57, Base64FormattingOptions.InsertLineBreaks);
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.InsertLineBreaks);
2907 Assert.IsTrue (base64.Contains (Environment.NewLine), "58-nl"); // two lines
2908 Assert.IsTrue (base64.EndsWith ("AA=="), "58-le"); // no NewLine
2912 public void Base64String_LineEnds_None ()
2914 string base64 = ToBase64 (0, Base64FormattingOptions.None);
2915 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "0-le");
2916 Assert.AreEqual (String.Empty, base64, "0");
2918 base64 = ToBase64 (1, Base64FormattingOptions.None);
2919 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "1-le");
2920 Assert.AreEqual ("AA==", base64, "1");
2922 base64 = ToBase64 (57, Base64FormattingOptions.None);
2923 Assert.IsFalse (base64.Contains (Environment.NewLine), "57-nl"); // one lines
2924 Assert.IsFalse (base64.EndsWith (Environment.NewLine), "57-le");
2925 Assert.AreEqual ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", base64, "55");
2927 base64 = ToBase64 (58, Base64FormattingOptions.None);
2928 Assert.IsFalse (base64.Contains (Environment.NewLine), "58-nl"); // one lines
2929 Assert.IsTrue (base64.EndsWith ("AA=="), "58-le"); // no NewLine
2932 /* Have experienced some problems with FromBase64CharArray using mono. Something
2933 * about error in a unicode file.
2935 * However the test seems to run fine using mono in a cygwin environment
2939 [ExpectedException (typeof (ArgumentNullException))]
2940 public void FromBase64CharArray_Null ()
2942 Convert.FromBase64CharArray (null, 1, 5);
2946 public void FromBase64CharArray_Empty ()
2948 Assert.AreEqual (new byte [0], Convert.FromBase64CharArray (new char[0], 0, 0));
2952 public void FormatBase64CharArray_OnlyWhitespace ()
2954 Assert.AreEqual (new byte [0], Convert.FromBase64CharArray (new char[3] {' ',
2955 '\r', '\t'}, 0, 3));
2959 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2960 public void FromBase64CharArray_OutOfRangeStart ()
2962 Convert.FromBase64CharArray (new char [4], -1, 4);
2966 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2967 public void FromBase64CharArray_OutOfRangeLength ()
2969 Convert.FromBase64CharArray (new char [4], 2, 4);
2973 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2974 public void FromBase64CharArray_Overflow ()
2976 Convert.FromBase64CharArray (new char [4], Int32.MaxValue, 4);
2980 [ExpectedException (typeof (FormatException))]
2981 public void FromBase64CharArray_InvalidLength ()
2983 Convert.FromBase64CharArray (new char [4], 0, 3);
2987 [ExpectedException (typeof (FormatException))]
2988 public void FromBase64CharArray_WideChar ()
2990 char[] c = new char [4] { 'A', 'A', 'A', (char) Char.MaxValue };
2991 Convert.FromBase64CharArray (c, 0, 4);
2995 public void FromBase64CharArray ()
2997 char[] charArr = {'M','o','n','o','m','o','n','o'};
2998 byte[] expectedByteArr = {50, 137, 232, 154, 137, 232};
3000 byte[] fromCharArr = Convert.FromBase64CharArray(charArr, 0, 8);
3002 for (int i = 0; i < fromCharArr.Length; i++){
3003 Assert.AreEqual (expectedByteArr[i], fromCharArr[i], "#U0" + i);
3007 /* Have experienced some problems with FromBase64String using mono. Something about
3008 * error in a unicode file.
3010 * However the test seems to run fine using mono in a cygwin environment
3014 [ExpectedException (typeof (ArgumentNullException))]
3015 public void FromBase64String_Null ()
3017 Convert.FromBase64String (null);
3021 [ExpectedException (typeof (FormatException))]
3022 public void FromBase64String_InvalidLength ()
3024 Convert.FromBase64String ("foo");
3028 [ExpectedException (typeof (FormatException))]
3029 public void FromBase64String_InvalidLength2 ()
3031 Convert.FromBase64String (tryStr);
3035 public void FromBase64String_InvalidLengthBecauseOfIgnoredChars ()
3037 byte[] result = Convert.FromBase64String ("AAAA\t");
3038 Assert.AreEqual (3, result.Length, "InvalidLengthBecauseOfIgnoredChars");
3041 private const string ignored = "\t\r\n ";
3042 private const string base64data = "AAAAAAAAAAAAAAAAAAAA"; // 15 bytes 0x00
3045 public void FromBase64_IgnoreCharsBefore ()
3047 string s = ignored + base64data;
3048 byte[] data = Convert.FromBase64String (s);
3049 Assert.AreEqual (15, data.Length, "String-IgnoreCharsBefore-Ignored");
3051 char[] c = s.ToCharArray ();
3052 data = Convert.FromBase64CharArray (c, 0, c.Length);
3053 Assert.AreEqual (15, data.Length, "CharArray-IgnoreCharsBefore-Ignored");
3057 public void FromBase64_IgnoreCharsInside ()
3059 string s = base64data + ignored + base64data;
3060 byte[] data = Convert.FromBase64String (s);
3061 Assert.AreEqual (30, data.Length, "String-IgnoreCharsInside-Ignored");
3063 char[] c = s.ToCharArray ();
3064 data = Convert.FromBase64CharArray (c, 0, c.Length);
3065 Assert.AreEqual (30, data.Length, "CharArray-IgnoreCharsInside-Ignored");
3069 public void FromBase64_IgnoreCharsAfter ()
3071 string s = base64data + ignored;
3072 byte[] data = Convert.FromBase64String (s);
3073 Assert.AreEqual (15, data.Length, "String-IgnoreCharsAfter-Ignored");
3075 char[] c = s.ToCharArray ();
3076 data = Convert.FromBase64CharArray (c, 0, c.Length);
3077 Assert.AreEqual (15, data.Length, "CharArray-IgnoreCharsAfter-Ignored");
3081 public void FromBase64_Empty ()
3083 Assert.AreEqual (new byte[0], Convert.FromBase64String (string.Empty));
3087 public void FromBase64_OnlyWhiteSpace ()
3089 Assert.AreEqual (new byte[0], Convert.FromBase64String (" \r\t"));
3093 [ExpectedException (typeof (FormatException))]
3094 public void FromBase64_InvalidChar ()
3096 Convert.FromBase64String ("amVsb3U=\u0100");
3100 [ExpectedException (typeof (FormatException))]
3101 public void FromBase64_Min ()
3103 Convert.FromBase64String ("amVsb3U= \r \n\u007B");
3107 public void FromBase64_TrailingEqualAndSpaces () // From bug #75840.
3109 string base64 = "\n fdy6S2NLpnT4fMdokUHSHsmpcvo= ";
3110 byte [] bytes = Convert.FromBase64String (base64);
3111 Assert.AreEqual (20, bytes.Length, "#01");
3112 byte [] target = new byte [] { 0x7D, 0xDC, 0xBA, 0x4B, 0x63, 0x4B, 0xA6, 0x74, 0xF8, 0x7C, 0xC7,
3113 0x68, 0x91, 0x41, 0xD2, 0x1E, 0xC9, 0xA9, 0x72, 0xFA };
3115 for (int i = 0; i < 20; i++) {
3116 if (bytes [i] != target [i])
3117 Assert.Fail ("Item #" + i);
3122 public void TestConvertFromNull() {
3124 Assert.AreEqual (false, Convert.ToBoolean (null as object), "#W1");
3125 Assert.AreEqual (0, Convert.ToByte (null as object), "#W2");
3126 Assert.AreEqual (0, Convert.ToChar (null as object), "#W3");
3127 Assert.AreEqual (new DateTime (1, 1, 1, 0, 0, 0), Convert.ToDateTime (null as object), "#W4");
3128 Assert.AreEqual (0, Convert.ToDecimal (null as object), "#W5");
3129 Assert.AreEqual (0, Convert.ToDouble (null as object), "#W6");
3130 Assert.AreEqual (0, Convert.ToInt16 (null as object), "#W7");
3131 Assert.AreEqual (0, Convert.ToInt32 (null as object), "#W8");
3132 Assert.AreEqual (0, Convert.ToInt64 (null as object), "#W9");
3133 Assert.AreEqual (0, Convert.ToSByte (null as object), "#W10");
3134 Assert.AreEqual (0, Convert.ToSingle (null as object), "#W11");
3135 Assert.AreEqual ("", Convert.ToString (null as object), "#W12");
3136 Assert.AreEqual (0, Convert.ToUInt16 (null as object), "#W13");
3137 Assert.AreEqual (0, Convert.ToUInt32 (null as object), "#W14");
3138 Assert.AreEqual (0, Convert.ToUInt64 (null as object), "#W15");
3139 Assert.AreEqual (false, Convert.ToBoolean (null as string), "#W16");
3140 Assert.AreEqual (0, Convert.ToByte (null as string), "#W17");
3143 Convert.ToChar (null as string);
3145 } catch (Exception e) {
3146 Assert.AreEqual (typeof (ArgumentNullException), e.GetType (), "#W18");
3149 Assert.AreEqual (new DateTime (1, 1, 1, 0, 0, 0), Convert.ToDateTime (null as string), "#W19");
3150 Assert.AreEqual (0, Convert.ToDecimal (null as string), "#W20");
3151 Assert.AreEqual (0, Convert.ToDouble (null as string), "#W21");
3152 Assert.AreEqual (0, Convert.ToInt16 (null as string), "#W22");
3153 Assert.AreEqual (0, Convert.ToInt32 (null as string), "#W23");
3154 Assert.AreEqual (0, Convert.ToInt64 (null as string), "#W24");
3155 Assert.AreEqual (0, Convert.ToSByte (null as string), "#W25");
3156 Assert.AreEqual (0, Convert.ToSingle (null as string), "#W26");
3157 Assert.AreEqual (null, Convert.ToString (null as string), "#W27");
3158 Assert.AreEqual (0, Convert.ToUInt16 (null as string), "#W28");
3159 Assert.AreEqual (0, Convert.ToUInt32 (null as string), "#W29");
3160 Assert.AreEqual (0, Convert.ToUInt64 (null as string), "#W30");
3164 [ExpectedException (typeof (FormatException))]
3165 public void FromBase64StringInvalidFormat ()
3167 Convert.FromBase64String ("Tgtm+DBN====");
3171 [ExpectedException (typeof (FormatException))]
3172 public void FromBase64StringInvalidFormat2 ()
3174 Convert.FromBase64String ("Tgtm+DBN========");
3178 public void ToByte_PrefixedHexStringInBase16 ()
3180 Assert.AreEqual (255, Convert.ToByte ("0xff", 16), "0xff");
3181 Assert.AreEqual (255, Convert.ToByte ("0xfF", 16), "0xfF");
3182 Assert.AreEqual (255, Convert.ToByte ("0xFf", 16), "0xFf");
3183 Assert.AreEqual (255, Convert.ToByte ("0xFF", 16), "0xFF");
3185 Assert.AreEqual (255, Convert.ToByte ("0Xff", 16), "0Xff");
3186 Assert.AreEqual (255, Convert.ToByte ("0XfF", 16), "0XfF");
3187 Assert.AreEqual (255, Convert.ToByte ("0XFf", 16), "0XFf");
3188 Assert.AreEqual (255, Convert.ToByte ("0XFF", 16), "0XFF");
3190 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0x0", 16), "0x0");
3194 [ExpectedException (typeof (OverflowException))]
3195 public void ToByte_NegativeString ()
3197 Convert.ToByte ("-1");
3201 [ExpectedException (typeof (ArgumentException))]
3202 public void ToByte_NegativeStringNonBase10 ()
3204 Convert.ToByte ("-0", 16);
3208 [ExpectedException (typeof (OverflowException))]
3209 public void ToByte_NegativeString_Base10 ()
3211 Convert.ToByte ("-0", 10);
3215 public void ToByte_NegativeZeroString ()
3217 Convert.ToByte ("-0");
3218 Convert.ToByte ("-0", null);
3222 [ExpectedException (typeof (OverflowException))]
3223 public void ToUInt16_NegativeString ()
3225 Convert.ToUInt16 ("-1");
3229 [ExpectedException (typeof (ArgumentException))]
3230 public void ToUInt16_NegativeStringNonBase10 ()
3232 Convert.ToUInt16 ("-0", 16);
3236 [ExpectedException (typeof (OverflowException))]
3237 public void ToUInt16_NegativeString_Base10 ()
3239 Convert.ToUInt16 ("-0", 10);
3243 public void ToUInt16_NegativeZeroString ()
3245 Convert.ToUInt16 ("-0");
3246 Convert.ToUInt16 ("-0", null);
3250 [ExpectedException (typeof (OverflowException))]
3251 public void ToUInt32_NegativeString ()
3253 Convert.ToUInt32 ("-1");
3257 [ExpectedException (typeof (ArgumentException))]
3258 public void ToUInt32_NegativeStringNonBase10 ()
3260 Convert.ToUInt32 ("-0", 16);
3264 [ExpectedException (typeof (OverflowException))]
3265 public void ToUInt32_NegativeString_Base10 ()
3267 Convert.ToUInt32 ("-0", 10);
3271 public void ToUInt32_NegativeZeroString ()
3273 Convert.ToUInt32 ("-0");
3274 Convert.ToUInt32 ("-0", null);
3278 [ExpectedException (typeof (OverflowException))]
3279 public void ToUInt64_NegativeString ()
3281 Convert.ToUInt64 ("-1");
3285 [ExpectedException (typeof (ArgumentException))]
3286 public void ToUInt64_NegativeStringNonBase10 ()
3288 Convert.ToUInt64 ("-0", 16);
3292 [ExpectedException (typeof (OverflowException))]
3293 public void ToUInt64_NegativeString_Base10 ()
3295 Convert.ToUInt64 ("-0", 10);
3299 public void ToUInt64_NegativeZeroString ()
3301 Convert.ToUInt64 ("-0");
3302 Convert.ToUInt64 ("-0", null);
3308 public void ToByte_MaxValue ()
3310 Assert.AreEqual (Byte.MaxValue, Convert.ToByte ("ff", 16), "ff,16");
3311 Assert.AreEqual (Byte.MaxValue, Convert.ToByte ("0XFF", 16), "0xFF,16");
3312 Assert.AreEqual (Byte.MaxValue, Convert.ToByte ("255", 10), "255,10");
3313 Assert.AreEqual (Byte.MaxValue, Convert.ToByte ("377", 8), "377,8");
3314 Assert.AreEqual (Byte.MaxValue, Convert.ToByte ("11111111", 2), "11111111,2");
3318 public void ToByte_MinValue ()
3320 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0", 16), "0,16");
3321 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0x0", 16), "0x0,16");
3322 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0", 10), "0,10");
3323 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0", 8), "0,8");
3324 Assert.AreEqual (Byte.MinValue, Convert.ToByte ("0", 2), "0,2");
3328 public void ToUInt16_MaxValue ()
3330 Assert.AreEqual (UInt16.MaxValue, Convert.ToUInt16 ("ffff", 16), "ffff,16");
3331 Assert.AreEqual (UInt16.MaxValue, Convert.ToUInt16 ("0XFFFF", 16), "0XFFFF,16");
3332 Assert.AreEqual (UInt16.MaxValue, Convert.ToUInt16 ("65535", 10), "65535,10");
3333 Assert.AreEqual (UInt16.MaxValue, Convert.ToUInt16 ("177777", 8), "177777,8");
3334 Assert.AreEqual (UInt16.MaxValue, Convert.ToUInt16 ("1111111111111111", 2), "1111111111111111,2");
3338 public void ToUInt16_MinValue ()
3340 Assert.AreEqual (UInt16.MinValue, Convert.ToUInt16 ("0", 16), "0,16");
3341 Assert.AreEqual (UInt16.MinValue, Convert.ToUInt16 ("0x0", 16), "0x0,16");
3342 Assert.AreEqual (UInt16.MinValue, Convert.ToUInt16 ("0", 10), "0,10");
3343 Assert.AreEqual (UInt16.MinValue, Convert.ToUInt16 ("0", 8), "0,8");
3344 Assert.AreEqual (UInt16.MinValue, Convert.ToUInt16 ("0", 2), "0,2");
3348 public void ToUInt32_MaxValue ()
3350 Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 ("ffffffff", 16), "ffffffff,16");
3351 Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 ("0XFFFFFFFF", 16), "0XFFFFFFFF,16");
3352 Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 ("4294967295", 10), "4294967295,10");
3353 Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 ("37777777777", 8), "37777777777,8");
3354 Assert.AreEqual (UInt32.MaxValue, Convert.ToUInt32 ("11111111111111111111111111111111", 2), "11111111111111111111111111111111,2");
3358 public void ToUInt32_MinValue ()
3360 Assert.AreEqual (UInt32.MinValue, Convert.ToUInt32 ("0", 16), "0,16");
3361 Assert.AreEqual (UInt32.MinValue, Convert.ToUInt32 ("0x0", 16), "0x0,16");
3362 Assert.AreEqual (UInt32.MinValue, Convert.ToUInt32 ("0", 10), "0,10");
3363 Assert.AreEqual (UInt32.MinValue, Convert.ToUInt32 ("0", 8), "0,8");
3364 Assert.AreEqual (UInt32.MinValue, Convert.ToUInt32 ("0", 2), "0,2");
3368 public void ToUInt64_MaxValue ()
3370 Assert.AreEqual (UInt64.MaxValue, Convert.ToUInt64 ("ffffffffffffffff", 16), "ffffffffffffffff,16");
3371 Assert.AreEqual (UInt64.MaxValue, Convert.ToUInt64 ("0XFFFFFFFFFFFFFFFF", 16), "0XFFFFFFFFFFFFFFFF,16");
3372 Assert.AreEqual (UInt64.MaxValue, Convert.ToUInt64 ("18446744073709551615", 10), "18446744073709551615,10");
3373 Assert.AreEqual (UInt64.MaxValue, Convert.ToUInt64 ("1777777777777777777777", 8), "1777777777777777777777,8");
3374 Assert.AreEqual (UInt64.MaxValue, Convert.ToUInt64 ("1111111111111111111111111111111111111111111111111111111111111111", 2), "1111111111111111111111111111111111111111111111111111111111111111,2");
3378 public void ToUInt64_MinValue ()
3380 Assert.AreEqual (UInt64.MinValue, Convert.ToUInt64 ("0", 16), "0,16");
3381 Assert.AreEqual (UInt64.MinValue, Convert.ToUInt64 ("0x0", 16), "0x0,16");
3382 Assert.AreEqual (UInt64.MinValue, Convert.ToUInt64 ("0", 10), "0,10");
3383 Assert.AreEqual (UInt64.MinValue, Convert.ToUInt64 ("0", 8), "0,8");
3384 Assert.AreEqual (UInt64.MinValue, Convert.ToUInt64 ("0", 2), "0,2");
3390 public void ToSByte_MaxValue ()
3392 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("7f", 16), "7F,16");
3393 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("0X7F", 16), "0X7F,16");
3394 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("127", 10), "127,10");
3395 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("177", 8), "177,8");
3396 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("1111111", 2), "1111111,2");
3400 public void ToSByte_MinValue ()
3402 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("80", 16), "80,16");
3403 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("-128", 10), "-128,10");
3404 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("200", 8), "200,8");
3405 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("10000000", 2), "10000000,2");
3409 public void ToInt16_MaxValue ()
3411 Assert.AreEqual (Int16.MaxValue, Convert.ToInt16 ("7fff", 16), "7FFF,16");
3412 Assert.AreEqual (Int16.MaxValue, Convert.ToInt16 ("0X7FFF", 16), "0X7FFF,16");
3413 Assert.AreEqual (Int16.MaxValue, Convert.ToInt16 ("32767", 10), "32767,10");
3414 Assert.AreEqual (Int16.MaxValue, Convert.ToInt16 ("77777", 8), "77777,8");
3415 Assert.AreEqual (Int16.MaxValue, Convert.ToInt16 ("111111111111111", 2), "111111111111111,2");
3419 public void ToInt16_MinValue ()
3421 Assert.AreEqual (Int16.MinValue, Convert.ToInt16 ("8000", 16), "8000,16");
3422 Assert.AreEqual (Int16.MinValue, Convert.ToInt16 ("-32768", 10), "-32768,10");
3423 Assert.AreEqual (Int16.MinValue, Convert.ToInt16 ("100000", 8), "100000,8");
3424 Assert.AreEqual (Int16.MinValue, Convert.ToInt16 ("1000000000000000", 2), "1000000000000000,2");
3428 public void ToInt32_MaxValue ()
3430 Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 ("7fffffff", 16), "7fffffff,16");
3431 Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 ("0X7FFFFFFF", 16), "0X7FFFFFFF,16");
3432 Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 ("2147483647", 10), "2147483647,10");
3433 Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 ("17777777777", 8), "17777777777,8");
3434 Assert.AreEqual (Int32.MaxValue, Convert.ToInt32 ("1111111111111111111111111111111", 2), "1111111111111111111111111111111,2");
3438 public void ToInt32_MinValue ()
3440 Assert.AreEqual (Int32.MinValue, Convert.ToInt32 ("80000000", 16), "80000000,16");
3441 Assert.AreEqual (Int32.MinValue, Convert.ToInt32 ("-2147483648", 10), "-2147483648,10");
3442 Assert.AreEqual (Int32.MinValue, Convert.ToInt32 ("20000000000", 8), "20000000000,8");
3443 Assert.AreEqual (Int32.MinValue, Convert.ToInt32 ("10000000000000000000000000000000", 2), "10000000000000000000000000000000,2");
3447 public void ToInt64_MaxValue ()
3449 Assert.AreEqual (Int64.MaxValue, Convert.ToInt64 ("7fffffffffffffff", 16), "7fffffffffffffff,16");
3450 Assert.AreEqual (Int64.MaxValue, Convert.ToInt64 ("0X7FFFFFFFFFFFFFFF", 16), "0X7FFFFFFFFFFFFFFF,16");
3451 Assert.AreEqual (Int64.MaxValue, Convert.ToInt64 ("9223372036854775807", 10), "9223372036854775807,10");
3452 Assert.AreEqual (Int64.MaxValue, Convert.ToInt64 ("777777777777777777777", 8), "777777777777777777777,8");
3453 Assert.AreEqual (Int64.MaxValue, Convert.ToInt64 ("111111111111111111111111111111111111111111111111111111111111111", 2), "111111111111111111111111111111111111111111111111111111111111111,2");
3457 public void ToInt64_MinValue ()
3459 Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("8000000000000000", 16), "8000000000000000,16");
3460 Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("0x8000000000000000", 16), "0x8000000000000000,16");
3461 Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("-9223372036854775808", 10), "-9223372036854775808,10");
3462 Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("1000000000000000000000", 8), "1000000000000000000000,8");
3463 Assert.AreEqual (Int64.MinValue, Convert.ToInt64 ("1000000000000000000000000000000000000000000000000000000000000000", 2), "1000000000000000000000000000000000000000000000000000000000000000,2");
3467 public void MoreOverflows ()
3470 Convert.ToInt16 ("ffff7fff", 16);
3472 } catch (OverflowException) {
3476 Convert.ToSByte ("ffff7fff", 16);
3478 } catch (OverflowException) {
3482 Convert.ToUInt32 ("4294967298", 10);
3484 } catch (OverflowException) {
3491 [ExpectedException (typeof (OverflowException))]
3492 public void ToSByte_OverMaxValue ()
3494 string max_plus1 = "128";
3495 Convert.ToSByte (max_plus1);
3499 [ExpectedException (typeof (OverflowException))]
3500 public void ToSByte_OverMinValue ()
3502 string min_minus1 = "-129";
3503 Convert.ToSByte (min_minus1);
3507 [ExpectedException (typeof (OverflowException))]
3508 public void ToInt16_OverMaxValue ()
3510 string max_plus1 = "32768";
3511 Convert.ToInt16 (max_plus1);
3515 [ExpectedException (typeof (OverflowException))]
3516 public void ToInt16_OverMinValue ()
3518 string min_minus1 = "-32769";
3519 Convert.ToInt16 (min_minus1);
3523 [ExpectedException (typeof (OverflowException))]
3524 public void ToInt32_OverMaxValue ()
3526 string max_plus1 = "2147483648";
3527 Convert.ToInt32 (max_plus1);
3531 [ExpectedException (typeof (OverflowException))]
3532 public void ToInt32_OverMinValue ()
3534 string min_minus1 = "-2147483649";
3535 Convert.ToInt32 (min_minus1);
3539 [ExpectedException (typeof (OverflowException))]
3540 public void ToInt64_OverMaxValue ()
3542 string max_plus1 = "9223372036854775808";
3543 Convert.ToInt64 (max_plus1);
3547 [ExpectedException (typeof (OverflowException))]
3548 public void ToInt64_OverMinValue ()
3550 string min_minus1 = "-9223372036854775809";
3551 Convert.ToInt64 (min_minus1);
3557 [ExpectedException (typeof (OverflowException))]
3558 public void ToByte_OverMaxValue ()
3560 string max_plus1 = "257";
3561 Convert.ToByte (max_plus1);
3565 [ExpectedException (typeof (OverflowException))]
3566 public void ToByte_OverMinValue ()
3568 string min_minus1 = "-1";
3569 Convert.ToByte (min_minus1);
3573 [ExpectedException (typeof (OverflowException))]
3574 public void ToUInt16_OverMaxValue ()
3576 string max_plus1 = "65536";
3577 Convert.ToUInt16 (max_plus1);
3581 [ExpectedException (typeof (OverflowException))]
3582 public void ToUInt16_OverMinValue ()
3584 string min_minus1 = "-1";
3585 Convert.ToUInt16 (min_minus1);
3589 [ExpectedException (typeof (OverflowException))]
3590 public void ToUInt32_OverMaxValue ()
3592 string max_plus1 = "4294967296";
3593 Convert.ToUInt32 (max_plus1);
3597 [ExpectedException (typeof (OverflowException))]
3598 public void ToUInt32_OverMinValue ()
3600 string min_minus1 = "-1";
3601 Convert.ToUInt32 (min_minus1);
3605 [ExpectedException (typeof (OverflowException))]
3606 public void ToUInt64_OverMaxValue ()
3608 string max_plus1 = "18446744073709551616";
3609 Convert.ToUInt64 (max_plus1);
3613 [ExpectedException (typeof (OverflowException))]
3614 public void ToUInt64_OverMinValue ()
3616 string min_minus1 = "-1";
3617 Convert.ToUInt64 (min_minus1);
3621 public void To_NullString ()
3625 Assert.AreEqual (0, Convert.ToSByte (s), "ToSByte");
3626 Assert.AreEqual (0, Convert.ToSByte (s, 10), "ToSByte+base");
3627 Assert.AreEqual (0, Convert.ToInt16 (s), "ToInt16");
3628 Assert.AreEqual (0, Convert.ToInt16 (s, 10), "ToInt16+base");
3629 Assert.AreEqual (0, Convert.ToInt32 (s), "ToInt32");
3630 Assert.AreEqual (0, Convert.ToInt32 (s, 10), "ToInt32+base");
3631 Assert.AreEqual (0, Convert.ToInt64 (s), "ToInt64");
3632 Assert.AreEqual (0, Convert.ToInt64 (s, 10), "ToInt64+base");
3634 Assert.AreEqual (0, Convert.ToByte (s), "ToByte");
3635 Assert.AreEqual (0, Convert.ToByte (s, 10), "ToByte+base");
3636 Assert.AreEqual (0, Convert.ToUInt16 (s), "ToUInt16");
3637 Assert.AreEqual (0, Convert.ToUInt16 (s, 10), "ToUInt16+base");
3638 Assert.AreEqual (0, Convert.ToUInt32 (s), "ToUInt32");
3639 Assert.AreEqual (0, Convert.ToUInt32 (s, 10), "ToUInt32+base");
3640 Assert.AreEqual (0, Convert.ToUInt64 (s), "ToUInt64");
3641 Assert.AreEqual (0, Convert.ToUInt64 (s, 10), "ToUInt64+base");
3645 public void To_NullObject ()
3649 Assert.AreEqual (0, Convert.ToSByte (o), "ToSByte");
3650 Assert.AreEqual (0, Convert.ToInt16 (o), "ToInt16");
3651 Assert.AreEqual (0, Convert.ToInt32 (o), "ToInt32");
3652 Assert.AreEqual (0, Convert.ToInt64 (o), "ToInt64");
3654 Assert.AreEqual (0, Convert.ToByte (o), "ToByte");
3655 Assert.AreEqual (0, Convert.ToUInt16 (o), "ToUInt16");
3656 Assert.AreEqual (0, Convert.ToUInt32 (o), "ToUInt32");
3657 Assert.AreEqual (0, Convert.ToUInt64 (o), "ToUInt64");
3661 public void To_NullObjectFormatProvider ()
3664 IFormatProvider fp = (IFormatProvider) new NumberFormatInfo ();
3666 Assert.AreEqual (0, Convert.ToSByte (o, fp), "ToSByte");
3667 Assert.AreEqual (0, Convert.ToInt16 (o, fp), "ToInt16");
3668 Assert.AreEqual (0, Convert.ToInt32 (o, fp), "ToInt32");
3669 Assert.AreEqual (0, Convert.ToInt64 (o, fp), "ToInt64");
3671 Assert.AreEqual (0, Convert.ToByte (o, fp), "ToByte");
3672 Assert.AreEqual (0, Convert.ToUInt16 (o, fp), "ToUInt16");
3673 Assert.AreEqual (0, Convert.ToUInt32 (o, fp), "ToUInt32");
3674 Assert.AreEqual (0, Convert.ToUInt64 (o, fp), "ToUInt64");
3678 [ExpectedException (typeof (ArgumentNullException))]
3679 public void ToSByte_NullStringFormatProvider ()
3682 // SByte is a "special" case ???
3683 Convert.ToSByte (s, new NumberFormatInfo ());
3687 public void To_NullStringFormatProvider ()
3690 IFormatProvider fp = (IFormatProvider) new NumberFormatInfo ();
3693 Assert.AreEqual (0, Convert.ToInt16 (s, fp), "ToInt16");
3694 Assert.AreEqual (0, Convert.ToInt32 (s, fp), "ToInt32");
3695 Assert.AreEqual (0, Convert.ToInt64 (s, fp), "ToInt64");
3697 Assert.AreEqual (0, Convert.ToByte (s, fp), "ToByte");
3698 Assert.AreEqual (0, Convert.ToUInt16 (s, fp), "ToUInt16");
3699 Assert.AreEqual (0, Convert.ToUInt32 (s, fp), "ToUInt32");
3700 Assert.AreEqual (0, Convert.ToUInt64 (s, fp), "ToUInt64");
3704 [ExpectedException (typeof (InvalidCastException))]
3705 public void ChangeTypeToTypeCodeEmpty ()
3707 Convert.ChangeType (true, TypeCode.Empty);
3711 [ExpectedException (typeof (ArgumentNullException))]
3712 public void CharChangeTypeNullNull ()
3715 IConvertible convert = a;
3716 convert.ToType (null, null);
3720 [ExpectedException (typeof (ArgumentNullException))]
3721 public void StringChangeTypeNullNull ()
3724 IConvertible convert = a;
3725 convert.ToType (null, null);
3729 [ExpectedException (typeof (InvalidCastException))]
3730 public void ChangeTypeNullToValuetype ()
3732 Convert.ChangeType (null, typeof (int));
3736 public void ToString_MinMax_WithBase ()
3738 Assert.AreEqual ("0", Convert.ToString (Byte.MinValue, 2), "Byte.MinValue base 2");
3739 Assert.AreEqual ("0", Convert.ToString (Byte.MinValue, 8), "Byte.MinValue base 8");
3740 Assert.AreEqual ("0", Convert.ToString (Byte.MinValue, 10), "Byte.MinValue base 10");
3741 Assert.AreEqual ("0", Convert.ToString (Byte.MinValue, 16), "Byte.MinValue base 16");
3743 Assert.AreEqual ("11111111", Convert.ToString (Byte.MaxValue, 2), "Byte.MaxValue base 2");
3744 Assert.AreEqual ("377", Convert.ToString (Byte.MaxValue, 8), "Byte.MaxValue base 8");
3745 Assert.AreEqual ("255", Convert.ToString (Byte.MaxValue, 10), "Byte.MaxValue base 10");
3746 Assert.AreEqual ("ff", Convert.ToString (Byte.MaxValue, 16), "Byte.MaxValue base 16");
3748 Assert.AreEqual ("1000000000000000", Convert.ToString (Int16.MinValue, 2), "Int16.MinValue base 2");
3749 Assert.AreEqual ("100000", Convert.ToString (Int16.MinValue, 8), "Int16.MinValue base 8");
3750 Assert.AreEqual ("-32768", Convert.ToString (Int16.MinValue, 10), "Int16.MinValue base 10");
3751 Assert.AreEqual ("8000", Convert.ToString (Int16.MinValue, 16), "Int16.MinValue base 16");
3753 Assert.AreEqual ("111111111111111", Convert.ToString (Int16.MaxValue, 2), "Int16.MaxValue base 2");
3754 Assert.AreEqual ("77777", Convert.ToString (Int16.MaxValue, 8), "Int16.MaxValue base 8");
3755 Assert.AreEqual ("32767", Convert.ToString (Int16.MaxValue, 10), "Int16.MaxValue base 10");
3756 Assert.AreEqual ("7fff", Convert.ToString (Int16.MaxValue, 16), "Int16.MaxValue base 16");
3758 Assert.AreEqual ("10000000000000000000000000000000", Convert.ToString (Int32.MinValue, 2), "Int32.MinValue base 2");
3759 Assert.AreEqual ("20000000000", Convert.ToString (Int32.MinValue, 8), "Int32.MinValue base 8");
3760 Assert.AreEqual ("-2147483648", Convert.ToString (Int32.MinValue, 10), "Int32.MinValue base 10");
3761 Assert.AreEqual ("80000000", Convert.ToString (Int32.MinValue, 16), "Int32.MinValue base 16");
3763 Assert.AreEqual ("1111111111111111111111111111111", Convert.ToString (Int32.MaxValue, 2), "Int32.MaxValue base 2");
3764 Assert.AreEqual ("17777777777", Convert.ToString (Int32.MaxValue, 8), "Int32.MaxValue base 8");
3765 Assert.AreEqual ("2147483647", Convert.ToString (Int32.MaxValue, 10), "Int32.MaxValue base 10");
3766 Assert.AreEqual ("7fffffff", Convert.ToString (Int32.MaxValue, 16), "Int32.MaxValue base 16");
3768 Assert.AreEqual ("1000000000000000000000000000000000000000000000000000000000000000", Convert.ToString (Int64.MinValue, 2), "Int64.MinValue base 2");
3769 Assert.AreEqual ("1000000000000000000000", Convert.ToString (Int64.MinValue, 8), "Int64.MinValue base 8");
3770 Assert.AreEqual ("-9223372036854775808", Convert.ToString (Int64.MinValue, 10), "Int64.MinValue base 10");
3771 Assert.AreEqual ("8000000000000000", Convert.ToString (Int64.MinValue, 16), "Int64.MinValue base 16");
3773 Assert.AreEqual ("111111111111111111111111111111111111111111111111111111111111111", Convert.ToString (Int64.MaxValue, 2), "Int64.MaxValue base 2");
3774 Assert.AreEqual ("777777777777777777777", Convert.ToString (Int64.MaxValue, 8), "Int64.MaxValue base 8");
3775 Assert.AreEqual ("9223372036854775807", Convert.ToString (Int64.MaxValue, 10), "Int64.MaxValue base 10");
3776 Assert.AreEqual ("7fffffffffffffff", Convert.ToString (Int64.MaxValue, 16), "Int64.MaxValue base 16");
3780 [ExpectedException (typeof (FormatException))]
3781 public void ToByte_BadHexPrefix1 ()
3783 Convert.ToByte ("#10", 16);
3787 [ExpectedException (typeof (FormatException))]
3788 public void ToByte_BadHexPrefix2 ()
3790 Convert.ToByte ("&H10", 16);
3794 [ExpectedException (typeof (FormatException))]
3795 public void ToByte_BadHexPrefix3 ()
3797 Convert.ToByte ("&h10", 16);
3801 [ExpectedException (typeof (FormatException))]
3802 public void ToInt16_BadHexPrefix1 ()
3804 Convert.ToInt16 ("#10", 16);
3808 [ExpectedException (typeof (FormatException))]
3809 public void ToInt16_BadHexPrefix2 ()
3811 Convert.ToInt16 ("&H10", 16);
3815 [ExpectedException (typeof (FormatException))]
3816 public void ToInt16_BadHexPrefix3 ()
3818 Convert.ToInt16 ("&h10", 16);
3822 [ExpectedException (typeof (FormatException))]
3823 public void ToInt32_BadHexPrefix1 ()
3825 Convert.ToInt32 ("#10", 16);
3829 [ExpectedException (typeof (FormatException))]
3830 public void ToInt32_BadHexPrefix2 ()
3832 Convert.ToInt32 ("&H10", 16);
3836 [ExpectedException (typeof (FormatException))]
3837 public void ToInt32_BadHexPrefix3 ()
3839 Convert.ToInt32 ("&h10", 16);
3843 [ExpectedException (typeof (FormatException))]
3844 public void ToInt64_BadHexPrefix1 ()
3846 Convert.ToInt64 ("#10", 16);
3850 [ExpectedException (typeof (FormatException))]
3851 public void ToInt64_BadHexPrefix2 ()
3853 Convert.ToInt64 ("&H10", 16);
3857 [ExpectedException (typeof (FormatException))]
3858 public void ToInt64_BadHexPrefix3 ()
3860 Convert.ToInt64 ("&h10", 16);
3864 [ExpectedException (typeof (FormatException))]
3865 public void ToSByte_BadHexPrefix1 ()
3867 Convert.ToSByte ("#10", 16);
3871 [ExpectedException (typeof (FormatException))]
3872 public void ToSByte_BadHexPrefix2 ()
3874 Convert.ToSByte ("&H10", 16);
3878 [ExpectedException (typeof (FormatException))]
3879 public void ToSByte_BadHexPrefix3 ()
3881 Convert.ToSByte ("&h10", 16);
3885 [ExpectedException (typeof (FormatException))]
3886 public void ToUInt16_BadHexPrefix1 ()
3888 Convert.ToUInt16 ("#10", 16);
3892 [ExpectedException (typeof (FormatException))]
3893 public void ToUInt16_BadHexPrefix2 ()
3895 Convert.ToUInt16 ("&H10", 16);
3899 [ExpectedException (typeof (FormatException))]
3900 public void ToUInt16_BadHexPrefix3 ()
3902 Convert.ToUInt16 ("&h10", 16);
3906 [ExpectedException (typeof (FormatException))]
3907 public void ToUInt32_BadHexPrefix1 ()
3909 Convert.ToUInt32 ("#10", 16);
3913 [ExpectedException (typeof (FormatException))]
3914 public void ToUInt32_BadHexPrefix2 ()
3916 Convert.ToUInt32 ("&H10", 16);
3920 [ExpectedException (typeof (FormatException))]
3921 public void ToUInt32_BadHexPrefix3 ()
3923 Convert.ToUInt32 ("&h10", 16);
3927 [ExpectedException (typeof (FormatException))]
3928 public void ToUInt64_BadHexPrefix1 ()
3930 Convert.ToUInt64 ("#10", 16);
3934 [ExpectedException (typeof (FormatException))]
3935 public void ToUInt64_BadHexPrefix2 ()
3937 Convert.ToUInt64 ("&H10", 16);
3941 [ExpectedException (typeof (FormatException))]
3942 public void ToUInt64_BadHexPrefix3 ()
3944 Convert.ToUInt64 ("&h10", 16);
3948 public void ToSByte_Base16_MinMax ()
3950 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("80", 16), "80,16");
3951 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("0x80", 16), "0x80,16");
3952 Assert.AreEqual (SByte.MinValue, Convert.ToSByte ("0X80", 16), "0X80,16");
3954 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("7f", 16), "7f,16");
3955 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("7F", 16), "7F,16");
3956 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("0x7f", 16), "0x7f,16");
3957 Assert.AreEqual (SByte.MaxValue, Convert.ToSByte ("0X7F", 16), "0X7F,16");
3961 public void ToInt16_Base16_MinMax ()
3963 Assert.AreEqual (short.MinValue, Convert.ToInt16 ("8000", 16), "8000,16");
3964 Assert.AreEqual (short.MinValue, Convert.ToInt16 ("0x8000", 16), "0x8000,16");
3965 Assert.AreEqual (short.MinValue, Convert.ToInt16 ("0X8000", 16), "0X8000,16");
3967 Assert.AreEqual (short.MaxValue, Convert.ToInt16 ("7fff", 16), "7fff,16");
3968 Assert.AreEqual (short.MaxValue, Convert.ToInt16 ("7FFF", 16), "7FFF,16");
3969 Assert.AreEqual (short.MaxValue, Convert.ToInt16 ("0x7fff", 16), "0x7fff,16");
3970 Assert.AreEqual (short.MaxValue, Convert.ToInt16 ("0X7FFF", 16), "0X7FFF,16");
3974 public void ToInt32_Base16_MinMax ()
3976 Assert.AreEqual (int.MinValue, Convert.ToInt32 ("80000000", 16), "80000000,16");
3977 Assert.AreEqual (int.MinValue, Convert.ToInt32 ("0x80000000", 16), "0x80000000,16");
3978 Assert.AreEqual (int.MinValue, Convert.ToInt32 ("0X80000000", 16), "0X80000000,16");
3980 Assert.AreEqual (int.MaxValue, Convert.ToInt32 ("7fffffff", 16), "7fffffff,16");
3981 Assert.AreEqual (int.MaxValue, Convert.ToInt32 ("7FFFFFFF", 16), "7FFFFFFF,16");
3982 Assert.AreEqual (int.MaxValue, Convert.ToInt32 ("0x7fffffff", 16), "0x7fffffff,16");
3983 Assert.AreEqual (int.MaxValue, Convert.ToInt32 ("0X7FFFFFFF", 16), "0X7FFFFFFF,16");
3987 [ExpectedException (typeof (FormatException))]
3988 public void ToByte_Base10_InvalidChars1 ()
3990 Convert.ToByte ("0-1", 10);
3994 [ExpectedException (typeof (FormatException))]
3995 public void ToByte_Base10_InvalidChars2 ()
3997 Convert.ToByte ("FF", 10);
4001 [ExpectedException (typeof (FormatException))]
4002 public void ToInt16_Base10_InvalidChars1 ()
4004 Convert.ToInt16 ("0-1", 10);
4008 [ExpectedException (typeof (FormatException))]
4009 public void ToInt16_Base10_InvalidChars2 ()
4011 Convert.ToInt16 ("FF", 10);
4015 [ExpectedException (typeof (FormatException))]
4016 public void ToInt32_Base10_InvalidChars1 ()
4018 Convert.ToInt32 ("0-1", 10);
4022 [ExpectedException (typeof (FormatException))]
4023 public void ToInt32_Base10_InvalidChars2 ()
4025 Convert.ToInt32 ("FF", 10);
4029 [ExpectedException (typeof (FormatException))]
4030 public void ToInt64_Base10_InvalidChars1 ()
4032 Convert.ToInt64 ("0-1", 10);
4036 [ExpectedException (typeof (FormatException))]
4037 public void ToInt64_Base10_InvalidChars2 ()
4039 Convert.ToInt64 ("FF", 10);
4043 [ExpectedException (typeof (FormatException))]
4044 public void ToSByte_Base10_InvalidChars1 ()
4046 Convert.ToSByte ("0-1", 10);
4050 [ExpectedException (typeof (FormatException))]
4051 public void ToSByte_Base10_InvalidChars2 ()
4053 Convert.ToSByte ("FF", 10);
4057 [ExpectedException (typeof (FormatException))]
4058 public void ToUInt16_Base10_InvalidChars1 ()
4060 Convert.ToUInt16 ("0-1", 10);
4064 [ExpectedException (typeof (FormatException))]
4065 public void ToUInt16_Base10_InvalidChars2 ()
4067 Convert.ToUInt16 ("FF", 10);
4071 [ExpectedException (typeof (FormatException))]
4072 public void ToUInt32_Base10_InvalidChars1 ()
4074 Convert.ToUInt32 ("0-1", 10);
4078 [ExpectedException (typeof (FormatException))]
4079 public void ToUInt32_Base10_InvalidChars2 ()
4081 Convert.ToUInt32 ("FF", 10);
4085 [ExpectedException (typeof (FormatException))]
4086 public void ToUInt64_Base10_InvalidChars1 ()
4088 Convert.ToUInt64 ("0-1", 10);
4092 [ExpectedException (typeof (FormatException))]
4093 public void ToUInt64_Base10_InvalidChars2 ()
4095 Convert.ToUInt64 ("FF", 10);
4099 [ExpectedException (typeof (FormatException))]
4100 public void ToByte_Base16_InvalidChars1 ()
4102 Convert.ToByte ("0-1", 16);
4106 [ExpectedException (typeof (FormatException))]
4107 public void ToByte_Base16_InvalidChars2 ()
4109 Convert.ToByte ("GG", 16);
4113 [ExpectedException (typeof (FormatException))]
4114 public void ToInt16_Base16_InvalidChars1 ()
4116 Convert.ToInt16 ("0-1", 16);
4120 [ExpectedException (typeof (FormatException))]
4121 public void ToInt16_Base16_InvalidChars2 ()
4123 Convert.ToInt16 ("GG", 16);
4127 [ExpectedException (typeof (FormatException))]
4128 public void ToInt32_Base16_InvalidChars1 ()
4130 Convert.ToInt32 ("0-1", 16);
4134 [ExpectedException (typeof (FormatException))]
4135 public void ToInt32_Base16_InvalidChars2 ()
4137 Convert.ToInt32 ("GG", 16);
4141 [ExpectedException (typeof (FormatException))]
4142 public void ToInt64_Base16_InvalidChars1 ()
4144 Convert.ToInt64 ("0-1", 16);
4148 [ExpectedException (typeof (FormatException))]
4149 public void ToInt64_Base16_InvalidChars2 ()
4151 Convert.ToInt64 ("GG", 16);
4155 [ExpectedException (typeof (FormatException))]
4156 public void ToSByte_Base16_InvalidChars1 ()
4158 Convert.ToSByte ("0-1", 16);
4162 [ExpectedException (typeof (FormatException))]
4163 public void ToSByte_Base16_InvalidChars2 ()
4165 Convert.ToSByte ("GG", 16);
4169 [ExpectedException (typeof (FormatException))]
4170 public void ToUInt16_Base16_InvalidChars1 ()
4172 Convert.ToUInt16 ("0-1", 16);
4176 [ExpectedException (typeof (FormatException))]
4177 public void ToUInt16_Base16_InvalidChars2 ()
4179 Convert.ToUInt16 ("GG", 16);
4183 [ExpectedException (typeof (FormatException))]
4184 public void ToUInt32_Base16_InvalidChars1 ()
4186 Convert.ToUInt32 ("0-1", 16);
4190 [ExpectedException (typeof (FormatException))]
4191 public void ToUInt32_Base16_InvalidChars2 ()
4193 Convert.ToUInt32 ("GG", 16);
4197 [ExpectedException (typeof (FormatException))]
4198 public void ToUInt64_Base16_InvalidChars1 ()
4200 Convert.ToUInt64 ("0-1", 16);
4204 [ExpectedException (typeof (FormatException))]
4205 public void ToUInt64_Base16_InvalidChars2 ()
4207 Convert.ToUInt64 ("GG", 16);
4211 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4212 public void ToByte_Base2_Empty ()
4214 Convert.ToByte ("", 2);
4218 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4219 public void ToByte_Base8_Empty ()
4221 Convert.ToByte ("", 8);
4225 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4226 public void ToByte_Base10_Empty ()
4228 Convert.ToByte ("", 10);
4232 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4233 public void ToByte_Base16_Empty ()
4235 Convert.ToByte ("", 16);
4239 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4240 public void ToInt16_Base2_Empty ()
4242 Convert.ToInt16 ("", 2);
4246 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4247 public void ToInt16_Base8_Empty ()
4249 Convert.ToInt16 ("", 8);
4253 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4254 public void ToInt16_Base10_Empty ()
4256 Convert.ToInt16 ("", 10);
4260 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4261 public void ToInt16_Base16_Empty ()
4263 Convert.ToInt16 ("", 16);
4267 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4268 public void ToInt32_Base2_Empty ()
4270 Convert.ToInt32 ("", 2);
4274 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4275 public void ToInt32_Base8_Empty ()
4277 Convert.ToInt32 ("", 8);
4281 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4282 public void ToInt32_Base10_Empty ()
4284 Convert.ToInt32 ("", 10);
4288 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4289 public void ToInt32_Base16_Empty ()
4291 Convert.ToInt32 ("", 16);
4295 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4296 public void ToInt64_Base2_Empty ()
4298 Convert.ToInt64 ("", 2);
4302 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4303 public void ToInt64_Base8_Empty ()
4305 Convert.ToInt64 ("", 8);
4309 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4310 public void ToInt64_Base10_Empty ()
4312 Convert.ToInt64 ("", 10);
4316 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4317 public void ToInt64_Base16_Empty ()
4319 Convert.ToInt64 ("", 16);
4323 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4324 public void ToSByte_Base2_Empty ()
4326 Convert.ToSByte ("", 2);
4330 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4331 public void ToSByte_Base8_Empty ()
4333 Convert.ToSByte ("", 8);
4337 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4338 public void ToSByte_Base10_Empty ()
4340 Convert.ToSByte ("", 10);
4344 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4345 public void ToSByte_Base16_Empty ()
4347 Convert.ToSByte ("", 16);
4351 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4352 public void ToUInt16_Base2_Empty ()
4354 Convert.ToUInt16 ("", 2);
4358 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4359 public void ToUInt16_Base8_Empty ()
4361 Convert.ToUInt16 ("", 8);
4365 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4366 public void ToUInt16_Base10_Empty ()
4368 Convert.ToUInt16 ("", 10);
4372 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4373 public void ToUInt16_Base16_Empty ()
4375 Convert.ToUInt16 ("", 16);
4379 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4380 public void ToUInt32_Base2_Empty ()
4382 Convert.ToUInt32 ("", 2);
4386 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4387 public void ToUInt32_Base8_Empty ()
4389 Convert.ToUInt32 ("", 8);
4393 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4394 public void ToUInt32_Base10_Empty ()
4396 Convert.ToUInt32 ("", 10);
4400 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4401 public void ToUInt32_Base16_Empty ()
4403 Convert.ToUInt32 ("", 16);
4407 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4408 public void ToUInt64_Base2_Empty ()
4410 Convert.ToUInt64 ("", 2);
4414 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4415 public void ToUInt64_Base8_Empty ()
4417 Convert.ToUInt64 ("", 8);
4421 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4422 public void ToUInt64_Base10_Empty ()
4424 Convert.ToUInt64 ("", 10);
4428 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4429 public void ToUInt64_Base16_Empty ()
4431 Convert.ToUInt64 ("", 16);
4435 [ExpectedException (typeof (FormatException))]
4436 public void ToByte_HexPrefixOnly ()
4438 Convert.ToByte ("0x", 16);
4442 [ExpectedException (typeof (FormatException))]
4443 public void ToInt16_HexPrefixOnly ()
4445 Convert.ToInt16 ("0x", 16);
4449 [ExpectedException (typeof (FormatException))]
4450 public void ToInt32_HexPrefixOnly ()
4452 Convert.ToInt32 ("0x", 16);
4456 [ExpectedException (typeof (FormatException))]
4457 public void ToInt64_HexPrefixOnly ()
4459 Convert.ToInt64 ("0x", 16);
4463 [ExpectedException (typeof (FormatException))]
4464 public void ToSByte_HexPrefixOnly ()
4466 Convert.ToSByte ("0x", 16);
4470 [ExpectedException (typeof (FormatException))]
4471 public void ToUInt16_HexPrefixOnly ()
4473 Convert.ToUInt16 ("0x", 16);
4477 [ExpectedException (typeof (FormatException))]
4478 public void ToUInt32_HexPrefixOnly ()
4480 Convert.ToUInt32 ("0x", 16);
4484 [ExpectedException (typeof (FormatException))]
4485 public void ToUInt64_HexPrefixOnly ()
4487 Convert.ToUInt64 ("0x", 16);
4491 [ExpectedException (typeof (ArgumentException))]
4492 public void ToByte_Base2_NegativeSignOnly ()
4494 Convert.ToByte ("-", 2);
4498 [ExpectedException (typeof (ArgumentException))]
4499 public void ToByte_Base8_NegativeSignOnly ()
4501 Convert.ToByte ("-", 8);
4505 [ExpectedException (typeof (OverflowException))]
4506 public void ToByte_Base10_NegativeSignOnly ()
4508 Convert.ToByte ("-", 10);
4512 [ExpectedException (typeof (ArgumentException))]
4513 public void ToByte_Base16_NegativeSignOnly ()
4515 Convert.ToByte ("-", 16);
4519 [ExpectedException (typeof (ArgumentException))]
4520 public void ToInt16_Base2_NegativeSignOnly ()
4522 Convert.ToInt16 ("-", 2);
4526 [ExpectedException (typeof (ArgumentException))]
4527 public void ToInt16_Base8_NegativeSignOnly ()
4529 Convert.ToInt16 ("-", 8);
4533 [ExpectedException (typeof (FormatException))]
4534 public void ToInt16_Base10_NegativeSignOnly ()
4536 Convert.ToInt16 ("-", 10);
4540 [ExpectedException (typeof (ArgumentException))]
4541 public void ToInt16_Base16_NegativeSignOnly ()
4543 Convert.ToInt16 ("-", 16);
4547 [ExpectedException (typeof (ArgumentException))]
4548 public void ToInt32_Base2_NegativeSignOnly ()
4550 Convert.ToInt32 ("-", 2);
4554 [ExpectedException (typeof (ArgumentException))]
4555 public void ToInt32_Base8_NegativeSignOnly ()
4557 Convert.ToInt32 ("-", 8);
4561 [ExpectedException (typeof (FormatException))]
4562 public void ToInt32_Base10_NegativeSignOnly ()
4564 Convert.ToInt32 ("-", 10);
4568 [ExpectedException (typeof (ArgumentException))]
4569 public void ToInt32_Base16_NegativeSignOnly ()
4571 Convert.ToInt32 ("-", 16);
4575 [ExpectedException (typeof (ArgumentException))]
4576 public void ToInt64_Base2_NegativeSignOnly ()
4578 Convert.ToInt64 ("-", 2);
4582 [ExpectedException (typeof (ArgumentException))]
4583 public void ToInt64_Base8_NegativeSignOnly ()
4585 Convert.ToInt64 ("-", 8);
4589 [ExpectedException (typeof (FormatException))]
4590 public void ToInt64_Base10_NegativeSignOnly ()
4592 Convert.ToInt64 ("-", 10);
4596 [ExpectedException (typeof (ArgumentException))]
4597 public void ToInt64_Base16_NegativeSignOnly ()
4599 Convert.ToInt64 ("-", 16);
4603 [ExpectedException (typeof (ArgumentException))]
4604 public void ToSByte_Base2_NegativeSignOnly ()
4606 Convert.ToSByte ("-", 2);
4610 [ExpectedException (typeof (ArgumentException))]
4611 public void ToSByte_Base8_NegativeSignOnly ()
4613 Convert.ToSByte ("-", 8);
4617 [ExpectedException (typeof (FormatException))]
4618 public void ToSByte_Base10_NegativeSignOnly ()
4620 Convert.ToSByte ("-", 10);
4624 [ExpectedException (typeof (ArgumentException))]
4625 public void ToSByte_Base16_NegativeSignOnly ()
4627 Convert.ToSByte ("-", 16);
4631 [ExpectedException (typeof (ArgumentException))]
4632 public void ToUInt16_Base2_NegativeSignOnly ()
4634 Convert.ToUInt16 ("-", 2);
4638 [ExpectedException (typeof (ArgumentException))]
4639 public void ToUInt16_Base8_NegativeSignOnly ()
4641 Convert.ToUInt16 ("-", 8);
4645 [ExpectedException (typeof (OverflowException))]
4646 public void ToUInt16_Base10_NegativeSignOnly ()
4648 Convert.ToUInt16 ("-", 10);
4652 [ExpectedException (typeof (ArgumentException))]
4653 public void ToUInt16_Base16_NegativeSignOnly ()
4655 Convert.ToUInt16 ("-", 16);
4659 [ExpectedException (typeof (ArgumentException))]
4660 public void ToUInt32_Base2_NegativeSignOnly ()
4662 Convert.ToUInt32 ("-", 2);
4666 [ExpectedException (typeof (ArgumentException))]
4667 public void ToUInt32_Base8_NegativeSignOnly ()
4669 Convert.ToUInt32 ("-", 8);
4673 [ExpectedException (typeof (OverflowException))]
4674 public void ToUInt32_Base10_NegativeSignOnly ()
4676 Convert.ToUInt32 ("-", 10);
4680 [ExpectedException (typeof (ArgumentException))]
4681 public void ToUInt32_Base16_NegativeSignOnly ()
4683 Convert.ToUInt32 ("-", 16);
4687 [ExpectedException (typeof (ArgumentException))]
4688 public void ToUInt64_Base2_NegativeSignOnly ()
4690 Convert.ToUInt64 ("-", 2);
4694 [ExpectedException (typeof (ArgumentException))]
4695 public void ToUInt64_Base8_NegativeSignOnly ()
4697 Convert.ToUInt64 ("-", 8);
4701 [ExpectedException (typeof (OverflowException))]
4702 public void ToUInt64_Base10_NegativeSignOnly ()
4704 Convert.ToUInt64 ("-", 10);
4708 [ExpectedException (typeof (ArgumentException))]
4709 public void ToUInt64_Base16_NegativeSignOnly ()
4711 Convert.ToUInt64 ("-", 16);
4714 [Test] // bug #481687
4715 public void ChangeType_Value_IConvertible ()
4717 BitmapStatus bitmapStatus = new BitmapStatus (3);
4718 Image c1 = Convert.ChangeType (bitmapStatus, typeof (Image)) as Image;
4719 Assert.IsNotNull (c1, "#A1");
4720 Assert.AreEqual (32, c1.ColorDepth, "#A2");
4722 bitmapStatus.ConvertToImage = false;
4723 object c2 = Convert.ChangeType (bitmapStatus, typeof (Image));
4724 Assert.IsNull (c2, "#B");
4726 object c3 = Convert.ChangeType (bitmapStatus, typeof (int));
4727 Assert.IsNotNull (c3, "#C1");
4728 Assert.AreEqual (3, c3, "#C2");
4731 // This is a simple and happy struct.
4736 [ExpectedException (typeof (InvalidCastException))]
4737 public void ChangeType_ShouldThrowOnString ()
4739 Convert.ChangeType ("this-is-a-string", typeof (Foo));
4743 [ExpectedException (typeof (OverflowException))]
4744 public void ToInt32_NaN ()
4746 Convert.ToInt32 (Double.NaN);
4750 public void ChangeTypeFromInvalidDouble ()
4752 // types which should generate OverflowException from double.NaN, etc.
4753 Type[] types = new Type []{
4754 typeof (byte), typeof (sbyte), typeof (decimal),
4755 typeof (short), typeof (int), typeof (long),
4756 typeof (ushort), typeof (uint), typeof (ulong),
4759 CheckChangeTypeOverflow ("double.NaN", double.NaN, types);
4760 CheckChangeTypeOverflow ("double.PositiveInfinity", double.PositiveInfinity, types);
4761 CheckChangeTypeOverflow ("double.NegativeInfinity", double.NegativeInfinity, types);
4762 CheckChangeTypeOverflow ("float.NaN", float.NaN, types);
4763 CheckChangeTypeOverflow ("float.PositiveInfinity", float.PositiveInfinity, types);
4764 CheckChangeTypeOverflow ("float.NegativeInfinity", float.NegativeInfinity, types);
4767 static void CheckChangeTypeOverflow (string svalue, object value, Type[] types)
4769 foreach (Type type in types) {
4770 string message = string.Format (" when converting {0} to {1}", svalue, type.FullName);
4772 Convert.ChangeType (value, type);
4773 Assert.Fail ("Expected System.OverflowException " + message);
4775 catch (OverflowException) {
4778 catch (Exception e) {
4779 Assert.Fail ("Unexpected exception type " + e.GetType ().FullName + message);
4785 public void ToInt32_InvalidFormatProvider ()
4787 Assert.AreEqual (5, Convert.ToInt32 ("5", new InvalidFormatProvider ()));
4793 private int colorDepth;
4800 public Image (int colorDepth)
4802 this.colorDepth = colorDepth;
4805 public int ColorDepth {
4806 get { return colorDepth; }
4810 public class BitmapStatus : IConvertible
4812 protected int m_Status;
4813 private bool convertToImage;
4815 public BitmapStatus (int status)
4818 convertToImage = true;
4821 public bool ConvertToImage {
4822 get { return convertToImage; }
4823 set { convertToImage = value; }
4826 TypeCode IConvertible.GetTypeCode ()
4828 return TypeCode.Int32;
4831 bool IConvertible.ToBoolean (IFormatProvider provider)
4833 return (bool)((IConvertible)this).ToType (typeof (bool), provider);
4836 byte IConvertible.ToByte (IFormatProvider provider)
4838 return (byte)((IConvertible)this).ToType (typeof (byte), provider);
4841 char IConvertible.ToChar (IFormatProvider provider)
4843 return (char)((IConvertible)this).ToType (typeof (char), provider);
4846 DateTime IConvertible.ToDateTime (IFormatProvider provider)
4848 return (DateTime)((IConvertible)this).ToType (typeof (DateTime), provider);
4851 decimal IConvertible.ToDecimal (IFormatProvider provider)
4853 return (decimal)((IConvertible)this).ToType (typeof (decimal), provider);
4856 double IConvertible.ToDouble (IFormatProvider provider)
4858 return (double)((IConvertible)this).ToType (typeof (double), provider);
4861 short IConvertible.ToInt16 (IFormatProvider provider)
4863 return (short)((IConvertible)this).ToType (typeof (short), provider);
4866 int IConvertible.ToInt32 (IFormatProvider provider)
4868 return (int)m_Status;
4871 long IConvertible.ToInt64 (IFormatProvider provider)
4873 return (long)((IConvertible)this).ToType (typeof (long), provider);
4876 sbyte IConvertible.ToSByte (IFormatProvider provider)
4878 return (sbyte)((IConvertible)this).ToType (typeof (sbyte), provider);
4881 float IConvertible.ToSingle (IFormatProvider provider)
4883 return (float)((IConvertible)this).ToType (typeof (float), provider);
4886 string IConvertible.ToString (IFormatProvider provider)
4888 return (string)((IConvertible)this).ToType (typeof (string), provider);
4891 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
4893 if (ConvertToImage && conversionType == typeof (Image))
4894 return new Image (32);
4895 else if (conversionType.IsAssignableFrom (typeof (int)))
4896 return Convert.ChangeType (1, conversionType, provider);
4900 ushort IConvertible.ToUInt16 (IFormatProvider provider)
4902 return (ushort)((IConvertible)this).ToType (typeof (ushort), provider);
4905 uint IConvertible.ToUInt32 (IFormatProvider provider)
4907 return (uint)((IConvertible)this).ToType (typeof (uint), provider);
4910 ulong IConvertible.ToUInt64 (IFormatProvider provider)
4912 return (ulong)((IConvertible)this).ToType (typeof (ulong), provider);
4916 class InvalidFormatProvider : IFormatProvider
4918 public object GetFormat (Type formatType)