1 // UInt32Test.cs - NUnit Test Cases for the System.UInt32 struct
3 // Mario Martinez (mariom925@home.om)
5 // (C) Ximian, Inc. http://www.ximian.com
10 using System
.Globalization
;
11 using System
.Threading
;
13 namespace MonoTests
.System
17 public class UInt32Test
19 private const UInt32 MyUInt32_1
= 42;
20 private const UInt32 MyUInt32_2
= 0;
21 private const UInt32 MyUInt32_3
= 4294967295;
22 private const string MyString1
= "42";
23 private const string MyString2
= "0";
24 private const string MyString3
= "4294967295";
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
= {"",
28 "0", "0.000000e+000", "0.00",
29 "0", "0.00", "0.00 %", "0"};
30 private string[] ResultsNfi1
= {NumberFormatInfo
.InvariantInfo
.CurrencySymbol
+"0.00",
31 "0", "0.000000e+000", "0.00",
32 "0", "0.00", "0.00 %", "0"};
33 private string[] Results2
= {"",
34 "4294967295", "4.29497e+009", "4294967295.00000",
35 "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
36 private string[] ResultsNfi2
= {NumberFormatInfo
.InvariantInfo
.CurrencySymbol
+"4,294,967,295.00000",
37 "4294967295", "4.29497e+009", "4294967295.00000",
38 "4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
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
+ "0.00";
57 Results1
[3] = "0." + decimals
;
58 Results1
[5] = "0." + decimals
;
59 Results1
[6] = perPattern
.Replace ("n","0.00");
61 Results2
[0] = NumberFormatInfo
.CurrentInfo
.CurrencySymbol
+ "4,294,967,295.00000";
62 Results2
[6] = perPattern
.Replace ("n","429,496,729,500.00000");
66 public void TearDown ()
68 Thread
.CurrentThread
.CurrentCulture
= old_culture
;
72 public void TestMinMax()
75 Assert
.AreEqual(UInt32
.MinValue
, MyUInt32_2
);
76 Assert
.AreEqual(UInt32
.MaxValue
, MyUInt32_3
);
80 public void TestCompareTo()
82 Assert
.IsTrue(MyUInt32_3
.CompareTo(MyUInt32_2
) > 0);
83 Assert
.IsTrue(MyUInt32_2
.CompareTo(MyUInt32_2
) == 0);
84 Assert
.IsTrue(MyUInt32_1
.CompareTo((UInt32
)(42)) == 0);
85 Assert
.IsTrue(MyUInt32_2
.CompareTo(MyUInt32_3
) < 0);
86 Assert
.IsTrue (1 == UInt32
.Parse ("1"));
87 Assert
.IsTrue (1 == UInt32
.Parse (" 1"));
88 Assert
.IsTrue (1 == UInt32
.Parse (" 1"));
89 Assert
.IsTrue (1 == UInt32
.Parse ("1 "));
90 Assert
.IsTrue (1 == UInt32
.Parse ("+1"));
93 UInt32
.Parse (" + 1 ");
94 Assert
.Fail ("Should raise FormatException1");
95 } catch (Exception e
){
96 Assert
.IsTrue (typeof (FormatException
) == e
.GetType ());
100 UInt32
.Parse (" + ");
101 Assert
.Fail ("Should raise FormatException");
102 } catch (Exception e
){
103 Assert
.IsTrue (typeof (FormatException
) == e
.GetType ());
106 MyUInt32_2
.CompareTo((object)(Int16
)100);
107 Assert
.Fail("Should raise a System.ArgumentException");
109 catch (Exception e
) {
110 Assert
.IsTrue(typeof(ArgumentException
) == e
.GetType());
115 public void TestEquals()
117 Assert
.IsTrue(MyUInt32_1
.Equals(MyUInt32_1
));
118 Assert
.IsTrue(MyUInt32_1
.Equals((object)(UInt32
)(42)));
119 Assert
.IsTrue(MyUInt32_1
.Equals((object)(SByte
)(42)) == false);
120 Assert
.IsTrue(MyUInt32_1
.Equals(MyUInt32_2
) == false);
124 public void TestGetHashCode()
127 MyUInt32_1
.GetHashCode();
128 MyUInt32_2
.GetHashCode();
129 MyUInt32_3
.GetHashCode();
132 Assert
.Fail("GetHashCode should not raise an exception here");
137 public void TestParse()
139 //test Parse(string s)
140 Assert
.IsTrue(MyUInt32_1
== UInt32
.Parse(MyString1
), "Parse problem on \""+MyString1
+"\"");
141 Assert
.IsTrue(MyUInt32_2
== UInt32
.Parse(MyString2
), "Parse problem on \""+MyString2
+"\"");
142 Assert
.IsTrue(MyUInt32_3
== UInt32
.Parse(MyString3
), "Parse problem on \""+MyString3
+"\"");
145 Assert
.Fail("Should raise a System.ArgumentNullException");
147 catch (Exception e
) {
148 Assert
.IsTrue(typeof(ArgumentNullException
) == e
.GetType(), "Did not get ArgumentNullException type");
151 UInt32
.Parse("not-a-number");
152 Assert
.Fail("Should raise a System.FormatException");
154 catch (Exception e
) {
155 Assert
.IsTrue(typeof(FormatException
) == e
.GetType(), "Did not get FormatException type");
158 // TODO: Use this after ToString() is completed. For now, hard code string that generates
160 //double OverInt = (double)UInt32.MaxValue + 1;
161 //UInt32.Parse(OverInt.ToString());
162 UInt32
.Parse("4294967296");
163 Assert
.Fail("Should raise a System.OverflowException");
165 catch (Exception e
) {
166 Assert
.IsTrue(typeof(OverflowException
) == e
.GetType(), "Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e
.GetType()+"'");
168 //test Parse(string s, NumberStyles style)
169 Assert
.IsTrue(42 == UInt32
.Parse(" "+NumberFormatInfo
.CurrentInfo
.CurrencySymbol
+"42 ", NumberStyles
.Currency
));
171 UInt32
.Parse("$42", NumberStyles
.Integer
);
172 Assert
.Fail("Should raise a System.FormatException");
174 catch (Exception e
) {
175 Assert
.IsTrue(typeof(FormatException
) == e
.GetType());
177 //test Parse(string s, IFormatProvider provider)
178 Assert
.IsTrue(42 == UInt32
.Parse(" 42 ", Nfi
));
180 UInt32
.Parse("%42", Nfi
);
181 Assert
.Fail("Should raise a System.FormatException");
183 catch (Exception e
) {
184 Assert
.IsTrue(typeof(FormatException
) == e
.GetType());
186 //test Parse(string s, NumberStyles style, IFormatProvider provider)
187 Assert
.IsTrue(16 == UInt32
.Parse(" 10 ", NumberStyles
.HexNumber
, Nfi
));
189 UInt32
.Parse("$42", NumberStyles
.Integer
, Nfi
);
190 Assert
.Fail("Should raise a System.FormatException");
191 } catch (FormatException e
) {
195 UInt32
.Parse ("5", NumberStyles
.Any
, CultureInfo
.InvariantCulture
);
196 Assert
.Fail ("C#42");
197 } catch (FormatException
) {
200 // Pass a DateTimeFormatInfo, it is unable to format
201 // numbers, but we should not crash
202 UInt32
.Parse ("123", new DateTimeFormatInfo ());
204 Assert
.AreEqual (734561, UInt32
.Parse ("734561\0"), "C#43");
206 UInt32
.Parse ("734561\0\0\0 \0");
207 Assert
.Fail ("C#44");
208 } catch (FormatException
) {}
211 UInt32
.Parse ("734561\0\0\0 ");
212 Assert
.Fail ("C#45");
213 } catch (FormatException
) {}
215 Assert
.AreEqual (734561, UInt32
.Parse ("734561\0\0\0"), "C#46");
217 Assert
.AreEqual (0, UInt32
.Parse ("0+", NumberStyles
.Any
), "#50");
221 public void TestParseExponent ()
223 Assert
.AreEqual (2, uint.Parse ("2E0", NumberStyles
.AllowExponent
), "A#1");
224 Assert
.AreEqual (20, uint.Parse ("2E1", NumberStyles
.AllowExponent
), "A#2");
225 Assert
.AreEqual (200, uint.Parse ("2E2", NumberStyles
.AllowExponent
), "A#3");
226 Assert
.AreEqual (2000000, uint.Parse ("2E6", NumberStyles
.AllowExponent
), "A#4");
227 Assert
.AreEqual (200, uint.Parse ("2E+2", NumberStyles
.AllowExponent
), "A#5");
228 Assert
.AreEqual (2, uint.Parse ("2", NumberStyles
.AllowExponent
), "A#6");
229 Assert
.AreEqual (21, uint.Parse ("2.1E1", NumberStyles
.AllowDecimalPoint
| NumberStyles
.AllowExponent
), "A#7");
230 Assert
.AreEqual (520, uint.Parse (".52E3", NumberStyles
.AllowDecimalPoint
| NumberStyles
.AllowExponent
), "A#8");
231 Assert
.AreEqual (32500000, uint.Parse ("32.5E6", NumberStyles
.AllowDecimalPoint
| NumberStyles
.AllowExponent
), "A#9");
232 Assert
.AreEqual (890, uint.Parse ("8.9000E2", NumberStyles
.AllowDecimalPoint
| NumberStyles
.AllowExponent
), "A#10");
237 } catch (FormatException
) {
241 uint.Parse ("2E3.0", NumberStyles
.AllowExponent
); // decimal notation for the exponent
243 } catch (FormatException
) {
247 uint.Parse ("2E 2", NumberStyles
.AllowExponent
);
249 } catch (FormatException
) {
253 uint.Parse ("2E2 ", NumberStyles
.AllowExponent
);
255 } catch (FormatException
) {
259 uint.Parse ("2E66", NumberStyles
.AllowExponent
); // final result overflow
261 } catch (OverflowException
) {
265 long exponent
= (long) Int32
.MaxValue
+ 10;
266 uint.Parse ("2E" + exponent
.ToString (), NumberStyles
.AllowExponent
);
268 } catch (OverflowException
) {
272 uint.Parse ("2E-1", NumberStyles
.AllowExponent
); // negative exponent
274 } catch (OverflowException
) {
278 uint.Parse ("2 math e1", NumberStyles
.AllowExponent
);
280 } catch (FormatException
) {
284 uint.Parse ("2.09E1", NumberStyles
.AllowDecimalPoint
| NumberStyles
.AllowExponent
);
286 } catch (OverflowException
) {
291 public void TestTryParse()
295 Assert
.AreEqual (true, UInt32
.TryParse (MyString1
, out result
));
296 Assert
.AreEqual (MyUInt32_1
, result
);
297 Assert
.AreEqual (true, UInt32
.TryParse (MyString2
, out result
));
298 Assert
.AreEqual (MyUInt32_2
, result
);
299 Assert
.AreEqual (true, UInt32
.TryParse (MyString3
, out result
));
300 Assert
.AreEqual (MyUInt32_3
, result
);
302 Assert
.AreEqual (true, UInt32
.TryParse ("1", out result
));
303 Assert
.AreEqual (1, result
);
304 Assert
.AreEqual (true, UInt32
.TryParse (" 1", out result
));
305 Assert
.AreEqual (1, result
);
306 Assert
.AreEqual (true, UInt32
.TryParse (" 1", out result
));
307 Assert
.AreEqual (1, result
);
308 Assert
.AreEqual (true, UInt32
.TryParse ("1 ", out result
));
309 Assert
.AreEqual (1, result
);
310 Assert
.AreEqual (true, UInt32
.TryParse ("+1", out result
));
311 Assert
.AreEqual (1, result
);
312 Assert
.AreEqual (false, UInt32
.TryParse ("-1", out result
));
313 Assert
.AreEqual (false, UInt32
.TryParse (" -1", out result
));
314 Assert
.AreEqual (false, UInt32
.TryParse (" -1 ", out result
));
315 Assert
.AreEqual (false, UInt32
.TryParse (" -1 ", out result
));
318 Assert
.AreEqual (false, UInt32
.TryParse (null, out result
));
319 Assert
.AreEqual (0, result
);
321 Assert
.AreEqual (false, UInt32
.TryParse ("not-a-number", out result
));
323 double OverInt
= (double)UInt32
.MaxValue
+ 1;
324 Assert
.AreEqual (false, UInt32
.TryParse (OverInt
.ToString (), out result
));
325 Assert
.AreEqual (false, UInt32
.TryParse (OverInt
.ToString (), NumberStyles
.None
, CultureInfo
.InvariantCulture
, out result
));
327 Assert
.AreEqual (false, UInt32
.TryParse ("$42", NumberStyles
.Integer
, null, out result
));
328 Assert
.AreEqual (false, UInt32
.TryParse ("%42", NumberStyles
.Integer
, Nfi
, out result
));
329 Assert
.AreEqual (false, UInt32
.TryParse ("$42", NumberStyles
.Integer
, Nfi
, out result
));
330 Assert
.AreEqual (false, UInt32
.TryParse (" - 1 ", out result
));
331 Assert
.AreEqual (false, UInt32
.TryParse (" - ", out result
));
332 Assert
.AreEqual (false, UInt32
.TryParse ("100000000", NumberStyles
.HexNumber
, Nfi
, out result
));
333 Assert
.AreEqual (false, UInt32
.TryParse ("10000000000", out result
));
334 Assert
.AreEqual (false, UInt32
.TryParse ("-10000000000", out result
));
335 Assert
.AreEqual (true, UInt32
.TryParse ("7fffffff", NumberStyles
.HexNumber
, Nfi
, out result
));
336 Assert
.AreEqual (Int32
.MaxValue
, result
);
337 Assert
.AreEqual (true, UInt32
.TryParse ("80000000", NumberStyles
.HexNumber
, Nfi
, out result
));
338 Assert
.AreEqual (Int32
.MaxValue
+ (uint)1, result
);
339 Assert
.AreEqual (true, UInt32
.TryParse ("ffffffff", NumberStyles
.HexNumber
, Nfi
, out result
));
340 Assert
.AreEqual (uint.MaxValue
, result
);
341 Assert
.IsFalse (uint.TryParse ("-", NumberStyles
.AllowLeadingSign
, Nfi
, out result
));
342 Assert
.IsFalse (uint.TryParse (Nfi
.CurrencySymbol
+ "-", NumberStyles
.AllowLeadingSign
| NumberStyles
.AllowCurrencySymbol
, Nfi
, out result
));
346 public void TestToString()
351 Assert
.AreEqual(MyString1
, MyUInt32_1
.ToString());
353 Assert
.AreEqual(MyString2
, MyUInt32_2
.ToString());
355 Assert
.AreEqual(MyString3
, MyUInt32_3
.ToString());
356 } catch (Exception e
) {
357 Assert
.Fail("TestToString: Assert.Failed on TestNumber=" + TestNumber
358 + " with exception: " + e
.ToString());
361 //test ToString(string format)
362 for (int i
=0; i
< Formats1
.Length
; i
++) {
364 Assert
.AreEqual(Results1
[i
], MyUInt32_2
.ToString(Formats1
[i
]));
365 } catch (Exception e
) {
366 Assert
.Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i
367 + ". e = " + e
.ToString());
371 Assert
.AreEqual(Results2
[i
], MyUInt32_3
.ToString(Formats2
[i
]));
372 } catch (Exception e
) {
373 Assert
.Fail("TestToString: MyUInt32_3.ToString(Formats2[i]) i=" + i
374 + ". e = " + e
.ToString());
377 //test ToString(string format, IFormatProvider provider);
378 for (int i
=0; i
< Formats1
.Length
; i
++) {
379 Assert
.AreEqual(ResultsNfi1
[i
], MyUInt32_2
.ToString(Formats1
[i
], Nfi
));
380 Assert
.AreEqual(ResultsNfi2
[i
], MyUInt32_3
.ToString(Formats2
[i
], Nfi
));
383 MyUInt32_1
.ToString("z");
384 Assert
.Fail("Should raise a System.FormatException");
386 catch (Exception e
) {
387 Assert
.IsTrue(typeof(FormatException
) == e
.GetType());
392 public void ToString_Defaults ()
395 // everything defaults to "G"
396 string def
= i
.ToString ("G");
397 Assert
.AreEqual (def
, i
.ToString (), "ToString()");
398 Assert
.AreEqual (def
, i
.ToString ((IFormatProvider
)null), "ToString((IFormatProvider)null)");
399 Assert
.AreEqual (def
, i
.ToString ((string)null), "ToString((string)null)");
400 Assert
.AreEqual (def
, i
.ToString (String
.Empty
), "ToString(empty)");
401 Assert
.AreEqual (def
, i
.ToString (null, null), "ToString(null,null)");
402 Assert
.AreEqual (def
, i
.ToString (String
.Empty
, null), "ToString(empty,null)");
404 Assert
.AreEqual ("254", def
, "ToString(G)");