Merge pull request #3522 from henricm/fix-csharp-compiler-path-windows
[mono.git] / mcs / class / corlib / Test / System / DoubleTest.cs
index 33a6a1eecd4739394e67b3b54cc6d225f2e89469..81b2a5fdb2a45300da291b23c2e54d7a5714d113 100644 (file)
@@ -31,7 +31,8 @@ namespace MonoTests.System
                private string [] string_values;
                private string [] string_values_fail = {
                        "",     // empty
-                       "- 1.0" // Inner whitespace
+                       "- 1.0", // Inner whitespace
+                       "3 5" // Inner whitespace 2
                };
 
                private double [] double_values = {
@@ -45,7 +46,7 @@ namespace MonoTests.System
                };
 
                [SetUp]
-               public void GetReady ()
+               public void Setup ()
                {
                        string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
                        string_values = new string [15];
@@ -146,6 +147,7 @@ namespace MonoTests.System
                }
 
                [Test]
+               [SetCulture ("en-US")]
                public void Parse ()
                {
                        int i = 0;
@@ -166,12 +168,12 @@ namespace MonoTests.System
                        try {
                                Assert.AreEqual (1234.5678, Double.Parse ("1,234.5678", NumberStyles.Float | NumberStyles.AllowThousands, Nfi), "#C1");
                        } catch (Exception e) {
-                               Assertion.Fail ("#C2: Parse Failed NumberStyles.AllowThousands with e = " + e.ToString ());
+                               Assert.Fail ("#C2: Parse Failed NumberStyles.AllowThousands with e = " + e.ToString ());
                        }
 
                        try {
                                Double.Parse (null);
-                               Assertion.Fail ("#D1");
+                               Assert.Fail ("#D1");
                        } catch (ArgumentNullException ex) {
                                Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
                                Assert.IsNull (ex.InnerException, "#D3");
@@ -201,7 +203,7 @@ namespace MonoTests.System
 
                        try {
                                Double.Parse ("-1" + sep + "79769313486232e308");
-                               Assertion.Fail ("#G1");
+                               Assert.Fail ("#G1");
                        } catch (OverflowException ex) {
                                Assert.AreEqual (typeof (OverflowException), ex.GetType (), "#G2");
                                Assert.IsNull (ex.InnerException, "#G3");
@@ -211,26 +213,27 @@ namespace MonoTests.System
                        for (i = 0; i < string_values_fail.Length; ++i) {
                                try {
                                        Double.Parse (string_values_fail [i]);
-                                       Assertion.Fail ("#H1: " + string_values_fail [i]);
+                                       Assert.Fail ("#H1: " + string_values_fail [i]);
                                } catch (FormatException ex) {
                                        Assert.AreEqual (typeof (FormatException), ex.GetType (), "#H2");
                                        Assert.IsNull (ex.InnerException, "#H3");
                                        Assert.IsNotNull (ex.Message, "#H4");
                                }
                        }
+               }
 
-                       try {
-                               Double.Parse (" ");
-                               Assert.Fail ("#I1");
-                       } catch (FormatException ex) {
-                               Assert.AreEqual (typeof (FormatException), ex.GetType (), "#I2");
-                               Assert.IsNull (ex.InnerException, "#I3");
-                               Assert.IsNotNull (ex.Message, "#I4");
-                       }
+               [Test]
+               public void ParseAllowWhitespaces ()
+               {
+                       var nf = CultureInfo.CurrentCulture.NumberFormat;
+                       NumberStyles style = NumberStyles.Float;
+                       double.Parse (" 32 ");
+                       double.Parse (string.Format ("  {0}  ", nf.PositiveInfinitySymbol));
+                       double.Parse (string.Format ("  {0}  ", nf.NegativeInfinitySymbol));
+                       double.Parse (string.Format ("  {0}  ", nf.NaNSymbol));
                }
 
                [Test] // bug #81630
-               [Category ("NotWorking")]
                public void Parse_Whitespace ()
                {
                        try {
@@ -243,6 +246,28 @@ namespace MonoTests.System
                        }
                }
 
+               [Test] // //bug #81777
+               public void Parse_TrailingGarbage ()
+               {
+                       try {
+                               double.Parse ("22 foo");
+                               Assert.Fail ("#1");
+                       } catch (FormatException ex) {
+                               Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+               }
+
+               [Test]
+               public void Parse_Infinity ()
+               {
+                       double value;
+                       IFormatProvider german = new CultureInfo ("de-DE");
+                       var res = double.Parse ("+unendlich", NumberStyles.Float, german);
+                       Assert.AreEqual (double.PositiveInfinity, res);
+               }
+
                [Test]
                public void TestToString ()
                {
@@ -373,6 +398,7 @@ namespace MonoTests.System
                        list.Add (new Element (0.086d, "00000", "00000"));
                        list.Add (new Element (0.086d, "(###) ### - ####", "()  - "));
                        list.Add (new Element (0.086d, "#.##", ".09"));
+                       list.Add (new Element (0.086d, "#.#", ".1"));
                        list.Add (new Element (0.086d, "0.00", "0.09"));
                        list.Add (new Element (0.086d, "00.00", "00.09"));
                        list.Add (new Element (0.086d, "#,#", ""));
@@ -516,6 +542,12 @@ namespace MonoTests.System
                        }
                }
 
+               [Test]
+               public void TestRoundtrip () // bug #320433
+               {
+                       Assert.AreEqual ("10.78", 10.78.ToString ("R", NumberFormatInfo.InvariantInfo));
+               }
+
                [Test] // bug #72955
                public void LongLongValueRoundtrip ()
                {
@@ -531,13 +563,7 @@ namespace MonoTests.System
                }
 
                [Test]
-#if NET_2_0
                [ExpectedException (typeof (ArgumentException))]
-#else
-               [Category ("NotWorking")]
-               // MS accept hex values under 1.x but the results neither match the long value
-               // nor the value of a 64bits double
-#endif
                public void HexNumber_WithHexToParse ()
                {
                        // from bug #72221
@@ -547,13 +573,7 @@ namespace MonoTests.System
                }
 
                [Test]
-#if NET_2_0
                [ExpectedException (typeof (ArgumentException))]
-#else
-               [Category ("NotWorking")]
-               // MS accept hex values under 1.x but the results neither match the long value
-               // nor the value of a 64bits double
-#endif
                public void HexNumber_NoHexToParse ()
                {
                        double d;
@@ -569,6 +589,18 @@ namespace MonoTests.System
                                null, out value));
                }
 
+               [Test]
+               public void TryParse_NonDigitStrings ()
+               {
+                       double value;
+                       Assert.IsFalse (Double.TryParse ("string", NumberStyles.Any, null, out value), "#1");
+                       Assert.IsFalse (Double.TryParse ("with whitespace", NumberStyles.Any, null, out value), "#2");
+                       
+                       Assert.IsFalse (Double.TryParse ("string", out value), "#3");
+                       Assert.IsFalse (Double.TryParse ("with whitespace", out value), "#4");
+               }
+
+                                       
                [Test] // bug #77721
                public void ParseCurrency ()
                {
@@ -579,10 +611,35 @@ namespace MonoTests.System
                                double d = double.Parse ("$4.56", NumberStyles.Currency, f);
                                Assert.AreEqual (4.56, d);
                        } finally {
-                               // restore original culture
                                Thread.CurrentThread.CurrentCulture = originalCulture;
                        }
+               }
+
+               [Test]
+               public void ParseEmptyNumberGroupSeparator ()
+               {
+                       CultureInfo originalCulture = CultureInfo.CurrentCulture;
+                       Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
+                       try {
+                               var nf = new NumberFormatInfo ();
+                               nf.NumberDecimalSeparator = ".";
+                               nf.NumberGroupSeparator = "";
+                               double d = double.Parse ("4.5", nf);
+                               Assert.AreEqual (4.5, d);
+                       } finally {
+                               Thread.CurrentThread.CurrentCulture = originalCulture;
+                       }
+               }
 
+               [Test]
+               public void ParseAdvanced ()
+               {
+                       Assert.AreEqual (-456, double.Parse("(456 ) ", NumberStyles.AllowParentheses | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture), "#1");
+                       Assert.AreEqual (-456, double.Parse("(456)", NumberStyles.AllowParentheses, CultureInfo.InvariantCulture), "#2");
+                       Assert.AreEqual (-3, double.Parse("3-", NumberStyles.AllowTrailingSign, CultureInfo.InvariantCulture), "#3");
+                       Assert.AreEqual (-3.5, double.Parse("3.5-", NumberStyles.AllowTrailingSign | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture), "#4");
+                       Assert.AreEqual (-0.5, double.Parse ("0.5 - ", NumberStyles.AllowTrailingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture), "#5");
+                       Assert.AreEqual (-900000, double.Parse ("-9E5 " + CultureInfo.InvariantCulture.NumberFormat.CurrencySymbol + "   ", NumberStyles.Any, CultureInfo.InvariantCulture), "#6");
                }
        }
 }