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;
44 public void SetUpFixture()
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");
68 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
72 public void TearDown()
74 Thread.CurrentThread.CurrentCulture = old_culture;
78 public void TestMinMax()
81 Assert.AreEqual(Int32.MinValue, MyInt32_2, "#A01");
82 Assert.AreEqual(Int32.MaxValue, MyInt32_3, "#A02");
86 public void TestCompareTo()
88 Assert.IsTrue(MyInt32_3.CompareTo(MyInt32_2) > 0, "MyInt32_3.CompareTo(MyInt32_2) > 0");
89 Assert.IsTrue(MyInt32_2.CompareTo(MyInt32_2) == 0, "MyInt32_2.CompareTo(MyInt32_2) == 0");
90 Assert.IsTrue(MyInt32_1.CompareTo((object)(Int32)(-42)) == 0, "MyInt32_1.CompareTo((Int32)(-42)) == 0");
91 Assert.IsTrue(MyInt32_2.CompareTo(MyInt32_3) < 0, "MyInt32_2.CompareTo(MyInt32_3) < 0");
93 MyInt32_2.CompareTo((object)(Int16)100);
94 Assert.Fail("Should raise a System.ArgumentException");
97 Assert.IsTrue(typeof(ArgumentException) == e.GetType(), "typeof(ArgumentException) == e.GetType()");
102 public void TestEquals()
104 Assert.IsTrue (MyInt32_1.Equals (MyInt32_1), "#B01");
105 Assert.IsTrue (MyInt32_1.Equals ((Int32)(-42)), "#B02");
106 Assert.IsTrue (MyInt32_1.Equals ((object)(SByte)(-42)) == false, "#B03");
107 Assert.IsTrue (MyInt32_1.Equals (MyInt32_2) == false, "#B04");
111 public void TestGetHashCode()
114 MyInt32_1.GetHashCode();
115 MyInt32_2.GetHashCode();
116 MyInt32_3.GetHashCode();
119 Assert.Fail("GetHashCode should not raise an exception here");
124 public void TestParse()
126 //test Parse(string s)
127 Assert.AreEqual (MyInt32_1, Int32.Parse (MyString1), "#C01");
128 Assert.AreEqual (MyInt32_2, Int32.Parse (MyString2), "#C02");
129 Assert.AreEqual (MyInt32_3, Int32.Parse (MyString3), "#C03");
131 Assert.AreEqual (1, Int32.Parse ("1"), "#C04");
132 Assert.AreEqual (1, Int32.Parse (" 1"), "#C05");
133 Assert.AreEqual (1, Int32.Parse (" 1"), "#C06");
134 Assert.AreEqual (1, Int32.Parse ("1 "), "#C07");
135 Assert.AreEqual (1, Int32.Parse ("+1"), "#C08");
136 Assert.AreEqual (-1, Int32.Parse ("-1"), "#C09");
137 Assert.AreEqual (-1, Int32.Parse (" -1"), "#C10");
138 Assert.AreEqual (-1, Int32.Parse (" -1 "), "#C11");
139 Assert.AreEqual (-1, Int32.Parse (" -1 "), "#C12");
143 Assert.Fail ("#C13: Should raise a System.ArgumentNullException");
145 catch (Exception e) {
146 Assert.IsTrue (typeof (ArgumentNullException) == e.GetType(), "#C14");
149 Int32.Parse("not-a-number");
150 Assert.Fail ("#C15: Should raise a System.FormatException");
152 catch (Exception e) {
153 Assert.IsTrue (typeof (FormatException) == e.GetType(), "#C16");
156 double OverInt = (double)Int32.MaxValue + 1;
157 Int32.Parse(OverInt.ToString());
158 Assert.Fail ("#C17: Should raise a System.OverflowException");
160 catch (Exception e) {
161 Assert.AreEqual (typeof (OverflowException), e.GetType(), "#C18");
163 //test Parse(string s, NumberStyles style)
164 Assert.AreEqual (42, Int32.Parse (" $42 ", NumberStyles.Currency), "#C19");
166 Int32.Parse("$42", NumberStyles.Integer);
167 Assert.Fail ("#C20: Should raise a System.FormatException");
169 catch (Exception e) {
170 Assert.IsTrue (typeof (FormatException) == e.GetType(), "#C21");
172 //test Parse(string s, IFormatProvider provider)
173 Assert.AreEqual (-42, Int32.Parse (" -42 ", Nfi), "#C22");
175 Int32.Parse("%42", Nfi);
176 Assert.Fail ("#C23: Should raise a System.FormatException");
178 catch (Exception e) {
179 Assert.IsTrue (typeof (FormatException) == e.GetType(), "#C24");
181 //test Parse(string s, NumberStyles style, IFormatProvider provider)
182 Assert.AreEqual (16, Int32.Parse (" 10 ", NumberStyles.HexNumber, Nfi), "#C25");
184 Int32.Parse("$42", NumberStyles.Integer, Nfi);
185 Assert.Fail ("#C26: Should raise a System.FormatException");
187 catch (Exception e) {
188 Assert.IsTrue(typeof (FormatException) == e.GetType(), "#C27");
192 Int32.Parse (" - 1 ");
193 Assert.Fail ("#C28: Should raise FormatException");
194 } catch (Exception e){
195 Assert.IsTrue (typeof (FormatException) == e.GetType (), "#C29");
200 Assert.Fail ("#C30: Should raise FormatException");
201 } catch (Exception e){
202 Assert.IsTrue (typeof (FormatException) == e.GetType (), "#C31");
204 Assert.AreEqual (-123, Int32.Parse ("ffffff85", NumberStyles.HexNumber, Nfi), "#C32");
206 Int32.Parse ("100000000", NumberStyles.HexNumber, Nfi);
207 Assert.Fail ("#C33: Should raise OverflowException");
208 } catch (Exception e){
209 Assert.IsTrue (typeof (OverflowException) == e.GetType (), "#C34");
212 Int32.Parse ("2147483648");
213 Assert.Fail ("C#35: should raise OverflowException");
214 } catch (Exception e) {
215 Assert.IsTrue (typeof (OverflowException) == e.GetType (), "C#36");
218 Int32.Parse ("2147483648", CultureInfo.InvariantCulture);
219 Assert.Fail ("C#37: should raise OverflowException");
220 } catch (Exception e) {
221 Assert.IsTrue (typeof (OverflowException) == e.GetType (), "C#38");
226 Assert.Fail ("C#39: Should raise an ArgumentNullException");
227 } catch (Exception e){
228 Assert.IsTrue (typeof (ArgumentNullException) == e.GetType (), "C#40");
232 Int32.Parse ("123", (NumberStyles) 60000);
233 Assert.Fail ("C#41 Should raise an ArgumentException");
234 } catch (Exception e){
235 Assert.IsTrue (typeof (ArgumentException) == e.GetType (), "C#42");
238 // Pass a DateTimeFormatInfo, it is unable to format
239 // numbers, but we should not crash
241 Int32.Parse ("123", new DateTimeFormatInfo ());
243 Assert.AreEqual (734561, Int32.Parse ("734561\0"), "C#43");
244 Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0 \0"), "C#44");
245 Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0 "), "C#45");
246 Assert.AreEqual (734561, Int32.Parse ("734561\0\0\0"), "C#46");
250 public void TestParseExponent ()
252 Assert.AreEqual (2, Int32.Parse ("2E0", NumberStyles.AllowExponent), "A#1");
253 Assert.AreEqual (20, Int32.Parse ("2E1", NumberStyles.AllowExponent), "A#2");
254 Assert.AreEqual (200, Int32.Parse ("2E2", NumberStyles.AllowExponent), "A#3");
255 Assert.AreEqual (2000000, Int32.Parse ("2E6", NumberStyles.AllowExponent), "A#4");
256 Assert.AreEqual (200, Int32.Parse ("2E+2", NumberStyles.AllowExponent), "A#5");
261 } catch (FormatException) {
265 Int32.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent
267 } catch (FormatException) {
271 Int32.Parse ("2E 2", NumberStyles.AllowExponent);
273 } catch (FormatException) {
277 Int32.Parse ("2E2 ", NumberStyles.AllowExponent);
279 } catch (FormatException) {
283 Int32.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow
285 } catch (OverflowException) {
289 long exponent = (long)Int32.MaxValue + 10;
290 Int32.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent);
292 } catch (OverflowException) {
296 Int32.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent
298 } catch (OverflowException) {
304 public void TestTryParse()
308 Assert.AreEqual (true, Int32.TryParse (MyString1, out result));
309 Assert.AreEqual (MyInt32_1, result);
310 Assert.AreEqual (true, Int32.TryParse (MyString2, out result));
311 Assert.AreEqual (MyInt32_2, result);
312 Assert.AreEqual (true, Int32.TryParse (MyString3, out result));
313 Assert.AreEqual (MyInt32_3, 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);
327 Assert.AreEqual (true, Int32.TryParse (" -1", out result));
328 Assert.AreEqual (-1, result);
329 Assert.AreEqual (true, Int32.TryParse (" -1 ", out result));
330 Assert.AreEqual (-1, result);
331 Assert.AreEqual (true, Int32.TryParse (" -1 ", out result));
332 Assert.AreEqual (-1, result);
335 Assert.AreEqual (false, Int32.TryParse (null, out result));
336 Assert.AreEqual (0, result);
338 Assert.AreEqual (false, Int32.TryParse ("not-a-number", out result));
340 double OverInt = (double)Int32.MaxValue + 1;
341 Assert.AreEqual (false, Int32.TryParse (OverInt.ToString (), out result));
342 Assert.AreEqual (false, Int32.TryParse (OverInt.ToString (), NumberStyles.None, CultureInfo.InvariantCulture, out result));
344 Assert.AreEqual (false, Int32.TryParse ("$42", NumberStyles.Integer, null, out result));
345 Assert.AreEqual (false, Int32.TryParse ("%42", NumberStyles.Integer, Nfi, out result));
346 Assert.AreEqual (false, Int32.TryParse ("$42", NumberStyles.Integer, Nfi, out result));
347 Assert.AreEqual (false, Int32.TryParse (" - 1 ", out result));
348 Assert.AreEqual (false, Int32.TryParse (" - ", out result));
349 Assert.AreEqual (false, Int32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
350 Assert.AreEqual (false, Int32.TryParse ("10000000000", out result));
351 Assert.AreEqual (false, Int32.TryParse ("-10000000000", out result));
352 Assert.AreEqual (true, Int32.TryParse ("7fffffff", NumberStyles.HexNumber, Nfi, out result));
353 Assert.AreEqual (Int32.MaxValue, result);
354 Assert.AreEqual (true, Int32.TryParse ("80000000", NumberStyles.HexNumber, Nfi, out result));
355 Assert.AreEqual (Int32.MinValue, result);
356 Assert.AreEqual (true, Int32.TryParse ("ffffffff", NumberStyles.HexNumber, Nfi, out result));
357 Assert.AreEqual (-1, result);
358 Assert.AreEqual (false, Int32.TryParse ("100000000", NumberStyles.HexNumber, Nfi, out result));
363 public void TestToString()
366 Assert.AreEqual (MyString1, MyInt32_1.ToString (), "#D01");
367 Assert.AreEqual (MyString2, MyInt32_2.ToString (), "#D02");
368 Assert.AreEqual (MyString3, MyInt32_3.ToString (), "#D03");
370 //test ToString(string format, IFormatProvider provider);
371 for (int i=0; i < Formats1.Length; i++) {
372 Assert.AreEqual (ResultsNfi1 [i], MyInt32_2.ToString (Formats1 [i], Nfi),
373 "#D04(" + i + "," + Formats1 [i] + ")");
374 Assert.AreEqual (ResultsNfi2 [i], MyInt32_3.ToString (Formats2 [i], Nfi),
375 "#D05(" + i + "," + Formats2 [i] + ")");
378 //test ToString(string format)
379 for (int i=0; i < Formats1.Length; i++) {
380 Assert.AreEqual (Results1 [i], MyInt32_2.ToString(Formats1[i]), "#D06(" + i + ")");
381 Assert.AreEqual (Results2 [i], MyInt32_3.ToString(Formats2[i]),
387 MyInt32_1.ToString("z");
388 Assert.Fail ("#D08: Should raise a System.FormatException");
390 catch (Exception e) {
391 Assert.IsTrue (typeof (FormatException) == e.GetType(), "#D09");
396 public void TestCustomToString()
400 Assert.AreEqual ("00123", i.ToString ("00000"), "Custom format string 00000");
401 Assert.AreEqual ("123", i.ToString ("####"), "Custom format string ####");
402 Assert.AreEqual ("0123", i.ToString ("0###"), "Custom format string ####");
403 Assert.AreEqual ("0123", i.ToString ("#0###"), "Custom format string ####");
404 Assert.AreEqual ("000123", i.ToString ("0#0###"), "Custom format string ####");
408 public void TestSections ()
413 Assert.IsTrue ( hundred.ToString ("#;#") == "100", "#TS1");
414 Assert.IsTrue ( hundred.ToString ("-#;#") == "-100", "#TS2");
415 Assert.IsTrue ( neghund.ToString ("#;#") == "100", "#TS3");
416 Assert.IsTrue ( neghund.ToString ("#;-#") == "-100", "#TS3");
420 public void ToString_Defaults ()
423 // everything defaults to "G"
424 string def = i.ToString ("G");
425 Assert.AreEqual (def, i.ToString (), "ToString()");
426 Assert.AreEqual (def, i.ToString ((IFormatProvider)null), "ToString((IFormatProvider)null)");
427 Assert.AreEqual (def, i.ToString ((string)null), "ToString((string)null)");
428 Assert.AreEqual (def, i.ToString (String.Empty), "ToString(empty)");
429 Assert.AreEqual (def, i.ToString (null, null), "ToString(null,null)");
430 Assert.AreEqual (def, i.ToString (String.Empty, null), "ToString(empty,null)");
432 Assert.AreEqual ("254", def, "ToString(G)");