UInt64ConverterTests.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // System.ComponentModel.UInt64Converter 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 UInt64ConverterTests
  18. {
  19. private UInt64Converter converter;
  20. [SetUp]
  21. public void SetUp ()
  22. {
  23. converter = new UInt64Converter ();
  24. }
  25. [Test]
  26. public void CanConvertFrom ()
  27. {
  28. Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
  29. Assert.IsFalse (converter.CanConvertFrom (typeof (ulong)), "#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 (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
  43. Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
  44. Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
  45. Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
  46. Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
  47. }
  48. [Test]
  49. public void ConvertFrom_MaxValue ()
  50. {
  51. Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffffffffffffffff"), "#1");
  52. Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFFFFFFFFFFFFFF"), "#2");
  53. Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffffffffffffffff"), "#3");
  54. Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFFFFFFFFFFFFFF"), "#4");
  55. Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffffffffffffffff"), "#5");
  56. Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFFFFFFFFFFFFFF"), "#6");
  57. }
  58. [Test]
  59. [ExpectedException (typeof (NotSupportedException))]
  60. public void ConvertFrom_Object ()
  61. {
  62. converter.ConvertFrom (new object ());
  63. }
  64. [Test]
  65. [ExpectedException (typeof (NotSupportedException))]
  66. public void ConvertFrom_Int32 ()
  67. {
  68. converter.ConvertFrom (10);
  69. }
  70. [Test]
  71. public void ConvertTo_MinValue ()
  72. {
  73. Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.InvariantCulture),
  74. converter.ConvertTo (null, CultureInfo.InvariantCulture, ulong.MinValue,
  75. typeof (string)), "#1");
  76. Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.CurrentCulture),
  77. converter.ConvertTo (null, CultureInfo.CurrentCulture, ulong.MinValue,
  78. typeof (string)), "#2");
  79. Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.CurrentCulture),
  80. converter.ConvertTo (ulong.MinValue, typeof (string)), "#3");
  81. }
  82. [Test]
  83. public void ConvertTo_MaxValue ()
  84. {
  85. Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.InvariantCulture),
  86. converter.ConvertTo (null, CultureInfo.InvariantCulture, ulong.MaxValue,
  87. typeof (string)), "#1");
  88. Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.CurrentCulture),
  89. converter.ConvertTo (null, CultureInfo.CurrentCulture, ulong.MaxValue,
  90. typeof (string)), "#2");
  91. Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.CurrentCulture),
  92. converter.ConvertTo (ulong.MaxValue, typeof (string)), "#3");
  93. }
  94. [Test]
  95. public void ConvertToString ()
  96. {
  97. CultureInfo culture = new MyCultureInfo ();
  98. NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
  99. Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
  100. }
  101. [Test]
  102. public void ConvertFromString_Invalid1 ()
  103. {
  104. try {
  105. converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
  106. Assert.Fail ("#1");
  107. } catch (AssertionException) {
  108. throw;
  109. } catch (Exception ex) {
  110. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  111. Assert.IsNotNull (ex.InnerException, "#3");
  112. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  113. }
  114. }
  115. [Test]
  116. public void ConvertFromString_Invalid2 ()
  117. {
  118. try {
  119. converter.ConvertFromString (null, CultureInfo.InvariantCulture,
  120. double.MaxValue.ToString(CultureInfo.InvariantCulture));
  121. Assert.Fail ("#1");
  122. } catch (AssertionException) {
  123. throw;
  124. } catch (Exception ex) {
  125. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  126. Assert.IsNotNull (ex.InnerException, "#3");
  127. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  128. }
  129. }
  130. [Test]
  131. public void ConvertFromString_Invalid3 ()
  132. {
  133. try {
  134. converter.ConvertFromString ("*1");
  135. Assert.Fail ("#1");
  136. } catch (AssertionException) {
  137. throw;
  138. } catch (Exception ex) {
  139. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  140. Assert.IsNotNull (ex.InnerException, "#3");
  141. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  142. }
  143. }
  144. [Test]
  145. public void ConvertFromString_Invalid4 ()
  146. {
  147. try {
  148. converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
  149. Assert.Fail ("#1");
  150. } catch (AssertionException) {
  151. throw;
  152. } catch (Exception ex) {
  153. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  154. Assert.IsNotNull (ex.InnerException, "#3");
  155. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  156. }
  157. }
  158. [Test]
  159. public void ConvertFrom_InvalidString1 ()
  160. {
  161. try {
  162. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
  163. Assert.Fail ("#1");
  164. } catch (AssertionException) {
  165. throw;
  166. } catch (Exception ex) {
  167. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  168. Assert.IsNotNull (ex.InnerException, "#3");
  169. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  170. }
  171. }
  172. [Test]
  173. public void ConvertFrom_InvalidString2 ()
  174. {
  175. try {
  176. converter.ConvertFrom (null, CultureInfo.InvariantCulture,
  177. double.MaxValue.ToString (CultureInfo.InvariantCulture));
  178. Assert.Fail ("#1");
  179. } catch (AssertionException) {
  180. throw;
  181. } catch (Exception ex) {
  182. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  183. Assert.IsNotNull (ex.InnerException, "#3");
  184. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  185. }
  186. }
  187. [Test]
  188. public void ConvertFrom_InvalidString3 ()
  189. {
  190. try {
  191. converter.ConvertFrom ("*1");
  192. Assert.Fail ("#1");
  193. } catch (AssertionException) {
  194. throw;
  195. } catch (Exception ex) {
  196. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  197. Assert.IsNotNull (ex.InnerException, "#3");
  198. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  199. }
  200. }
  201. [Test]
  202. public void ConvertFrom_InvalidString4 ()
  203. {
  204. try {
  205. converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
  206. Assert.Fail ("#1");
  207. } catch (AssertionException) {
  208. throw;
  209. } catch (Exception ex) {
  210. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  211. Assert.IsNotNull (ex.InnerException, "#3");
  212. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  213. }
  214. }
  215. [Serializable]
  216. private sealed class MyCultureInfo : CultureInfo
  217. {
  218. internal MyCultureInfo ()
  219. : base ("en-US")
  220. {
  221. }
  222. public override object GetFormat (Type formatType)
  223. {
  224. if (formatType == typeof (NumberFormatInfo)) {
  225. NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
  226. nfi.NegativeSign = "myNegativeSign";
  227. return NumberFormatInfo.ReadOnly (nfi);
  228. } else {
  229. return base.GetFormat (formatType);
  230. }
  231. }
  232. #if NET_2_0
  233. // adding this override in 1.x shows different result in .NET (it is ignored).
  234. // Some compatibility kids might want to fix this issue.
  235. public override NumberFormatInfo NumberFormat {
  236. get {
  237. NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
  238. nfi.NegativeSign = "myNegativeSign";
  239. return nfi;
  240. }
  241. set { throw new NotSupportedException (); }
  242. }
  243. #endif
  244. }
  245. }
  246. }