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((Int32)(-42)) == 0);
83 Assert("MyInt32_2.CompareTo(MyInt32_3) < 0", MyInt32_2.CompareTo(MyInt32_3) < 0);
85 MyInt32_2.CompareTo((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 ((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 ());
203 public void TestTryParse()
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);
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);
234 AssertEquals (false, Int32.TryParse (null, out result));
235 AssertEquals (0, result);
237 AssertEquals (false, Int32.TryParse ("not-a-number", out result));
239 double OverInt = (double)Int32.MaxValue + 1;
240 AssertEquals (false, Int32.TryParse (OverInt.ToString (), out result));
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));
251 public void TestToString()
254 AssertEquals ("#D01", MyString1, MyInt32_1.ToString ());
255 AssertEquals ("#D02", MyString2, MyInt32_2.ToString ());
256 AssertEquals ("#D03", MyString3, MyInt32_3.ToString ());
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));
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]));
275 MyInt32_1.ToString("z");
276 Fail ("#D08: Should raise a System.FormatException");
278 catch (Exception e) {
279 Assert ("#D09", typeof (FormatException) == e.GetType());
283 public void TestCustomToString()
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###"));
295 public void ToString_Defaults ()
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));
307 AssertEquals ("ToString(G)", "254", def);