2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System / Int64Test.cs
1 // Int64Test.cs - NUnit Test Cases for the System.Int64 struct
2 //
3 // Author: Martin Weindel (martin.weindel@t-online.de)
4 //
5 // (C) Martin Weindel, 2001
6 // 
7 // tests ToString and Parse function with the culture independent 
8 // NumberFormatInfo.InvariantInfo
9
10 using NUnit.Framework;
11 using System;
12 using System.Globalization;
13 using System.Threading;
14
15     /// <summary>
16     /// Tests for System.Int64
17     /// </summary>
18 namespace MonoTests.System
19 {
20
21 [TestFixture]
22 public class Int64Test : Assertion
23 {
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"};
40
41         private long[] vals
42         = { 0, Int64.MaxValue, Int64.MinValue,
43               1L, 12L, 123L, 1234L, -123L, 
44               1234567890123456L, 6543210987654321L };
45
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;
63
64         private CultureInfo old_culture;
65
66         [SetUp]
67         public void SetUp () 
68         {
69                 old_culture = Thread.CurrentThread.CurrentCulture;
70
71                 // Set culture to en-US and don't let the user override.
72                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
73
74                 int cdd = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
75                 string csym = NumberFormatInfo.CurrentInfo.CurrencySymbol;
76                 string csuffix = (cdd > 0 ? "." : "").PadRight(cdd + (cdd > 0 ? 1 : 0), '0');
77                 
78                 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
79                 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
80                 
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");
85                 
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");
88                 
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 = "%%%";
104         }
105
106         [TearDown]
107         public void TearDown ()
108         {
109                 Thread.CurrentThread.CurrentCulture = old_culture;
110         }
111
112         public void TestMinMax()
113         {
114                 
115                 AssertEquals(Int64.MinValue, MyInt64_2);
116                 AssertEquals(Int64.MaxValue, MyInt64_3);
117         }
118         
119         public void TestCompareTo()
120         {
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);
125                 try {
126                         MyInt64_2.CompareTo((Int16)100);
127                         Fail("Should raise a System.ArgumentException");
128                 }
129                 catch (Exception e) {
130                         Assert(typeof(ArgumentException) == e.GetType());
131                 }
132         }
133
134         public void TestEquals()
135         {
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);
140         }
141         
142         public void TestGetHashCode()
143         {
144                 try {
145                         MyInt64_1.GetHashCode();
146                         MyInt64_2.GetHashCode();
147                         MyInt64_3.GetHashCode();
148                 }
149                 catch {
150                         Fail("GetHashCode should not raise an exception here");
151                 }
152         }
153         
154     public void TestRoundTripGeneral() 
155     {
156         foreach(long lv in vals) 
157         {
158             string s = lv.ToString(Nfi);
159             long lv2 = Int64.Parse(s);
160             Assert(lv == lv2);
161             long lv3 = Int64.Parse(s, NumberStyles.Integer, Nfi);
162             Assert(lv == lv3);
163         }
164     }
165
166     public void TestRoundTripHex() 
167     {
168         foreach(long lv in vals) 
169         {
170             string s = lv.ToString("x", Nfi);
171             long lv2 = Int64.Parse(s, NumberStyles.HexNumber, Nfi);
172             Assert(lv == lv2);
173         }
174     }
175
176     public void TestParseNull()
177     {
178         try 
179         {
180             Int64.Parse(null);
181             Fail("Should raise System.ArgumentNullException"); 
182         } 
183         catch (ArgumentNullException) 
184         {
185             // ok
186         }
187     }
188
189     public void TestParse()
190     {
191         long lv;
192
193         lv = Int64.Parse(sval1Test1, style1, Nfi);
194         AssertEquals("Long value should be equal for Test1", val1, lv);
195
196         try
197         {
198             lv = Int64.Parse(sval1Test1, Nfi);
199             Fail("Should raise FormatException 1");
200         }
201         catch (FormatException)
202         {
203             // ok
204         }
205
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);
210
211         try
212         {
213             lv = Int64.Parse(sval1Test4, style1, Nfi);
214             Fail("Should raise FormatException 3");
215         }
216         catch (FormatException)
217         {
218             // ok
219         }
220
221         lv = Int64.Parse(sval1Test5, NumberStyles.Currency, Nfi);
222         AssertEquals("Value should be the same for Test5 and currency style", val1, lv);
223
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));
228         try {
229                 Int64.Parse(null);
230                 Fail("Should raise a System.ArgumentNullException");
231         }
232         catch (Exception e) {
233                 Assert(typeof(ArgumentNullException) == e.GetType());
234         }
235         try {
236                 Int64.Parse("not-a-number");
237                 Fail("Should raise a System.FormatException");
238         }
239         catch (Exception e) {
240                 Assert(typeof(FormatException) == e.GetType());
241         }
242         //test Parse(string s, NumberStyles style)
243         try {
244                 double OverInt = (double)Int64.MaxValue + 1;
245                 Int64.Parse(OverInt.ToString(), NumberStyles.Float);
246                 Fail("Should raise a System.OverflowException");
247         }
248         catch (Exception e) {
249                 Assert(typeof(OverflowException) == e.GetType());
250         }
251         try {
252                 Int64.Parse("10000000000000000", NumberStyles.HexNumber);
253                 Fail("Should raise a System.OverflowException");
254         }
255         catch (Exception e) {
256                 Assert(typeof(OverflowException) == e.GetType());
257         }
258         try {
259                 double OverInt = (double)Int64.MaxValue + 1;
260                 Int64.Parse(OverInt.ToString(), NumberStyles.Integer);
261                 Fail("Should raise a System.FormatException");
262         }
263         catch (Exception e) {
264                 Assert(typeof(FormatException) == e.GetType());
265         }
266         AssertEquals("A1", (long)42, Int64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
267         try {
268                 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
269                 Fail("Should raise a System.FormatException");
270         }
271         catch (Exception e) {
272                 Assert(typeof(FormatException) == e.GetType());
273         }
274         //test Parse(string s, IFormatProvider provider)
275         Assert(-42 == Int64.Parse(" -42 ", Nfi));
276         try {
277                 Int64.Parse("%42", Nfi);
278                 Fail("Should raise a System.FormatException");
279         }
280         catch (Exception e) {
281                 Assert(typeof(FormatException) == e.GetType());
282         }
283         //test Parse(string s, NumberStyles style, IFormatProvider provider)
284         Assert(16 == Int64.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
285         try {
286                 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
287                 Fail("Should raise a System.FormatException");
288         }
289         catch (Exception e) {
290                 Assert(typeof(FormatException) == e.GetType());
291         }    
292     }
293
294     public void TestToString() 
295     {
296         string s;
297
298         s = val1.ToString("c", Nfi);
299         Assert("val1 does not become sval1Test6", s.Equals(sval1Test6));
300
301         s = val1.ToString("n", Nfi);
302         AssertEquals("val1 does not become sval1Test7", sval1Test7, s);
303
304         //test ToString()
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]));
312         }
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));
317         }
318         try {
319                 MyInt64_1.ToString("z");
320                 Fail("Should raise a System.FormatException");
321         }
322         catch (Exception e) {
323                 AssertEquals("Exception is wrong type", typeof(FormatException), e.GetType());
324         }
325     }
326
327     public void TestUserCurrency()
328     {
329         string s= "";
330         long v;
331         int iTest = 1;
332         try {
333                 s = val1.ToString("c", NfiUser);
334                 iTest++;
335                 AssertEquals("Currency value type 1 is not what we want to try to parse", sval1UserCur1, s);
336                 iTest++;
337                 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
338                 iTest++;
339                 Assert(v == val1);
340         } catch (Exception e) {
341                 Fail ("1 Unexpected exception at iTest = " + iTest + ", s = " + s + ":e = " + e);
342         }
343    
344         iTest = 1;
345         try {
346                 s = val2.ToString("c", NfiUser);
347                 iTest++;
348                 AssertEquals("Currency value type 2 is not what we want to try to parse", sval2UserCur1, s);
349                 iTest++;
350                 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
351                 iTest++;
352                 Assert(v == val2);
353         } catch (Exception e) {
354                 Fail ("2 Unexpected exception at iTest = " + iTest + ":e = " + e);
355         }
356     }
357
358     public void TestUserPercent()
359     {
360         string s;
361
362         s = val1.ToString("p", NfiUser);
363         Assert(s.Equals(sval1UserPercent1));
364
365         s = val2.ToString("p", NfiUser);
366         Assert(s.Equals(sval2UserPercent1));
367     }
368
369                 [Test]
370                 public void Parse_MaxValue ()
371                 {
372                         AssertEquals ("9223372036854775807", Int64.MaxValue, Int64.Parse ("9223372036854775807"));
373                 }
374
375                 [Test]
376                 public void Parse_MinValue ()
377                 {
378                         AssertEquals ("-9223372036854775808,10", Int64.MinValue, Int64.Parse ("-9223372036854775808"));
379                 }
380
381                 [Test]
382                 [ExpectedException (typeof (OverflowException))]
383                 public void Parse_OverByOneMaxValue ()
384                 {
385                         Int64.Parse ("9223372036854775808");
386                 }
387
388                 [Test]
389                 [ExpectedException (typeof (OverflowException))]
390                 [Category("NotWorking")]
391                 public void Parse_WayOverMaxValue ()
392                 {
393                         Int64.Parse ("1" + Int64.MaxValue.ToString ());
394                 }
395
396                 [Test]
397                 [ExpectedException (typeof (OverflowException))]
398                 public void Parse_OverByOneMinValue ()
399                 {
400                         Int64.Parse ("-9223372036854775809");
401                 }
402
403                 [Test]
404                 [ExpectedException (typeof (OverflowException))]
405                 public void Parse_WayOverMinValue ()
406                 {
407                         Int64.Parse (Int64.MinValue.ToString () + "1");
408                 }
409
410                 [Test]
411                 public void ToString_Defaults () 
412                 {
413                         Int64 i = 254;
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));
422
423                         AssertEquals ("ToString(G)", "254", def);
424                 }
425         }
426 }