[corlib] Make primitive types smaller than int compare result compatible
[mono.git] / mcs / class / corlib / Test / System / Int16Test.cs
index aa4285eadd9b34b7cb289fe0153709975e042f2e..93317046963230c0fed81c4b0bbb9fd69e41949a 100644 (file)
@@ -14,7 +14,7 @@ namespace MonoTests.System
 {
 
 [TestFixture]
-public class Int16Test : Assertion
+public class Int16Test 
 {
        private const Int16 MyInt16_1 = -42;
        private const Int16 MyInt16_2 = -32768;
@@ -37,7 +37,7 @@ public class Int16Test : Assertion
        private CultureInfo old_culture;
 
        [TestFixtureSetUp]
-       public void SetUp () 
+       public void SetUpFixture () 
        {
                old_culture = Thread.CurrentThread.CurrentCulture;
 
@@ -48,6 +48,12 @@ public class Int16Test : Assertion
                Results1 [0] = "("+NumberFormatInfo.CurrentInfo.CurrencySymbol+"32,768.00)";
                Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"32,767.00000";
        }
+       
+       [SetUp]
+       public void Setup ()
+       {
+               Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
+       }
 
        [TestFixtureTearDown]
        public void TearDown ()
@@ -55,36 +61,37 @@ public class Int16Test : Assertion
                Thread.CurrentThread.CurrentCulture = old_culture;
        }
 
+       [Test]
        public void TestMinMax()
        {
-               
-               AssertEquals(Int16.MinValue, MyInt16_2);
-               AssertEquals(Int16.MaxValue, MyInt16_3);
+               Assert.AreEqual(Int16.MinValue, MyInt16_2);
+               Assert.AreEqual(Int16.MaxValue, MyInt16_3);
        }
-       
+
+       [Test]  
        public void TestCompareTo()
        {
-               Assert(MyInt16_3.CompareTo(MyInt16_2) > 0);
-               Assert(MyInt16_2.CompareTo(MyInt16_2) == 0);
-               Assert(MyInt16_1.CompareTo((Int16)(-42)) == 0);
-               Assert(MyInt16_2.CompareTo(MyInt16_3) < 0);
+               Assert.AreEqual(65535, MyInt16_3.CompareTo(MyInt16_2), "#1");
+               Assert.AreEqual(0, MyInt16_2.CompareTo(MyInt16_2), "#2");
+               Assert.AreEqual(0, MyInt16_1.CompareTo((Int16)(-42)), "#3");
+               Assert.AreEqual(-65535, MyInt16_2.CompareTo(MyInt16_3), "#4");
                try {
                        MyInt16_2.CompareTo((object)100);
-                       Fail("Should raise a System.ArgumentException");
-               }
-               catch (Exception e) {
-                       Assert(typeof(ArgumentException) == e.GetType());
+                       Assert.Fail ("Should raise a System.ArgumentException");
+               } catch (ArgumentException e) {
                }
        }
 
+       [Test]
        public void TestEquals()
        {
-               Assert(MyInt16_1.Equals(MyInt16_1));
-               Assert(MyInt16_1.Equals((object)(Int16)(-42)));
-               Assert(MyInt16_1.Equals((object)(SByte)(-42)) == false);
-               Assert(MyInt16_1.Equals(MyInt16_2) == false);
+               Assert.IsTrue(MyInt16_1.Equals(MyInt16_1));
+               Assert.IsTrue(MyInt16_1.Equals((object)(Int16)(-42)));
+               Assert.IsTrue(MyInt16_1.Equals((object)(SByte)(-42)) == false);
+               Assert.IsTrue(MyInt16_1.Equals(MyInt16_2) == false);
        }
-       
+
+       [Test]  
        public void TestGetHashCode()
        {
                try {
@@ -93,93 +100,169 @@ public class Int16Test : Assertion
                        MyInt16_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(MyInt16_1 == Int16.Parse(MyString1));
-               Assert(MyInt16_2 == Int16.Parse(MyString2));
-               Assert(MyInt16_3 == Int16.Parse(MyString3));
+               Assert.IsTrue(MyInt16_1 == Int16.Parse(MyString1));
+               Assert.IsTrue(MyInt16_2 == Int16.Parse(MyString2));
+               Assert.IsTrue(MyInt16_3 == Int16.Parse(MyString3));
                try {
                        Int16.Parse(null);
-                       Fail("Should raise a System.ArgumentNullException");
+                       Assert.Fail ("Should raise a System.ArgumentNullException");
                }
                catch (Exception e) {
-                       Assert(typeof(ArgumentNullException) == e.GetType());
+                       Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
                }
                try {
                        Int16.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());
                }
                try {
                        int OverInt = Int16.MaxValue + 1;
                        Int16.Parse(OverInt.ToString());
-                       Fail("Should raise a System.OverflowException");
+                       Assert.Fail ("Should raise a System.OverflowException");
                }
                catch (Exception e) {
-                       Assert(typeof(OverflowException) == e.GetType());
+                       Assert.IsTrue(typeof(OverflowException) == e.GetType());
                }
                //test Parse(string s, NumberStyles style)
-               Assert(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
+               Assert.IsTrue(42 == Int16.Parse(" $42 ", NumberStyles.Currency));
                try {
                        Int16.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 == Int16.Parse(" -42 ", Nfi));
+               Assert.IsTrue(-42 == Int16.Parse(" -42 ", Nfi));
                try {
                        Int16.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 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
+               Assert.IsTrue(16 == Int16.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
                try {
                        Int16.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());
                }
+
+               Assert.AreEqual (7345, Int16.Parse ("7345\0"), "#1");
+               Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0    \0"), "#2");
+               Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0    "), "#3");
+               Assert.AreEqual (7345, Int16.Parse ("7345\0\0\0"), "#4");
+
+               Assert.AreEqual (0, Int16.Parse ("0+", NumberStyles.Any), "#5");
        }
-       
+
+       [Test]
+       public void TestParseExponent ()
+       {
+               Assert.AreEqual (2, Int16.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
+               Assert.AreEqual (20, Int16.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
+               Assert.AreEqual (200, Int16.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
+               Assert.AreEqual (200, Int16.Parse ("2E+2", NumberStyles.AllowExponent), "A#4");
+               Assert.AreEqual (2, Int16.Parse ("2", NumberStyles.AllowExponent), "A#5");
+
+               try {
+                       Int16.Parse ("2E");
+                       Assert.Fail ("B#1");
+               } catch (FormatException) {
+               }
+
+               try {
+                       Int16.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
+                       Assert.Fail ("B#2");
+               } catch (FormatException) {
+               }
+
+               try {
+                       Int16.Parse ("2E 2", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#3");
+               } catch (FormatException) {
+               }
+
+               try {
+                       Int16.Parse ("2E2 ", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#4");
+               } catch (FormatException) {
+               }
+
+               try {
+                       Int16.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
+                       Assert.Fail ("B#5");
+               } catch (OverflowException) {
+               }
+
+               try {
+                       long exponent = (long) Int32.MaxValue + 10;
+                       Int16.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
+                       Assert.Fail ("B#6");
+               } catch (OverflowException) {
+               }
+
+               try {
+                       Int16.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
+                       Assert.Fail ("B#7");
+               } catch (OverflowException) {
+               }
+               
+               try {
+                       Int16.Parse ("2 math e1", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#8");
+               } catch (FormatException) {
+               }
+       }
+
+       [Test]
+       public void Parse_MinMax ()
+       {
+               Assert.AreEqual (Int16.MinValue, Int16.Parse ("-32768"), "MinValue");
+               Assert.AreEqual (Int16.MaxValue, Int16.Parse ("32767"), "MaxValue");
+               Assert.AreEqual (-1, Int16.Parse ("FFFF", NumberStyles.HexNumber), "MaxHex");
+       }
+
+       [Test]  
        public void TestToString()
        {
                //test ToString()
-               Assert(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
-               Assert(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
-               Assert(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
+               Assert.IsTrue(String.Compare(MyString1, MyInt16_1.ToString()) == 0);
+               Assert.IsTrue(String.Compare(MyString2, MyInt16_2.ToString()) == 0);
+               Assert.IsTrue(String.Compare(MyString3, MyInt16_3.ToString()) == 0);
                //test ToString(string format)
                /*
                TODO: These tests are culture sensitive.  Need to find a way to determine the culture
                        of the system to decide the correct expected result.
                for (int i=0; i < Formats1.Length; i++) {
-                       Assert(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
-                       Assert(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
+                       Assert.IsTrue(String.Compare(Results1[i], MyInt16_2.ToString(Formats1[i])) == 0);
+                       Assert.IsTrue(String.Compare(Results2[i], MyInt16_3.ToString(Formats2[i])) == 0);
                }
                */
                //test ToString(string format, IFormatProvider provider);
                for (int i=0; i < Formats1.Length; i++) {
-                       Assert("i="+i+", ResultsNfi1[i]="+ResultsNfi1[i]+", MyInt16_2.ToString(Formats1[i]="+Formats1[i]+"): Expected "+ResultsNfi1[i]+" but got "+MyInt16_2.ToString(Formats1[i], Nfi), String.Compare(ResultsNfi1[i], MyInt16_2.ToString(Formats1[i], Nfi)) == 0);
-                       Assert("i="+i+", ResultsNfi2[i]="+ResultsNfi2[i]+", MyInt16_3.ToString(Formats2[i]="+Formats2[i]+"): Expected "+ResultsNfi2[i]+" but got "+MyInt16_3.ToString(Formats2[i], Nfi), String.Compare(ResultsNfi2[i], MyInt16_3.ToString(Formats2[i], Nfi)) == 0);
+                       Assert.IsTrue(String.Compare(ResultsNfi1[i], MyInt16_2.ToString(Formats1[i], Nfi)) == 0, "i="+i+", ResultsNfi1[i]="+ResultsNfi1[i]+", MyInt16_2.ToString(Formats1[i]="+Formats1[i]+"): Expected "+ResultsNfi1[i]+" but got "+MyInt16_2.ToString(Formats1[i], Nfi));
+                       Assert.IsTrue(String.Compare(ResultsNfi2[i], MyInt16_3.ToString(Formats2[i], Nfi)) == 0, "i="+i+", ResultsNfi2[i]="+ResultsNfi2[i]+", MyInt16_3.ToString(Formats2[i]="+Formats2[i]+"): Expected "+ResultsNfi2[i]+" but got "+MyInt16_3.ToString(Formats2[i], Nfi));
                }
                try {
                        MyInt16_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());
                }
        }
 
@@ -189,14 +272,20 @@ public class Int16Test : Assertion
                Int16 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)");
+       }
+
+       [Test]
+       public void Bug3677 ()
+       {
+               Assert.AreEqual (-7197, short.Parse("E3E3", NumberStyles.HexNumber), "HexNumber");
        }
 }