Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[mono.git] / mcs / class / corlib / Test / System / UInt32Test.cs
1 // UInt32Test.cs - NUnit Test Cases for the System.UInt32 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 UInt32Test 
18 {
19         private const UInt32 MyUInt32_1 = 42;
20         private const UInt32 MyUInt32_2 = 0;
21         private const UInt32 MyUInt32_3 = 4294967295;
22         private const string MyString1 = "42";
23         private const string MyString2 = "0";
24         private const string MyString3 = "4294967295";
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                                         "4294967295", "4.29497e+009", "4294967295.00000",
35                                         "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
36         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"4,294,967,295.00000",
37                                         "4294967295", "4.29497e+009", "4294967295.00000",
38                                         "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
39         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
40         
41         private CultureInfo old_culture;
42
43         [SetUp]
44         public void SetUp () 
45         {
46                 old_culture = Thread.CurrentThread.CurrentCulture;
47
48                 // Set culture to en-US and don't let the user override.
49                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
50
51                 // We can't initialize this until we set the culture.
52                 
53                 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits);
54                 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern];
55                 
56                 Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "0.00";
57                 Results1 [3] = "0." + decimals;
58                 Results1 [5] = "0." + decimals;
59                 Results1 [6] = perPattern.Replace ("n","0.00");
60                 
61                 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "4,294,967,295.00000";
62                 Results2 [6] = perPattern.Replace ("n","429,496,729,500.00000");
63         }
64
65         [TearDown]
66         public void TearDown ()
67         {
68                 Thread.CurrentThread.CurrentCulture = old_culture;
69         }
70
71         [Test]
72         public void TestMinMax()
73         {
74                 
75                 Assert.AreEqual(UInt32.MinValue, MyUInt32_2);
76                 Assert.AreEqual(UInt32.MaxValue, MyUInt32_3);
77         }
78
79         [Test]
80         public void TestCompareTo()
81         {
82                 Assert.IsTrue(MyUInt32_3.CompareTo(MyUInt32_2) > 0);
83                 Assert.IsTrue(MyUInt32_2.CompareTo(MyUInt32_2) == 0);
84                 Assert.IsTrue(MyUInt32_1.CompareTo((UInt32)(42)) == 0);
85                 Assert.IsTrue(MyUInt32_2.CompareTo(MyUInt32_3) < 0);
86                 Assert.IsTrue (1 == UInt32.Parse ("1"));
87                 Assert.IsTrue (1 == UInt32.Parse (" 1"));
88                 Assert.IsTrue (1 == UInt32.Parse ("     1"));
89                 Assert.IsTrue (1 == UInt32.Parse ("1    "));
90                 Assert.IsTrue (1 == UInt32.Parse ("+1"));
91
92                 try {
93                         UInt32.Parse (" + 1 ");
94                         Assert.Fail ("Should raise FormatException1");
95                 } catch (Exception e){
96                         Assert.IsTrue (typeof (FormatException) == e.GetType ());
97                 }
98
99                 try {
100                         UInt32.Parse (" + ");
101                         Assert.Fail ("Should raise FormatException");
102                 } catch (Exception e){
103                         Assert.IsTrue (typeof (FormatException) == e.GetType ());
104                 }
105                 try {
106                         MyUInt32_2.CompareTo((object)(Int16)100);
107                         Assert.Fail("Should raise a System.ArgumentException");
108                 }
109                 catch (Exception e) {
110                         Assert.IsTrue(typeof(ArgumentException) == e.GetType());
111                 }
112         }
113
114         [Test]
115         public void TestEquals()
116         {
117                 Assert.IsTrue(MyUInt32_1.Equals(MyUInt32_1));
118                 Assert.IsTrue(MyUInt32_1.Equals((object)(UInt32)(42)));
119                 Assert.IsTrue(MyUInt32_1.Equals((object)(SByte)(42)) == false);
120                 Assert.IsTrue(MyUInt32_1.Equals(MyUInt32_2) == false);
121         }
122
123         [Test]
124         public void TestGetHashCode()
125         {
126                 try {
127                         MyUInt32_1.GetHashCode();
128                         MyUInt32_2.GetHashCode();
129                         MyUInt32_3.GetHashCode();
130                 }
131                 catch {
132                         Assert.Fail("GetHashCode should not raise an exception here");
133                 }
134         }
135
136         [Test]
137         public void TestParse()
138         {
139                 //test Parse(string s)
140                 Assert.IsTrue(MyUInt32_1 == UInt32.Parse(MyString1), "Parse problem on \""+MyString1+"\"");
141                 Assert.IsTrue(MyUInt32_2 == UInt32.Parse(MyString2), "Parse problem on \""+MyString2+"\"");
142                 Assert.IsTrue(MyUInt32_3 == UInt32.Parse(MyString3), "Parse problem on \""+MyString3+"\"");
143                 try {
144                         UInt32.Parse(null);
145                         Assert.Fail("Should raise a System.ArgumentNullException");
146                 }
147                 catch (Exception e) {
148                         Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "Did not get ArgumentNullException type");
149                 }
150                 try {
151                         UInt32.Parse("not-a-number");
152                         Assert.Fail("Should raise a System.FormatException");
153                 }
154                 catch (Exception e) {
155                         Assert.IsTrue(typeof(FormatException) == e.GetType(), "Did not get FormatException type");
156                 }
157                 try {
158                         // TODO: Use this after ToString() is completed. For now, hard code string that generates
159                         // exception.
160                         //double OverInt = (double)UInt32.MaxValue + 1;
161                         //UInt32.Parse(OverInt.ToString());
162                         UInt32.Parse("4294967296");
163                         Assert.Fail("Should raise a System.OverflowException");
164                 }
165                 catch (Exception e) {
166                         Assert.IsTrue(typeof(OverflowException) == e.GetType(), "Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e.GetType()+"'");
167                 }
168                 //test Parse(string s, NumberStyles style)
169                 Assert.IsTrue(42 == UInt32.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
170                 try {
171                         UInt32.Parse("$42", NumberStyles.Integer);
172                         Assert.Fail("Should raise a System.FormatException");
173                 }
174                 catch (Exception e) {
175                         Assert.IsTrue(typeof(FormatException) == e.GetType());
176                 }
177                 //test Parse(string s, IFormatProvider provider)
178                 Assert.IsTrue(42 == UInt32.Parse(" 42 ", Nfi));
179                 try {
180                         UInt32.Parse("%42", Nfi);
181                         Assert.Fail("Should raise a System.FormatException");
182                 }
183                 catch (Exception e) {
184                         Assert.IsTrue(typeof(FormatException) == e.GetType());
185                 }
186                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
187                 Assert.IsTrue(16 == UInt32.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
188                 try {
189                         UInt32.Parse("$42", NumberStyles.Integer, Nfi);
190                         Assert.Fail("Should raise a System.FormatException");
191                 }
192                 catch (Exception e) {
193                         Assert.IsTrue(typeof(FormatException) == e.GetType());
194                 }
195                 // Pass a DateTimeFormatInfo, it is unable to format
196                 // numbers, but we should not crash
197                 
198                 UInt32.Parse ("123", new DateTimeFormatInfo ());
199
200                 Assert.AreEqual (734561, UInt32.Parse ("734561\0"), "C#43");
201                 Assert.AreEqual (734561, UInt32.Parse ("734561\0\0\0    \0"), "C#44");
202                 Assert.AreEqual (734561, UInt32.Parse ("734561\0\0\0    "), "C#45");
203                 Assert.AreEqual (734561, UInt32.Parse ("734561\0\0\0"), "C#46");
204
205                 Assert.AreEqual (0, UInt32.Parse ("0+", NumberStyles.Any), "#50");
206         }
207
208         [Test]
209         public void TestParseExponent ()
210         {
211                 Assert.AreEqual (2, uint.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
212                 Assert.AreEqual (20, uint.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
213                 Assert.AreEqual (200, uint.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
214                 Assert.AreEqual (2000000, uint.Parse ("2E6", NumberStyles.AllowExponent), "A#4");
215                 Assert.AreEqual (200, uint.Parse ("2E+2", NumberStyles.AllowExponent), "A#5");
216                 Assert.AreEqual (2, uint.Parse ("2", NumberStyles.AllowExponent), "A#6");
217                 Assert.AreEqual (21, uint.Parse ("2.1E1", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#7");
218                 Assert.AreEqual (520, uint.Parse (".52E3", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#8");
219                 Assert.AreEqual (32500000, uint.Parse ("32.5E6", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#9");
220                 Assert.AreEqual (890, uint.Parse ("8.9000E2", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent), "A#10");            
221
222                 try {
223                         uint.Parse ("2E");
224                         Assert.Fail ("B#1");
225                 } catch (FormatException) {
226                 }
227
228                 try {
229                         uint.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
230                         Assert.Fail ("B#2");
231                 } catch (FormatException) {
232                 }
233
234                 try {
235                         uint.Parse ("2E 2", NumberStyles.AllowExponent);
236                         Assert.Fail ("B#3");
237                 } catch (FormatException) {
238                 }
239
240                 try {
241                         uint.Parse ("2E2 ", NumberStyles.AllowExponent);
242                         Assert.Fail ("B#4");
243                 } catch (FormatException) {
244                 }
245
246                 try {
247                         uint.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
248                         Assert.Fail ("B#5");
249                 } catch (OverflowException) {
250                 }
251
252                 try {
253                         long exponent = (long) Int32.MaxValue + 10;
254                         uint.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
255                         Assert.Fail ("B#6");
256                 } catch (OverflowException) {
257                 }
258
259                 try {
260                         uint.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
261                         Assert.Fail ("B#7");
262                 } catch (OverflowException) {
263                 }
264                 
265                 try {
266                         uint.Parse ("2 math e1", NumberStyles.AllowExponent);
267                         Assert.Fail ("B#8");
268                 } catch (FormatException) {
269                 }
270
271                 try {
272                         uint.Parse ("2.09E1",  NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);
273                         Assert.Fail ("B#9");
274                 } catch (OverflowException) {
275                 }
276         }
277
278         [Test]
279         public void TestTryParse()
280         {
281                 uint result;
282
283                 Assert.AreEqual (true, UInt32.TryParse (MyString1, out result));
284                 Assert.AreEqual (MyUInt32_1, result);
285                 Assert.AreEqual (true, UInt32.TryParse (MyString2, out result));
286                 Assert.AreEqual (MyUInt32_2, result);
287                 Assert.AreEqual (true, UInt32.TryParse (MyString3, out result));
288                 Assert.AreEqual (MyUInt32_3, result);
289
290                 Assert.AreEqual (true, UInt32.TryParse ("1", out result));
291                 Assert.AreEqual (1, result);
292                 Assert.AreEqual (true, UInt32.TryParse (" 1", out result));
293                 Assert.AreEqual (1, result);
294                 Assert.AreEqual (true, UInt32.TryParse ("     1", out result));
295                 Assert.AreEqual (1, result);
296                 Assert.AreEqual (true, UInt32.TryParse ("1    ", out result));
297                 Assert.AreEqual (1, result);
298                 Assert.AreEqual (true, UInt32.TryParse ("+1", out result));
299                 Assert.AreEqual (1, result);
300                 Assert.AreEqual (false, UInt32.TryParse ("-1", out result));
301                 Assert.AreEqual (false, UInt32.TryParse ("  -1", out result));
302                 Assert.AreEqual (false, UInt32.TryParse ("  -1  ", out result));
303                 Assert.AreEqual (false, UInt32.TryParse ("  -1  ", out result));
304
305                 result = 1;
306                 Assert.AreEqual (false, UInt32.TryParse (null, out result));
307                 Assert.AreEqual (0, result);
308
309                 Assert.AreEqual (false, UInt32.TryParse ("not-a-number", out result));
310
311                 double OverInt = (double)UInt32.MaxValue + 1;
312                 Assert.AreEqual (false, UInt32.TryParse (OverInt.ToString (), out result));
313                 Assert.AreEqual (false, UInt32.TryParse (OverInt.ToString (), NumberStyles.None, CultureInfo.InvariantCulture, out result));
314
315                 Assert.AreEqual (false, UInt32.TryParse ("$42", NumberStyles.Integer, null, out result));
316                 Assert.AreEqual (false, UInt32.TryParse ("%42", NumberStyles.Integer, Nfi, out result));
317                 Assert.AreEqual (false, UInt32.TryParse ("$42", NumberStyles.Integer, Nfi, out result));
318                 Assert.AreEqual (false, UInt32.TryParse (" - 1 ", out result));
319                 Assert.AreEqual (false, UInt32.TryParse (" - ", out result));
320                 Assert.AreEqual (true, UInt32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
321                 Assert.AreEqual (false, UInt32.TryParse ("10000000000", out result));
322                 Assert.AreEqual (false, UInt32.TryParse ("-10000000000", out result));
323                 Assert.AreEqual (true, UInt32.TryParse ("7fffffff", NumberStyles.HexNumber, Nfi, out result));
324                 Assert.AreEqual (Int32.MaxValue, result);
325                 Assert.AreEqual (true, UInt32.TryParse ("80000000", NumberStyles.HexNumber, Nfi, out result));
326                 Assert.AreEqual (Int32.MaxValue + (uint)1, result);
327                 Assert.AreEqual (true, UInt32.TryParse ("ffffffff", NumberStyles.HexNumber, Nfi, out result));
328                 Assert.AreEqual (uint.MaxValue, result);
329                 Assert.AreEqual (true, UInt32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
330                 Assert.IsFalse (uint.TryParse ("-", NumberStyles.AllowLeadingSign, Nfi, out result));
331                 Assert.IsFalse (uint.TryParse (Nfi.CurrencySymbol + "-", NumberStyles.AllowLeadingSign | NumberStyles.AllowCurrencySymbol, Nfi, out result));
332         }       
333
334         [Test]
335         public void TestToString()
336         {
337                 int TestNumber = 1;
338                 try {
339                         //test ToString()
340                         Assert.AreEqual(MyString1, MyUInt32_1.ToString());
341                         TestNumber++;
342                         Assert.AreEqual(MyString2, MyUInt32_2.ToString());
343                         TestNumber++;
344                         Assert.AreEqual(MyString3, MyUInt32_3.ToString());
345                 } catch (Exception e) {
346                         Assert.Fail("TestToString: Assert.Failed on TestNumber=" + TestNumber 
347                                 + " with exception: " + e.ToString());
348                 }
349
350                 //test ToString(string format)
351                 for (int i=0; i < Formats1.Length; i++) {
352                         try {
353                                 Assert.AreEqual(Results1[i], MyUInt32_2.ToString(Formats1[i]));
354                         } catch (Exception e) {
355                                 Assert.Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i 
356                                         + ". e = " + e.ToString());
357                         }
358
359                         try {
360                                 Assert.AreEqual(Results2[i], MyUInt32_3.ToString(Formats2[i]));
361                         } catch (Exception e) {
362                                 Assert.Fail("TestToString: MyUInt32_3.ToString(Formats2[i]) i=" + i
363                                         + ". e = " + e.ToString());
364                         }
365                 }
366                 //test ToString(string format, IFormatProvider provider);
367                 for (int i=0; i < Formats1.Length; i++) {
368                         Assert.AreEqual(ResultsNfi1[i], MyUInt32_2.ToString(Formats1[i], Nfi));
369                         Assert.AreEqual(ResultsNfi2[i], MyUInt32_3.ToString(Formats2[i], Nfi));
370                 }
371                 try {
372                         MyUInt32_1.ToString("z");
373                         Assert.Fail("Should raise a System.FormatException");
374                 }
375                 catch (Exception e) {
376                         Assert.IsTrue(typeof(FormatException) == e.GetType());
377                 }
378         }
379
380         [Test]
381         public void ToString_Defaults () 
382         {
383                 UInt32 i = 254;
384                 // everything defaults to "G"
385                 string def = i.ToString ("G");
386                 Assert.AreEqual (def, i.ToString (), "ToString()");
387                 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
388                 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
389                 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
390                 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
391                 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
392
393                 Assert.AreEqual ("254", def, "ToString(G)");
394         }
395 }
396
397
398 }