1 // UInt32Test.cs - NUnit Test Cases for the System.UInt32 struct 2 // 3 // Mario Martinez (mariom925@home.om) 4 // 5 // (C) Ximian, Inc. http://www.ximian.com 6 // 7 8 using NUnit.Framework; 9 using System; 10 using System.Globalization; 11 using System.Threading; 12 13 namespace MonoTests.System 14 { 15 16 [TestFixture] 17 public class UInt32Test 18 { 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; 40 41 private CultureInfo old_culture; 42 43 [SetUp] SetUp()44 public void SetUp () 45 { 46 old_culture = Thread.CurrentThread.CurrentCulture; 47 48 // Set culture to en-US and don't let the user override. 49 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false); 50 51 // We can't initialize this until we set the culture. 52 53 string decimals = new String ('0', NumberFormatInfo.CurrentInfo.NumberDecimalDigits); 54 string perPattern = new string[] {"n %","n%","%n"} [NumberFormatInfo.CurrentInfo.PercentPositivePattern]; 55 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"); 60 61 Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol + "4,294,967,295.00000"; 62 Results2 [6] = perPattern.Replace ("n","429,496,729,500.00000"); 63 } 64 65 [TearDown] TearDown()66 public void TearDown () 67 { 68 Thread.CurrentThread.CurrentCulture = old_culture; 69 } 70 71 [Test] TestMinMax()72 public void TestMinMax() 73 { 74 75 Assert.AreEqual(UInt32.MinValue, MyUInt32_2); 76 Assert.AreEqual(UInt32.MaxValue, MyUInt32_3); 77 } 78 79 [Test] TestCompareTo()80 public void TestCompareTo() 81 { 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")); 91 92 try { 93 UInt32.Parse (" + 1 "); 94 Assert.Fail ("Should raise FormatException1"); 95 } catch (Exception e){ 96 Assert.IsTrue (typeof (FormatException) == e.GetType ()); 97 } 98 99 try { 100 UInt32.Parse (" + "); 101 Assert.Fail ("Should raise FormatException"); 102 } catch (Exception e){ 103 Assert.IsTrue (typeof (FormatException) == e.GetType ()); 104 } 105 try { 106 MyUInt32_2.CompareTo((object)(Int16)100); 107 Assert.Fail("Should raise a System.ArgumentException"); 108 } 109 catch (Exception e) { 110 Assert.IsTrue(typeof(ArgumentException) == e.GetType()); 111 } 112 } 113 114 [Test] TestEquals()115 public void TestEquals() 116 { 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); 121 } 122 123 [Test] TestGetHashCode()124 public void TestGetHashCode() 125 { 126 try { 127 MyUInt32_1.GetHashCode(); 128 MyUInt32_2.GetHashCode(); 129 MyUInt32_3.GetHashCode(); 130 } 131 catch { 132 Assert.Fail("GetHashCode should not raise an exception here"); 133 } 134 } 135 136 [Test] TestParse()137 public void TestParse() 138 { 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+"\""); 143 try { 144 UInt32.Parse(null); 145 Assert.Fail("Should raise a System.ArgumentNullException"); 146 } 147 catch (Exception e) { 148 Assert.IsTrue(typeof(ArgumentNullException) == e.GetType(), "Did not get ArgumentNullException type"); 149 } 150 try { 151 UInt32.Parse("not-a-number"); 152 Assert.Fail("Should raise a System.FormatException"); 153 } 154 catch (Exception e) { 155 Assert.IsTrue(typeof(FormatException) == e.GetType(), "Did not get FormatException type"); 156 } 157 try { 158 // TODO: Use this after ToString() is completed. For now, hard code string that generates 159 // exception. 160 //double OverInt = (double)UInt32.MaxValue + 1; 161 //UInt32.Parse(OverInt.ToString()); 162 UInt32.Parse("4294967296"); 163 Assert.Fail("Should raise a System.OverflowException"); 164 } 165 catch (Exception e) { 166 Assert.IsTrue(typeof(OverflowException) == e.GetType(), "Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e.GetType()+"'"); 167 } 168 //test Parse(string s, NumberStyles style) 169 Assert.IsTrue(42 == UInt32.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency)); 170 try { 171 UInt32.Parse("$42", NumberStyles.Integer); 172 Assert.Fail("Should raise a System.FormatException"); 173 } 174 catch (Exception e) { 175 Assert.IsTrue(typeof(FormatException) == e.GetType()); 176 } 177 //test Parse(string s, IFormatProvider provider) 178 Assert.IsTrue(42 == UInt32.Parse(" 42 ", Nfi)); 179 try { 180 UInt32.Parse("%42", Nfi); 181 Assert.Fail("Should raise a System.FormatException"); 182 } 183 catch (Exception e) { 184 Assert.IsTrue(typeof(FormatException) == e.GetType()); 185 } 186 //test Parse(string s, NumberStyles style, IFormatProvider provider) 187 Assert.IsTrue(16 == UInt32.Parse(" 10 ", NumberStyles.HexNumber, Nfi)); 188 try { 189 UInt32.Parse("$42", NumberStyles.Integer, Nfi); 190 Assert.Fail("Should raise a System.FormatException"); 191 } catch (FormatException e) { 192 } 193 194 try { 195 UInt32.Parse ("5", NumberStyles.Any, CultureInfo.InvariantCulture); 196 Assert.Fail ("C#42"); 197 } catch (FormatException) { 198 } 199 200 // Pass a DateTimeFormatInfo, it is unable to format 201 // numbers, but we should not crash 202 UInt32.Parse ("123", new DateTimeFormatInfo ()); 203 204 Assert.AreEqual (734561, UInt32.Parse ("734561\0"), "C#43"); 205 try { 206 UInt32.Parse ("734561\0\0\0 \0"); 207 Assert.Fail ("C#44"); 208 } catch (FormatException) {} 209 210 try { 211 UInt32.Parse ("734561\0\0\0 "); 212 Assert.Fail ("C#45"); 213 } catch (FormatException) {} 214 215 Assert.AreEqual (734561, UInt32.Parse ("734561\0\0\0"), "C#46"); 216 217 Assert.AreEqual (0, UInt32.Parse ("0+", NumberStyles.Any), "#50"); 218 } 219 220 [Test] TestParseExponent()221 public void TestParseExponent () 222 { 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"); 233 234 try { 235 uint.Parse ("2E"); 236 Assert.Fail ("B#1"); 237 } catch (FormatException) { 238 } 239 240 try { 241 uint.Parse ("2E3.0", NumberStyles.AllowExponent); // decimal notation for the exponent 242 Assert.Fail ("B#2"); 243 } catch (FormatException) { 244 } 245 246 try { 247 uint.Parse ("2E 2", NumberStyles.AllowExponent); 248 Assert.Fail ("B#3"); 249 } catch (FormatException) { 250 } 251 252 try { 253 uint.Parse ("2E2 ", NumberStyles.AllowExponent); 254 Assert.Fail ("B#4"); 255 } catch (FormatException) { 256 } 257 258 try { 259 uint.Parse ("2E66", NumberStyles.AllowExponent); // final result overflow 260 Assert.Fail ("B#5"); 261 } catch (OverflowException) { 262 } 263 264 try { 265 long exponent = (long) Int32.MaxValue + 10; 266 uint.Parse ("2E" + exponent.ToString (), NumberStyles.AllowExponent); 267 Assert.Fail ("B#6"); 268 } catch (OverflowException) { 269 } 270 271 try { 272 uint.Parse ("2E-1", NumberStyles.AllowExponent); // negative exponent 273 Assert.Fail ("B#7"); 274 } catch (OverflowException) { 275 } 276 277 try { 278 uint.Parse ("2 math e1", NumberStyles.AllowExponent); 279 Assert.Fail ("B#8"); 280 } catch (FormatException) { 281 } 282 283 try { 284 uint.Parse ("2.09E1", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent); 285 Assert.Fail ("B#9"); 286 } catch (OverflowException) { 287 } 288 } 289 290 [Test] TestTryParse()291 public void TestTryParse() 292 { 293 uint result; 294 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); 301 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)); 316 317 result = 1; 318 Assert.AreEqual (false, UInt32.TryParse (null, out result)); 319 Assert.AreEqual (0, result); 320 321 Assert.AreEqual (false, UInt32.TryParse ("not-a-number", out result)); 322 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)); 326 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)); 343 } 344 345 [Test] TestToString()346 public void TestToString() 347 { 348 int TestNumber = 1; 349 try { 350 //test ToString() 351 Assert.AreEqual(MyString1, MyUInt32_1.ToString()); 352 TestNumber++; 353 Assert.AreEqual(MyString2, MyUInt32_2.ToString()); 354 TestNumber++; 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()); 359 } 360 361 //test ToString(string format) 362 for (int i=0; i < Formats1.Length; i++) { 363 try { 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()); 368 } 369 370 try { 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()); 375 } 376 } 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)); 381 } 382 try { 383 MyUInt32_1.ToString("z"); 384 Assert.Fail("Should raise a System.FormatException"); 385 } 386 catch (Exception e) { 387 Assert.IsTrue(typeof(FormatException) == e.GetType()); 388 } 389 } 390 391 [Test] ToString_Defaults()392 public void ToString_Defaults () 393 { 394 UInt32 i = 254; 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)"); 403 404 Assert.AreEqual ("254", def, "ToString(G)"); 405 } 406 } 407 408 409 } 410