2004-10-02 Zoltan Varga <vargaz@freemail.hu>
[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((Int32)(-42)) == 0);
83                 Assert("MyInt32_2.CompareTo(MyInt32_3) < 0", MyInt32_2.CompareTo(MyInt32_3) < 0);
84                 try {
85                         MyInt32_2.CompareTo((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 ((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         }
201
202 #if NET_2_0     
203         public void TestTryParse()
204         {
205                 int result;
206
207                 AssertEquals (true, Int32.TryParse (MyString1, out result));
208                 AssertEquals (MyInt32_1, result);
209                 AssertEquals (true, Int32.TryParse (MyString2, out result));
210                 AssertEquals (MyInt32_2, result);
211                 AssertEquals (true, Int32.TryParse (MyString3, out result));
212                 AssertEquals (MyInt32_3, result);
213
214                 AssertEquals (true, Int32.TryParse ("1", out result));
215                 AssertEquals (1, result);
216                 AssertEquals (true, Int32.TryParse (" 1", out result));
217                 AssertEquals (1, result);
218                 AssertEquals (true, Int32.TryParse ("     1", out result));
219                 AssertEquals (1, result);
220                 AssertEquals (true, Int32.TryParse ("1    ", out result));
221                 AssertEquals (1, result);
222                 AssertEquals (true, Int32.TryParse ("+1", out result));
223                 AssertEquals (1, result);
224                 AssertEquals (true, Int32.TryParse ("-1", out result));
225                 AssertEquals (-1, result);
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
233                 result = 1;
234                 AssertEquals (false, Int32.TryParse (null, out result));
235                 AssertEquals (0, result);
236
237                 AssertEquals (false, Int32.TryParse ("not-a-number", out result));
238
239                 double OverInt = (double)Int32.MaxValue + 1;
240                 AssertEquals (false, Int32.TryParse (OverInt.ToString (), out result));
241
242                 AssertEquals (false, Int32.TryParse ("$42", NumberStyles.Integer, null, out result));
243                 AssertEquals (false, Int32.TryParse ("%42", NumberStyles.Integer, Nfi, out result));
244                 AssertEquals (false, Int32.TryParse ("$42", NumberStyles.Integer, Nfi, out result));
245                 AssertEquals (false, Int32.TryParse (" - 1 ", out result));
246                 AssertEquals (false, Int32.TryParse (" - ", out result));
247                 AssertEquals (false, Int32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
248         }
249 #endif
250         
251         public void TestToString()
252         {
253                 //test ToString()
254                 AssertEquals ("#D01", MyString1, MyInt32_1.ToString ());
255                 AssertEquals ("#D02", MyString2, MyInt32_2.ToString ());
256                 AssertEquals ("#D03", MyString3, MyInt32_3.ToString ());
257
258                 //test ToString(string format, IFormatProvider provider);
259                 for (int i=0; i < Formats1.Length; i++) {
260                         AssertEquals ("#D04(" + i + "," + Formats1 [i] + ")",
261                                       ResultsNfi1 [i], MyInt32_2.ToString (Formats1 [i], Nfi));
262                         AssertEquals ("#D05(" + i + "," + Formats2 [i] + ")",
263                                       ResultsNfi2 [i], MyInt32_3.ToString (Formats2 [i], Nfi));
264                 }
265
266                 //test ToString(string format)
267                 for (int i=0; i < Formats1.Length; i++) {
268                         AssertEquals ("#D06(" + i + ")", Results1 [i],
269                                       MyInt32_2.ToString(Formats1[i]));
270                         AssertEquals ("#D07(" + i + ")", Results2 [i],
271                                       MyInt32_3.ToString(Formats2[i]));
272                 }
273
274                 try {
275                         MyInt32_1.ToString("z");
276                         Fail ("#D08: Should raise a System.FormatException");
277                 }
278                 catch (Exception e) {
279                         Assert ("#D09", typeof (FormatException) == e.GetType());
280                 }
281         }
282
283         public void TestCustomToString()
284         {
285                 int i = 123;
286
287                 AssertEquals ("Custom format string 00000", "00123", i.ToString ("00000"));
288                 AssertEquals ("Custom format string ####", "123", i.ToString ("####"));
289                 AssertEquals ("Custom format string ####", "0123", i.ToString ("0###"));
290                 AssertEquals ("Custom format string ####", "0123", i.ToString ("#0###"));
291                 AssertEquals ("Custom format string ####", "000123", i.ToString ("0#0###"));
292         }
293
294         [Test]
295         public void ToString_Defaults () 
296         {
297                 Int32 i = 254;
298                 // everything defaults to "G"
299                 string def = i.ToString ("G");
300                 AssertEquals ("ToString()", def, i.ToString ());
301                 AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
302                 AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
303                 AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
304                 AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
305                 AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
306
307                 AssertEquals ("ToString(G)", "254", def);
308         }
309 }
310
311 }