* support-test-*.cs: Rename from test-*-p2.cs.
[mono.git] / mcs / class / System / Test / System.ComponentModel / Int64ConverterTests.cs
1 //
2 // System.ComponentModel.Int64Converter 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 Int64ConverterTests
21         {
22                 private Int64Converter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new Int64Converter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (long)), "#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 (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#8000000000000000"), "#1");
50                         Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x8000000000000000"), "#2");
51                         Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X8000000000000000"), "#3");
52                         Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x8000000000000000"), "#4");
53                         Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X8000000000000000"), "#5");
54                 }
55
56                 [Test]
57                 public void ConvertFrom_MaxValue ()
58                 {
59                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fffffffffffffff"), "#1");
60                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFFFFFFFFFFFFFF"), "#2");
61                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fffffffffffffff"), "#3");
62                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFFFFFFFFFFFFFF"), "#4");
63                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fffffffffffffff"), "#5");
64                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFFFFFFFFFFFFFF"), "#6");
65                 }
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 (long.MinValue.ToString (CultureInfo.InvariantCulture),
86                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, long.MinValue,
87                                 typeof (string)), "#1");
88                         Assert.AreEqual (long.MinValue.ToString (CultureInfo.CurrentCulture),
89                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, long.MinValue,
90                                 typeof (string)), "#2");
91                         Assert.AreEqual (long.MinValue.ToString (CultureInfo.CurrentCulture),
92                                 converter.ConvertTo (long.MinValue, typeof (string)), "#3");
93                 }
94
95                 [Test]
96                 public void ConvertTo_MaxValue ()
97                 {
98                         Assert.AreEqual (long.MaxValue.ToString (CultureInfo.InvariantCulture),
99                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, long.MaxValue,
100                                 typeof (string)), "#1");
101                         Assert.AreEqual (long.MaxValue.ToString (CultureInfo.CurrentCulture),
102                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, long.MaxValue,
103                                 typeof (string)), "#2");
104                         Assert.AreEqual (long.MaxValue.ToString (CultureInfo.CurrentCulture),
105                                 converter.ConvertTo (long.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 (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (long) -5), "#1");
115                         Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
116                 }
117
118                 [Test]
119                 public void ConvertFromString ()
120                 {
121                         CultureInfo culture = new MyCultureInfo ();
122                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
123
124                         Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
125                 }
126
127                 [Test]
128                 public void ConvertFromString_Invalid1 ()
129                 {
130                         try {
131                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
132                                 Assert.Fail ("#1");
133                         } catch (AssertionException) {
134                                 throw;
135                         } catch (Exception ex) {
136                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
137                                 Assert.IsNotNull (ex.InnerException, "#3");
138                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
139                         }
140                 }
141
142                 [Test]
143                 public void ConvertFromString_Invalid2 ()
144                 {
145                         try {
146                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
147                                         double.MaxValue.ToString(CultureInfo.InvariantCulture));
148                                 Assert.Fail ("#1");
149                         } catch (AssertionException) {
150                                 throw;
151                         } catch (Exception ex) {
152                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
153                                 Assert.IsNotNull (ex.InnerException, "#3");
154                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
155                         }
156                 }
157
158                 [Test]
159                 public void ConvertFromString_Invalid3 ()
160                 {
161                         try {
162                                 converter.ConvertFromString ("*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
173                 [Test]
174                 public void ConvertFromString_Invalid4 ()
175                 {
176                         try {
177                                 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
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
188                 [Test]
189                 public void ConvertFrom_InvalidString1 ()
190                 {
191                         try {
192                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
193                                 Assert.Fail ("#1");
194                         } catch (AssertionException) {
195                                 throw;
196                         } catch (Exception ex) {
197                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
198                                 Assert.IsNotNull (ex.InnerException, "#3");
199                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
200                         }
201                 }
202
203                 [Test]
204                 public void ConvertFrom_InvalidString2 ()
205                 {
206                         try {
207                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
208                                         double.MaxValue.ToString (CultureInfo.InvariantCulture));
209                                 Assert.Fail ("#1");
210                         } catch (AssertionException) {
211                                 throw;
212                         } catch (Exception ex) {
213                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
214                                 Assert.IsNotNull (ex.InnerException, "#3");
215                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
216                         }
217                 }
218
219                 [Test]
220                 public void ConvertFrom_InvalidString3 ()
221                 {
222                         try {
223                                 converter.ConvertFrom ("*1");
224                                 Assert.Fail ("#1");
225                         } catch (AssertionException) {
226                                 throw;
227                         } catch (Exception ex) {
228                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
229                                 Assert.IsNotNull (ex.InnerException, "#3");
230                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
231                         }
232                 }
233
234                 [Test]
235                 public void ConvertFrom_InvalidString4 ()
236                 {
237                         try {
238                                 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
239                                 Assert.Fail ("#1");
240                         } catch (AssertionException) {
241                                 throw;
242                         } catch (Exception ex) {
243                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
244                                 Assert.IsNotNull (ex.InnerException, "#3");
245                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
246                         }
247                 }
248
249                 [Serializable]
250                 private sealed class MyCultureInfo : CultureInfo
251                 {
252                         internal MyCultureInfo ()
253                                 : base ("en-US")
254                         {
255                         }
256
257                         public override object GetFormat (Type formatType)
258                         {
259                                 if (formatType == typeof (NumberFormatInfo)) {
260                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
261
262                                         nfi.NegativeSign = "myNegativeSign";
263                                         return NumberFormatInfo.ReadOnly (nfi);
264                                 } else {
265                                         return base.GetFormat (formatType);
266                                 }
267                         }
268                 }
269         }
270 }