2004-05-27 Sebastien Pouliot <sebastien@ximian.com>
[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 public class Int64Test : TestCase
22 {
23         private const Int64 MyInt64_1 = -42;
24         private const Int64 MyInt64_2 = -9223372036854775808;
25         private const Int64 MyInt64_3 = 9223372036854775807;
26         private const string MyString1 = "-42";
27         private const string MyString2 = "-9223372036854775808";
28         private const string MyString3 = "9223372036854775807";
29         private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
30         private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
31         private string[] Results1 = {"", "-9223372036854775808", "-9.223372e+018", "-9223372036854775808.00",
32                                           "-9223372036854775808", "-9,223,372,036,854,775,808.00", "-922,337,203,685,477,580,800.00 %", "8000000000000000"};
33         private string[] Results2 = {"", "9223372036854775807", "9.22337e+018", "9223372036854775807.00000",
34                                           "9.2234e+18", "9,223,372,036,854,775,807.00000", "922,337,203,685,477,580,700.00000 %", "7fffffffffffffff"};
35         private string[] ResultsNfi1 = {"("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"9,223,372,036,854,775,808.00)", "-9223372036854775808", "-9.223372e+018", "-9223372036854775808.00",
36                                           "-9223372036854775808", "-9,223,372,036,854,775,808.00", "-922,337,203,685,477,580,800.00 %", "8000000000000000"};
37         private string[] ResultsNfi2 = {""+NumberFormatInfo.InvariantInfo.CurrencySymbol+"9,223,372,036,854,775,807.00000", "9223372036854775807", "9.22337e+018", "9223372036854775807.00000",
38                                           "9.2234e+18", "9,223,372,036,854,775,807.00000", "922,337,203,685,477,580,700.00000 %", "7fffffffffffffff"};
39
40         private long[] vals
41         = { 0, Int64.MaxValue, Int64.MinValue,
42               1L, 12L, 123L, 1234L, -123L, 
43               1234567890123456L, 6543210987654321L };
44
45         private const long val1 = -1234567L;
46         private const long val2 = 1234567L;
47         private const string sval1Test1 = "  -1,234,567   ";
48         private const string sval1Test2 = "  -1234567   ";
49         //private const string sval1Test3 = "  -12345,,,,67   "; // interesting: this case works on SDK Beta2, but the specification says nothing about this case
50         private const string sval1Test4 = "  -12345 67   ";
51         private  string sval1Test5 = "  -"+NumberFormatInfo.InvariantInfo.CurrencySymbol+"1,234,567.00 ";
52         private  string sval1Test6 = "("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"1,234,567.00)";
53         private const string sval1Test7 = "-1,234,567.00";
54         private const string sval1UserCur1 = "1234/5/67:000 XYZ-";
55         private const string sval2UserCur1 = "1234/5/67:000 XYZ";
56         private const string sval1UserPercent1 = "-%%%1~2~3~4~5~6~7~0~0;0";
57         private const string sval2UserPercent1 = "%%%1~2~3~4~5~6~7~0~0;0";
58         private const NumberStyles style1 =  NumberStyles.AllowLeadingWhite | NumberStyles.AllowLeadingSign
59                                         | NumberStyles.AllowTrailingWhite | NumberStyles.AllowThousands;
60         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
61         private NumberFormatInfo NfiUser;
62
63         public Int64Test() {}
64
65         private CultureInfo old_culture;
66
67         protected override 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         protected override void TearDown()
107         {
108                 Thread.CurrentThread.CurrentCulture = old_culture;
109         }
110
111         public void TestMinMax()
112         {
113                 
114                 AssertEquals(Int64.MinValue, MyInt64_2);
115                 AssertEquals(Int64.MaxValue, MyInt64_3);
116         }
117         
118         public void TestCompareTo()
119         {
120                 Assert(MyInt64_3.CompareTo(MyInt64_2) > 0);
121                 Assert(MyInt64_2.CompareTo(MyInt64_2) == 0);
122                 Assert(MyInt64_1.CompareTo((Int64)(-42)) == 0);
123                 Assert(MyInt64_2.CompareTo(MyInt64_3) < 0);
124                 try {
125                         MyInt64_2.CompareTo((Int16)100);
126                         Fail("Should raise a System.ArgumentException");
127                 }
128                 catch (Exception e) {
129                         Assert(typeof(ArgumentException) == e.GetType());
130                 }
131         }
132
133         public void TestEquals()
134         {
135                 Assert(MyInt64_1.Equals(MyInt64_1));
136                 Assert(MyInt64_1.Equals((Int64)(-42)));
137                 Assert(MyInt64_1.Equals((SByte)(-42)) == false);
138                 Assert(MyInt64_1.Equals(MyInt64_2) == false);
139         }
140         
141         public void TestGetHashCode()
142         {
143                 try {
144                         MyInt64_1.GetHashCode();
145                         MyInt64_2.GetHashCode();
146                         MyInt64_3.GetHashCode();
147                 }
148                 catch {
149                         Fail("GetHashCode should not raise an exception here");
150                 }
151         }
152         
153     public void TestRoundTripGeneral() 
154     {
155         foreach(long lv in vals) 
156         {
157             string s = lv.ToString(Nfi);
158             long lv2 = Int64.Parse(s);
159             Assert(lv == lv2);
160             long lv3 = Int64.Parse(s, NumberStyles.Integer, Nfi);
161             Assert(lv == lv3);
162         }
163     }
164
165     public void TestRoundTripHex() 
166     {
167         foreach(long lv in vals) 
168         {
169             string s = lv.ToString("x", Nfi);
170             long lv2 = Int64.Parse(s, NumberStyles.HexNumber, Nfi);
171             Assert(lv == lv2);
172         }
173     }
174
175     public void TestParseNull()
176     {
177         try 
178         {
179             Int64.Parse(null);
180             Fail("Should raise System.ArgumentNullException"); 
181         } 
182         catch (ArgumentNullException) 
183         {
184             // ok
185         }
186     }
187
188     public void TestParse()
189     {
190         long lv;
191
192         lv = Int64.Parse(sval1Test1, style1, Nfi);
193         AssertEquals("Long value should be equal for Test1", val1, lv);
194
195         try
196         {
197             lv = Int64.Parse(sval1Test1, Nfi);
198             Fail("Should raise FormatException 1");
199         }
200         catch (FormatException)
201         {
202             // ok
203         }
204
205         lv = Int64.Parse(sval1Test2, style1, Nfi);
206         AssertEquals("Value should be the same for Test2 with style1", val1, lv);
207         lv = Int64.Parse(sval1Test2, Nfi);
208         AssertEquals("Value should be the same for Test2 without style1", val1, lv);
209
210         try
211         {
212             lv = Int64.Parse(sval1Test4, style1, Nfi);
213             Fail("Should raise FormatException 3");
214         }
215         catch (FormatException)
216         {
217             // ok
218         }
219
220         lv = Int64.Parse(sval1Test5, NumberStyles.Currency, Nfi);
221         AssertEquals("Value should be the same for Test5 and currency style", val1, lv);
222
223         //test Parse(string s)
224         Assert(MyInt64_1 == Int64.Parse(MyString1));
225         Assert(MyInt64_2 == Int64.Parse(MyString2));
226         Assert(MyInt64_3 == Int64.Parse(MyString3));
227         try {
228                 Int64.Parse(null);
229                 Fail("Should raise a System.ArgumentNullException");
230         }
231         catch (Exception e) {
232                 Assert(typeof(ArgumentNullException) == e.GetType());
233         }
234         try {
235                 Int64.Parse("not-a-number");
236                 Fail("Should raise a System.FormatException");
237         }
238         catch (Exception e) {
239                 Assert(typeof(FormatException) == e.GetType());
240         }
241         //test Parse(string s, NumberStyles style)
242         try {
243                 double OverInt = (double)Int64.MaxValue + 1;
244                 Int64.Parse(OverInt.ToString(), NumberStyles.Float);
245                 Fail("Should raise a System.OverflowException");
246         }
247         catch (Exception e) {
248                 Assert(typeof(OverflowException) == e.GetType());
249         }
250         try {
251                 Int64.Parse("10000000000000000", NumberStyles.HexNumber);
252                 Fail("Should raise a System.OverflowException");
253         }
254         catch (Exception e) {
255                 Assert(typeof(OverflowException) == e.GetType());
256         }
257         try {
258                 double OverInt = (double)Int64.MaxValue + 1;
259                 Int64.Parse(OverInt.ToString(), NumberStyles.Integer);
260                 Fail("Should raise a System.FormatException");
261         }
262         catch (Exception e) {
263                 Assert(typeof(FormatException) == e.GetType());
264         }
265         AssertEquals("A1", (long)42, Int64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
266         try {
267                 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
268                 Fail("Should raise a System.FormatException");
269         }
270         catch (Exception e) {
271                 Assert(typeof(FormatException) == e.GetType());
272         }
273         //test Parse(string s, IFormatProvider provider)
274         Assert(-42 == Int64.Parse(" -42 ", Nfi));
275         try {
276                 Int64.Parse("%42", Nfi);
277                 Fail("Should raise a System.FormatException");
278         }
279         catch (Exception e) {
280                 Assert(typeof(FormatException) == e.GetType());
281         }
282         //test Parse(string s, NumberStyles style, IFormatProvider provider)
283         Assert(16 == Int64.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
284         try {
285                 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
286                 Fail("Should raise a System.FormatException");
287         }
288         catch (Exception e) {
289                 Assert(typeof(FormatException) == e.GetType());
290         }    
291     }
292
293     public void TestToString() 
294     {
295         string s;
296
297         s = val1.ToString("c", Nfi);
298         Assert("val1 does not become sval1Test6", s.Equals(sval1Test6));
299
300         s = val1.ToString("n", Nfi);
301         AssertEquals("val1 does not become sval1Test7", sval1Test7, s);
302
303         //test ToString()
304         AssertEquals("MyInt64_1.ToString()", MyString1, MyInt64_1.ToString());
305         AssertEquals("MyInt64_2.ToString()", MyString2, MyInt64_2.ToString());
306         AssertEquals("MyInt64_3.ToString()", MyString3, MyInt64_3.ToString());
307         //test ToString(string format)
308         for (int i=0; i < Formats1.Length; i++) {
309                 AssertEquals("MyInt64_2.ToString(Formats1["+i+"])", Results1[i], MyInt64_2.ToString(Formats1[i]));
310                 AssertEquals("MyInt64_3.ToString(Formats2["+i+"])", Results2[i], MyInt64_3.ToString(Formats2[i]));
311         }
312         //test ToString(string format, IFormatProvider provider);
313         for (int i=0; i < Formats1.Length; i++) {
314                 AssertEquals("MyInt64_2.ToString(Formats1["+i+"], Nfi)", ResultsNfi1[i], MyInt64_2.ToString(Formats1[i], Nfi));
315                 AssertEquals("MyInt64_3.ToString(Formats2["+i+"], Nfi)", ResultsNfi2[i], MyInt64_3.ToString(Formats2[i], Nfi));
316         }
317         try {
318                 MyInt64_1.ToString("z");
319                 Fail("Should raise a System.FormatException");
320         }
321         catch (Exception e) {
322                 AssertEquals("Exception is wrong type", typeof(FormatException), e.GetType());
323         }
324     }
325
326     public void TestUserCurrency()
327     {
328         string s= "";
329         long v;
330         int iTest = 1;
331         try {
332                 s = val1.ToString("c", NfiUser);
333                 iTest++;
334                 AssertEquals("Currency value type 1 is not what we want to try to parse", sval1UserCur1, s);
335                 iTest++;
336                 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
337                 iTest++;
338                 Assert(v == val1);
339         } catch (Exception e) {
340                 Fail ("1 Unexpected exception at iTest = " + iTest + ", s = " + s + ":e = " + e);
341         }
342    
343         iTest = 1;
344         try {
345                 s = val2.ToString("c", NfiUser);
346                 iTest++;
347                 AssertEquals("Currency value type 2 is not what we want to try to parse", sval2UserCur1, s);
348                 iTest++;
349                 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
350                 iTest++;
351                 Assert(v == val2);
352         } catch (Exception e) {
353                 Fail ("2 Unexpected exception at iTest = " + iTest + ":e = " + e);
354         }
355     }
356
357     public void TestUserPercent()
358     {
359         string s;
360
361         s = val1.ToString("p", NfiUser);
362         Assert(s.Equals(sval1UserPercent1));
363
364         s = val2.ToString("p", NfiUser);
365         Assert(s.Equals(sval2UserPercent1));
366     }
367 }
368
369 }