"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;
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]
Assert.IsTrue (typeof (ArgumentException) == e.GetType (), "C#42");
}
+ try {
+ Int32.Parse ("5", NumberStyles.Any, CultureInfo.InvariantCulture);
+ Assert.Fail ("C#42");
+ } catch (FormatException) {
+ }
+
+ try {
+ Int32.Parse ("\xFF15\xFF15", NumberStyles.Any, CultureInfo.InvariantCulture);
+ Assert.Fail ("C#43");
+ } catch (FormatException) {
+ }
+
// Pass a DateTimeFormatInfo, it is unable to format
// numbers, but we should not crash
Int32.Parse ("123", new DateTimeFormatInfo ());
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");
+ try {
+ Int32.Parse ("734561\0\0\0 \0");
+ Assert.Fail ("C#44");
+ } catch (FormatException) {}
+
+ try {
+ Int32.Parse ("734561\0\0\0 ");
+ Assert.Fail ("C#45");
+ } catch (FormatException) {}
+
Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0"), "C#46");
+
+ Assert.AreEqual (0, Int32.Parse ("0+", NumberStyles.Any), "#50");
}
- [Test]
+ [Test]
public void TestParseExponent ()
{
Assert.AreEqual (2, Int32.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
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");
+ Assert.AreEqual (35, Int32.Parse ("3500E-02", NumberStyles.AllowExponent), "A#5");
try {
Int32.Parse ("2E");
}
try {
- Int32.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
+ Int32.Parse ("2E-1", NumberStyles.AllowExponent);
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) {
}
}
Assert.AreEqual (true, Int32.TryParse ("ffffffff", NumberStyles.HexNumber, Nfi, out result));
Assert.AreEqual (-1, result);
Assert.AreEqual (false, Int32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
+ Assert.IsFalse (int.TryParse ("-", NumberStyles.AllowLeadingSign, Nfi, out result));
+ Assert.IsFalse (int.TryParse (Nfi.CurrencySymbol + "-", NumberStyles.AllowLeadingSign | NumberStyles.AllowCurrencySymbol, Nfi, out result));
}
[Test]
int hundred = 100;
int neghund = -100;
- Assert.IsTrue ( hundred.ToString ("#;#") == "100", "#TS1");
- Assert.IsTrue ( hundred.ToString ("-#;#") == "-100", "#TS2");
- Assert.IsTrue ( neghund.ToString ("#;#") == "100", "#TS3");
- Assert.IsTrue ( neghund.ToString ("#;-#") == "-100", "#TS3");
+ Assert.AreEqual ("100", hundred.ToString ("#;#"), "#TS1");
+ Assert.AreEqual ("-100", hundred.ToString ("-#;#"), "#TS2");
+ Assert.AreEqual ("100", neghund.ToString ("#;#"), "#TS3");
+ Assert.AreEqual ("-100", neghund.ToString ("#;-#"), "#TS4");
+ Assert.AreEqual ("3", 0.ToString ("3;;"), "#TS5");
+ Assert.AreEqual ("3", 0.ToString ("3;2;"), "#TS6");
+ Assert.AreEqual ("3", 0.ToString ("3;"), "#TS7");
+ Assert.AreEqual ("3", 0.ToString ("3;;;;;;;"), "#TS8");
}
[Test]
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);
+ }
}
}