* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / corlib / Test / System / BitConverterTest.cs
1 //
2 // BitConverterTest.cs - NUnit Test Cases for System.BitConverter
3 //
4 // author:
5 //   Duco Fijma (duco@lorentz.xs4all.nl)
6 //
7 // (C) 2002 Duco Fijma
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 // 
10
11 using NUnit.Framework;
12 using System;
13
14 namespace MonoTests.System
15 {
16
17 [TestFixture]
18 public class BitConverterTest : Assertion {
19         
20         public void TestIsLittleEndian ()
21         {
22                 byte[] b;
23
24                 b = BitConverter.GetBytes (1);
25                 AssertEquals ("A1", b[0] == 1, BitConverter.IsLittleEndian );
26         }
27
28         private void PrivateTestSingle (float v1)
29         {
30                 float v2;
31                 byte[] b;
32                 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; 
33                 bool exception;
34                 
35                 b = BitConverter.GetBytes (v1);
36                 AssertEquals("A1", 4, b.Length);
37
38                 v2 = BitConverter.ToSingle (b, 0);
39                 AssertEquals("A2", v1, v2);     
40
41                 b.CopyTo (larger, 1);
42                 v2 = BitConverter.ToSingle (larger, 1);
43                 AssertEquals("A3", v1, v2);
44
45                 try {
46                         v2 = BitConverter.ToSingle (larger, 8);
47                         exception = false;
48                 }
49                 catch (ArgumentException) {
50                         exception = true;
51                 } 
52                 Assert ("A4", exception);
53
54                 try {
55                         v2 = BitConverter.ToSingle ((byte[]) null, 77);
56                         exception = false;
57                 }
58                 catch (ArgumentNullException) {
59                         exception = true;
60                 }
61                 Assert ("A5", exception);
62         }
63
64         public void TestSingle()
65         {
66                 PrivateTestSingle (0.1f);
67                 PrivateTestSingle (24.1e30f);
68         }
69
70         private void PrivateTestDouble (double v1)
71         {
72                 double v2;
73                 byte[] b;
74                 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; 
75                 bool exception;
76                 
77                 b = BitConverter.GetBytes (v1);
78                 AssertEquals("A1", 8, b.Length);
79
80                 v2 = BitConverter.ToDouble (b, 0);
81                 AssertEquals("A2", v1, v2);     
82
83                 b.CopyTo (larger, 1);
84                 v2 = BitConverter.ToDouble (larger, 1);
85                 AssertEquals("A3", v1, v2);
86
87                 try {
88                         v2 = BitConverter.ToDouble (larger, 3);
89                         exception = false;
90                 }
91                 catch (ArgumentException) {
92                         exception = true;
93                 } 
94                 Assert ("A4", exception);
95
96                 try {
97                         v2 = BitConverter.ToDouble ((byte[]) null, 77);
98                         exception = false;
99                 }
100                 catch (ArgumentNullException) {
101                         exception = true;
102                 }
103                 Assert ("A5", exception);
104         }
105
106         public void TestDouble ()
107         {
108                 double d = 123.321;
109
110                 AssertEquals("A1", d, BitConverter.Int64BitsToDouble (BitConverter.DoubleToInt64Bits (d)));
111
112                 PrivateTestDouble (0.1);
113                 PrivateTestDouble (24.1e77);
114         }
115
116         private void PrivateTestBool (bool v1)
117         {
118                 bool v2;
119                 byte[] b;
120                 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 }; 
121                 bool exception;
122                 
123                 b = BitConverter.GetBytes (v1);
124                 AssertEquals("A1", 1, b.Length);
125
126                 v2 = BitConverter.ToBoolean (b, 0);
127                 AssertEquals("A2", v1, v2);     
128
129                 b.CopyTo (larger, 1);
130                 v2 = BitConverter.ToBoolean (larger, 1);
131                 AssertEquals("A3", v1, v2);
132
133                 try {
134                         v2 = BitConverter.ToBoolean (larger, 4);
135                         exception = false;
136                 }
137                 catch (ArgumentException) {
138                         exception = true;
139                 } 
140                 Assert ("A4", exception);
141
142                 try {
143                         v2 = BitConverter.ToBoolean ((byte[]) null, 77);
144                         exception = false;
145                 }
146                 catch (ArgumentNullException) {
147                         exception = true;
148                 }
149                 Assert ("A5", exception);
150         }
151
152         public void TestBool () {
153                 PrivateTestBool(true);
154                 PrivateTestBool(false);
155         }
156
157         private void PrivateTestChar (char v1)
158         {
159                 char v2;
160                 byte[] b;
161                 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 }; 
162                 bool exception;
163                 
164                 b = BitConverter.GetBytes (v1);
165                 AssertEquals("A1", 2, b.Length);
166
167                 v2 = BitConverter.ToChar (b, 0);
168                 AssertEquals("A2", v1, v2);     
169
170                 b.CopyTo (larger, 1);
171                 v2 = BitConverter.ToChar (larger, 1);
172                 AssertEquals("A3", v1, v2);
173
174                 try {
175                         v2 = BitConverter.ToChar (larger, 3);
176                         exception = false;
177                 }
178                 // LAMESPEC:
179                 // the docs say it should be ArgumentOutOfRangeException, but
180                 // the mscorlib throws an ArgumentException.
181                 catch (ArgumentException) {
182                         exception = true;
183                 } 
184                 Assert ("A4", exception);
185
186                 try {
187                         v2 = BitConverter.ToChar ((byte[]) null, 77);
188                         exception = false;
189                 }
190                 catch (ArgumentNullException) {
191                         exception = true;
192                 }
193                 Assert ("A5", exception);
194         }
195
196         public void TestChar ()
197         {
198                 PrivateTestChar('A');
199                 PrivateTestChar('\x01ff');
200         }
201
202         private void PrivateTestInt16 (short v1)
203         {
204                 short v2;
205                 byte[] b;
206                 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 }; 
207                 bool exception;
208                 
209                 b = BitConverter.GetBytes (v1);
210                 AssertEquals("A1", 2, b.Length);
211
212                 v2 = BitConverter.ToInt16 (b, 0);
213                 AssertEquals("A2", v1, v2);     
214
215                 b.CopyTo (larger, 1);
216                 v2 = BitConverter.ToInt16 (larger, 1);
217                 AssertEquals("A3", v1, v2);
218
219                 try {
220                         v2 = BitConverter.ToInt16 (larger, 3);
221                         exception = false;
222                 }
223                 catch (ArgumentException) {
224                         exception = true;
225                 } 
226                 Assert ("A4", exception);
227
228                 try {
229                         v2 = BitConverter.ToInt16 ((byte[]) null, 77);
230                         exception = false;
231                 }
232                 catch (ArgumentNullException) {
233                         exception = true;
234                 }
235                 Assert ("A5", exception);
236         }
237
238         public void TestInt16 ()
239         {
240                 PrivateTestInt16 (0);
241                 PrivateTestInt16 (1000);
242                 PrivateTestInt16 (-32768);
243                 PrivateTestInt16 (32767);
244         }
245
246         private void PrivateTestUInt16 (ushort v1)
247         {
248                 ushort v2;
249                 byte[] b;
250                 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 }; 
251                 bool exception;
252                 
253                 b = BitConverter.GetBytes (v1);
254                 AssertEquals("A1", 2, b.Length);
255
256                 v2 = BitConverter.ToUInt16 (b, 0);
257                 AssertEquals("A2", v1, v2);     
258
259                 b.CopyTo (larger, 1);
260                 v2 = BitConverter.ToUInt16 (larger, 1);
261                 AssertEquals("A3", v1, v2);
262
263                 try {
264                         v2 = BitConverter.ToUInt16 (larger, 3);
265                         exception = false;
266                 }
267                 catch (ArgumentException) {
268                         exception = true;
269                 } 
270                 Assert ("A4", exception);
271
272                 try {
273                         v2 = BitConverter.ToUInt16 ((byte[]) null, 77);
274                         exception = false;
275                 }
276                 catch (ArgumentNullException) {
277                         exception = true;
278                 }
279                 Assert ("A5", exception);
280         }
281
282         public void TestUInt16 ()
283         {
284                 PrivateTestUInt16 (0);
285                 PrivateTestUInt16 (1000);
286                 PrivateTestUInt16 (65535);
287         }
288
289         private void PrivateTestInt32 (int v1)
290         {
291                 int v2;
292                 byte[] b;
293                 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; 
294                 bool exception;
295                 
296                 b = BitConverter.GetBytes (v1);
297                 AssertEquals("A1", 4, b.Length);
298
299                 v2 = BitConverter.ToInt32 (b, 0);
300                 AssertEquals("A2", v1, v2);     
301
302                 b.CopyTo (larger, 1);
303                 v2 = BitConverter.ToInt32 (larger, 1);
304                 AssertEquals("A3", v1, v2);
305
306                 try {
307                         v2 = BitConverter.ToInt32 (larger, 8);
308                         exception = false;
309                 }
310                 catch (ArgumentException) {
311                         exception = true;
312                 } 
313                 Assert ("A4", exception);
314
315                 try {
316                         v2 = BitConverter.ToInt32 ((byte[]) null, 77);
317                         exception = false;
318                 }
319                 catch (ArgumentNullException) {
320                         exception = true;
321                 }
322                 Assert ("A5", exception);
323         }
324
325         public void TestInt32 ()
326         {
327                 PrivateTestInt32 (0);
328                 PrivateTestInt32 (1000);
329                 PrivateTestInt32 (-2147483648);
330                 PrivateTestInt32 (2147483647);
331         }
332
333         private void PrivateTestUInt32 (uint v1)
334         {
335                 uint v2;
336                 byte[] b;
337                 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; 
338                 bool exception;
339                 
340                 b = BitConverter.GetBytes (v1);
341                 AssertEquals ("A1", 4, b.Length);
342
343                 v2 = BitConverter.ToUInt32 (b, 0);
344                 AssertEquals ("A2", v1, v2);    
345
346                 b.CopyTo (larger, 1);
347                 v2 = BitConverter.ToUInt32 (larger, 1);
348                 AssertEquals ("A3", v1, v2);
349
350                 try {
351                         v2 = BitConverter.ToUInt32 (larger, 8);
352                         exception = false;
353                 }
354                 catch (ArgumentException) {
355                         exception = true;
356                 } 
357                 Assert ("A4", exception);
358
359                 try {
360                         v2 = BitConverter.ToUInt32 ((byte[]) null, 77);
361                         exception = false;
362                 }
363                 catch (ArgumentNullException) {
364                         exception = true;
365                 }
366                 Assert ("A5", exception);
367         }
368
369         public void TestUInt32 ()
370         {
371                 PrivateTestUInt32 (0u);
372                 PrivateTestUInt32 (1000u);
373                 PrivateTestUInt32 (4294967295u);
374         }
375
376         private void PrivateTestInt64 (long v1)
377         {
378                 long v2;
379                 byte[] b;
380                 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; 
381                 bool exception;
382                 
383                 b = BitConverter.GetBytes (v1);
384                 AssertEquals ("A1", 8, b.Length);
385
386                 v2 = BitConverter.ToInt64 (b, 0);
387                 AssertEquals ("A2", v1, v2);    
388
389                 b.CopyTo (larger, 1);
390                 v2 = BitConverter.ToInt64 (larger, 1);
391                 AssertEquals ("A3", v1, v2);
392
393                 try {
394                         v2 = BitConverter.ToInt64 (larger, 8);
395                         exception = false;
396                 }
397                 catch (ArgumentException) {
398                         exception = true;
399                 } 
400                 Assert ("A4", exception);
401
402                 try {
403                         v2 = BitConverter.ToInt64 ((byte[]) null, 77);
404                         exception = false;
405                 }
406                 catch (ArgumentNullException) {
407                         exception = true;
408                 }
409                 Assert ("A5", exception);
410         }
411
412         public void TestInt64 ()
413         {
414                 PrivateTestInt64 (0);
415                 PrivateTestInt64 (1000);
416                 PrivateTestInt64 (-9223372036854775808);
417                 PrivateTestInt64 (9223372036854775807);
418         }
419
420         private void PrivateTestUInt64 (ulong v1)
421         {
422                 ulong v2;
423                 byte[] b;
424                 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; 
425                 bool exception;
426                 
427                 b = BitConverter.GetBytes (v1);
428                 AssertEquals("A1", 8, b.Length);
429
430                 v2 = BitConverter.ToUInt64 (b, 0);
431                 AssertEquals("A2", v1, v2);     
432
433                 b.CopyTo (larger, 1);
434                 v2 = BitConverter.ToUInt64 (larger, 1);
435                 AssertEquals("A3", v1, v2);
436
437                 try {
438                         v2 = BitConverter.ToUInt64 (larger, 8);
439                         exception = false;
440                 }
441                 catch (ArgumentException) {
442                         exception = true;
443                 } 
444                 Assert ("A4", exception);
445
446                 try {
447                         v2 = BitConverter.ToUInt64 ((byte[]) null, 77);
448                         exception = false;
449                 }
450                 catch (ArgumentNullException) {
451                         exception = true;
452                 }
453                 Assert ("A5", exception);
454         }
455
456         public void TestUInt64 ()
457         {
458                 PrivateTestUInt64 (0);
459                 PrivateTestUInt64 (1000);
460                 PrivateTestUInt64 (18446744073709551615);
461         }
462
463         public void TestToString ()
464         {
465                 string s;
466                 bool exception;
467
468                 byte[] b = new byte[] {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
469
470                 AssertEquals ("A1", "00-11-22-33-44-55-66-77-88-99-AA-BB-CC-DD-EE-FF", BitConverter.ToString (b));
471                 AssertEquals ("A2", "66-77-88-99-AA-BB-CC-DD-EE-FF", BitConverter.ToString (b, 6));
472                 AssertEquals ("A3", "66-77-88", BitConverter.ToString (b, 6, 3));
473
474                 try {
475                         s = BitConverter.ToString ((byte[]) null);
476                         exception = false;
477                 }
478                 catch (ArgumentNullException) {
479                         exception = true;
480                 }
481                 Assert ("A4", exception);
482
483                 try {
484                         s = BitConverter.ToString (b, 20);
485                         exception = false;
486                 }
487                 catch (ArgumentException) {
488                         exception = true;
489                 } 
490                 Assert ("A5", exception);
491
492                 try {
493                         s = BitConverter.ToString ((byte[]) null, 20);
494                         exception = false;
495                 }
496                 catch (ArgumentNullException) {
497                         exception = true;
498                 }
499                 Assert ("A6", exception);
500
501                 try {
502                         s = BitConverter.ToString (b, 20, 3);
503                         exception = false;
504                 }
505                 catch (ArgumentOutOfRangeException) {
506                         exception = true;
507                 }
508                 Assert ("A7", exception);
509
510                 try {
511                         s = BitConverter.ToString ((byte[]) null, 20, 3);
512                         exception = false;
513                 }
514                 catch (ArgumentNullException) {
515                         exception = true;
516                 }
517                 Assert ("A8", exception);
518
519                 try {
520                         s = BitConverter.ToString (b, 16, 0);
521                         exception = false;
522                 }
523                 catch (ArgumentOutOfRangeException) {
524                         exception = true;
525                 }
526                 Assert ("A9", exception);
527
528
529         }
530
531         [Test]
532         [ExpectedException (typeof (ArgumentOutOfRangeException))]
533         public void ToString_StartIndexOverflow () 
534         {
535                 byte[] array = new byte [4];
536                 BitConverter.ToString (array, Int32.MaxValue, 1);
537         }
538
539         [Test]
540         [ExpectedException (typeof (ArgumentException))]
541         public void ToString_LengthOverflow () 
542         {
543                 byte[] array = new byte [4];
544                 BitConverter.ToString (array, 1, Int32.MaxValue);
545         }
546
547         [Test]
548         [ExpectedException (typeof (ArgumentOutOfRangeException))]
549         public void ToUpperLimit () 
550         {
551                 byte[] array = new byte [4];
552                 BitConverter.ToInt32 (array, Int32.MaxValue);
553         }
554
555         [Test]
556         [ExpectedException (typeof (ArgumentOutOfRangeException))]
557         public void ToLowerLimit () 
558         {
559                 byte[] array = new byte [4];
560                 BitConverter.ToInt32 (array, Int32.MinValue);
561         }
562
563         [Test]
564 #if !NET_2_0
565         [ExpectedException (typeof (ArgumentOutOfRangeException))]
566 #endif
567         public void ToString_Empty ()
568         {
569                 byte[] empty = new byte [0];
570                 AssertEquals ("Empty", String.Empty, BitConverter.ToString (empty));
571         }
572
573         [Test]
574         public void ToBoolean () 
575         {
576                 byte[] array = new byte [2] { 0x02, 0x00 };
577                 Assert ("True", BitConverter.ToBoolean (array, 0));
578                 AssertEquals ("True==True", true, BitConverter.ToBoolean (array, 0));
579                 Assert ("False", !BitConverter.ToBoolean (array, 1));
580         }
581 }
582 }