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