(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System / ConvertTest.cs
blob1e18f832594946c20f990e3cf2c1f38a27ae29a3
1 // TestConvert.cs - NUnit Test Cases for System.Convert class
2 //
3 // Krister Hansson (ds99krha@thn.htu.se)
4 // Andreas Jonsson (ds99anjn@thn.htu.se)
5 //
6 // (C) Krister Hansson & Andreas Jonsson
7 //
9 using NUnit.Framework;
10 using System;
11 using System.Globalization;
13 namespace MonoTests.System {
15 [TestFixture]
16 public class ConvertTest : Assertion {
18 bool boolTrue;
19 bool boolFalse;
20 byte tryByte;
21 char tryChar;
22 DateTime tryDT;
23 decimal tryDec;
24 double tryDbl;
25 short tryInt16;
26 int tryInt32;
27 long tryInt64;
28 object tryObj;
29 sbyte trySByte;
30 float tryFloat;
31 string falseString;
32 string trueString;
33 string nullString;
34 string tryStr;
35 ushort tryUI16;
36 uint tryUI32;
37 ulong tryUI64;
38 CultureInfo ci;
40 [SetUp]
41 public void SetUp ()
43 boolTrue = true;
44 boolFalse = false;
45 tryByte = 0;
46 tryChar = 'a';
47 tryDT = new DateTime(2002,1,1);
48 tryDec = 1234.2345m;
49 tryDbl = 0;
50 tryInt16 = 1234;
51 tryInt32 = 12345;
52 tryInt64 = 123456789012;
53 tryObj = new Object();
54 trySByte = 123;
55 tryFloat = 1234.2345f;
56 falseString = "false";
57 trueString = "true";
58 nullString = "null";
59 tryStr = "foobar";
60 tryUI16 = 34567;
61 tryUI32 = 567891234;
62 tryUI64 = 0;
63 ci = new CultureInfo("en-US");
64 ci.NumberFormat.NumberDecimalDigits = 3;
67 public void TestChangeType() {
68 int iTest = 1;
69 try {
70 AssertEquals("#A01", (short)12345, Convert.ChangeType(tryInt32, typeof(short)));
71 iTest++;
72 AssertEquals("#A02", 'A', Convert.ChangeType(65, typeof(char)));
73 iTest++;
74 AssertEquals("#A03", 66, Convert.ChangeType('B', typeof(int)));
75 iTest++;
76 AssertEquals("#A04", ((ulong)12345), Convert.ChangeType(tryInt32, typeof(ulong)));
78 iTest++;
79 AssertEquals("#A05", true, Convert.ChangeType(tryDec, TypeCode.Boolean));
80 iTest++;
81 AssertEquals("#A06", 'f', Convert.ChangeType("f", TypeCode.Char));
82 iTest++;
83 AssertEquals("#A07", (decimal)123456789012, Convert.ChangeType(tryInt64, TypeCode.Decimal));
84 iTest++;
85 AssertEquals("#A08", (int)34567, Convert.ChangeType(tryUI16, TypeCode.Int32));
87 iTest++;
88 AssertEquals("#A09", (double)567891234, Convert.ChangeType(tryUI32, typeof(double), ci));
89 iTest++;
90 AssertEquals("#A10", (ushort)0, Convert.ChangeType(tryByte, typeof(ushort), ci));
91 iTest++;
92 AssertEquals("#A11", (decimal)567891234, Convert.ChangeType(tryUI32, typeof(decimal), ci));
93 iTest++;
94 AssertEquals("#A12", (float)1234, Convert.ChangeType(tryInt16, typeof(float), ci));
95 iTest++;
96 AssertEquals("#A13", null, Convert.ChangeType(null, null, ci));
98 iTest++;
99 AssertEquals("#A14", (decimal)0, Convert.ChangeType(tryByte, TypeCode.Decimal, ci));
100 iTest++;
101 AssertEquals("#A15", "f", Convert.ChangeType('f', TypeCode.String, ci));
102 iTest++;
103 AssertEquals("#A16", 'D', Convert.ChangeType(68, TypeCode.Char, ci));
104 iTest++;
105 AssertEquals("#A17", (long)34567, Convert.ChangeType(tryUI16, TypeCode.Int64, ci));
106 iTest++;
107 AssertEquals("#A18", null, Convert.ChangeType(null, TypeCode.Empty, ci));
108 } catch (Exception e) {
109 Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
112 try {
113 Convert.ChangeType(boolTrue, typeof(char));
114 Fail();
116 catch (Exception e) {
117 AssertEquals("#A25", typeof(InvalidCastException), e.GetType());
120 try {
121 Convert.ChangeType(tryChar, typeof(DateTime));
122 Fail();
124 catch (Exception e) {
125 AssertEquals("#A26", typeof(InvalidCastException), e.GetType());
128 try {
129 Convert.ChangeType(ci, TypeCode.String);
130 Fail();
132 catch (Exception e) {
133 AssertEquals("#A27", typeof(InvalidCastException), e.GetType());
136 try {
137 Convert.ChangeType(tryInt32, null);
138 Fail();
140 catch (Exception e) {
141 AssertEquals("#A28", typeof(ArgumentNullException), e.GetType());
144 try
146 Convert.ChangeType(boolTrue, typeof(DateTime), ci);
147 Fail();
149 catch (Exception e) {
150 AssertEquals("#A29", typeof(InvalidCastException), e.GetType());
153 try {
154 Convert.ChangeType(ci, typeof(DateTime), ci);
155 Fail();
157 catch (Exception e) {
158 AssertEquals("#A30", typeof(InvalidCastException), e.GetType());
161 /* Should throw ArgumentException but throws InvalidCastException
162 try {
163 Convert.ChangeType(tryUI32, typeof(FormatException), ci);
164 Fail();
166 catch (Exception e) {
167 AssertEquals("#A??", typeof(ArgumentException), e.GetType());
170 try {
171 Convert.ChangeType(tryUI32, TypeCode.Byte, ci);
172 Fail();
174 catch (Exception e) {
175 AssertEquals("#A31", typeof(OverflowException), e.GetType());
178 try {
179 Convert.ChangeType(boolTrue, TypeCode.Char, ci);
180 Fail();
182 catch (Exception e) {
183 AssertEquals("#A32", typeof(InvalidCastException), e.GetType());
186 try {
187 Convert.ChangeType(boolTrue, null, ci);
188 Fail();
190 catch (Exception e) {
191 AssertEquals("#A33", typeof(ArgumentNullException), e.GetType());
194 try {
195 /* should fail to convert string to any enumeration type. */
196 Convert.ChangeType("random string", typeof(DayOfWeek));
197 Fail();
199 catch (Exception e) {
200 AssertEquals("#A34", typeof(ArgumentException), e.GetType());
205 public void TestGetTypeCode() {
206 int marker = 1;
207 try {
208 AssertEquals("#B01", TypeCode.String, Convert.GetTypeCode(tryStr));
209 marker++;
210 AssertEquals("#B02", TypeCode.UInt16, Convert.GetTypeCode(tryUI16));
211 marker++;
212 AssertEquals("#B03", TypeCode.UInt32, Convert.GetTypeCode(tryUI32));
213 marker++;
214 AssertEquals("#B04", TypeCode.UInt64, Convert.GetTypeCode(tryUI64));
215 marker++;
216 AssertEquals("#B05", TypeCode.Double, Convert.GetTypeCode(tryDbl));
217 marker++;
218 AssertEquals("#B06", TypeCode.Int16, Convert.GetTypeCode(tryInt16));
219 marker++;
220 AssertEquals("#B07", TypeCode.Int64, Convert.GetTypeCode(tryInt64));
221 marker++;
222 AssertEquals("#B08", TypeCode.Object, Convert.GetTypeCode(tryObj));
223 marker++;
224 AssertEquals("#B09", TypeCode.SByte, Convert.GetTypeCode(trySByte));
225 marker++;
226 AssertEquals("#B10", TypeCode.Single, Convert.GetTypeCode(tryFloat));
227 marker++;
228 AssertEquals("#B11", TypeCode.Byte, Convert.GetTypeCode(tryByte));
229 marker++;
230 AssertEquals("#B12", TypeCode.Char, Convert.GetTypeCode(tryChar));
231 marker++;
232 // AssertEquals("#B13", TypeCode.DateTime, Convert.GetTypeCode(tryDT));
233 marker++;
234 AssertEquals("#B14", TypeCode.Decimal, Convert.GetTypeCode(tryDec));
235 marker++;
236 AssertEquals("#B15", TypeCode.Int32, Convert.GetTypeCode(tryInt32));
237 marker++;
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));
274 try {
275 Convert.ToBoolean(tryChar);
276 Fail();
278 catch (Exception e) {
279 AssertEquals("#D20", typeof(InvalidCastException), e.GetType());
282 try {
283 Convert.ToBoolean(tryDT);
284 Fail();
286 catch (Exception e) {
287 AssertEquals("#D21", typeof(InvalidCastException), e.GetType());
290 try {
291 Convert.ToBoolean(tryStr);
292 Fail();
294 catch (Exception e) {
295 AssertEquals("#D22", typeof(FormatException), e.GetType());
298 try {
299 Convert.ToBoolean(nullString);
300 Fail();
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));
330 try {
331 Convert.ToByte('\u03A9'); // sign of Omega on Win2k
332 Fail();
334 catch (Exception e) {
335 AssertEquals("#E25", typeof(OverflowException), e.GetType());
338 try {
339 Convert.ToByte(tryDT);
340 Fail();
342 catch (Exception e) {
343 AssertEquals("#D26", typeof(InvalidCastException), e.GetType());
346 try {
347 Convert.ToByte((decimal)22000);
348 Fail();
350 catch (Exception e) {
351 AssertEquals("#E27", typeof(OverflowException), e.GetType());
354 try {
355 Convert.ToByte((double)255.5);
356 Fail();
358 catch (Exception e) {
359 AssertEquals("#E28", typeof(OverflowException), e.GetType());
362 try {
363 Convert.ToByte(-tryInt16);
364 Fail();
366 catch (Exception e) {
367 AssertEquals("#E29", typeof(OverflowException), e.GetType());
370 try {
371 Convert.ToByte((int)-256);
372 Fail();
374 catch (Exception e) {
375 AssertEquals("#E30", typeof(OverflowException), e.GetType());
378 try {
379 Convert.ToByte(tryInt64);
380 Fail();
382 catch (Exception e) {
383 AssertEquals("#E31", typeof(OverflowException), e.GetType());
386 try {
387 Convert.ToByte((object)ci);
388 Fail();
390 catch (Exception e) {
391 AssertEquals("#E32", typeof(InvalidCastException), e.GetType());
394 try {
395 Convert.ToByte((sbyte)-1);
396 Fail();
398 catch (Exception e) {
399 AssertEquals("#E33", typeof(OverflowException), e.GetType());
402 try {
403 Convert.ToByte((float)-0.6f);
404 Fail();
406 catch (Exception e) {
407 AssertEquals("#E34", typeof(OverflowException), e.GetType());
410 try {
411 Convert.ToByte("1a1");
412 Fail();
414 catch (Exception e) {
415 AssertEquals("#E35", typeof(FormatException), e.GetType());
418 try {
419 Convert.ToByte("457");
420 Fail();
422 catch (Exception e) {
423 AssertEquals("#E36", typeof(OverflowException), e.GetType());
426 try {
427 Convert.ToByte((ushort)30000);
428 Fail();
430 catch (Exception e) {
431 AssertEquals("#E37", typeof(OverflowException), e.GetType());
434 try {
435 Convert.ToByte((uint)300);
436 Fail();
438 catch (Exception e) {
439 AssertEquals("#E38", typeof(OverflowException), e.GetType());
442 try {
443 Convert.ToByte((ulong)987654321321);
444 Fail();
446 catch (Exception e) {
447 AssertEquals("#E39", typeof(OverflowException), e.GetType());
450 try {
451 Convert.ToByte("10010111", 3);
452 Fail();
454 catch (Exception e) {
455 AssertEquals("#E40", typeof(ArgumentException), e.GetType());
458 try {
459 Convert.ToByte("3F3", 16);
460 Fail();
462 catch (Exception e) {
463 AssertEquals("#E41", typeof(OverflowException), e.GetType());
467 public void TestToChar(){
468 tryByte = 58;
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", 'È', Convert.ToChar((ulong)200));
479 AssertEquals("#F11", '{', Convert.ToChar((object)trySByte, ci));
480 AssertEquals("#F12", 'o', Convert.ToChar(tryStr.Substring(1,1), ci));
482 try {
483 Convert.ToChar(boolTrue);
484 Fail();
486 catch (Exception e) {
487 AssertEquals("#F20", typeof(InvalidCastException), e.GetType());
490 try {
491 Convert.ToChar(tryDT);
492 Fail();
494 catch (Exception e) {
495 AssertEquals("#F21", typeof(InvalidCastException), e.GetType());
498 try {
499 Convert.ToChar(tryDec);
500 Fail();
502 catch (Exception e) {
503 AssertEquals("#F22", typeof(InvalidCastException), e.GetType());
506 try {
507 Convert.ToChar(tryDbl);
508 Fail();
510 catch (Exception e) {
511 AssertEquals("#F23", typeof(InvalidCastException), e.GetType());
514 try {
515 Convert.ToChar((short)-1);
516 Fail();
518 catch (Exception e) {
519 AssertEquals("#F24", typeof(OverflowException), e.GetType());
522 try {
523 Convert.ToChar(Int32.MinValue);
524 Fail();
526 catch (Exception e) {
527 AssertEquals("#F25", typeof(OverflowException), e.GetType());
530 try {
531 Convert.ToChar(Int32.MaxValue);
532 Fail();
534 catch (Exception e) {
535 AssertEquals("#F26", typeof(OverflowException), e.GetType());
538 try {
539 Convert.ToChar(tryInt64);
540 Fail();
542 catch (Exception e) {
543 AssertEquals("#F27", typeof(OverflowException), e.GetType());
546 try {
547 Convert.ToChar((long)-123);
548 Fail();
550 catch (Exception e) {
551 AssertEquals("#F28", typeof(OverflowException), e.GetType());
554 try {
555 Convert.ToChar(ci);
556 Fail();
558 catch (Exception e) {
559 AssertEquals("#F29", typeof(InvalidCastException), e.GetType());
562 try {
563 Convert.ToChar(-trySByte);
564 Fail();
566 catch (Exception e) {
567 AssertEquals("#F30", typeof(OverflowException), e.GetType());
570 try {
571 Convert.ToChar(tryFloat);
572 Fail();
574 catch (Exception e) {
575 AssertEquals("#F31", typeof(InvalidCastException), e.GetType());
578 try {
579 Convert.ToChar("foo");
580 Fail();
582 catch (Exception e) {
583 AssertEquals("#F32", typeof(FormatException), e.GetType());
586 try {
587 Convert.ToChar(null);
588 Fail();
590 catch (Exception e) {
591 AssertEquals("#F33", typeof(ArgumentNullException), e.GetType());
594 try {
595 Convert.ToChar(new Exception(), ci);
596 Fail();
598 catch (Exception e) {
599 AssertEquals("#F34", typeof(InvalidCastException), e.GetType());
602 try {
603 Convert.ToChar(null, ci);
604 Fail();
606 catch (Exception e) {
607 AssertEquals("#F35", typeof(ArgumentNullException), e.GetType());
610 try {
611 Convert.ToChar("", ci);
612 Fail();
614 catch (Exception e) {
615 AssertEquals("#F36", typeof(FormatException), e.GetType());
618 try {
619 Convert.ToChar(tryStr, ci);
620 Fail();
622 catch (Exception e) {
623 AssertEquals("#F37", typeof(FormatException), e.GetType());
627 /*[Ignore ("http://bugzilla.ximian.com/show_bug.cgi?id=45286")]
628 [Test]
629 [ExpectedException (typeof (ArgumentOutOfRangeException))]
630 public void G22 () {
631 Convert.ToDateTime("20002-25-01");
632 } */
634 public void TestToDateTime() {
635 string dateString = "01/01/2002";
636 int iTest = 1;
637 try {
638 AssertEquals("#G01", tryDT, Convert.ToDateTime(tryDT));
639 iTest++;
640 AssertEquals("#G02", tryDT, Convert.ToDateTime(dateString));
641 iTest++;
642 AssertEquals("#G03", tryDT, Convert.ToDateTime(dateString, ci));
643 } catch (Exception e) {
644 Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
647 try {
648 Convert.ToDateTime(boolTrue);
649 Fail();
651 catch (Exception e) {
652 AssertEquals("#G10", typeof(InvalidCastException), e.GetType());
655 try {
656 Convert.ToDateTime(tryByte);
657 Fail();
659 catch (Exception e) {
660 AssertEquals("#G11", typeof(InvalidCastException), e.GetType());
663 try {
664 Convert.ToDateTime(tryChar);
665 Fail();
667 catch (Exception e) {
668 AssertEquals("#G12", typeof(InvalidCastException), e.GetType());
671 try {
672 Convert.ToDateTime(tryDec);
673 Fail();
675 catch (Exception e) {
676 AssertEquals("#G13", typeof(InvalidCastException), e.GetType());
679 try {
680 Convert.ToDateTime(tryDbl);
681 Fail();
683 catch (Exception e) {
684 AssertEquals("#G14", typeof(InvalidCastException), e.GetType());
687 try {
688 Convert.ToDateTime(tryInt16);
689 Fail();
691 catch (Exception e) {
692 AssertEquals("#G15", typeof(InvalidCastException), e.GetType());
695 try {
696 Convert.ToDateTime(tryInt32);
697 Fail();
699 catch (Exception e) {
700 AssertEquals("#G16", typeof(InvalidCastException), e.GetType());
703 try {
704 Convert.ToDateTime(tryInt64);
705 Fail();
707 catch (Exception e) {
708 AssertEquals("#G17", typeof(InvalidCastException), e.GetType());
711 try {
712 Convert.ToDateTime(ci);
713 Fail();
715 catch (Exception e) {
716 AssertEquals("#G18", typeof(InvalidCastException), e.GetType());
719 try {
720 Convert.ToDateTime(trySByte);
721 Fail();
723 catch (Exception e) {
724 AssertEquals("#G19", typeof(InvalidCastException), e.GetType());
727 try {
728 Convert.ToDateTime(tryFloat);
729 Fail();
731 catch (Exception e) {
732 AssertEquals("#G20", typeof(InvalidCastException), e.GetType());
735 try {
736 Convert.ToDateTime("20a2-01-01");
737 Fail();
739 catch (Exception e) {
740 AssertEquals("#G21", typeof(FormatException), e.GetType());
743 try {
744 Convert.ToDateTime(tryUI16);
745 Fail();
747 catch (Exception e) {
748 AssertEquals("#G23", typeof(InvalidCastException), e.GetType());
751 try {
752 Convert.ToDateTime(tryUI32);
753 Fail();
755 catch (Exception e) {
756 AssertEquals("#G24", typeof(InvalidCastException), e.GetType());
759 try {
760 Convert.ToDateTime(tryUI64);
761 Fail();
763 catch (Exception e) {
764 AssertEquals("#G25", typeof(InvalidCastException), e.GetType());
767 try {
768 Convert.ToDateTime(ci, ci);
769 Fail();
771 catch (Exception e) {
772 AssertEquals("#G26", typeof(InvalidCastException), e.GetType());
775 try {
776 Convert.ToDateTime("20a2-01-01", ci);
777 Fail();
779 catch (Exception e) {
780 AssertEquals("#G27", typeof(FormatException), e.GetType());
783 // this is supported by .net 1.1 (defect 41845)
784 try {
785 Convert.ToDateTime("20022-01-01");
786 Fail();
788 catch (Exception e) {
789 AssertEquals("#G28", typeof(ArgumentOutOfRangeException), e.GetType());
792 try {
793 Convert.ToDateTime("2002-21-01");
794 Fail();
796 catch (Exception e) {
797 AssertEquals("#G29", typeof(FormatException), e.GetType());
800 try {
801 Convert.ToDateTime("2002-111-01");
802 Fail();
804 catch (Exception e) {
805 AssertEquals("#G30", typeof(FormatException), e.GetType());
808 try {
809 Convert.ToDateTime("2002-01-41");
810 Fail();
812 catch (Exception e) {
813 AssertEquals("#G31", typeof(FormatException), e.GetType());
816 try {
817 Convert.ToDateTime("2002-01-111");
818 Fail();
820 catch (Exception e) {
821 AssertEquals("#G32", typeof(FormatException), e.GetType());
824 try {
825 AssertEquals("#G33", tryDT, Convert.ToDateTime("2002-01-01"));
826 } catch (Exception e) {
827 Fail ("Unexpected exception at #G33 " + e);
830 try {
831 Convert.ToDateTime("2002-01-11 34:11:11");
832 Fail();
834 catch (Exception e) {
835 AssertEquals("#G34", typeof(FormatException), e.GetType());
838 try {
839 Convert.ToDateTime("2002-01-11 11:70:11");
840 Fail();
842 catch (Exception e) {
843 AssertEquals("#G35", typeof(FormatException), e.GetType());
846 try {
847 Convert.ToDateTime("2002-01-11 11:11:70");
848 Fail();
850 catch (Exception e) {
851 AssertEquals("#G36", typeof(FormatException), e.GetType());
856 public void TestToDecimal() {
857 AssertEquals("#H01", (decimal)1, Convert.ToDecimal(boolTrue));
858 AssertEquals("#H02", (decimal)0, Convert.ToDecimal(boolFalse));
859 AssertEquals("#H03", (decimal)tryByte, Convert.ToDecimal(tryByte));
860 AssertEquals("#H04", tryDec, Convert.ToDecimal(tryDec));
861 AssertEquals("#H05", (decimal)tryDbl, Convert.ToDecimal(tryDbl));
862 AssertEquals("#H06", (decimal)tryInt16, Convert.ToDecimal(tryInt16));
863 AssertEquals("#H07", (decimal)tryInt32, Convert.ToDecimal(tryInt32));
864 AssertEquals("#H08", (decimal)tryInt64, Convert.ToDecimal(tryInt64));
865 AssertEquals("#H09", (decimal)trySByte, Convert.ToDecimal(trySByte));
866 AssertEquals("#H10", (decimal)tryFloat, Convert.ToDecimal(tryFloat));
867 string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
868 // AssertEquals("#H11", (decimal)23456.432, Convert.ToDecimal("23456" + sep + "432"));
869 // Note: changed because the number were the same but with a different base
870 // and this isn't a Convert bug (but a Decimal bug). See #60227 for more details.
871 // http://bugzilla.ximian.com/show_bug.cgi?id=60227
872 Assert ("#H11", Decimal.Equals (23456.432m, Convert.ToDecimal ("23456" + sep + "432")));
873 AssertEquals("#H12", (decimal)tryUI16, Convert.ToDecimal(tryUI16));
874 AssertEquals("#H13", (decimal)tryUI32, Convert.ToDecimal(tryUI32));
875 AssertEquals("#H14", (decimal)tryUI64, Convert.ToDecimal(tryUI64));
876 AssertEquals("#H15", (decimal)63784, Convert.ToDecimal("63784", ci));
878 try {
879 Convert.ToDecimal(tryChar);
880 Fail();
882 catch (Exception e) {
883 AssertEquals("#H20", typeof(InvalidCastException), e.GetType());
886 try {
887 Convert.ToDecimal(tryDT);
888 Fail();
890 catch (Exception e) {
891 AssertEquals("#H21", typeof(InvalidCastException), e.GetType());
894 try {
895 Convert.ToDecimal(double.MaxValue);
896 Fail();
898 catch (Exception e) {
899 AssertEquals("#H22", typeof(OverflowException), e.GetType());
902 try {
903 Convert.ToDecimal(double.MinValue);
904 Fail();
906 catch (Exception e) {
907 AssertEquals("#H23", typeof(OverflowException), e.GetType());
910 try {
911 Convert.ToDecimal(ci);
912 Fail();
914 catch (Exception e) {
915 AssertEquals("#H24", typeof(InvalidCastException), e.GetType());
918 try {
919 Convert.ToDecimal(tryStr);
920 Fail();
922 catch (Exception e) {
923 AssertEquals("#H25", typeof(FormatException), e.GetType());
926 try {
927 string maxDec = decimal.MaxValue.ToString();
928 maxDec = maxDec + "1";
929 Convert.ToDecimal(maxDec);
930 Fail();
932 catch (Exception e) {
933 AssertEquals("#H26", typeof(OverflowException), e.GetType());
936 try {
937 Convert.ToDecimal(ci, ci);
938 Fail();
940 catch (Exception e) {
941 AssertEquals("#H27", typeof(InvalidCastException), e.GetType());
944 try {
945 Convert.ToDecimal(tryStr, ci);
946 Fail();
948 catch (Exception e) {
949 AssertEquals("#H28", typeof(FormatException), e.GetType());
952 try {
953 string maxDec = decimal.MaxValue.ToString();
954 maxDec = maxDec + "1";
955 Convert.ToDecimal(maxDec, ci);
956 Fail();
958 catch (Exception e) {
959 AssertEquals("#H29", typeof(OverflowException), e.GetType());
963 public void TestToDouble() {
964 int iTest = 1;
965 try {
966 AssertEquals("#I01", (double)1, Convert.ToDouble(boolTrue));
967 iTest++;
968 AssertEquals("#I02", (double)0, Convert.ToDouble(boolFalse));
969 iTest++;
970 AssertEquals("#I03", (double)tryByte, Convert.ToDouble(tryByte));
971 iTest++;
972 AssertEquals("#I04", tryDbl, Convert.ToDouble(tryDbl));
973 iTest++;
974 AssertEquals("#I05", (double)tryDec, Convert.ToDouble(tryDec));
975 iTest++;
976 AssertEquals("#I06", (double)tryInt16, Convert.ToDouble(tryInt16));
977 iTest++;
978 AssertEquals("#I07", (double)tryInt32, Convert.ToDouble(tryInt32));
979 iTest++;
980 AssertEquals("#I08", (double)tryInt64, Convert.ToDouble(tryInt64));
981 iTest++;
982 AssertEquals("#I09", (double)trySByte, Convert.ToDouble(trySByte));
983 iTest++;
984 AssertEquals("#I10", (double)tryFloat, Convert.ToDouble(tryFloat));
985 iTest++;
986 string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
987 AssertEquals("#I11", (double)23456.432, Convert.ToDouble("23456" + sep + "432"));
988 iTest++;
989 AssertEquals("#I12", (double)tryUI16, Convert.ToDouble(tryUI16));
990 iTest++;
991 AssertEquals("#I13", (double)tryUI32, Convert.ToDouble(tryUI32));
992 iTest++;
993 AssertEquals("#I14", (double)tryUI64, Convert.ToDouble(tryUI64));
994 iTest++;
995 AssertEquals("#H15", (double)63784, Convert.ToDouble("63784", ci));
996 } catch (Exception e) {
997 Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1000 try {
1001 Convert.ToDouble(tryChar);
1002 Fail();
1004 catch (Exception e) {
1005 AssertEquals("#I20", typeof(InvalidCastException), e.GetType());
1008 try {
1009 Convert.ToDouble(tryDT);
1010 Fail();
1012 catch (Exception e) {
1013 AssertEquals("#I21", typeof(InvalidCastException), e.GetType());
1016 try {
1017 Convert.ToDouble(ci);
1018 Fail();
1020 catch (Exception e) {
1021 AssertEquals("#I22", typeof(InvalidCastException), e.GetType());
1024 try {
1025 Convert.ToDouble(tryStr);
1026 Fail();
1028 catch (Exception e) {
1029 AssertEquals("#I23", typeof(FormatException), e.GetType());
1032 try {
1033 string maxDec = double.MaxValue.ToString();
1034 maxDec = maxDec + "1";
1035 Convert.ToDouble(maxDec);
1036 Fail();
1038 catch (Exception e) {
1039 AssertEquals("#I24", typeof(OverflowException), e.GetType());
1042 try {
1043 Convert.ToDouble(ci, ci);
1044 Fail();
1046 catch (Exception e) {
1047 AssertEquals("#I25", typeof(InvalidCastException), e.GetType());
1050 try {
1051 Convert.ToDouble(tryStr, ci);
1052 Fail();
1054 catch (Exception e) {
1055 AssertEquals("#I26", typeof(FormatException), e.GetType());
1058 try {
1059 string maxDec = double.MaxValue.ToString();
1060 maxDec = maxDec + "1";
1061 Convert.ToDouble(maxDec, ci);
1062 Fail();
1064 catch (Exception e) {
1065 AssertEquals("#I27", typeof(OverflowException), e.GetType());
1068 try {
1069 Convert.ToDouble(tryObj, ci);
1070 Fail();
1072 catch (Exception e) {
1073 AssertEquals("#I28", typeof(InvalidCastException), e.GetType());
1077 public void TestToInt16() {
1078 AssertEquals("#J01", (short)0, Convert.ToInt16(boolFalse));
1079 AssertEquals("#J02", (short)1, Convert.ToInt16(boolTrue));
1080 AssertEquals("#J03", (short)97, Convert.ToInt16(tryChar));
1081 AssertEquals("#J04", (short)1234, Convert.ToInt16(tryDec));
1082 AssertEquals("#J05", (short)0, Convert.ToInt16(tryDbl));
1083 AssertEquals("#J06", (short)1234, Convert.ToInt16(tryInt16));
1084 AssertEquals("#J07", (short)12345, Convert.ToInt16(tryInt32));
1085 AssertEquals("#J08", (short)30000, Convert.ToInt16((long)30000));
1086 AssertEquals("#J09", (short)123, Convert.ToInt16(trySByte));
1087 AssertEquals("#J10", (short)1234, Convert.ToInt16(tryFloat));
1088 AssertEquals("#J11", (short)578, Convert.ToInt16("578"));
1089 AssertEquals("#J12", (short)15500, Convert.ToInt16((ushort)15500));
1090 AssertEquals("#J13", (short)5489, Convert.ToInt16((uint)5489));
1091 AssertEquals("#J14", (short)9876, Convert.ToInt16((ulong)9876));
1092 AssertEquals("#J15", (short)14, Convert.ToInt16("14", ci));
1093 AssertEquals("#J16", (short)11, Convert.ToInt16("01011", 2));
1094 AssertEquals("#J17", (short)1540, Convert.ToInt16("3004", 8));
1095 AssertEquals("#J18", (short)321, Convert.ToInt16("321", 10));
1096 AssertEquals("#J19", (short)2748, Convert.ToInt16("ABC", 16));
1098 try {
1099 Convert.ToInt16(char.MaxValue);
1100 Fail();
1102 catch (Exception e) {
1103 AssertEquals("#J25", typeof(OverflowException), e.GetType());
1106 try {
1107 Convert.ToInt16(tryDT);
1108 Fail();
1110 catch (Exception e) {
1111 AssertEquals("#J26", typeof(InvalidCastException), e.GetType());
1114 try {
1115 Convert.ToInt16((decimal)(short.MaxValue + 1));
1116 Fail();
1118 catch (Exception e) {
1119 AssertEquals("#J27", typeof(OverflowException), e.GetType());
1122 try {
1123 Convert.ToInt16((decimal)(short.MinValue - 1));
1124 Fail();
1126 catch (Exception e) {
1127 AssertEquals("#J28", typeof(OverflowException), e.GetType());
1130 try {
1131 Convert.ToInt16((double)(short.MaxValue + 1));
1132 Fail();
1134 catch (Exception e) {
1135 AssertEquals("#J29", typeof(OverflowException), e.GetType());
1138 try {
1139 Convert.ToInt16((double)(short.MinValue - 1));
1140 Fail();
1142 catch (Exception e) {
1143 AssertEquals("#J30", typeof(OverflowException), e.GetType());
1146 try {
1147 Convert.ToInt16(50000);
1148 Fail();
1150 catch (Exception e) {
1151 AssertEquals("#J31", typeof(OverflowException), e.GetType());
1154 try {
1155 Convert.ToInt16(-50000);
1156 Fail();
1158 catch (Exception e) {
1159 AssertEquals("#J32", typeof(OverflowException), e.GetType());
1162 try {
1163 Convert.ToInt16(tryInt64);
1164 Fail();
1166 catch (Exception e) {
1167 AssertEquals("#J33", typeof(OverflowException), e.GetType());
1170 try {
1171 Convert.ToInt16(-tryInt64);
1172 Fail();
1174 catch (Exception e) {
1175 AssertEquals("#J34", typeof(OverflowException), e.GetType());
1178 try {
1179 Convert.ToInt16(tryObj);
1180 Fail();
1182 catch (Exception e) {
1183 AssertEquals("#J35", typeof(InvalidCastException), e.GetType());
1186 try {
1187 Convert.ToInt16((float)32767.5);
1188 Fail();
1190 catch (Exception e) {
1191 AssertEquals("#J36", typeof(OverflowException), e.GetType());
1194 try {
1195 Convert.ToInt16((float)-33000.54);
1196 Fail();
1198 catch (Exception e) {
1199 AssertEquals("#J37", typeof(OverflowException), e.GetType());
1202 try {
1203 Convert.ToInt16(tryStr);
1204 Fail();
1206 catch (Exception e) {
1207 AssertEquals("#J38", typeof(FormatException), e.GetType());
1210 try {
1211 Convert.ToInt16("-33000");
1212 Fail();
1214 catch (Exception e) {
1215 AssertEquals("#J39", typeof(OverflowException), e.GetType());
1218 try {
1219 Convert.ToInt16(ushort.MaxValue);
1220 Fail();
1222 catch (Exception e) {
1223 AssertEquals("#J40", typeof(OverflowException), e.GetType());
1226 try {
1227 Convert.ToInt16(uint.MaxValue);
1228 Fail();
1230 catch (Exception e) {
1231 AssertEquals("#J41", typeof(OverflowException), e.GetType());
1234 try {
1235 Convert.ToInt16(ulong.MaxValue);
1236 Fail();
1238 catch (Exception e) {
1239 AssertEquals("#J42", typeof(OverflowException), e.GetType());
1242 try {
1243 Convert.ToInt16(tryObj, ci);
1244 Fail();
1246 catch (Exception e) {
1247 AssertEquals("#J43", typeof(InvalidCastException), e.GetType());
1250 try {
1251 Convert.ToInt16(tryStr, ci);
1252 Fail();
1254 catch (Exception e) {
1255 AssertEquals("#J44", typeof(FormatException), e.GetType());
1258 try {
1259 Convert.ToInt16("-33000", ci);
1260 Fail();
1262 catch (Exception e) {
1263 AssertEquals("#J45", typeof(OverflowException), e.GetType());
1266 try {
1267 Convert.ToInt16("321", 11);
1268 Fail();
1270 catch (Exception e) {
1271 AssertEquals("#J46", typeof(ArgumentException), e.GetType());
1274 try {
1275 Convert.ToInt16("D8BF1", 16);
1276 Fail();
1278 catch (Exception e) {
1279 AssertEquals("#J47", typeof(OverflowException), e.GetType());
1283 public void TestToInt32() {
1284 long tryMax = long.MaxValue;
1285 long tryMin = long.MinValue;
1286 AssertEquals("#K01", (int)0, Convert.ToInt32(boolFalse));
1287 AssertEquals("#K02", (int)1, Convert.ToInt32(boolTrue));
1288 AssertEquals("#K03", (int)0, Convert.ToInt32(tryByte));
1289 AssertEquals("#K04", (int)97, Convert.ToInt32(tryChar));
1290 AssertEquals("#K05", (int)1234, Convert.ToInt32(tryDec));
1291 AssertEquals("#K06", (int)0, Convert.ToInt32(tryDbl));
1292 AssertEquals("#K07", (int)1234, Convert.ToInt32(tryInt16));
1293 AssertEquals("#K08", (int)12345, Convert.ToInt32(tryInt32));
1294 AssertEquals("#K09", (int)60000, Convert.ToInt32((long)60000));
1295 AssertEquals("#K10", (int)123, Convert.ToInt32(trySByte));
1296 AssertEquals("#K11", (int)1234, Convert.ToInt32(tryFloat));
1297 AssertEquals("#K12", (int)9876, Convert.ToInt32((string)"9876"));
1298 AssertEquals("#K13", (int)34567, Convert.ToInt32(tryUI16));
1299 AssertEquals("#K14", (int)567891234, Convert.ToInt32(tryUI32));
1300 AssertEquals("#K15", (int)0, Convert.ToInt32(tryUI64));
1301 AssertEquals("#K16", (int)123, Convert.ToInt32("123", ci));
1302 AssertEquals("#K17", (int)128, Convert.ToInt32("10000000", 2));
1303 AssertEquals("#K18", (int)302, Convert.ToInt32("456", 8));
1304 AssertEquals("#K19", (int)456, Convert.ToInt32("456", 10));
1305 AssertEquals("#K20", (int)1110, Convert.ToInt32("456", 16));
1307 try {
1308 Convert.ToInt32(tryDT);
1309 Fail();
1311 catch (Exception e) {
1312 AssertEquals("#K25", typeof(InvalidCastException), e.GetType());
1315 try {
1316 Convert.ToInt32((decimal)tryMax);
1317 Fail();
1319 catch (Exception e) {
1320 AssertEquals("#K26", typeof(OverflowException), e.GetType());
1323 try {
1324 Convert.ToInt32((decimal)tryMin);
1325 Fail();
1327 catch (Exception e) {
1328 AssertEquals("#K27", typeof(OverflowException), e.GetType());
1331 try {
1332 Convert.ToInt32((double)tryMax);
1333 Fail();
1335 catch (Exception e) {
1336 AssertEquals("#K28", typeof(OverflowException), e.GetType());
1339 try {
1340 Convert.ToInt32((double)tryMin);
1341 Fail();
1343 catch (Exception e) {
1344 AssertEquals("#K29", typeof(OverflowException), e.GetType());
1347 try {
1348 Convert.ToInt32(tryInt64);
1349 Fail();
1351 catch (Exception e) {
1352 AssertEquals("#K30", typeof(OverflowException), e.GetType());
1355 try {
1356 Convert.ToInt32(-tryInt64);
1357 Fail();
1359 catch (Exception e) {
1360 AssertEquals("#K31", typeof(OverflowException), e.GetType());
1363 try {
1364 Convert.ToInt32(tryObj);
1365 Fail();
1367 catch (Exception e) {
1368 AssertEquals("#K32", typeof(InvalidCastException), e.GetType());
1371 try {
1372 Convert.ToInt32((float)tryMax);
1373 Fail();
1375 catch (Exception e) {
1376 AssertEquals("#K33", typeof(OverflowException), e.GetType());
1379 try {
1380 Convert.ToInt32((float)tryMin);
1381 Fail();
1383 catch (Exception e) {
1384 AssertEquals("#K34", typeof(OverflowException), e.GetType());
1387 try {
1388 Convert.ToInt32(tryStr, ci);
1389 Fail();
1391 catch (Exception e) {
1392 AssertEquals("#K35", typeof(FormatException), e.GetType());
1395 try {
1396 Convert.ToInt32("-46565465123");
1397 Fail();
1399 catch (Exception e) {
1400 AssertEquals("#K36", typeof(OverflowException), e.GetType());
1403 try {
1404 Convert.ToInt32("46565465123");
1405 Fail();
1407 catch (Exception e) {
1408 AssertEquals("#K37", typeof(OverflowException), e.GetType());
1411 try {
1412 Convert.ToInt32((uint)tryMax);
1413 Fail();
1415 catch (Exception e) {
1416 AssertEquals("#K38", typeof(OverflowException), e.GetType());
1419 try {
1420 Convert.ToInt32((ulong)tryMax);
1421 Fail();
1423 catch (Exception e) {
1424 AssertEquals("#K39", typeof(OverflowException), e.GetType());
1427 try {
1428 Convert.ToInt32(tryObj, ci);
1429 Fail();
1431 catch (Exception e) {
1432 AssertEquals("#K40", typeof(InvalidCastException), e.GetType());
1435 try {
1436 Convert.ToInt32(tryStr, ci);
1437 Fail();
1439 catch (Exception e) {
1440 AssertEquals("#K41", typeof(FormatException), e.GetType());
1443 try {
1444 Convert.ToInt32("-46565465123", ci);
1445 Fail();
1447 catch (Exception e) {
1448 AssertEquals("#K42", typeof(OverflowException), e.GetType());
1451 try {
1452 Convert.ToInt32("654", 9);
1453 Fail();
1455 catch (Exception e) {
1456 AssertEquals("#K43", typeof(ArgumentException), e.GetType());
1459 public void TestToInt64() {
1460 decimal longMax = long.MaxValue;
1461 longMax += 1000000;
1462 decimal longMin = long.MinValue;
1463 longMin -= 1000000;
1465 AssertEquals("#L01", (long)0, Convert.ToInt64(boolFalse));
1466 AssertEquals("#L02", (long)1, Convert.ToInt64(boolTrue));
1467 AssertEquals("#L03", (long)97, Convert.ToInt64(tryChar));
1468 AssertEquals("#L04", (long)1234, Convert.ToInt64(tryDec));
1469 AssertEquals("#L05", (long)0, Convert.ToInt64(tryDbl));
1470 AssertEquals("#L06", (long)1234, Convert.ToInt64(tryInt16));
1471 AssertEquals("#L07", (long)12345, Convert.ToInt64(tryInt32));
1472 AssertEquals("#L08", (long)123456789012, Convert.ToInt64(tryInt64));
1473 AssertEquals("#L09", (long)123, Convert.ToInt64(trySByte));
1474 AssertEquals("#L10", (long)1234, Convert.ToInt64(tryFloat));
1475 AssertEquals("#L11", (long)564897, Convert.ToInt64("564897"));
1476 AssertEquals("#L12", (long)34567, Convert.ToInt64(tryUI16));
1477 AssertEquals("#L13", (long)567891234, Convert.ToInt64(tryUI32));
1478 AssertEquals("#L14", (long)0, Convert.ToInt64(tryUI64));
1479 AssertEquals("#L15", (long)-2548751, Convert.ToInt64("-2548751", ci));
1480 AssertEquals("#L16", (long)24987562, Convert.ToInt64("1011111010100011110101010", 2));
1481 AssertEquals("#L17", (long)-24578965, Convert.ToInt64("1777777777777642172153", 8));
1482 AssertEquals("#L18", (long)248759757, Convert.ToInt64("248759757", 10));
1483 AssertEquals("#L19", (long)256, Convert.ToInt64("100", 16));
1485 try {
1486 Convert.ToInt64(tryDT);
1487 Fail();
1489 catch (Exception e) {
1490 AssertEquals("#L20", typeof(InvalidCastException), e.GetType());
1493 try {
1494 Convert.ToInt64((decimal)longMax + 1);
1495 Fail();
1497 catch (Exception e) {
1498 AssertEquals("#L21", typeof(OverflowException), e.GetType());
1501 try {
1502 Convert.ToInt64((decimal)longMin);
1503 Fail();
1505 catch (Exception e) {
1506 AssertEquals("#L24", typeof(OverflowException), e.GetType());
1509 try {
1510 Convert.ToInt64((double)longMax);
1511 Fail();
1513 catch (Exception e) {
1514 AssertEquals("#L25:"+longMax, typeof(OverflowException), e.GetType());
1517 try {
1518 Convert.ToInt64((double)longMin);
1519 Fail();
1521 catch (Exception e) {
1522 AssertEquals("#L26", typeof(OverflowException), e.GetType());
1525 try {
1526 Convert.ToInt64(new Exception());
1527 Fail();
1529 catch (Exception e) {
1530 AssertEquals("#L27", typeof(InvalidCastException), e.GetType());
1533 try {
1534 Convert.ToInt64(((float)longMax)*100);
1535 Fail();
1537 catch (Exception e) {
1538 AssertEquals("#L28:"+longMax, typeof(OverflowException), e.GetType());
1541 try {
1542 Convert.ToInt64(((float)longMin)*100);
1543 Fail();
1545 catch (Exception e) {
1546 AssertEquals("#L29", typeof(OverflowException), e.GetType());
1549 try {
1550 Convert.ToInt64("-567b3");
1551 Fail();
1553 catch (Exception e) {
1554 AssertEquals("#L30", typeof(FormatException), e.GetType());
1557 try {
1558 Convert.ToInt64(longMax.ToString());
1559 Fail();
1561 catch (Exception e) {
1562 AssertEquals("#L31:", typeof(OverflowException), e.GetType());
1565 try {
1566 Convert.ToInt64(ulong.MaxValue);
1567 Fail();
1569 catch (Exception e) {
1570 AssertEquals("#L32", typeof(OverflowException), e.GetType());
1573 try {
1574 Convert.ToInt64(tryStr, ci);
1575 Fail();
1577 catch (Exception e) {
1578 AssertEquals("#L32b", typeof(FormatException), e.GetType());
1581 try {
1582 Convert.ToInt64(longMin.ToString(), ci);
1583 Fail();
1585 catch (Exception e) {
1586 AssertEquals("#L33", typeof(OverflowException), e.GetType());
1589 try {
1590 Convert.ToInt64("321", 11);
1591 Fail();
1593 catch (Exception e) {
1594 AssertEquals("#L34", typeof(ArgumentException), e.GetType());
1598 public void TestToSByte() {
1599 int iTest = 1;
1600 try {
1601 AssertEquals("#M01", (sbyte)0, Convert.ToSByte(boolFalse));
1602 iTest++;
1603 AssertEquals("#M02", (sbyte)1, Convert.ToSByte(boolTrue));
1604 iTest++;
1605 AssertEquals("#M03", (sbyte)97, Convert.ToSByte(tryChar));
1606 iTest++;
1607 AssertEquals("#M04", (sbyte)15, Convert.ToSByte((decimal)15));
1608 iTest++;
1609 AssertEquals("#M05", (sbyte)0, Convert.ToSByte(tryDbl));
1610 iTest++;
1611 AssertEquals("#M06", (sbyte)127, Convert.ToSByte((short)127));
1612 iTest++;
1613 AssertEquals("#M07", (sbyte)-128, Convert.ToSByte((int)-128));
1614 iTest++;
1615 AssertEquals("#M08", (sbyte)30, Convert.ToSByte((long)30));
1616 iTest++;
1617 AssertEquals("#M09", (sbyte)123, Convert.ToSByte(trySByte));
1618 iTest++;
1619 AssertEquals("#M10", (sbyte)12, Convert.ToSByte((float)12.46987f));
1620 iTest++;
1621 AssertEquals("#M11", (sbyte)1, Convert.ToSByte("1"));
1622 iTest++;
1623 AssertEquals("#M12", (sbyte)99, Convert.ToSByte((ushort)99));
1624 iTest++;
1625 AssertEquals("#M13", (sbyte)54, Convert.ToSByte((uint)54));
1626 iTest++;
1627 AssertEquals("#M14", (sbyte)127, Convert.ToSByte((ulong)127));
1628 iTest++;
1629 AssertEquals("#M15", (sbyte)14, Convert.ToSByte("14", ci));
1630 iTest++;
1631 AssertEquals("#M16", (sbyte)11, Convert.ToSByte("01011", 2));
1632 iTest++;
1633 AssertEquals("#M17", (sbyte)5, Convert.ToSByte("5", 8));
1634 iTest++;
1635 AssertEquals("#M18", (sbyte)100, Convert.ToSByte("100", 10));
1636 iTest++;
1637 AssertEquals("#M19", (sbyte)-1, Convert.ToSByte("FF", 16));
1638 } catch (Exception e) {
1639 Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1642 try {
1643 Convert.ToSByte((byte)200);
1644 Fail();
1646 catch (Exception e) {
1647 AssertEquals("#M25", typeof(OverflowException), e.GetType());
1650 try {
1651 Convert.ToSByte((char)130);
1652 Fail();
1654 catch (Exception e) {
1655 AssertEquals("#M26", typeof(OverflowException), e.GetType());
1658 try {
1659 Convert.ToSByte(tryDT);
1660 Fail();
1662 catch (Exception e) {
1663 AssertEquals("#M27", typeof(InvalidCastException), e.GetType());
1666 try {
1667 Convert.ToSByte((decimal)127.5m);
1668 Fail();
1670 catch (Exception e) {
1671 AssertEquals("#M28", typeof(OverflowException), e.GetType());
1674 try {
1675 Convert.ToSByte((decimal)-200m);
1676 Fail();
1678 catch (Exception e) {
1679 AssertEquals("#M29", typeof(OverflowException), e.GetType());
1682 try {
1683 Convert.ToSByte((double)150);
1684 Fail();
1686 catch (Exception e) {
1687 AssertEquals("#M30", typeof(OverflowException), e.GetType());
1690 try {
1691 Convert.ToSByte((double)-128.6);
1692 Fail();
1694 catch (Exception e) {
1695 AssertEquals("#M31", typeof(OverflowException), e.GetType());
1698 try {
1699 Convert.ToSByte((short)150);
1700 Fail();
1702 catch (Exception e) {
1703 AssertEquals("#M32", typeof(OverflowException), e.GetType());
1706 try {
1707 Convert.ToSByte((short)-300);
1708 Fail();
1710 catch (Exception e) {
1711 AssertEquals("#M33", typeof(OverflowException), e.GetType());
1714 try {
1715 Convert.ToSByte((int)1500);
1716 Fail();
1718 catch (Exception e) {
1719 AssertEquals("#M34", typeof(OverflowException), e.GetType());
1722 try {
1723 Convert.ToSByte((int)-1286);
1724 Fail();
1726 catch (Exception e) {
1727 AssertEquals("#M35", typeof(OverflowException), e.GetType());
1730 try {
1731 Convert.ToSByte((long)128);
1732 Fail();
1734 catch (Exception e) {
1735 AssertEquals("#M36", typeof(OverflowException), e.GetType());
1738 try {
1739 Convert.ToSByte((long)-129);
1740 Fail();
1742 catch (Exception e) {
1743 AssertEquals("#M37", typeof(OverflowException), e.GetType());
1746 try {
1747 Convert.ToSByte(new NumberFormatInfo());
1748 Fail();
1750 catch (Exception e) {
1751 AssertEquals("#M38", typeof(InvalidCastException), e.GetType());
1754 try {
1755 Convert.ToSByte((float)333);
1756 Fail();
1758 catch (Exception e) {
1759 AssertEquals("#M39", typeof(OverflowException), e.GetType());
1762 try {
1763 Convert.ToSByte((float)-666);
1764 Fail();
1766 catch (Exception e) {
1767 AssertEquals("#M40", typeof(OverflowException), e.GetType());
1770 try {
1771 Convert.ToSByte("B3");
1772 Fail();
1774 catch (Exception e) {
1775 AssertEquals("#M41", typeof(FormatException), e.GetType());
1778 try {
1779 Convert.ToSByte("251");
1780 Fail();
1782 catch (Exception e) {
1783 AssertEquals("#M42", typeof(OverflowException), e.GetType());
1786 try {
1787 Convert.ToSByte(ushort.MaxValue);
1788 Fail();
1790 catch (Exception e) {
1791 AssertEquals("#M43", typeof(OverflowException), e.GetType());
1794 try {
1795 Convert.ToSByte((uint)600);
1796 Fail();
1798 catch (Exception e) {
1799 AssertEquals("#M44", typeof(OverflowException), e.GetType());
1802 try {
1803 Convert.ToSByte(ulong.MaxValue);
1804 Fail();
1806 catch (Exception e) {
1807 AssertEquals("#M45", typeof(OverflowException), e.GetType());
1810 try {
1811 Convert.ToSByte(ci, ci);
1812 Fail();
1814 catch (Exception e) {
1815 AssertEquals("#M46", typeof(InvalidCastException), e.GetType());
1818 try {
1819 Convert.ToSByte(tryStr, ci);
1820 Fail();
1822 catch (Exception e) {
1823 AssertEquals("#M47", typeof(FormatException), e.GetType());
1826 try {
1827 Convert.ToSByte("325", ci);
1828 Fail();
1830 catch (Exception e) {
1831 AssertEquals("#M48", typeof(OverflowException), e.GetType());
1834 try {
1835 Convert.ToSByte("5D", 15);
1836 Fail();
1838 catch (Exception e) {
1839 AssertEquals("#M49", typeof(ArgumentException), e.GetType());
1842 try {
1843 Convert.ToSByte("111111111", 2);
1844 Fail();
1846 catch (Exception e) {
1847 AssertEquals("#M50", typeof(OverflowException), e.GetType());
1851 public void TestToSingle() {
1852 int iTest = 1;
1853 try {
1854 AssertEquals("#N01", (float)0, Convert.ToSingle(boolFalse));
1855 iTest++;
1856 AssertEquals("#N02", (float)1, Convert.ToSingle(boolTrue));
1857 iTest++;
1858 AssertEquals("#N03", (float)0, Convert.ToSingle(tryByte));
1859 iTest++;
1860 AssertEquals("#N04", (float)1234,234, Convert.ToSingle(tryDec));
1861 iTest++;
1862 AssertEquals("#N05", (float)0, Convert.ToSingle(tryDbl));
1863 iTest++;
1864 AssertEquals("#N06", (float)1234, Convert.ToSingle(tryInt16));
1865 iTest++;
1866 AssertEquals("#N07", (float)12345, Convert.ToSingle(tryInt32));
1867 iTest++;
1868 AssertEquals("#N08", (float)123456789012, Convert.ToSingle(tryInt64));
1869 iTest++;
1870 AssertEquals("#N09", (float)123, Convert.ToSingle(trySByte));
1871 iTest++;
1872 AssertEquals("#N10", (float)1234,2345, Convert.ToSingle(tryFloat));
1873 iTest++;
1874 AssertEquals("#N11", (float)987, Convert.ToSingle("987"));
1875 iTest++;
1876 AssertEquals("#N12", (float)34567, Convert.ToSingle(tryUI16));
1877 iTest++;
1878 AssertEquals("#N13", (float)567891234, Convert.ToSingle(tryUI32));
1879 iTest++;
1880 AssertEquals("#N14", (float)0, Convert.ToSingle(tryUI64));
1881 iTest++;
1882 AssertEquals("#N15", (float)654.234, Convert.ToSingle("654.234", ci));
1883 } catch (Exception e) {
1884 Fail ("Unexpected exception at iTest = " + iTest + ": e = " + e);
1887 try {
1888 Convert.ToSingle(tryChar);
1889 Fail();
1891 catch (Exception e) {
1892 AssertEquals("#N25", typeof(InvalidCastException), e.GetType());
1895 try {
1896 Convert.ToSingle(tryDT);
1897 Fail();
1899 catch (Exception e) {
1900 AssertEquals("#N26", typeof(InvalidCastException), e.GetType());
1903 try {
1904 Convert.ToSingle(tryObj);
1905 Fail();
1907 catch (Exception e) {
1908 AssertEquals("#N27", typeof(InvalidCastException), e.GetType());
1911 try {
1912 Convert.ToSingle("A345H");
1913 Fail();
1915 catch (Exception e) {
1916 AssertEquals("#N28", typeof(FormatException), e.GetType());
1919 try {
1920 Convert.ToSingle(double.MaxValue.ToString());
1921 Fail();
1923 catch (Exception e) {
1924 AssertEquals("#N29", typeof(OverflowException), e.GetType());
1927 try {
1928 Convert.ToSingle(tryObj, ci);
1929 Fail();
1931 catch (Exception e) {
1932 AssertEquals("#N30", typeof(InvalidCastException), e.GetType());
1935 try {
1936 Convert.ToSingle("J345K", ci);
1937 Fail();
1939 catch (Exception e) {
1940 AssertEquals("#N31", typeof(FormatException), e.GetType());
1943 try {
1944 Convert.ToSingle("11000000000000000000000000000000000000000000000", ci);
1945 Fail();
1947 catch (Exception e) {
1948 AssertEquals("#N32", typeof(OverflowException), e.GetType());
1952 public void TestToString() {
1954 tryByte = 123;
1955 AssertEquals("#O01", "False", Convert.ToString(boolFalse));
1956 AssertEquals("#O02", "True", Convert.ToString(boolTrue));
1957 AssertEquals("#O03", "123", Convert.ToString(tryByte));
1958 AssertEquals("#O04", "a", Convert.ToString(tryChar));
1959 AssertEquals("#O05", tryDT.ToString(), Convert.ToString(tryDT));
1960 AssertEquals("#O06", tryDec.ToString(), Convert.ToString(tryDec));
1961 AssertEquals("#O07", tryDbl.ToString(), Convert.ToString(tryDbl));
1962 AssertEquals("#O08", "1234", Convert.ToString(tryInt16));
1963 AssertEquals("#O09", "12345", Convert.ToString(tryInt32));
1964 AssertEquals("#O10", "123456789012", Convert.ToString(tryInt64));
1965 AssertEquals("#O11", "123", Convert.ToString(trySByte));
1966 AssertEquals("#O12", tryFloat.ToString(), Convert.ToString(tryFloat));
1967 AssertEquals("#O13", "foobar", Convert.ToString(tryStr));
1968 AssertEquals("#O14", "34567", Convert.ToString(tryUI16));
1969 AssertEquals("#O15", "567891234", Convert.ToString(tryUI32));
1970 AssertEquals("#O16", "True", Convert.ToString(boolTrue, ci));
1971 AssertEquals("#O17", "False", Convert.ToString(boolFalse, ci));
1972 AssertEquals("#O18", "123", Convert.ToString(tryByte, ci));
1973 AssertEquals("#O19", "1111011", Convert.ToString(tryByte, 2));
1974 AssertEquals("#O20", "173", Convert.ToString(tryByte, 8));
1975 AssertEquals("#O21", "123", Convert.ToString(tryByte, 10));
1976 AssertEquals("#O22", "7b", Convert.ToString(tryByte, 16));
1977 AssertEquals("#O23", "a", Convert.ToString(tryChar, ci));
1978 AssertEquals("#O24", tryDT.ToString(ci), Convert.ToString(tryDT, ci));
1979 AssertEquals("#O25", tryDec.ToString(ci), Convert.ToString(tryDec,ci));
1980 AssertEquals("#O26", tryDbl.ToString(ci), Convert.ToString(tryDbl, ci));
1981 AssertEquals("#O27", "1234", Convert.ToString(tryInt16, ci));
1982 AssertEquals("#O28", "10011010010", Convert.ToString(tryInt16, 2));
1983 AssertEquals("#O29", "2322", Convert.ToString(tryInt16, 8));
1984 AssertEquals("#O30", "1234", Convert.ToString(tryInt16, 10));
1985 AssertEquals("#O31", "4d2", Convert.ToString(tryInt16, 16));
1986 AssertEquals("#O32", "12345", Convert.ToString(tryInt32, ci));
1987 AssertEquals("#O33", "11000000111001", Convert.ToString(tryInt32, 2));
1988 AssertEquals("#O34", "30071", Convert.ToString(tryInt32, 8));
1989 AssertEquals("#O35", "12345", Convert.ToString(tryInt32, 10));
1990 AssertEquals("#O36", "3039", Convert.ToString(tryInt32, 16));
1991 AssertEquals("#O37", "123456789012", Convert.ToString(tryInt64, ci));
1992 AssertEquals("#O38", "1110010111110100110010001101000010100",
1993 Convert.ToString(tryInt64, 2));
1994 AssertEquals("#O39", "1627646215024", Convert.ToString(tryInt64, 8));
1995 AssertEquals("#O40", "123456789012", Convert.ToString(tryInt64, 10));
1996 AssertEquals("#O41", "1cbe991a14", Convert.ToString(tryInt64, 16));
1997 AssertEquals("#O42", "123", Convert.ToString((trySByte), ci));
1998 AssertEquals("#O43", tryFloat.ToString(ci), Convert.ToString((tryFloat), ci));
1999 AssertEquals("#O44", "foobar", Convert.ToString((tryStr), ci));
2000 AssertEquals("#O45", "34567", Convert.ToString((tryUI16), ci));
2001 AssertEquals("#O46", "567891234", Convert.ToString((tryUI32), ci));
2002 AssertEquals("#O47", "0", Convert.ToString(tryUI64));
2003 AssertEquals("#O48", "0", Convert.ToString((tryUI64), ci));
2005 try {
2006 Convert.ToString(tryInt16, 5);
2007 Fail();
2009 catch (Exception e) {
2010 AssertEquals("#O55", typeof(ArgumentException), e.GetType());
2013 try {
2014 Convert.ToString(tryInt32, 17);
2015 Fail();
2017 catch (Exception e) {
2018 AssertEquals("#O56", typeof(ArgumentException), e.GetType());
2021 try {
2022 Convert.ToString(tryInt64, 1);
2023 Fail();
2025 catch (Exception e) {
2026 AssertEquals("#O57", typeof(ArgumentException), e.GetType());
2030 public void TestToUInt16() {
2031 AssertEquals("#P01", (ushort)0, Convert.ToUInt16(boolFalse));
2032 AssertEquals("#P02", (ushort)1, Convert.ToUInt16(boolTrue));
2033 AssertEquals("#P03", (ushort)0, Convert.ToUInt16(tryByte));
2034 AssertEquals("#P04", (ushort)97, Convert.ToUInt16(tryChar));
2035 AssertEquals("#P05", (ushort)1234, Convert.ToUInt16(tryDec));
2036 AssertEquals("#P06", (ushort)0, Convert.ToUInt16(tryDbl));
2037 AssertEquals("#P07", (ushort)1234, Convert.ToUInt16(tryInt16));
2038 AssertEquals("#P08", (ushort)12345, Convert.ToUInt16(tryInt32));
2039 AssertEquals("#P09", (ushort)43752, Convert.ToUInt16((long)43752));
2040 AssertEquals("#P10", (ushort)123, Convert.ToUInt16(trySByte));
2041 AssertEquals("#P11", (ushort)1234, Convert.ToUInt16(tryFloat));
2042 AssertEquals("#P12", (ushort)123, Convert.ToUInt16((string)"123"));
2043 AssertEquals("#P13", (ushort)34567, Convert.ToUInt16(tryUI16));
2044 AssertEquals("#P14", (ushort)56789, Convert.ToUInt16((uint)56789));
2045 AssertEquals("#P15", (ushort)0, Convert.ToUInt16(tryUI64));
2046 AssertEquals("#P16", (ushort)31, Convert.ToUInt16("31", ci));
2047 AssertEquals("#P17", (ushort)14, Convert.ToUInt16("1110", 2));
2048 AssertEquals("#P18", (ushort)32, Convert.ToUInt16("40", 8));
2049 AssertEquals("#P19", (ushort)40, Convert.ToUInt16("40", 10));
2050 AssertEquals("#P20", (ushort)64, Convert.ToUInt16("40", 16));
2053 try {
2054 Convert.ToUInt16(tryDT);
2055 Fail();
2057 catch (Exception e) {
2058 AssertEquals("#P25", typeof(InvalidCastException), e.GetType());
2061 try {
2062 Convert.ToUInt16(decimal.MaxValue);
2063 Fail();
2065 catch (Exception e) {
2066 AssertEquals("#P26", typeof(OverflowException), e.GetType());
2069 try {
2070 Convert.ToUInt16(decimal.MinValue);
2071 Fail();
2073 catch (Exception e) {
2074 AssertEquals("#P27", typeof(OverflowException), e.GetType());
2077 try {
2078 Convert.ToUInt16(double.MaxValue);
2079 Fail();
2081 catch (Exception e) {
2082 AssertEquals("#P28", typeof(OverflowException), e.GetType());
2085 try {
2086 Convert.ToUInt16(double.MinValue);
2087 Fail();
2089 catch (Exception e) {
2090 AssertEquals("#P29", typeof(OverflowException), e.GetType());
2093 try {
2094 Convert.ToUInt16(short.MinValue);
2095 Fail();
2097 catch (Exception e) {
2098 AssertEquals("#P30", typeof(OverflowException), e.GetType());
2101 try {
2102 Convert.ToUInt16(int.MaxValue);
2103 Fail();
2105 catch (Exception e) {
2106 AssertEquals("#P31", typeof(OverflowException), e.GetType());
2109 try {
2110 Convert.ToUInt16(int.MinValue);
2111 Fail();
2113 catch (Exception e) {
2114 AssertEquals("#P32", typeof(OverflowException), e.GetType());
2117 try {
2118 Convert.ToUInt16(long.MaxValue);
2119 Fail();
2121 catch (Exception e) {
2122 AssertEquals("#P33", typeof(OverflowException), e.GetType());
2125 try {
2126 Convert.ToUInt16(long.MinValue);
2127 Fail();
2129 catch (Exception e) {
2130 AssertEquals("#P34", typeof(OverflowException), e.GetType());
2133 try {
2134 Convert.ToUInt16(tryObj);
2135 Fail();
2137 catch (Exception e) {
2138 AssertEquals("#P35", typeof(InvalidCastException), e.GetType());
2141 try {
2142 Convert.ToUInt16(sbyte.MinValue);
2143 Fail();
2145 catch (Exception e) {
2146 AssertEquals("#P36", typeof(OverflowException), e.GetType());
2149 try {
2150 Convert.ToUInt16(float.MaxValue);
2151 Fail();
2153 catch (Exception e) {
2154 AssertEquals("#P37", typeof(OverflowException), e.GetType());
2157 try {
2158 Convert.ToUInt16(float.MinValue);
2159 Fail();
2161 catch (Exception e) {
2162 AssertEquals("#P38", typeof(OverflowException), e.GetType());
2165 try {
2166 Convert.ToUInt16("1A2");
2167 Fail();
2169 catch (Exception e) {
2170 AssertEquals("#P39", typeof(FormatException), e.GetType());
2173 try {
2174 Convert.ToUInt16("-32800");
2175 Fail();
2177 catch (Exception e) {
2178 AssertEquals("#P40", typeof(OverflowException), e.GetType());
2181 try {
2182 Convert.ToUInt16(int.MaxValue.ToString());
2183 Fail();
2185 catch (Exception e) {
2186 AssertEquals("#P41", typeof(OverflowException), e.GetType());
2189 try {
2190 Convert.ToUInt16(ulong.MaxValue);
2191 Fail();
2193 catch (Exception e) {
2194 AssertEquals("#P42", typeof(OverflowException), e.GetType());
2197 try {
2198 Convert.ToUInt16("1A2", ci);
2199 Fail();
2201 catch (Exception e) {
2202 AssertEquals("#P43", typeof(FormatException), e.GetType());
2205 try {
2206 Convert.ToUInt16("-32800", ci);
2207 Fail();
2209 catch (Exception e) {
2210 AssertEquals("#P44", typeof(OverflowException), e.GetType());
2213 try {
2214 Convert.ToUInt16("456987", ci);
2215 Fail();
2217 catch (Exception e) {
2218 AssertEquals("#P45", typeof(OverflowException), e.GetType());
2221 try {
2222 Convert.ToUInt16("40", 9);
2223 Fail();
2225 catch (Exception e) {
2226 AssertEquals("#P46", typeof(ArgumentException), e.GetType());
2229 try {
2230 Convert.ToUInt16 ("abcde", 16);
2231 Fail ();
2233 catch (Exception e) {
2234 AssertEquals ("#P47", typeof (OverflowException), e.GetType ());
2238 public void TestSignedToInt() {
2239 // String cannot contain a minus sign if the base is not 10.
2240 // But can if it is ten, and + is allowed everywhere.
2241 AssertEquals("Signed0", -1, Convert.ToInt32 ("-1", 10));
2242 AssertEquals("Signed1", 1, Convert.ToInt32 ("+1", 10));
2243 AssertEquals("Signed2", 1, Convert.ToInt32 ("+1", 2));
2244 AssertEquals("Signed3", 1, Convert.ToInt32 ("+1", 8));
2245 AssertEquals("Signed4", 1, Convert.ToInt32 ("+1", 16));
2247 try {
2248 Convert.ToInt32("-1", 2);
2249 Fail();
2251 catch (Exception) {
2253 try {
2254 Convert.ToInt32("-1", 8);
2255 Fail();
2257 catch (Exception) {
2259 try {
2260 Convert.ToInt32("-1", 16);
2261 Fail();
2263 catch (Exception) {
2269 public void TestToUInt32() {
2270 AssertEquals("#Q01", (uint)1, Convert.ToUInt32(boolTrue));
2271 AssertEquals("#Q02", (uint)0, Convert.ToUInt32(boolFalse));
2272 AssertEquals("#Q03", (uint)0, Convert.ToUInt32(tryByte));
2273 AssertEquals("#Q04", (uint)97, Convert.ToUInt32(tryChar));
2274 AssertEquals("#Q05", (uint)1234, Convert.ToUInt32(tryDec));
2275 AssertEquals("#Q06", (uint)0, Convert.ToUInt32(tryDbl));
2276 AssertEquals("#Q07", (uint)1234, Convert.ToUInt32(tryInt16));
2277 AssertEquals("#Q08", (uint)12345, Convert.ToUInt32(tryInt32));
2278 AssertEquals("#Q09", (uint)1234567890, Convert.ToUInt32((long)1234567890));
2279 AssertEquals("#Q10", (uint)123, Convert.ToUInt32(trySByte));
2280 AssertEquals("#Q11", (uint)1234, Convert.ToUInt32(tryFloat));
2281 AssertEquals("#Q12", (uint)3456789, Convert.ToUInt32("3456789"));
2282 AssertEquals("#Q13", (uint)34567, Convert.ToUInt32(tryUI16));
2283 AssertEquals("#Q14", (uint)567891234, Convert.ToUInt32(tryUI32));
2284 AssertEquals("#Q15", (uint)0, Convert.ToUInt32(tryUI64));
2285 AssertEquals("#Q16", (uint)415, Convert.ToUInt32("110011111", 2));
2286 AssertEquals("#Q17", (uint)156, Convert.ToUInt32("234" ,8));
2287 AssertEquals("#Q18", (uint)234, Convert.ToUInt32("234" ,10));
2288 AssertEquals("#Q19", (uint)564, Convert.ToUInt32("234" ,16));
2291 try {
2292 Convert.ToUInt32(tryDT);
2293 Fail();
2295 catch (Exception e) {
2296 AssertEquals("#Q25", typeof(InvalidCastException), e.GetType());
2299 try {
2300 Convert.ToUInt32(decimal.MaxValue);
2301 Fail();
2303 catch (Exception e) {
2304 AssertEquals("#Q26", typeof(OverflowException), e.GetType());
2307 try {
2308 Convert.ToUInt32((decimal)-150);
2309 Fail();
2311 catch (Exception e) {
2312 AssertEquals("#Q27", typeof(OverflowException), e.GetType());
2315 try {
2316 Convert.ToUInt32(double.MaxValue);
2317 Fail();
2319 catch (Exception e) {
2320 AssertEquals("#Q28", typeof(OverflowException), e.GetType());
2323 try {
2324 Convert.ToUInt32((double)-1);
2325 Fail();
2327 catch (Exception e) {
2328 AssertEquals("#Q29", typeof(OverflowException), e.GetType());
2331 try {
2332 Convert.ToUInt32(short.MinValue);
2333 Fail();
2335 catch (Exception e) {
2336 AssertEquals("#Q30", typeof(OverflowException), e.GetType());
2339 try {
2340 Convert.ToUInt32(int.MinValue);
2341 Fail();
2343 catch (Exception e) {
2344 AssertEquals("#Q31", typeof(OverflowException), e.GetType());
2347 try {
2348 Convert.ToUInt32(long.MaxValue);
2349 Fail();
2351 catch (Exception e) {
2352 AssertEquals("#Q32", typeof(OverflowException), e.GetType());
2355 try {
2356 Convert.ToUInt32((long)-50000);
2357 Fail();
2359 catch (Exception e) {
2360 AssertEquals("#Q33", typeof(OverflowException), e.GetType());
2363 try {
2364 Convert.ToUInt32(new Exception());
2365 Fail();
2367 catch (Exception e) {
2368 AssertEquals("#Q34", typeof(InvalidCastException), e.GetType());
2371 try {
2372 Convert.ToUInt32(sbyte.MinValue);
2373 Fail();
2375 catch (Exception e) {
2376 AssertEquals("#Q35", typeof(OverflowException), e.GetType());
2379 try {
2380 Convert.ToUInt32(float.MaxValue);
2381 Fail();
2383 catch (Exception e) {
2384 AssertEquals("#Q36", typeof(OverflowException), e.GetType());
2387 try {
2388 Convert.ToUInt32(float.MinValue);
2389 Fail();
2391 catch (Exception e) {
2392 AssertEquals("#Q37", typeof(OverflowException), e.GetType());
2395 try {
2396 Convert.ToUInt32("45t54");
2397 Fail();
2399 catch (Exception e) {
2400 AssertEquals("#Q38", typeof(FormatException), e.GetType());
2403 try {
2404 Convert.ToUInt32("-55");
2405 Fail();
2407 catch (Exception e) {
2408 AssertEquals("#Q39", typeof(OverflowException), e.GetType());
2411 try {
2412 Convert.ToUInt32(ulong.MaxValue);
2413 Fail();
2415 catch (Exception e) {
2416 AssertEquals("#Q40", typeof(OverflowException), e.GetType());
2419 try {
2420 Convert.ToUInt32(new Exception(), ci);
2421 Fail();
2423 catch (Exception e) {
2424 AssertEquals("#Q41", typeof(InvalidCastException), e.GetType());
2427 try {
2428 Convert.ToUInt32(tryStr, ci);
2429 Fail();
2431 catch (Exception e) {
2432 AssertEquals("#Q42", typeof(FormatException), e.GetType());
2435 try {
2436 Convert.ToUInt32("-50", ci);
2437 Fail();
2439 catch (Exception e) {
2440 AssertEquals("#Q43", typeof(OverflowException), e.GetType());
2443 try {
2444 Convert.ToUInt32(decimal.MaxValue.ToString(), ci);
2445 Fail();
2447 catch (Exception e) {
2448 AssertEquals("#Q44", typeof(OverflowException), e.GetType());
2451 try {
2452 Convert.ToUInt32("1001110", 1);
2453 Fail();
2455 catch (Exception e) {
2456 AssertEquals("#Q45", typeof(ArgumentException), e.GetType());
2460 public void TestToUInt64()
2462 int iTest = 1;
2463 try {
2464 AssertEquals("#R01", (ulong)1, Convert.ToUInt64(boolTrue));
2465 iTest++;
2466 AssertEquals("#R02", (ulong)0, Convert.ToUInt64(boolFalse));
2467 iTest++;
2468 AssertEquals("#R03", (ulong)0, Convert.ToUInt64(tryByte));
2469 iTest++;
2470 AssertEquals("#R04", (ulong)97, Convert.ToUInt64(tryChar));
2471 iTest++;
2472 AssertEquals("#R05", (ulong)1234, Convert.ToUInt64(tryDec));
2473 iTest++;
2474 AssertEquals("#R06", (ulong)0, Convert.ToUInt64(tryDbl));
2475 iTest++;
2476 AssertEquals("#R07", (ulong)1234, Convert.ToUInt64(tryInt16));
2477 iTest++;
2478 AssertEquals("#R08", (ulong)12345, Convert.ToUInt64(tryInt32));
2479 iTest++;
2480 AssertEquals("#R09", (ulong)123456789012, Convert.ToUInt64(tryInt64));
2481 iTest++;
2482 AssertEquals("#R10", (ulong)123, Convert.ToUInt64(trySByte));
2483 iTest++;
2484 AssertEquals("#R11", (ulong)1234, Convert.ToUInt64(tryFloat));
2485 iTest++;
2486 AssertEquals("#R12", (ulong)345678, Convert.ToUInt64("345678"));
2487 iTest++;
2488 AssertEquals("#R13", (ulong)34567, Convert.ToUInt64(tryUI16));
2489 iTest++;
2490 AssertEquals("#R14", (ulong)567891234, Convert.ToUInt64(tryUI32));
2491 iTest++;
2492 AssertEquals("#R15", (ulong)0, Convert.ToUInt64(tryUI64));
2493 iTest++;
2494 AssertEquals("#R16", (ulong)123, Convert.ToUInt64("123", ci));
2495 iTest++;
2496 AssertEquals("#R17", (ulong)4, Convert.ToUInt64("100", 2));
2497 iTest++;
2498 AssertEquals("#R18", (ulong)64, Convert.ToUInt64("100", 8));
2499 iTest++;
2500 AssertEquals("#R19", (ulong)100, Convert.ToUInt64("100", 10));
2501 iTest++;
2502 AssertEquals("#R20", (ulong)256, Convert.ToUInt64("100", 16));
2503 } catch (Exception e) {
2504 Fail ("Unexpected exception caught when iTest = " + iTest + ": e = " + e);
2507 try {
2508 Convert.ToUInt64(tryDT);
2509 Fail();
2511 catch (Exception e) {
2512 AssertEquals("#R25", typeof(InvalidCastException), e.GetType());
2515 try {
2516 Convert.ToUInt64(decimal.MaxValue);
2517 Fail();
2519 catch (Exception e) {
2520 AssertEquals("#R26", typeof(OverflowException), e.GetType());
2523 try {
2524 Convert.ToUInt64((decimal)-140);
2525 Fail();
2527 catch (Exception e) {
2528 AssertEquals("#R27", typeof(OverflowException), e.GetType());
2531 try {
2532 Convert.ToUInt64(double.MaxValue);
2533 Fail();
2535 catch (Exception e) {
2536 AssertEquals("#R28", typeof(OverflowException), e.GetType());
2539 try {
2540 Convert.ToUInt64((double)-1);
2541 Fail();
2543 catch (Exception e) {
2544 AssertEquals("#R29", typeof(OverflowException), e.GetType());
2547 try {
2548 Convert.ToUInt64(short.MinValue);
2549 Fail();
2551 catch (Exception e) {
2552 AssertEquals("#R30", typeof(OverflowException), e.GetType());
2555 try {
2556 Convert.ToUInt64(int.MinValue);
2557 Fail();
2559 catch (Exception e) {
2560 AssertEquals("#R31", typeof(OverflowException), e.GetType());
2563 try {
2564 Convert.ToUInt64(long.MinValue);
2565 Fail();
2567 catch (Exception e) {
2568 AssertEquals("#R32", typeof(OverflowException), e.GetType());
2571 try {
2572 Convert.ToUInt64(tryObj);
2573 Fail();
2575 catch (Exception e) {
2576 AssertEquals("#R33", typeof(InvalidCastException), e.GetType());
2579 try {
2580 Convert.ToUInt64(sbyte.MinValue);
2581 Fail();
2583 catch (Exception e) {
2584 AssertEquals("#R34", typeof(OverflowException), e.GetType());
2587 try {
2588 Convert.ToUInt64(float.MinValue);
2589 Fail();
2591 catch (Exception e) {
2592 AssertEquals("#R35", typeof(OverflowException), e.GetType());
2595 try {
2596 Convert.ToUInt64(float.MaxValue);
2597 Fail();
2599 catch (Exception e) {
2600 AssertEquals("#R36", typeof(OverflowException), e.GetType());
2603 try {
2604 Convert.ToUInt64("234rt78");
2605 Fail();
2607 catch (Exception e) {
2608 AssertEquals("#R37", typeof(FormatException), e.GetType());
2611 try {
2612 Convert.ToUInt64("-68");
2613 Fail();
2615 catch (Exception e) {
2616 AssertEquals("#R38", typeof(OverflowException), e.GetType());
2619 try {
2620 Convert.ToUInt64(decimal.MaxValue.ToString());
2621 Fail();
2623 catch (Exception e) {
2624 AssertEquals("#R39", typeof(OverflowException), e.GetType());
2627 try {
2628 Convert.ToUInt64("23rd2", ci);
2629 Fail();
2631 catch (Exception e) {
2632 AssertEquals("#R40", typeof(FormatException), e.GetType());
2635 try {
2636 Convert.ToUInt64(decimal.MinValue.ToString(), ci);
2637 Fail();
2639 catch (Exception e) {
2640 AssertEquals("#R41", typeof(OverflowException), e.GetType());
2643 try {
2644 Convert.ToUInt64(decimal.MaxValue.ToString(), ci);
2645 Fail();
2647 catch (Exception e) {
2648 AssertEquals("#R42", typeof(OverflowException), e.GetType());
2651 try {
2652 Convert.ToUInt64("132", 9);
2653 Fail();
2655 catch (Exception e) {
2656 AssertEquals("#R43", typeof(ArgumentException), e.GetType());
2660 [Test]
2661 [ExpectedException (typeof (FormatException))]
2662 public void TestInvalidBase64() {
2663 // This has to be a multiple of 4 characters, otherwise you
2664 // are testing something else. Ideally one will become a byte
2665 // > 128
2667 // This test is designed to see what happens with invalid bytes
2668 string brokenB64 = "AB~\u00a3";
2669 Convert.FromBase64String(brokenB64);
2673 public void TestToBase64CharArray ()
2675 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2676 // 0 1 2 3 4 5 6 7
2677 char[] expectedCharArr = {'I', 'X', '/', '/', 'b', 'a', 'o', '2'};
2678 char[] result = new Char[8];
2680 Convert.ToBase64CharArray(byteArr, 0, byteArr.Length, result, 0);
2682 for (int i = 0; i < expectedCharArr.Length; i++) {
2683 AssertEquals("#S0" + i, expectedCharArr[i], result[i]);
2687 [Test]
2688 [ExpectedException (typeof(ArgumentNullException))]
2689 public void ToBase64CharArray_InNull ()
2691 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2692 char[] result = new Char[8];
2693 Convert.ToBase64CharArray (null, 0, byteArr.Length, result, 0);
2696 [Test]
2697 [ExpectedException (typeof(ArgumentNullException))]
2698 public void ToBase64CharArray_OutNull ()
2700 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2701 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, null, 0);
2704 [Test]
2705 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2706 public void ToBase64CharArray_OffsetInNegative ()
2708 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2709 char[] result = new Char[8];
2710 Convert.ToBase64CharArray (byteArr, -1, byteArr.Length, result, 0);
2713 [Test]
2714 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2715 public void ToBase64CharArray_LengthNegative ()
2717 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2718 char[] result = new Char[8];
2719 Convert.ToBase64CharArray (byteArr, 0, -5, result, 0);
2722 [Test]
2723 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2724 public void ToBase64CharArray_OffsetOutNegative ()
2726 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2727 char[] result = new Char[8];
2728 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, -2);
2731 [Test]
2732 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2733 public void ToBase64CharArray_TotalIn ()
2735 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2736 char[] result = new Char[8];
2737 Convert.ToBase64CharArray (byteArr, 4, byteArr.Length, result, 0);
2740 [Test]
2741 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2742 public void ToBase64CharArray_TotalInOverflow ()
2744 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2745 char[] result = new Char[8];
2746 Convert.ToBase64CharArray (byteArr, Int32.MaxValue, byteArr.Length, result, 0);
2749 [Test]
2750 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2751 public void ToBase64CharArray_TotalOut ()
2753 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2754 char[] result = new Char[8];
2755 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, 2);
2758 [Test]
2759 [ExpectedException (typeof(ArgumentOutOfRangeException))]
2760 public void ToBase64CharArray_TotalOutOverflow ()
2762 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2763 char[] result = new Char[8];
2764 Convert.ToBase64CharArray (byteArr, 0, byteArr.Length, result, Int32.MaxValue);
2767 public void TestToBase64String() {
2768 byte[] byteArr = {33, 127, 255, 109, 170, 54};
2769 string expectedStr = "IX//bao2";
2770 string result1;
2771 string result2;
2773 result1 = Convert.ToBase64String(byteArr);
2774 result2 = Convert.ToBase64String(byteArr, 0, byteArr.Length);
2776 AssertEquals("#T01", expectedStr, result1);
2777 AssertEquals("#T02", expectedStr, result2);
2779 try {
2780 Convert.ToBase64String(null);
2781 Fail();
2783 catch (Exception e) {
2784 AssertEquals("#T05", typeof(ArgumentNullException), e.GetType());
2787 try {
2788 Convert.ToBase64String(byteArr, -1, byteArr.Length);
2789 Fail();
2791 catch (Exception e) {
2792 AssertEquals("#T06", typeof(ArgumentOutOfRangeException), e.GetType());
2795 try {
2796 Convert.ToBase64String(byteArr, 0, -10);
2797 Fail();
2799 catch (Exception e) {
2800 AssertEquals("#T07", typeof(ArgumentOutOfRangeException), e.GetType());
2803 try {
2804 Convert.ToBase64String(byteArr, 4, byteArr.Length);
2805 Fail();
2807 catch (Exception e) {
2808 AssertEquals("#T08", typeof(ArgumentOutOfRangeException), e.GetType());
2812 /* Have experienced some problems with FromBase64CharArray using mono. Something
2813 * about error in a unicode file.
2815 * However the test seems to run fine using mono in a cygwin environment
2818 [Test]
2819 [ExpectedException (typeof (ArgumentNullException))]
2820 public void FromBase64CharArray_Null ()
2822 Convert.FromBase64CharArray (null, 1, 5);
2825 [Test]
2826 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2827 public void FromBase64CharArray_OutOfRangeStart ()
2829 Convert.FromBase64CharArray (new char [4], -1, 4);
2832 [Test]
2833 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2834 public void FromBase64CharArray_OutOfRangeLength ()
2836 Convert.FromBase64CharArray (new char [4], 2, 4);
2839 [Test]
2840 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2841 public void FromBase64CharArray_Overflow ()
2843 Convert.FromBase64CharArray (new char [4], Int32.MaxValue, 4);
2846 [Test]
2847 [ExpectedException (typeof (FormatException))]
2848 public void FromBase64CharArray_InvalidLength ()
2850 Convert.FromBase64CharArray (new char [4], 0, 3);
2853 [Test]
2854 [ExpectedException (typeof (FormatException))]
2855 public void FromBase64CharArray_WideChar ()
2857 char[] c = new char [4] { 'A', 'A', 'A', (char) Char.MaxValue };
2858 Convert.FromBase64CharArray (c, 0, 4);
2861 [Test]
2862 public void FromBase64CharArray ()
2864 char[] charArr = {'M','o','n','o','m','o','n','o'};
2865 byte[] expectedByteArr = {50, 137, 232, 154, 137, 232};
2867 byte[] fromCharArr = Convert.FromBase64CharArray(charArr, 0, 8);
2869 for (int i = 0; i < fromCharArr.Length; i++){
2870 AssertEquals("#U0" + i, expectedByteArr[i], fromCharArr[i]);
2874 /* Have experienced some problems with FromBase64String using mono. Something about
2875 * error in a unicode file.
2877 * However the test seems to run fine using mono in a cygwin environment
2880 [Test]
2881 [ExpectedException (typeof (ArgumentNullException))]
2882 public void FromBase64String_Null ()
2884 Convert.FromBase64String (null);
2887 [Test]
2888 [ExpectedException (typeof (FormatException))]
2889 public void FromBase64String_InvalidLength ()
2891 Convert.FromBase64String ("foo");
2894 [Test]
2895 [ExpectedException (typeof (FormatException))]
2896 public void FromBase64String_InvalidLength2 ()
2898 Convert.FromBase64String (tryStr);
2901 [Test]
2902 public void FromBase64String_InvalidLengthBecauseOfIgnoredChars ()
2904 byte[] result = Convert.FromBase64String ("AAAA\t");
2905 AssertEquals ("InvalidLengthBecauseOfIgnoredChars", 3, result.Length);
2908 private const string ignored = "\t\r\n ";
2909 private const string base64data = "AAAAAAAAAAAAAAAAAAAA"; // 15 bytes 0x00
2911 [Test]
2912 public void FromBase64_IgnoreCharsBefore ()
2914 string s = ignored + base64data;
2915 byte[] data = Convert.FromBase64String (s);
2916 AssertEquals ("String-IgnoreCharsBefore-Ignored", 15, data.Length);
2918 char[] c = s.ToCharArray ();
2919 data = Convert.FromBase64CharArray (c, 0, c.Length);
2920 AssertEquals ("CharArray-IgnoreCharsBefore-Ignored", 15, data.Length);
2923 [Test]
2924 public void FromBase64_IgnoreCharsInside ()
2926 string s = base64data + ignored + base64data;
2927 byte[] data = Convert.FromBase64String (s);
2928 AssertEquals ("String-IgnoreCharsInside-Ignored", 30, data.Length);
2930 char[] c = s.ToCharArray ();
2931 data = Convert.FromBase64CharArray (c, 0, c.Length);
2932 AssertEquals ("CharArray-IgnoreCharsInside-Ignored", 30, data.Length);
2935 [Test]
2936 public void FromBase64_IgnoreCharsAfter ()
2938 string s = base64data + ignored;
2939 byte[] data = Convert.FromBase64String (s);
2940 AssertEquals ("String-IgnoreCharsAfter-Ignored", 15, data.Length);
2942 char[] c = s.ToCharArray ();
2943 data = Convert.FromBase64CharArray (c, 0, c.Length);
2944 AssertEquals ("CharArray-IgnoreCharsAfter-Ignored", 15, data.Length);
2947 public void TestConvertFromNull() {
2949 AssertEquals ("#W1", false, Convert.ToBoolean (null as object));
2950 AssertEquals ("#W2", 0, Convert.ToByte (null as object));
2951 AssertEquals ("#W3", 0, Convert.ToChar (null as object));
2952 AssertEquals ("#W4", new DateTime (1,1,1,0,0,0), Convert.ToDateTime (null as object));
2953 AssertEquals ("#W5", 0, Convert.ToDecimal (null as object));
2954 AssertEquals ("#W6", 0, Convert.ToDouble (null as object));
2955 AssertEquals ("#W7", 0, Convert.ToInt16 (null as object));
2956 AssertEquals ("#W8", 0, Convert.ToInt32 (null as object));
2957 AssertEquals ("#W9", 0, Convert.ToInt64 (null as object));
2958 AssertEquals ("#W10", 0, Convert.ToSByte (null as object));
2959 AssertEquals ("#W11", 0, Convert.ToSingle (null as object));
2960 AssertEquals ("#W12", "", Convert.ToString (null as object));
2961 AssertEquals ("#W13", 0, Convert.ToUInt16 (null as object));
2962 AssertEquals ("#W14", 0, Convert.ToUInt32 (null as object));
2963 AssertEquals ("#W15", 0, Convert.ToUInt64 (null as object));
2964 AssertEquals ("#W16", false, Convert.ToBoolean (null as string));
2965 AssertEquals ("#W17", 0, Convert.ToByte (null as string));
2967 try {
2968 Convert.ToChar (null as string);
2969 Fail ();
2970 } catch (Exception e) {
2971 AssertEquals ("#W18", typeof (ArgumentNullException), e.GetType ());
2974 AssertEquals ("#W19", new DateTime (1,1,1,0,0,0), Convert.ToDateTime (null as string));
2975 AssertEquals ("#W20", 0, Convert.ToDecimal (null as string));
2976 AssertEquals ("#W21", 0, Convert.ToDouble (null as string));
2977 AssertEquals ("#W22", 0, Convert.ToInt16 (null as string));
2978 AssertEquals ("#W23", 0, Convert.ToInt32 (null as string));
2979 AssertEquals ("#W24", 0, Convert.ToInt64 (null as string));
2980 AssertEquals ("#W25", 0, Convert.ToSByte (null as string));
2981 AssertEquals ("#W26", 0, Convert.ToSingle (null as string));
2982 AssertEquals ("#W27", null, Convert.ToString (null as string));
2983 AssertEquals ("#W28", 0, Convert.ToUInt16 (null as string));
2984 AssertEquals ("#W29", 0, Convert.ToUInt32 (null as string));
2985 AssertEquals ("#W30", 0, Convert.ToUInt64 (null as string));
2988 [Test]
2989 public void ToByte_PrefixedHexStringInBase16 ()
2991 AssertEquals ("0xff", 255, Convert.ToByte ("0xff", 16));
2992 AssertEquals ("0xfF", 255, Convert.ToByte ("0xfF", 16));
2993 AssertEquals ("0xFf", 255, Convert.ToByte ("0xFf", 16));
2994 AssertEquals ("0xFF", 255, Convert.ToByte ("0xFF", 16));
2996 AssertEquals ("0Xff", 255, Convert.ToByte ("0Xff", 16));
2997 AssertEquals ("0XfF", 255, Convert.ToByte ("0XfF", 16));
2998 AssertEquals ("0XFf", 255, Convert.ToByte ("0XFf", 16));
2999 AssertEquals ("0XFF", 255, Convert.ToByte ("0XFF", 16));
3001 AssertEquals ("0x0", Byte.MinValue, Convert.ToByte ("0x0", 16));
3004 [Test]
3005 [ExpectedException (typeof (FormatException))]
3006 public void ToByte_BadHexPrefix ()
3008 Convert.ToByte ("0x", 16);
3011 [Test]
3012 [ExpectedException (typeof (OverflowException))]
3013 public void ToByte_NegativeString ()
3015 Convert.ToByte ("-1");
3018 [Test]
3019 [ExpectedException (typeof (ArgumentException))]
3020 public void ToByte_NegativeStringNonBase10 ()
3022 Convert.ToByte ("-0", 16);
3025 [Test]
3026 [ExpectedException (typeof (OverflowException))]
3027 public void ToByte_NegativeString_Base10 ()
3029 Convert.ToByte ("-0", 10);
3032 [Test]
3033 public void ToByte_NegativeZeroString ()
3035 Convert.ToByte ("-0");
3036 Convert.ToByte ("-0", null);
3039 [Test]
3040 [ExpectedException (typeof (OverflowException))]
3041 public void ToUInt16_NegativeString ()
3043 Convert.ToUInt16 ("-1");
3046 [Test]
3047 [ExpectedException (typeof (ArgumentException))]
3048 public void ToUInt16_NegativeStringNonBase10 ()
3050 Convert.ToUInt16 ("-0", 16);
3053 [Test]
3054 [ExpectedException (typeof (OverflowException))]
3055 public void ToUInt16_NegativeString_Base10 ()
3057 Convert.ToUInt16 ("-0", 10);
3060 [Test]
3061 public void ToUInt16_NegativeZeroString ()
3063 Convert.ToUInt16 ("-0");
3064 Convert.ToUInt16 ("-0", null);
3067 [Test]
3068 [ExpectedException (typeof (OverflowException))]
3069 public void ToUInt32_NegativeString ()
3071 Convert.ToUInt32 ("-1");
3074 [Test]
3075 [ExpectedException (typeof (ArgumentException))]
3076 public void ToUInt32_NegativeStringNonBase10 ()
3078 Convert.ToUInt32 ("-0", 16);
3081 [Test]
3082 [ExpectedException (typeof (OverflowException))]
3083 public void ToUInt32_NegativeString_Base10 ()
3085 Convert.ToUInt32 ("-0", 10);
3088 [Test]
3089 public void ToUInt32_NegativeZeroString ()
3091 Convert.ToUInt32 ("-0");
3092 Convert.ToUInt32 ("-0", null);
3095 [Test]
3096 [ExpectedException (typeof (OverflowException))]
3097 public void ToUInt64_NegativeString ()
3099 Convert.ToUInt64 ("-1");
3102 [Test]
3103 [ExpectedException (typeof (ArgumentException))]
3104 public void ToUInt64_NegativeStringNonBase10 ()
3106 Convert.ToUInt64 ("-0", 16);
3109 [Test]
3110 [ExpectedException (typeof (OverflowException))]
3111 public void ToUInt64_NegativeString_Base10 ()
3113 Convert.ToUInt64 ("-0", 10);
3116 [Test]
3117 public void ToUInt64_NegativeZeroString ()
3119 Convert.ToUInt64 ("-0");
3120 Convert.ToUInt64 ("-0", null);
3123 // min/max unsigned
3125 [Test]
3126 public void ToByte_MaxValue ()
3128 AssertEquals ("ff,16", Byte.MaxValue, Convert.ToByte ("ff", 16));
3129 AssertEquals ("255,10", Byte.MaxValue, Convert.ToByte ("255", 10));
3130 AssertEquals ("377,8", Byte.MaxValue, Convert.ToByte ("377", 8));
3131 AssertEquals ("11111111,2", Byte.MaxValue, Convert.ToByte ("11111111", 2));
3134 [Test]
3135 public void ToByte_MinValue ()
3137 AssertEquals ("0,16", Byte.MinValue, Convert.ToByte ("0", 16));
3138 AssertEquals ("0,10", Byte.MinValue, Convert.ToByte ("0", 10));
3139 AssertEquals ("0,8", Byte.MinValue, Convert.ToByte ("0", 8));
3140 AssertEquals ("0,2", Byte.MinValue, Convert.ToByte ("0", 2));
3143 [Test]
3144 public void ToUInt16_MaxValue ()
3146 AssertEquals ("ffff,16", UInt16.MaxValue, Convert.ToUInt16 ("ffff", 16));
3147 AssertEquals ("65535,10", UInt16.MaxValue, Convert.ToUInt16 ("65535", 10));
3148 AssertEquals ("177777,8", UInt16.MaxValue, Convert.ToUInt16 ("177777", 8));
3149 AssertEquals ("1111111111111111,2", UInt16.MaxValue, Convert.ToUInt16 ("1111111111111111", 2));
3152 [Test]
3153 public void ToUInt16_MinValue ()
3155 AssertEquals ("0,16", UInt16.MinValue, Convert.ToUInt16 ("0", 16));
3156 AssertEquals ("0,10", UInt16.MinValue, Convert.ToUInt16 ("0", 10));
3157 AssertEquals ("0,8", UInt16.MinValue, Convert.ToUInt16 ("0", 8));
3158 AssertEquals ("0,2", UInt16.MinValue, Convert.ToUInt16 ("0", 2));
3161 [Test]
3162 public void ToUInt32_MaxValue ()
3164 AssertEquals ("ffffffff,16", UInt32.MaxValue, Convert.ToUInt32 ("ffffffff", 16));
3165 AssertEquals ("4294967295,10", UInt32.MaxValue, Convert.ToUInt32 ("4294967295", 10));
3166 AssertEquals ("37777777777,8", UInt32.MaxValue, Convert.ToUInt32 ("37777777777", 8));
3167 AssertEquals ("11111111111111111111111111111111,2", UInt32.MaxValue, Convert.ToUInt32 ("11111111111111111111111111111111", 2));
3170 [Test]
3171 public void ToUInt32_MinValue ()
3173 AssertEquals ("0,16", UInt32.MinValue, Convert.ToUInt32 ("0", 16));
3174 AssertEquals ("0,10", UInt32.MinValue, Convert.ToUInt32 ("0", 10));
3175 AssertEquals ("0,8", UInt32.MinValue, Convert.ToUInt32 ("0", 8));
3176 AssertEquals ("0,2", UInt32.MinValue, Convert.ToUInt32 ("0", 2));
3179 [Test]
3180 public void ToUInt64_MaxValue ()
3182 AssertEquals ("ffffffffffffffff,16", UInt64.MaxValue, Convert.ToUInt64 ("ffffffffffffffff", 16));
3183 AssertEquals ("18446744073709551615,10", UInt64.MaxValue, Convert.ToUInt64 ("18446744073709551615", 10));
3184 AssertEquals ("1777777777777777777777,8", UInt64.MaxValue, Convert.ToUInt64 ("1777777777777777777777", 8));
3185 AssertEquals ("1111111111111111111111111111111111111111111111111111111111111111,2", UInt64.MaxValue, Convert.ToUInt64 ("1111111111111111111111111111111111111111111111111111111111111111", 2));
3188 [Test]
3189 public void ToUInt64_MinValue ()
3191 AssertEquals ("0,16", UInt64.MinValue, Convert.ToUInt64 ("0", 16));
3192 AssertEquals ("0,10", UInt64.MinValue, Convert.ToUInt64 ("0", 10));
3193 AssertEquals ("0,8", UInt64.MinValue, Convert.ToUInt64 ("0", 8));
3194 AssertEquals ("0,2", UInt64.MinValue, Convert.ToUInt64 ("0", 2));
3197 // min/max signed
3199 [Test]
3200 public void ToSByte_MaxValue ()
3202 AssertEquals ("7F,16", SByte.MaxValue, Convert.ToSByte ("7f", 16));
3203 AssertEquals ("127,10", SByte.MaxValue, Convert.ToSByte ("127", 10));
3204 AssertEquals ("177,8", SByte.MaxValue, Convert.ToSByte ("177", 8));
3205 AssertEquals ("1111111,2", SByte.MaxValue, Convert.ToSByte ("1111111", 2));
3208 [Test]
3209 public void ToSByte_MinValue ()
3211 AssertEquals ("80,16", SByte.MinValue, Convert.ToSByte ("80", 16));
3212 AssertEquals ("-128,10", SByte.MinValue, Convert.ToSByte ("-128", 10));
3213 AssertEquals ("200,8", SByte.MinValue, Convert.ToSByte ("200", 8));
3214 AssertEquals ("10000000,2", SByte.MinValue, Convert.ToSByte ("10000000", 2));
3217 [Test]
3218 public void ToInt16_MaxValue ()
3220 AssertEquals ("7FFF,16", Int16.MaxValue, Convert.ToInt16 ("7fff", 16));
3221 AssertEquals ("32767,10", Int16.MaxValue, Convert.ToInt16 ("32767", 10));
3222 AssertEquals ("77777,8", Int16.MaxValue, Convert.ToInt16 ("77777", 8));
3223 AssertEquals ("111111111111111,2", Int16.MaxValue, Convert.ToInt16 ("111111111111111", 2));
3226 [Test]
3227 public void ToInt16_MinValue ()
3229 AssertEquals ("8000,16", Int16.MinValue, Convert.ToInt16 ("8000", 16));
3230 AssertEquals ("-32768,10", Int16.MinValue, Convert.ToInt16 ("-32768", 10));
3231 AssertEquals ("100000,8", Int16.MinValue, Convert.ToInt16 ("100000", 8));
3232 AssertEquals ("1000000000000000,2", Int16.MinValue, Convert.ToInt16 ("1000000000000000", 2));
3235 [Test]
3236 public void ToInt32_MaxValue ()
3238 AssertEquals ("7fffffff,16", Int32.MaxValue, Convert.ToInt32 ("7fffffff", 16));
3239 AssertEquals ("2147483647,10", Int32.MaxValue, Convert.ToInt32 ("2147483647", 10));
3240 AssertEquals ("17777777777,8", Int32.MaxValue, Convert.ToInt32 ("17777777777", 8));
3241 AssertEquals ("1111111111111111111111111111111,2", Int32.MaxValue, Convert.ToInt32 ("1111111111111111111111111111111", 2));
3244 [Test]
3245 public void ToInt32_MinValue ()
3247 AssertEquals ("80000000,16", Int32.MinValue, Convert.ToInt32 ("80000000", 16));
3248 AssertEquals ("-2147483648,10", Int32.MinValue, Convert.ToInt32 ("-2147483648", 10));
3249 AssertEquals ("20000000000,8", Int32.MinValue, Convert.ToInt32 ("20000000000", 8));
3250 AssertEquals ("10000000000000000000000000000000,2", Int32.MinValue, Convert.ToInt32 ("10000000000000000000000000000000", 2));
3253 [Test]
3254 public void ToInt64_MaxValue ()
3256 AssertEquals ("7fffffffffffffff,16", Int64.MaxValue, Convert.ToInt64 ("7fffffffffffffff", 16));
3257 AssertEquals ("9223372036854775807,10", Int64.MaxValue, Convert.ToInt64 ("9223372036854775807", 10));
3258 AssertEquals ("777777777777777777777,8", Int64.MaxValue, Convert.ToInt64 ("777777777777777777777", 8));
3259 AssertEquals ("111111111111111111111111111111111111111111111111111111111111111,2", Int64.MaxValue, Convert.ToInt64 ("111111111111111111111111111111111111111111111111111111111111111", 2));
3262 [Test]
3263 public void ToInt64_MinValue ()
3265 AssertEquals ("8000000000000000,16", Int64.MinValue, Convert.ToInt64 ("8000000000000000", 16));
3266 AssertEquals ("-9223372036854775808,10", Int64.MinValue, Convert.ToInt64 ("-9223372036854775808", 10));
3267 AssertEquals ("1000000000000000000000,8", Int64.MinValue, Convert.ToInt64 ("1000000000000000000000", 8));
3268 AssertEquals ("1000000000000000000000000000000000000000000000000000000000000000,2", Int64.MinValue, Convert.ToInt64 ("1000000000000000000000000000000000000000000000000000000000000000", 2));
3271 // signed types
3273 [Test]
3274 [ExpectedException (typeof (OverflowException))]
3275 public void ToSByte_OverMaxValue ()
3277 string max_plus1 = "128";
3278 Convert.ToSByte (max_plus1);
3281 [Test]
3282 [ExpectedException (typeof (OverflowException))]
3283 public void ToSByte_OverMinValue ()
3285 string min_minus1 = "-129";
3286 Convert.ToSByte (min_minus1);
3289 [Test]
3290 [ExpectedException (typeof (OverflowException))]
3291 public void ToInt16_OverMaxValue ()
3293 string max_plus1 = "32768";
3294 Convert.ToInt16 (max_plus1);
3297 [Test]
3298 [ExpectedException (typeof (OverflowException))]
3299 public void ToInt16_OverMinValue ()
3301 string min_minus1 = "-32769";
3302 Convert.ToInt16 (min_minus1);
3305 [Test]
3306 [ExpectedException (typeof (OverflowException))]
3307 public void ToInt32_OverMaxValue ()
3309 string max_plus1 = "2147483648";
3310 Convert.ToInt32 (max_plus1);
3313 [Test]
3314 [ExpectedException (typeof (OverflowException))]
3315 public void ToInt32_OverMinValue ()
3317 string min_minus1 = "-2147483649";
3318 Convert.ToInt32 (min_minus1);
3321 [Test]
3322 [ExpectedException (typeof (OverflowException))]
3323 public void ToInt64_OverMaxValue ()
3325 string max_plus1 = "9223372036854775808";
3326 Convert.ToInt64 (max_plus1);
3329 [Test]
3330 [ExpectedException (typeof (OverflowException))]
3331 public void ToInt64_OverMinValue ()
3333 string min_minus1 = "-9223372036854775809";
3334 Convert.ToInt64 (min_minus1);
3337 // unsigned types
3339 [Test]
3340 [ExpectedException (typeof (OverflowException))]
3341 public void ToByte_OverMaxValue ()
3343 string max_plus1 = "257";
3344 Convert.ToByte (max_plus1);
3347 [Test]
3348 [ExpectedException (typeof (OverflowException))]
3349 public void ToByte_OverMinValue ()
3351 string min_minus1 = "-1";
3352 Convert.ToByte (min_minus1);
3355 [Test]
3356 [ExpectedException (typeof (OverflowException))]
3357 public void ToUInt16_OverMaxValue ()
3359 string max_plus1 = "65536";
3360 Convert.ToUInt16 (max_plus1);
3363 [Test]
3364 [ExpectedException (typeof (OverflowException))]
3365 public void ToUInt16_OverMinValue ()
3367 string min_minus1 = "-1";
3368 Convert.ToUInt16 (min_minus1);
3371 [Test]
3372 [ExpectedException (typeof (OverflowException))]
3373 public void ToUInt32_OverMaxValue ()
3375 string max_plus1 = "4294967296";
3376 Convert.ToUInt32 (max_plus1);
3379 [Test]
3380 [ExpectedException (typeof (OverflowException))]
3381 public void ToUInt32_OverMinValue ()
3383 string min_minus1 = "-1";
3384 Convert.ToUInt32 (min_minus1);
3387 [Test]
3388 [ExpectedException (typeof (OverflowException))]
3389 public void ToUInt64_OverMaxValue ()
3391 string max_plus1 = "18446744073709551616";
3392 Convert.ToUInt64 (max_plus1);
3395 [Test]
3396 [ExpectedException (typeof (OverflowException))]
3397 public void ToUInt64_OverMinValue ()
3399 string min_minus1 = "-1";
3400 Convert.ToUInt64 (min_minus1);
3403 [Test]
3404 public void To_NullString ()
3406 string s = null;
3407 // signed
3408 AssertEquals ("ToSByte", 0, Convert.ToSByte (s));
3409 AssertEquals ("ToSByte+base", 0, Convert.ToSByte (s, 10));
3410 AssertEquals ("ToInt16", 0, Convert.ToInt16 (s));
3411 AssertEquals ("ToInt16+base", 0, Convert.ToInt16 (s, 10));
3412 AssertEquals ("ToInt32", 0, Convert.ToInt32 (s));
3413 AssertEquals ("ToInt32+base", 0, Convert.ToInt32 (s, 10));
3414 AssertEquals ("ToInt64", 0, Convert.ToInt64 (s));
3415 AssertEquals ("ToInt64+base", 0, Convert.ToInt64 (s, 10));
3416 // unsigned
3417 AssertEquals ("ToByte", 0, Convert.ToByte (s));
3418 AssertEquals ("ToByte+base", 0, Convert.ToByte (s, 10));
3419 AssertEquals ("ToUInt16", 0, Convert.ToUInt16 (s));
3420 AssertEquals ("ToUInt16+base", 0, Convert.ToUInt16 (s, 10));
3421 AssertEquals ("ToUInt32", 0, Convert.ToUInt32 (s));
3422 AssertEquals ("ToUInt32+base", 0, Convert.ToUInt32 (s, 10));
3423 AssertEquals ("ToUInt64", 0, Convert.ToUInt64 (s));
3424 AssertEquals ("ToUInt64+base", 0, Convert.ToUInt64 (s, 10));
3427 [Test]
3428 public void To_NullObject ()
3430 object o = null;
3431 // signed
3432 AssertEquals ("ToSByte", 0, Convert.ToSByte (o));
3433 AssertEquals ("ToInt16", 0, Convert.ToInt16 (o));
3434 AssertEquals ("ToInt32", 0, Convert.ToInt32 (o));
3435 AssertEquals ("ToInt64", 0, Convert.ToInt64 (o));
3436 // unsigned
3437 AssertEquals ("ToByte", 0, Convert.ToByte (o));
3438 AssertEquals ("ToUInt16", 0, Convert.ToUInt16 (o));
3439 AssertEquals ("ToUInt32", 0, Convert.ToUInt32 (o));
3440 AssertEquals ("ToUInt64", 0, Convert.ToUInt64 (o));
3443 [Test]
3444 public void To_NullObjectFormatProvider ()
3446 object o = null;
3447 IFormatProvider fp = (IFormatProvider) new NumberFormatInfo ();
3448 // signed
3449 AssertEquals ("ToSByte", 0, Convert.ToSByte (o, fp));
3450 AssertEquals ("ToInt16", 0, Convert.ToInt16 (o, fp));
3451 AssertEquals ("ToInt32", 0, Convert.ToInt32 (o, fp));
3452 AssertEquals ("ToInt64", 0, Convert.ToInt64 (o, fp));
3453 // unsigned
3454 AssertEquals ("ToByte", 0, Convert.ToByte (o, fp));
3455 AssertEquals ("ToUInt16", 0, Convert.ToUInt16 (o, fp));
3456 AssertEquals ("ToUInt32", 0, Convert.ToUInt32 (o, fp));
3457 AssertEquals ("ToUInt64", 0, Convert.ToUInt64 (o, fp));
3460 [Test]
3461 [ExpectedException (typeof (ArgumentNullException))]
3462 public void ToSByte_NullStringFormatProvider ()
3464 string s = null;
3465 // SByte is a "special" case ???
3466 Convert.ToSByte (s, new NumberFormatInfo ());
3469 [Test]
3470 public void To_NullStringFormatProvider ()
3472 string s = null;
3473 IFormatProvider fp = (IFormatProvider) new NumberFormatInfo ();
3474 // signed
3475 // No SByte here
3476 AssertEquals ("ToInt16", 0, Convert.ToInt16 (s, fp));
3477 AssertEquals ("ToInt32", 0, Convert.ToInt32 (s, fp));
3478 AssertEquals ("ToInt64", 0, Convert.ToInt64 (s, fp));
3479 // unsigned
3480 AssertEquals ("ToByte", 0, Convert.ToByte (s, fp));
3481 AssertEquals ("ToUInt16", 0, Convert.ToUInt16 (s, fp));
3482 AssertEquals ("ToUInt32", 0, Convert.ToUInt32 (s, fp));
3483 AssertEquals ("ToUInt64", 0, Convert.ToUInt64 (s, fp));
3486 [Test]
3487 [ExpectedException (typeof (InvalidCastException))]
3488 public void ChangeTypeToTypeCodeEmpty ()
3490 Convert.ChangeType (true, TypeCode.Empty);
3493 [Test]
3494 [ExpectedException (typeof (InvalidCastException))]
3495 public void ChangeTypeNullToValuetype ()
3497 Convert.ChangeType (null, typeof (int));
3500 [Test]
3501 public void ToString_MinMax_WithBase ()
3503 AssertEquals ("Byte.MinValue base 2", "0", Convert.ToString (Byte.MinValue, 2));
3504 AssertEquals ("Byte.MinValue base 8", "0", Convert.ToString (Byte.MinValue, 8));
3505 AssertEquals ("Byte.MinValue base 10", "0", Convert.ToString (Byte.MinValue, 10));
3506 AssertEquals ("Byte.MinValue base 16", "0", Convert.ToString (Byte.MinValue, 16));
3508 AssertEquals ("Byte.MaxValue base 2", "11111111", Convert.ToString (Byte.MaxValue, 2));
3509 AssertEquals ("Byte.MaxValue base 8", "377", Convert.ToString (Byte.MaxValue, 8));
3510 AssertEquals ("Byte.MaxValue base 10", "255", Convert.ToString (Byte.MaxValue, 10));
3511 AssertEquals ("Byte.MaxValue base 16", "ff", Convert.ToString (Byte.MaxValue, 16));
3513 AssertEquals ("Int16.MinValue base 2", "1000000000000000", Convert.ToString (Int16.MinValue, 2));
3514 AssertEquals ("Int16.MinValue base 8", "100000", Convert.ToString (Int16.MinValue, 8));
3515 AssertEquals ("Int16.MinValue base 10", "-32768", Convert.ToString (Int16.MinValue, 10));
3516 AssertEquals ("Int16.MinValue base 16", "8000", Convert.ToString (Int16.MinValue, 16));
3518 AssertEquals ("Int16.MaxValue base 2", "111111111111111", Convert.ToString (Int16.MaxValue, 2));
3519 AssertEquals ("Int16.MaxValue base 8", "77777", Convert.ToString (Int16.MaxValue, 8));
3520 AssertEquals ("Int16.MaxValue base 10", "32767", Convert.ToString (Int16.MaxValue, 10));
3521 AssertEquals ("Int16.MaxValue base 16", "7fff", Convert.ToString (Int16.MaxValue, 16));
3523 AssertEquals ("Int32.MinValue base 2", "10000000000000000000000000000000", Convert.ToString (Int32.MinValue, 2));
3524 AssertEquals ("Int32.MinValue base 8", "20000000000", Convert.ToString (Int32.MinValue, 8));
3525 AssertEquals ("Int32.MinValue base 10", "-2147483648", Convert.ToString (Int32.MinValue, 10));
3526 AssertEquals ("Int32.MinValue base 16", "80000000", Convert.ToString (Int32.MinValue, 16));
3528 AssertEquals ("Int32.MaxValue base 2", "1111111111111111111111111111111", Convert.ToString (Int32.MaxValue, 2));
3529 AssertEquals ("Int32.MaxValue base 8", "17777777777", Convert.ToString (Int32.MaxValue, 8));
3530 AssertEquals ("Int32.MaxValue base 10", "2147483647", Convert.ToString (Int32.MaxValue, 10));
3531 AssertEquals ("Int32.MaxValue base 16", "7fffffff", Convert.ToString (Int32.MaxValue, 16));
3533 AssertEquals ("Int64.MinValue base 2", "1000000000000000000000000000000000000000000000000000000000000000", Convert.ToString (Int64.MinValue, 2));
3534 AssertEquals ("Int64.MinValue base 8", "1000000000000000000000", Convert.ToString (Int64.MinValue, 8));
3535 AssertEquals ("Int64.MinValue base 10", "-9223372036854775808", Convert.ToString (Int64.MinValue, 10));
3536 AssertEquals ("Int64.MinValue base 16", "8000000000000000", Convert.ToString (Int64.MinValue, 16));
3538 AssertEquals ("Int64.MaxValue base 2", "111111111111111111111111111111111111111111111111111111111111111", Convert.ToString (Int64.MaxValue, 2));
3539 AssertEquals ("Int64.MaxValue base 8", "777777777777777777777", Convert.ToString (Int64.MaxValue, 8));
3540 AssertEquals ("Int64.MaxValue base 10", "9223372036854775807", Convert.ToString (Int64.MaxValue, 10));
3541 AssertEquals ("Int64.MaxValue base 16", "7fffffffffffffff", Convert.ToString (Int64.MaxValue, 16));