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;
NfiUser.PercentNegativePattern = 2;
NfiUser.PercentPositivePattern = 2;
NfiUser.PercentSymbol = "%%%";
+
+ NfiBroken = new NumberFormatInfo ();
+ NfiBroken.NumberDecimalSeparator = ".";
+ NfiBroken.NumberGroupSeparator = ".";
+ NfiBroken.CurrencyDecimalSeparator = ".";
+ NfiBroken.CurrencyGroupSeparator = ".";
}
[TestFixtureTearDown]
}
}
+ [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 ()
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 ) ",
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]
} catch (OverflowException) {
//ok
}
+
+ try {
+ d = Decimal.Parse ("5\05");
+ Assert.Fail ("Expected FormatException" + d);
+ } catch (FormatException) {
+ //ok
+ }
}
[Test]
}
[Test]
+ [SetCulture("en-US")]
public void TestConstructDouble ()
{
Decimal d;
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);
}
[Test]
+ [SetCulture("en-US")]
public void ToString_Defaults ()
{
Decimal d = 254.9m;
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 ()
NumberFormatInfo.InvariantInfo, out r));
}
}
-#endif
[Test]
[ExpectedException (typeof (DivideByZeroException))]
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]
+ public void Remainder2 ()
+ {
+ decimal a = 20.0M;
+ decimal b = 10.0M;
+ decimal c = 10.00M;
+
+ Assert.AreEqual (0.00m, a % c, "20.0M % 10.00M");
+
}
[Test]
}
[Test] // bug #59425
+ [SetCulture("en-US")]
public void ParseAndKeepPrecision ()
{
string value = "5";
}
[Test]
+ [SetCulture("en-US")]
public void ToString_G ()
{
Assert.AreEqual ("1.0", (1.0m).ToString (), "00");
Assert.AreEqual ("0.0000000000000000000000000001", (0.0000000000000000000000000001m).ToString (), "28");
}
-#if NET_2_0
[Test]
public void MidpointRoundingAwayFromZero ()
{
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");
+ }
}
}