[480178] Fix handling of surrogate characters.
[mono.git] / mcs / class / corlib / Test / System / UInt32Test.cs
index 35e59d2b6aa2e00b882066ee12dd6919874d3d94..46ea2675c625128d57ff6a4bc86b5a0a237da9cf 100644 (file)
@@ -8,11 +8,13 @@
 using NUnit.Framework;
 using System;
 using System.Globalization;
+using System.Threading;
 
 namespace MonoTests.System
 {
 
-public class UInt32Test : TestCase
+[TestFixture]
+public class UInt32Test 
 {
        private const UInt32 MyUInt32_1 = 42;
        private const UInt32 MyUInt32_2 = 0;
@@ -22,71 +24,97 @@ public class UInt32Test : TestCase
        private const string MyString3 = "4294967295";
        private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
        private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
-       private string[] Results1 = {"$0.00", "0", "0.000000e+000", "0.00",
-                                         "0", "0.00", "0.00 %", "0"};
-       private string[] Results2 = {"$4,294,967,295.00000", "4294967295", "4.29497e+009", "4294967295.00000",
-                                         "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
+       private string[] Results1 = {"",
+                                       "0", "0.000000e+000", "0.00",
+                                       "0", "0.00", "0.00 %", "0"};
+       private string[] ResultsNfi1 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
+                                       "0", "0.000000e+000", "0.00",
+                                       "0", "0.00", "0.00 %", "0"};
+       private string[] Results2 = {"",
+                                       "4294967295", "4.29497e+009", "4294967295.00000",
+                                       "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
+       private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"4,294,967,295.00000",
+                                       "4294967295", "4.29497e+009", "4294967295.00000",
+                                       "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
        private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
        
-       public UInt32Test(string name) : base(name) {}
+       private CultureInfo old_culture;
 
-       protected override void SetUp() 
+       [TestFixtureSetUp]
+       public void SetUp () 
        {
+               old_culture = Thread.CurrentThread.CurrentCulture;
+
+               // Set culture to en-US and don't let the user override.
+               Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
+
+               // We can't initialize this until we set the culture.
+               
+               string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
+               string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
+               
+               Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "0.00";
+               Results1 [3] = "0." + decimals;
+               Results1 [5] = "0." + decimals;
+               Results1 [6] = perPattern.Replace ("n","0.00");
+               
+               Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "4,294,967,295.00000";
+               Results2 [6] = perPattern.Replace ("n","429,496,729,500.00000");
        }
 
-       public static ITest Suite {
-               get { 
-                       return new TestSuite(typeof(UInt32Test)); 
-               }
+       [TestFixtureTearDown]
+       public void TearDown ()
+       {
+               Thread.CurrentThread.CurrentCulture = old_culture;
        }
-    
+
        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);
        }
        
        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((Int16)100);
-                       Fail("Should raise a System.ArgumentException");
+                       MyUInt32_2.CompareTo((object)(Int16)100);
+                       Assert.Fail("Should raise a System.ArgumentException");
                }
                catch (Exception e) {
-                       Assert(typeof(ArgumentException) == e.GetType());
+                       Assert.IsTrue(typeof(ArgumentException) == e.GetType());
                }
        }
 
        public void TestEquals()
        {
-               Assert(MyUInt32_1.Equals(MyUInt32_1));
-               Assert(MyUInt32_1.Equals((UInt32)(42)));
-               Assert(MyUInt32_1.Equals((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);
        }
        
        public void TestGetHashCode()
@@ -97,91 +125,134 @@ public class UInt32Test : TestCase
                        MyUInt32_3.GetHashCode();
                }
                catch {
-                       Fail("GetHashCode should not raise an exception here");
+                       Assert.Fail("GetHashCode should not raise an exception here");
                }
        }
        
        public void TestParse()
        {
                //test Parse(string s)
-               Assert(MyUInt32_1 == UInt32.Parse(MyString1));
-               Assert(MyUInt32_2 == UInt32.Parse(MyString2));
-               Assert(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(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(typeof(FormatException) == e.GetType());
+                       Assert.IsTrue(typeof(FormatException) == e.GetType(), "Did not get FormatException type");
                }
                try {
-                       double OverInt = (double)UInt32.MaxValue + 1;
-                       UInt32.Parse(OverInt.ToString());
-                       Fail("Should raise a System.OverflowException");
+                       // TODO: Use this after ToString() is completed. For now, hard code string that generates
+                       // exception.
+                       //double OverInt = (double)UInt32.MaxValue + 1;
+                       //UInt32.Parse(OverInt.ToString());
+                       UInt32.Parse("4294967296");
+                       Assert.Fail("Should raise a System.OverflowException");
                }
                catch (Exception e) {
-                       Assert(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(" $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 ());
        }
        
        public void TestToString()
        {
-               //test ToString()
-               Assert(String.Compare(MyString1, MyUInt32_1.ToString()) == 0);
-               Assert(String.Compare(MyString2, MyUInt32_2.ToString()) == 0);
-               Assert(String.Compare(MyString3, MyUInt32_3.ToString()) == 0);
+               int TestNumber = 1;
+               try {
+                       //test ToString()
+                       Assert.AreEqual(MyString1, MyUInt32_1.ToString());
+                       TestNumber++;
+                       Assert.AreEqual(MyString2, MyUInt32_2.ToString());
+                       TestNumber++;
+                       Assert.AreEqual(MyString3, MyUInt32_3.ToString());
+               } catch (Exception e) {
+                       Assert.Fail("TestToString: Assert.Failed on TestNumber=" + TestNumber 
+                               + " with exception: " + e.ToString());
+               }
+
                //test ToString(string format)
                for (int i=0; i < Formats1.Length; i++) {
-                       Assert(String.Compare(Results1[i], MyUInt32_2.ToString(Formats1[i])) == 0);
-                       Assert(String.Compare(Results2[i], MyUInt32_3.ToString(Formats2[i])) == 0);
+                       try {
+                               Assert.AreEqual(Results1[i], MyUInt32_2.ToString(Formats1[i]));
+                       } catch (Exception e) {
+                               Assert.Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i 
+                                       + ". e = " + e.ToString());
+                       }
+
+                       try {
+                               Assert.AreEqual(Results2[i], MyUInt32_3.ToString(Formats2[i]));
+                       } catch (Exception e) {
+                               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++) {
-                       Assert(String.Compare(Results1[i], MyUInt32_2.ToString(Formats1[i], Nfi)) == 0);
-                       Assert(String.Compare(Results2[i], MyUInt32_3.ToString(Formats2[i], Nfi)) == 0);
+                       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());
                }
        }
+
+       [Test]
+       public void ToString_Defaults () 
+       {
+               UInt32 i = 254;
+               // everything defaults to "G"
+               string def = i.ToString ("G");
+               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)");
+       }
 }