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 : Assertion
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 sval1UserCur1 = "1234/5/67:000 XYZ-";
56 private const string sval2UserCur1 = "1234/5/67:000 XYZ";
57 private const string sval1UserPercent1 = "-%%%1~2~3~4~5~6~7~0~0;0";
58 private const string sval2UserPercent1 = "%%%1~2~3~4~5~6~7~0~0;0";
59 private const NumberStyles style1 = NumberStyles.AllowLeadingWhite | NumberStyles.AllowLeadingSign
60 | NumberStyles.AllowTrailingWhite | NumberStyles.AllowThousands;
61 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
62 private NumberFormatInfo NfiUser;
64 private CultureInfo old_culture;
69 old_culture = Thread.CurrentThread.CurrentCulture;
71 // Set culture to en-US and don't let the user override.
72 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
74 int cdd = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
75 string csym = NumberFormatInfo.CurrentInfo.CurrencySymbol;
76 string csuffix = (cdd > 0 ? "." : "").PadRight(cdd + (cdd > 0 ? 1 : 0), '0');
78 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
79 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
81 Results1[0] = "(" + csym + "9,223,372,036,854,775,808" + csuffix + ")";
82 Results1[3] = "-9223372036854775808." + decimals;
83 Results1[5] = "-9,223,372,036,854,775,808." + decimals;
84 Results1[6] = perPattern.Replace ("n","-922,337,203,685,477,580,800.00");
86 Results2[0] = csym + "9,223,372,036,854,775,807.00000";
87 Results2[6] = perPattern.Replace ("n","922,337,203,685,477,580,700.00000");
89 NfiUser = new NumberFormatInfo();
90 NfiUser.CurrencyDecimalDigits = 3;
91 NfiUser.CurrencyDecimalSeparator = ":";
92 NfiUser.CurrencyGroupSeparator = "/";
93 NfiUser.CurrencyGroupSizes = new int[] { 2,1,0 };
94 NfiUser.CurrencyNegativePattern = 10; // n $-
95 NfiUser.CurrencyPositivePattern = 3; // n $
96 NfiUser.CurrencySymbol = "XYZ";
97 NfiUser.PercentDecimalDigits = 1;
98 NfiUser.PercentDecimalSeparator = ";";
99 NfiUser.PercentGroupSeparator = "~";
100 NfiUser.PercentGroupSizes = new int[] {1};
101 NfiUser.PercentNegativePattern = 2;
102 NfiUser.PercentPositivePattern = 2;
103 NfiUser.PercentSymbol = "%%%";
107 public void TearDown ()
109 Thread.CurrentThread.CurrentCulture = old_culture;
112 public void TestMinMax()
115 AssertEquals(Int64.MinValue, MyInt64_2);
116 AssertEquals(Int64.MaxValue, MyInt64_3);
119 public void TestCompareTo()
121 Assert(MyInt64_3.CompareTo(MyInt64_2) > 0);
122 Assert(MyInt64_2.CompareTo(MyInt64_2) == 0);
123 Assert(MyInt64_1.CompareTo((Int64)(-42)) == 0);
124 Assert(MyInt64_2.CompareTo(MyInt64_3) < 0);
126 MyInt64_2.CompareTo((Int16)100);
127 Fail("Should raise a System.ArgumentException");
129 catch (Exception e) {
130 Assert(typeof(ArgumentException) == e.GetType());
134 public void TestEquals()
136 Assert(MyInt64_1.Equals(MyInt64_1));
137 Assert(MyInt64_1.Equals((Int64)(-42)));
138 Assert(MyInt64_1.Equals((SByte)(-42)) == false);
139 Assert(MyInt64_1.Equals(MyInt64_2) == false);
142 public void TestGetHashCode()
145 MyInt64_1.GetHashCode();
146 MyInt64_2.GetHashCode();
147 MyInt64_3.GetHashCode();
150 Fail("GetHashCode should not raise an exception here");
154 public void TestRoundTripGeneral()
156 foreach(long lv in vals)
158 string s = lv.ToString(Nfi);
159 long lv2 = Int64.Parse(s);
161 long lv3 = Int64.Parse(s, NumberStyles.Integer, Nfi);
166 public void TestRoundTripHex()
168 foreach(long lv in vals)
170 string s = lv.ToString("x", Nfi);
171 long lv2 = Int64.Parse(s, NumberStyles.HexNumber, Nfi);
176 public void TestParseNull()
181 Fail("Should raise System.ArgumentNullException");
183 catch (ArgumentNullException)
189 public void TestParse()
193 lv = Int64.Parse(sval1Test1, style1, Nfi);
194 AssertEquals("Long value should be equal for Test1", val1, lv);
198 lv = Int64.Parse(sval1Test1, Nfi);
199 Fail("Should raise FormatException 1");
201 catch (FormatException)
206 lv = Int64.Parse(sval1Test2, style1, Nfi);
207 AssertEquals("Value should be the same for Test2 with style1", val1, lv);
208 lv = Int64.Parse(sval1Test2, Nfi);
209 AssertEquals("Value should be the same for Test2 without style1", val1, lv);
213 lv = Int64.Parse(sval1Test4, style1, Nfi);
214 Fail("Should raise FormatException 3");
216 catch (FormatException)
221 lv = Int64.Parse(sval1Test5, NumberStyles.Currency, Nfi);
222 AssertEquals("Value should be the same for Test5 and currency style", val1, lv);
224 //test Parse(string s)
225 Assert(MyInt64_1 == Int64.Parse(MyString1));
226 Assert(MyInt64_2 == Int64.Parse(MyString2));
227 Assert(MyInt64_3 == Int64.Parse(MyString3));
230 Fail("Should raise a System.ArgumentNullException");
232 catch (Exception e) {
233 Assert(typeof(ArgumentNullException) == e.GetType());
236 Int64.Parse("not-a-number");
237 Fail("Should raise a System.FormatException");
239 catch (Exception e) {
240 Assert(typeof(FormatException) == e.GetType());
242 //test Parse(string s, NumberStyles style)
244 double OverInt = (double)Int64.MaxValue + 1;
245 Int64.Parse(OverInt.ToString(), NumberStyles.Float);
246 Fail("Should raise a System.OverflowException");
248 catch (Exception e) {
249 Assert(typeof(OverflowException) == e.GetType());
252 Int64.Parse("10000000000000000", NumberStyles.HexNumber);
253 Fail("Should raise a System.OverflowException");
255 catch (Exception e) {
256 Assert(typeof(OverflowException) == e.GetType());
259 double OverInt = (double)Int64.MaxValue + 1;
260 Int64.Parse(OverInt.ToString(), NumberStyles.Integer);
261 Fail("Should raise a System.FormatException");
263 catch (Exception e) {
264 Assert(typeof(FormatException) == e.GetType());
266 AssertEquals("A1", (long)42, Int64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
268 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
269 Fail("Should raise a System.FormatException");
271 catch (Exception e) {
272 Assert(typeof(FormatException) == e.GetType());
274 //test Parse(string s, IFormatProvider provider)
275 Assert(-42 == Int64.Parse(" -42 ", Nfi));
277 Int64.Parse("%42", Nfi);
278 Fail("Should raise a System.FormatException");
280 catch (Exception e) {
281 Assert(typeof(FormatException) == e.GetType());
283 //test Parse(string s, NumberStyles style, IFormatProvider provider)
284 Assert(16 == Int64.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
286 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
287 Fail("Should raise a System.FormatException");
289 catch (Exception e) {
290 Assert(typeof(FormatException) == e.GetType());
294 public void TestToString()
298 s = val1.ToString("c", Nfi);
299 Assert("val1 does not become sval1Test6", s.Equals(sval1Test6));
301 s = val1.ToString("n", Nfi);
302 AssertEquals("val1 does not become sval1Test7", sval1Test7, s);
305 AssertEquals("MyInt64_1.ToString()", MyString1, MyInt64_1.ToString());
306 AssertEquals("MyInt64_2.ToString()", MyString2, MyInt64_2.ToString());
307 AssertEquals("MyInt64_3.ToString()", MyString3, MyInt64_3.ToString());
308 //test ToString(string format)
309 for (int i=0; i < Formats1.Length; i++) {
310 AssertEquals("MyInt64_2.ToString(Formats1["+i+"])", Results1[i], MyInt64_2.ToString(Formats1[i]));
311 AssertEquals("MyInt64_3.ToString(Formats2["+i+"])", Results2[i], MyInt64_3.ToString(Formats2[i]));
313 //test ToString(string format, IFormatProvider provider);
314 for (int i=0; i < Formats1.Length; i++) {
315 AssertEquals("MyInt64_2.ToString(Formats1["+i+"], Nfi)", ResultsNfi1[i], MyInt64_2.ToString(Formats1[i], Nfi));
316 AssertEquals("MyInt64_3.ToString(Formats2["+i+"], Nfi)", ResultsNfi2[i], MyInt64_3.ToString(Formats2[i], Nfi));
319 MyInt64_1.ToString("z");
320 Fail("Should raise a System.FormatException");
322 catch (Exception e) {
323 AssertEquals("Exception is wrong type", typeof(FormatException), e.GetType());
327 public void TestUserCurrency()
333 s = val1.ToString("c", NfiUser);
335 AssertEquals("Currency value type 1 is not what we want to try to parse", sval1UserCur1, s);
337 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
340 } catch (Exception e) {
341 Fail ("1 Unexpected exception at iTest = " + iTest + ", s = " + s + ":e = " + e);
346 s = val2.ToString("c", NfiUser);
348 AssertEquals("Currency value type 2 is not what we want to try to parse", sval2UserCur1, s);
350 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
353 } catch (Exception e) {
354 Fail ("2 Unexpected exception at iTest = " + iTest + ":e = " + e);
358 public void TestUserPercent()
362 s = val1.ToString("p", NfiUser);
363 Assert(s.Equals(sval1UserPercent1));
365 s = val2.ToString("p", NfiUser);
366 Assert(s.Equals(sval2UserPercent1));
370 public void Parse_MaxValue ()
372 AssertEquals ("9223372036854775807", Int64.MaxValue, Int64.Parse ("9223372036854775807"));
376 public void Parse_MinValue ()
378 AssertEquals ("-9223372036854775808,10", Int64.MinValue, Int64.Parse ("-9223372036854775808"));
382 [ExpectedException (typeof (OverflowException))]
383 public void Parse_OverByOneMaxValue ()
385 Int64.Parse ("9223372036854775808");
389 [ExpectedException (typeof (OverflowException))]
390 [Category("NotWorking")]
391 public void Parse_WayOverMaxValue ()
393 Int64.Parse ("1" + Int64.MaxValue.ToString ());
397 [ExpectedException (typeof (OverflowException))]
398 public void Parse_OverByOneMinValue ()
400 Int64.Parse ("-9223372036854775809");
404 [ExpectedException (typeof (OverflowException))]
405 public void Parse_WayOverMinValue ()
407 Int64.Parse (Int64.MinValue.ToString () + "1");
411 public void ToString_Defaults ()
414 // everything defaults to "G"
415 string def = i.ToString ("G");
416 AssertEquals ("ToString()", def, i.ToString ());
417 AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
418 AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
419 AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
420 AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
421 AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
423 AssertEquals ("ToString(G)", "254", def);