In .:
[mono.git] / mcs / class / System / Test / System.ComponentModel / Int16ConverterTests.cs
1 //
2 // System.ComponentModel.Int16Converter 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 Int16ConverterTests
21         {
22                 private Int16Converter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new Int16Converter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (short)), "#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 (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#8000"), "#1");
50                         Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x8000"), "#2");
51                         Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X8000"), "#3");
52                         Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x8000"), "#4");
53                         Assert.AreEqual (short.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X8000"), "#5");
54                 }
55
56                 [Test]
57                 public void ConvertFrom_MaxValue ()
58                 {
59                         Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fff"), "#1");
60                         Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFF"), "#2");
61                         Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fff"), "#3");
62                         Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFF"), "#4");
63                         Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fff"), "#5");
64                         Assert.AreEqual (short.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFF"), "#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_Int16 ()
77                 {
78                         converter.ConvertFrom ((short) 10);
79                 }
80
81                 [Test]
82                 [ExpectedException (typeof (NotSupportedException))]
83                 public void ConvertFrom_Int32 ()
84                 {
85                         converter.ConvertFrom (10);
86                 }
87
88                 [Test]
89                 public void ConvertTo_MinValue ()
90                 {
91                         Assert.AreEqual (short.MinValue.ToString (CultureInfo.InvariantCulture),
92                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, short.MinValue,
93                                 typeof (string)), "#1");
94                         Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
95                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, short.MinValue,
96                                 typeof (string)), "#2");
97                         Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
98                                 converter.ConvertTo (short.MinValue, typeof (string)), "#3");
99                 }
100
101                 [Test]
102                 public void ConvertTo_MaxValue ()
103                 {
104                         Assert.AreEqual (short.MaxValue.ToString (CultureInfo.InvariantCulture),
105                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, short.MaxValue,
106                                 typeof (string)), "#1");
107                         Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
108                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, short.MaxValue,
109                                 typeof (string)), "#2");
110                         Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
111                                 converter.ConvertTo (short.MaxValue, typeof (string)), "#3");
112                 }
113
114                 [Test]
115                 public void ConvertToString_MinValue ()
116                 {
117                         Assert.AreEqual (short.MinValue.ToString (CultureInfo.InvariantCulture),
118                                 converter.ConvertToString (null, CultureInfo.InvariantCulture,
119                                 short.MinValue), "#1");
120
121                         Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
122                                 converter.ConvertToString (null, short.MinValue), "#2");
123                         Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
124                                 converter.ConvertToString (null, CultureInfo.CurrentCulture,
125                                 short.MinValue), "#3");
126                         Assert.AreEqual (short.MinValue.ToString (CultureInfo.CurrentCulture),
127                                 converter.ConvertToString (short.MinValue), "#4");
128                 }
129
130                 [Test]
131                 public void ConvertToString_MaxValue ()
132                 {
133                         Assert.AreEqual (short.MaxValue.ToString (CultureInfo.InvariantCulture),
134                                 converter.ConvertToString (null, CultureInfo.InvariantCulture,
135                                 short.MaxValue), "#1");
136
137                         Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
138                                 converter.ConvertToString (null, short.MaxValue), "#2");
139                         Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
140                                 converter.ConvertToString (null, CultureInfo.CurrentCulture,
141                                 short.MaxValue), "#3");
142                         Assert.AreEqual (short.MaxValue.ToString (CultureInfo.CurrentCulture),
143                                 converter.ConvertToString (short.MaxValue), "#4");
144                 }
145
146                 [Test]
147                 public void ConvertFrom_InvalidValue ()
148                 {
149                         try {
150                                 converter.ConvertFrom ("*1");
151                                 Assert.Fail ("#1");
152                         } catch (AssertionException) {
153                                 throw;
154                         } catch (Exception ex) {
155                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
156                                 Assert.IsNotNull (ex.InnerException, "#3");
157                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
158                         }
159                 }
160
161                 [Test]
162                 public void ConvertFrom_InvalidValue_Invariant ()
163                 {
164                         try {
165                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
166                                 Assert.Fail ("#1");
167                         } catch (AssertionException) {
168                                 throw;
169                         } catch (Exception ex) {
170                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
171                                 Assert.IsNotNull (ex.InnerException, "#3");
172                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
173                         }
174                 }
175
176                 [Test]
177                 public void ConvertFrom_Base10_MinOverflow ()
178                 {
179                         string minOverflow = ((int) (short.MinValue - 1)).ToString (
180                                 CultureInfo.CurrentCulture);
181
182                         try {
183                                 converter.ConvertFrom (minOverflow);
184                                 Assert.Fail ("#1");
185                         } catch (AssertionException) {
186                                 throw;
187                         } catch (Exception ex) {
188                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
189                                 Assert.IsNotNull (ex.InnerException, "#3");
190                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
191                         }
192                 }
193
194                 [Test]
195                 public void ConvertFrom_Base10_MinOverflow_Invariant ()
196                 {
197                         string minOverflow = ((int) (short.MinValue - 1)).ToString (
198                                 CultureInfo.InvariantCulture);
199
200                         try {
201                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
202                                         minOverflow);
203                                 Assert.Fail ("#1");
204                         } catch (AssertionException) {
205                                 throw;
206                         } catch (Exception ex) {
207                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
208                                 Assert.IsNotNull (ex.InnerException, "#3");
209                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
210                         }
211                 }
212
213                 [Test]
214                 public void ConvertFrom_Base10_MaxOverflow ()
215                 {
216                         string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
217                                 CultureInfo.CurrentCulture);
218
219                         try {
220                                 converter.ConvertFrom (maxOverflow);
221                                 Assert.Fail ("#1");
222                         } catch (AssertionException) {
223                                 throw;
224                         } catch (Exception ex) {
225                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
226                                 Assert.IsNotNull (ex.InnerException, "#3");
227                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
228                         }
229                 }
230
231                 [Test]
232                 public void ConvertFrom_Base10_MaxOverflow_Invariant ()
233                 {
234                         string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
235                                 CultureInfo.InvariantCulture);
236
237                         try {
238                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
239                                         maxOverflow);
240                                 Assert.Fail ("#1");
241                         } catch (AssertionException) {
242                                 throw;
243                         } catch (Exception ex) {
244                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
245                                 Assert.IsNotNull (ex.InnerException, "#3");
246                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
247                         }
248                 }
249
250                 [Test]
251                 public void ConvertFrom_Base16_MinOverflow ()
252                 {
253                         string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
254                                 CultureInfo.CurrentCulture);
255
256                         try {
257                                 converter.ConvertFrom ("#" + minOverflow);
258                                 Assert.Fail ("#1");
259                         } catch (AssertionException) {
260                                 throw;
261                         } catch (Exception ex) {
262                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
263                                 Assert.IsNotNull (ex.InnerException, "#3");
264                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
265                         }
266                 }
267
268                 [Test]
269                 public void ConvertFrom_Base16_MinOverflow_Invariant ()
270                 {
271                         string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
272                                 CultureInfo.InvariantCulture);
273
274                         try {
275                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
276                                         "#" + minOverflow);
277                                 Assert.Fail ("#1");
278                         } catch (AssertionException) {
279                                 throw;
280                         } catch (Exception ex) {
281                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
282                                 Assert.IsNotNull (ex.InnerException, "#3");
283                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#4");
284                         }
285                 }
286
287                 [Test]
288                 public void ConvertFrom_Base16_MaxOverflow ()
289                 {
290                         string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
291                                 CultureInfo.CurrentCulture);
292                         Assert.AreEqual (-32768, converter.ConvertFrom (null, CultureInfo.CurrentCulture,
293                                 "#" + maxOverflow), "#1");
294
295                         maxOverflow = ((int) (ushort.MaxValue)).ToString ("x",
296                                 CultureInfo.CurrentCulture);
297                         Assert.AreEqual (-1, converter.ConvertFrom (null, CultureInfo.CurrentCulture,
298                                 "#" + maxOverflow), "#2");
299
300                         maxOverflow = (ushort.MaxValue + 1).ToString ("x",
301                                 CultureInfo.CurrentCulture);
302
303                         try {
304                                 converter.ConvertFrom (null, CultureInfo.CurrentCulture,
305                                         "#" + maxOverflow);
306                                 Assert.Fail ("#3");
307                         } catch (AssertionException) {
308                                 throw;
309                         } catch (Exception ex) {
310                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#4");
311                                 Assert.IsNotNull (ex.InnerException, "#5");
312                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#6");
313                         }
314                 }
315
316                 [Test]
317                 public void ConvertFrom_Base16_MaxOverflow_Invariant ()
318                 {
319                         string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
320                                 CultureInfo.InvariantCulture);
321                         Assert.AreEqual (-32768, converter.ConvertFrom (null, CultureInfo.InvariantCulture,
322                                 "#" + maxOverflow), "#1");
323
324                         maxOverflow = ((int) (ushort.MaxValue)).ToString ("x",
325                                 CultureInfo.InvariantCulture);
326                         Assert.AreEqual (-1, converter.ConvertFrom (null, CultureInfo.InvariantCulture,
327                                 "#" + maxOverflow), "#2");
328
329                         maxOverflow = (ushort.MaxValue + 1).ToString ("x",
330                                 CultureInfo.InvariantCulture);
331
332                         try {
333                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
334                                         "#" + maxOverflow);
335                                 Assert.Fail ("#3");
336                         } catch (AssertionException) {
337                                 throw;
338                         } catch (Exception ex) {
339                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#4");
340                                 Assert.IsNotNull (ex.InnerException, "#5");
341                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#6");
342                         }
343                 }
344
345                 [Test]
346                 public void ConvertFromString_InvalidValue ()
347                 {
348                         try {
349                                 converter.ConvertFromString ("*1");
350                                 Assert.Fail ("#1");
351                         } catch (AssertionException) {
352                                 throw;
353                         } catch (Exception ex) {
354                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
355                                 Assert.IsNotNull (ex.InnerException, "#3");
356                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
357                         }
358                 }
359
360                 [Test]
361                 public void ConvertFromString_InvalidValue_Invariant ()
362                 {
363                         try {
364                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
365                                 Assert.Fail ("#1");
366                         } catch (AssertionException) {
367                                 throw;
368                         } catch (Exception ex) {
369                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
370                                 Assert.IsNotNull (ex.InnerException, "#3");
371                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
372                         }
373                 }
374
375                 [Test]
376                 public void ConvertFromString_Base10_MinOverflow ()
377                 {
378                         string minOverflow = ((int) (short.MinValue - 1)).ToString (
379                                 CultureInfo.CurrentCulture);
380
381                         try {
382                                 converter.ConvertFromString (minOverflow);
383                                 Assert.Fail ("#1");
384                         } catch (AssertionException) {
385                                 throw;
386                         } catch (Exception ex) {
387                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
388                                 Assert.IsNotNull (ex.InnerException, "#3");
389                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
390                         }
391                 }
392
393                 [Test]
394                 public void ConvertFromString_Base10_MinOverflow_Invariant ()
395                 {
396                         string minOverflow = ((int) (short.MinValue - 1)).ToString (
397                                 CultureInfo.InvariantCulture);
398
399                         try {
400                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
401                                         minOverflow);
402                                 Assert.Fail ("#1");
403                         } catch (AssertionException) {
404                                 throw;
405                         } catch (Exception ex) {
406                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
407                                 Assert.IsNotNull (ex.InnerException, "#3");
408                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
409                         }
410                 }
411
412                 [Test]
413                 public void ConvertFromString_Base10_MaxOverflow ()
414                 {
415                         string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
416                                 CultureInfo.CurrentCulture);
417
418                         try {
419                                 converter.ConvertFromString (maxOverflow);
420                                 Assert.Fail ("#1");
421                         } catch (AssertionException) {
422                                 throw;
423                         } catch (Exception ex) {
424                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
425                                 Assert.IsNotNull (ex.InnerException, "#3");
426                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
427                         }
428                 }
429
430                 [Test]
431                 public void ConvertFromString_Base10_MaxOverflow_Invariant ()
432                 {
433                         string maxOverflow = ((int) (short.MaxValue + 1)).ToString (
434                                 CultureInfo.InvariantCulture);
435
436                         try {
437                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
438                                         maxOverflow);
439                                 Assert.Fail ("#1");
440                         } catch (AssertionException) {
441                                 throw;
442                         } catch (Exception ex) {
443                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
444                                 Assert.IsNotNull (ex.InnerException, "#3");
445                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
446                         }
447                 }
448
449                 [Test]
450                 public void ConvertFromString_Base16_MinOverflow ()
451                 {
452                         string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
453                                 CultureInfo.CurrentCulture);
454
455                         try {
456                                 converter.ConvertFromString ("#" + minOverflow);
457                                 Assert.Fail ("#1");
458                         } catch (AssertionException) {
459                                 throw;
460                         } catch (Exception ex) {
461                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
462                                 Assert.IsNotNull (ex.InnerException, "#3");
463                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
464                         }
465                 }
466
467                 [Test]
468                 public void ConvertFromString_Base16_MinOverflow_Invariant ()
469                 {
470                         string minOverflow = ((int) (short.MinValue - 1)).ToString ("x",
471                                 CultureInfo.InvariantCulture);
472
473                         try {
474                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
475                                         "#" + minOverflow);
476                                 Assert.Fail ("#1");
477                         } catch (AssertionException) {
478                                 throw;
479                         } catch (Exception ex) {
480                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
481                                 Assert.IsNotNull (ex.InnerException, "#3");
482                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
483                         }
484                 }
485
486                 [Test]
487                 public void ConvertFromString_Base16_MaxOverflow ()
488                 {
489                         string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
490                                 CultureInfo.CurrentCulture);
491                         Assert.AreEqual (-32768, converter.ConvertFromString ("#" + maxOverflow), "#1");
492
493                         maxOverflow = (ushort.MaxValue).ToString ("x",
494                                 CultureInfo.CurrentCulture);
495                         Assert.AreEqual (-1, converter.ConvertFromString ("#" + maxOverflow), "#2");
496
497                         maxOverflow = (ushort.MaxValue + 1).ToString ("x",
498                                 CultureInfo.CurrentCulture);
499
500                         try {
501                                 converter.ConvertFromString ("#" + maxOverflow);
502                                 Assert.Fail ("#3");
503                         } catch (AssertionException) {
504                                 throw;
505                         } catch (Exception ex) {
506                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#4");
507                                 Assert.IsNotNull (ex.InnerException, "#5");
508                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#6");
509                         }
510                 }
511
512                 [Test]
513                 public void ConvertFromString_Base16_MaxOverflow_Invariant ()
514                 {
515                         string maxOverflow = ((int) (short.MaxValue + 1)).ToString ("x",
516                                 CultureInfo.CurrentCulture);
517                         Assert.AreEqual (-32768, converter.ConvertFromString ("#" + maxOverflow), "#1");
518
519                         maxOverflow = (ushort.MaxValue + 1).ToString ("x",
520                                 CultureInfo.CurrentCulture);
521
522                         try {
523                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
524                                         "#" + maxOverflow);
525                                 Assert.Fail ("#2");
526                         } catch (AssertionException) {
527                                 throw;
528                         } catch (Exception ex) {
529                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#3");
530                                 Assert.IsNotNull (ex.InnerException, "#4");
531                                 Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#5");
532                         }
533                 }
534         }
535 }
536