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
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;
72 public void TestMinMax()
75 Assert.AreEqual(Int32.MinValue, MyInt32_2, "#A01");
76 Assert.AreEqual(Int32.MaxValue, MyInt32_3, "#A02");
80 public void TestCompareTo()
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");
87 MyInt32_2.CompareTo((object)(Int16)100);
88 Assert.Fail("Should raise a System.ArgumentException");
91 Assert.IsTrue(typeof(ArgumentException) == e.GetType(), "typeof(ArgumentException) == e.GetType()");
96 public void TestEquals()
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");
105 public void TestGetHashCode()
108 MyInt32_1.GetHashCode();
109 MyInt32_2.GetHashCode();
110 MyInt32_3.GetHashCode();
113 Assert.Fail("GetHashCode should not raise an exception here");
118 public void TestParse()
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");
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");
137 Assert.Fail ("#C13: Should raise a System.ArgumentNullException");
139 catch (Exception e) {
140 Assert.IsTrue (typeof (ArgumentNullException) == e.GetType(), "#C14");
143 Int32.Parse("not-a-number");
144 Assert.Fail ("#C15: Should raise a System.FormatException");
146 catch (Exception e) {
147 Assert.IsTrue (typeof (FormatException) == e.GetType(), "#C16");
150 double OverInt = (double)Int32.MaxValue + 1;
151 Int32.Parse(OverInt.ToString());
152 Assert.Fail ("#C17: Should raise a System.OverflowException");
154 catch (Exception e) {
155 Assert.AreEqual (typeof (OverflowException), e.GetType(), "#C18");
157 //test Parse(string s, NumberStyles style)
158 Assert.AreEqual (42, Int32.Parse (" $42 ", NumberStyles.Currency), "#C19");
160 Int32.Parse("$42", NumberStyles.Integer);
161 Assert.Fail ("#C20: Should raise a System.FormatException");
163 catch (Exception e) {
164 Assert.IsTrue (typeof (FormatException) == e.GetType(), "#C21");
166 //test Parse(string s, IFormatProvider provider)
167 Assert.AreEqual (-42, Int32.Parse (" -42 ", Nfi), "#C22");
169 Int32.Parse("%42", Nfi);
170 Assert.Fail ("#C23: Should raise a System.FormatException");
172 catch (Exception e) {
173 Assert.IsTrue (typeof (FormatException) == e.GetType(), "#C24");
175 //test Parse(string s, NumberStyles style, IFormatProvider provider)
176 Assert.AreEqual (16, Int32.Parse (" 10 ", NumberStyles.HexNumber, Nfi), "#C25");
178 Int32.Parse("$42", NumberStyles.Integer, Nfi);
179 Assert.Fail ("#C26: Should raise a System.FormatException");
181 catch (Exception e) {
182 Assert.IsTrue(typeof (FormatException) == e.GetType(), "#C27");
186 Int32.Parse (" - 1 ");
187 Assert.Fail ("#C28: Should raise FormatException");
188 } catch (Exception e){
189 Assert.IsTrue (typeof (FormatException) == e.GetType (), "#C29");
194 Assert.Fail ("#C30: Should raise FormatException");
195 } catch (Exception e){
196 Assert.IsTrue (typeof (FormatException) == e.GetType (), "#C31");
198 Assert.AreEqual (-123, Int32.Parse ("ffffff85", NumberStyles.HexNumber, Nfi), "#C32");
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");
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");
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");
220 Assert.Fail ("C#39: Should raise an ArgumentNullException");
221 } catch (Exception e){
222 Assert.IsTrue (typeof (ArgumentNullException) == e.GetType (), "C#40");
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");
232 // Pass a DateTimeFormatInfo, it is unable to format
233 // numbers, but we should not crash
235 Int32.Parse ("123", new DateTimeFormatInfo ());
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");
244 public void TestParseExponent ()
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");
255 } catch (FormatException) {
259 Int32.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
261 } catch (FormatException) {
265 Int32.Parse ("2E 2", NumberStyles.AllowExponent);
267 } catch (FormatException) {
271 Int32.Parse ("2E2 ", NumberStyles.AllowExponent);
273 } catch (FormatException) {
277 Int32.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
279 } catch (OverflowException) {
283 long exponent = (long)Int32.MaxValue + 10;
284 Int32.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
286 } catch (OverflowException) {
290 Int32.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
292 } catch (OverflowException) {
298 public void TestTryParse()
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);
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);
329 Assert.AreEqual (false, Int32.TryParse (null, out result));
330 Assert.AreEqual (0, result);
332 Assert.AreEqual (false, Int32.TryParse ("not-a-number", out result));
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));
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));
357 public void TestToString()
360 Assert.AreEqual (MyString1, MyInt32_1.ToString (), "#D01");
361 Assert.AreEqual (MyString2, MyInt32_2.ToString (), "#D02");
362 Assert.AreEqual (MyString3, MyInt32_3.ToString (), "#D03");
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] + ")");
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]),
381 MyInt32_1.ToString("z");
382 Assert.Fail ("#D08: Should raise a System.FormatException");
384 catch (Exception e) {
385 Assert.IsTrue (typeof (FormatException) == e.GetType(), "#D09");
390 public void TestCustomToString()
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 ####");
402 public void TestSections ()
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");
414 public void ToString_Defaults ()
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)");
426 Assert.AreEqual ("254", def, "ToString(G)");