DoubleConverterTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //
  2. // System.ComponentModel.DoubleConverter 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 DoubleConverterTests
  18. {
  19. private DoubleConverter converter;
  20. [SetUp]
  21. public void SetUp ()
  22. {
  23. converter = new DoubleConverter ();
  24. }
  25. [Test]
  26. public void CanConvertFrom ()
  27. {
  28. Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
  29. Assert.IsFalse (converter.CanConvertFrom (typeof (double)), "#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_String ()
  41. {
  42. Assert.AreEqual (10, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "10"), "#1");
  43. }
  44. [Test]
  45. [ExpectedException (typeof (NotSupportedException))]
  46. public void ConvertFrom_Object ()
  47. {
  48. converter.ConvertFrom (new object ());
  49. }
  50. [Test]
  51. [ExpectedException (typeof (NotSupportedException))]
  52. public void ConvertFrom_Int32 ()
  53. {
  54. converter.ConvertFrom (int.MaxValue);
  55. }
  56. [Test]
  57. public void ConvertTo_Negative ()
  58. {
  59. Assert.AreEqual ((-1.5D).ToString (CultureInfo.InvariantCulture),
  60. converter.ConvertTo (null, CultureInfo.InvariantCulture, -1.5D,
  61. typeof (string)), "#1");
  62. Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
  63. converter.ConvertTo (null, CultureInfo.CurrentCulture, -1.5D,
  64. typeof (string)), "#2");
  65. Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
  66. converter.ConvertTo (-1.5D, typeof (string)), "#3");
  67. }
  68. [Test]
  69. public void ConvertTo_Positive ()
  70. {
  71. Assert.AreEqual (1.5D.ToString (CultureInfo.InvariantCulture),
  72. converter.ConvertTo (null, CultureInfo.InvariantCulture, 1.5D,
  73. typeof (string)), "#1");
  74. Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
  75. converter.ConvertTo (null, CultureInfo.CurrentCulture, 1.5D,
  76. typeof (string)), "#2");
  77. Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
  78. converter.ConvertTo (1.5D, typeof (string)), "#3");
  79. }
  80. [Test]
  81. public void ConvertToString_Negative ()
  82. {
  83. Assert.AreEqual ((-1.5D).ToString (CultureInfo.InvariantCulture),
  84. converter.ConvertToString (null, CultureInfo.InvariantCulture,
  85. -1.5D), "#1");
  86. Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
  87. converter.ConvertToString (null, -1.5D), "#2");
  88. Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
  89. converter.ConvertToString (null, CultureInfo.CurrentCulture,
  90. -1.5D), "#3");
  91. Assert.AreEqual ((-1.5D).ToString (CultureInfo.CurrentCulture),
  92. converter.ConvertToString (-1.5D), "#4");
  93. }
  94. [Test]
  95. public void ConvertToString_Positive ()
  96. {
  97. Assert.AreEqual (1.5D.ToString (CultureInfo.InvariantCulture),
  98. converter.ConvertToString (null, CultureInfo.InvariantCulture,
  99. 1.5D), "#1");
  100. Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
  101. converter.ConvertToString (null, 1.5D), "#2");
  102. Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
  103. converter.ConvertToString (null, CultureInfo.CurrentCulture,
  104. 1.5D), "#3");
  105. Assert.AreEqual (1.5D.ToString (CultureInfo.CurrentCulture),
  106. converter.ConvertToString (1.5D), "#4");
  107. }
  108. [Test]
  109. public void ConvertToString ()
  110. {
  111. CultureInfo culture = new MyCultureInfo ();
  112. NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
  113. Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (double) -5), "#1");
  114. Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
  115. }
  116. [Test]
  117. public void ConvertFromString ()
  118. {
  119. CultureInfo culture = new MyCultureInfo ();
  120. NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
  121. Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
  122. }
  123. [Test]
  124. public void ConvertFrom_InvalidValue ()
  125. {
  126. try {
  127. converter.ConvertFrom ("*1");
  128. Assert.Fail ("#1");
  129. } catch (AssertionException) {
  130. throw;
  131. } catch (Exception ex) {
  132. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  133. Assert.IsNotNull (ex.InnerException, "#3");
  134. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  135. }
  136. }
  137. [Test]
  138. public void ConvertFrom_InvalidValue_Invariant ()
  139. {
  140. try {
  141. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
  142. Assert.Fail ("#1");
  143. } catch (AssertionException) {
  144. throw;
  145. } catch (Exception ex) {
  146. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  147. Assert.IsNotNull (ex.InnerException, "#3");
  148. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  149. }
  150. }
  151. [Test]
  152. public void ConvertFrom_Base10_MinOverflow ()
  153. {
  154. string minOverflow = double.MinValue.ToString (
  155. CultureInfo.CurrentCulture);
  156. try {
  157. converter.ConvertFrom (minOverflow);
  158. Assert.Fail ("#1");
  159. } catch (AssertionException) {
  160. throw;
  161. } catch (Exception ex) {
  162. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  163. Assert.IsNotNull (ex.InnerException, "#3");
  164. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  165. }
  166. }
  167. [Test]
  168. public void ConvertFrom_Base10_MinOverflow_Invariant ()
  169. {
  170. string minOverflow = double.MinValue.ToString (
  171. CultureInfo.InvariantCulture);
  172. try {
  173. converter.ConvertFrom (null, CultureInfo.InvariantCulture,
  174. minOverflow);
  175. Assert.Fail ("#1");
  176. } catch (AssertionException) {
  177. throw;
  178. } catch (Exception ex) {
  179. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  180. Assert.IsNotNull (ex.InnerException, "#3");
  181. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  182. }
  183. }
  184. [Test]
  185. public void ConvertFrom_Base10_MaxOverflow ()
  186. {
  187. string maxOverflow = double.MaxValue.ToString (
  188. CultureInfo.CurrentCulture);
  189. try {
  190. converter.ConvertFrom (maxOverflow);
  191. Assert.Fail ("#1");
  192. } catch (AssertionException) {
  193. throw;
  194. } catch (Exception ex) {
  195. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  196. Assert.IsNotNull (ex.InnerException, "#3");
  197. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  198. }
  199. }
  200. [Test]
  201. public void ConvertFrom_Base10_MaxOverflow_Invariant ()
  202. {
  203. string maxOverflow = double.MaxValue.ToString (
  204. CultureInfo.InvariantCulture);
  205. try {
  206. converter.ConvertFrom (null, CultureInfo.InvariantCulture,
  207. maxOverflow);
  208. Assert.Fail ("#1");
  209. } catch (AssertionException) {
  210. throw;
  211. } catch (Exception ex) {
  212. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  213. Assert.IsNotNull (ex.InnerException, "#3");
  214. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  215. }
  216. }
  217. [Test]
  218. public void ConvertFromString_InvalidValue ()
  219. {
  220. try {
  221. converter.ConvertFromString ("*1");
  222. Assert.Fail ("#1");
  223. } catch (AssertionException) {
  224. throw;
  225. } catch (Exception ex) {
  226. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  227. Assert.IsNotNull (ex.InnerException, "#3");
  228. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  229. }
  230. }
  231. [Test]
  232. public void ConvertFromString_InvalidValue_Invariant ()
  233. {
  234. try {
  235. converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
  236. Assert.Fail ("#1");
  237. } catch (AssertionException) {
  238. throw;
  239. } catch (Exception ex) {
  240. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  241. Assert.IsNotNull (ex.InnerException, "#3");
  242. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  243. }
  244. }
  245. [Test]
  246. public void ConvertFromString_Base10_MinOverflow ()
  247. {
  248. string minOverflow = double.MinValue.ToString (
  249. CultureInfo.CurrentCulture);
  250. try {
  251. converter.ConvertFromString (minOverflow);
  252. Assert.Fail ("#1");
  253. } catch (AssertionException) {
  254. throw;
  255. } catch (Exception ex) {
  256. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  257. Assert.IsNotNull (ex.InnerException, "#3");
  258. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  259. }
  260. }
  261. [Test]
  262. public void ConvertFromString_Base10_MinOverflow_Invariant ()
  263. {
  264. string minOverflow = double.MinValue.ToString (
  265. CultureInfo.InvariantCulture);
  266. try {
  267. converter.ConvertFromString (null, CultureInfo.InvariantCulture,
  268. minOverflow);
  269. Assert.Fail ("#1");
  270. } catch (AssertionException) {
  271. throw;
  272. } catch (Exception ex) {
  273. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  274. Assert.IsNotNull (ex.InnerException, "#3");
  275. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  276. }
  277. }
  278. [Test]
  279. public void ConvertFromString_Base10_MaxOverflow ()
  280. {
  281. string maxOverflow = double.MaxValue.ToString (
  282. CultureInfo.CurrentCulture);
  283. try {
  284. converter.ConvertFromString (maxOverflow);
  285. Assert.Fail ("#1");
  286. } catch (AssertionException) {
  287. throw;
  288. } catch (Exception ex) {
  289. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  290. Assert.IsNotNull (ex.InnerException, "#3");
  291. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  292. }
  293. }
  294. [Test]
  295. public void ConvertFromString_Base10_MaxOverflow_Invariant ()
  296. {
  297. string maxOverflow = double.MaxValue.ToString (
  298. CultureInfo.InvariantCulture);
  299. try {
  300. converter.ConvertFromString (null, CultureInfo.InvariantCulture,
  301. maxOverflow);
  302. Assert.Fail ("#1");
  303. } catch (AssertionException) {
  304. throw;
  305. } catch (Exception ex) {
  306. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  307. Assert.IsNotNull (ex.InnerException, "#3");
  308. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  309. }
  310. }
  311. [Serializable]
  312. private sealed class MyCultureInfo : CultureInfo
  313. {
  314. internal MyCultureInfo ()
  315. : base ("en-US")
  316. {
  317. }
  318. public override object GetFormat (Type formatType)
  319. {
  320. if (formatType == typeof (NumberFormatInfo)) {
  321. NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
  322. nfi.NegativeSign = "myNegativeSign";
  323. return NumberFormatInfo.ReadOnly (nfi);
  324. } else {
  325. return base.GetFormat (formatType);
  326. }
  327. }
  328. #if NET_2_0
  329. // adding this override in 1.x shows different result in .NET (it is ignored).
  330. // Some compatibility kids might want to fix this issue.
  331. public override NumberFormatInfo NumberFormat {
  332. get {
  333. NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
  334. nfi.NegativeSign = "myNegativeSign";
  335. return nfi;
  336. }
  337. set { throw new NotSupportedException (); }
  338. }
  339. #endif
  340. }
  341. }
  342. }