merged Sys.Web.Services 2.0 support in my branch:
[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 : Assertion
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         public void TestMinMax()
72         {
73                 
74                 AssertEquals("#A01", Int32.MinValue, MyInt32_2);
75                 AssertEquals("#A02", Int32.MaxValue, MyInt32_3);
76         }
77         
78         public void TestCompareTo()
79         {
80                 Assert("MyInt32_3.CompareTo(MyInt32_2) > 0", MyInt32_3.CompareTo(MyInt32_2) > 0);
81                 Assert("MyInt32_2.CompareTo(MyInt32_2) == 0", MyInt32_2.CompareTo(MyInt32_2) == 0);
82                 Assert("MyInt32_1.CompareTo((Int32)(-42)) == 0", MyInt32_1.CompareTo((object)(Int32)(-42)) == 0);
83                 Assert("MyInt32_2.CompareTo(MyInt32_3) < 0", MyInt32_2.CompareTo(MyInt32_3) < 0);
84                 try {
85                         MyInt32_2.CompareTo((object)(Int16)100);
86                         Fail("Should raise a System.ArgumentException");
87                 }
88                 catch (Exception e) {
89                         Assert("typeof(ArgumentException) == e.GetType()", typeof(ArgumentException) == e.GetType());
90                 }
91         }
92
93         public void TestEquals()
94         {
95                 Assert ("#B01", MyInt32_1.Equals (MyInt32_1));
96                 Assert ("#B02", MyInt32_1.Equals ((Int32)(-42)));
97                 Assert ("#B03", MyInt32_1.Equals ((object)(SByte)(-42)) == false);
98                 Assert ("#B04", MyInt32_1.Equals (MyInt32_2) == false);
99         }
100         
101         public void TestGetHashCode()
102         {
103                 try {
104                         MyInt32_1.GetHashCode();
105                         MyInt32_2.GetHashCode();
106                         MyInt32_3.GetHashCode();
107                 }
108                 catch {
109                         Fail("GetHashCode should not raise an exception here");
110                 }
111         }
112         
113         public void TestParse()
114         {
115                 //test Parse(string s)
116                 AssertEquals ("#C01", MyInt32_1, Int32.Parse (MyString1));
117                 AssertEquals ("#C02", MyInt32_2, Int32.Parse (MyString2));
118                 AssertEquals ("#C03", MyInt32_3, Int32.Parse (MyString3));
119
120                 AssertEquals ("#C04", 1, Int32.Parse ("1"));
121                 AssertEquals ("#C05", 1, Int32.Parse (" 1"));
122                 AssertEquals ("#C06", 1, Int32.Parse ("     1"));
123                 AssertEquals ("#C07", 1, Int32.Parse ("1    "));
124                 AssertEquals ("#C08", 1, Int32.Parse ("+1"));
125                 AssertEquals ("#C09", -1, Int32.Parse ("-1"));
126                 AssertEquals ("#C10", -1, Int32.Parse ("  -1"));
127                 AssertEquals ("#C11", -1, Int32.Parse ("  -1  "));
128                 AssertEquals ("#C12", -1, Int32.Parse ("  -1  "));
129
130                 try {
131                         Int32.Parse(null);
132                         Fail ("#C13: Should raise a System.ArgumentNullException");
133                 }
134                 catch (Exception e) {
135                         Assert ("#C14", typeof (ArgumentNullException) == e.GetType());
136                 }
137                 try {
138                         Int32.Parse("not-a-number");
139                         Fail ("#C15: Should raise a System.FormatException");
140                 }
141                 catch (Exception e) {
142                         Assert ("#C16", typeof (FormatException) == e.GetType());
143                 }
144                 try {
145                         double OverInt = (double)Int32.MaxValue + 1;
146                         Int32.Parse(OverInt.ToString());
147                         Fail ("#C17: Should raise a System.OverflowException");
148                 }
149                 catch (Exception e) {
150                         AssertEquals ("#C18", typeof (OverflowException), e.GetType());
151                 }
152                 //test Parse(string s, NumberStyles style)
153                 AssertEquals ("#C19", 42, Int32.Parse (" $42 ", NumberStyles.Currency));
154                 try {
155                         Int32.Parse("$42", NumberStyles.Integer);
156                         Fail ("#C20: Should raise a System.FormatException");
157                 }
158                 catch (Exception e) {
159                         Assert ("#C21", typeof (FormatException) == e.GetType());
160                 }
161                 //test Parse(string s, IFormatProvider provider)
162                 AssertEquals ("#C22", -42, Int32.Parse (" -42 ", Nfi));
163                 try {
164                         Int32.Parse("%42", Nfi);
165                         Fail ("#C23: Should raise a System.FormatException");
166                 }
167                 catch (Exception e) {
168                         Assert ("#C24", typeof (FormatException) == e.GetType());
169                 }
170                 //test Parse(string s, NumberStyles style, IFormatProvider provider)
171                 AssertEquals ("#C25", 16, Int32.Parse (" 10 ", NumberStyles.HexNumber, Nfi));
172                 try {
173                         Int32.Parse("$42", NumberStyles.Integer, Nfi);
174                         Fail ("#C26: Should raise a System.FormatException");
175                 }
176                 catch (Exception e) {
177                         Assert("#C27", typeof (FormatException) == e.GetType());
178                 }
179
180                 try {
181                         Int32.Parse (" - 1 ");
182                         Fail ("#C28: Should raise FormatException");
183                 } catch (Exception e){
184                         Assert ("#C29", typeof (FormatException) == e.GetType ());
185                 }
186
187                 try {
188                         Int32.Parse (" - ");
189                         Fail ("#C30: Should raise FormatException");
190                 } catch (Exception e){
191                         Assert ("#C31", typeof (FormatException) == e.GetType ());
192                 }
193                 AssertEquals ("#C32", -123, Int32.Parse ("ffffff85", NumberStyles.HexNumber, Nfi));
194                 try {
195                         Int32.Parse ("100000000", NumberStyles.HexNumber, Nfi);
196                         Fail ("#C33: Should raise OverflowException");
197                 } catch (Exception e){
198                         Assert ("#C34", typeof (OverflowException) == e.GetType ());
199                 }
200                 try {
201                         Int32.Parse ("2147483648");
202                         Fail ("C#35: should raise OverflowException");
203                 } catch (Exception e) {
204                         Assert ("C#36", typeof (OverflowException) == e.GetType ());
205                 }
206                 try {
207                         Int32.Parse ("2147483648", CultureInfo.InvariantCulture);
208                         Fail ("C#37: should raise OverflowException");
209                 } catch (Exception e) {
210                         Assert ("C#38", typeof (OverflowException) == e.GetType ());
211                 }
212         }
213
214 #if NET_2_0     
215         public void TestTryParse()
216         {
217                 int result;
218
219                 AssertEquals (true, Int32.TryParse (MyString1, out result));
220                 AssertEquals (MyInt32_1, result);
221                 AssertEquals (true, Int32.TryParse (MyString2, out result));
222                 AssertEquals (MyInt32_2, result);
223                 AssertEquals (true, Int32.TryParse (MyString3, out result));
224                 AssertEquals (MyInt32_3, result);
225
226                 AssertEquals (true, Int32.TryParse ("1", out result));
227                 AssertEquals (1, result);
228                 AssertEquals (true, Int32.TryParse (" 1", out result));
229                 AssertEquals (1, result);
230                 AssertEquals (true, Int32.TryParse ("     1", out result));
231                 AssertEquals (1, result);
232                 AssertEquals (true, Int32.TryParse ("1    ", out result));
233                 AssertEquals (1, result);
234                 AssertEquals (true, Int32.TryParse ("+1", out result));
235                 AssertEquals (1, result);
236                 AssertEquals (true, Int32.TryParse ("-1", out result));
237                 AssertEquals (-1, result);
238                 AssertEquals (true, Int32.TryParse ("  -1", out result));
239                 AssertEquals (-1, result);
240                 AssertEquals (true, Int32.TryParse ("  -1  ", out result));
241                 AssertEquals (-1, result);
242                 AssertEquals (true, Int32.TryParse ("  -1  ", out result));
243                 AssertEquals (-1, result);
244
245                 result = 1;
246                 AssertEquals (false, Int32.TryParse (null, out result));
247                 AssertEquals (0, result);
248
249                 AssertEquals (false, Int32.TryParse ("not-a-number", out result));
250
251                 double OverInt = (double)Int32.MaxValue + 1;
252                 AssertEquals (false, Int32.TryParse (OverInt.ToString (), out result));
253
254                 AssertEquals (false, Int32.TryParse ("$42", NumberStyles.Integer, null, out result));
255                 AssertEquals (false, Int32.TryParse ("%42", NumberStyles.Integer, Nfi, out result));
256                 AssertEquals (false, Int32.TryParse ("$42", NumberStyles.Integer, Nfi, out result));
257                 AssertEquals (false, Int32.TryParse (" - 1 ", out result));
258                 AssertEquals (false, Int32.TryParse (" - ", out result));
259                 AssertEquals (false, Int32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
260         }
261 #endif
262         
263         public void TestToString()
264         {
265                 //test ToString()
266                 AssertEquals ("#D01", MyString1, MyInt32_1.ToString ());
267                 AssertEquals ("#D02", MyString2, MyInt32_2.ToString ());
268                 AssertEquals ("#D03", MyString3, MyInt32_3.ToString ());
269
270                 //test ToString(string format, IFormatProvider provider);
271                 for (int i=0; i < Formats1.Length; i++) {
272                         AssertEquals ("#D04(" + i + "," + Formats1 [i] + ")",
273                                       ResultsNfi1 [i], MyInt32_2.ToString (Formats1 [i], Nfi));
274                         AssertEquals ("#D05(" + i + "," + Formats2 [i] + ")",
275                                       ResultsNfi2 [i], MyInt32_3.ToString (Formats2 [i], Nfi));
276                 }
277
278                 //test ToString(string format)
279                 for (int i=0; i < Formats1.Length; i++) {
280                         AssertEquals ("#D06(" + i + ")", Results1 [i],
281                                       MyInt32_2.ToString(Formats1[i]));
282                         AssertEquals ("#D07(" + i + ")", Results2 [i],
283                                       MyInt32_3.ToString(Formats2[i]));
284                 }
285
286                 try {
287                         MyInt32_1.ToString("z");
288                         Fail ("#D08: Should raise a System.FormatException");
289                 }
290                 catch (Exception e) {
291                         Assert ("#D09", typeof (FormatException) == e.GetType());
292                 }
293         }
294
295         public void TestCustomToString()
296         {
297                 int i = 123;
298
299                 AssertEquals ("Custom format string 00000", "00123", i.ToString ("00000"));
300                 AssertEquals ("Custom format string ####", "123", i.ToString ("####"));
301                 AssertEquals ("Custom format string ####", "0123", i.ToString ("0###"));
302                 AssertEquals ("Custom format string ####", "0123", i.ToString ("#0###"));
303                 AssertEquals ("Custom format string ####", "000123", i.ToString ("0#0###"));
304         }
305
306         [Test]
307         public void TestSections ()
308         {
309                 int hundred = 100;
310                 int neghund = -100;
311                 
312                 Assert ("#TS1",  hundred.ToString ("#;#") == "100");
313                 Assert ("#TS2",  hundred.ToString ("-#;#") == "-100");
314                 Assert ("#TS3",  neghund.ToString ("#;#") == "100");
315                 Assert ("#TS3",  neghund.ToString ("#;-#") == "-100");
316         }
317         
318         [Test]
319         public void ToString_Defaults () 
320         {
321                 Int32 i = 254;
322                 // everything defaults to "G"
323                 string def = i.ToString ("G");
324                 AssertEquals ("ToString()", def, i.ToString ());
325                 AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
326                 AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
327                 AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
328                 AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
329                 AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
330
331                 AssertEquals ("ToString(G)", "254", def);
332         }
333 }
334
335 }