ByteConverterTests.cs 17 KB

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