This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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         public void TestToString()
203         {
204                 //test ToString()
205                 AssertEquals ("#D01", MyString1, MyInt32_1.ToString ());
206                 AssertEquals ("#D02", MyString2, MyInt32_2.ToString ());
207                 AssertEquals ("#D03", MyString3, MyInt32_3.ToString ());
208
209                 //test ToString(string format, IFormatProvider provider);
210                 for (int i=0; i < Formats1.Length; i++) {
211                         AssertEquals ("#D04(" + i + "," + Formats1 [i] + ")",
212                                       ResultsNfi1 [i], MyInt32_2.ToString (Formats1 [i], Nfi));
213                         AssertEquals ("#D05(" + i + "," + Formats2 [i] + ")",
214                                       ResultsNfi2 [i], MyInt32_3.ToString (Formats2 [i], Nfi));
215                 }
216
217                 //test ToString(string format)
218                 for (int i=0; i < Formats1.Length; i++) {
219                         AssertEquals ("#D06(" + i + ")", Results1 [i],
220                                       MyInt32_2.ToString(Formats1[i]));
221                         AssertEquals ("#D07(" + i + ")", Results2 [i],
222                                       MyInt32_3.ToString(Formats2[i]));
223                 }
224
225                 try {
226                         MyInt32_1.ToString("z");
227                         Fail ("#D08: Should raise a System.FormatException");
228                 }
229                 catch (Exception e) {
230                         Assert ("#D09", typeof (FormatException) == e.GetType());
231                 }
232         }
233
234         public void TestCustomToString()
235         {
236                 int i = 123;
237
238                 AssertEquals ("Custom format string 00000", "00123", i.ToString ("00000"));
239                 AssertEquals ("Custom format string ####", "123", i.ToString ("####"));
240                 AssertEquals ("Custom format string ####", "0123", i.ToString ("0###"));
241                 AssertEquals ("Custom format string ####", "0123", i.ToString ("#0###"));
242                 AssertEquals ("Custom format string ####", "000123", i.ToString ("0#0###"));
243         }
244
245         [Test]
246         public void ToString_Defaults () 
247         {
248                 Int32 i = 254;
249                 // everything defaults to "G"
250                 string def = i.ToString ("G");
251                 AssertEquals ("ToString()", def, i.ToString ());
252                 AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
253                 AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
254                 AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
255                 AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
256                 AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
257
258                 AssertEquals ("ToString(G)", "254", def);
259         }
260 }
261
262 }