Merge remote branch 'upstream/master'
[mono.git] / mcs / class / corlib / Test / System / Int32Test.cs
1 // Int32Test.cs - NUnit Test Cases for the System.Int32 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.Threading;
11 using System.Globalization;
12
13 namespace MonoTests.System
14 {
15
16 [TestFixture]
17 public class Int32Test 
18 {
19         private const Int32 MyInt32_1 = -42;
20         private const Int32 MyInt32_2 = -2147483648;
21         private const Int32 MyInt32_3 = 2147483647;
22         private const string MyString1 = "-42";
23         private const string MyString2 = "-2147483648";
24         private const string MyString3 = "2147483647";
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 = {null,
28                                         "-2147483648", "-2.147484e+009", "-2147483648.00",
29                                         "-2147483648", "-2,147,483,648.00", "-214,748,364,800.00 %", "80000000"};
30         private string[] Results2 = {null,
31                                         "2147483647", "2.14748e+009", "2147483647.00000",
32                                         "2.1475e+09", "2,147,483,647.00000", "214,748,364,700.00000 %", "7fffffff"};
33         private string[] ResultsNfi1 = {"("+NumberFormatInfo.InvariantInfo.CurrencySymbol+"2,147,483,648.00)",
34                                         "-2147483648", "-2.147484e+009", "-2147483648.00",
35                                         "-2147483648", "-2,147,483,648.00", "-214,748,364,800.00 %", "80000000"};
36         private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"2,147,483,647.00000",
37                                         "2147483647", "2.14748e+009", "2147483647.00000",
38                                         "2.1475e+09", "2,147,483,647.00000", "214,748,364,700.00000 %", "7fffffff"};
39         private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
40         
41         private CultureInfo old_culture;
42
43         [TestFixtureSetUp]
44         public void SetUpFixture() 
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+"2,147,483,648.00)";
57                 Results1 [3] = "-2147483648." + decimals;
58                 Results1 [5] = "-2,147,483,648." + decimals;
59                 Results1 [6] = perPattern.Replace ("n","-214,748,364,800.00");
60                 
61                 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"2,147,483,647.00000";
62                 Results2 [6] = perPattern.Replace ("n","214,748,364,700.00000");
63         }
64         
65         [SetUp]
66         public void Setup ()
67         {
68                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
69         }
70
71         [TestFixtureTearDown]
72         public void TearDown()
73         {
74                 Thread.CurrentThread.CurrentCulture = old_culture;
75         }
76
77         [Test]
78         public void TestMinMax()
79         {
80                 
81                 Assert.AreEqual(Int32.MinValue, MyInt32_2, "#A01");
82                 Assert.AreEqual(Int32.MaxValue, MyInt32_3, "#A02");
83         }
84
85         [Test]  
86         public void TestCompareTo()
87         {
88                 Assert.IsTrue(MyInt32_3.CompareTo(MyInt32_2) > 0, "MyInt32_3.CompareTo(MyInt32_2) > 0");
89                 Assert.IsTrue(MyInt32_2.CompareTo(MyInt32_2) == 0, "MyInt32_2.CompareTo(MyInt32_2) == 0");
90                 Assert.IsTrue(MyInt32_1.CompareTo((object)(Int32)(-42)) == 0, "MyInt32_1.CompareTo((Int32)(-42)) == 0");
91                 Assert.IsTrue(MyInt32_2.CompareTo(MyInt32_3) < 0, "MyInt32_2.CompareTo(MyInt32_3) < 0");
92                 try {
93                         MyInt32_2.CompareTo((object)(Int16)100);
94                         Assert.Fail("Should raise a System.ArgumentException");
95                 }
96                 catch (Exception e) {
97                         Assert.IsTrue(typeof(ArgumentException) == e.GetType(), "typeof(ArgumentException) == e.GetType()");
98                 }
99         }
100
101         [Test]
102         public void TestEquals()
103         {
104                 Assert.IsTrue (MyInt32_1.Equals (MyInt32_1), "#B01");
105                 Assert.IsTrue (MyInt32_1.Equals ((Int32)(-42)), "#B02");
106                 Assert.IsTrue (MyInt32_1.Equals ((object)(SByte)(-42)) == false, "#B03");
107                 Assert.IsTrue (MyInt32_1.Equals (MyInt32_2) == false, "#B04");
108         }
109
110         [Test]  
111         public void TestGetHashCode()
112         {
113                 try {
114                         MyInt32_1.GetHashCode();
115                         MyInt32_2.GetHashCode();
116                         MyInt32_3.GetHashCode();
117                 }
118                 catch {
119                         Assert.Fail("GetHashCode should not raise an exception here");
120                 }
121         }
122
123         [Test]  
124         public void TestParse()
125         {
126                 //test Parse(string s)
127                 Assert.AreEqual (MyInt32_1, Int32.Parse (MyString1), "#C01");
128                 Assert.AreEqual (MyInt32_2, Int32.Parse (MyString2), "#C02");
129                 Assert.AreEqual (MyInt32_3, Int32.Parse (MyString3), "#C03");
130
131                 Assert.AreEqual (1, Int32.Parse ("1"), "#C04");
132                 Assert.AreEqual (1, Int32.Parse (" 1"), "#C05");
133                 Assert.AreEqual (1, Int32.Parse ("     1"), "#C06");
134                 Assert.AreEqual (1, Int32.Parse ("1    "), "#C07");
135                 Assert.AreEqual (1, Int32.Parse ("+1"), "#C08");
136                 Assert.AreEqual (-1, Int32.Parse ("-1"), "#C09");
137                 Assert.AreEqual (-1, Int32.Parse ("  -1"), "#C10");
138                 Assert.AreEqual (-1, Int32.Parse ("  -1  "), "#C11");
139                 Assert.AreEqual (-1, Int32.Parse ("  -1  "), "#C12");
140
141                 try {
142                         Int32.Parse(null);
143                         Assert.Fail ("#C13: Should raise a System.ArgumentNullException");
144                 }
145                 catch (Exception e) {
146                         Assert.IsTrue (typeof (ArgumentNullException) == e.GetType(), "#C14");
147                 }
148                 try {
149                         Int32.Parse("not-a-number");
150                         Assert.Fail ("#C15: Should raise a System.FormatException");
151                 }
152                 catch (Exception e) {
153                         Assert.IsTrue (typeof (FormatException) == e.GetType(), "#C16");
154                 }
155                 try {
156                         double OverInt = (double)Int32.MaxValue + 1;
157                         Int32.Parse(OverInt.ToString());
158                         Assert.Fail ("#C17: Should raise a System.OverflowException");
159                 }
160                 catch (Exception e) {
161                         Assert.AreEqual (typeof (OverflowException), e.GetType(), "#C18");
162                 }
163                 //test Parse(string s, NumberStyles style)
164                 Assert.AreEqual (42, Int32.Parse (" $42 ", NumberStyles.Currency), "#C19");
165                 try {
166                         Int32.Parse("$42", NumberStyles.Integer);
167                         Assert.Fail ("#C20: Should raise a System.FormatException");
168                 }
169                 catch (Exception e) {
170                         Assert.IsTrue (typeof (FormatException) == e.GetType(), "#C21");
171                 }
172                 //test Parse(string s, IFormatProvider provider)
173                 Assert.AreEqual (-42, Int32.Parse (" -42 ", Nfi), "#C22");
174                 try {
175                         Int32.Parse("%42", Nfi);
176                         Assert.Fail ("#C23: Should raise a System.FormatException");
177                 }
178                 catch (Exception e) {
179                         Assert.IsTrue (typeof (FormatException) == e.GetType(), "#C24");
180                 }
181                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
182                 Assert.AreEqual (16, Int32.Parse (" 10 ", NumberStyles.HexNumber, Nfi), "#C25");
183                 try {
184                         Int32.Parse("$42", NumberStyles.Integer, Nfi);
185                         Assert.Fail ("#C26: Should raise a System.FormatException");
186                 }
187                 catch (Exception e) {
188                         Assert.IsTrue(typeof (FormatException) == e.GetType(), "#C27");
189                 }
190
191                 try {
192                         Int32.Parse (" - 1 ");
193                         Assert.Fail ("#C28: Should raise FormatException");
194                 } catch (Exception e){
195                         Assert.IsTrue (typeof (FormatException) == e.GetType (), "#C29");
196                 }
197
198                 try {
199                         Int32.Parse (" - ");
200                         Assert.Fail ("#C30: Should raise FormatException");
201                 } catch (Exception e){
202                         Assert.IsTrue (typeof (FormatException) == e.GetType (), "#C31");
203                 }
204                 Assert.AreEqual (-123, Int32.Parse ("ffffff85", NumberStyles.HexNumber, Nfi), "#C32");
205                 try {
206                         Int32.Parse ("100000000", NumberStyles.HexNumber, Nfi);
207                         Assert.Fail ("#C33: Should raise OverflowException");
208                 } catch (Exception e){
209                         Assert.IsTrue (typeof (OverflowException) == e.GetType (), "#C34");
210                 }
211                 try {
212                         Int32.Parse ("2147483648");
213                         Assert.Fail ("C#35: should raise OverflowException");
214                 } catch (Exception e) {
215                         Assert.IsTrue (typeof (OverflowException) == e.GetType (), "C#36");
216                 }
217                 try {
218                         Int32.Parse ("2147483648", CultureInfo.InvariantCulture);
219                         Assert.Fail ("C#37: should raise OverflowException");
220                 } catch (Exception e) {
221                         Assert.IsTrue (typeof (OverflowException) == e.GetType (), "C#38");
222                 }
223
224                 try {
225                         Int32.Parse (null);
226                         Assert.Fail ("C#39: Should raise an ArgumentNullException");
227                 } catch (Exception e){
228                         Assert.IsTrue (typeof (ArgumentNullException) == e.GetType (), "C#40");
229                 }
230
231                 try {
232                         Int32.Parse ("123", (NumberStyles) 60000);
233                         Assert.Fail ("C#41 Should raise an ArgumentException");
234                 } catch (Exception e){
235                         Assert.IsTrue (typeof (ArgumentException) == e.GetType (), "C#42");
236                 }
237
238                 // Pass a DateTimeFormatInfo, it is unable to format
239                 // numbers, but we should not crash
240                 
241                 Int32.Parse ("123", new DateTimeFormatInfo ());
242
243                 Assert.AreEqual (734561, Int32.Parse ("734561\0"), "C#43");
244                 Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0    \0"), "C#44");
245                 Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0    "), "C#45");
246                 Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0"), "C#46");
247         }
248
249         [Test]
250         public void TestParseExponent ()
251         {
252                 Assert.AreEqual (2, Int32.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
253                 Assert.AreEqual (20, Int32.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
254                 Assert.AreEqual (200, Int32.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
255                 Assert.AreEqual (2000000, Int32.Parse ("2E6", NumberStyles.AllowExponent), "A#4");
256                 Assert.AreEqual (200, Int32.Parse ("2E+2", NumberStyles.AllowExponent), "A#5");
257
258                 try {
259                         Int32.Parse ("2E");
260                         Assert.Fail ("B#1");
261                 } catch (FormatException) {
262                 }
263
264                 try {
265                         Int32.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
266                         Assert.Fail ("B#2");
267                 } catch (FormatException) {
268                 }
269
270                 try {
271                         Int32.Parse ("2E 2", NumberStyles.AllowExponent);
272                         Assert.Fail ("B#3");
273                 } catch (FormatException) {
274                 }
275
276                 try {
277                         Int32.Parse ("2E2 ", NumberStyles.AllowExponent);
278                         Assert.Fail ("B#4");
279                 } catch (FormatException) {
280                 }
281
282                 try {
283                         Int32.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
284                         Assert.Fail ("B#5");
285                 } catch (OverflowException) {
286                 }
287
288                 try {
289                         long exponent = (long)Int32.MaxValue + 10;
290                         Int32.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
291                         Assert.Fail ("B#6");
292                 } catch (OverflowException) {
293                 }
294
295                 try {
296                         Int32.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
297                         Assert.Fail ("B#7");
298                 } catch (OverflowException) {
299                 }
300         }
301
302 #if NET_2_0     
303         [Test]
304         public void TestTryParse()
305         {
306                 int result;
307
308                 Assert.AreEqual (true, Int32.TryParse (MyString1, out result));
309                 Assert.AreEqual (MyInt32_1, result);
310                 Assert.AreEqual (true, Int32.TryParse (MyString2, out result));
311                 Assert.AreEqual (MyInt32_2, result);
312                 Assert.AreEqual (true, Int32.TryParse (MyString3, out result));
313                 Assert.AreEqual (MyInt32_3, result);
314
315                 Assert.AreEqual (true, Int32.TryParse ("1", out result));
316                 Assert.AreEqual (1, result);
317                 Assert.AreEqual (true, Int32.TryParse (" 1", out result));
318                 Assert.AreEqual (1, result);
319                 Assert.AreEqual (true, Int32.TryParse ("     1", out result));
320                 Assert.AreEqual (1, result);
321                 Assert.AreEqual (true, Int32.TryParse ("1    ", out result));
322                 Assert.AreEqual (1, result);
323                 Assert.AreEqual (true, Int32.TryParse ("+1", out result));
324                 Assert.AreEqual (1, result);
325                 Assert.AreEqual (true, Int32.TryParse ("-1", out result));
326                 Assert.AreEqual (-1, result);
327                 Assert.AreEqual (true, Int32.TryParse ("  -1", out result));
328                 Assert.AreEqual (-1, result);
329                 Assert.AreEqual (true, Int32.TryParse ("  -1  ", out result));
330                 Assert.AreEqual (-1, result);
331                 Assert.AreEqual (true, Int32.TryParse ("  -1  ", out result));
332                 Assert.AreEqual (-1, result);
333
334                 result = 1;
335                 Assert.AreEqual (false, Int32.TryParse (null, out result));
336                 Assert.AreEqual (0, result);
337
338                 Assert.AreEqual (false, Int32.TryParse ("not-a-number", out result));
339
340                 double OverInt = (double)Int32.MaxValue + 1;
341                 Assert.AreEqual (false, Int32.TryParse (OverInt.ToString (), out result));
342                 Assert.AreEqual (false, Int32.TryParse (OverInt.ToString (), NumberStyles.None, CultureInfo.InvariantCulture, out result));
343
344                 Assert.AreEqual (false, Int32.TryParse ("$42", NumberStyles.Integer, null, out result));
345                 Assert.AreEqual (false, Int32.TryParse ("%42", NumberStyles.Integer, Nfi, out result));
346                 Assert.AreEqual (false, Int32.TryParse ("$42", NumberStyles.Integer, Nfi, out result));
347                 Assert.AreEqual (false, Int32.TryParse (" - 1 ", out result));
348                 Assert.AreEqual (false, Int32.TryParse (" - ", out result));
349                 Assert.AreEqual (false, Int32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
350                 Assert.AreEqual (false, Int32.TryParse ("10000000000", out result));
351                 Assert.AreEqual (false, Int32.TryParse ("-10000000000", out result));
352                 Assert.AreEqual (true, Int32.TryParse ("7fffffff", NumberStyles.HexNumber, Nfi, out result));
353                 Assert.AreEqual (Int32.MaxValue, result);
354                 Assert.AreEqual (true, Int32.TryParse ("80000000", NumberStyles.HexNumber, Nfi, out result));
355                 Assert.AreEqual (Int32.MinValue, result);
356                 Assert.AreEqual (true, Int32.TryParse ("ffffffff", NumberStyles.HexNumber, Nfi, out result));
357                 Assert.AreEqual (-1, result);
358                 Assert.AreEqual (false, Int32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
359         }
360 #endif
361
362         [Test]  
363         public void TestToString()
364         {
365                 //test ToString()
366                 Assert.AreEqual (MyString1, MyInt32_1.ToString (), "#D01");
367                 Assert.AreEqual (MyString2, MyInt32_2.ToString (), "#D02");
368                 Assert.AreEqual (MyString3, MyInt32_3.ToString (), "#D03");
369
370                 //test ToString(string format, IFormatProvider provider);
371                 for (int i=0; i < Formats1.Length; i++) {
372                         Assert.AreEqual (ResultsNfi1 [i], MyInt32_2.ToString (Formats1 [i], Nfi),
373                                                          "#D04(" + i + "," + Formats1 [i] + ")");
374                         Assert.AreEqual (ResultsNfi2 [i], MyInt32_3.ToString (Formats2 [i], Nfi), 
375                                                          "#D05(" + i + "," + Formats2 [i] + ")");
376                 }
377
378                 //test ToString(string format)
379                 for (int i=0; i < Formats1.Length; i++) {
380                         Assert.AreEqual (Results1 [i], MyInt32_2.ToString(Formats1[i]), "#D06(" + i + ")");
381                         Assert.AreEqual (Results2 [i], MyInt32_3.ToString(Formats2[i]),
382                                                          "#D07(" + i + ")");
383                                       
384                 }
385
386                 try {
387                         MyInt32_1.ToString("z");
388                         Assert.Fail ("#D08: Should raise a System.FormatException");
389                 }
390                 catch (Exception e) {
391                         Assert.IsTrue (typeof (FormatException) == e.GetType(), "#D09");
392                 }
393         }
394
395         [Test]
396         public void TestCustomToString()
397         {
398                 int i = 123;
399
400                 Assert.AreEqual ("00123", i.ToString ("00000"), "Custom format string 00000");
401                 Assert.AreEqual ("123", i.ToString ("####"), "Custom format string ####");
402                 Assert.AreEqual ("0123", i.ToString ("0###"), "Custom format string ####");
403                 Assert.AreEqual ("0123", i.ToString ("#0###"), "Custom format string ####");
404                 Assert.AreEqual ("000123", i.ToString ("0#0###"), "Custom format string ####");
405         }
406
407         [Test]
408         public void TestSections ()
409         {
410                 int hundred = 100;
411                 int neghund = -100;
412                 
413                 Assert.IsTrue ( hundred.ToString ("#;#") == "100", "#TS1");
414                 Assert.IsTrue ( hundred.ToString ("-#;#") == "-100", "#TS2");
415                 Assert.IsTrue ( neghund.ToString ("#;#") == "100", "#TS3");
416                 Assert.IsTrue ( neghund.ToString ("#;-#") == "-100", "#TS3");
417         }
418         
419         [Test]
420         public void ToString_Defaults () 
421         {
422                 Int32 i = 254;
423                 // everything defaults to "G"
424                 string def = i.ToString ("G");
425                 Assert.AreEqual (def, i.ToString (), "ToString()");
426                 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
427                 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
428                 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
429                 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
430                 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
431
432                 Assert.AreEqual ("254", def, "ToString(G)");
433         }
434 }
435
436 }