1 // 2 // System.ComponentModel.ByteConverter test cases 3 // 4 // Authors: 5 // Gert Driesen (drieseng@users.sourceforge.net) 6 // 7 // (c) 2005 Novell, Inc. (http://www.ximian.com) 8 // 9 10 using System; 11 using System.ComponentModel; 12 using System.ComponentModel.Design.Serialization; 13 using System.Globalization; 14 15 using NUnit.Framework; 16 17 namespace MonoTests.System.ComponentModel 18 { 19 [TestFixture] 20 public class ByteConverterTests 21 { 22 private ByteConverter converter; 23 24 [SetUp] SetUp()25 public void SetUp () 26 { 27 converter = new ByteConverter (); 28 } 29 30 [Test] CanConvertFrom()31 public void CanConvertFrom () 32 { 33 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1"); 34 Assert.IsFalse (converter.CanConvertFrom (typeof (byte)), "#2"); 35 Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#3"); 36 Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#4"); 37 } 38 39 [Test] CanConvertTo()40 public void CanConvertTo () 41 { 42 Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1"); 43 Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#2"); 44 Assert.IsTrue (converter.CanConvertTo (typeof (int)), "#3"); 45 } 46 47 [Test] ConvertFrom_MinValue()48 public void ConvertFrom_MinValue () 49 { 50 Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1"); 51 Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2"); 52 Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3"); 53 Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4"); 54 Assert.AreEqual (byte.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5"); 55 } 56 57 [Test] ConvertFrom_MaxValue()58 public void ConvertFrom_MaxValue () 59 { 60 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ff"), "#1"); 61 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FF"), "#2"); 62 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xff"), "#3"); 63 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFF"), "#4"); 64 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xff"), "#5"); 65 Assert.AreEqual (byte.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFF"), "#6"); 66 } 67 68 [Test] ConvertToString()69 public void ConvertToString () 70 { 71 CultureInfo culture = new MyCultureInfo (); 72 NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo)); 73 74 Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5)); 75 } 76 77 [Test] 78 [ExpectedException (typeof (NotSupportedException))] ConvertFrom_Object()79 public void ConvertFrom_Object () 80 { 81 converter.ConvertFrom (new object ()); 82 } 83 84 [Test] 85 [ExpectedException (typeof (NotSupportedException))] ConvertFrom_Byte()86 public void ConvertFrom_Byte () 87 { 88 converter.ConvertFrom (byte.MaxValue); 89 } 90 91 [Test] 92 [ExpectedException (typeof (NotSupportedException))] ConvertFrom_Int16()93 public void ConvertFrom_Int16 () 94 { 95 converter.ConvertFrom ((short) 10); 96 } 97 98 [Test] 99 [ExpectedException (typeof (NotSupportedException))] ConvertFrom_Int32()100 public void ConvertFrom_Int32 () 101 { 102 converter.ConvertFrom (10); 103 } 104 105 [Test] ConvertTo_MinValue()106 public void ConvertTo_MinValue () 107 { 108 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.InvariantCulture), 109 converter.ConvertTo (null, CultureInfo.InvariantCulture, byte.MinValue, 110 typeof (string)), "#1"); 111 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture), 112 converter.ConvertTo (null, CultureInfo.CurrentCulture, byte.MinValue, 113 typeof (string)), "#2"); 114 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture), 115 converter.ConvertTo (byte.MinValue, typeof (string)), "#3"); 116 } 117 118 [Test] ConvertTo_MaxValue()119 public void ConvertTo_MaxValue () 120 { 121 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.InvariantCulture), 122 converter.ConvertTo (null, CultureInfo.InvariantCulture, byte.MaxValue, 123 typeof (string)), "#1"); 124 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture), 125 converter.ConvertTo (null, CultureInfo.CurrentCulture, byte.MaxValue, 126 typeof (string)), "#2"); 127 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture), 128 converter.ConvertTo (byte.MaxValue, typeof (string)), "#3"); 129 } 130 131 [Test] ConvertToString_MinValue()132 public void ConvertToString_MinValue () 133 { 134 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.InvariantCulture), 135 converter.ConvertToString (null, CultureInfo.InvariantCulture, 136 byte.MinValue), "#1"); 137 138 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture), 139 converter.ConvertToString (null, byte.MinValue), "#2"); 140 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture), 141 converter.ConvertToString (null, CultureInfo.CurrentCulture, 142 byte.MinValue), "#3"); 143 Assert.AreEqual (byte.MinValue.ToString (CultureInfo.CurrentCulture), 144 converter.ConvertToString (byte.MinValue), "#4"); 145 } 146 147 [Test] ConvertToString_MaxValue()148 public void ConvertToString_MaxValue () 149 { 150 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.InvariantCulture), 151 converter.ConvertToString (null, CultureInfo.InvariantCulture, 152 byte.MaxValue), "#1"); 153 154 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture), 155 converter.ConvertToString (null, byte.MaxValue), "#2"); 156 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture), 157 converter.ConvertToString (null, CultureInfo.CurrentCulture, 158 byte.MaxValue), "#3"); 159 Assert.AreEqual (byte.MaxValue.ToString (CultureInfo.CurrentCulture), 160 converter.ConvertToString (byte.MaxValue), "#4"); 161 } 162 163 [Test] ConvertFrom_InvalidValue()164 public void ConvertFrom_InvalidValue () 165 { 166 try { 167 converter.ConvertFrom ("*1"); 168 Assert.Fail ("#1"); 169 } catch (AssertionException) { 170 throw; 171 } catch (Exception ex) { 172 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 173 Assert.IsNotNull (ex.InnerException, "#3"); 174 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3"); 175 } 176 } 177 178 [Test] ConvertFrom_InvalidValue_Invariant()179 public void ConvertFrom_InvalidValue_Invariant () 180 { 181 try { 182 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1"); 183 Assert.Fail ("#1"); 184 } catch (AssertionException) { 185 throw; 186 } catch (Exception ex) { 187 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 188 Assert.IsNotNull (ex.InnerException, "#3"); 189 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3"); 190 } 191 } 192 193 [Test] ConvertFrom_Base10_MinOverflow()194 public void ConvertFrom_Base10_MinOverflow () 195 { 196 string minOverflow = ((int) (byte.MinValue - 1)).ToString ( 197 CultureInfo.CurrentCulture); 198 199 try { 200 converter.ConvertFrom (minOverflow); 201 Assert.Fail ("#1"); 202 } catch (AssertionException) { 203 throw; 204 } catch (Exception ex) { 205 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 206 Assert.IsNotNull (ex.InnerException, "#3"); 207 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 208 } 209 } 210 211 [Test] ConvertFrom_Base10_MinOverflow_Invariant()212 public void ConvertFrom_Base10_MinOverflow_Invariant () 213 { 214 string minOverflow = ((int) (byte.MinValue - 1)).ToString ( 215 CultureInfo.InvariantCulture); 216 217 try { 218 converter.ConvertFrom (null, CultureInfo.InvariantCulture, 219 minOverflow); 220 Assert.Fail ("#1"); 221 } catch (AssertionException) { 222 throw; 223 } catch (Exception ex) { 224 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 225 Assert.IsNotNull (ex.InnerException, "#3"); 226 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 227 } 228 } 229 230 [Test] ConvertFrom_Base10_MaxOverflow()231 public void ConvertFrom_Base10_MaxOverflow () 232 { 233 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ( 234 CultureInfo.CurrentCulture); 235 236 try { 237 converter.ConvertFrom (maxOverflow); 238 Assert.Fail ("#1"); 239 } catch (AssertionException) { 240 throw; 241 } catch (Exception ex) { 242 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 243 Assert.IsNotNull (ex.InnerException, "#3"); 244 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 245 } 246 } 247 248 [Test] ConvertFrom_Base10_MaxOverflow_Invariant()249 public void ConvertFrom_Base10_MaxOverflow_Invariant () 250 { 251 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ( 252 CultureInfo.InvariantCulture); 253 254 try { 255 converter.ConvertFrom (null, CultureInfo.InvariantCulture, 256 maxOverflow); 257 Assert.Fail ("#1"); 258 } catch (AssertionException) { 259 throw; 260 } catch (Exception ex) { 261 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 262 Assert.IsNotNull (ex.InnerException, "#3"); 263 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 264 } 265 } 266 267 [Test] ConvertFrom_Base16_MinOverflow()268 public void ConvertFrom_Base16_MinOverflow () 269 { 270 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x", 271 CultureInfo.CurrentCulture); 272 273 try { 274 converter.ConvertFrom ("#" + minOverflow); 275 Assert.Fail ("#1"); 276 } catch (AssertionException) { 277 throw; 278 } catch (Exception ex) { 279 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 280 Assert.IsNotNull (ex.InnerException, "#3"); 281 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 282 } 283 } 284 285 [Test] ConvertFrom_Base16_MinOverflow_Invariant()286 public void ConvertFrom_Base16_MinOverflow_Invariant () 287 { 288 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x", 289 CultureInfo.InvariantCulture); 290 291 try { 292 converter.ConvertFrom (null, CultureInfo.InvariantCulture, 293 "#" + minOverflow); 294 Assert.Fail ("#1"); 295 } catch (AssertionException) { 296 throw; 297 } catch (Exception ex) { 298 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 299 Assert.IsNotNull (ex.InnerException, "#3"); 300 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 301 } 302 } 303 304 [Test] ConvertFrom_Base16_MaxOverflow()305 public void ConvertFrom_Base16_MaxOverflow () 306 { 307 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x", 308 CultureInfo.CurrentCulture); 309 310 try { 311 converter.ConvertFrom ("#" + maxOverflow); 312 Assert.Fail ("#1"); 313 } catch (AssertionException) { 314 throw; 315 } catch (Exception ex) { 316 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 317 Assert.IsNotNull (ex.InnerException, "#3"); 318 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 319 } 320 } 321 322 [Test] ConvertFrom_Base16_MaxOverflow_Invariant()323 public void ConvertFrom_Base16_MaxOverflow_Invariant () 324 { 325 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x", 326 CultureInfo.InvariantCulture); 327 328 try { 329 converter.ConvertFrom (null, CultureInfo.InvariantCulture, 330 "#" + maxOverflow); 331 Assert.Fail ("#1"); 332 } catch (AssertionException) { 333 throw; 334 } catch (Exception ex) { 335 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 336 Assert.IsNotNull (ex.InnerException, "#3"); 337 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 338 } 339 } 340 341 [Test] ConvertFromString_InvalidValue()342 public void ConvertFromString_InvalidValue () 343 { 344 try { 345 converter.ConvertFromString ("*1"); 346 Assert.Fail ("#1"); 347 } catch (AssertionException) { 348 throw; 349 } catch (Exception ex) { 350 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 351 Assert.IsNotNull (ex.InnerException, "#3"); 352 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3"); 353 } 354 } 355 356 [Test] ConvertFromString_InvalidValue_Invariant()357 public void ConvertFromString_InvalidValue_Invariant () 358 { 359 try { 360 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1"); 361 Assert.Fail ("#1"); 362 } catch (AssertionException) { 363 throw; 364 } catch (Exception ex) { 365 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 366 Assert.IsNotNull (ex.InnerException, "#3"); 367 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3"); 368 } 369 } 370 371 [Test] ConvertFromString_Base10_MinOverflow()372 public void ConvertFromString_Base10_MinOverflow () 373 { 374 string minOverflow = ((int) (byte.MinValue - 1)).ToString ( 375 CultureInfo.CurrentCulture); 376 377 try { 378 converter.ConvertFromString (minOverflow); 379 Assert.Fail ("#1"); 380 } catch (AssertionException) { 381 throw; 382 } catch (Exception ex) { 383 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 384 Assert.IsNotNull (ex.InnerException, "#3"); 385 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 386 } 387 } 388 389 [Test] ConvertFromString_Base10_MinOverflow_Invariant()390 public void ConvertFromString_Base10_MinOverflow_Invariant () 391 { 392 string minOverflow = ((int) (byte.MinValue - 1)).ToString ( 393 CultureInfo.InvariantCulture); 394 395 try { 396 converter.ConvertFromString (null, CultureInfo.InvariantCulture, 397 minOverflow); 398 Assert.Fail ("#1"); 399 } catch (AssertionException) { 400 throw; 401 } catch (Exception ex) { 402 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 403 Assert.IsNotNull (ex.InnerException, "#3"); 404 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 405 } 406 } 407 408 [Test] ConvertFromString_Base10_MaxOverflow()409 public void ConvertFromString_Base10_MaxOverflow () 410 { 411 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ( 412 CultureInfo.CurrentCulture); 413 414 try { 415 converter.ConvertFromString (maxOverflow); 416 Assert.Fail ("#1"); 417 } catch (AssertionException) { 418 throw; 419 } catch (Exception ex) { 420 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 421 Assert.IsNotNull (ex.InnerException, "#3"); 422 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 423 } 424 } 425 426 [Test] ConvertFromString_Base10_MaxOverflow_Invariant()427 public void ConvertFromString_Base10_MaxOverflow_Invariant () 428 { 429 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ( 430 CultureInfo.InvariantCulture); 431 432 try { 433 converter.ConvertFromString (null, CultureInfo.InvariantCulture, 434 maxOverflow); 435 Assert.Fail ("#1"); 436 } catch (AssertionException) { 437 throw; 438 } catch (Exception ex) { 439 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 440 Assert.IsNotNull (ex.InnerException, "#3"); 441 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 442 } 443 } 444 445 [Test] ConvertFromString_Base16_MinOverflow()446 public void ConvertFromString_Base16_MinOverflow () 447 { 448 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x", 449 CultureInfo.CurrentCulture); 450 451 try { 452 converter.ConvertFromString ("#" + minOverflow); 453 Assert.Fail ("#1"); 454 } catch (AssertionException) { 455 throw; 456 } catch (Exception ex) { 457 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 458 Assert.IsNotNull (ex.InnerException, "#3"); 459 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 460 } 461 } 462 463 [Test] ConvertFromString_Base16_MinOverflow_Invariant()464 public void ConvertFromString_Base16_MinOverflow_Invariant () 465 { 466 string minOverflow = ((int) (byte.MinValue - 1)).ToString ("x", 467 CultureInfo.InvariantCulture); 468 469 try { 470 converter.ConvertFromString (null, CultureInfo.InvariantCulture, 471 "#" + minOverflow); 472 Assert.Fail ("#1"); 473 } catch (AssertionException) { 474 throw; 475 } catch (Exception ex) { 476 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 477 Assert.IsNotNull (ex.InnerException, "#3"); 478 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 479 } 480 } 481 482 [Test] ConvertFromString_Base16_MaxOverflow()483 public void ConvertFromString_Base16_MaxOverflow () 484 { 485 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x", 486 CultureInfo.CurrentCulture); 487 488 try { 489 converter.ConvertFromString ("#" + maxOverflow); 490 Assert.Fail ("#1"); 491 } catch (AssertionException) { 492 throw; 493 } catch (Exception ex) { 494 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 495 Assert.IsNotNull (ex.InnerException, "#3"); 496 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 497 } 498 } 499 500 [Test] ConvertFromString_Base16_MaxOverflow_Invariant()501 public void ConvertFromString_Base16_MaxOverflow_Invariant () 502 { 503 string maxOverflow = ((int) (byte.MaxValue + 1)).ToString ("x", 504 CultureInfo.InvariantCulture); 505 506 try { 507 converter.ConvertFromString (null, CultureInfo.InvariantCulture, 508 "#" + maxOverflow); 509 Assert.Fail ("#1"); 510 } catch (AssertionException) { 511 throw; 512 } catch (Exception ex) { 513 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 514 Assert.IsNotNull (ex.InnerException, "#3"); 515 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3"); 516 } 517 } 518 519 [Serializable] 520 private sealed class MyCultureInfo : CultureInfo 521 { MyCultureInfo()522 internal MyCultureInfo () 523 : base ("en-US") 524 { 525 } 526 GetFormat(Type formatType)527 public override object GetFormat (Type formatType) 528 { 529 if (formatType == typeof (NumberFormatInfo)) { 530 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone (); 531 532 nfi.NegativeSign = "myNegativeSign"; 533 return NumberFormatInfo.ReadOnly (nfi); 534 } else { 535 return base.GetFormat (formatType); 536 } 537 } 538 539 // adding this override in 1.x shows different result in .NET (it is ignored). 540 // Some compatibility kids might want to fix this issue. 541 public override NumberFormatInfo NumberFormat { 542 get { 543 NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone (); 544 nfi.NegativeSign = "myNegativeSign"; 545 return nfi; 546 } 547 set { throw new NotSupportedException (); } 548 } 549 } 550 } 551 } 552