Int64ConverterTests.cs 9.0 KB

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