Test DateTimeOffset.Parse with format yyyy-dd-MMzzz. Covers #22558.
[mono.git] / mcs / class / corlib / Test / System / ConvertTest.cs
index 0598692b65bac280623c211efb2c47aa1b1e8973..8aee8a9cc657db1a0dc9cdd3bdeb26f77da10970 100644 (file)
@@ -2678,6 +2678,31 @@ namespace MonoTests.System {
                  Convert.FromBase64String(brokenB64);
                }
 
+               [Test] // bug #5464
+               [ExpectedException (typeof (FormatException))]
+               public void TestInvalidBase64_Bug5464 ()
+               {
+                       Convert.FromBase64String ("dGVzdA==DQo=");
+               }
+
+               [Test] // bug #5464
+               public void TestValidBase64_Bug5464 ()
+               {
+                       byte[] result = Convert.FromBase64String ("dGVzdA==");
+                       Assert.AreEqual(4, result.Length, "Array.Length expected to be 4.");
+                       Assert.AreEqual(116, result[0], "#A01");
+                       Assert.AreEqual(101, result[1], "#A02");
+                       Assert.AreEqual(115, result[2], "#A03");
+                       Assert.AreEqual(116, result[3], "#A04");
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void TestInvalidBase64_TooManyPaddings ()
+               {
+                       Convert.FromBase64String ("dGVzd===");
+               }
+
                [Test]
                public void TestBeginWithSpaces ()
                {
@@ -2827,7 +2852,7 @@ namespace MonoTests.System {
                                Assert.AreEqual (typeof(ArgumentOutOfRangeException), e.GetType(), "#T08");
                        }               
                }
-#if NET_2_0
+
                [Test]
                public void ToBase64String_Bug76876 ()
                {
@@ -2882,7 +2907,7 @@ namespace MonoTests.System {
                        Assert.IsFalse (base64.Contains (Environment.NewLine), "58-nl"); // one lines
                        Assert.IsTrue (base64.EndsWith ("AA=="), "58-le"); // no NewLine
                }
-#endif
+
                /* Have experienced some problems with FromBase64CharArray using mono. Something 
                 * about error in a unicode file.
                 *
@@ -2898,7 +2923,6 @@ namespace MonoTests.System {
 
                [Test]
                [ExpectedException (typeof (FormatException))]
-               [Category ("TargetJvmNotWorking")]
                public void FromBase64CharArray_Empty ()
                {
                        Convert.FromBase64CharArray (new char[0], 0, 0);
@@ -2906,7 +2930,6 @@ namespace MonoTests.System {
 
                [Test]
                [ExpectedException (typeof (FormatException))]
-               [Category ("TargetJvmNotWorking")]
                public void FormatBase64CharArray_OnlyWhitespace ()
                {
                        Convert.FromBase64CharArray (new char[3] {' ', 
@@ -3036,23 +3059,15 @@ namespace MonoTests.System {
                }
 
                [Test]
-               [Category ("TargetJvmNotWorking")]
                public void FromBase64_Empty ()
                {
                        Assert.AreEqual (new byte[0], Convert.FromBase64String (string.Empty));
                }
 
                [Test]
-#if !NET_2_0
-               [ExpectedException (typeof (FormatException))]
-#endif
                public void FromBase64_OnlyWhiteSpace ()
                {
-#if NET_2_0
                        Assert.AreEqual (new byte[0], Convert.FromBase64String ("  \r\t"));
-#else
-                       Convert.FromBase64String ("  \r\t");
-#endif
                }
 
                [Test]
@@ -3084,6 +3099,7 @@ namespace MonoTests.System {
                        }
                }
 
+               [Test]
                public void TestConvertFromNull() {
                        
                        Assert.AreEqual (false, Convert.ToBoolean (null as object), "#W1");
@@ -3669,14 +3685,7 @@ namespace MonoTests.System {
                }
 
                [Test]
-               // 2005/01/10: The docs say this should throw an InvalidCastException,
-               // however, MS.NET 1.1 throws a NullReferenceException. Assuming docs
-               // are wrong.
-#if NET_2_0
                [ExpectedException (typeof (InvalidCastException))]
-#else
-               [ExpectedException (typeof (NullReferenceException))]
-#endif
                public void ChangeTypeNullToValuetype ()
                {
                        Convert.ChangeType (null, typeof (int));
@@ -4688,6 +4697,54 @@ namespace MonoTests.System {
                {
                        Convert.ChangeType ("this-is-a-string", typeof (Foo));
                }
+
+               [Test]
+               [ExpectedException (typeof (OverflowException))]
+               public void ToInt32_NaN ()
+               {
+                       Convert.ToInt32 (Double.NaN);
+               }
+
+               [Test]
+               public void ChangeTypeFromInvalidDouble ()
+               {
+                       // types which should generate OverflowException from double.NaN, etc.
+                       Type[] types = new Type []{
+                               typeof (byte), typeof (sbyte), typeof (decimal), 
+                               typeof (short), typeof (int), typeof (long),
+                               typeof (ushort), typeof (uint), typeof (ulong),
+                       };
+
+                       CheckChangeTypeOverflow ("double.NaN",              double.NaN,               types);
+                       CheckChangeTypeOverflow ("double.PositiveInfinity", double.PositiveInfinity,  types);
+                       CheckChangeTypeOverflow ("double.NegativeInfinity", double.NegativeInfinity,  types);
+                       CheckChangeTypeOverflow ("float.NaN",               float.NaN,                types);
+                       CheckChangeTypeOverflow ("float.PositiveInfinity",  float.PositiveInfinity,   types);
+                       CheckChangeTypeOverflow ("float.NegativeInfinity",  float.NegativeInfinity,   types);
+               }
+
+               static void CheckChangeTypeOverflow (string svalue, object value, Type[] types)
+               {
+                       foreach (Type type in types) {
+                               string message = string.Format (" when converting {0} to {1}", svalue, type.FullName);
+                               try {
+                                       Convert.ChangeType (value, type);
+                                       Assert.Fail ("Expected System.OverflowException " + message);
+                               }
+                               catch (OverflowException) {
+                                       // success
+                               }
+                               catch (Exception e) {
+                                       Assert.Fail ("Unexpected exception type " + e.GetType ().FullName + message);
+                               }
+                       }
+               }
+
+               [Test]
+               public void ToInt32_InvalidFormatProvider ()
+               {
+                       Assert.AreEqual (5, Convert.ToInt32 ("5", new InvalidFormatProvider ()));
+               }
        }
 
        public class Image
@@ -4814,4 +4871,12 @@ namespace MonoTests.System {
                        return (ulong)((IConvertible)this).ToType (typeof (ulong), provider);
                }
        }
+
+       class InvalidFormatProvider : IFormatProvider
+       {
+               public object GetFormat (Type formatType)
+               {
+                       return "";
+               }
+       }
 }