1 // Int32Test.cs - NUnit Test Cases for the System.Int32 struct
3 // Mario Martinez (mariom925@home.om)
5 // (C) Ximian, Inc. http://www.ximian.com
10 using System
.Threading
;
11 using System
.Globalization
;
13 namespace MonoTests
.System
17 public class Int32Test
: Assertion
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
;
41 private CultureInfo old_culture
;
46 old_culture
= Thread
.CurrentThread
.CurrentCulture
;
48 // Set culture to en-US and don't let the user override.
49 Thread
.CurrentThread
.CurrentCulture
= new CultureInfo ("en-US", false);
51 // We can't initialize this until we set the culture.
53 string decimals
= new String ('0', NumberFormatInfo
.CurrentInfo
.NumberDecimalDigits
);
54 string perPattern
= new string[] {"n %","n%","%n"}
[NumberFormatInfo
.CurrentInfo
.PercentPositivePattern
];
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");
61 Results2
[0] = NumberFormatInfo
.CurrentInfo
.CurrencySymbol
+"2,147,483,647.00000";
62 Results2
[6] = perPattern
.Replace ("n","214,748,364,700.00000");
66 public void TearDown()
68 Thread
.CurrentThread
.CurrentCulture
= old_culture
;
71 public void TestMinMax()
74 AssertEquals("#A01", Int32
.MinValue
, MyInt32_2
);
75 AssertEquals("#A02", Int32
.MaxValue
, MyInt32_3
);
78 public void TestCompareTo()
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);
85 MyInt32_2
.CompareTo((object)(Int16
)100);
86 Fail("Should raise a System.ArgumentException");
89 Assert("typeof(ArgumentException) == e.GetType()", typeof(ArgumentException
) == e
.GetType());
93 public void TestEquals()
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);
101 public void TestGetHashCode()
104 MyInt32_1
.GetHashCode();
105 MyInt32_2
.GetHashCode();
106 MyInt32_3
.GetHashCode();
109 Fail("GetHashCode should not raise an exception here");
113 public void TestParse()
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
));
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 "));
132 Fail ("#C13: Should raise a System.ArgumentNullException");
134 catch (Exception e
) {
135 Assert ("#C14", typeof (ArgumentNullException
) == e
.GetType());
138 Int32
.Parse("not-a-number");
139 Fail ("#C15: Should raise a System.FormatException");
141 catch (Exception e
) {
142 Assert ("#C16", typeof (FormatException
) == e
.GetType());
145 double OverInt
= (double)Int32
.MaxValue
+ 1;
146 Int32
.Parse(OverInt
.ToString());
147 Fail ("#C17: Should raise a System.OverflowException");
149 catch (Exception e
) {
150 AssertEquals ("#C18", typeof (OverflowException
), e
.GetType());
152 //test Parse(string s, NumberStyles style)
153 AssertEquals ("#C19", 42, Int32
.Parse (" $42 ", NumberStyles
.Currency
));
155 Int32
.Parse("$42", NumberStyles
.Integer
);
156 Fail ("#C20: Should raise a System.FormatException");
158 catch (Exception e
) {
159 Assert ("#C21", typeof (FormatException
) == e
.GetType());
161 //test Parse(string s, IFormatProvider provider)
162 AssertEquals ("#C22", -42, Int32
.Parse (" -42 ", Nfi
));
164 Int32
.Parse("%42", Nfi
);
165 Fail ("#C23: Should raise a System.FormatException");
167 catch (Exception e
) {
168 Assert ("#C24", typeof (FormatException
) == e
.GetType());
170 //test Parse(string s, NumberStyles style, IFormatProvider provider)
171 AssertEquals ("#C25", 16, Int32
.Parse (" 10 ", NumberStyles
.HexNumber
, Nfi
));
173 Int32
.Parse("$42", NumberStyles
.Integer
, Nfi
);
174 Fail ("#C26: Should raise a System.FormatException");
176 catch (Exception e
) {
177 Assert("#C27", typeof (FormatException
) == e
.GetType());
181 Int32
.Parse (" - 1 ");
182 Fail ("#C28: Should raise FormatException");
183 } catch (Exception e
){
184 Assert ("#C29", typeof (FormatException
) == e
.GetType ());
189 Fail ("#C30: Should raise FormatException");
190 } catch (Exception e
){
191 Assert ("#C31", typeof (FormatException
) == e
.GetType ());
193 AssertEquals ("#C32", -123, Int32
.Parse ("ffffff85", NumberStyles
.HexNumber
, Nfi
));
195 Int32
.Parse ("100000000", NumberStyles
.HexNumber
, Nfi
);
196 Fail ("#C33: Should raise OverflowException");
197 } catch (Exception e
){
198 Assert ("#C34", typeof (OverflowException
) == e
.GetType ());
201 Int32
.Parse ("2147483648");
202 Fail ("C#35: should raise OverflowException");
203 } catch (Exception e
) {
204 Assert ("C#36", typeof (OverflowException
) == e
.GetType ());
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 ());
215 Fail ("C#39: Should raise an ArgumentNullException");
216 } catch (Exception e
){
217 Assert ("C#40", typeof (ArgumentNullException
) == e
.GetType ());
221 Int32
.Parse ("123", (NumberStyles
) 60000);
222 Fail ("C#41 Should raise an ArgumentException");
223 } catch (Exception e
){
224 Assert ("C#42", typeof (ArgumentException
) == e
.GetType ());
227 // Pass a DateTimeFormatInfo, it is unable to format
228 // numbers, but we should not crash
230 Int32
.Parse ("123", new DateTimeFormatInfo ());
234 public void TestTryParse()
238 AssertEquals (true, Int32
.TryParse (MyString1
, out result
));
239 AssertEquals (MyInt32_1
, result
);
240 AssertEquals (true, Int32
.TryParse (MyString2
, out result
));
241 AssertEquals (MyInt32_2
, result
);
242 AssertEquals (true, Int32
.TryParse (MyString3
, out result
));
243 AssertEquals (MyInt32_3
, result
);
245 AssertEquals (true, Int32
.TryParse ("1", out result
));
246 AssertEquals (1, result
);
247 AssertEquals (true, Int32
.TryParse (" 1", out result
));
248 AssertEquals (1, result
);
249 AssertEquals (true, Int32
.TryParse (" 1", out result
));
250 AssertEquals (1, result
);
251 AssertEquals (true, Int32
.TryParse ("1 ", out result
));
252 AssertEquals (1, result
);
253 AssertEquals (true, Int32
.TryParse ("+1", out result
));
254 AssertEquals (1, result
);
255 AssertEquals (true, Int32
.TryParse ("-1", out result
));
256 AssertEquals (-1, result
);
257 AssertEquals (true, Int32
.TryParse (" -1", out result
));
258 AssertEquals (-1, result
);
259 AssertEquals (true, Int32
.TryParse (" -1 ", out result
));
260 AssertEquals (-1, result
);
261 AssertEquals (true, Int32
.TryParse (" -1 ", out result
));
262 AssertEquals (-1, result
);
265 AssertEquals (false, Int32
.TryParse (null, out result
));
266 AssertEquals (0, result
);
268 AssertEquals (false, Int32
.TryParse ("not-a-number", out result
));
270 double OverInt
= (double)Int32
.MaxValue
+ 1;
271 AssertEquals (false, Int32
.TryParse (OverInt
.ToString (), out result
));
273 AssertEquals (false, Int32
.TryParse ("$42", NumberStyles
.Integer
, null, out result
));
274 AssertEquals (false, Int32
.TryParse ("%42", NumberStyles
.Integer
, Nfi
, out result
));
275 AssertEquals (false, Int32
.TryParse ("$42", NumberStyles
.Integer
, Nfi
, out result
));
276 AssertEquals (false, Int32
.TryParse (" - 1 ", out result
));
277 AssertEquals (false, Int32
.TryParse (" - ", out result
));
278 AssertEquals (false, Int32
.TryParse ("100000000", NumberStyles
.HexNumber
, Nfi
, out result
));
279 AssertEquals (false, Int32
.TryParse ("10000000000", out result
));
280 AssertEquals (false, Int32
.TryParse ("-10000000000", out result
));
281 AssertEquals (true, Int32
.TryParse ("7fffffff", NumberStyles
.HexNumber
, Nfi
, out result
));
282 AssertEquals (Int32
.MaxValue
, result
);
283 AssertEquals (true, Int32
.TryParse ("80000000", NumberStyles
.HexNumber
, Nfi
, out result
));
284 AssertEquals (Int32
.MinValue
, result
);
285 AssertEquals (true, Int32
.TryParse ("ffffffff", NumberStyles
.HexNumber
, Nfi
, out result
));
286 AssertEquals (-1, result
);
287 AssertEquals (false, Int32
.TryParse ("100000000", NumberStyles
.HexNumber
, Nfi
, out result
));
291 public void TestToString()
294 AssertEquals ("#D01", MyString1
, MyInt32_1
.ToString ());
295 AssertEquals ("#D02", MyString2
, MyInt32_2
.ToString ());
296 AssertEquals ("#D03", MyString3
, MyInt32_3
.ToString ());
298 //test ToString(string format, IFormatProvider provider);
299 for (int i
=0; i
< Formats1
.Length
; i
++) {
300 AssertEquals ("#D04(" + i
+ "," + Formats1
[i
] + ")",
301 ResultsNfi1
[i
], MyInt32_2
.ToString (Formats1
[i
], Nfi
));
302 AssertEquals ("#D05(" + i
+ "," + Formats2
[i
] + ")",
303 ResultsNfi2
[i
], MyInt32_3
.ToString (Formats2
[i
], Nfi
));
306 //test ToString(string format)
307 for (int i
=0; i
< Formats1
.Length
; i
++) {
308 AssertEquals ("#D06(" + i
+ ")", Results1
[i
],
309 MyInt32_2
.ToString(Formats1
[i
]));
310 AssertEquals ("#D07(" + i
+ ")", Results2
[i
],
311 MyInt32_3
.ToString(Formats2
[i
]));
315 MyInt32_1
.ToString("z");
316 Fail ("#D08: Should raise a System.FormatException");
318 catch (Exception e
) {
319 Assert ("#D09", typeof (FormatException
) == e
.GetType());
323 public void TestCustomToString()
327 AssertEquals ("Custom format string 00000", "00123", i
.ToString ("00000"));
328 AssertEquals ("Custom format string ####", "123", i
.ToString ("####"));
329 AssertEquals ("Custom format string ####", "0123", i
.ToString ("0###"));
330 AssertEquals ("Custom format string ####", "0123", i
.ToString ("#0###"));
331 AssertEquals ("Custom format string ####", "000123", i
.ToString ("0#0###"));
335 public void TestSections ()
340 Assert ("#TS1", hundred
.ToString ("#;#") == "100");
341 Assert ("#TS2", hundred
.ToString ("-#;#") == "-100");
342 Assert ("#TS3", neghund
.ToString ("#;#") == "100");
343 Assert ("#TS3", neghund
.ToString ("#;-#") == "-100");
347 public void ToString_Defaults ()
350 // everything defaults to "G"
351 string def
= i
.ToString ("G");
352 AssertEquals ("ToString()", def
, i
.ToString ());
353 AssertEquals ("ToString((IFormatProvider)null)", def
, i
.ToString ((IFormatProvider
)null));
354 AssertEquals ("ToString((string)null)", def
, i
.ToString ((string)null));
355 AssertEquals ("ToString(empty)", def
, i
.ToString (String
.Empty
));
356 AssertEquals ("ToString(null,null)", def
, i
.ToString (null, null));
357 AssertEquals ("ToString(empty,null)", def
, i
.ToString (String
.Empty
, null));
359 AssertEquals ("ToString(G)", "254", def
);