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