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