Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / Test / System / UInt64Test.cs
index 77a2947b3cf7a4d1317ccee532c54fc47e4c8c69..7633fea270fa99273483a29e4b40fbf9d4ca6802 100644 (file)
@@ -14,7 +14,7 @@ namespace MonoTests.System
 {
 
 [TestFixture]
-public class UInt64Test : Assertion
+public class UInt64Test 
 {
        private const UInt64 MyUInt64_1 = 42;
        private const UInt64 MyUInt64_2 = 0;
@@ -43,7 +43,7 @@ public class UInt64Test : Assertion
        
        private CultureInfo old_culture;
 
-       [TestFixtureSetUp]
+       [SetUp]
        public void SetUp() 
        {
                old_culture = Thread.CurrentThread.CurrentCulture;
@@ -65,42 +65,45 @@ public class UInt64Test : Assertion
                Results2 [6] = perPattern.Replace ("n","1,844,674,407,370,955,161,500.00000");
        }
 
-       [TestFixtureTearDown]
+       [TearDown]
        public void TearDown ()
        {
                Thread.CurrentThread.CurrentCulture = old_culture;
        }
 
+       [Test]
        public void TestMinMax()
        {
-               
-               AssertEquals(UInt64.MinValue, MyUInt64_2);
-               AssertEquals(UInt64.MaxValue, MyUInt64_3);
+               Assert.AreEqual(UInt64.MinValue, MyUInt64_2);
+               Assert.AreEqual(UInt64.MaxValue, MyUInt64_3);
        }
-       
+
+       [Test]
        public void TestCompareTo()
        {
-               Assert(MyUInt64_3.CompareTo(MyUInt64_2) > 0);
-               Assert(MyUInt64_2.CompareTo(MyUInt64_2) == 0);
-               Assert(MyUInt64_1.CompareTo((UInt64)(42)) == 0);
-               Assert(MyUInt64_2.CompareTo(MyUInt64_3) < 0);
+               Assert.IsTrue(MyUInt64_3.CompareTo(MyUInt64_2) > 0);
+               Assert.IsTrue(MyUInt64_2.CompareTo(MyUInt64_2) == 0);
+               Assert.IsTrue(MyUInt64_1.CompareTo((UInt64)(42)) == 0);
+               Assert.IsTrue(MyUInt64_2.CompareTo(MyUInt64_3) < 0);
                try {
                        MyUInt64_2.CompareTo((object)(Int16)100);
-                       Fail("Should raise a System.ArgumentException");
+                       Assert.Fail("Should raise a System.ArgumentException");
                }
                catch (Exception e) {
-                       Assert(typeof(ArgumentException) == e.GetType());
+                       Assert.IsTrue(typeof(ArgumentException) == e.GetType());
                }
        }
 
+       [Test]
        public void TestEquals()
        {
-               Assert(MyUInt64_1.Equals(MyUInt64_1));
-               Assert(MyUInt64_1.Equals((object)(UInt64)(42)));
-               Assert(MyUInt64_1.Equals((object)(SByte)(42)) == false);
-               Assert(MyUInt64_1.Equals(MyUInt64_2) == false);
+               Assert.IsTrue(MyUInt64_1.Equals(MyUInt64_1));
+               Assert.IsTrue(MyUInt64_1.Equals((object)(UInt64)(42)));
+               Assert.IsTrue(MyUInt64_1.Equals((object)(SByte)(42)) == false);
+               Assert.IsTrue(MyUInt64_1.Equals(MyUInt64_2) == false);
        }
-       
+
+       [Test]
        public void TestGetHashCode()
        {
                try {
@@ -109,97 +112,240 @@ public class UInt64Test : Assertion
                        MyUInt64_3.GetHashCode();
                }
                catch {
-                       Fail("GetHashCode should not raise an exception here");
+                       Assert.Fail("GetHashCode should not raise an exception here");
                }
        }
-       
+
+       [Test]
        public void TestParse()
        {
                //test Parse(string s)
-               Assert(MyUInt64_1 == UInt64.Parse(MyString1));
-               Assert(MyUInt64_2 == UInt64.Parse(MyString2));
-               Assert(MyUInt64_3 == UInt64.Parse(MyString3));
+               Assert.IsTrue(MyUInt64_1 == UInt64.Parse(MyString1));
+               Assert.IsTrue(MyUInt64_2 == UInt64.Parse(MyString2));
+               Assert.IsTrue(MyUInt64_3 == UInt64.Parse(MyString3));
                try {
                        UInt64.Parse(null);
-                       Fail("Should raise a ArgumentNullException");
+                       Assert.Fail("Should raise a ArgumentNullException");
                }
                catch (Exception e) {
-                       Assert(typeof(ArgumentNullException) == e.GetType());
+                       Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
                }
                try {
                        UInt64.Parse("not-a-number");
-                       Fail("Should raise a System.FormatException");
+                       Assert.Fail("Should raise a System.FormatException");
                }
                catch (Exception e) {
-                       Assert(typeof(FormatException) == e.GetType());
+                       Assert.IsTrue(typeof(FormatException) == e.GetType());
                }
                //test Parse(string s, NumberStyles style)
                try {
                        double OverInt = (double)UInt64.MaxValue + 1;
                        UInt64.Parse(OverInt.ToString(), NumberStyles.Float);
-                       Fail("Should raise a OverflowException");
+                       Assert.Fail("Should raise a OverflowException");
                }
                catch (Exception e) {
-                       Assert(typeof(OverflowException) == e.GetType());
+                       Assert.IsTrue(typeof(OverflowException) == e.GetType());
                }
                try {
                        double OverInt = (double)UInt64.MaxValue + 1;
                        UInt64.Parse(OverInt.ToString(), NumberStyles.Integer);
-                       Fail("Should raise a System.FormatException");
+                       Assert.Fail("Should raise a System.FormatException");
                }
                catch (Exception e) {
-                       Assert(typeof(FormatException) == e.GetType());
+                       Assert.IsTrue(typeof(FormatException) == e.GetType());
                }
-               Assert(42 == UInt64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
+               Assert.IsTrue(42 == UInt64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
                try {
                        UInt64.Parse("$42", NumberStyles.Integer);
-                       Fail("Should raise a FormatException");
+                       Assert.Fail("Should raise a FormatException");
                }
                catch (Exception e) {
-                       Assert(typeof(FormatException) == e.GetType());
+                       Assert.IsTrue(typeof(FormatException) == e.GetType());
                }
                //test Parse(string s, IFormatProvider provider)
-               Assert(42 == UInt64.Parse(" 42 ", Nfi));
+               Assert.IsTrue(42 == UInt64.Parse(" 42 ", Nfi));
                try {
                        UInt64.Parse("%42", Nfi);
-                       Fail("Should raise a System.FormatException");
+                       Assert.Fail("Should raise a System.FormatException");
                }
                catch (Exception e) {
-                       Assert(typeof(FormatException) == e.GetType());
+                       Assert.IsTrue(typeof(FormatException) == e.GetType());
                }
                //test Parse(string s, NumberStyles style, IFormatProvider provider)
-               Assert(16 == UInt64.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
+               Assert.IsTrue(16 == UInt64.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
                try {
                        UInt64.Parse("$42", NumberStyles.Integer, Nfi);
-                       Fail("Should raise a System.FormatException");
+                       Assert.Fail("Should raise a System.FormatException");
+               } catch (FormatException e) {
                }
-               catch (Exception e) {
-                       Assert(typeof(FormatException) == e.GetType());
+
+               try {
+                       UInt64.Parse ("5", NumberStyles.Any, CultureInfo.InvariantCulture);
+                       Assert.Fail ("C#42");
+               } catch (FormatException) {
                }
+
+               // Pass a DateTimeFormatInfo, it is unable to format
+               // numbers, but we should not crash             
+               UInt64.Parse ("123", new DateTimeFormatInfo ());
+
+               Assert.AreEqual (734561, UInt64.Parse ("734561\0"), "C#43");
+               Assert.AreEqual (734561, UInt64.Parse ("734561\0\0\0    \0"), "C#44");
+               Assert.AreEqual (734561, UInt64.Parse ("734561\0\0\0    "), "C#45");
+               Assert.AreEqual (734561, UInt64.Parse ("734561\0\0\0"), "C#46");
+
+               Assert.AreEqual (0, UInt64.Parse ("0+", NumberStyles.Any), "#50");
        }
-       
+
+       [Test]
+       public void TestParseExponent ()
+       {
+               Assert.AreEqual (2, ulong.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
+               Assert.AreEqual (20, ulong.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
+               Assert.AreEqual (200, ulong.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
+               Assert.AreEqual (2000000, ulong.Parse ("2E6", NumberStyles.AllowExponent), "A#4");
+               Assert.AreEqual (200, ulong.Parse ("2E+2", NumberStyles.AllowExponent), "A#5");
+               Assert.AreEqual (2, ulong.Parse ("2", NumberStyles.AllowExponent), "A#6");
+               Assert.AreEqual (21, ulong.Parse ("2.1E1", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#7");
+               Assert.AreEqual (520, ulong.Parse (".52E3", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#8");
+               Assert.AreEqual (32500000, ulong.Parse ("32.5E6", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#9");
+               Assert.AreEqual (890, ulong.Parse ("8.9000E2", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#10");           
+
+               try {
+                       ulong.Parse ("2E");
+                       Assert.Fail ("B#1");
+               } catch (FormatException) {
+               }
+
+               try {
+                       ulong.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
+                       Assert.Fail ("B#2");
+               } catch (FormatException) {
+               }
+
+               try {
+                       ulong.Parse ("2E 2", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#3");
+               } catch (FormatException) {
+               }
+
+               try {
+                       ulong.Parse ("2E2 ", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#4");
+               } catch (FormatException) {
+               }
+
+               try {
+                       ulong.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
+                       Assert.Fail ("B#5");
+               } catch (OverflowException) {
+               }
+
+               try {
+                       long exponent = (long) Int32.MaxValue + 10;
+                       ulong.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
+                       Assert.Fail ("B#6");
+               } catch (OverflowException) {
+               }
+
+               try {
+                       ulong.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
+                       Assert.Fail ("B#7");
+               } catch (OverflowException) {
+               }
+               
+               try {
+                       ulong.Parse ("2 math e1", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#8");
+               } catch (FormatException) {
+               }
+
+               try {
+                       ulong.Parse ("2.09E1",  NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);
+                       Assert.Fail ("B#9");
+               } catch (OverflowException) {
+               }
+       }
+
+       [Test]
+       public void TestTryParse()
+       {
+               ulong result;
+
+               Assert.AreEqual (true, ulong.TryParse (MyString1, out result));
+               Assert.AreEqual (MyUInt64_1, result);
+               Assert.AreEqual (true, ulong.TryParse (MyString2, out result));
+               Assert.AreEqual (MyUInt64_2, result);
+               Assert.AreEqual (true, ulong.TryParse (MyString3, out result));
+               Assert.AreEqual (MyUInt64_3, result);
+
+               Assert.AreEqual (true, ulong.TryParse ("1", out result));
+               Assert.AreEqual (1, result);
+               Assert.AreEqual (true, ulong.TryParse (" 1", out result));
+               Assert.AreEqual (1, result);
+               Assert.AreEqual (true, ulong.TryParse ("     1", out result));
+               Assert.AreEqual (1, result);
+               Assert.AreEqual (true, ulong.TryParse ("1    ", out result));
+               Assert.AreEqual (1, result);
+               Assert.AreEqual (true, ulong.TryParse ("+1", out result));
+               Assert.AreEqual (1, result);
+               Assert.AreEqual (false, ulong.TryParse ("-1", out result));
+               Assert.AreEqual (false, ulong.TryParse ("  -1", out result));
+               Assert.AreEqual (false, ulong.TryParse ("  -1  ", out result));
+               Assert.AreEqual (false, ulong.TryParse ("  -1  ", out result));
+
+               result = 1;
+               Assert.AreEqual (false, ulong.TryParse (null, out result));
+               Assert.AreEqual (0, result);
+
+               Assert.AreEqual (false, ulong.TryParse ("not-a-number", out result));
+
+               double OverInt = (double)ulong.MaxValue + 1;
+               Assert.AreEqual (false, ulong.TryParse (OverInt.ToString (), out result));
+               Assert.AreEqual (false, ulong.TryParse (OverInt.ToString (), NumberStyles.None, CultureInfo.InvariantCulture, out result));
+
+               Assert.AreEqual (false, ulong.TryParse ("$42", NumberStyles.Integer, null, out result));
+               Assert.AreEqual (false, ulong.TryParse ("%42", NumberStyles.Integer, Nfi, out result));
+               Assert.AreEqual (false, ulong.TryParse ("$42", NumberStyles.Integer, Nfi, out result));
+               Assert.AreEqual (false, ulong.TryParse (" - 1 ", out result));
+               Assert.AreEqual (false, ulong.TryParse (" - ", out result));
+               Assert.AreEqual (true, ulong.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
+               Assert.AreEqual (true, ulong.TryParse ("10000000000", out result));
+               Assert.AreEqual (false, ulong.TryParse ("-10000000000", out result));
+               Assert.AreEqual (true, ulong.TryParse ("7fffffff", NumberStyles.HexNumber, Nfi, out result));
+               Assert.AreEqual (int.MaxValue, result);
+               Assert.AreEqual (true, ulong.TryParse ("80000000", NumberStyles.HexNumber, Nfi, out result));
+               Assert.AreEqual (2147483648, result);
+               Assert.AreEqual (true, ulong.TryParse ("ffffffff", NumberStyles.HexNumber, Nfi, out result));
+               Assert.AreEqual (uint.MaxValue, result);
+               Assert.AreEqual (true, ulong.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
+               Assert.IsFalse (ulong.TryParse ("-", NumberStyles.AllowLeadingSign, Nfi, out result));
+               Assert.IsFalse (ulong.TryParse (Nfi.CurrencySymbol + "-", NumberStyles.AllowLeadingSign | NumberStyles.AllowCurrencySymbol, Nfi, out result));
+       }       
+
+       [Test]
        public void TestToString()
        {
                //test ToString()
-               AssertEquals("A", MyString1, MyUInt64_1.ToString());
-               AssertEquals("B", MyString2, MyUInt64_2.ToString());
-               AssertEquals("C", MyString3, MyUInt64_3.ToString());
+               Assert.AreEqual(MyString1, MyUInt64_1.ToString(), "A");
+               Assert.AreEqual(MyString2, MyUInt64_2.ToString(), "B");
+               Assert.AreEqual(MyString3, MyUInt64_3.ToString(), "C");
                //test ToString(string format)
                for (int i=0; i < Formats1.Length; i++) {
-                       AssertEquals("D", Results1[i], MyUInt64_2.ToString(Formats1[i]));
-                       AssertEquals("E: format #" + i, Results2[i], MyUInt64_3.ToString(Formats2[i]));
+                       Assert.AreEqual(Results1[i], MyUInt64_2.ToString(Formats1[i]), "D");
+                       Assert.AreEqual(Results2[i], MyUInt64_3.ToString(Formats2[i]), "E: format #" + i);
                }
                //test ToString(string format, IFormatProvider provider);
                for (int i=0; i < Formats1.Length; i++) {
-                       AssertEquals("F", ResultsNfi1[i], MyUInt64_2.ToString(Formats1[i], Nfi));
-                       AssertEquals("G", ResultsNfi2[i], MyUInt64_3.ToString(Formats2[i], Nfi));
+                       Assert.AreEqual(ResultsNfi1[i], MyUInt64_2.ToString(Formats1[i], Nfi), "F");
+                       Assert.AreEqual(ResultsNfi2[i], MyUInt64_3.ToString(Formats2[i], Nfi), "G");
                }
                try {
                        MyUInt64_1.ToString("z");
-                       Fail("Should raise a System.FormatException");
+                       Assert.Fail("Should raise a System.FormatException");
                }
                catch (Exception e) {
-                       Assert("H", typeof(FormatException) == e.GetType());
+                       Assert.IsTrue(typeof(FormatException) == e.GetType(), "H");
                }
        }
 
@@ -209,14 +355,14 @@ public class UInt64Test : Assertion
                UInt64 i = 254;
                // everything defaults to "G"
                string def = i.ToString ("G");
-               AssertEquals ("ToString()", def, i.ToString ());
-               AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
-               AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
-               AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
-               AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
-               AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
-
-               AssertEquals ("ToString(G)", "254", def);
+               Assert.AreEqual (def, i.ToString (), "ToString()");
+               Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
+               Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
+               Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
+               Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
+               Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
+
+               Assert.AreEqual ("254", def, "ToString(G)");
        }
 }