Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / corlib / Test / System / DecimalTest.cs
index bdddd95cb06b7bbdccb672950f30ce37f1d42358..8419857df40583b95c61f7bc52508ac89980c344 100644 (file)
@@ -77,7 +77,7 @@ namespace MonoTests.System
                private int [] partsMaxValue = { -1, -1, -1, 0 };
                private int [] partsMinValue = { -1, -1, -1, negativeBitValue };
                private int [] parts6 = { 1234, 5678, 8888, negativeScale4Value };
-               private NumberFormatInfo NfiUser;
+               private NumberFormatInfo NfiUser, NfiBroken;
 
                private CultureInfo old_culture;
 
@@ -108,6 +108,12 @@ namespace MonoTests.System
                        NfiUser.PercentNegativePattern = 2;
                        NfiUser.PercentPositivePattern = 2;
                        NfiUser.PercentSymbol = "%%%";
+
+                       NfiBroken = new NumberFormatInfo ();
+                       NfiBroken.NumberDecimalSeparator = ".";
+                       NfiBroken.NumberGroupSeparator = ".";
+                       NfiBroken.CurrencyDecimalSeparator = ".";
+                       NfiBroken.CurrencyGroupSeparator = ".";
                }
 
                [TestFixtureTearDown]
@@ -275,6 +281,12 @@ namespace MonoTests.System
                        }
                }
 
+               [Test]
+               public void TestBrokenNFI ()
+               {
+                       Assert.AreEqual (5.3m, decimal.Parse ("5.3", NumberStyles.Number, NfiBroken), "Parsing with broken NFI");
+               }
+               
                [Test]
                [Category ("TargetJvmNotWorking")]
                public void TestPercentPattern ()
@@ -309,6 +321,8 @@ namespace MonoTests.System
 
                ParseTest [] tab = {
                                new ParseTest("1.2345", 1.2345m),
+                               new ParseTest("1.2345\0", 1.2345m),
+                               new ParseTest("1.2345\0\0\0\0", 1.2345m),
                                new ParseTest("-9876543210", -9876543210m),
                                new ParseTest(NumberFormatInfo.InvariantInfo.CurrencySymbol 
                                        + " (  79,228,162,514,264,337,593,543,950,335.000 ) ", 
@@ -321,7 +335,10 @@ namespace MonoTests.System
                                new ParseTest("-000000000000001922816251426433759354395033.300000000000000", -1922816251426433759354395033.3m),
                                new ParseTest("-7922816251426433759354395033.150000000000", -7922816251426433759354395033.2m),
                                new ParseTest("-7922816251426433759354395033.2400000000000", -7922816251426433759354395033.2m),
-                               new ParseTest("-7922816251426433759354395033.2600000000000", -7922816251426433759354395033.3m)
+                               new ParseTest("-7922816251426433759354395033.2600000000000", -7922816251426433759354395033.3m),
+                               new ParseTest("987654321098765432109876543.25999", 987654321098765432109876543.3m, NumberStyles.Float),
+                               new ParseTest("987654321098765432109876543.25199", 987654321098765432109876543.3m, NumberStyles.Float),
+                               new ParseTest("2.22222222222222222222222222225", 2.2222222222222222222222222222m, NumberStyles.Float)
                };
 
                [Test]
@@ -365,6 +382,13 @@ namespace MonoTests.System
                        } catch (OverflowException) {
                                //ok
                        }
+
+                       try {
+                               d = Decimal.Parse ("5\05");
+                               Assert.Fail ("Expected FormatException" + d);
+                       } catch (FormatException) {
+                               //ok
+                       }
                }
 
                [Test]
@@ -608,6 +632,7 @@ namespace MonoTests.System
                }
 
                [Test]
+               [SetCulture("en-US")]
                public void TestConstructDouble ()
                {
                        Decimal d;
@@ -656,6 +681,29 @@ namespace MonoTests.System
                        d = new Decimal (1.2342278901234e-25);
                        Assert.AreEqual (d, 1.234e-25m, "A10");
 
+                       //
+                       // Make sure that 0.6 is turned into
+                       // the 96 bit value 6 with the magnitude set to
+                       //
+                       double mydouble = 0.6;
+                       d = new Decimal (mydouble);
+                       int [] bits = Decimal.GetBits (d);
+                       Assert.AreEqual (bits [0], 6, "f1");
+                       Assert.AreEqual (bits [1], 0, "f2");
+                       Assert.AreEqual (bits [2], 0, "f3");
+                       Assert.AreEqual (bits [3], 65536, "f4");
+
+                       //
+                       // Make sure that we properly parse this value
+                       // this in particular exposes a bug in the
+                       // unmanaged version which rounds to 1234 instead
+                       // of 1235, here to make sure we do not regress
+                       // on the future.
+                       //
+                       mydouble = 1.2345679329684657e-25;
+                       d = new Decimal (mydouble);
+                       Assert.AreEqual (d.ToString (), "0.0000000000000000000000001235", "f5");
+                       
                        // test exceptions
                        try {
                                d = new Decimal (8e28);
@@ -1023,6 +1071,7 @@ namespace MonoTests.System
                }
 
                [Test]
+               [SetCulture("en-US")]
                public void ToString_Defaults ()
                {
                        Decimal d = 254.9m;
@@ -1066,8 +1115,30 @@ namespace MonoTests.System
                        decimal d = Decimal.Parse ("9223372036854775808.0000000009", CultureInfo.InvariantCulture);
                        long l = (long) d;
                }
+/* Not yet fixed
+               [Test]
+               public void ParseEmptyNumberGroupSeparator ()
+               {
+                       CultureInfo originalCulture = CultureInfo.CurrentCulture;
+                       Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
+                       try {
+                               var nf = new NumberFormatInfo ();
+                               nf.NumberDecimalSeparator = ".";
+                               nf.NumberGroupSeparator = "";
+                               decimal d = decimal.Parse ("4.5", nf);
+                               Assert.AreEqual (4.5, d);
+                       } finally {
+                               Thread.CurrentThread.CurrentCulture = originalCulture;
+                       }
+               }
+*/
+
+               [Test]
+               public void ParseCultureSeparator ()
+               {
+                       Assert.AreEqual (2.2m, decimal.Parse ("2.2", new CultureInfo("es-MX")));
+               }
 
-#if NET_2_0
                [Test]
                [Category ("TargetJvmNotWorking")]
                public void TryParse ()
@@ -1086,7 +1157,6 @@ namespace MonoTests.System
                                        NumberFormatInfo.InvariantInfo, out r));
                        }
                }
-#endif
 
                [Test]
                [ExpectedException (typeof (DivideByZeroException))]
@@ -1112,17 +1182,11 @@ namespace MonoTests.System
                        Assert.AreEqual (-12.1m, Decimal.Remainder (n2, p1), "-12.1 % 254.9");
                        Assert.AreEqual (12.1m, Decimal.Remainder (p2, n1), "12.1 % -254.9");
                        Assert.AreEqual (-12.1m, Decimal.Remainder (n2, n1), "-12.1 % -254.9");
-#if NET_2_0
+
                        Assert.AreEqual (0.0m, Decimal.Remainder (p1, p1), "12.1 % 12.1");
                        Assert.AreEqual (0.0m, Decimal.Remainder (n1, p1), "-12.1 % 12.1");
                        Assert.AreEqual (0.0m, Decimal.Remainder (p1, n1), "12.1 % -12.1");
                        Assert.AreEqual (0.0m, Decimal.Remainder (n1, n1), "-12.1 % -12.1");
-#else
-                       Assert.AreEqual (0, Decimal.Remainder (p1, p1), "12.1 % 12.1");
-                       Assert.AreEqual (0, Decimal.Remainder (n1, p1), "-12.1 % 12.1");
-                       Assert.AreEqual (0, Decimal.Remainder (p1, n1), "12.1 % -12.1");
-                       Assert.AreEqual (0, Decimal.Remainder (n1, n1), "-12.1 % -12.1");
-#endif
                }
 
                [Test]
@@ -1323,6 +1387,7 @@ namespace MonoTests.System
                }
 
                [Test] // bug #59425
+               [SetCulture("en-US")]
                public void ParseAndKeepPrecision ()
                {
                        string value = "5";
@@ -1343,6 +1408,7 @@ namespace MonoTests.System
                }
 
                [Test]
+               [SetCulture("en-US")]
                public void ToString_G ()
                {
                        Assert.AreEqual ("1.0", (1.0m).ToString (), "00");
@@ -1376,7 +1442,6 @@ namespace MonoTests.System
                        Assert.AreEqual ("0.0000000000000000000000000001", (0.0000000000000000000000000001m).ToString (), "28");
                }
 
-#if NET_2_0
                [Test]
                public void MidpointRoundingAwayFromZero ()
                {
@@ -1399,6 +1464,102 @@ namespace MonoTests.System
                        Assert.AreEqual (-2.1M, Math.Round (-2.08M, 1, m), "#15");
                        Assert.AreEqual (-3.1M, Math.Round (-3.05M, 1, m), "#16");
                }
-#endif
+
+               [Test] // bug #4814
+               [SetCulture("")]
+               public void Parse_NumberGroupSeparatorIsEmpty_DoNotThrowIndexOutOfRangeException ()
+               {
+                       NumberFormatInfo nf = new NumberFormatInfo ();
+                       nf.NumberGroupSeparator = "";
+                       Decimal.Parse ("1.5", nf);
+               }
+
+               [Test] // bug #4814
+               [SetCulture("")]
+               public void Parse_CurrencyGroupSeparatorIsEmpty_DoNotThrowIndexOutOfRangeException ()
+               {
+                       NumberFormatInfo nf = new NumberFormatInfo ();
+                       nf.CurrencyGroupSeparator = "";
+                       Decimal.Parse ("\u00A41.5", NumberStyles.Currency, nf);
+               }
+
+               [Test] // bug #4814
+               [SetCulture("")]
+               public void Parse_LeadingSign_PositiveSignIsEmpty_DoNotThrowIndexOutOfRangeException ()
+               {
+                       NumberFormatInfo nf = new NumberFormatInfo ();
+                       nf.PositiveSign = "";
+                       try {
+                               Decimal.Parse ("+15", nf);
+                       } catch (FormatException) {
+                               return;
+                       }
+
+                       Assert.Fail ("Expected FormatException");
+               }
+
+               [Test] // bug #4814
+               [SetCulture("")]
+               public void Parse_LeadingSign_NegativeSignIsEmpty_DoNotThrowIndexOutOfRangeException ()
+               {
+                       NumberFormatInfo nf = new NumberFormatInfo ();
+                       nf.NegativeSign = "";
+                       try {
+                               Decimal.Parse ("-15", nf);
+                       } catch (FormatException) {
+                               return;
+                       }
+
+                       Assert.Fail ("Expected FormatException");
+               }
+
+               [Test] // bug #4814
+               [SetCulture("")]
+               public void Parse_TrailingSign_PositiveSignIsEmpty_DoNotThrowIndexOutOfRangeException ()
+               {
+                       NumberFormatInfo nf = new NumberFormatInfo ();
+                       nf.PositiveSign = "";
+                       try {
+                               Decimal.Parse ("15+", nf);
+                       } catch (FormatException) {
+                               return;
+                       }
+
+                       Assert.Fail ("Expected FormatException");
+               }
+
+               [Test] // bug #4814
+               [SetCulture("")]
+               public void Parse_TrailingSign_NegativeSignIsEmpty_DoNotThrowIndexOutOfRangeException ()
+               {
+                       NumberFormatInfo nf = new NumberFormatInfo ();
+                       nf.NegativeSign = "";
+                       try {
+                               Decimal.Parse ("15-", nf);
+                       } catch (FormatException) {
+                               return;
+                       }
+
+                       Assert.Fail ("Expected FormatException");
+               }
+
+               [Test]
+               [SetCulture("en-US")]
+               public void ParseZeros ()
+               {
+                       var d = Decimal.Parse ("0.000");
+                       var bits = Decimal.GetBits (d);
+                       Assert.AreEqual (0, bits[0], "#1");
+                       Assert.AreEqual (0, bits[1], "#2");
+                       Assert.AreEqual (0, bits[2], "#3");
+                       Assert.AreEqual (196608, bits[3], "#4");
+                       Assert.AreEqual ("0.000", d.ToString (), "#5");
+
+                       d = Decimal.Parse("0.000000000000000000000000000000000000000000000000000000000000000000");
+                       Assert.AreEqual ("0.0000000000000000000000000000", d.ToString (), "#10");
+
+                       d = Decimal.Parse ("0.");
+                       Assert.AreEqual ("0", d.ToString (), "#11");
+               }
        }
 }