1 // Int64Test.cs - NUnit Test Cases for the System.Int64 struct
3 // Author: Martin Weindel (martin.weindel@t-online.de)
5 // (C) Martin Weindel, 2001
7 // tests ToString and Parse function with the culture independent
8 // NumberFormatInfo.InvariantInfo
10 using NUnit.Framework;
12 using System.Globalization;
13 using System.Threading;
16 /// Tests for System.Int64
18 namespace MonoTests.System
22 public class Int64Test
24 private const Int64 MyInt64_1 = -42;
25 private const Int64 MyInt64_2 = -9223372036854775808;
26 private const Int64 MyInt64_3 = 9223372036854775807;
27 private const string MyString1 = "-42";
28 private const string MyString2 = "-9223372036854775808";
29 private const string MyString3 = "9223372036854775807";
30 private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
31 private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
32 private string[] Results1 = {"", "-9223372036854775808", "-9.223372e+018", "-9223372036854775808.00",
33 "-9223372036854775808", "-9,223,372,036,854,775,808.00", "-922,337,203,685,477,580,800.00 %", "8000000000000000"};
34 private string[] Results2 = {"", "9223372036854775807", "9.22337e+018", "9223372036854775807.00000",
35 "9.2234e+18", "9,223,372,036,854,775,807.00000", "922,337,203,685,477,580,700.00000 %", "7fffffffffffffff"};
36 private string[] ResultsNfi1 = {"("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"9,223,372,036,854,775,808.00)", "-9223372036854775808", "-9.223372e+018", "-9223372036854775808.00",
37 "-9223372036854775808", "-9,223,372,036,854,775,808.00", "-922,337,203,685,477,580,800.00 %", "8000000000000000"};
38 private string[] ResultsNfi2 = {""+NumberFormatInfo.InvariantInfo.CurrencySymbol+"9,223,372,036,854,775,807.00000", "9223372036854775807", "9.22337e+018", "9223372036854775807.00000",
39 "9.2234e+18", "9,223,372,036,854,775,807.00000", "922,337,203,685,477,580,700.00000 %", "7fffffffffffffff"};
42 = { 0, Int64.MaxValue, Int64.MinValue,
43 1L, 12L, 123L, 1234L, -123L,
44 1234567890123456L, 6543210987654321L };
46 private const long val1 = -1234567L;
47 private const long val2 = 1234567L;
48 private const string sval1Test1 = " -1,234,567 ";
49 private const string sval1Test2 = " -1234567 ";
50 //private const string sval1Test3 = " -12345,,,,67 "; // interesting: this case works on SDK Beta2, but the specification says nothing about this case
51 private const string sval1Test4 = " -12345 67 ";
52 private string sval1Test5 = " -"+NumberFormatInfo.InvariantInfo.CurrencySymbol+"1,234,567.00 ";
53 private string sval1Test6 = "("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"1,234,567.00)";
54 private const string sval1Test7 = "-1,234,567.00";
55 private const string sval1UserPercent1 = "-%%%1~2~3~4~5~6~7~0~0;0";
56 private const string sval2UserPercent1 = "%%%1~2~3~4~5~6~7~0~0;0";
57 private const NumberStyles style1 = NumberStyles.AllowLeadingWhite | NumberStyles.AllowLeadingSign
58 | NumberStyles.AllowTrailingWhite | NumberStyles.AllowThousands;
59 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
60 private NumberFormatInfo NfiUser;
62 private CultureInfo old_culture;
67 old_culture = Thread.CurrentThread.CurrentCulture;
69 // Set culture to en-US and don't let the user override.
70 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
72 int cdd = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
73 string csym = NumberFormatInfo.CurrentInfo.CurrencySymbol;
74 string csuffix = (cdd > 0 ? "." : "").PadRight(cdd + (cdd > 0 ? 1 : 0), '0');
76 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
77 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
79 Results1[0] = "(" + csym + "9,223,372,036,854,775,808" + csuffix + ")";
80 Results1[3] = "-9223372036854775808." + decimals;
81 Results1[5] = "-9,223,372,036,854,775,808." + decimals;
82 Results1[6] = perPattern.Replace ("n","-922,337,203,685,477,580,800.00");
84 Results2[0] = csym + "9,223,372,036,854,775,807.00000";
85 Results2[6] = perPattern.Replace ("n","922,337,203,685,477,580,700.00000");
87 NfiUser = new NumberFormatInfo();
88 NfiUser.CurrencyDecimalDigits = 3;
89 NfiUser.CurrencyDecimalSeparator = ":";
90 NfiUser.CurrencyGroupSeparator = "/";
91 NfiUser.CurrencyGroupSizes = new int[] { 2,1,0 };
92 NfiUser.CurrencyNegativePattern = 10; // n $-
93 NfiUser.CurrencyPositivePattern = 3; // n $
94 NfiUser.CurrencySymbol = "XYZ";
95 NfiUser.PercentDecimalDigits = 1;
96 NfiUser.PercentDecimalSeparator = ";";
97 NfiUser.PercentGroupSeparator = "~";
98 NfiUser.PercentGroupSizes = new int[] {1};
99 NfiUser.PercentNegativePattern = 2;
100 NfiUser.PercentPositivePattern = 2;
101 NfiUser.PercentSymbol = "%%%";
105 public void TearDown ()
107 Thread.CurrentThread.CurrentCulture = old_culture;
111 public void TestMinMax()
114 Assert.AreEqual(Int64.MinValue, MyInt64_2);
115 Assert.AreEqual(Int64.MaxValue, MyInt64_3);
119 public void TestCompareTo()
121 Assert.IsTrue(MyInt64_3.CompareTo(MyInt64_2) > 0);
122 Assert.IsTrue(MyInt64_2.CompareTo(MyInt64_2) == 0);
123 Assert.IsTrue(MyInt64_1.CompareTo((object)(Int64)(-42)) == 0);
124 Assert.IsTrue(MyInt64_2.CompareTo(MyInt64_3) < 0);
126 MyInt64_2.CompareTo((object)(Int16)100);
127 Assert.Fail("Should raise a System.ArgumentException");
129 catch (Exception e) {
130 Assert.IsTrue(typeof(ArgumentException) == e.GetType());
135 public void TestEquals()
137 Assert.IsTrue(MyInt64_1.Equals(MyInt64_1));
138 Assert.IsTrue(MyInt64_1.Equals((object)(Int64)(-42)));
139 Assert.IsTrue(MyInt64_1.Equals((object)(SByte)(-42)) == false);
140 Assert.IsTrue(MyInt64_1.Equals(MyInt64_2) == false);
144 public void TestGetHashCode()
147 MyInt64_1.GetHashCode();
148 MyInt64_2.GetHashCode();
149 MyInt64_3.GetHashCode();
152 Assert.Fail("GetHashCode should not raise an exception here");
157 public void TestRoundTripGeneral()
159 foreach(long lv in vals)
161 string s = lv.ToString(Nfi);
162 long lv2 = Int64.Parse(s);
163 Assert.IsTrue(lv == lv2);
164 long lv3 = Int64.Parse(s, NumberStyles.Integer, Nfi);
165 Assert.IsTrue(lv == lv3);
170 public void TestRoundTripHex()
172 foreach(long lv in vals)
174 string s = lv.ToString("x", Nfi);
175 long lv2 = Int64.Parse(s, NumberStyles.HexNumber, Nfi);
176 Assert.IsTrue(lv == lv2);
181 public void TestParseNull()
186 Assert.Fail("Should raise System.ArgumentNullException");
188 catch (ArgumentNullException)
195 public void TestParse()
199 lv = Int64.Parse(sval1Test1, style1, Nfi);
200 Assert.AreEqual(val1, lv, "Long value should be equal for Test1");
204 lv = Int64.Parse(sval1Test1, Nfi);
205 Assert.Fail("Should raise FormatException 1");
207 catch (FormatException)
212 lv = Int64.Parse(sval1Test2, style1, Nfi);
213 Assert.AreEqual(val1, lv, "Value should be the same for Test2 with style1");
214 lv = Int64.Parse(sval1Test2, Nfi);
215 Assert.AreEqual(val1, lv, "Value should be the same for Test2 without style1");
219 lv = Int64.Parse(sval1Test4, style1, Nfi);
220 Assert.Fail("Should raise FormatException 3");
222 catch (FormatException)
227 lv = Int64.Parse(sval1Test5, NumberStyles.Currency, Nfi);
228 Assert.AreEqual(val1, lv, "Value should be the same for Test5 and currency style");
230 //test Parse(string s)
231 Assert.IsTrue(MyInt64_1 == Int64.Parse(MyString1));
232 Assert.IsTrue(MyInt64_2 == Int64.Parse(MyString2));
233 Assert.IsTrue(MyInt64_3 == Int64.Parse(MyString3));
236 Assert.Fail("#1:Should raise a System.ArgumentNullException");
238 catch (Exception e) {
239 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "#2");
242 Int64.Parse("not-a-number");
243 Assert.Fail("#3:Should raise a System.FormatException");
245 catch (Exception e) {
246 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#4");
248 //test Parse(string s, NumberStyles style)
250 double OverInt = (double)Int64.MaxValue + 1;
251 Int64.Parse(OverInt.ToString(), NumberStyles.Float);
252 Assert.Fail("#5:Should raise a System.OverflowException");
254 catch (Exception e) {
255 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#6");
258 Int64.Parse("10000000000000000", NumberStyles.HexNumber);
259 Assert.Fail("#7:Should raise a System.OverflowException");
261 catch (Exception e) {
262 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#8");
265 double OverInt = (double)Int64.MaxValue + 1;
266 Int64.Parse(OverInt.ToString(), NumberStyles.Integer);
267 Assert.Fail("#9:Should raise a System.FormatException");
269 catch (Exception e) {
270 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#10");
272 Assert.AreEqual((long)42, Int64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency), "A1");
274 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
275 Assert.Fail("#11:Should raise a System.FormatException");
277 catch (Exception e) {
278 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#12");
280 //test Parse(string s, IFormatProvider provider)
281 Assert.IsTrue(-42 == Int64.Parse(" -42 ", Nfi), "A2");
283 Int64.Parse("%42", Nfi);
284 Assert.Fail("#13:Should raise a System.FormatException");
286 catch (Exception e) {
287 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#14");
289 //test Parse(string s, NumberStyles style, IFormatProvider provider)
290 Assert.IsTrue(16 == Int64.Parse(" 10 ", NumberStyles.HexNumber, Nfi), "A3");
292 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
293 Assert.Fail("#15:Should raise a System.FormatException");
295 catch (Exception e) {
296 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#16");
299 long.Parse ("9223372036854775808");
300 Assert.Fail ("#17:should raise an OverflowException");
301 } catch (Exception e) {
302 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#18");
305 long.Parse ("9223372036854775808", CultureInfo.InvariantCulture);
306 Assert.Fail ("#19:should raise an OverflowException");
307 } catch (Exception e) {
308 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#20");
311 // Pass a DateTimeFormatInfo, it is unable to format
312 // numbers, but we should not crash
314 Int64.Parse ("123", new DateTimeFormatInfo ());
316 Assert.AreEqual (734561, Int64.Parse ("734561\0"), "#21");
317 Assert.AreEqual (734561, Int64.Parse ("734561\0\0\0 \0"), "#22");
318 Assert.AreEqual (734561, Int64.Parse ("734561\0\0\0 "), "#23");
319 Assert.AreEqual (734561, Int64.Parse ("734561\0\0\0"), "#24");
321 Assert.AreEqual (0, Int64.Parse ("0+", NumberStyles.Any), "#30");
325 public void TestParseExponent ()
327 Assert.AreEqual (2, long.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
328 Assert.AreEqual (20, long.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
329 Assert.AreEqual (200, long.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
330 Assert.AreEqual (2000000, long.Parse ("2E6", NumberStyles.AllowExponent), "A#4");
331 Assert.AreEqual (200, long.Parse ("2E+2", NumberStyles.AllowExponent), "A#5");
332 Assert.AreEqual (2, long.Parse ("2", NumberStyles.AllowExponent), "A#6");
333 Assert.AreEqual (21, long.Parse ("2.1E1", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#7");
334 Assert.AreEqual (520, long.Parse (".52E3", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#8");
335 Assert.AreEqual (32500000, long.Parse ("32.5E6", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#9");
336 Assert.AreEqual (890, long.Parse ("8.9000E2", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#10");
341 } catch (FormatException) {
345 long.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
347 } catch (FormatException) {
351 long.Parse ("2E 2", NumberStyles.AllowExponent);
353 } catch (FormatException) {
357 long.Parse ("2E2 ", NumberStyles.AllowExponent);
359 } catch (FormatException) {
363 long.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
365 } catch (OverflowException) {
369 long exponent = (long) Int32.MaxValue + 10;
370 long.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
372 } catch (OverflowException) {
376 long.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
378 } catch (OverflowException) {
382 long.Parse ("2 math e1", NumberStyles.AllowExponent);
384 } catch (FormatException) {
388 long.Parse ("2.09E1", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);
390 } catch (OverflowException) {
395 public void TestTryParse()
399 Assert.AreEqual (true, long.TryParse (MyString1, out result));
400 Assert.AreEqual (MyInt64_1, result);
401 Assert.AreEqual (true, long.TryParse (MyString2, out result));
402 Assert.AreEqual (MyInt64_2, result);
403 Assert.AreEqual (true, long.TryParse (MyString3, out result));
404 Assert.AreEqual (MyInt64_3, result);
406 Assert.AreEqual (true, long.TryParse ("1", out result));
407 Assert.AreEqual (1, result);
408 Assert.AreEqual (true, long.TryParse (" 1", out result));
409 Assert.AreEqual (1, result);
410 Assert.AreEqual (true, long.TryParse (" 1", out result));
411 Assert.AreEqual (1, result);
412 Assert.AreEqual (true, long.TryParse ("1 ", out result));
413 Assert.AreEqual (1, result);
414 Assert.AreEqual (true, long.TryParse ("+1", out result));
415 Assert.AreEqual (1, result);
416 Assert.AreEqual (true, long.TryParse ("-1", out result));
417 Assert.AreEqual (-1, result);
418 Assert.AreEqual (true, long.TryParse (" -1", out result));
419 Assert.AreEqual (-1, result);
420 Assert.AreEqual (true, long.TryParse (" -1 ", out result));
421 Assert.AreEqual (-1, result);
422 Assert.AreEqual (true, long.TryParse (" -1 ", out result));
423 Assert.AreEqual (-1, result);
426 Assert.AreEqual (false, long.TryParse (null, out result));
427 Assert.AreEqual (0, result);
429 Assert.AreEqual (false, long.TryParse ("not-a-number", out result));
431 double OverInt = (double)long.MaxValue + 1;
432 Assert.AreEqual (false, long.TryParse (OverInt.ToString (), out result));
433 Assert.AreEqual (false, long.TryParse (OverInt.ToString (), NumberStyles.None, CultureInfo.InvariantCulture, out result));
435 Assert.AreEqual (false, long.TryParse ("$42", NumberStyles.Integer, null, out result));
436 Assert.AreEqual (false, long.TryParse ("%42", NumberStyles.Integer, Nfi, out result));
437 Assert.AreEqual (false, long.TryParse ("$42", NumberStyles.Integer, Nfi, out result));
438 Assert.AreEqual (false, long.TryParse (" - 1 ", out result));
439 Assert.AreEqual (false, long.TryParse (" - ", out result));
440 Assert.AreEqual (true, long.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
441 Assert.AreEqual (true, long.TryParse ("10000000000", out result));
442 Assert.AreEqual (true, long.TryParse ("-10000000000", out result));
443 Assert.AreEqual (true, long.TryParse ("7fffffff", NumberStyles.HexNumber, Nfi, out result));
444 Assert.AreEqual (int.MaxValue, result);
445 Assert.AreEqual (true, long.TryParse ("80000000", NumberStyles.HexNumber, Nfi, out result));
446 Assert.AreEqual (2147483648, result);
447 Assert.AreEqual (true, long.TryParse ("ffffffff", NumberStyles.HexNumber, Nfi, out result));
448 Assert.AreEqual (uint.MaxValue, result);
449 Assert.AreEqual (true, long.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
450 Assert.IsFalse (long.TryParse ("-", NumberStyles.AllowLeadingSign, Nfi, out result));
451 Assert.IsFalse (long.TryParse (Nfi.CurrencySymbol + "-", NumberStyles.AllowLeadingSign | NumberStyles.AllowCurrencySymbol, Nfi, out result));
455 public void TestToString()
459 s = val1.ToString("c", Nfi);
460 Assert.IsTrue(s.Equals(sval1Test6), "val1 does not become sval1Test6");
462 s = val1.ToString("n", Nfi);
463 Assert.AreEqual(sval1Test7, s, "val1 does not become sval1Test7");
466 Assert.AreEqual(MyString1, MyInt64_1.ToString(), "MyInt64_1.ToString()");
467 Assert.AreEqual(MyString2, MyInt64_2.ToString(), "MyInt64_2.ToString()");
468 Assert.AreEqual(MyString3, MyInt64_3.ToString(), "MyInt64_3.ToString()");
469 //test ToString(string format)
470 for (int i=0; i < Formats1.Length; i++) {
471 Assert.AreEqual(Results1[i], MyInt64_2.ToString(Formats1[i]), "MyInt64_2.ToString(Formats1["+i+"])");
472 Assert.AreEqual(Results2[i], MyInt64_3.ToString(Formats2[i]), "MyInt64_3.ToString(Formats2["+i+"])");
474 //test ToString(string format, IFormatProvider provider);
475 for (int i=0; i < Formats1.Length; i++) {
476 Assert.AreEqual(ResultsNfi1[i], MyInt64_2.ToString(Formats1[i], Nfi), "MyInt64_2.ToString(Formats1["+i+"], Nfi)");
477 Assert.AreEqual(ResultsNfi2[i], MyInt64_3.ToString(Formats2[i], Nfi), "MyInt64_3.ToString(Formats2["+i+"], Nfi)");
480 MyInt64_1.ToString("z");
481 Assert.Fail("Should raise a System.FormatException");
483 catch (Exception e) {
484 Assert.AreEqual(typeof(FormatException), e.GetType(), "Exception is wrong type");
489 public void TestUserCurrency ()
493 s = val1.ToString ("c", NfiUser);
494 Assert.AreEqual ("1234/5/67:000 XYZ-", s, "Currency value type 1 is not what we want to try to parse");
495 v = Int64.Parse ("1234/5/67:000 XYZ-", NumberStyles.Currency, NfiUser);
496 Assert.AreEqual (val1, v);
498 s = val2.ToString ("c", NfiUser);
499 Assert.AreEqual ("1234/5/67:000 XYZ", s, "Currency value type 2 is not what we want to try to parse");
500 v = Int64.Parse (s, NumberStyles.Currency, NfiUser);
501 Assert.AreEqual (val2, v);
505 public void TestUserPercent()
509 s = val1.ToString("p", NfiUser);
510 Assert.IsTrue(s.Equals(sval1UserPercent1));
512 s = val2.ToString("p", NfiUser);
513 Assert.IsTrue(s.Equals(sval2UserPercent1));
517 public void Parse_MaxValue ()
519 Assert.AreEqual (Int64.MaxValue, Int64.Parse ("9223372036854775807"), "9223372036854775807");
523 public void Parse_MinValue ()
525 Assert.AreEqual (Int64.MinValue, Int64.Parse ("-9223372036854775808"), "-9223372036854775808,10");
529 [ExpectedException (typeof (OverflowException))]
530 public void Parse_OverByOneMaxValue ()
532 Int64.Parse ("9223372036854775808");
536 [ExpectedException (typeof (OverflowException))]
537 public void Parse_WayOverMaxValue ()
539 Int64.Parse ("1" + Int64.MaxValue.ToString ());
543 [ExpectedException (typeof (OverflowException))]
544 public void Parse_OverByOneMinValue ()
546 Int64.Parse ("-9223372036854775809");
550 [ExpectedException (typeof (OverflowException))]
551 public void Parse_WayOverMinValue ()
553 Int64.Parse (Int64.MinValue.ToString () + "1");
557 public void ToString_Defaults ()
560 // everything defaults to "G"
561 string def = i.ToString ("G");
562 Assert.AreEqual (def, i.ToString (), "ToString()");
563 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
564 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
565 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
566 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
567 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
569 Assert.AreEqual ("254", def, "ToString(G)");