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 : Assertion {
47 tryDT = new DateTime(2002,1,1);
52 tryInt64 = 123456789012;
53 tryObj = new Object();
55 tryFloat = 1234.2345f;
56 falseString = "false";
63 ci = new CultureInfo("en-US");
64 ci.NumberFormat.NumberDecimalDigits = 3;
67 public void TestChangeType() {
70 AssertEquals("#A01", (short)12345, Convert.ChangeType(tryInt32, typeof(short)));
72 AssertEquals("#A02", 'A', Convert.ChangeType(65, typeof(char)));
74 AssertEquals("#A03", 66, Convert.ChangeType('B', typeof(int)));
76 AssertEquals("#A04", ((ulong)12345), Convert.ChangeType(tryInt32, typeof(ulong)));
79 AssertEquals("#A05", true, Convert.ChangeType(tryDec, TypeCode.Boolean));
81 AssertEquals("#A06", 'f', Convert.ChangeType("f", TypeCode.Char));
83 AssertEquals("#A07", (decimal)123456789012, Convert.ChangeType(tryInt64, TypeCode.Decimal));
85 AssertEquals("#A08", (int)34567, Convert.ChangeType(tryUI16, TypeCode.Int32));
88 AssertEquals("#A09", (double)567891234, Convert.ChangeType(tryUI32, typeof(double), ci));
90 AssertEquals("#A10", (ushort)0, Convert.ChangeType(tryByte, typeof(ushort), ci));
92 AssertEquals("#A11", (decimal)567891234, Convert.ChangeType(tryUI32, typeof(decimal), ci));
94 AssertEquals("#A12", (float)1234, Convert.ChangeType(tryInt16, typeof(float), ci));
96 AssertEquals("#A13", null, Convert.ChangeType(null, null, ci));
99 AssertEquals("#A14", (decimal)0, Convert.ChangeType(tryByte, TypeCode.Decimal, ci));
101 AssertEquals("#A15", "f", Convert.ChangeType('f', TypeCode.String, ci));
103 AssertEquals("#A16", 'D', Convert.ChangeType(68, TypeCode.Char, ci));
105 AssertEquals("#A17", (long)34567, Convert.ChangeType(tryUI16, TypeCode.Int64, ci));
107 AssertEquals("#A18", null, Convert.ChangeType(null, TypeCode.Empty, ci));
108 } catch (Exception e) {
109 Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
113 Convert.ChangeType(boolTrue, typeof(char));
116 catch (Exception e) {
117 AssertEquals("#A25", typeof(InvalidCastException), e.GetType());
121 Convert.ChangeType(tryChar, typeof(DateTime));
124 catch (Exception e) {
125 AssertEquals("#A26", typeof(InvalidCastException), e.GetType());
129 Convert.ChangeType(ci, TypeCode.String);
132 catch (Exception e) {
133 AssertEquals("#A27", typeof(InvalidCastException), e.GetType());
137 Convert.ChangeType(tryInt32, null);
140 catch (Exception e) {
141 AssertEquals("#A28", typeof(ArgumentNullException), e.GetType());
146 Convert.ChangeType(boolTrue, typeof(DateTime), ci);
149 catch (Exception e) {
150 AssertEquals("#A29", typeof(InvalidCastException), e.GetType());
154 Convert.ChangeType(ci, typeof(DateTime), ci);
157 catch (Exception e) {
158 AssertEquals("#A30", typeof(InvalidCastException), e.GetType());
161 /* Should throw ArgumentException but throws InvalidCastException
163 Convert.ChangeType(tryUI32, typeof(FormatException), ci);
166 catch (Exception e) {
167 AssertEquals("#A??", typeof(ArgumentException), e.GetType());
171 Convert.ChangeType(tryUI32, TypeCode.Byte, ci);
174 catch (Exception e) {
175 AssertEquals("#A31", typeof(OverflowException), e.GetType());
179 Convert.ChangeType(boolTrue, TypeCode.Char, ci);
182 catch (Exception e) {
183 AssertEquals("#A32", typeof(InvalidCastException), e.GetType());
187 Convert.ChangeType(boolTrue, null, ci);
190 catch (Exception e) {
191 AssertEquals("#A33", typeof(ArgumentNullException), e.GetType());
195 /* should fail to convert string to any enumeration type. */
196 Convert.ChangeType("random string", typeof(DayOfWeek));
199 catch (Exception e) {
200 AssertEquals("#A34", typeof(InvalidCastException), e.GetType());
205 public void TestGetTypeCode() {
208 AssertEquals("#B01", TypeCode.String, Convert.GetTypeCode(tryStr));
210 AssertEquals("#B02", TypeCode.UInt16, Convert.GetTypeCode(tryUI16));
212 AssertEquals("#B03", TypeCode.UInt32, Convert.GetTypeCode(tryUI32));
214 AssertEquals("#B04", TypeCode.UInt64, Convert.GetTypeCode(tryUI64));
216 AssertEquals("#B05", TypeCode.Double, Convert.GetTypeCode(tryDbl));
218 AssertEquals("#B06", TypeCode.Int16, Convert.GetTypeCode(tryInt16));
220 AssertEquals("#B07", TypeCode.Int64, Convert.GetTypeCode(tryInt64));
222 AssertEquals("#B08", TypeCode.Object, Convert.GetTypeCode(tryObj));
224 AssertEquals("#B09", TypeCode.SByte, Convert.GetTypeCode(trySByte));
226 AssertEquals("#B10", TypeCode.Single, Convert.GetTypeCode(tryFloat));
228 AssertEquals("#B11", TypeCode.Byte, Convert.GetTypeCode(tryByte));
230 AssertEquals("#B12", TypeCode.Char, Convert.GetTypeCode(tryChar));
232 // AssertEquals("#B13", TypeCode.DateTime, Convert.GetTypeCode(tryDT));
234 AssertEquals("#B14", TypeCode.Decimal, Convert.GetTypeCode(tryDec));
236 AssertEquals("#B15", TypeCode.Int32, Convert.GetTypeCode(tryInt32));
238 AssertEquals("#B16", TypeCode.Boolean, Convert.GetTypeCode(boolTrue));
239 } catch (Exception e) {
240 Fail ("Unexpected exception at " + marker + ": " + e);
244 public void TestIsDBNull() {
245 AssertEquals("#C01", false, Convert.IsDBNull(tryInt32));
246 AssertEquals("#C02", true, Convert.IsDBNull(Convert.DBNull));
247 AssertEquals("#C03", false, Convert.IsDBNull(boolTrue));
248 AssertEquals("#C04", false, Convert.IsDBNull(tryChar));
249 AssertEquals("#C05", false, Convert.IsDBNull(tryFloat));
252 public void TestToBoolean() {
253 tryObj = (object)tryDbl;
255 AssertEquals("#D01", true, Convert.ToBoolean(boolTrue));
256 AssertEquals("#D02", false, Convert.ToBoolean(tryByte));
257 AssertEquals("#D03", true, Convert.ToBoolean(tryDec));
258 AssertEquals("#D04", false, Convert.ToBoolean(tryDbl));
259 AssertEquals("#D05", true, Convert.ToBoolean(tryInt16));
260 AssertEquals("#D06", true, Convert.ToBoolean(tryInt32));
261 AssertEquals("#D07", true, Convert.ToBoolean(tryInt64));
262 AssertEquals("#D08", false, Convert.ToBoolean(tryObj));
263 AssertEquals("#D09", true, Convert.ToBoolean(trySByte));
264 AssertEquals("#D10", true, Convert.ToBoolean(tryFloat));
265 AssertEquals("#D11", true, Convert.ToBoolean(trueString));
266 AssertEquals("#D12", false, Convert.ToBoolean(falseString));
267 AssertEquals("#D13", true, Convert.ToBoolean(tryUI16));
268 AssertEquals("#D14", true, Convert.ToBoolean(tryUI32));
269 AssertEquals("#D15", false, Convert.ToBoolean(tryUI64));
270 AssertEquals("#D16", false, Convert.ToBoolean(tryObj,ci));
271 AssertEquals("#D17", true, Convert.ToBoolean(trueString, ci));
272 AssertEquals("#D18", false, Convert.ToBoolean(falseString, ci));
275 Convert.ToBoolean(tryChar);
278 catch (Exception e) {
279 AssertEquals("#D20", typeof(InvalidCastException), e.GetType());
283 Convert.ToBoolean(tryDT);
286 catch (Exception e) {
287 AssertEquals("#D21", typeof(InvalidCastException), e.GetType());
291 Convert.ToBoolean(tryStr);
294 catch (Exception e) {
295 AssertEquals("#D22", typeof(FormatException), e.GetType());
299 Convert.ToBoolean(nullString);
302 catch (Exception e) {
303 AssertEquals("#D23", typeof(FormatException), e.GetType());
307 public void TestToByte() {
309 AssertEquals("#E01", (byte)1, Convert.ToByte(boolTrue));
310 AssertEquals("#E02", (byte)0, Convert.ToByte(boolFalse));
311 AssertEquals("#E03", tryByte, Convert.ToByte(tryByte));
312 AssertEquals("#E04", (byte)114, Convert.ToByte('r'));
313 AssertEquals("#E05", (byte)201, Convert.ToByte((decimal)200.6));
314 AssertEquals("#E06", (byte)125, Convert.ToByte((double)125.4));
315 AssertEquals("#E07", (byte)255, Convert.ToByte((short)255));
316 AssertEquals("#E08", (byte)254, Convert.ToByte((int)254));
317 AssertEquals("#E09", (byte)34, Convert.ToByte((long)34));
318 AssertEquals("#E10", (byte)1, Convert.ToByte((object)boolTrue));
319 AssertEquals("#E11", (byte)123, Convert.ToByte((float)123.49f));
320 AssertEquals("#E12", (byte)57, Convert.ToByte("57"));
321 AssertEquals("#E13", (byte)75, Convert.ToByte((ushort)75));
322 AssertEquals("#E14", (byte)184, Convert.ToByte((uint)184));
323 AssertEquals("#E15", (byte)241, Convert.ToByte((ulong)241));
324 AssertEquals("#E16", (byte)123, Convert.ToByte(trySByte, ci));
325 AssertEquals("#E17", (byte)27, Convert.ToByte("011011", 2));
326 AssertEquals("#E18", (byte)13, Convert.ToByte("15", 8));
327 AssertEquals("#E19", (byte)27, Convert.ToByte("27", 10));
328 AssertEquals("#E20", (byte)250, Convert.ToByte("FA", 16));
331 Convert.ToByte('\u03A9'); // sign of Omega on Win2k
334 catch (Exception e) {
335 AssertEquals("#E25", typeof(OverflowException), e.GetType());
339 Convert.ToByte(tryDT);
342 catch (Exception e) {
343 AssertEquals("#D26", typeof(InvalidCastException), e.GetType());
347 Convert.ToByte((decimal)22000);
350 catch (Exception e) {
351 AssertEquals("#E27", typeof(OverflowException), e.GetType());
355 Convert.ToByte((double)255.5);
358 catch (Exception e) {
359 AssertEquals("#E28", typeof(OverflowException), e.GetType());
363 Convert.ToByte(-tryInt16);
366 catch (Exception e) {
367 AssertEquals("#E29", typeof(OverflowException), e.GetType());
371 Convert.ToByte((int)-256);
374 catch (Exception e) {
375 AssertEquals("#E30", typeof(OverflowException), e.GetType());
379 Convert.ToByte(tryInt64);
382 catch (Exception e) {
383 AssertEquals("#E31", typeof(OverflowException), e.GetType());
387 Convert.ToByte((object)ci);
390 catch (Exception e) {
391 AssertEquals("#E32", typeof(InvalidCastException), e.GetType());
395 Convert.ToByte((sbyte)-1);
398 catch (Exception e) {
399 AssertEquals("#E33", typeof(OverflowException), e.GetType());
403 Convert.ToByte((float)-0.6f);
406 catch (Exception e) {
407 AssertEquals("#E34", typeof(OverflowException), e.GetType());
411 Convert.ToByte("1a1");
414 catch (Exception e) {
415 AssertEquals("#E35", typeof(FormatException), e.GetType());
419 Convert.ToByte("457");
422 catch (Exception e) {
423 AssertEquals("#E36", typeof(OverflowException), e.GetType());
427 Convert.ToByte((ushort)30000);
430 catch (Exception e) {
431 AssertEquals("#E37", typeof(OverflowException), e.GetType());
435 Convert.ToByte((uint)300);
438 catch (Exception e) {
439 AssertEquals("#E38", typeof(OverflowException), e.GetType());
443 Convert.ToByte((ulong)987654321321);
446 catch (Exception e) {
447 AssertEquals("#E39", typeof(OverflowException), e.GetType());
451 Convert.ToByte("10010111", 3);
454 catch (Exception e) {
455 AssertEquals("#E40", typeof(ArgumentException), e.GetType());
459 Convert.ToByte("3F3", 16);
462 catch (Exception e) {
463 AssertEquals("#E41", typeof(OverflowException), e.GetType());
467 public void TestToChar(){
469 AssertEquals("#F01", ':', Convert.ToChar(tryByte));
470 AssertEquals("#F02", 'a', Convert.ToChar(tryChar));
471 AssertEquals("#F03", 'A', Convert.ToChar((short)65));
472 AssertEquals("#F04", 'x', Convert.ToChar((int)120));
473 AssertEquals("#F05", '"', Convert.ToChar((long)34));
474 AssertEquals("#F06", '-', Convert.ToChar((sbyte)45));
475 AssertEquals("#F07", '@', Convert.ToChar("@"));
476 AssertEquals("#F08", 'K', Convert.ToChar((ushort)75));
477 AssertEquals("#F09", '=', Convert.ToChar((uint)61));
478 // AssertEquals("#F10", 'E', Convert.ToChar((ulong)200));
479 AssertEquals("#F11", '{', Convert.ToChar((object)trySByte, ci));
480 AssertEquals("#F12", 'o', Convert.ToChar(tryStr.Substring(1,1), ci));
483 Convert.ToChar(boolTrue);
486 catch (Exception e) {
487 AssertEquals("#F20", typeof(InvalidCastException), e.GetType());
491 Convert.ToChar(tryDT);
494 catch (Exception e) {
495 AssertEquals("#F21", typeof(InvalidCastException), e.GetType());
499 Convert.ToChar(tryDec);
502 catch (Exception e) {
503 AssertEquals("#F22", typeof(InvalidCastException), e.GetType());
507 Convert.ToChar(tryDbl);
510 catch (Exception e) {
511 AssertEquals("#F23", typeof(InvalidCastException), e.GetType());
515 Convert.ToChar((short)-1);
518 catch (Exception e) {
519 AssertEquals("#F24", typeof(OverflowException), e.GetType());
523 Convert.ToChar(Int32.MinValue);
526 catch (Exception e) {
527 AssertEquals("#F25", typeof(OverflowException), e.GetType());
531 Convert.ToChar(Int32.MaxValue);
534 catch (Exception e) {
535 AssertEquals("#F26", typeof(OverflowException), e.GetType());
539 Convert.ToChar(tryInt64);
542 catch (Exception e) {
543 AssertEquals("#F27", typeof(OverflowException), e.GetType());
547 Convert.ToChar((long)-123);
550 catch (Exception e) {
551 AssertEquals("#F28", typeof(OverflowException), e.GetType());
558 catch (Exception e) {
559 AssertEquals("#F29", typeof(InvalidCastException), e.GetType());
563 Convert.ToChar(-trySByte);
566 catch (Exception e) {
567 AssertEquals("#F30", typeof(OverflowException), e.GetType());
571 Convert.ToChar(tryFloat);
574 catch (Exception e) {
575 AssertEquals("#F31", typeof(InvalidCastException), e.GetType());
579 Convert.ToChar("foo");
582 catch (Exception e) {
583 AssertEquals("#F32", typeof(FormatException), e.GetType());
587 Convert.ToChar(null);
590 catch (Exception e) {
591 AssertEquals("#F33", typeof(ArgumentNullException), e.GetType());
595 Convert.ToChar(new Exception(), ci);
598 catch (Exception e) {
599 AssertEquals("#F34", typeof(InvalidCastException), e.GetType());
603 Convert.ToChar(null, ci);
606 catch (Exception e) {
607 AssertEquals("#F35", typeof(ArgumentNullException), e.GetType());
611 Convert.ToChar("", ci);
614 catch (Exception e) {
615 AssertEquals("#F36", typeof(FormatException), e.GetType());
619 Convert.ToChar(tryStr, ci);
622 catch (Exception e) {
623 AssertEquals("#F37", typeof(FormatException), e.GetType());
627 /*[Ignore ("http://bugzilla.ximian.com/show_bug.cgi?id=45286")]
629 [ExpectedException (typeof (ArgumentOutOfRangeException))]
631 Convert.ToDateTime("20002-25-01");
634 public void TestToDateTime() {
635 string dateString = "01/01/2002";
638 AssertEquals("#G01", tryDT, Convert.ToDateTime(tryDT));
640 AssertEquals("#G02", tryDT, Convert.ToDateTime(dateString));
642 AssertEquals("#G03", tryDT, Convert.ToDateTime(dateString, ci));
643 } catch (Exception e) {
644 Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
648 Convert.ToDateTime(boolTrue);
651 catch (Exception e) {
652 AssertEquals("#G10", typeof(InvalidCastException), e.GetType());
656 Convert.ToDateTime(tryByte);
659 catch (Exception e) {
660 AssertEquals("#G11", typeof(InvalidCastException), e.GetType());
664 Convert.ToDateTime(tryChar);
667 catch (Exception e) {
668 AssertEquals("#G12", typeof(InvalidCastException), e.GetType());
672 Convert.ToDateTime(tryDec);
675 catch (Exception e) {
676 AssertEquals("#G13", typeof(InvalidCastException), e.GetType());
680 Convert.ToDateTime(tryDbl);
683 catch (Exception e) {
684 AssertEquals("#G14", typeof(InvalidCastException), e.GetType());
688 Convert.ToDateTime(tryInt16);
691 catch (Exception e) {
692 AssertEquals("#G15", typeof(InvalidCastException), e.GetType());
696 Convert.ToDateTime(tryInt32);
699 catch (Exception e) {
700 AssertEquals("#G16", typeof(InvalidCastException), e.GetType());
704 Convert.ToDateTime(tryInt64);
707 catch (Exception e) {
708 AssertEquals("#G17", typeof(InvalidCastException), e.GetType());
712 Convert.ToDateTime(ci);
715 catch (Exception e) {
716 AssertEquals("#G18", typeof(InvalidCastException), e.GetType());
720 Convert.ToDateTime(trySByte);
723 catch (Exception e) {
724 AssertEquals("#G19", typeof(InvalidCastException), e.GetType());
728 Convert.ToDateTime(tryFloat);
731 catch (Exception e) {
732 AssertEquals("#G20", typeof(InvalidCastException), e.GetType());
736 Convert.ToDateTime("20a2-01-01");
739 catch (Exception e) {
740 AssertEquals("#G21", typeof(FormatException), e.GetType());
744 Convert.ToDateTime(tryUI16);
747 catch (Exception e) {
748 AssertEquals("#G23", typeof(InvalidCastException), e.GetType());
752 Convert.ToDateTime(tryUI32);
755 catch (Exception e) {
756 AssertEquals("#G24", typeof(InvalidCastException), e.GetType());
760 Convert.ToDateTime(tryUI64);
763 catch (Exception e) {
764 AssertEquals("#G25", typeof(InvalidCastException), e.GetType());
768 Convert.ToDateTime(ci, ci);
771 catch (Exception e) {
772 AssertEquals("#G26", typeof(InvalidCastException), e.GetType());
776 Convert.ToDateTime("20a2-01-01", ci);
779 catch (Exception e) {
780 AssertEquals("#G27", typeof(FormatException), e.GetType());
783 // this is supported by .net 1.1 (defect 41845)
785 Convert.ToDateTime("20022-01-01");
788 catch (Exception e) {
790 AssertEquals("#G28", typeof(FormatException), e.GetType());
792 AssertEquals("#G28", typeof(ArgumentOutOfRangeException), e.GetType());
797 Convert.ToDateTime("2002-21-01");
800 catch (Exception e) {
801 AssertEquals("#G29", typeof(FormatException), e.GetType());
805 Convert.ToDateTime("2002-111-01");
808 catch (Exception e) {
809 AssertEquals("#G30", typeof(FormatException), e.GetType());
813 Convert.ToDateTime("2002-01-41");
816 catch (Exception e) {
817 AssertEquals("#G31", typeof(FormatException), e.GetType());
821 Convert.ToDateTime("2002-01-111");
824 catch (Exception e) {
825 AssertEquals("#G32", typeof(FormatException), e.GetType());
829 AssertEquals("#G33", tryDT, Convert.ToDateTime("2002-01-01"));
830 } catch (Exception e) {
831 Fail ("Unexpected exception at #G33 " + e);
835 Convert.ToDateTime("2002-01-11 34:11:11");
838 catch (Exception e) {
839 AssertEquals("#G34", typeof(FormatException), e.GetType());
843 Convert.ToDateTime("2002-01-11 11:70:11");
846 catch (Exception e) {
847 AssertEquals("#G35", typeof(FormatException), e.GetType());
851 Convert.ToDateTime("2002-01-11 11:11:70");
854 catch (Exception e) {
855 AssertEquals("#G36", typeof(FormatException), e.GetType());
860 public void TestToDecimal() {
861 AssertEquals("#H01", (decimal)1, Convert.ToDecimal(boolTrue));
862 AssertEquals("#H02", (decimal)0, Convert.ToDecimal(boolFalse));
863 AssertEquals("#H03", (decimal)tryByte, Convert.ToDecimal(tryByte));
864 AssertEquals("#H04", tryDec, Convert.ToDecimal(tryDec));
865 AssertEquals("#H05", (decimal)tryDbl, Convert.ToDecimal(tryDbl));
866 AssertEquals("#H06", (decimal)tryInt16, Convert.ToDecimal(tryInt16));
867 AssertEquals("#H07", (decimal)tryInt32, Convert.ToDecimal(tryInt32));
868 AssertEquals("#H08", (decimal)tryInt64, Convert.ToDecimal(tryInt64));
869 AssertEquals("#H09", (decimal)trySByte, Convert.ToDecimal(trySByte));
870 AssertEquals("#H10", (decimal)tryFloat, Convert.ToDecimal(tryFloat));
871 string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
872 // AssertEquals("#H11", (decimal)23456.432, Convert.ToDecimal("23456" + sep + "432"));
873 // Note: changed because the number were the same but with a different base
874 // and this isn't a Convert bug (but a Decimal bug). See #60227 for more details.
875 // http://bugzilla.ximian.com/show_bug.cgi?id=60227
876 Assert ("#H11", Decimal.Equals (23456.432m, Convert.ToDecimal ("23456" + sep + "432")));
877 AssertEquals("#H12", (decimal)tryUI16, Convert.ToDecimal(tryUI16));
878 AssertEquals("#H13", (decimal)tryUI32, Convert.ToDecimal(tryUI32));
879 AssertEquals("#H14", (decimal)tryUI64, Convert.ToDecimal(tryUI64));
880 AssertEquals("#H15", (decimal)63784, Convert.ToDecimal("63784", ci));
883 Convert.ToDecimal(tryChar);
886 catch (Exception e) {
887 AssertEquals("#H20", typeof(InvalidCastException), e.GetType());
891 Convert.ToDecimal(tryDT);
894 catch (Exception e) {
895 AssertEquals("#H21", typeof(InvalidCastException), e.GetType());
899 Convert.ToDecimal(double.MaxValue);
902 catch (Exception e) {
903 AssertEquals("#H22", typeof(OverflowException), e.GetType());
907 Convert.ToDecimal(double.MinValue);
910 catch (Exception e) {
911 AssertEquals("#H23", typeof(OverflowException), e.GetType());
915 Convert.ToDecimal(ci);
918 catch (Exception e) {
919 AssertEquals("#H24", typeof(InvalidCastException), e.GetType());
923 Convert.ToDecimal(tryStr);
926 catch (Exception e) {
927 AssertEquals("#H25", typeof(FormatException), e.GetType());
931 string maxDec = decimal.MaxValue.ToString();
932 maxDec = maxDec + "1";
933 Convert.ToDecimal(maxDec);
936 catch (Exception e) {
937 AssertEquals("#H26", typeof(OverflowException), e.GetType());
941 Convert.ToDecimal(ci, ci);
944 catch (Exception e) {
945 AssertEquals("#H27", typeof(InvalidCastException), e.GetType());
949 Convert.ToDecimal(tryStr, ci);
952 catch (Exception e) {
953 AssertEquals("#H28", typeof(FormatException), e.GetType());
957 string maxDec = decimal.MaxValue.ToString();
958 maxDec = maxDec + "1";
959 Convert.ToDecimal(maxDec, ci);
962 catch (Exception e) {
963 AssertEquals("#H29", typeof(OverflowException), e.GetType());
967 public void TestToDouble() {
970 AssertEquals("#I01", (double)1, Convert.ToDouble(boolTrue));
972 AssertEquals("#I02", (double)0, Convert.ToDouble(boolFalse));
974 AssertEquals("#I03", (double)tryByte, Convert.ToDouble(tryByte));
976 AssertEquals("#I04", tryDbl, Convert.ToDouble(tryDbl));
978 AssertEquals("#I05", (double)tryDec, Convert.ToDouble(tryDec));
980 AssertEquals("#I06", (double)tryInt16, Convert.ToDouble(tryInt16));
982 AssertEquals("#I07", (double)tryInt32, Convert.ToDouble(tryInt32));
984 AssertEquals("#I08", (double)tryInt64, Convert.ToDouble(tryInt64));
986 AssertEquals("#I09", (double)trySByte, Convert.ToDouble(trySByte));
988 AssertEquals("#I10", (double)tryFloat, Convert.ToDouble(tryFloat));
990 string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
991 AssertEquals("#I11", (double)23456.432, Convert.ToDouble("23456" + sep + "432"));
993 AssertEquals("#I12", (double)tryUI16, Convert.ToDouble(tryUI16));
995 AssertEquals("#I13", (double)tryUI32, Convert.ToDouble(tryUI32));
997 AssertEquals("#I14", (double)tryUI64, Convert.ToDouble(tryUI64));
999 AssertEquals("#H15", (double)63784, Convert.ToDouble("63784", ci));
1000 } catch (Exception e) {
1001 Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1005 Convert.ToDouble(tryChar);
1008 catch (Exception e) {
1009 AssertEquals("#I20", typeof(InvalidCastException), e.GetType());
1013 Convert.ToDouble(tryDT);
1016 catch (Exception e) {
1017 AssertEquals("#I21", typeof(InvalidCastException), e.GetType());
1021 Convert.ToDouble(ci);
1024 catch (Exception e) {
1025 AssertEquals("#I22", typeof(InvalidCastException), e.GetType());
1029 Convert.ToDouble(tryStr);
1032 catch (Exception e) {
1033 AssertEquals("#I23", typeof(FormatException), e.GetType());
1037 string maxDec = double.MaxValue.ToString();
1038 maxDec = maxDec + "1";
1039 Convert.ToDouble(maxDec);
1042 catch (Exception e) {
1043 AssertEquals("#I24", typeof(OverflowException), e.GetType());
1047 Convert.ToDouble(ci, ci);
1050 catch (Exception e) {
1051 AssertEquals("#I25", typeof(InvalidCastException), e.GetType());
1055 Convert.ToDouble(tryStr, ci);
1058 catch (Exception e) {
1059 AssertEquals("#I26", typeof(FormatException), e.GetType());
1063 string maxDec = double.MaxValue.ToString();
1064 maxDec = maxDec + "1";
1065 Convert.ToDouble(maxDec, ci);
1068 catch (Exception e) {
1069 AssertEquals("#I27", typeof(OverflowException), e.GetType());
1073 Convert.ToDouble(tryObj, ci);
1076 catch (Exception e) {
1077 AssertEquals("#I28", typeof(InvalidCastException), e.GetType());
1081 public void TestToInt16() {
1082 AssertEquals("#J01", (short)0, Convert.ToInt16(boolFalse));
1083 AssertEquals("#J02", (short)1, Convert.ToInt16(boolTrue));
1084 AssertEquals("#J03", (short)97, Convert.ToInt16(tryChar));
1085 AssertEquals("#J04", (short)1234, Convert.ToInt16(tryDec));
1086 AssertEquals("#J05", (short)0, Convert.ToInt16(tryDbl));
1087 AssertEquals("#J06", (short)1234, Convert.ToInt16(tryInt16));
1088 AssertEquals("#J07", (short)12345, Convert.ToInt16(tryInt32));
1089 AssertEquals("#J08", (short)30000, Convert.ToInt16((long)30000));
1090 AssertEquals("#J09", (short)123, Convert.ToInt16(trySByte));
1091 AssertEquals("#J10", (short)1234, Convert.ToInt16(tryFloat));
1092 AssertEquals("#J11", (short)578, Convert.ToInt16("578"));
1093 AssertEquals("#J12", (short)15500, Convert.ToInt16((ushort)15500));
1094 AssertEquals("#J13", (short)5489, Convert.ToInt16((uint)5489));
1095 AssertEquals("#J14", (short)9876, Convert.ToInt16((ulong)9876));
1096 AssertEquals("#J15", (short)14, Convert.ToInt16("14", ci));
1097 AssertEquals("#J16", (short)11, Convert.ToInt16("01011", 2));
1098 AssertEquals("#J17", (short)1540, Convert.ToInt16("3004", 8));
1099 AssertEquals("#J18", (short)321, Convert.ToInt16("321", 10));
1100 AssertEquals("#J19", (short)2748, Convert.ToInt16("ABC", 16));
1103 Convert.ToInt16(char.MaxValue);
1106 catch (Exception e) {
1107 AssertEquals("#J25", typeof(OverflowException), e.GetType());
1111 Convert.ToInt16(tryDT);
1114 catch (Exception e) {
1115 AssertEquals("#J26", typeof(InvalidCastException), e.GetType());
1119 Convert.ToInt16((decimal)(short.MaxValue + 1));
1122 catch (Exception e) {
1123 AssertEquals("#J27", typeof(OverflowException), e.GetType());
1127 Convert.ToInt16((decimal)(short.MinValue - 1));
1130 catch (Exception e) {
1131 AssertEquals("#J28", typeof(OverflowException), e.GetType());
1135 Convert.ToInt16((double)(short.MaxValue + 1));
1138 catch (Exception e) {
1139 AssertEquals("#J29", typeof(OverflowException), e.GetType());
1143 Convert.ToInt16((double)(short.MinValue - 1));
1146 catch (Exception e) {
1147 AssertEquals("#J30", typeof(OverflowException), e.GetType());
1151 Convert.ToInt16(50000);
1154 catch (Exception e) {
1155 AssertEquals("#J31", typeof(OverflowException), e.GetType());
1159 Convert.ToInt16(-50000);
1162 catch (Exception e) {
1163 AssertEquals("#J32", typeof(OverflowException), e.GetType());
1167 Convert.ToInt16(tryInt64);
1170 catch (Exception e) {
1171 AssertEquals("#J33", typeof(OverflowException), e.GetType());
1175 Convert.ToInt16(-tryInt64);
1178 catch (Exception e) {
1179 AssertEquals("#J34", typeof(OverflowException), e.GetType());
1183 Convert.ToInt16(tryObj);
1186 catch (Exception e) {
1187 AssertEquals("#J35", typeof(InvalidCastException), e.GetType());
1191 Convert.ToInt16((float)32767.5);
1194 catch (Exception e) {
1195 AssertEquals("#J36", typeof(OverflowException), e.GetType());
1199 Convert.ToInt16((float)-33000.54);
1202 catch (Exception e) {
1203 AssertEquals("#J37", typeof(OverflowException), e.GetType());
1207 Convert.ToInt16(tryStr);
1210 catch (Exception e) {
1211 AssertEquals("#J38", typeof(FormatException), e.GetType());
1215 Convert.ToInt16("-33000");
1218 catch (Exception e) {
1219 AssertEquals("#J39", typeof(OverflowException), e.GetType());
1223 Convert.ToInt16(ushort.MaxValue);
1226 catch (Exception e) {
1227 AssertEquals("#J40", typeof(OverflowException), e.GetType());
1231 Convert.ToInt16(uint.MaxValue);
1234 catch (Exception e) {
1235 AssertEquals("#J41", typeof(OverflowException), e.GetType());
1239 Convert.ToInt16(ulong.MaxValue);
1242 catch (Exception e) {
1243 AssertEquals("#J42", typeof(OverflowException), e.GetType());
1247 Convert.ToInt16(tryObj, ci);
1250 catch (Exception e) {
1251 AssertEquals("#J43", typeof(InvalidCastException), e.GetType());
1255 Convert.ToInt16(tryStr, ci);
1258 catch (Exception e) {
1259 AssertEquals("#J44", typeof(FormatException), e.GetType());
1263 Convert.ToInt16("-33000", ci);
1266 catch (Exception e) {
1267 AssertEquals("#J45", typeof(OverflowException), e.GetType());
1271 Convert.ToInt16("321", 11);
1274 catch (Exception e) {
1275 AssertEquals("#J46", typeof(ArgumentException), e.GetType());
1279 Convert.ToInt16("D8BF1", 16);
1282 catch (Exception e) {
1283 AssertEquals("#J47", typeof(OverflowException), e.GetType());
1287 public void TestToInt32() {
1288 long tryMax = long.MaxValue;
1289 long tryMin = long.MinValue;
1290 AssertEquals("#K01", (int)0, Convert.ToInt32(boolFalse));
1291 AssertEquals("#K02", (int)1, Convert.ToInt32(boolTrue));
1292 AssertEquals("#K03", (int)0, Convert.ToInt32(tryByte));
1293 AssertEquals("#K04", (int)97, Convert.ToInt32(tryChar));
1294 AssertEquals("#K05", (int)1234, Convert.ToInt32(tryDec));
1295 AssertEquals("#K06", (int)0, Convert.ToInt32(tryDbl));
1296 AssertEquals("#K07", (int)1234, Convert.ToInt32(tryInt16));
1297 AssertEquals("#K08", (int)12345, Convert.ToInt32(tryInt32));
1298 AssertEquals("#K09", (int)60000, Convert.ToInt32((long)60000));
1299 AssertEquals("#K10", (int)123, Convert.ToInt32(trySByte));
1300 AssertEquals("#K11", (int)1234, Convert.ToInt32(tryFloat));
1301 AssertEquals("#K12", (int)9876, Convert.ToInt32((string)"9876"));
1302 AssertEquals("#K13", (int)34567, Convert.ToInt32(tryUI16));
1303 AssertEquals("#K14", (int)567891234, Convert.ToInt32(tryUI32));
1304 AssertEquals("#K15", (int)0, Convert.ToInt32(tryUI64));
1305 AssertEquals("#K16", (int)123, Convert.ToInt32("123", ci));
1306 AssertEquals("#K17", (int)128, Convert.ToInt32("10000000", 2));
1307 AssertEquals("#K18", (int)302, Convert.ToInt32("456", 8));
1308 AssertEquals("#K19", (int)456, Convert.ToInt32("456", 10));
1309 AssertEquals("#K20", (int)1110, Convert.ToInt32("456", 16));
1312 Convert.ToInt32(tryDT);
1315 catch (Exception e) {
1316 AssertEquals("#K25", typeof(InvalidCastException), e.GetType());
1320 Convert.ToInt32((decimal)tryMax);
1323 catch (Exception e) {
1324 AssertEquals("#K26", typeof(OverflowException), e.GetType());
1328 Convert.ToInt32((decimal)tryMin);
1331 catch (Exception e) {
1332 AssertEquals("#K27", typeof(OverflowException), e.GetType());
1336 Convert.ToInt32((double)tryMax);
1339 catch (Exception e) {
1340 AssertEquals("#K28", typeof(OverflowException), e.GetType());
1344 Convert.ToInt32((double)tryMin);
1347 catch (Exception e) {
1348 AssertEquals("#K29", typeof(OverflowException), e.GetType());
1352 Convert.ToInt32(tryInt64);
1355 catch (Exception e) {
1356 AssertEquals("#K30", typeof(OverflowException), e.GetType());
1360 Convert.ToInt32(-tryInt64);
1363 catch (Exception e) {
1364 AssertEquals("#K31", typeof(OverflowException), e.GetType());
1368 Convert.ToInt32(tryObj);
1371 catch (Exception e) {
1372 AssertEquals("#K32", typeof(InvalidCastException), e.GetType());
1376 Convert.ToInt32((float)tryMax);
1379 catch (Exception e) {
1380 AssertEquals("#K33", typeof(OverflowException), e.GetType());
1384 Convert.ToInt32((float)tryMin);
1387 catch (Exception e) {
1388 AssertEquals("#K34", typeof(OverflowException), e.GetType());
1392 Convert.ToInt32(tryStr, ci);
1395 catch (Exception e) {
1396 AssertEquals("#K35", typeof(FormatException), e.GetType());
1400 Convert.ToInt32("-46565465123");
1403 catch (Exception e) {
1404 AssertEquals("#K36", typeof(OverflowException), e.GetType());
1408 Convert.ToInt32("46565465123");
1411 catch (Exception e) {
1412 AssertEquals("#K37", typeof(OverflowException), e.GetType());
1416 Convert.ToInt32((uint)tryMax);
1419 catch (Exception e) {
1420 AssertEquals("#K38", typeof(OverflowException), e.GetType());
1424 Convert.ToInt32((ulong)tryMax);
1427 catch (Exception e) {
1428 AssertEquals("#K39", typeof(OverflowException), e.GetType());
1432 Convert.ToInt32(tryObj, ci);
1435 catch (Exception e) {
1436 AssertEquals("#K40", typeof(InvalidCastException), e.GetType());
1440 Convert.ToInt32(tryStr, ci);
1443 catch (Exception e) {
1444 AssertEquals("#K41", typeof(FormatException), e.GetType());
1448 Convert.ToInt32("-46565465123", ci);
1451 catch (Exception e) {
1452 AssertEquals("#K42", typeof(OverflowException), e.GetType());
1456 Convert.ToInt32("654", 9);
1459 catch (Exception e) {
1460 AssertEquals("#K43", typeof(ArgumentException), e.GetType());
1464 public void TestToInt64() {
1465 decimal longMax = long.MaxValue;
1467 decimal longMin = long.MinValue;
1470 AssertEquals("#L01", (long)0, Convert.ToInt64(boolFalse));
1471 AssertEquals("#L02", (long)1, Convert.ToInt64(boolTrue));
1472 AssertEquals("#L03", (long)97, Convert.ToInt64(tryChar));
1473 AssertEquals("#L04", (long)1234, Convert.ToInt64(tryDec));
1474 AssertEquals("#L05", (long)0, Convert.ToInt64(tryDbl));
1475 AssertEquals("#L06", (long)1234, Convert.ToInt64(tryInt16));
1476 AssertEquals("#L07", (long)12345, Convert.ToInt64(tryInt32));
1477 AssertEquals("#L08", (long)123456789012, Convert.ToInt64(tryInt64));
1478 AssertEquals("#L09", (long)123, Convert.ToInt64(trySByte));
1479 AssertEquals("#L10", (long)1234, Convert.ToInt64(tryFloat));
1480 AssertEquals("#L11", (long)564897, Convert.ToInt64("564897"));
1481 AssertEquals("#L12", (long)34567, Convert.ToInt64(tryUI16));
1482 AssertEquals("#L13", (long)567891234, Convert.ToInt64(tryUI32));
1483 AssertEquals("#L14", (long)0, Convert.ToInt64(tryUI64));
1484 AssertEquals("#L15", (long)-2548751, Convert.ToInt64("-2548751", ci));
1485 AssertEquals("#L16", (long)24987562, Convert.ToInt64("1011111010100011110101010", 2));
1486 AssertEquals("#L17", (long)-24578965, Convert.ToInt64("1777777777777642172153", 8));
1487 AssertEquals("#L18", (long)248759757, Convert.ToInt64("248759757", 10));
1488 AssertEquals("#L19", (long)256, Convert.ToInt64("100", 16));
1491 Convert.ToInt64(tryDT);
1494 catch (Exception e) {
1495 AssertEquals("#L20", typeof(InvalidCastException), e.GetType());
1499 Convert.ToInt64((decimal)longMax + 1);
1502 catch (Exception e) {
1503 AssertEquals("#L21", typeof(OverflowException), e.GetType());
1507 Convert.ToInt64((decimal)longMin);
1510 catch (Exception e) {
1511 AssertEquals("#L24", typeof(OverflowException), e.GetType());
1515 Convert.ToInt64((double)longMax);
1518 catch (Exception e) {
1519 AssertEquals("#L25:"+longMax, typeof(OverflowException), e.GetType());
1523 Convert.ToInt64((double)longMin);
1526 catch (Exception e) {
1527 AssertEquals("#L26", typeof(OverflowException), e.GetType());
1531 Convert.ToInt64(new Exception());
1534 catch (Exception e) {
1535 AssertEquals("#L27", typeof(InvalidCastException), e.GetType());
1539 Convert.ToInt64(((float)longMax)*100);
1542 catch (Exception e) {
1543 AssertEquals("#L28:"+longMax, typeof(OverflowException), e.GetType());
1547 Convert.ToInt64(((float)longMin)*100);
1550 catch (Exception e) {
1551 AssertEquals("#L29", typeof(OverflowException), e.GetType());
1555 Convert.ToInt64("-567b3");
1558 catch (Exception e) {
1559 AssertEquals("#L30", typeof(FormatException), e.GetType());
1563 Convert.ToInt64(longMax.ToString());
1566 catch (Exception e) {
1567 AssertEquals("#L31:", typeof(OverflowException), e.GetType());
1571 Convert.ToInt64(ulong.MaxValue);
1574 catch (Exception e) {
1575 AssertEquals("#L32", typeof(OverflowException), e.GetType());
1579 Convert.ToInt64(tryStr, ci);
1582 catch (Exception e) {
1583 AssertEquals("#L32b", typeof(FormatException), e.GetType());
1587 Convert.ToInt64(longMin.ToString(), ci);
1590 catch (Exception e) {
1591 AssertEquals("#L33", typeof(OverflowException), e.GetType());
1595 Convert.ToInt64("321", 11);
1598 catch (Exception e) {
1599 AssertEquals("#L34", typeof(ArgumentException), e.GetType());
1603 public void TestToSByte() {
1606 AssertEquals("#M01", (sbyte)0, Convert.ToSByte(boolFalse));
1608 AssertEquals("#M02", (sbyte)1, Convert.ToSByte(boolTrue));
1610 AssertEquals("#M03", (sbyte)97, Convert.ToSByte(tryChar));
1612 AssertEquals("#M04", (sbyte)15, Convert.ToSByte((decimal)15));
1614 AssertEquals("#M05", (sbyte)0, Convert.ToSByte(tryDbl));
1616 AssertEquals("#M06", (sbyte)127, Convert.ToSByte((short)127));
1618 AssertEquals("#M07", (sbyte)-128, Convert.ToSByte((int)-128));
1620 AssertEquals("#M08", (sbyte)30, Convert.ToSByte((long)30));
1622 AssertEquals("#M09", (sbyte)123, Convert.ToSByte(trySByte));
1624 AssertEquals("#M10", (sbyte)12, Convert.ToSByte((float)12.46987f));
1626 AssertEquals("#M11", (sbyte)1, Convert.ToSByte("1"));
1628 AssertEquals("#M12", (sbyte)99, Convert.ToSByte((ushort)99));
1630 AssertEquals("#M13", (sbyte)54, Convert.ToSByte((uint)54));
1632 AssertEquals("#M14", (sbyte)127, Convert.ToSByte((ulong)127));
1634 AssertEquals("#M15", (sbyte)14, Convert.ToSByte("14", ci));
1636 AssertEquals("#M16", (sbyte)11, Convert.ToSByte("01011", 2));
1638 AssertEquals("#M17", (sbyte)5, Convert.ToSByte("5", 8));
1640 AssertEquals("#M18", (sbyte)100, Convert.ToSByte("100", 10));
1642 AssertEquals("#M19", (sbyte)-1, Convert.ToSByte("FF", 16));
1643 } catch (Exception e) {
1644 Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1648 Convert.ToSByte((byte)200);
1651 catch (Exception e) {
1652 AssertEquals("#M25", typeof(OverflowException), e.GetType());
1656 Convert.ToSByte((char)130);
1659 catch (Exception e) {
1660 AssertEquals("#M26", typeof(OverflowException), e.GetType());
1664 Convert.ToSByte(tryDT);
1667 catch (Exception e) {
1668 AssertEquals("#M27", typeof(InvalidCastException), e.GetType());
1672 Convert.ToSByte((decimal)127.5m);
1675 catch (Exception e) {
1676 AssertEquals("#M28", typeof(OverflowException), e.GetType());
1680 Convert.ToSByte((decimal)-200m);
1683 catch (Exception e) {
1684 AssertEquals("#M29", typeof(OverflowException), e.GetType());
1688 Convert.ToSByte((double)150);
1691 catch (Exception e) {
1692 AssertEquals("#M30", typeof(OverflowException), e.GetType());
1696 Convert.ToSByte((double)-128.6);
1699 catch (Exception e) {
1700 AssertEquals("#M31", typeof(OverflowException), e.GetType());
1704 Convert.ToSByte((short)150);
1707 catch (Exception e) {
1708 AssertEquals("#M32", typeof(OverflowException), e.GetType());
1712 Convert.ToSByte((short)-300);
1715 catch (Exception e) {
1716 AssertEquals("#M33", typeof(OverflowException), e.GetType());
1720 Convert.ToSByte((int)1500);
1723 catch (Exception e) {
1724 AssertEquals("#M34", typeof(OverflowException), e.GetType());
1728 Convert.ToSByte((int)-1286);
1731 catch (Exception e) {
1732 AssertEquals("#M35", typeof(OverflowException), e.GetType());
1736 Convert.ToSByte((long)128);
1739 catch (Exception e) {
1740 AssertEquals("#M36", typeof(OverflowException), e.GetType());
1744 Convert.ToSByte((long)-129);
1747 catch (Exception e) {
1748 AssertEquals("#M37", typeof(OverflowException), e.GetType());
1752 Convert.ToSByte(new NumberFormatInfo());
1755 catch (Exception e) {
1756 AssertEquals("#M38", typeof(InvalidCastException), e.GetType());
1760 Convert.ToSByte((float)333);
1763 catch (Exception e) {
1764 AssertEquals("#M39", typeof(OverflowException), e.GetType());
1768 Convert.ToSByte((float)-666);
1771 catch (Exception e) {
1772 AssertEquals("#M40", typeof(OverflowException), e.GetType());
1776 Convert.ToSByte("B3");
1779 catch (Exception e) {
1780 AssertEquals("#M41", typeof(FormatException), e.GetType());
1784 Convert.ToSByte("251");
1787 catch (Exception e) {
1788 AssertEquals("#M42", typeof(OverflowException), e.GetType());
1792 Convert.ToSByte(ushort.MaxValue);
1795 catch (Exception e) {
1796 AssertEquals("#M43", typeof(OverflowException), e.GetType());
1800 Convert.ToSByte((uint)600);
1803 catch (Exception e) {
1804 AssertEquals("#M44", typeof(OverflowException), e.GetType());
1808 Convert.ToSByte(ulong.MaxValue);
1811 catch (Exception e) {
1812 AssertEquals("#M45", typeof(OverflowException), e.GetType());
1816 Convert.ToSByte(ci, ci);
1819 catch (Exception e) {
1820 AssertEquals("#M46", typeof(InvalidCastException), e.GetType());
1824 Convert.ToSByte(tryStr, ci);
1827 catch (Exception e) {
1828 AssertEquals("#M47", typeof(FormatException), e.GetType());
1832 Convert.ToSByte("325", ci);
1835 catch (Exception e) {
1836 AssertEquals("#M48", typeof(OverflowException), e.GetType());
1840 Convert.ToSByte("5D", 15);
1843 catch (Exception e) {
1844 AssertEquals("#M49", typeof(ArgumentException), e.GetType());
1848 Convert.ToSByte("111111111", 2);
1851 catch (Exception e) {
1852 AssertEquals("#M50", typeof(OverflowException), e.GetType());
1856 public void TestToSingle() {
1859 AssertEquals("#N01", (float)0, Convert.ToSingle(boolFalse));
1861 AssertEquals("#N02", (float)1, Convert.ToSingle(boolTrue));
1863 AssertEquals("#N03", (float)0, Convert.ToSingle(tryByte));
1865 AssertEquals("#N04", (float)1234,234, Convert.ToSingle(tryDec));
1867 AssertEquals("#N05", (float)0, Convert.ToSingle(tryDbl));
1869 AssertEquals("#N06", (float)1234, Convert.ToSingle(tryInt16));
1871 AssertEquals("#N07", (float)12345, Convert.ToSingle(tryInt32));
1873 AssertEquals("#N08", (float)123456789012, Convert.ToSingle(tryInt64));
1875 AssertEquals("#N09", (float)123, Convert.ToSingle(trySByte));
1877 AssertEquals("#N10", (float)1234,2345, Convert.ToSingle(tryFloat));
1879 AssertEquals("#N11", (float)987, Convert.ToSingle("987"));
1881 AssertEquals("#N12", (float)34567, Convert.ToSingle(tryUI16));
1883 AssertEquals("#N13", (float)567891234, Convert.ToSingle(tryUI32));
1885 AssertEquals("#N14", (float)0, Convert.ToSingle(tryUI64));
1887 AssertEquals("#N15", (float)654.234, Convert.ToSingle("654.234", ci));
1888 } catch (Exception e) {
1889 Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1893 Convert.ToSingle(tryChar);
1896 catch (Exception e) {
1897 AssertEquals("#N25", typeof(InvalidCastException), e.GetType());
1901 Convert.ToSingle(tryDT);
1904 catch (Exception e) {
1905 AssertEquals("#N26", typeof(InvalidCastException), e.GetType());
1909 Convert.ToSingle(tryObj);
1912 catch (Exception e) {
1913 AssertEquals("#N27", typeof(InvalidCastException), e.GetType());
1917 Convert.ToSingle("A345H");
1920 catch (Exception e) {
1921 AssertEquals("#N28", typeof(FormatException), e.GetType());
1925 Convert.ToSingle(double.MaxValue.ToString());
1928 catch (Exception e) {
1929 AssertEquals("#N29", typeof(OverflowException), e.GetType());
1933 Convert.ToSingle(tryObj, ci);
1936 catch (Exception e) {
1937 AssertEquals("#N30", typeof(InvalidCastException), e.GetType());
1941 Convert.ToSingle("J345K", ci);
1944 catch (Exception e) {
1945 AssertEquals("#N31", typeof(FormatException), e.GetType());
1949 Convert.ToSingle("11000000000000000000000000000000000000000000000", ci);
1952 catch (Exception e) {
1953 AssertEquals("#N32", typeof(OverflowException), e.GetType());
1957 public void TestToString() {
1960 AssertEquals("#O01", "False", Convert.ToString(boolFalse));
1961 AssertEquals("#O02", "True", Convert.ToString(boolTrue));
1962 AssertEquals("#O03", "123", Convert.ToString(tryByte));
1963 AssertEquals("#O04", "a", Convert.ToString(tryChar));
1964 AssertEquals("#O05", tryDT.ToString(), Convert.ToString(tryDT));
1965 AssertEquals("#O06", tryDec.ToString(), Convert.ToString(tryDec));
1966 AssertEquals("#O07", tryDbl.ToString(), Convert.ToString(tryDbl));
1967 AssertEquals("#O08", "1234", Convert.ToString(tryInt16));
1968 AssertEquals("#O09", "12345", Convert.ToString(tryInt32));
1969 AssertEquals("#O10", "123456789012", Convert.ToString(tryInt64));
1970 AssertEquals("#O11", "123", Convert.ToString(trySByte));
1971 AssertEquals("#O12", tryFloat.ToString(), Convert.ToString(tryFloat));
1972 AssertEquals("#O13", "foobar", Convert.ToString(tryStr));
1973 AssertEquals("#O14", "34567", Convert.ToString(tryUI16));
1974 AssertEquals("#O15", "567891234", Convert.ToString(tryUI32));
1975 AssertEquals("#O16", "True", Convert.ToString(boolTrue, ci));
1976 AssertEquals("#O17", "False", Convert.ToString(boolFalse, ci));
1977 AssertEquals("#O18", "123", Convert.ToString(tryByte, ci));
1978 AssertEquals("#O19", "1111011", Convert.ToString(tryByte, 2));
1979 AssertEquals("#O20", "173", Convert.ToString(tryByte, 8));
1980 AssertEquals("#O21", "123", Convert.ToString(tryByte, 10));
1981 AssertEquals("#O22", "7b", Convert.ToString(tryByte, 16));
1982 AssertEquals("#O23", "a", Convert.ToString(tryChar, ci));
1983 AssertEquals("#O24", tryDT.ToString(ci), Convert.ToString(tryDT, ci));
1984 AssertEquals("#O25", tryDec.ToString(ci), Convert.ToString(tryDec,ci));
1985 AssertEquals("#O26", tryDbl.ToString(ci), Convert.ToString(tryDbl, ci));
1986 AssertEquals("#O27", "1234", Convert.ToString(tryInt16, ci));
1987 AssertEquals("#O28", "10011010010", Convert.ToString(tryInt16, 2));
1988 AssertEquals("#O29", "2322", Convert.ToString(tryInt16, 8));
1989 AssertEquals("#O30", "1234", Convert.ToString(tryInt16, 10));
1990 AssertEquals("#O31", "4d2", Convert.ToString(tryInt16, 16));
1991 AssertEquals("#O32", "12345", Convert.ToString(tryInt32, ci));
1992 AssertEquals("#O33", "11000000111001", Convert.ToString(tryInt32, 2));
1993 AssertEquals("#O34", "30071", Convert.ToString(tryInt32, 8));
1994 AssertEquals("#O35", "12345", Convert.ToString(tryInt32, 10));
1995 AssertEquals("#O36", "3039", Convert.ToString(tryInt32, 16));
1996 AssertEquals("#O37", "123456789012", Convert.ToString(tryInt64, ci));
1997 AssertEquals("#O38", "1110010111110100110010001101000010100",
1998 Convert.ToString(tryInt64, 2));
1999 AssertEquals("#O39", "1627646215024", Convert.ToString(tryInt64, 8));
2000 AssertEquals("#O40", "123456789012", Convert.ToString(tryInt64, 10));
2001 AssertEquals("#O41", "1cbe991a14", Convert.ToString(tryInt64, 16));
2002 AssertEquals("#O42", "123", Convert.ToString((trySByte), ci));
2003 AssertEquals("#O43", tryFloat.ToString(ci), Convert.ToString((tryFloat), ci));
2004 AssertEquals("#O44", "foobar", Convert.ToString((tryStr), ci));
2005 AssertEquals("#O45", "34567", Convert.ToString((tryUI16), ci));
2006 AssertEquals("#O46", "567891234", Convert.ToString((tryUI32), ci));
2007 AssertEquals("#O47", "0", Convert.ToString(tryUI64));
2008 AssertEquals("#O48", "0", Convert.ToString((tryUI64), ci));
2011 Convert.ToString(tryInt16, 5);
2014 catch (Exception e) {
2015 AssertEquals("#O55", typeof(ArgumentException), e.GetType());
2019 Convert.ToString(tryInt32, 17);
2022 catch (Exception e) {
2023 AssertEquals("#O56", typeof(ArgumentException), e.GetType());
2027 Convert.ToString(tryInt64, 1);
2030 catch (Exception e) {
2031 AssertEquals("#O57", typeof(ArgumentException), e.GetType());
2035 public void TestToUInt16() {
2036 AssertEquals("#P01", (ushort)0, Convert.ToUInt16(boolFalse));
2037 AssertEquals("#P02", (ushort)1, Convert.ToUInt16(boolTrue));
2038 AssertEquals("#P03", (ushort)0, Convert.ToUInt16(tryByte));
2039 AssertEquals("#P04", (ushort)97, Convert.ToUInt16(tryChar));
2040 AssertEquals("#P05", (ushort)1234, Convert.ToUInt16(tryDec));
2041 AssertEquals("#P06", (ushort)0, Convert.ToUInt16(tryDbl));
2042 AssertEquals("#P07", (ushort)1234, Convert.ToUInt16(tryInt16));
2043 AssertEquals("#P08", (ushort)12345, Convert.ToUInt16(tryInt32));
2044 AssertEquals("#P09", (ushort)43752, Convert.ToUInt16((long)43752));
2045 AssertEquals("#P10", (ushort)123, Convert.ToUInt16(trySByte));
2046 AssertEquals("#P11", (ushort)1234, Convert.ToUInt16(tryFloat));
2047 AssertEquals("#P12", (ushort)123, Convert.ToUInt16((string)"123"));
2048 AssertEquals("#P13", (ushort)34567, Convert.ToUInt16(tryUI16));
2049 AssertEquals("#P14", (ushort)56789, Convert.ToUInt16((uint)56789));
2050 AssertEquals("#P15", (ushort)0, Convert.ToUInt16(tryUI64));
2051 AssertEquals("#P16", (ushort)31, Convert.ToUInt16("31", ci));
2052 AssertEquals("#P17", (ushort)14, Convert.ToUInt16("1110", 2));
2053 AssertEquals("#P18", (ushort)32, Convert.ToUInt16("40", 8));
2054 AssertEquals("#P19", (ushort)40, Convert.ToUInt16("40", 10));
2055 AssertEquals("#P20", (ushort)64, Convert.ToUInt16("40", 16));
2059 Convert.ToUInt16(tryDT);
2062 catch (Exception e) {
2063 AssertEquals("#P25", typeof(InvalidCastException), e.GetType());
2067 Convert.ToUInt16(decimal.MaxValue);
2070 catch (Exception e) {
2071 AssertEquals("#P26", typeof(OverflowException), e.GetType());
2075 Convert.ToUInt16(decimal.MinValue);
2078 catch (Exception e) {
2079 AssertEquals("#P27", typeof(OverflowException), e.GetType());
2083 Convert.ToUInt16(double.MaxValue);
2086 catch (Exception e) {
2087 AssertEquals("#P28", typeof(OverflowException), e.GetType());
2091 Convert.ToUInt16(double.MinValue);
2094 catch (Exception e) {
2095 AssertEquals("#P29", typeof(OverflowException), e.GetType());
2099 Convert.ToUInt16(short.MinValue);
2102 catch (Exception e) {
2103 AssertEquals("#P30", typeof(OverflowException), e.GetType());
2107 Convert.ToUInt16(int.MaxValue);
2110 catch (Exception e) {
2111 AssertEquals("#P31", typeof(OverflowException), e.GetType());
2115 Convert.ToUInt16(int.MinValue);
2118 catch (Exception e) {
2119 AssertEquals("#P32", typeof(OverflowException), e.GetType());
2123 Convert.ToUInt16(long.MaxValue);
2126 catch (Exception e) {
2127 AssertEquals("#P33", typeof(OverflowException), e.GetType());
2131 Convert.ToUInt16(long.MinValue);
2134 catch (Exception e) {
2135 AssertEquals("#P34", typeof(OverflowException), e.GetType());
2139 Convert.ToUInt16(tryObj);
2142 catch (Exception e) {
2143 AssertEquals("#P35", typeof(InvalidCastException), e.GetType());
2147 Convert.ToUInt16(sbyte.MinValue);
2150 catch (Exception e) {
2151 AssertEquals("#P36", typeof(OverflowException), e.GetType());
2155 Convert.ToUInt16(float.MaxValue);
2158 catch (Exception e) {
2159 AssertEquals("#P37", typeof(OverflowException), e.GetType());
2163 Convert.ToUInt16(float.MinValue);
2166 catch (Exception e) {
2167 AssertEquals("#P38", typeof(OverflowException), e.GetType());
2171 Convert.ToUInt16("1A2");
2174 catch (Exception e) {
2175 AssertEquals("#P39", typeof(FormatException), e.GetType());
2179 Convert.ToUInt16("-32800");
2182 catch (Exception e) {
2183 AssertEquals("#P40", typeof(OverflowException), e.GetType());
2187 Convert.ToUInt16(int.MaxValue.ToString());
2190 catch (Exception e) {
2191 AssertEquals("#P41", typeof(OverflowException), e.GetType());
2195 Convert.ToUInt16(ulong.MaxValue);
2198 catch (Exception e) {
2199 AssertEquals("#P42", typeof(OverflowException), e.GetType());
2203 Convert.ToUInt16("1A2", ci);
2206 catch (Exception e) {
2207 AssertEquals("#P43", typeof(FormatException), e.GetType());
2211 Convert.ToUInt16("-32800", ci);
2214 catch (Exception e) {
2215 AssertEquals("#P44", typeof(OverflowException), e.GetType());
2219 Convert.ToUInt16("456987", ci);
2222 catch (Exception e) {
2223 AssertEquals("#P45", typeof(OverflowException), e.GetType());
2227 Convert.ToUInt16("40", 9);
2230 catch (Exception e) {
2231 AssertEquals("#P46", typeof(ArgumentException), e.GetType());
2235 Convert.ToUInt16 ("abcde", 16);
2238 catch (Exception e) {
2239 AssertEquals ("#P47", typeof (OverflowException), e.GetType ());
2243 public void TestSignedToInt() {
2244 // String cannot contain a minus sign if the base is not 10.
2245 // But can if it is ten, and + is allowed everywhere.
2246 AssertEquals("Signed0", -1, Convert.ToInt32 ("-1", 10));
2247 AssertEquals("Signed1", 1, Convert.ToInt32 ("+1", 10));
2248 AssertEquals("Signed2", 1, Convert.ToInt32 ("+1", 2));
2249 AssertEquals("Signed3", 1, Convert.ToInt32 ("+1", 8));
2250 AssertEquals("Signed4", 1, Convert.ToInt32 ("+1", 16));
2253 Convert.ToInt32("-1", 2);
2259 Convert.ToInt32("-1", 8);
2265 Convert.ToInt32("-1", 16);
2274 public void TestToUInt32() {
2275 AssertEquals("#Q01", (uint)1, Convert.ToUInt32(boolTrue));
2276 AssertEquals("#Q02", (uint)0, Convert.ToUInt32(boolFalse));
2277 AssertEquals("#Q03", (uint)0, Convert.ToUInt32(tryByte));
2278 AssertEquals("#Q04", (uint)97, Convert.ToUInt32(tryChar));
2279 AssertEquals("#Q05", (uint)1234, Convert.ToUInt32(tryDec));
2280 AssertEquals("#Q06", (uint)0, Convert.ToUInt32(tryDbl));
2281 AssertEquals("#Q07", (uint)1234, Convert.ToUInt32(tryInt16));
2282 AssertEquals("#Q08", (uint)12345, Convert.ToUInt32(tryInt32));
2283 AssertEquals("#Q09", (uint)1234567890, Convert.ToUInt32((long)1234567890));
2284 AssertEquals("#Q10", (uint)123, Convert.ToUInt32(trySByte));
2285 AssertEquals("#Q11", (uint)1234, Convert.ToUInt32(tryFloat));
2286 AssertEquals("#Q12", (uint)3456789, Convert.ToUInt32("3456789"));
2287 AssertEquals("#Q13", (uint)34567, Convert.ToUInt32(tryUI16));
2288 AssertEquals("#Q14", (uint)567891234, Convert.ToUInt32(tryUI32));
2289 AssertEquals("#Q15", (uint)0, Convert.ToUInt32(tryUI64));
2290 AssertEquals("#Q16", (uint)415, Convert.ToUInt32("110011111", 2));
2291 AssertEquals("#Q17", (uint)156, Convert.ToUInt32("234" ,8));
2292 AssertEquals("#Q18", (uint)234, Convert.ToUInt32("234" ,10));
2293 AssertEquals("#Q19", (uint)564, Convert.ToUInt32("234" ,16));
2297 Convert.ToUInt32(tryDT);
2300 catch (Exception e) {
2301 AssertEquals("#Q25", typeof(InvalidCastException), e.GetType());
2305 Convert.ToUInt32(decimal.MaxValue);
2308 catch (Exception e) {
2309 AssertEquals("#Q26", typeof(OverflowException), e.GetType());
2313 Convert.ToUInt32((decimal)-150);
2316 catch (Exception e) {
2317 AssertEquals("#Q27", typeof(OverflowException), e.GetType());
2321 Convert.ToUInt32(double.MaxValue);
2324 catch (Exception e) {
2325 AssertEquals("#Q28", typeof(OverflowException), e.GetType());
2329 Convert.ToUInt32((double)-1);
2332 catch (Exception e) {
2333 AssertEquals("#Q29", typeof(OverflowException), e.GetType());
2337 Convert.ToUInt32(short.MinValue);
2340 catch (Exception e) {
2341 AssertEquals("#Q30", typeof(OverflowException), e.GetType());
2345 Convert.ToUInt32(int.MinValue);
2348 catch (Exception e) {
2349 AssertEquals("#Q31", typeof(OverflowException), e.GetType());
2353 Convert.ToUInt32(long.MaxValue);
2356 catch (Exception e) {
2357 AssertEquals("#Q32", typeof(OverflowException), e.GetType());
2361 Convert.ToUInt32((long)-50000);
2364 catch (Exception e) {
2365 AssertEquals("#Q33", typeof(OverflowException), e.GetType());
2369 Convert.ToUInt32(new Exception());
2372 catch (Exception e) {
2373 AssertEquals("#Q34", typeof(InvalidCastException), e.GetType());
2377 Convert.ToUInt32(sbyte.MinValue);
2380 catch (Exception e) {
2381 AssertEquals("#Q35", typeof(OverflowException), e.GetType());
2385 Convert.ToUInt32(float.MaxValue);
2388 catch (Exception e) {
2389 AssertEquals("#Q36", typeof(OverflowException), e.GetType());
2393 Convert.ToUInt32(float.MinValue);
2396 catch (Exception e) {
2397 AssertEquals("#Q37", typeof(OverflowException), e.GetType());
2401 Convert.ToUInt32("45t54");
2404 catch (Exception e) {
2405 AssertEquals("#Q38", typeof(FormatException), e.GetType());
2409 Convert.ToUInt32("-55");
2412 catch (Exception e) {
2413 AssertEquals("#Q39", typeof(OverflowException), e.GetType());
2417 Convert.ToUInt32(ulong.MaxValue);
2420 catch (Exception e) {
2421 AssertEquals("#Q40", typeof(OverflowException), e.GetType());
2425 Convert.ToUInt32(new Exception(), ci);
2428 catch (Exception e) {
2429 AssertEquals("#Q41", typeof(InvalidCastException), e.GetType());
2433 Convert.ToUInt32(tryStr, ci);
2436 catch (Exception e) {
2437 AssertEquals("#Q42", typeof(FormatException), e.GetType());
2441 Convert.ToUInt32("-50", ci);
2444 catch (Exception e) {
2445 AssertEquals("#Q43", typeof(OverflowException), e.GetType());
2449 Convert.ToUInt32(decimal.MaxValue.ToString(), ci);
2452 catch (Exception e) {
2453 AssertEquals("#Q44", typeof(OverflowException), e.GetType());
2457 Convert.ToUInt32("1001110", 1);
2460 catch (Exception e) {
2461 AssertEquals("#Q45", typeof(ArgumentException), e.GetType());
2465 public void TestToUInt64()
2469 AssertEquals("#R01", (ulong)1, Convert.ToUInt64(boolTrue));
2471 AssertEquals("#R02", (ulong)0, Convert.ToUInt64(boolFalse));
2473 AssertEquals("#R03", (ulong)0, Convert.ToUInt64(tryByte));
2475 AssertEquals("#R04", (ulong)97, Convert.ToUInt64(tryChar));
2477 AssertEquals("#R05", (ulong)1234, Convert.ToUInt64(tryDec));
2479 AssertEquals("#R06", (ulong)0, Convert.ToUInt64(tryDbl));
2481 AssertEquals("#R07", (ulong)1234, Convert.ToUInt64(tryInt16));
2483 AssertEquals("#R08", (ulong)12345, Convert.ToUInt64(tryInt32));
2485 AssertEquals("#R09", (ulong)123456789012, Convert.ToUInt64(tryInt64));
2487 AssertEquals("#R10", (ulong)123, Convert.ToUInt64(trySByte));
2489 AssertEquals("#R11", (ulong)1234, Convert.ToUInt64(tryFloat));
2491 AssertEquals("#R12", (ulong)345678, Convert.ToUInt64("345678"));
2493 AssertEquals("#R13", (ulong)34567, Convert.ToUInt64(tryUI16));
2495 AssertEquals("#R14", (ulong)567891234, Convert.ToUInt64(tryUI32));
2497 AssertEquals("#R15", (ulong)0, Convert.ToUInt64(tryUI64));
2499 AssertEquals("#R16", (ulong)123, Convert.ToUInt64("123", ci));
2501 AssertEquals("#R17", (ulong)4, Convert.ToUInt64("100", 2));
2503 AssertEquals("#R18", (ulong)64, Convert.ToUInt64("100", 8));
2505 AssertEquals("#R19", (ulong)100, Convert.ToUInt64("100", 10));
2507 AssertEquals("#R20", (ulong)256, Convert.ToUInt64("100", 16));
2508 } catch (Exception e) {
2509 Fail ("Unexpected exception caught when iTest = " + iTest + ": e = " + e);
2513 Convert.ToUInt64(tryDT);
2516 catch (Exception e) {
2517 AssertEquals("#R25", typeof(InvalidCastException), e.GetType());
2521 Convert.ToUInt64(decimal.MaxValue);
2524 catch (Exception e) {
2525 AssertEquals("#R26", typeof(OverflowException), e.GetType());
2529 Convert.ToUInt64((decimal)-140);
2532 catch (Exception e) {
2533 AssertEquals("#R27", typeof(OverflowException), e.GetType());
2537 Convert.ToUInt64(double.MaxValue);
2540 catch (Exception e) {
2541 AssertEquals("#R28", typeof(OverflowException), e.GetType());
2545 Convert.ToUInt64((double)-1);
2548 catch (Exception e) {
2549 AssertEquals("#R29", typeof(OverflowException), e.GetType());
2553 Convert.ToUInt64(short.MinValue);
2556 catch (Exception e) {
2557 AssertEquals("#R30", typeof(OverflowException), e.GetType());
2561 Convert.ToUInt64(int.MinValue);
2564 catch (Exception e) {
2565 AssertEquals("#R31", typeof(OverflowException), e.GetType());
2569 Convert.ToUInt64(long.MinValue);
2572 catch (Exception e) {
2573 AssertEquals("#R32", typeof(OverflowException), e.GetType());
2577 Convert.ToUInt64(tryObj);
2580 catch (Exception e) {
2581 AssertEquals("#R33", typeof(InvalidCastException), e.GetType());
2585 Convert.ToUInt64(sbyte.MinValue);
2588 catch (Exception e) {
2589 AssertEquals("#R34", typeof(OverflowException), e.GetType());
2593 Convert.ToUInt64(float.MinValue);
2596 catch (Exception e) {
2597 AssertEquals("#R35", typeof(OverflowException), e.GetType());
2601 Convert.ToUInt64(float.MaxValue);
2604 catch (Exception e) {
2605 AssertEquals("#R36", typeof(OverflowException), e.GetType());
2609 Convert.ToUInt64("234rt78");
2612 catch (Exception e) {
2613 AssertEquals("#R37", typeof(FormatException), e.GetType());
2617 Convert.ToUInt64("-68");
2620 catch (Exception e) {
2621 AssertEquals("#R38", typeof(OverflowException), e.GetType());
2625 Convert.ToUInt64(decimal.MaxValue.ToString());
2628 catch (Exception e) {
2629 AssertEquals("#R39", typeof(OverflowException), e.GetType());
2633 Convert.ToUInt64("23rd2", ci);
2636 catch (Exception e) {
2637 AssertEquals("#R40", typeof(FormatException), e.GetType());
2641 Convert.ToUInt64(decimal.MinValue.ToString(), ci);
2644 catch (Exception e) {
2645 AssertEquals("#R41", typeof(OverflowException), e.GetType());
2649 Convert.ToUInt64(decimal.MaxValue.ToString(), ci);
2652 catch (Exception e) {
2653 AssertEquals("#R42", typeof(OverflowException), e.GetType());
2657 Convert.ToUInt64("132", 9);
2660 catch (Exception e) {
2661 AssertEquals("#R43", typeof(ArgumentException), e.GetType());
2665 AssertEquals ("#L35", (ulong) 256, Convert.ToUInt64 ("0x100", 16));
2666 AssertEquals ("#L36", (ulong) 256, Convert.ToUInt64 ("0X100", 16));
2667 AssertEquals ("#L37", ulong.MaxValue, Convert.ToUInt64 ("0xFFFFFFFFFFFFFFFF", 16));
2671 [ExpectedException (typeof (FormatException))]
2672 public void TestInvalidBase64() {
2673 // This has to be a multiple of 4 characters, otherwise you
2674 // are testing something else. Ideally one will become a byte
2677 // This test is designed to see what happens with invalid bytes
2678 string brokenB64 = "AB~\u00a3";
2679 Convert.FromBase64String(brokenB64);
2683 public void TestBeginWithSpaces ()
2685 byte[] bb = new byte[] { 1, 2, 3};
2686 string s = Convert.ToBase64String (bb);
2687 byte [] b2 = Convert.FromBase64String (" " + s);
2688 Assertion.AssertEquals ("#01", 3, b2.Length);
2689 for (int i = 0; i < 3; i++)
2690 Assertion.AssertEquals ("#0" + (i + 2), bb [i], b2 [i]);
2693 public void TestToBase64CharArray ()
2695 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2697 char[] expectedCharArr = {'I', 'X', '/', '/', 'b', 'a', 'o', '2'};
2698 char[] result = new Char[8];
2700 Convert.ToBase64CharArray(byteArr, 0, byteArr.Length, result, 0);
2702 for (int i = 0; i < expectedCharArr.Length; i++) {
2703 AssertEquals("#S0" + i, expectedCharArr[i], result[i]);
2708 [ExpectedException (typeof(ArgumentNullException))]
2709 public void ToBase64CharArray_InNull ()
2711 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2712 char[] result = new Char[8];
2713 Convert.ToBase64CharArray (null, 0, byteArr.Length, result, 0);
2717 [ExpectedException (typeof(ArgumentNullException))]
2718 public void ToBase64CharArray_OutNull ()
2720 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2721 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, null, 0);
2725 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2726 public void ToBase64CharArray_OffsetInNegative ()
2728 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2729 char[] result = new Char[8];
2730 Convert.ToBase64CharArray (byteArr, -1, byteArr.Length, result, 0);
2734 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2735 public void ToBase64CharArray_LengthNegative ()
2737 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2738 char[] result = new Char[8];
2739 Convert.ToBase64CharArray (byteArr, 0, -5, result, 0);
2743 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2744 public void ToBase64CharArray_OffsetOutNegative ()
2746 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2747 char[] result = new Char[8];
2748 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, -2);
2752 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2753 public void ToBase64CharArray_TotalIn ()
2755 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2756 char[] result = new Char[8];
2757 Convert.ToBase64CharArray (byteArr, 4, byteArr.Length, result, 0);
2761 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2762 public void ToBase64CharArray_TotalInOverflow ()
2764 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2765 char[] result = new Char[8];
2766 Convert.ToBase64CharArray (byteArr, Int32.MaxValue, byteArr.Length, result, 0);
2770 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2771 public void ToBase64CharArray_TotalOut ()
2773 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2774 char[] result = new Char[8];
2775 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, 2);
2779 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2780 public void ToBase64CharArray_TotalOutOverflow ()
2782 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2783 char[] result = new Char[8];
2784 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, Int32.MaxValue);
2787 public void TestToBase64String() {
2788 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2789 string expectedStr = "IX//bao2";
2793 result1 = Convert.ToBase64String(byteArr);
2794 result2 = Convert.ToBase64String(byteArr, 0, byteArr.Length);
2796 AssertEquals("#T01", expectedStr, result1);
2797 AssertEquals("#T02", expectedStr, result2);
2800 Convert.ToBase64String(null);
2803 catch (Exception e) {
2804 AssertEquals("#T05", typeof(ArgumentNullException), e.GetType());
2808 Convert.ToBase64String(byteArr, -1, byteArr.Length);
2811 catch (Exception e) {
2812 AssertEquals("#T06", typeof(ArgumentOutOfRangeException), e.GetType());
2816 Convert.ToBase64String(byteArr, 0, -10);
2819 catch (Exception e) {
2820 AssertEquals("#T07", typeof(ArgumentOutOfRangeException), e.GetType());
2824 Convert.ToBase64String(byteArr, 4, byteArr.Length);
2827 catch (Exception e) {
2828 AssertEquals("#T08", typeof(ArgumentOutOfRangeException), e.GetType());
2833 public void ToBase64String_Bug76876 ()
2835 byte[] bs = Convert.FromBase64String ("ZuBZ7PESb3VRXgrl/KSRJd/hTGBvaEvEplqH3izPomDv5nBjS9MzcD1h8tOWzS7/wYGnaip8\nbhBfCrpWxivi8G7R08oBcADIiclpZeqRxai9kG4WoBUzJ6MCbxuvb1k757q+D9nqoL0p9Rer\n+5+vNodYkHYwqnKKyMBSQ11sspw=\n");
2836 string s = Convert.ToBase64String (bs, Base64FormattingOptions.None);
2837 Assert ("no new line", !s.Contains ("\n"));
2840 /* Have experienced some problems with FromBase64CharArray using mono. Something
2841 * about error in a unicode file.
2843 * However the test seems to run fine using mono in a cygwin environment
2847 [ExpectedException (typeof (ArgumentNullException))]
2848 public void FromBase64CharArray_Null ()
2850 Convert.FromBase64CharArray (null, 1, 5);
2854 [ExpectedException (typeof (FormatException))]
2855 [Category ("TargetJvmNotWorking")]
2856 public void FromBase64CharArray_Empty ()
2858 Convert.FromBase64CharArray (new char[0], 0, 0);
2862 [ExpectedException (typeof (FormatException))]
2863 [Category ("TargetJvmNotWorking")]
2864 public void FormatBase64CharArray_OnlyWhitespace ()
2866 Convert.FromBase64CharArray (new char[3] {' ',
2871 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2872 public void FromBase64CharArray_OutOfRangeStart ()
2874 Convert.FromBase64CharArray (new char [4], -1, 4);
2878 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2879 public void FromBase64CharArray_OutOfRangeLength ()
2881 Convert.FromBase64CharArray (new char [4], 2, 4);
2885 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2886 public void FromBase64CharArray_Overflow ()
2888 Convert.FromBase64CharArray (new char [4], Int32.MaxValue, 4);
2892 [ExpectedException (typeof (FormatException))]
2893 public void FromBase64CharArray_InvalidLength ()
2895 Convert.FromBase64CharArray (new char [4], 0, 3);
2899 [ExpectedException (typeof (FormatException))]
2900 public void FromBase64CharArray_WideChar ()
2902 char[] c = new char [4] { 'A', 'A', 'A', (char) Char.MaxValue };
2903 Convert.FromBase64CharArray (c, 0, 4);
2907 public void FromBase64CharArray ()
2909 char[] charArr = {'M','o','n','o','m','o','n','o'};
2910 byte[] expectedByteArr = {50, 137, 232, 154, 137, 232};
2912 byte[] fromCharArr = Convert.FromBase64CharArray(charArr, 0, 8);
2914 for (int i = 0; i < fromCharArr.Length; i++){
2915 AssertEquals("#U0" + i, expectedByteArr[i], fromCharArr[i]);
2919 /* Have experienced some problems with FromBase64String using mono. Something about
2920 * error in a unicode file.
2922 * However the test seems to run fine using mono in a cygwin environment
2926 [ExpectedException (typeof (ArgumentNullException))]
2927 public void FromBase64String_Null ()
2929 Convert.FromBase64String (null);
2933 [ExpectedException (typeof (FormatException))]
2934 public void FromBase64String_InvalidLength ()
2936 Convert.FromBase64String ("foo");
2940 [ExpectedException (typeof (FormatException))]
2941 public void FromBase64String_InvalidLength2 ()
2943 Convert.FromBase64String (tryStr);
2947 public void FromBase64String_InvalidLengthBecauseOfIgnoredChars ()
2949 byte[] result = Convert.FromBase64String ("AAAA\t");
2950 AssertEquals ("InvalidLengthBecauseOfIgnoredChars", 3, result.Length);
2953 private const string ignored = "\t\r\n ";
2954 private const string base64data = "AAAAAAAAAAAAAAAAAAAA"; // 15 bytes 0x00
2957 public void FromBase64_IgnoreCharsBefore ()
2959 string s = ignored + base64data;
2960 byte[] data = Convert.FromBase64String (s);
2961 AssertEquals ("String-IgnoreCharsBefore-Ignored", 15, data.Length);
2963 char[] c = s.ToCharArray ();
2964 data = Convert.FromBase64CharArray (c, 0, c.Length);
2965 AssertEquals ("CharArray-IgnoreCharsBefore-Ignored", 15, data.Length);
2969 public void FromBase64_IgnoreCharsInside ()
2971 string s = base64data + ignored + base64data;
2972 byte[] data = Convert.FromBase64String (s);
2973 AssertEquals ("String-IgnoreCharsInside-Ignored", 30, data.Length);
2975 char[] c = s.ToCharArray ();
2976 data = Convert.FromBase64CharArray (c, 0, c.Length);
2977 AssertEquals ("CharArray-IgnoreCharsInside-Ignored", 30, data.Length);
2981 public void FromBase64_IgnoreCharsAfter ()
2983 string s = base64data + ignored;
2984 byte[] data = Convert.FromBase64String (s);
2985 AssertEquals ("String-IgnoreCharsAfter-Ignored", 15, data.Length);
2987 char[] c = s.ToCharArray ();
2988 data = Convert.FromBase64CharArray (c, 0, c.Length);
2989 AssertEquals ("CharArray-IgnoreCharsAfter-Ignored", 15, data.Length);
2993 [Category ("TargetJvmNotWorking")]
2994 public void FromBase64_Empty ()
2996 AssertEquals (new byte[0], Convert.FromBase64String (string.Empty));
3001 [ExpectedException (typeof (FormatException))]
3003 public void FromBase64_OnlyWhiteSpace ()
3006 AssertEquals (new byte[0], Convert.FromBase64String (" \r\t"));
3008 Convert.FromBase64String (" \r\t");
3013 [ExpectedException (typeof (FormatException))]
3014 public void FromBase64_InvalidChar ()
3016 Convert.FromBase64String ("amVsb3U=\u0100");
3020 [ExpectedException (typeof (FormatException))]
3021 public void FromBase64_Min ()
3023 Convert.FromBase64String ("amVsb3U= \r \n\u007B");
3027 public void FromBase64_TrailingEqualAndSpaces () // From bug #75840.
3029 string base64 = "\n fdy6S2NLpnT4fMdokUHSHsmpcvo= ";
3030 byte [] bytes = Convert.FromBase64String (base64);
3031 AssertEquals ("#01", 20, bytes.Length);
3032 byte [] target = new byte [] { 0x7D, 0xDC, 0xBA, 0x4B, 0x63, 0x4B, 0xA6, 0x74, 0xF8, 0x7C, 0xC7,
3033 0x68, 0x91, 0x41, 0xD2, 0x1E, 0xC9, 0xA9, 0x72, 0xFA };
3035 for (int i = 0; i < 20; i++) {
3036 if (bytes [i] != target [i])
3037 Fail ("Item #" + i);
3041 public void TestConvertFromNull() {
3043 AssertEquals ("#W1", false, Convert.ToBoolean (null as object));
3044 AssertEquals ("#W2", 0, Convert.ToByte (null as object));
3045 AssertEquals ("#W3", 0, Convert.ToChar (null as object));
3046 AssertEquals ("#W4", new DateTime (1,1,1,0,0,0), Convert.ToDateTime (null as object));
3047 AssertEquals ("#W5", 0, Convert.ToDecimal (null as object));
3048 AssertEquals ("#W6", 0, Convert.ToDouble (null as object));
3049 AssertEquals ("#W7", 0, Convert.ToInt16 (null as object));
3050 AssertEquals ("#W8", 0, Convert.ToInt32 (null as object));
3051 AssertEquals ("#W9", 0, Convert.ToInt64 (null as object));
3052 AssertEquals ("#W10", 0, Convert.ToSByte (null as object));
3053 AssertEquals ("#W11", 0, Convert.ToSingle (null as object));
3054 AssertEquals ("#W12", "", Convert.ToString (null as object));
3055 AssertEquals ("#W13", 0, Convert.ToUInt16 (null as object));
3056 AssertEquals ("#W14", 0, Convert.ToUInt32 (null as object));
3057 AssertEquals ("#W15", 0, Convert.ToUInt64 (null as object));
3058 AssertEquals ("#W16", false, Convert.ToBoolean (null as string));
3059 AssertEquals ("#W17", 0, Convert.ToByte (null as string));
3062 Convert.ToChar (null as string);
3064 } catch (Exception e) {
3065 AssertEquals ("#W18", typeof (ArgumentNullException), e.GetType ());
3068 AssertEquals ("#W19", new DateTime (1,1,1,0,0,0), Convert.ToDateTime (null as string));
3069 AssertEquals ("#W20", 0, Convert.ToDecimal (null as string));
3070 AssertEquals ("#W21", 0, Convert.ToDouble (null as string));
3071 AssertEquals ("#W22", 0, Convert.ToInt16 (null as string));
3072 AssertEquals ("#W23", 0, Convert.ToInt32 (null as string));
3073 AssertEquals ("#W24", 0, Convert.ToInt64 (null as string));
3074 AssertEquals ("#W25", 0, Convert.ToSByte (null as string));
3075 AssertEquals ("#W26", 0, Convert.ToSingle (null as string));
3076 AssertEquals ("#W27", null, Convert.ToString (null as string));
3077 AssertEquals ("#W28", 0, Convert.ToUInt16 (null as string));
3078 AssertEquals ("#W29", 0, Convert.ToUInt32 (null as string));
3079 AssertEquals ("#W30", 0, Convert.ToUInt64 (null as string));
3083 [ExpectedException (typeof (FormatException))]
3084 public void FromBase64StringInvalidFormat ()
3086 Convert.FromBase64String ("Tgtm+DBN====");
3090 [ExpectedException (typeof (FormatException))]
3091 public void FromBase64StringInvalidFormat2 ()
3093 Convert.FromBase64String ("Tgtm+DBN========");
3097 public void ToByte_PrefixedHexStringInBase16 ()
3099 AssertEquals ("0xff", 255, Convert.ToByte ("0xff", 16));
3100 AssertEquals ("0xfF", 255, Convert.ToByte ("0xfF", 16));
3101 AssertEquals ("0xFf", 255, Convert.ToByte ("0xFf", 16));
3102 AssertEquals ("0xFF", 255, Convert.ToByte ("0xFF", 16));
3104 AssertEquals ("0Xff", 255, Convert.ToByte ("0Xff", 16));
3105 AssertEquals ("0XfF", 255, Convert.ToByte ("0XfF", 16));
3106 AssertEquals ("0XFf", 255, Convert.ToByte ("0XFf", 16));
3107 AssertEquals ("0XFF", 255, Convert.ToByte ("0XFF", 16));
3109 AssertEquals ("0x0", Byte.MinValue, Convert.ToByte ("0x0", 16));
3113 [ExpectedException (typeof (OverflowException))]
3114 public void ToByte_NegativeString ()
3116 Convert.ToByte ("-1");
3120 [ExpectedException (typeof (ArgumentException))]
3121 public void ToByte_NegativeStringNonBase10 ()
3123 Convert.ToByte ("-0", 16);
3127 [ExpectedException (typeof (OverflowException))]
3128 public void ToByte_NegativeString_Base10 ()
3130 Convert.ToByte ("-0", 10);
3134 public void ToByte_NegativeZeroString ()
3136 Convert.ToByte ("-0");
3137 Convert.ToByte ("-0", null);
3141 [ExpectedException (typeof (OverflowException))]
3142 public void ToUInt16_NegativeString ()
3144 Convert.ToUInt16 ("-1");
3148 [ExpectedException (typeof (ArgumentException))]
3149 public void ToUInt16_NegativeStringNonBase10 ()
3151 Convert.ToUInt16 ("-0", 16);
3155 [ExpectedException (typeof (OverflowException))]
3156 public void ToUInt16_NegativeString_Base10 ()
3158 Convert.ToUInt16 ("-0", 10);
3162 public void ToUInt16_NegativeZeroString ()
3164 Convert.ToUInt16 ("-0");
3165 Convert.ToUInt16 ("-0", null);
3169 [ExpectedException (typeof (OverflowException))]
3170 public void ToUInt32_NegativeString ()
3172 Convert.ToUInt32 ("-1");
3176 [ExpectedException (typeof (ArgumentException))]
3177 public void ToUInt32_NegativeStringNonBase10 ()
3179 Convert.ToUInt32 ("-0", 16);
3183 [ExpectedException (typeof (OverflowException))]
3184 public void ToUInt32_NegativeString_Base10 ()
3186 Convert.ToUInt32 ("-0", 10);
3190 public void ToUInt32_NegativeZeroString ()
3192 Convert.ToUInt32 ("-0");
3193 Convert.ToUInt32 ("-0", null);
3197 [ExpectedException (typeof (OverflowException))]
3198 public void ToUInt64_NegativeString ()
3200 Convert.ToUInt64 ("-1");
3204 [ExpectedException (typeof (ArgumentException))]
3205 public void ToUInt64_NegativeStringNonBase10 ()
3207 Convert.ToUInt64 ("-0", 16);
3211 [ExpectedException (typeof (OverflowException))]
3212 public void ToUInt64_NegativeString_Base10 ()
3214 Convert.ToUInt64 ("-0", 10);
3218 public void ToUInt64_NegativeZeroString ()
3220 Convert.ToUInt64 ("-0");
3221 Convert.ToUInt64 ("-0", null);
3227 public void ToByte_MaxValue ()
3229 AssertEquals ("ff,16", Byte.MaxValue, Convert.ToByte ("ff", 16));
3230 AssertEquals ("0xFF,16", Byte.MaxValue, Convert.ToByte ("0XFF", 16));
3231 AssertEquals ("255,10", Byte.MaxValue, Convert.ToByte ("255", 10));
3232 AssertEquals ("377,8", Byte.MaxValue, Convert.ToByte ("377", 8));
3233 AssertEquals ("11111111,2", Byte.MaxValue, Convert.ToByte ("11111111", 2));
3237 public void ToByte_MinValue ()
3239 AssertEquals ("0,16", Byte.MinValue, Convert.ToByte ("0", 16));
3240 AssertEquals ("0x0,16", Byte.MinValue, Convert.ToByte ("0x0", 16));
3241 AssertEquals ("0,10", Byte.MinValue, Convert.ToByte ("0", 10));
3242 AssertEquals ("0,8", Byte.MinValue, Convert.ToByte ("0", 8));
3243 AssertEquals ("0,2", Byte.MinValue, Convert.ToByte ("0", 2));
3247 public void ToUInt16_MaxValue ()
3249 AssertEquals ("ffff,16", UInt16.MaxValue, Convert.ToUInt16 ("ffff", 16));
3250 AssertEquals ("0XFFFF,16", UInt16.MaxValue, Convert.ToUInt16 ("0XFFFF", 16));
3251 AssertEquals ("65535,10", UInt16.MaxValue, Convert.ToUInt16 ("65535", 10));
3252 AssertEquals ("177777,8", UInt16.MaxValue, Convert.ToUInt16 ("177777", 8));
3253 AssertEquals ("1111111111111111,2", UInt16.MaxValue, Convert.ToUInt16 ("1111111111111111", 2));
3257 public void ToUInt16_MinValue ()
3259 AssertEquals ("0,16", UInt16.MinValue, Convert.ToUInt16 ("0", 16));
3260 AssertEquals ("0x0,16", UInt16.MinValue, Convert.ToUInt16 ("0x0", 16));
3261 AssertEquals ("0,10", UInt16.MinValue, Convert.ToUInt16 ("0", 10));
3262 AssertEquals ("0,8", UInt16.MinValue, Convert.ToUInt16 ("0", 8));
3263 AssertEquals ("0,2", UInt16.MinValue, Convert.ToUInt16 ("0", 2));
3267 public void ToUInt32_MaxValue ()
3269 AssertEquals ("ffffffff,16", UInt32.MaxValue, Convert.ToUInt32 ("ffffffff", 16));
3270 AssertEquals ("0XFFFFFFFF,16", UInt32.MaxValue, Convert.ToUInt32 ("0XFFFFFFFF", 16));
3271 AssertEquals ("4294967295,10", UInt32.MaxValue, Convert.ToUInt32 ("4294967295", 10));
3272 AssertEquals ("37777777777,8", UInt32.MaxValue, Convert.ToUInt32 ("37777777777", 8));
3273 AssertEquals ("11111111111111111111111111111111,2", UInt32.MaxValue, Convert.ToUInt32 ("11111111111111111111111111111111", 2));
3277 public void ToUInt32_MinValue ()
3279 AssertEquals ("0,16", UInt32.MinValue, Convert.ToUInt32 ("0", 16));
3280 AssertEquals ("0x0,16", UInt32.MinValue, Convert.ToUInt32 ("0x0", 16));
3281 AssertEquals ("0,10", UInt32.MinValue, Convert.ToUInt32 ("0", 10));
3282 AssertEquals ("0,8", UInt32.MinValue, Convert.ToUInt32 ("0", 8));
3283 AssertEquals ("0,2", UInt32.MinValue, Convert.ToUInt32 ("0", 2));
3287 public void ToUInt64_MaxValue ()
3289 AssertEquals ("ffffffffffffffff,16", UInt64.MaxValue, Convert.ToUInt64 ("ffffffffffffffff", 16));
3290 AssertEquals ("0XFFFFFFFFFFFFFFFF,16", UInt64.MaxValue, Convert.ToUInt64 ("0XFFFFFFFFFFFFFFFF", 16));
3291 AssertEquals ("18446744073709551615,10", UInt64.MaxValue, Convert.ToUInt64 ("18446744073709551615", 10));
3292 AssertEquals ("1777777777777777777777,8", UInt64.MaxValue, Convert.ToUInt64 ("1777777777777777777777", 8));
3293 AssertEquals ("1111111111111111111111111111111111111111111111111111111111111111,2", UInt64.MaxValue, Convert.ToUInt64 ("1111111111111111111111111111111111111111111111111111111111111111", 2));
3297 public void ToUInt64_MinValue ()
3299 AssertEquals ("0,16", UInt64.MinValue, Convert.ToUInt64 ("0", 16));
3300 AssertEquals ("0x0,16", UInt64.MinValue, Convert.ToUInt64 ("0x0", 16));
3301 AssertEquals ("0,10", UInt64.MinValue, Convert.ToUInt64 ("0", 10));
3302 AssertEquals ("0,8", UInt64.MinValue, Convert.ToUInt64 ("0", 8));
3303 AssertEquals ("0,2", UInt64.MinValue, Convert.ToUInt64 ("0", 2));
3309 public void ToSByte_MaxValue ()
3311 AssertEquals ("7F,16", SByte.MaxValue, Convert.ToSByte ("7f", 16));
3312 AssertEquals ("0X7F,16", SByte.MaxValue, Convert.ToSByte ("0X7F", 16));
3313 AssertEquals ("127,10", SByte.MaxValue, Convert.ToSByte ("127", 10));
3314 AssertEquals ("177,8", SByte.MaxValue, Convert.ToSByte ("177", 8));
3315 AssertEquals ("1111111,2", SByte.MaxValue, Convert.ToSByte ("1111111", 2));
3319 public void ToSByte_MinValue ()
3321 AssertEquals ("80,16", SByte.MinValue, Convert.ToSByte ("80", 16));
3322 AssertEquals ("-128,10", SByte.MinValue, Convert.ToSByte ("-128", 10));
3323 AssertEquals ("200,8", SByte.MinValue, Convert.ToSByte ("200", 8));
3324 AssertEquals ("10000000,2", SByte.MinValue, Convert.ToSByte ("10000000", 2));
3328 public void ToInt16_MaxValue ()
3330 AssertEquals ("7FFF,16", Int16.MaxValue, Convert.ToInt16 ("7fff", 16));
3331 AssertEquals ("0X7FFF,16", Int16.MaxValue, Convert.ToInt16 ("0X7FFF", 16));
3332 AssertEquals ("32767,10", Int16.MaxValue, Convert.ToInt16 ("32767", 10));
3333 AssertEquals ("77777,8", Int16.MaxValue, Convert.ToInt16 ("77777", 8));
3334 AssertEquals ("111111111111111,2", Int16.MaxValue, Convert.ToInt16 ("111111111111111", 2));
3338 public void ToInt16_MinValue ()
3340 AssertEquals ("8000,16", Int16.MinValue, Convert.ToInt16 ("8000", 16));
3341 AssertEquals ("-32768,10", Int16.MinValue, Convert.ToInt16 ("-32768", 10));
3342 AssertEquals ("100000,8", Int16.MinValue, Convert.ToInt16 ("100000", 8));
3343 AssertEquals ("1000000000000000,2", Int16.MinValue, Convert.ToInt16 ("1000000000000000", 2));
3347 public void ToInt32_MaxValue ()
3349 AssertEquals ("7fffffff,16", Int32.MaxValue, Convert.ToInt32 ("7fffffff", 16));
3350 AssertEquals ("0X7FFFFFFF,16", Int32.MaxValue, Convert.ToInt32 ("0X7FFFFFFF", 16));
3351 AssertEquals ("2147483647,10", Int32.MaxValue, Convert.ToInt32 ("2147483647", 10));
3352 AssertEquals ("17777777777,8", Int32.MaxValue, Convert.ToInt32 ("17777777777", 8));
3353 AssertEquals ("1111111111111111111111111111111,2", Int32.MaxValue, Convert.ToInt32 ("1111111111111111111111111111111", 2));
3357 public void ToInt32_MinValue ()
3359 AssertEquals ("80000000,16", Int32.MinValue, Convert.ToInt32 ("80000000", 16));
3360 AssertEquals ("-2147483648,10", Int32.MinValue, Convert.ToInt32 ("-2147483648", 10));
3361 AssertEquals ("20000000000,8", Int32.MinValue, Convert.ToInt32 ("20000000000", 8));
3362 AssertEquals ("10000000000000000000000000000000,2", Int32.MinValue, Convert.ToInt32 ("10000000000000000000000000000000", 2));
3366 public void ToInt64_MaxValue ()
3368 AssertEquals ("7fffffffffffffff,16", Int64.MaxValue, Convert.ToInt64 ("7fffffffffffffff", 16));
3369 AssertEquals ("0X7FFFFFFFFFFFFFFF,16", Int64.MaxValue, Convert.ToInt64 ("0X7FFFFFFFFFFFFFFF", 16));
3370 AssertEquals ("9223372036854775807,10", Int64.MaxValue, Convert.ToInt64 ("9223372036854775807", 10));
3371 AssertEquals ("777777777777777777777,8", Int64.MaxValue, Convert.ToInt64 ("777777777777777777777", 8));
3372 AssertEquals ("111111111111111111111111111111111111111111111111111111111111111,2", Int64.MaxValue, Convert.ToInt64 ("111111111111111111111111111111111111111111111111111111111111111", 2));
3376 public void ToInt64_MinValue ()
3378 AssertEquals ("8000000000000000,16", Int64.MinValue, Convert.ToInt64 ("8000000000000000", 16));
3379 AssertEquals ("0x8000000000000000,16", Int64.MinValue, Convert.ToInt64 ("0x8000000000000000", 16));
3380 AssertEquals ("-9223372036854775808,10", Int64.MinValue, Convert.ToInt64 ("-9223372036854775808", 10));
3381 AssertEquals ("1000000000000000000000,8", Int64.MinValue, Convert.ToInt64 ("1000000000000000000000", 8));
3382 AssertEquals ("1000000000000000000000000000000000000000000000000000000000000000,2", Int64.MinValue, Convert.ToInt64 ("1000000000000000000000000000000000000000000000000000000000000000", 2));
3388 [ExpectedException (typeof (OverflowException))]
3389 public void ToSByte_OverMaxValue ()
3391 string max_plus1 = "128";
3392 Convert.ToSByte (max_plus1);
3396 [ExpectedException (typeof (OverflowException))]
3397 public void ToSByte_OverMinValue ()
3399 string min_minus1 = "-129";
3400 Convert.ToSByte (min_minus1);
3404 [ExpectedException (typeof (OverflowException))]
3405 public void ToInt16_OverMaxValue ()
3407 string max_plus1 = "32768";
3408 Convert.ToInt16 (max_plus1);
3412 [ExpectedException (typeof (OverflowException))]
3413 public void ToInt16_OverMinValue ()
3415 string min_minus1 = "-32769";
3416 Convert.ToInt16 (min_minus1);
3420 [ExpectedException (typeof (OverflowException))]
3421 public void ToInt32_OverMaxValue ()
3423 string max_plus1 = "2147483648";
3424 Convert.ToInt32 (max_plus1);
3428 [ExpectedException (typeof (OverflowException))]
3429 public void ToInt32_OverMinValue ()
3431 string min_minus1 = "-2147483649";
3432 Convert.ToInt32 (min_minus1);
3436 [ExpectedException (typeof (OverflowException))]
3437 public void ToInt64_OverMaxValue ()
3439 string max_plus1 = "9223372036854775808";
3440 Convert.ToInt64 (max_plus1);
3444 [ExpectedException (typeof (OverflowException))]
3445 public void ToInt64_OverMinValue ()
3447 string min_minus1 = "-9223372036854775809";
3448 Convert.ToInt64 (min_minus1);
3454 [ExpectedException (typeof (OverflowException))]
3455 public void ToByte_OverMaxValue ()
3457 string max_plus1 = "257";
3458 Convert.ToByte (max_plus1);
3462 [ExpectedException (typeof (OverflowException))]
3463 public void ToByte_OverMinValue ()
3465 string min_minus1 = "-1";
3466 Convert.ToByte (min_minus1);
3470 [ExpectedException (typeof (OverflowException))]
3471 public void ToUInt16_OverMaxValue ()
3473 string max_plus1 = "65536";
3474 Convert.ToUInt16 (max_plus1);
3478 [ExpectedException (typeof (OverflowException))]
3479 public void ToUInt16_OverMinValue ()
3481 string min_minus1 = "-1";
3482 Convert.ToUInt16 (min_minus1);
3486 [ExpectedException (typeof (OverflowException))]
3487 public void ToUInt32_OverMaxValue ()
3489 string max_plus1 = "4294967296";
3490 Convert.ToUInt32 (max_plus1);
3494 [ExpectedException (typeof (OverflowException))]
3495 public void ToUInt32_OverMinValue ()
3497 string min_minus1 = "-1";
3498 Convert.ToUInt32 (min_minus1);
3502 [ExpectedException (typeof (OverflowException))]
3503 public void ToUInt64_OverMaxValue ()
3505 string max_plus1 = "18446744073709551616";
3506 Convert.ToUInt64 (max_plus1);
3510 [ExpectedException (typeof (OverflowException))]
3511 public void ToUInt64_OverMinValue ()
3513 string min_minus1 = "-1";
3514 Convert.ToUInt64 (min_minus1);
3518 public void To_NullString ()
3522 AssertEquals ("ToSByte", 0, Convert.ToSByte (s));
3523 AssertEquals ("ToSByte+base", 0, Convert.ToSByte (s, 10));
3524 AssertEquals ("ToInt16", 0, Convert.ToInt16 (s));
3525 AssertEquals ("ToInt16+base", 0, Convert.ToInt16 (s, 10));
3526 AssertEquals ("ToInt32", 0, Convert.ToInt32 (s));
3527 AssertEquals ("ToInt32+base", 0, Convert.ToInt32 (s, 10));
3528 AssertEquals ("ToInt64", 0, Convert.ToInt64 (s));
3529 AssertEquals ("ToInt64+base", 0, Convert.ToInt64 (s, 10));
3531 AssertEquals ("ToByte", 0, Convert.ToByte (s));
3532 AssertEquals ("ToByte+base", 0, Convert.ToByte (s, 10));
3533 AssertEquals ("ToUInt16", 0, Convert.ToUInt16 (s));
3534 AssertEquals ("ToUInt16+base", 0, Convert.ToUInt16 (s, 10));
3535 AssertEquals ("ToUInt32", 0, Convert.ToUInt32 (s));
3536 AssertEquals ("ToUInt32+base", 0, Convert.ToUInt32 (s, 10));
3537 AssertEquals ("ToUInt64", 0, Convert.ToUInt64 (s));
3538 AssertEquals ("ToUInt64+base", 0, Convert.ToUInt64 (s, 10));
3542 public void To_NullObject ()
3546 AssertEquals ("ToSByte", 0, Convert.ToSByte (o));
3547 AssertEquals ("ToInt16", 0, Convert.ToInt16 (o));
3548 AssertEquals ("ToInt32", 0, Convert.ToInt32 (o));
3549 AssertEquals ("ToInt64", 0, Convert.ToInt64 (o));
3551 AssertEquals ("ToByte", 0, Convert.ToByte (o));
3552 AssertEquals ("ToUInt16", 0, Convert.ToUInt16 (o));
3553 AssertEquals ("ToUInt32", 0, Convert.ToUInt32 (o));
3554 AssertEquals ("ToUInt64", 0, Convert.ToUInt64 (o));
3558 public void To_NullObjectFormatProvider ()
3561 IFormatProvider fp = (IFormatProvider) new NumberFormatInfo ();
3563 AssertEquals ("ToSByte", 0, Convert.ToSByte (o, fp));
3564 AssertEquals ("ToInt16", 0, Convert.ToInt16 (o, fp));
3565 AssertEquals ("ToInt32", 0, Convert.ToInt32 (o, fp));
3566 AssertEquals ("ToInt64", 0, Convert.ToInt64 (o, fp));
3568 AssertEquals ("ToByte", 0, Convert.ToByte (o, fp));
3569 AssertEquals ("ToUInt16", 0, Convert.ToUInt16 (o, fp));
3570 AssertEquals ("ToUInt32", 0, Convert.ToUInt32 (o, fp));
3571 AssertEquals ("ToUInt64", 0, Convert.ToUInt64 (o, fp));
3575 [ExpectedException (typeof (ArgumentNullException))]
3576 public void ToSByte_NullStringFormatProvider ()
3579 // SByte is a "special" case ???
3580 Convert.ToSByte (s, new NumberFormatInfo ());
3584 public void To_NullStringFormatProvider ()
3587 IFormatProvider fp = (IFormatProvider) new NumberFormatInfo ();
3590 AssertEquals ("ToInt16", 0, Convert.ToInt16 (s, fp));
3591 AssertEquals ("ToInt32", 0, Convert.ToInt32 (s, fp));
3592 AssertEquals ("ToInt64", 0, Convert.ToInt64 (s, fp));
3594 AssertEquals ("ToByte", 0, Convert.ToByte (s, fp));
3595 AssertEquals ("ToUInt16", 0, Convert.ToUInt16 (s, fp));
3596 AssertEquals ("ToUInt32", 0, Convert.ToUInt32 (s, fp));
3597 AssertEquals ("ToUInt64", 0, Convert.ToUInt64 (s, fp));
3601 [ExpectedException (typeof (InvalidCastException))]
3602 public void ChangeTypeToTypeCodeEmpty ()
3604 Convert.ChangeType (true, TypeCode.Empty);
3608 // 2005/01/10: The docs say this should throw an InvalidCastException,
3609 // however, MS.NET 1.1 throws a NullReferenceException. Assuming docs
3612 [ExpectedException (typeof (InvalidCastException))]
3614 [ExpectedException (typeof (NullReferenceException))]
3616 public void ChangeTypeNullToValuetype ()
3618 Convert.ChangeType (null, typeof (int));
3622 public void ToString_MinMax_WithBase ()
3624 AssertEquals ("Byte.MinValue base 2", "0", Convert.ToString (Byte.MinValue, 2));
3625 AssertEquals ("Byte.MinValue base 8", "0", Convert.ToString (Byte.MinValue, 8));
3626 AssertEquals ("Byte.MinValue base 10", "0", Convert.ToString (Byte.MinValue, 10));
3627 AssertEquals ("Byte.MinValue base 16", "0", Convert.ToString (Byte.MinValue, 16));
3629 AssertEquals ("Byte.MaxValue base 2", "11111111", Convert.ToString (Byte.MaxValue, 2));
3630 AssertEquals ("Byte.MaxValue base 8", "377", Convert.ToString (Byte.MaxValue, 8));
3631 AssertEquals ("Byte.MaxValue base 10", "255", Convert.ToString (Byte.MaxValue, 10));
3632 AssertEquals ("Byte.MaxValue base 16", "ff", Convert.ToString (Byte.MaxValue, 16));
3634 AssertEquals ("Int16.MinValue base 2", "1000000000000000", Convert.ToString (Int16.MinValue, 2));
3635 AssertEquals ("Int16.MinValue base 8", "100000", Convert.ToString (Int16.MinValue, 8));
3636 AssertEquals ("Int16.MinValue base 10", "-32768", Convert.ToString (Int16.MinValue, 10));
3637 AssertEquals ("Int16.MinValue base 16", "8000", Convert.ToString (Int16.MinValue, 16));
3639 AssertEquals ("Int16.MaxValue base 2", "111111111111111", Convert.ToString (Int16.MaxValue, 2));
3640 AssertEquals ("Int16.MaxValue base 8", "77777", Convert.ToString (Int16.MaxValue, 8));
3641 AssertEquals ("Int16.MaxValue base 10", "32767", Convert.ToString (Int16.MaxValue, 10));
3642 AssertEquals ("Int16.MaxValue base 16", "7fff", Convert.ToString (Int16.MaxValue, 16));
3644 AssertEquals ("Int32.MinValue base 2", "10000000000000000000000000000000", Convert.ToString (Int32.MinValue, 2));
3645 AssertEquals ("Int32.MinValue base 8", "20000000000", Convert.ToString (Int32.MinValue, 8));
3646 AssertEquals ("Int32.MinValue base 10", "-2147483648", Convert.ToString (Int32.MinValue, 10));
3647 AssertEquals ("Int32.MinValue base 16", "80000000", Convert.ToString (Int32.MinValue, 16));
3649 AssertEquals ("Int32.MaxValue base 2", "1111111111111111111111111111111", Convert.ToString (Int32.MaxValue, 2));
3650 AssertEquals ("Int32.MaxValue base 8", "17777777777", Convert.ToString (Int32.MaxValue, 8));
3651 AssertEquals ("Int32.MaxValue base 10", "2147483647", Convert.ToString (Int32.MaxValue, 10));
3652 AssertEquals ("Int32.MaxValue base 16", "7fffffff", Convert.ToString (Int32.MaxValue, 16));
3654 AssertEquals ("Int64.MinValue base 2", "1000000000000000000000000000000000000000000000000000000000000000", Convert.ToString (Int64.MinValue, 2));
3655 AssertEquals ("Int64.MinValue base 8", "1000000000000000000000", Convert.ToString (Int64.MinValue, 8));
3656 AssertEquals ("Int64.MinValue base 10", "-9223372036854775808", Convert.ToString (Int64.MinValue, 10));
3657 AssertEquals ("Int64.MinValue base 16", "8000000000000000", Convert.ToString (Int64.MinValue, 16));
3659 AssertEquals ("Int64.MaxValue base 2", "111111111111111111111111111111111111111111111111111111111111111", Convert.ToString (Int64.MaxValue, 2));
3660 AssertEquals ("Int64.MaxValue base 8", "777777777777777777777", Convert.ToString (Int64.MaxValue, 8));
3661 AssertEquals ("Int64.MaxValue base 10", "9223372036854775807", Convert.ToString (Int64.MaxValue, 10));
3662 AssertEquals ("Int64.MaxValue base 16", "7fffffffffffffff", Convert.ToString (Int64.MaxValue, 16));
3666 [ExpectedException (typeof (FormatException))]
3667 public void ToByte_BadHexPrefix1 ()
3669 Convert.ToByte ("#10", 16);
3673 [ExpectedException (typeof (FormatException))]
3674 public void ToByte_BadHexPrefix2 ()
3676 Convert.ToByte ("&H10", 16);
3680 [ExpectedException (typeof (FormatException))]
3681 public void ToByte_BadHexPrefix3 ()
3683 Convert.ToByte ("&h10", 16);
3687 [ExpectedException (typeof (FormatException))]
3688 public void ToInt16_BadHexPrefix1 ()
3690 Convert.ToInt16 ("#10", 16);
3694 [ExpectedException (typeof (FormatException))]
3695 public void ToInt16_BadHexPrefix2 ()
3697 Convert.ToInt16 ("&H10", 16);
3701 [ExpectedException (typeof (FormatException))]
3702 public void ToInt16_BadHexPrefix3 ()
3704 Convert.ToInt16 ("&h10", 16);
3708 [ExpectedException (typeof (FormatException))]
3709 public void ToInt32_BadHexPrefix1 ()
3711 Convert.ToInt32 ("#10", 16);
3715 [ExpectedException (typeof (FormatException))]
3716 public void ToInt32_BadHexPrefix2 ()
3718 Convert.ToInt32 ("&H10", 16);
3722 [ExpectedException (typeof (FormatException))]
3723 public void ToInt32_BadHexPrefix3 ()
3725 Convert.ToInt32 ("&h10", 16);
3729 [ExpectedException (typeof (FormatException))]
3730 public void ToInt64_BadHexPrefix1 ()
3732 Convert.ToInt64 ("#10", 16);
3736 [ExpectedException (typeof (FormatException))]
3737 public void ToInt64_BadHexPrefix2 ()
3739 Convert.ToInt64 ("&H10", 16);
3743 [ExpectedException (typeof (FormatException))]
3744 public void ToInt64_BadHexPrefix3 ()
3746 Convert.ToInt64 ("&h10", 16);
3750 [ExpectedException (typeof (FormatException))]
3751 public void ToSByte_BadHexPrefix1 ()
3753 Convert.ToSByte ("#10", 16);
3757 [ExpectedException (typeof (FormatException))]
3758 public void ToSByte_BadHexPrefix2 ()
3760 Convert.ToSByte ("&H10", 16);
3764 [ExpectedException (typeof (FormatException))]
3765 public void ToSByte_BadHexPrefix3 ()
3767 Convert.ToSByte ("&h10", 16);
3771 [ExpectedException (typeof (FormatException))]
3772 public void ToUInt16_BadHexPrefix1 ()
3774 Convert.ToUInt16 ("#10", 16);
3778 [ExpectedException (typeof (FormatException))]
3779 public void ToUInt16_BadHexPrefix2 ()
3781 Convert.ToUInt16 ("&H10", 16);
3785 [ExpectedException (typeof (FormatException))]
3786 public void ToUInt16_BadHexPrefix3 ()
3788 Convert.ToUInt16 ("&h10", 16);
3792 [ExpectedException (typeof (FormatException))]
3793 public void ToUInt32_BadHexPrefix1 ()
3795 Convert.ToUInt32 ("#10", 16);
3799 [ExpectedException (typeof (FormatException))]
3800 public void ToUInt32_BadHexPrefix2 ()
3802 Convert.ToUInt32 ("&H10", 16);
3806 [ExpectedException (typeof (FormatException))]
3807 public void ToUInt32_BadHexPrefix3 ()
3809 Convert.ToUInt32 ("&h10", 16);
3813 [ExpectedException (typeof (FormatException))]
3814 public void ToUInt64_BadHexPrefix1 ()
3816 Convert.ToUInt64 ("#10", 16);
3820 [ExpectedException (typeof (FormatException))]
3821 public void ToUInt64_BadHexPrefix2 ()
3823 Convert.ToUInt64 ("&H10", 16);
3827 [ExpectedException (typeof (FormatException))]
3828 public void ToUInt64_BadHexPrefix3 ()
3830 Convert.ToUInt64 ("&h10", 16);
3834 public void ToSByte_Base16_MinMax ()
3836 AssertEquals ("80,16", SByte.MinValue, Convert.ToSByte ("80", 16));
3837 AssertEquals ("0x80,16", SByte.MinValue, Convert.ToSByte ("0x80", 16));
3838 AssertEquals ("0X80,16", SByte.MinValue, Convert.ToSByte ("0X80", 16));
3840 AssertEquals ("7f,16", SByte.MaxValue, Convert.ToSByte ("7f", 16));
3841 AssertEquals ("7F,16", SByte.MaxValue, Convert.ToSByte ("7F", 16));
3842 AssertEquals ("0x7f,16", SByte.MaxValue, Convert.ToSByte ("0x7f", 16));
3843 AssertEquals ("0X7F,16", SByte.MaxValue, Convert.ToSByte ("0X7F", 16));
3847 public void ToInt16_Base16_MinMax ()
3849 AssertEquals ("8000,16", short.MinValue, Convert.ToInt16 ("8000", 16));
3850 AssertEquals ("0x8000,16", short.MinValue, Convert.ToInt16 ("0x8000", 16));
3851 AssertEquals ("0X8000,16", short.MinValue, Convert.ToInt16 ("0X8000", 16));
3853 AssertEquals ("7fff,16", short.MaxValue, Convert.ToInt16 ("7fff", 16));
3854 AssertEquals ("7FFF,16", short.MaxValue, Convert.ToInt16 ("7FFF", 16));
3855 AssertEquals ("0x7fff,16", short.MaxValue, Convert.ToInt16 ("0x7fff", 16));
3856 AssertEquals ("0X7FFF,16", short.MaxValue, Convert.ToInt16 ("0X7FFF", 16));
3860 public void ToInt32_Base16_MinMax ()
3862 AssertEquals ("80000000,16", int.MinValue, Convert.ToInt32 ("80000000", 16));
3863 AssertEquals ("0x80000000,16", int.MinValue, Convert.ToInt32 ("0x80000000", 16));
3864 AssertEquals ("0X80000000,16", int.MinValue, Convert.ToInt32 ("0X80000000", 16));
3866 AssertEquals ("7fffffff,16", int.MaxValue, Convert.ToInt32 ("7fffffff", 16));
3867 AssertEquals ("7FFFFFFF,16", int.MaxValue, Convert.ToInt32 ("7FFFFFFF", 16));
3868 AssertEquals ("0x7fffffff,16", int.MaxValue, Convert.ToInt32 ("0x7fffffff", 16));
3869 AssertEquals ("0X7FFFFFFF,16", int.MaxValue, Convert.ToInt32 ("0X7FFFFFFF", 16));
3873 [ExpectedException (typeof (FormatException))]
3874 public void ToByte_Base10_InvalidChars1 ()
3876 Convert.ToByte ("0-1", 10);
3880 [ExpectedException (typeof (FormatException))]
3881 public void ToByte_Base10_InvalidChars2 ()
3883 Convert.ToByte ("FF", 10);
3887 [ExpectedException (typeof (FormatException))]
3888 public void ToInt16_Base10_InvalidChars1 ()
3890 Convert.ToInt16 ("0-1", 10);
3894 [ExpectedException (typeof (FormatException))]
3895 public void ToInt16_Base10_InvalidChars2 ()
3897 Convert.ToInt16 ("FF", 10);
3901 [ExpectedException (typeof (FormatException))]
3902 public void ToInt32_Base10_InvalidChars1 ()
3904 Convert.ToInt32 ("0-1", 10);
3908 [ExpectedException (typeof (FormatException))]
3909 public void ToInt32_Base10_InvalidChars2 ()
3911 Convert.ToInt32 ("FF", 10);
3915 [ExpectedException (typeof (FormatException))]
3916 public void ToInt64_Base10_InvalidChars1 ()
3918 Convert.ToInt64 ("0-1", 10);
3922 [ExpectedException (typeof (FormatException))]
3923 public void ToInt64_Base10_InvalidChars2 ()
3925 Convert.ToInt64 ("FF", 10);
3929 [ExpectedException (typeof (FormatException))]
3930 public void ToSByte_Base10_InvalidChars1 ()
3932 Convert.ToSByte ("0-1", 10);
3936 [ExpectedException (typeof (FormatException))]
3937 public void ToSByte_Base10_InvalidChars2 ()
3939 Convert.ToSByte ("FF", 10);
3943 [ExpectedException (typeof (FormatException))]
3944 public void ToUInt16_Base10_InvalidChars1 ()
3946 Convert.ToUInt16 ("0-1", 10);
3950 [ExpectedException (typeof (FormatException))]
3951 public void ToUInt16_Base10_InvalidChars2 ()
3953 Convert.ToUInt16 ("FF", 10);
3957 [ExpectedException (typeof (FormatException))]
3958 public void ToUInt32_Base10_InvalidChars1 ()
3960 Convert.ToUInt32 ("0-1", 10);
3964 [ExpectedException (typeof (FormatException))]
3965 public void ToUInt32_Base10_InvalidChars2 ()
3967 Convert.ToUInt32 ("FF", 10);
3971 [ExpectedException (typeof (FormatException))]
3972 public void ToUInt64_Base10_InvalidChars1 ()
3974 Convert.ToUInt64 ("0-1", 10);
3978 [ExpectedException (typeof (FormatException))]
3979 public void ToUInt64_Base10_InvalidChars2 ()
3981 Convert.ToUInt64 ("FF", 10);
3985 [ExpectedException (typeof (FormatException))]
3986 public void ToByte_Base16_InvalidChars1 ()
3988 Convert.ToByte ("0-1", 16);
3992 [ExpectedException (typeof (FormatException))]
3993 public void ToByte_Base16_InvalidChars2 ()
3995 Convert.ToByte ("GG", 16);
3999 [ExpectedException (typeof (FormatException))]
4000 public void ToInt16_Base16_InvalidChars1 ()
4002 Convert.ToInt16 ("0-1", 16);
4006 [ExpectedException (typeof (FormatException))]
4007 public void ToInt16_Base16_InvalidChars2 ()
4009 Convert.ToInt16 ("GG", 16);
4013 [ExpectedException (typeof (FormatException))]
4014 public void ToInt32_Base16_InvalidChars1 ()
4016 Convert.ToInt32 ("0-1", 16);
4020 [ExpectedException (typeof (FormatException))]
4021 public void ToInt32_Base16_InvalidChars2 ()
4023 Convert.ToInt32 ("GG", 16);
4027 [ExpectedException (typeof (FormatException))]
4028 public void ToInt64_Base16_InvalidChars1 ()
4030 Convert.ToInt64 ("0-1", 16);
4034 [ExpectedException (typeof (FormatException))]
4035 public void ToInt64_Base16_InvalidChars2 ()
4037 Convert.ToInt64 ("GG", 16);
4041 [ExpectedException (typeof (FormatException))]
4042 public void ToSByte_Base16_InvalidChars1 ()
4044 Convert.ToSByte ("0-1", 16);
4048 [ExpectedException (typeof (FormatException))]
4049 public void ToSByte_Base16_InvalidChars2 ()
4051 Convert.ToSByte ("GG", 16);
4055 [ExpectedException (typeof (FormatException))]
4056 public void ToUInt16_Base16_InvalidChars1 ()
4058 Convert.ToUInt16 ("0-1", 16);
4062 [ExpectedException (typeof (FormatException))]
4063 public void ToUInt16_Base16_InvalidChars2 ()
4065 Convert.ToUInt16 ("GG", 16);
4069 [ExpectedException (typeof (FormatException))]
4070 public void ToUInt32_Base16_InvalidChars1 ()
4072 Convert.ToUInt32 ("0-1", 16);
4076 [ExpectedException (typeof (FormatException))]
4077 public void ToUInt32_Base16_InvalidChars2 ()
4079 Convert.ToUInt32 ("GG", 16);
4083 [ExpectedException (typeof (FormatException))]
4084 public void ToUInt64_Base16_InvalidChars1 ()
4086 Convert.ToUInt64 ("0-1", 16);
4090 [ExpectedException (typeof (FormatException))]
4091 public void ToUInt64_Base16_InvalidChars2 ()
4093 Convert.ToUInt64 ("GG", 16);
4097 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4098 public void ToByte_Base2_Empty ()
4100 Convert.ToByte ("", 2);
4104 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4105 public void ToByte_Base8_Empty ()
4107 Convert.ToByte ("", 8);
4111 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4112 public void ToByte_Base10_Empty ()
4114 Convert.ToByte ("", 10);
4118 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4119 public void ToByte_Base16_Empty ()
4121 Convert.ToByte ("", 16);
4125 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4126 public void ToInt16_Base2_Empty ()
4128 Convert.ToInt16 ("", 2);
4132 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4133 public void ToInt16_Base8_Empty ()
4135 Convert.ToInt16 ("", 8);
4139 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4140 public void ToInt16_Base10_Empty ()
4142 Convert.ToInt16 ("", 10);
4146 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4147 public void ToInt16_Base16_Empty ()
4149 Convert.ToInt16 ("", 16);
4153 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4154 public void ToInt32_Base2_Empty ()
4156 Convert.ToInt32 ("", 2);
4160 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4161 public void ToInt32_Base8_Empty ()
4163 Convert.ToInt32 ("", 8);
4167 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4168 public void ToInt32_Base10_Empty ()
4170 Convert.ToInt32 ("", 10);
4174 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4175 public void ToInt32_Base16_Empty ()
4177 Convert.ToInt32 ("", 16);
4181 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4182 public void ToInt64_Base2_Empty ()
4184 Convert.ToInt64 ("", 2);
4188 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4189 public void ToInt64_Base8_Empty ()
4191 Convert.ToInt64 ("", 8);
4195 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4196 public void ToInt64_Base10_Empty ()
4198 Convert.ToInt64 ("", 10);
4202 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4203 public void ToInt64_Base16_Empty ()
4205 Convert.ToInt64 ("", 16);
4209 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4210 public void ToSByte_Base2_Empty ()
4212 Convert.ToSByte ("", 2);
4216 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4217 public void ToSByte_Base8_Empty ()
4219 Convert.ToSByte ("", 8);
4223 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4224 public void ToSByte_Base10_Empty ()
4226 Convert.ToSByte ("", 10);
4230 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4231 public void ToSByte_Base16_Empty ()
4233 Convert.ToSByte ("", 16);
4237 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4238 public void ToUInt16_Base2_Empty ()
4240 Convert.ToUInt16 ("", 2);
4244 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4245 public void ToUInt16_Base8_Empty ()
4247 Convert.ToUInt16 ("", 8);
4251 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4252 public void ToUInt16_Base10_Empty ()
4254 Convert.ToUInt16 ("", 10);
4258 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4259 public void ToUInt16_Base16_Empty ()
4261 Convert.ToUInt16 ("", 16);
4265 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4266 public void ToUInt32_Base2_Empty ()
4268 Convert.ToUInt32 ("", 2);
4272 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4273 public void ToUInt32_Base8_Empty ()
4275 Convert.ToUInt32 ("", 8);
4279 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4280 public void ToUInt32_Base10_Empty ()
4282 Convert.ToUInt32 ("", 10);
4286 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4287 public void ToUInt32_Base16_Empty ()
4289 Convert.ToUInt32 ("", 16);
4293 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4294 public void ToUInt64_Base2_Empty ()
4296 Convert.ToUInt64 ("", 2);
4300 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4301 public void ToUInt64_Base8_Empty ()
4303 Convert.ToUInt64 ("", 8);
4307 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4308 public void ToUInt64_Base10_Empty ()
4310 Convert.ToUInt64 ("", 10);
4314 [ExpectedException (typeof (ArgumentOutOfRangeException))]
4315 public void ToUInt64_Base16_Empty ()
4317 Convert.ToUInt64 ("", 16);
4321 [ExpectedException (typeof (FormatException))]
4322 public void ToByte_HexPrefixOnly ()
4324 Convert.ToByte ("0x", 16);
4328 [ExpectedException (typeof (FormatException))]
4329 public void ToInt16_HexPrefixOnly ()
4331 Convert.ToInt16 ("0x", 16);
4335 [ExpectedException (typeof (FormatException))]
4336 public void ToInt32_HexPrefixOnly ()
4338 Convert.ToInt32 ("0x", 16);
4342 [ExpectedException (typeof (FormatException))]
4343 public void ToInt64_HexPrefixOnly ()
4345 Convert.ToInt64 ("0x", 16);
4349 [ExpectedException (typeof (FormatException))]
4350 public void ToSByte_HexPrefixOnly ()
4352 Convert.ToSByte ("0x", 16);
4356 [ExpectedException (typeof (FormatException))]
4357 public void ToUInt16_HexPrefixOnly ()
4359 Convert.ToUInt16 ("0x", 16);
4363 [ExpectedException (typeof (FormatException))]
4364 public void ToUInt32_HexPrefixOnly ()
4366 Convert.ToUInt32 ("0x", 16);
4370 [ExpectedException (typeof (FormatException))]
4371 public void ToUInt64_HexPrefixOnly ()
4373 Convert.ToUInt64 ("0x", 16);
4377 [ExpectedException (typeof (ArgumentException))]
4378 public void ToByte_Base2_NegativeSignOnly ()
4380 Convert.ToByte ("-", 2);
4384 [ExpectedException (typeof (ArgumentException))]
4385 public void ToByte_Base8_NegativeSignOnly ()
4387 Convert.ToByte ("-", 8);
4391 [ExpectedException (typeof (OverflowException))]
4392 public void ToByte_Base10_NegativeSignOnly ()
4394 Convert.ToByte ("-", 10);
4398 [ExpectedException (typeof (ArgumentException))]
4399 public void ToByte_Base16_NegativeSignOnly ()
4401 Convert.ToByte ("-", 16);
4405 [ExpectedException (typeof (ArgumentException))]
4406 public void ToInt16_Base2_NegativeSignOnly ()
4408 Convert.ToInt16 ("-", 2);
4412 [ExpectedException (typeof (ArgumentException))]
4413 public void ToInt16_Base8_NegativeSignOnly ()
4415 Convert.ToInt16 ("-", 8);
4419 [ExpectedException (typeof (FormatException))]
4420 public void ToInt16_Base10_NegativeSignOnly ()
4422 Convert.ToInt16 ("-", 10);
4426 [ExpectedException (typeof (ArgumentException))]
4427 public void ToInt16_Base16_NegativeSignOnly ()
4429 Convert.ToInt16 ("-", 16);
4433 [ExpectedException (typeof (ArgumentException))]
4434 public void ToInt32_Base2_NegativeSignOnly ()
4436 Convert.ToInt32 ("-", 2);
4440 [ExpectedException (typeof (ArgumentException))]
4441 public void ToInt32_Base8_NegativeSignOnly ()
4443 Convert.ToInt32 ("-", 8);
4447 [ExpectedException (typeof (FormatException))]
4448 public void ToInt32_Base10_NegativeSignOnly ()
4450 Convert.ToInt32 ("-", 10);
4454 [ExpectedException (typeof (ArgumentException))]
4455 public void ToInt32_Base16_NegativeSignOnly ()
4457 Convert.ToInt32 ("-", 16);
4461 [ExpectedException (typeof (ArgumentException))]
4462 public void ToInt64_Base2_NegativeSignOnly ()
4464 Convert.ToInt64 ("-", 2);
4468 [ExpectedException (typeof (ArgumentException))]
4469 public void ToInt64_Base8_NegativeSignOnly ()
4471 Convert.ToInt64 ("-", 8);
4475 [ExpectedException (typeof (FormatException))]
4476 public void ToInt64_Base10_NegativeSignOnly ()
4478 Convert.ToInt64 ("-", 10);
4482 [ExpectedException (typeof (ArgumentException))]
4483 public void ToInt64_Base16_NegativeSignOnly ()
4485 Convert.ToInt64 ("-", 16);
4489 [ExpectedException (typeof (ArgumentException))]
4490 public void ToSByte_Base2_NegativeSignOnly ()
4492 Convert.ToSByte ("-", 2);
4496 [ExpectedException (typeof (ArgumentException))]
4497 public void ToSByte_Base8_NegativeSignOnly ()
4499 Convert.ToSByte ("-", 8);
4503 [ExpectedException (typeof (FormatException))]
4504 public void ToSByte_Base10_NegativeSignOnly ()
4506 Convert.ToSByte ("-", 10);
4510 [ExpectedException (typeof (ArgumentException))]
4511 public void ToSByte_Base16_NegativeSignOnly ()
4513 Convert.ToSByte ("-", 16);
4517 [ExpectedException (typeof (ArgumentException))]
4518 public void ToUInt16_Base2_NegativeSignOnly ()
4520 Convert.ToUInt16 ("-", 2);
4524 [ExpectedException (typeof (ArgumentException))]
4525 public void ToUInt16_Base8_NegativeSignOnly ()
4527 Convert.ToUInt16 ("-", 8);
4531 [ExpectedException (typeof (OverflowException))]
4532 public void ToUInt16_Base10_NegativeSignOnly ()
4534 Convert.ToUInt16 ("-", 10);
4538 [ExpectedException (typeof (ArgumentException))]
4539 public void ToUInt16_Base16_NegativeSignOnly ()
4541 Convert.ToUInt16 ("-", 16);
4545 [ExpectedException (typeof (ArgumentException))]
4546 public void ToUInt32_Base2_NegativeSignOnly ()
4548 Convert.ToUInt32 ("-", 2);
4552 [ExpectedException (typeof (ArgumentException))]
4553 public void ToUInt32_Base8_NegativeSignOnly ()
4555 Convert.ToUInt32 ("-", 8);
4559 [ExpectedException (typeof (OverflowException))]
4560 public void ToUInt32_Base10_NegativeSignOnly ()
4562 Convert.ToUInt32 ("-", 10);
4566 [ExpectedException (typeof (ArgumentException))]
4567 public void ToUInt32_Base16_NegativeSignOnly ()
4569 Convert.ToUInt32 ("-", 16);
4573 [ExpectedException (typeof (ArgumentException))]
4574 public void ToUInt64_Base2_NegativeSignOnly ()
4576 Convert.ToUInt64 ("-", 2);
4580 [ExpectedException (typeof (ArgumentException))]
4581 public void ToUInt64_Base8_NegativeSignOnly ()
4583 Convert.ToUInt64 ("-", 8);
4587 [ExpectedException (typeof (OverflowException))]
4588 public void ToUInt64_Base10_NegativeSignOnly ()
4590 Convert.ToUInt64 ("-", 10);
4594 [ExpectedException (typeof (ArgumentException))]
4595 public void ToUInt64_Base16_NegativeSignOnly ()
4597 Convert.ToUInt64 ("-", 16);
4600 [Test] // bug #481687
4601 public void ChangeType_Value_IConvertible ()
4603 BitmapStatus bitmapStatus = new BitmapStatus (3);
4604 Image c1 = Convert.ChangeType (bitmapStatus, typeof (Image)) as Image;
4605 AssertNotNull ("#A1", c1);
4606 AssertEquals ("#A2", 32, c1.ColorDepth);
4608 bitmapStatus.ConvertToImage = false;
4609 object c2 = Convert.ChangeType (bitmapStatus, typeof (Image));
4610 AssertNull ("#B", c2);
4612 object c3 = Convert.ChangeType (bitmapStatus, typeof (int));
4613 AssertNotNull ("#C1", c3);
4614 AssertEquals ("#C2", 3, c3);
4617 // This is a simple and happy struct.
4622 [ExpectedException (typeof (InvalidCastException))]
4623 public void ChangeType_ShouldThrowOnString ()
4625 Foo f = (Foo) Convert.ChangeType ("this-is-a-string", typeof (Foo));
4631 private int colorDepth;
4638 public Image (int colorDepth)
4640 this.colorDepth = colorDepth;
4643 public int ColorDepth {
4644 get { return colorDepth; }
4648 public class BitmapStatus : IConvertible
4650 protected int m_Status;
4651 private bool convertToImage;
4653 public BitmapStatus (int status)
4656 convertToImage = true;
4659 public bool ConvertToImage {
4660 get { return convertToImage; }
4661 set { convertToImage = value; }
4664 TypeCode IConvertible.GetTypeCode ()
4666 return TypeCode.Int32;
4669 bool IConvertible.ToBoolean (IFormatProvider provider)
4671 return (bool)((IConvertible)this).ToType (typeof (bool), provider);
4674 byte IConvertible.ToByte (IFormatProvider provider)
4676 return (byte)((IConvertible)this).ToType (typeof (byte), provider);
4679 char IConvertible.ToChar (IFormatProvider provider)
4681 return (char)((IConvertible)this).ToType (typeof (char), provider);
4684 DateTime IConvertible.ToDateTime (IFormatProvider provider)
4686 return (DateTime)((IConvertible)this).ToType (typeof (DateTime), provider);
4689 decimal IConvertible.ToDecimal (IFormatProvider provider)
4691 return (decimal)((IConvertible)this).ToType (typeof (decimal), provider);
4694 double IConvertible.ToDouble (IFormatProvider provider)
4696 return (double)((IConvertible)this).ToType (typeof (double), provider);
4699 short IConvertible.ToInt16 (IFormatProvider provider)
4701 return (short)((IConvertible)this).ToType (typeof (short), provider);
4704 int IConvertible.ToInt32 (IFormatProvider provider)
4706 return (int)m_Status;
4709 long IConvertible.ToInt64 (IFormatProvider provider)
4711 return (long)((IConvertible)this).ToType (typeof (long), provider);
4714 sbyte IConvertible.ToSByte (IFormatProvider provider)
4716 return (sbyte)((IConvertible)this).ToType (typeof (sbyte), provider);
4719 float IConvertible.ToSingle (IFormatProvider provider)
4721 return (float)((IConvertible)this).ToType (typeof (float), provider);
4724 string IConvertible.ToString (IFormatProvider provider)
4726 return (string)((IConvertible)this).ToType (typeof (string), provider);
4729 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
4731 if (ConvertToImage && conversionType == typeof (Image))
4732 return new Image (32);
4733 else if (conversionType.IsAssignableFrom (typeof (int)))
4734 return Convert.ChangeType (1, conversionType, provider);
4738 ushort IConvertible.ToUInt16 (IFormatProvider provider)
4740 return (ushort)((IConvertible)this).ToType (typeof (ushort), provider);
4743 uint IConvertible.ToUInt32 (IFormatProvider provider)
4745 return (uint)((IConvertible)this).ToType (typeof (uint), provider);
4748 ulong IConvertible.ToUInt64 (IFormatProvider provider)
4750 return (ulong)((IConvertible)this).ToType (typeof (ulong), provider);