Merge pull request #601 from knocte/sock_improvements
[mono.git] / mcs / class / corlib / Test / System / Int32Test.cs
index 69c4ff348f850d829a0fb0c46277d306ff5f8c99..128cc19790df3809993d52f43a8308e197ea9124 100644 (file)
@@ -37,11 +37,12 @@ public class Int32Test
                                        "2147483647", "2.14748e+009", "2147483647.00000",
                                        "2.1475e+09", "2,147,483,647.00000", "214,748,364,700.00000 %", "7fffffff"};
        private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
+       private NumberFormatInfo NfiUser;
        
        private CultureInfo old_culture;
 
        [TestFixtureSetUp]
-       public void SetUp() 
+       public void SetUpFixture() 
        {
                old_culture = Thread.CurrentThread.CurrentCulture;
 
@@ -60,6 +61,28 @@ public class Int32Test
                
                Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"2,147,483,647.00000";
                Results2 [6] = perPattern.Replace ("n","214,748,364,700.00000");
+
+               NfiUser = new NumberFormatInfo ();
+               NfiUser.CurrencyDecimalDigits = 3;
+               NfiUser.CurrencyDecimalSeparator = ":";
+               NfiUser.CurrencyGroupSeparator = "/";
+               NfiUser.CurrencyGroupSizes = new int[] { 2, 1, 0 };
+               NfiUser.CurrencyNegativePattern = 10;  // n $-
+               NfiUser.CurrencyPositivePattern = 3;  // n $
+               NfiUser.CurrencySymbol = "XYZ";
+               NfiUser.PercentDecimalDigits = 1;
+               NfiUser.PercentDecimalSeparator = ";";
+               NfiUser.PercentGroupSeparator = "~";
+               NfiUser.PercentGroupSizes = new int[] { 1 };
+               NfiUser.PercentNegativePattern = 2;
+               NfiUser.PercentPositivePattern = 2;
+               NfiUser.PercentSymbol = "%%%";
+       }
+       
+       [SetUp]
+       public void Setup ()
+       {
+               Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
        }
 
        [TestFixtureTearDown]
@@ -234,13 +257,84 @@ public class Int32Test
                
                Int32.Parse ("123", new DateTimeFormatInfo ());
 
-               Assert.AreEqual (734561, Int64.Parse ("734561\0"), "C#43");
-               Assert.AreEqual (734561, Int64.Parse ("734561\0\0\0    \0"), "C#44");
-               Assert.AreEqual (734561, Int64.Parse ("734561\0\0\0    "), "C#45");
-               Assert.AreEqual (734561, Int64.Parse ("734561\0\0\0"), "C#46");
+               Assert.AreEqual (734561, Int32.Parse ("734561\0"), "C#43");
+               Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0    \0"), "C#44");
+               Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0    "), "C#45");
+               Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0"), "C#46");
+
+               Assert.AreEqual (0, Int32.Parse ("0+", NumberStyles.Any), "#50");
+       }
+
+    [Test]
+       public void TestParseExponent ()
+       {
+               Assert.AreEqual (2, Int32.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
+               Assert.AreEqual (20, Int32.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
+               Assert.AreEqual (200, Int32.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
+               Assert.AreEqual (2000000, Int32.Parse ("2E6", NumberStyles.AllowExponent), "A#4");
+               Assert.AreEqual (200, Int32.Parse ("2E+2", NumberStyles.AllowExponent), "A#5");
+               Assert.AreEqual (2, Int32.Parse ("2", NumberStyles.AllowExponent), "A#6");
+               Assert.AreEqual (21, Int32.Parse ("2.1E1", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#7");
+               Assert.AreEqual (520, Int32.Parse (".52E3", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#8");
+               Assert.AreEqual (32500000, Int32.Parse ("32.5E6", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#9");
+               Assert.AreEqual (890, Int32.Parse ("8.9000E2", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#10");
+
+               try {
+                       Int32.Parse ("2E");
+                       Assert.Fail ("B#1");
+               } catch (FormatException) {
+               }
+
+               try {
+                       Int32.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
+                       Assert.Fail ("B#2");
+               } catch (FormatException) {
+               }
+
+               try {
+                       Int32.Parse ("2E 2", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#3");
+               } catch (FormatException) {
+               }
+
+               try {
+                       Int32.Parse ("2E2 ", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#4");
+               } catch (FormatException) {
+               }
+
+               try {
+                       Int32.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
+                       Assert.Fail ("B#5");
+               } catch (OverflowException) {
+               }
+
+               try {
+                       long exponent = (long)Int32.MaxValue + 10;
+                       Int32.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
+                       Assert.Fail ("B#6");
+               } catch (OverflowException) {
+               }
+
+               try {
+                       Int32.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
+                       Assert.Fail ("B#7");
+               } catch (OverflowException) {
+               }
+
+               try {
+                       Int32.Parse ("2 math e1", NumberStyles.AllowExponent);
+                       Assert.Fail ("B#8");
+               } catch (FormatException) {
+               }
+
+               try {
+                       Int32.Parse ("2.09E1",  NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);
+                       Assert.Fail ("B#9");
+               } catch (OverflowException) {
+               }               
        }
 
-#if NET_2_0    
        [Test]
        public void TestTryParse()
        {
@@ -280,6 +374,7 @@ public class Int32Test
 
                double OverInt = (double)Int32.MaxValue + 1;
                Assert.AreEqual (false, Int32.TryParse (OverInt.ToString (), out result));
+               Assert.AreEqual (false, Int32.TryParse (OverInt.ToString (), NumberStyles.None, CultureInfo.InvariantCulture, out result));
 
                Assert.AreEqual (false, Int32.TryParse ("$42", NumberStyles.Integer, null, out result));
                Assert.AreEqual (false, Int32.TryParse ("%42", NumberStyles.Integer, Nfi, out result));
@@ -297,7 +392,6 @@ public class Int32Test
                Assert.AreEqual (-1, result);
                Assert.AreEqual (false, Int32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
        }
-#endif
 
        [Test]  
        public void TestToString()
@@ -371,6 +465,50 @@ public class Int32Test
 
                Assert.AreEqual ("254", def, "ToString(G)");
        }
+
+       [Test]
+       public void ParseRespectCurrentCulture ()
+       {
+               var old = Thread.CurrentThread.CurrentCulture;
+               var cur = (CultureInfo)old.Clone ();
+
+               NumberFormatInfo ninfo = new NumberFormatInfo ();
+               ninfo.NegativeSign = ">";
+               ninfo.PositiveSign = "%";
+               cur.NumberFormat = ninfo;
+
+               Thread.CurrentThread.CurrentCulture = cur;
+
+               int val = 0;
+
+               try {
+                       Assert.IsTrue (int.TryParse (">11", out val), "#1");
+                       Assert.AreEqual (-11, val, "#2");
+                       Assert.IsTrue (int.TryParse ("%11", out val), "#3");
+                       Assert.AreEqual (11, val, "#4");
+               } finally {
+                       Thread.CurrentThread.CurrentCulture = old;
+               }
+       }
+
+       [Test]
+       public void TestUserCurrency ()
+       {
+               const int val1 = -1234567;
+               const int val2 = 1234567;
+
+               string s = "";
+               int v;
+               s = val1.ToString ("c", NfiUser);
+               Assert.AreEqual ("1234/5/67:000 XYZ-", s, "Currency value type 1 is not what we want to try to parse");
+               v = Int32.Parse ("1234/5/67:000   XYZ-", NumberStyles.Currency, NfiUser);
+               Assert.AreEqual (val1, v);
+
+               s = val2.ToString ("c", NfiUser);
+               Assert.AreEqual ("1234/5/67:000 XYZ", s, "Currency value type 2 is not what we want to try to parse");
+               v = Int32.Parse (s, NumberStyles.Currency, NfiUser);
+               Assert.AreEqual (val2, v);
+       }
 }
 
 }