Merge pull request #601 from knocte/sock_improvements
[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 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;
61
62         private CultureInfo old_culture;
63
64         [SetUp]
65         public void SetUp () 
66         {
67                 old_culture = Thread.CurrentThread.CurrentCulture;
68
69                 // Set culture to en-US and don't let the user override.
70                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
71
72                 int cdd = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
73                 string csym = NumberFormatInfo.CurrentInfo.CurrencySymbol;
74                 string csuffix = (cdd > 0 ? "." : "").PadRight(cdd + (cdd > 0 ? 1 : 0), '0');
75                 
76                 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
77                 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
78                 
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");
83                 
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");
86                 
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 = "%%%";
102         }
103
104         [TearDown]
105         public void TearDown ()
106         {
107                 Thread.CurrentThread.CurrentCulture = old_culture;
108         }
109
110         [Test]
111         public void TestMinMax()
112         {
113                 
114                 Assert.AreEqual(Int64.MinValue, MyInt64_2);
115                 Assert.AreEqual(Int64.MaxValue, MyInt64_3);
116         }
117
118         [Test]  
119         public void TestCompareTo()
120         {
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);
125                 try {
126                         MyInt64_2.CompareTo((object)(Int16)100);
127                         Assert.Fail("Should raise a System.ArgumentException");
128                 }
129                 catch (Exception e) {
130                         Assert.IsTrue(typeof(ArgumentException) == e.GetType());
131                 }
132         }
133
134         [Test]
135         public void TestEquals()
136         {
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);
141         }
142
143         [Test]  
144         public void TestGetHashCode()
145         {
146                 try {
147                         MyInt64_1.GetHashCode();
148                         MyInt64_2.GetHashCode();
149                         MyInt64_3.GetHashCode();
150                 }
151                 catch {
152                         Assert.Fail("GetHashCode should not raise an exception here");
153                 }
154         }
155
156         [Test]  
157     public void TestRoundTripGeneral() 
158     {
159         foreach(long lv in vals) 
160         {
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);
166         }
167     }
168
169         [Test]
170     public void TestRoundTripHex() 
171     {
172         foreach(long lv in vals) 
173         {
174             string s = lv.ToString("x", Nfi);
175             long lv2 = Int64.Parse(s, NumberStyles.HexNumber, Nfi);
176             Assert.IsTrue(lv == lv2);
177         }
178     }
179
180         [Test]
181     public void TestParseNull()
182     {
183         try 
184         {
185             Int64.Parse(null);
186             Assert.Fail("Should raise System.ArgumentNullException"); 
187         } 
188         catch (ArgumentNullException) 
189         {
190             // ok
191         }
192     }
193
194         [Test]
195     public void TestParse()
196     {
197         long lv;
198
199         lv = Int64.Parse(sval1Test1, style1, Nfi);
200         Assert.AreEqual(val1, lv, "Long value should be equal for Test1");
201
202         try
203         {
204             lv = Int64.Parse(sval1Test1, Nfi);
205             Assert.Fail("Should raise FormatException 1");
206         }
207         catch (FormatException)
208         {
209             // ok
210         }
211
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");
216
217         try
218         {
219             lv = Int64.Parse(sval1Test4, style1, Nfi);
220             Assert.Fail("Should raise FormatException 3");
221         }
222         catch (FormatException)
223         {
224             // ok
225         }
226
227         lv = Int64.Parse(sval1Test5, NumberStyles.Currency, Nfi);
228         Assert.AreEqual(val1, lv, "Value should be the same for Test5 and currency style");
229
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));
234         try {
235                 Int64.Parse(null);
236                 Assert.Fail("#1:Should raise a System.ArgumentNullException");
237         }
238         catch (Exception e) {
239                 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "#2");
240         }
241         try {
242                 Int64.Parse("not-a-number");
243                 Assert.Fail("#3:Should raise a System.FormatException");
244         }
245         catch (Exception e) {
246                 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#4");
247         }
248         //test Parse(string s, NumberStyles style)
249         try {
250                 double OverInt = (double)Int64.MaxValue + 1;
251                 Int64.Parse(OverInt.ToString(), NumberStyles.Float);
252                 Assert.Fail("#5:Should raise a System.OverflowException");
253         }
254         catch (Exception e) {
255                 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#6");
256         }
257         try {
258                 Int64.Parse("10000000000000000", NumberStyles.HexNumber);
259                 Assert.Fail("#7:Should raise a System.OverflowException");
260         }
261         catch (Exception e) {
262                 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#8");
263         }
264         try {
265                 double OverInt = (double)Int64.MaxValue + 1;
266                 Int64.Parse(OverInt.ToString(), NumberStyles.Integer);
267                 Assert.Fail("#9:Should raise a System.FormatException");
268         }
269         catch (Exception e) {
270                 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#10");
271         }
272         Assert.AreEqual((long)42, Int64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency), "A1");
273         try {
274                 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer);
275                 Assert.Fail("#11:Should raise a System.FormatException");
276         }
277         catch (Exception e) {
278                 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#12");
279         }
280         //test Parse(string s, IFormatProvider provider)
281         Assert.IsTrue(-42 == Int64.Parse(" -42 ", Nfi), "A2");
282         try {
283                 Int64.Parse("%42", Nfi);
284                 Assert.Fail("#13:Should raise a System.FormatException");
285         }
286         catch (Exception e) {
287                 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#14");
288         }
289         //test Parse(string s, NumberStyles style, IFormatProvider provider)
290         Assert.IsTrue(16 == Int64.Parse(" 10 ", NumberStyles.HexNumber, Nfi), "A3");
291         try {
292                 Int64.Parse(NumberFormatInfo.CurrentInfo.CurrencySymbol+"42", NumberStyles.Integer, Nfi);
293                 Assert.Fail("#15:Should raise a System.FormatException");
294         }
295         catch (Exception e) {
296                 Assert.IsTrue(typeof(FormatException) == e.GetType(), "#16");
297         }
298         try {
299                 long.Parse ("9223372036854775808");
300                 Assert.Fail ("#17:should raise an OverflowException");
301         } catch (Exception e) {
302                 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "#18");
303         }
304         try {
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");
309         }
310
311         // Pass a DateTimeFormatInfo, it is unable to format
312         // numbers, but we should not crash
313         
314         Int64.Parse ("123", new DateTimeFormatInfo ());
315         
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");
320
321         Assert.AreEqual (0, Int64.Parse ("0+", NumberStyles.Any), "#30");
322     }
323
324         [Test]
325         public void TestParseExponent ()
326         {
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");            
337
338                 try {
339                         long.Parse ("2E");
340                         Assert.Fail ("B#1");
341                 } catch (FormatException) {
342                 }
343
344                 try {
345                         long.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
346                         Assert.Fail ("B#2");
347                 } catch (FormatException) {
348                 }
349
350                 try {
351                         long.Parse ("2E 2", NumberStyles.AllowExponent);
352                         Assert.Fail ("B#3");
353                 } catch (FormatException) {
354                 }
355
356                 try {
357                         long.Parse ("2E2 ", NumberStyles.AllowExponent);
358                         Assert.Fail ("B#4");
359                 } catch (FormatException) {
360                 }
361
362                 try {
363                         long.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
364                         Assert.Fail ("B#5");
365                 } catch (OverflowException) {
366                 }
367
368                 try {
369                         long exponent = (long) Int32.MaxValue + 10;
370                         long.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
371                         Assert.Fail ("B#6");
372                 } catch (OverflowException) {
373                 }
374
375                 try {
376                         long.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
377                         Assert.Fail ("B#7");
378                 } catch (OverflowException) {
379                 }
380                 
381                 try {
382                         long.Parse ("2 math e1", NumberStyles.AllowExponent);
383                         Assert.Fail ("B#8");
384                 } catch (FormatException) {
385                 }
386
387                 try {
388                         long.Parse ("2.09E1",  NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);
389                         Assert.Fail ("B#9");
390                 } catch (OverflowException) {
391                 }
392         }
393
394         [Test]
395     public void TestToString() 
396     {
397         string s;
398
399         s = val1.ToString("c", Nfi);
400         Assert.IsTrue(s.Equals(sval1Test6), "val1 does not become sval1Test6");
401
402         s = val1.ToString("n", Nfi);
403         Assert.AreEqual(sval1Test7, s, "val1 does not become sval1Test7");
404
405         //test ToString()
406         Assert.AreEqual(MyString1, MyInt64_1.ToString(), "MyInt64_1.ToString()");
407         Assert.AreEqual(MyString2, MyInt64_2.ToString(), "MyInt64_2.ToString()");
408         Assert.AreEqual(MyString3, MyInt64_3.ToString(), "MyInt64_3.ToString()");
409         //test ToString(string format)
410         for (int i=0; i < Formats1.Length; i++) {
411                 Assert.AreEqual(Results1[i], MyInt64_2.ToString(Formats1[i]), "MyInt64_2.ToString(Formats1["+i+"])");
412                 Assert.AreEqual(Results2[i], MyInt64_3.ToString(Formats2[i]), "MyInt64_3.ToString(Formats2["+i+"])");
413         }
414         //test ToString(string format, IFormatProvider provider);
415         for (int i=0; i < Formats1.Length; i++) {
416                 Assert.AreEqual(ResultsNfi1[i], MyInt64_2.ToString(Formats1[i], Nfi), "MyInt64_2.ToString(Formats1["+i+"], Nfi)");
417                 Assert.AreEqual(ResultsNfi2[i], MyInt64_3.ToString(Formats2[i], Nfi), "MyInt64_3.ToString(Formats2["+i+"], Nfi)");
418         }
419         try {
420                 MyInt64_1.ToString("z");
421                 Assert.Fail("Should raise a System.FormatException");
422         }
423         catch (Exception e) {
424                 Assert.AreEqual(typeof(FormatException), e.GetType(), "Exception is wrong type");
425         }
426     }
427
428         [Test]
429         public void TestUserCurrency ()
430         {
431                 string s = "";
432                 long v;
433                 s = val1.ToString ("c", NfiUser);
434                 Assert.AreEqual ("1234/5/67:000 XYZ-", s, "Currency value type 1 is not what we want to try to parse");
435                 v = Int64.Parse ("1234/5/67:000   XYZ-", NumberStyles.Currency, NfiUser);
436                 Assert.AreEqual (val1, v);
437
438                 s = val2.ToString ("c", NfiUser);
439                 Assert.AreEqual ("1234/5/67:000 XYZ", s, "Currency value type 2 is not what we want to try to parse");
440                 v = Int64.Parse (s, NumberStyles.Currency, NfiUser);
441                 Assert.AreEqual (val2, v);
442         }
443
444         [Test]
445     public void TestUserPercent()
446     {
447         string s;
448
449         s = val1.ToString("p", NfiUser);
450         Assert.IsTrue(s.Equals(sval1UserPercent1));
451
452         s = val2.ToString("p", NfiUser);
453         Assert.IsTrue(s.Equals(sval2UserPercent1));
454     }
455
456                 [Test]
457                 public void Parse_MaxValue ()
458                 {
459                         Assert.AreEqual (Int64.MaxValue, Int64.Parse ("9223372036854775807"), "9223372036854775807");
460                 }
461
462                 [Test]
463                 public void Parse_MinValue ()
464                 {
465                         Assert.AreEqual (Int64.MinValue, Int64.Parse ("-9223372036854775808"), "-9223372036854775808,10");
466                 }
467
468                 [Test]
469                 [ExpectedException (typeof (OverflowException))]
470                 public void Parse_OverByOneMaxValue ()
471                 {
472                         Int64.Parse ("9223372036854775808");
473                 }
474
475                 [Test]
476                 [ExpectedException (typeof (OverflowException))]
477                 public void Parse_WayOverMaxValue ()
478                 {
479                         Int64.Parse ("1" + Int64.MaxValue.ToString ());
480                 }
481
482                 [Test]
483                 [ExpectedException (typeof (OverflowException))]
484                 public void Parse_OverByOneMinValue ()
485                 {
486                         Int64.Parse ("-9223372036854775809");
487                 }
488
489                 [Test]
490                 [ExpectedException (typeof (OverflowException))]
491                 public void Parse_WayOverMinValue ()
492                 {
493                         Int64.Parse (Int64.MinValue.ToString () + "1");
494                 }
495
496                 [Test]
497                 public void ToString_Defaults () 
498                 {
499                         Int64 i = 254;
500                         // everything defaults to "G"
501                         string def = i.ToString ("G");
502                         Assert.AreEqual (def, i.ToString (), "ToString()");
503                         Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
504                         Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
505                         Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
506                         Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
507                         Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
508
509                         Assert.AreEqual ("254", def, "ToString(G)");
510                 }
511         }
512 }