Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / Test / System / UInt64Test.cs
1 // UInt64Test.cs - NUnit Test Cases for the System.UInt64 struct
2 //
3 // Mario Martinez (mariom925@home.om)
4 //
5 // (C) Ximian, Inc.  http://www.ximian.com
6 // 
7
8 using NUnit.Framework;
9 using System;
10 using System.Globalization;
11 using System.Threading;
12
13 namespace MonoTests.System
14 {
15
16 [TestFixture]
17 public class UInt64Test 
18 {
19         private const UInt64 MyUInt64_1 = 42;
20         private const UInt64 MyUInt64_2 = 0;
21         private const UInt64 MyUInt64_3 = 18446744073709551615;
22         private const string MyString1 = "42";
23         private const string MyString2 = "0";
24         private const string MyString3 = "18446744073709551615";
25         private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
26         private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
27         private string[] Results1 = {"",
28                                         "0", "0.000000e+000", "0.00",
29                                         "0", "0.00", "0.00 %", "0"};
30         private string[] ResultsNfi1 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
31                                         "0", "0.000000e+000", "0.00",
32                                         "0", "0.00", "0.00 %", "0"};
33         private string[] Results2 = {"",
34                                         "18446744073709551615", "1.84467e+019", "18446744073709551615.00000",
35                                         "1.8447e+19", "18,446,744,073,709,551,615.00000",
36                                         "1,844,674,407,370,955,161,500.00000 %", "ffffffffffffffff"};
37         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"18,446,744,073,709,551,615.00000",
38                                         "18446744073709551615", "1.84467e+019", "18446744073709551615.00000",
39                                         "1.8447e+19", "18,446,744,073,709,551,615.00000",
40                                         "1,844,674,407,370,955,161,500.00000 %", "ffffffffffffffff"};
41
42         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
43         
44         private CultureInfo old_culture;
45
46         [SetUp]
47         public void SetUp() 
48         {
49                 old_culture = Thread.CurrentThread.CurrentCulture;
50
51                 // Set culture to en-US and don't let the user override.
52                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
53
54                 // We can't initialize this until we set the culture.
55                 
56                 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
57                 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
58                 
59                 Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "0.00";
60                 Results1 [3] = "0." + decimals;
61                 Results1 [5] = "0." + decimals;
62                 Results1 [6] = perPattern.Replace ("n","0.00");
63                 
64                 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "18,446,744,073,709,551,615.00000";
65                 Results2 [6] = perPattern.Replace ("n","1,844,674,407,370,955,161,500.00000");
66         }
67
68         [TearDown]
69         public void TearDown ()
70         {
71                 Thread.CurrentThread.CurrentCulture = old_culture;
72         }
73
74         [Test]
75         public void TestMinMax()
76         {
77                 Assert.AreEqual(UInt64.MinValue, MyUInt64_2);
78                 Assert.AreEqual(UInt64.MaxValue, MyUInt64_3);
79         }
80
81         [Test]
82         public void TestCompareTo()
83         {
84                 Assert.IsTrue(MyUInt64_3.CompareTo(MyUInt64_2) > 0);
85                 Assert.IsTrue(MyUInt64_2.CompareTo(MyUInt64_2) == 0);
86                 Assert.IsTrue(MyUInt64_1.CompareTo((UInt64)(42)) == 0);
87                 Assert.IsTrue(MyUInt64_2.CompareTo(MyUInt64_3) < 0);
88                 try {
89                         MyUInt64_2.CompareTo((object)(Int16)100);
90                         Assert.Fail("Should raise a System.ArgumentException");
91                 }
92                 catch (Exception e) {
93                         Assert.IsTrue(typeof(ArgumentException) == e.GetType());
94                 }
95         }
96
97         [Test]
98         public void TestEquals()
99         {
100                 Assert.IsTrue(MyUInt64_1.Equals(MyUInt64_1));
101                 Assert.IsTrue(MyUInt64_1.Equals((object)(UInt64)(42)));
102                 Assert.IsTrue(MyUInt64_1.Equals((object)(SByte)(42)) == false);
103                 Assert.IsTrue(MyUInt64_1.Equals(MyUInt64_2) == false);
104         }
105
106         [Test]
107         public void TestGetHashCode()
108         {
109                 try {
110                         MyUInt64_1.GetHashCode();
111                         MyUInt64_2.GetHashCode();
112                         MyUInt64_3.GetHashCode();
113                 }
114                 catch {
115                         Assert.Fail("GetHashCode should not raise an exception here");
116                 }
117         }
118
119         [Test]
120         public void TestParse()
121         {
122                 //test Parse(string s)
123                 Assert.IsTrue(MyUInt64_1 == UInt64.Parse(MyString1));
124                 Assert.IsTrue(MyUInt64_2 == UInt64.Parse(MyString2));
125                 Assert.IsTrue(MyUInt64_3 == UInt64.Parse(MyString3));
126                 try {
127                         UInt64.Parse(null);
128                         Assert.Fail("Should raise a ArgumentNullException");
129                 }
130                 catch (Exception e) {
131                         Assert.IsTrue(typeof(ArgumentNullException) == e.GetType());
132                 }
133                 try {
134                         UInt64.Parse("not-a-number");
135                         Assert.Fail("Should raise a System.FormatException");
136                 }
137                 catch (Exception e) {
138                         Assert.IsTrue(typeof(FormatException) == e.GetType());
139                 }
140                 //test Parse(string s, NumberStyles style)
141                 try {
142                         double OverInt = (double)UInt64.MaxValue + 1;
143                         UInt64.Parse(OverInt.ToString(), NumberStyles.Float);
144                         Assert.Fail("Should raise a OverflowException");
145                 }
146                 catch (Exception e) {
147                         Assert.IsTrue(typeof(OverflowException) == e.GetType());
148                 }
149                 try {
150                         double OverInt = (double)UInt64.MaxValue + 1;
151                         UInt64.Parse(OverInt.ToString(), NumberStyles.Integer);
152                         Assert.Fail("Should raise a System.FormatException");
153                 }
154                 catch (Exception e) {
155                         Assert.IsTrue(typeof(FormatException) == e.GetType());
156                 }
157                 Assert.IsTrue(42 == UInt64.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
158                 try {
159                         UInt64.Parse("$42", NumberStyles.Integer);
160                         Assert.Fail("Should raise a FormatException");
161                 }
162                 catch (Exception e) {
163                         Assert.IsTrue(typeof(FormatException) == e.GetType());
164                 }
165                 //test Parse(string s, IFormatProvider provider)
166                 Assert.IsTrue(42 == UInt64.Parse(" 42 ", Nfi));
167                 try {
168                         UInt64.Parse("%42", Nfi);
169                         Assert.Fail("Should raise a System.FormatException");
170                 }
171                 catch (Exception e) {
172                         Assert.IsTrue(typeof(FormatException) == e.GetType());
173                 }
174                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
175                 Assert.IsTrue(16 == UInt64.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
176                 try {
177                         UInt64.Parse("$42", NumberStyles.Integer, Nfi);
178                         Assert.Fail("Should raise a System.FormatException");
179                 } catch (FormatException e) {
180                 }
181
182                 try {
183                         UInt64.Parse ("5", NumberStyles.Any, CultureInfo.InvariantCulture);
184                         Assert.Fail ("C#42");
185                 } catch (FormatException) {
186                 }
187
188                 // Pass a DateTimeFormatInfo, it is unable to format
189                 // numbers, but we should not crash             
190                 UInt64.Parse ("123", new DateTimeFormatInfo ());
191
192                 Assert.AreEqual (734561, UInt64.Parse ("734561\0"), "C#43");
193                 Assert.AreEqual (734561, UInt64.Parse ("734561\0\0\0    \0"), "C#44");
194                 Assert.AreEqual (734561, UInt64.Parse ("734561\0\0\0    "), "C#45");
195                 Assert.AreEqual (734561, UInt64.Parse ("734561\0\0\0"), "C#46");
196
197                 Assert.AreEqual (0, UInt64.Parse ("0+", NumberStyles.Any), "#50");
198         }
199
200         [Test]
201         public void TestParseExponent ()
202         {
203                 Assert.AreEqual (2, ulong.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
204                 Assert.AreEqual (20, ulong.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
205                 Assert.AreEqual (200, ulong.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
206                 Assert.AreEqual (2000000, ulong.Parse ("2E6", NumberStyles.AllowExponent), "A#4");
207                 Assert.AreEqual (200, ulong.Parse ("2E+2", NumberStyles.AllowExponent), "A#5");
208                 Assert.AreEqual (2, ulong.Parse ("2", NumberStyles.AllowExponent), "A#6");
209                 Assert.AreEqual (21, ulong.Parse ("2.1E1", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#7");
210                 Assert.AreEqual (520, ulong.Parse (".52E3", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#8");
211                 Assert.AreEqual (32500000, ulong.Parse ("32.5E6", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#9");
212                 Assert.AreEqual (890, ulong.Parse ("8.9000E2", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#10");           
213
214                 try {
215                         ulong.Parse ("2E");
216                         Assert.Fail ("B#1");
217                 } catch (FormatException) {
218                 }
219
220                 try {
221                         ulong.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
222                         Assert.Fail ("B#2");
223                 } catch (FormatException) {
224                 }
225
226                 try {
227                         ulong.Parse ("2E 2", NumberStyles.AllowExponent);
228                         Assert.Fail ("B#3");
229                 } catch (FormatException) {
230                 }
231
232                 try {
233                         ulong.Parse ("2E2 ", NumberStyles.AllowExponent);
234                         Assert.Fail ("B#4");
235                 } catch (FormatException) {
236                 }
237
238                 try {
239                         ulong.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
240                         Assert.Fail ("B#5");
241                 } catch (OverflowException) {
242                 }
243
244                 try {
245                         long exponent = (long) Int32.MaxValue + 10;
246                         ulong.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
247                         Assert.Fail ("B#6");
248                 } catch (OverflowException) {
249                 }
250
251                 try {
252                         ulong.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
253                         Assert.Fail ("B#7");
254                 } catch (OverflowException) {
255                 }
256                 
257                 try {
258                         ulong.Parse ("2 math e1", NumberStyles.AllowExponent);
259                         Assert.Fail ("B#8");
260                 } catch (FormatException) {
261                 }
262
263                 try {
264                         ulong.Parse ("2.09E1",  NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);
265                         Assert.Fail ("B#9");
266                 } catch (OverflowException) {
267                 }
268         }
269
270         [Test]
271         public void TestTryParse()
272         {
273                 ulong result;
274
275                 Assert.AreEqual (true, ulong.TryParse (MyString1, out result));
276                 Assert.AreEqual (MyUInt64_1, result);
277                 Assert.AreEqual (true, ulong.TryParse (MyString2, out result));
278                 Assert.AreEqual (MyUInt64_2, result);
279                 Assert.AreEqual (true, ulong.TryParse (MyString3, out result));
280                 Assert.AreEqual (MyUInt64_3, result);
281
282                 Assert.AreEqual (true, ulong.TryParse ("1", out result));
283                 Assert.AreEqual (1, result);
284                 Assert.AreEqual (true, ulong.TryParse (" 1", out result));
285                 Assert.AreEqual (1, result);
286                 Assert.AreEqual (true, ulong.TryParse ("     1", out result));
287                 Assert.AreEqual (1, result);
288                 Assert.AreEqual (true, ulong.TryParse ("1    ", out result));
289                 Assert.AreEqual (1, result);
290                 Assert.AreEqual (true, ulong.TryParse ("+1", out result));
291                 Assert.AreEqual (1, result);
292                 Assert.AreEqual (false, ulong.TryParse ("-1", out result));
293                 Assert.AreEqual (false, ulong.TryParse ("  -1", out result));
294                 Assert.AreEqual (false, ulong.TryParse ("  -1  ", out result));
295                 Assert.AreEqual (false, ulong.TryParse ("  -1  ", out result));
296
297                 result = 1;
298                 Assert.AreEqual (false, ulong.TryParse (null, out result));
299                 Assert.AreEqual (0, result);
300
301                 Assert.AreEqual (false, ulong.TryParse ("not-a-number", out result));
302
303                 double OverInt = (double)ulong.MaxValue + 1;
304                 Assert.AreEqual (false, ulong.TryParse (OverInt.ToString (), out result));
305                 Assert.AreEqual (false, ulong.TryParse (OverInt.ToString (), NumberStyles.None, CultureInfo.InvariantCulture, out result));
306
307                 Assert.AreEqual (false, ulong.TryParse ("$42", NumberStyles.Integer, null, out result));
308                 Assert.AreEqual (false, ulong.TryParse ("%42", NumberStyles.Integer, Nfi, out result));
309                 Assert.AreEqual (false, ulong.TryParse ("$42", NumberStyles.Integer, Nfi, out result));
310                 Assert.AreEqual (false, ulong.TryParse (" - 1 ", out result));
311                 Assert.AreEqual (false, ulong.TryParse (" - ", out result));
312                 Assert.AreEqual (true, ulong.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
313                 Assert.AreEqual (true, ulong.TryParse ("10000000000", out result));
314                 Assert.AreEqual (false, ulong.TryParse ("-10000000000", out result));
315                 Assert.AreEqual (true, ulong.TryParse ("7fffffff", NumberStyles.HexNumber, Nfi, out result));
316                 Assert.AreEqual (int.MaxValue, result);
317                 Assert.AreEqual (true, ulong.TryParse ("80000000", NumberStyles.HexNumber, Nfi, out result));
318                 Assert.AreEqual (2147483648, result);
319                 Assert.AreEqual (true, ulong.TryParse ("ffffffff", NumberStyles.HexNumber, Nfi, out result));
320                 Assert.AreEqual (uint.MaxValue, result);
321                 Assert.AreEqual (true, ulong.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
322                 Assert.IsFalse (ulong.TryParse ("-", NumberStyles.AllowLeadingSign, Nfi, out result));
323                 Assert.IsFalse (ulong.TryParse (Nfi.CurrencySymbol + "-", NumberStyles.AllowLeadingSign | NumberStyles.AllowCurrencySymbol, Nfi, out result));
324         }       
325
326         [Test]
327         public void TestToString()
328         {
329                 //test ToString()
330                 Assert.AreEqual(MyString1, MyUInt64_1.ToString(), "A");
331                 Assert.AreEqual(MyString2, MyUInt64_2.ToString(), "B");
332                 Assert.AreEqual(MyString3, MyUInt64_3.ToString(), "C");
333                 //test ToString(string format)
334                 for (int i=0; i < Formats1.Length; i++) {
335                         Assert.AreEqual(Results1[i], MyUInt64_2.ToString(Formats1[i]), "D");
336                         Assert.AreEqual(Results2[i], MyUInt64_3.ToString(Formats2[i]), "E: format #" + i);
337                 }
338                 //test ToString(string format, IFormatProvider provider);
339                 for (int i=0; i < Formats1.Length; i++) {
340                         Assert.AreEqual(ResultsNfi1[i], MyUInt64_2.ToString(Formats1[i], Nfi), "F");
341                         Assert.AreEqual(ResultsNfi2[i], MyUInt64_3.ToString(Formats2[i], Nfi), "G");
342                 }
343                 try {
344                         MyUInt64_1.ToString("z");
345                         Assert.Fail("Should raise a System.FormatException");
346                 }
347                 catch (Exception e) {
348                         Assert.IsTrue(typeof(FormatException) == e.GetType(), "H");
349                 }
350         }
351
352         [Test]
353         public void ToString_Defaults () 
354         {
355                 UInt64 i = 254;
356                 // everything defaults to "G"
357                 string def = i.ToString ("G");
358                 Assert.AreEqual (def, i.ToString (), "ToString()");
359                 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
360                 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
361                 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
362                 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
363                 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
364
365                 Assert.AreEqual ("254", def, "ToString(G)");
366         }
367 }
368
369 }