Merge pull request #601 from knocte/sock_improvements
[mono.git] / mcs / class / corlib / Test / System / UInt32Test.cs
index 525f3d53fb3de702ceaacae9ae8eaac3d9d1f9b5..20f5a299e36fdf12f742ee4c953dde0a8f9f90e6 100644 (file)
@@ -14,7 +14,7 @@ namespace MonoTests.System
 {
 
 [TestFixture]
-public class UInt32Test : Assertion
+public class UInt32Test 
 {
        private const UInt32 MyUInt32_1 = 42;
        private const UInt32 MyUInt32_2 = 0;
@@ -40,7 +40,7 @@ public class UInt32Test : Assertion
        
        private CultureInfo old_culture;
 
-       [TestFixtureSetUp]
+       [SetUp]
        public void SetUp () 
        {
                old_culture = Thread.CurrentThread.CurrentCulture;
@@ -62,61 +62,65 @@ public class UInt32Test : Assertion
                Results2 [6] = perPattern.Replace ("n","429,496,729,500.00000");
        }
 
-       [TestFixtureTearDown]
+       [TearDown]
        public void TearDown ()
        {
                Thread.CurrentThread.CurrentCulture = old_culture;
        }
 
+       [Test]
        public void TestMinMax()
        {
                
-               AssertEquals(UInt32.MinValue, MyUInt32_2);
-               AssertEquals(UInt32.MaxValue, MyUInt32_3);
+               Assert.AreEqual(UInt32.MinValue, MyUInt32_2);
+               Assert.AreEqual(UInt32.MaxValue, MyUInt32_3);
        }
-       
+
+       [Test]
        public void TestCompareTo()
        {
-               Assert(MyUInt32_3.CompareTo(MyUInt32_2) > 0);
-               Assert(MyUInt32_2.CompareTo(MyUInt32_2) == 0);
-               Assert(MyUInt32_1.CompareTo((UInt32)(42)) == 0);
-               Assert(MyUInt32_2.CompareTo(MyUInt32_3) < 0);
-               Assert (1 == UInt32.Parse ("1"));
-               Assert (1 == UInt32.Parse (" 1"));
-               Assert (1 == UInt32.Parse ("     1"));
-               Assert (1 == UInt32.Parse ("1    "));
-               Assert (1 == UInt32.Parse ("+1"));
+               Assert.IsTrue(MyUInt32_3.CompareTo(MyUInt32_2) > 0);
+               Assert.IsTrue(MyUInt32_2.CompareTo(MyUInt32_2) == 0);
+               Assert.IsTrue(MyUInt32_1.CompareTo((UInt32)(42)) == 0);
+               Assert.IsTrue(MyUInt32_2.CompareTo(MyUInt32_3) < 0);
+               Assert.IsTrue (1 == UInt32.Parse ("1"));
+               Assert.IsTrue (1 == UInt32.Parse (" 1"));
+               Assert.IsTrue (1 == UInt32.Parse ("     1"));
+               Assert.IsTrue (1 == UInt32.Parse ("1    "));
+               Assert.IsTrue (1 == UInt32.Parse ("+1"));
 
                try {
                        UInt32.Parse (" + 1 ");
-                       Fail ("Should raise FormatException1");
+                       Assert.Fail ("Should raise FormatException1");
                } catch (Exception e){
-                       Assert (typeof (FormatException) == e.GetType ());
+                       Assert.IsTrue (typeof (FormatException) == e.GetType ());
                }
 
                try {
                        UInt32.Parse (" + ");
-                       Fail ("Should raise FormatException");
+                       Assert.Fail ("Should raise FormatException");
                } catch (Exception e){
-                       Assert (typeof (FormatException) == e.GetType ());
+                       Assert.IsTrue (typeof (FormatException) == e.GetType ());
                }
                try {
                        MyUInt32_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(MyUInt32_1.Equals(MyUInt32_1));
-               Assert(MyUInt32_1.Equals((object)(UInt32)(42)));
-               Assert(MyUInt32_1.Equals((object)(SByte)(42)) == false);
-               Assert(MyUInt32_1.Equals(MyUInt32_2) == false);
+               Assert.IsTrue(MyUInt32_1.Equals(MyUInt32_1));
+               Assert.IsTrue(MyUInt32_1.Equals((object)(UInt32)(42)));
+               Assert.IsTrue(MyUInt32_1.Equals((object)(SByte)(42)) == false);
+               Assert.IsTrue(MyUInt32_1.Equals(MyUInt32_2) == false);
        }
-       
+
+       [Test]
        public void TestGetHashCode()
        {
                try {
@@ -125,29 +129,30 @@ public class UInt32Test : Assertion
                        MyUInt32_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("Parse problem on \""+MyString1+"\"", MyUInt32_1 == UInt32.Parse(MyString1));
-               Assert("Parse problem on \""+MyString2+"\"", MyUInt32_2 == UInt32.Parse(MyString2));
-               Assert("Parse problem on \""+MyString3+"\"", MyUInt32_3 == UInt32.Parse(MyString3));
+               Assert.IsTrue(MyUInt32_1 == UInt32.Parse(MyString1), "Parse problem on \""+MyString1+"\"");
+               Assert.IsTrue(MyUInt32_2 == UInt32.Parse(MyString2), "Parse problem on \""+MyString2+"\"");
+               Assert.IsTrue(MyUInt32_3 == UInt32.Parse(MyString3), "Parse problem on \""+MyString3+"\"");
                try {
                        UInt32.Parse(null);
-                       Fail("Should raise a System.ArgumentNullException");
+                       Assert.Fail("Should raise a System.ArgumentNullException");
                }
                catch (Exception e) {
-                       Assert("Did not get ArgumentNullException type", typeof(ArgumentNullException) == e.GetType());
+                       Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "Did not get ArgumentNullException type");
                }
                try {
                        UInt32.Parse("not-a-number");
-                       Fail("Should raise a System.FormatException");
+                       Assert.Fail("Should raise a System.FormatException");
                }
                catch (Exception e) {
-                       Assert("Did not get FormatException type", typeof(FormatException) == e.GetType());
+                       Assert.IsTrue(typeof(FormatException) == e.GetType(), "Did not get FormatException type");
                }
                try {
                        // TODO: Use this after ToString() is completed. For now, hard code string that generates
@@ -155,82 +160,164 @@ public class UInt32Test : Assertion
                        //double OverInt = (double)UInt32.MaxValue + 1;
                        //UInt32.Parse(OverInt.ToString());
                        UInt32.Parse("4294967296");
-                       Fail("Should raise a System.OverflowException");
+                       Assert.Fail("Should raise a System.OverflowException");
                }
                catch (Exception e) {
-                       Assert("Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e.GetType()+"'", typeof(OverflowException) == e.GetType());
+                       Assert.IsTrue(typeof(OverflowException) == e.GetType(), "Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e.GetType()+"'");
                }
                //test Parse(string s, NumberStyles style)
-               Assert(42 == UInt32.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
+               Assert.IsTrue(42 == UInt32.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
                try {
                        UInt32.Parse("$42", 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());
                }
                //test Parse(string s, IFormatProvider provider)
-               Assert(42 == UInt32.Parse(" 42 ", Nfi));
+               Assert.IsTrue(42 == UInt32.Parse(" 42 ", Nfi));
                try {
                        UInt32.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 == UInt32.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
+               Assert.IsTrue(16 == UInt32.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
                try {
                        UInt32.Parse("$42", NumberStyles.Integer, 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());
                }
+               // Pass a DateTimeFormatInfo, it is unable to format
+               // numbers, but we should not crash
+               
+               UInt32.Parse ("123", new DateTimeFormatInfo ());
+
+               Assert.AreEqual (734561, UInt32.Parse ("734561\0"), "C#43");
+               Assert.AreEqual (734561, UInt32.Parse ("734561\0\0\0    \0"), "C#44");
+               Assert.AreEqual (734561, UInt32.Parse ("734561\0\0\0    "), "C#45");
+               Assert.AreEqual (734561, UInt32.Parse ("734561\0\0\0"), "C#46");
+
+               Assert.AreEqual (0, UInt32.Parse ("0+", NumberStyles.Any), "#50");
        }
-       
+
+       [Test]
+       public void TestParseExponent ()
+       {
+               Assert.AreEqual (2, uint.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
+               Assert.AreEqual (20, uint.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
+               Assert.AreEqual (200, uint.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
+               Assert.AreEqual (2000000, uint.Parse ("2E6", NumberStyles.AllowExponent), "A#4");
+               Assert.AreEqual (200, uint.Parse ("2E+2", NumberStyles.AllowExponent), "A#5");
+               Assert.AreEqual (2, uint.Parse ("2", NumberStyles.AllowExponent), "A#6");
+               Assert.AreEqual (21, uint.Parse ("2.1E1", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#7");
+               Assert.AreEqual (520, uint.Parse (".52E3", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#8");
+               Assert.AreEqual (32500000, uint.Parse ("32.5E6", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#9");
+               Assert.AreEqual (890, uint.Parse ("8.9000E2", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#10");            
+
+               try {
+                       uint.Parse ("2E");
+                       Assert.Fail ("B#1");
+               } catch (FormatException) {
+               }
+
+               try {
+                       uint.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
+                       Assert.Fail ("B#2");
+               } catch (FormatException) {
+               }
+
+               try {
+                       uint.Parse ("2E 2", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#3");
+               } catch (FormatException) {
+               }
+
+               try {
+                       uint.Parse ("2E2 ", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#4");
+               } catch (FormatException) {
+               }
+
+               try {
+                       uint.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
+                       Assert.Fail ("B#5");
+               } catch (OverflowException) {
+               }
+
+               try {
+                       long exponent = (long) Int32.MaxValue + 10;
+                       uint.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
+                       Assert.Fail ("B#6");
+               } catch (OverflowException) {
+               }
+
+               try {
+                       uint.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
+                       Assert.Fail ("B#7");
+               } catch (OverflowException) {
+               }
+               
+               try {
+                       uint.Parse ("2 math e1", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#8");
+               } catch (FormatException) {
+               }
+
+               try {
+                       uint.Parse ("2.09E1",  NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);
+                       Assert.Fail ("B#9");
+               } catch (OverflowException) {
+               }
+       }
+
+       [Test]
        public void TestToString()
        {
                int TestNumber = 1;
                try {
                        //test ToString()
-                       AssertEquals(MyString1, MyUInt32_1.ToString());
+                       Assert.AreEqual(MyString1, MyUInt32_1.ToString());
                        TestNumber++;
-                       AssertEquals(MyString2, MyUInt32_2.ToString());
+                       Assert.AreEqual(MyString2, MyUInt32_2.ToString());
                        TestNumber++;
-                       AssertEquals(MyString3, MyUInt32_3.ToString());
+                       Assert.AreEqual(MyString3, MyUInt32_3.ToString());
                } catch (Exception e) {
-                       Fail("TestToString: Failed on TestNumber=" + TestNumber 
+                       Assert.Fail("TestToString: Assert.Failed on TestNumber=" + TestNumber 
                                + " with exception: " + e.ToString());
                }
 
                //test ToString(string format)
                for (int i=0; i < Formats1.Length; i++) {
                        try {
-                               AssertEquals(Results1[i], MyUInt32_2.ToString(Formats1[i]));
+                               Assert.AreEqual(Results1[i], MyUInt32_2.ToString(Formats1[i]));
                        } catch (Exception e) {
-                               Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i 
+                               Assert.Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i 
                                        + ". e = " + e.ToString());
                        }
 
                        try {
-                               AssertEquals(Results2[i], MyUInt32_3.ToString(Formats2[i]));
+                               Assert.AreEqual(Results2[i], MyUInt32_3.ToString(Formats2[i]));
                        } catch (Exception e) {
-                               Fail("TestToString: MyUInt32_3.ToString(Formats2[i]) i=" + i
+                               Assert.Fail("TestToString: MyUInt32_3.ToString(Formats2[i]) i=" + i
                                        + ". e = " + e.ToString());
                        }
                }
                //test ToString(string format, IFormatProvider provider);
                for (int i=0; i < Formats1.Length; i++) {
-                       AssertEquals(ResultsNfi1[i], MyUInt32_2.ToString(Formats1[i], Nfi));
-                       AssertEquals(ResultsNfi2[i], MyUInt32_3.ToString(Formats2[i], Nfi));
+                       Assert.AreEqual(ResultsNfi1[i], MyUInt32_2.ToString(Formats1[i], Nfi));
+                       Assert.AreEqual(ResultsNfi2[i], MyUInt32_3.ToString(Formats2[i], Nfi));
                }
                try {
                        MyUInt32_1.ToString("z");
-                       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());
                }
        }
 
@@ -240,14 +327,14 @@ public class UInt32Test : Assertion
                UInt32 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)");
        }
 }