2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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 
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         [Test]
113         public void TestMinMax()
114         {
115                 
116                 Assert.AreEqual(Int64.MinValue, MyInt64_2);
117                 Assert.AreEqual(Int64.MaxValue, MyInt64_3);
118         }
119
120         [Test]  
121         public void TestCompareTo()
122         {
123                 Assert.IsTrue(MyInt64_3.CompareTo(MyInt64_2) > 0);
124                 Assert.IsTrue(MyInt64_2.CompareTo(MyInt64_2) == 0);
125                 Assert.IsTrue(MyInt64_1.CompareTo((object)(Int64)(-42)) == 0);
126                 Assert.IsTrue(MyInt64_2.CompareTo(MyInt64_3) < 0);
127                 try {
128                         MyInt64_2.CompareTo((object)(Int16)100);
129                         Assert.Fail("Should raise a System.ArgumentException");
130                 }
131                 catch (Exception e) {
132                         Assert.IsTrue(typeof(ArgumentException) == e.GetType());
133                 }
134         }
135
136         [Test]
137         public void TestEquals()
138         {
139                 Assert.IsTrue(MyInt64_1.Equals(MyInt64_1));
140                 Assert.IsTrue(MyInt64_1.Equals((object)(Int64)(-42)));
141                 Assert.IsTrue(MyInt64_1.Equals((object)(SByte)(-42)) == false);
142                 Assert.IsTrue(MyInt64_1.Equals(MyInt64_2) == false);
143         }
144
145         [Test]  
146         public void TestGetHashCode()
147         {
148                 try {
149                         MyInt64_1.GetHashCode();
150                         MyInt64_2.GetHashCode();
151                         MyInt64_3.GetHashCode();
152                 }
153                 catch {
154                         Assert.Fail("GetHashCode should not raise an exception here");
155                 }
156         }
157
158         [Test]  
159     public void TestRoundTripGeneral() 
160     {
161         foreach(long lv in vals) 
162         {
163             string s = lv.ToString(Nfi);
164             long lv2 = Int64.Parse(s);
165             Assert.IsTrue(lv == lv2);
166             long lv3 = Int64.Parse(s, NumberStyles.Integer, Nfi);
167             Assert.IsTrue(lv == lv3);
168         }
169     }
170
171         [Test]
172     public void TestRoundTripHex() 
173     {
174         foreach(long lv in vals) 
175         {
176             string s = lv.ToString("x", Nfi);
177             long lv2 = Int64.Parse(s, NumberStyles.HexNumber, Nfi);
178             Assert.IsTrue(lv == lv2);
179         }
180     }
181
182         [Test]
183     public void TestParseNull()
184     {
185         try 
186         {
187             Int64.Parse(null);
188             Assert.Fail("Should raise System.ArgumentNullException"); 
189         } 
190         catch (ArgumentNullException) 
191         {
192             // ok
193         }
194     }
195
196         [Test]
197     public void TestParse()
198     {
199         long lv;
200
201         lv = Int64.Parse(sval1Test1, style1, Nfi);
202         Assert.AreEqual(val1, lv, "Long value should be equal for Test1");
203
204         try
205         {
206             lv = Int64.Parse(sval1Test1, Nfi);
207             Assert.Fail("Should raise FormatException 1");
208         }
209         catch (FormatException)
210         {
211             // ok
212         }
213
214         lv = Int64.Parse(sval1Test2, style1, Nfi);
215         Assert.AreEqual(val1, lv, "Value should be the same for Test2 with style1");
216         lv = Int64.Parse(sval1Test2, Nfi);
217         Assert.AreEqual(val1, lv, "Value should be the same for Test2 without style1");
218
219         try
220         {
221             lv = Int64.Parse(sval1Test4, style1, Nfi);
222             Assert.Fail("Should raise FormatException 3");
223         }
224         catch (FormatException)
225         {
226             // ok
227         }
228
229         lv = Int64.Parse(sval1Test5, NumberStyles.Currency, Nfi);
230         Assert.AreEqual(val1, lv, "Value should be the same for Test5 and currency style");
231
232         //test Parse(string s)
233         Assert.IsTrue(MyInt64_1 == Int64.Parse(MyString1));
234         Assert.IsTrue(MyInt64_2 == Int64.Parse(MyString2));
235         Assert.IsTrue(MyInt64_3 == Int64.Parse(MyString3));
236         try {
237                 Int64.Parse(null);
238                 Assert.Fail("#1:Should raise a System.ArgumentNullException");
239         }
240         catch (Exception e) {
241                 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "#2");
242         }
243         try {
244                 Int64.Parse("not-a-number");
245                 Assert.Fail("#3:Should raise a System.FormatException");
246         }
247         catch (Exception e) {
248                 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#4");
249         }
250         //test Parse(string s, NumberStyles style)
251         try {
252                 double OverInt = (double)Int64.MaxValue + 1;
253                 Int64.Parse(OverInt.ToString(), NumberStyles.Float);
254                 Assert.Fail("#5:Should raise a System.OverflowException");
255         }
256         catch (Exception e) {
257                 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#6");
258         }
259         try {
260                 Int64.Parse("10000000000000000", NumberStyles.HexNumber);
261                 Assert.Fail("#7:Should raise a System.OverflowException");
262         }
263         catch (Exception e) {
264                 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#8");
265         }
266         try {
267                 double OverInt = (double)Int64.MaxValue + 1;
268                 Int64.Parse(OverInt.ToString(), NumberStyles.Integer);
269                 Assert.Fail("#9:Should raise a System.FormatException");
270         }
271         catch (Exception e) {
272                 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#10");
273         }
274         Assert.AreEqual((long)42, Int64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency), "A1");
275         try {
276                 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
277                 Assert.Fail("#11:Should raise a System.FormatException");
278         }
279         catch (Exception e) {
280                 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#12");
281         }
282         //test Parse(string s, IFormatProvider provider)
283         Assert.IsTrue(-42 == Int64.Parse(" -42 ", Nfi), "A2");
284         try {
285                 Int64.Parse("%42", Nfi);
286                 Assert.Fail("#13:Should raise a System.FormatException");
287         }
288         catch (Exception e) {
289                 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#14");
290         }
291         //test Parse(string s, NumberStyles style, IFormatProvider provider)
292         Assert.IsTrue(16 == Int64.Parse(" 10 ", NumberStyles.HexNumber, Nfi), "A3");
293         try {
294                 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
295                 Assert.Fail("#15:Should raise a System.FormatException");
296         }
297         catch (Exception e) {
298                 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#16");
299         }
300         try {
301                 long.Parse ("9223372036854775808");
302                 Assert.Fail ("#17:should raise an OverflowException");
303         } catch (Exception e) {
304                 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#18");
305         }
306         try {
307                 long.Parse ("9223372036854775808", CultureInfo.InvariantCulture);
308                 Assert.Fail ("#19:should raise an OverflowException");
309         } catch (Exception e) {
310                 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#20");
311         }
312     }
313
314         [Test]
315     public void TestToString() 
316     {
317         string s;
318
319         s = val1.ToString("c", Nfi);
320         Assert.IsTrue(s.Equals(sval1Test6), "val1 does not become sval1Test6");
321
322         s = val1.ToString("n", Nfi);
323         Assert.AreEqual(sval1Test7, s, "val1 does not become sval1Test7");
324
325         //test ToString()
326         Assert.AreEqual(MyString1, MyInt64_1.ToString(), "MyInt64_1.ToString()");
327         Assert.AreEqual(MyString2, MyInt64_2.ToString(), "MyInt64_2.ToString()");
328         Assert.AreEqual(MyString3, MyInt64_3.ToString(), "MyInt64_3.ToString()");
329         //test ToString(string format)
330         for (int i=0; i < Formats1.Length; i++) {
331                 Assert.AreEqual(Results1[i], MyInt64_2.ToString(Formats1[i]), "MyInt64_2.ToString(Formats1["+i+"])");
332                 Assert.AreEqual(Results2[i], MyInt64_3.ToString(Formats2[i]), "MyInt64_3.ToString(Formats2["+i+"])");
333         }
334         //test ToString(string format, IFormatProvider provider);
335         for (int i=0; i < Formats1.Length; i++) {
336                 Assert.AreEqual(ResultsNfi1[i], MyInt64_2.ToString(Formats1[i], Nfi), "MyInt64_2.ToString(Formats1["+i+"], Nfi)");
337                 Assert.AreEqual(ResultsNfi2[i], MyInt64_3.ToString(Formats2[i], Nfi), "MyInt64_3.ToString(Formats2["+i+"], Nfi)");
338         }
339         try {
340                 MyInt64_1.ToString("z");
341                 Assert.Fail("Should raise a System.FormatException");
342         }
343         catch (Exception e) {
344                 Assert.AreEqual(typeof(FormatException), e.GetType(), "Exception is wrong type");
345         }
346     }
347
348         [Test]
349     public void TestUserCurrency()
350     {
351         string s= "";
352         long v;
353         int iTest = 1;
354         try {
355                 s = val1.ToString("c", NfiUser);
356                 iTest++;
357                 Assert.AreEqual(sval1UserCur1, s, "Currency value type 1 is not what we want to try to parse");
358                 iTest++;
359                 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
360                 iTest++;
361                 Assert.IsTrue(v == val1);
362         } catch (Exception e) {
363                 Assert.Fail ("1 Unexpected exception at iTest = " + iTest + ", s = " + s + ":e = " + e);
364         }
365    
366         iTest = 1;
367         try {
368                 s = val2.ToString("c", NfiUser);
369                 iTest++;
370                 Assert.AreEqual(sval2UserCur1, s, "Currency value type 2 is not what we want to try to parse");
371                 iTest++;
372                 v = Int64.Parse(s, NumberStyles.Currency, NfiUser);
373                 iTest++;
374                 Assert.IsTrue(v == val2);
375         } catch (Exception e) {
376                 Assert.Fail ("2 Unexpected exception at iTest = " + iTest + ":e = " + e);
377         }
378     }
379
380         [Test]
381     public void TestUserPercent()
382     {
383         string s;
384
385         s = val1.ToString("p", NfiUser);
386         Assert.IsTrue(s.Equals(sval1UserPercent1));
387
388         s = val2.ToString("p", NfiUser);
389         Assert.IsTrue(s.Equals(sval2UserPercent1));
390     }
391
392                 [Test]
393                 public void Parse_MaxValue ()
394                 {
395                         Assert.AreEqual (Int64.MaxValue, Int64.Parse ("9223372036854775807"), "9223372036854775807");
396                 }
397
398                 [Test]
399                 public void Parse_MinValue ()
400                 {
401                         Assert.AreEqual (Int64.MinValue, Int64.Parse ("-9223372036854775808"), "-9223372036854775808,10");
402                 }
403
404                 [Test]
405                 [ExpectedException (typeof (OverflowException))]
406                 public void Parse_OverByOneMaxValue ()
407                 {
408                         Int64.Parse ("9223372036854775808");
409                 }
410
411                 [Test]
412                 [ExpectedException (typeof (OverflowException))]
413                 public void Parse_WayOverMaxValue ()
414                 {
415                         Int64.Parse ("1" + Int64.MaxValue.ToString ());
416                 }
417
418                 [Test]
419                 [ExpectedException (typeof (OverflowException))]
420                 public void Parse_OverByOneMinValue ()
421                 {
422                         Int64.Parse ("-9223372036854775809");
423                 }
424
425                 [Test]
426                 [ExpectedException (typeof (OverflowException))]
427                 public void Parse_WayOverMinValue ()
428                 {
429                         Int64.Parse (Int64.MinValue.ToString () + "1");
430                 }
431
432                 [Test]
433                 public void ToString_Defaults () 
434                 {
435                         Int64 i = 254;
436                         // everything defaults to "G"
437                         string def = i.ToString ("G");
438                         Assert.AreEqual (def, i.ToString (), "ToString()");
439                         Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
440                         Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
441                         Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
442                         Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
443                         Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
444
445                         Assert.AreEqual ("254", def, "ToString(G)");
446                 }
447         }
448 }