Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System / Test / System.ComponentModel / UInt64ConverterTests.cs
1 //
2 // System.ComponentModel.UInt64Converter 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 UInt64ConverterTests
21         {
22                 private UInt64Converter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new UInt64Converter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (ulong)), "#2");
35                         Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#3");
36                         Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#4");
37                 }
38
39                 [Test]
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]
48                 public void ConvertFrom_MinValue ()
49                 {
50                         Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
51                         Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
52                         Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
53                         Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
54                         Assert.AreEqual (ulong.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
55                 }
56
57                 [Test]
58                 public void ConvertFrom_MaxValue ()
59                 {
60                         Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffffffffffffffff"), "#1");
61                         Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFFFFFFFFFFFFFF"), "#2");
62                         Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffffffffffffffff"), "#3");
63                         Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFFFFFFFFFFFFFF"), "#4");
64                         Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffffffffffffffff"), "#5");
65                         Assert.AreEqual (ulong.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFFFFFFFFFFFFFF"), "#6");
66                 }
67
68                 [Test]
69                 [ExpectedException (typeof (NotSupportedException))]
70                 public void ConvertFrom_Object ()
71                 {
72                         converter.ConvertFrom (new object ());
73                 }
74
75                 [Test]
76                 [ExpectedException (typeof (NotSupportedException))]
77                 public void ConvertFrom_Int32 ()
78                 {
79                         converter.ConvertFrom (10);
80                 }
81
82                 [Test]
83                 public void ConvertTo_MinValue ()
84                 {
85                         Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.InvariantCulture),
86                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, ulong.MinValue,
87                                 typeof (string)), "#1");
88                         Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.CurrentCulture),
89                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, ulong.MinValue,
90                                 typeof (string)), "#2");
91                         Assert.AreEqual (ulong.MinValue.ToString (CultureInfo.CurrentCulture),
92                                 converter.ConvertTo (ulong.MinValue, typeof (string)), "#3");
93                 }
94
95                 [Test]
96                 public void ConvertTo_MaxValue ()
97                 {
98                         Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.InvariantCulture),
99                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, ulong.MaxValue,
100                                 typeof (string)), "#1");
101                         Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.CurrentCulture),
102                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, ulong.MaxValue,
103                                 typeof (string)), "#2");
104                         Assert.AreEqual (ulong.MaxValue.ToString (CultureInfo.CurrentCulture),
105                                 converter.ConvertTo (ulong.MaxValue, typeof (string)), "#3");
106                 }
107
108                 [Test]
109                 public void ConvertToString ()
110                 {
111                         CultureInfo culture = new MyCultureInfo ();
112                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
113
114                         Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
115                 }
116
117                 [Test]
118                 public void ConvertFromString_Invalid1 ()
119                 {
120                         try {
121                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
122                                 Assert.Fail ("#1");
123                         } catch (AssertionException) {
124                                 throw;
125                         } catch (Exception ex) {
126                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
127                                 Assert.IsNotNull (ex.InnerException, "#3");
128                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
129                         }
130                 }
131
132                 [Test]
133                 public void ConvertFromString_Invalid2 ()
134                 {
135                         try {
136                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
137                                         double.MaxValue.ToString(CultureInfo.InvariantCulture));
138                                 Assert.Fail ("#1");
139                         } catch (AssertionException) {
140                                 throw;
141                         } catch (Exception ex) {
142                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
143                                 Assert.IsNotNull (ex.InnerException, "#3");
144                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
145                         }
146                 }
147
148                 [Test]
149                 public void ConvertFromString_Invalid3 ()
150                 {
151                         try {
152                                 converter.ConvertFromString ("*1");
153                                 Assert.Fail ("#1");
154                         } catch (AssertionException) {
155                                 throw;
156                         } catch (Exception ex) {
157                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
158                                 Assert.IsNotNull (ex.InnerException, "#3");
159                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
160                         }
161                 }
162
163                 [Test]
164                 public void ConvertFromString_Invalid4 ()
165                 {
166                         try {
167                                 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
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]
179                 public void ConvertFrom_InvalidString1 ()
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]
194                 public void ConvertFrom_InvalidString2 ()
195                 {
196                         try {
197                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
198                                         double.MaxValue.ToString (CultureInfo.InvariantCulture));
199                                 Assert.Fail ("#1");
200                         } catch (AssertionException) {
201                                 throw;
202                         } catch (Exception ex) {
203                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
204                                 Assert.IsNotNull (ex.InnerException, "#3");
205                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
206                         }
207                 }
208
209                 [Test]
210                 public void ConvertFrom_InvalidString3 ()
211                 {
212                         try {
213                                 converter.ConvertFrom ("*1");
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
224                 [Test]
225                 public void ConvertFrom_InvalidString4 ()
226                 {
227                         try {
228                                 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
229                                 Assert.Fail ("#1");
230                         } catch (AssertionException) {
231                                 throw;
232                         } catch (Exception ex) {
233                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
234                                 Assert.IsNotNull (ex.InnerException, "#3");
235                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
236                         }
237                 }
238
239                 [Serializable]
240                 private sealed class MyCultureInfo : CultureInfo
241                 {
242                         internal MyCultureInfo ()
243                                 : base ("en-US")
244                         {
245                         }
246
247                         public override object GetFormat (Type formatType)
248                         {
249                                 if (formatType == typeof (NumberFormatInfo)) {
250                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
251
252                                         nfi.NegativeSign = "myNegativeSign";
253                                         return NumberFormatInfo.ReadOnly (nfi);
254                                 } else {
255                                         return base.GetFormat (formatType);
256                                 }
257                         }
258
259 #if NET_2_0
260 // adding this override in 1.x shows different result in .NET (it is ignored).
261 // Some compatibility kids might want to fix this issue.
262                         public override NumberFormatInfo NumberFormat {
263                                 get {
264                                         NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
265                                         nfi.NegativeSign = "myNegativeSign";
266                                         return nfi;
267                                 }
268                                 set { throw new NotSupportedException (); }
269                         }
270 #endif
271                 }
272         }
273 }