2 // BitConverterTest.cs - NUnit Test Cases for System.BitConverter
5 // Duco Fijma (duco@lorentz.xs4all.nl)
8 // Copyright (C) 2004 Novell (http://www.novell.com)
11 using NUnit.Framework;
14 namespace MonoTests.System
18 public class BitConverterTest : Assertion {
20 public void TestIsLittleEndian ()
24 b = BitConverter.GetBytes (1);
25 AssertEquals ("A1", b[0] == 1, BitConverter.IsLittleEndian );
28 private void PrivateTestSingle (float v1)
32 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
35 b = BitConverter.GetBytes (v1);
36 AssertEquals("A1", 4, b.Length);
38 v2 = BitConverter.ToSingle (b, 0);
39 AssertEquals("A2", v1, v2);
42 v2 = BitConverter.ToSingle (larger, 1);
43 AssertEquals("A3", v1, v2);
46 v2 = BitConverter.ToSingle (larger, 8);
49 catch (ArgumentException) {
52 Assert ("A4", exception);
55 v2 = BitConverter.ToSingle ((byte[]) null, 77);
58 catch (ArgumentNullException) {
61 Assert ("A5", exception);
64 public void TestSingle()
66 PrivateTestSingle (0.1f);
67 PrivateTestSingle (24.1e30f);
70 private void PrivateTestDouble (double v1)
74 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
77 b = BitConverter.GetBytes (v1);
78 AssertEquals("A1", 8, b.Length);
80 v2 = BitConverter.ToDouble (b, 0);
81 AssertEquals("A2", v1, v2);
84 v2 = BitConverter.ToDouble (larger, 1);
85 AssertEquals("A3", v1, v2);
88 v2 = BitConverter.ToDouble (larger, 3);
91 catch (ArgumentException) {
94 Assert ("A4", exception);
97 v2 = BitConverter.ToDouble ((byte[]) null, 77);
100 catch (ArgumentNullException) {
103 Assert ("A5", exception);
106 public void TestDouble ()
110 AssertEquals("A1", d, BitConverter.Int64BitsToDouble (BitConverter.DoubleToInt64Bits (d)));
112 PrivateTestDouble (0.1);
113 PrivateTestDouble (24.1e77);
116 private void PrivateTestBool (bool v1)
120 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 };
123 b = BitConverter.GetBytes (v1);
124 AssertEquals("A1", 1, b.Length);
126 v2 = BitConverter.ToBoolean (b, 0);
127 AssertEquals("A2", v1, v2);
129 b.CopyTo (larger, 1);
130 v2 = BitConverter.ToBoolean (larger, 1);
131 AssertEquals("A3", v1, v2);
134 v2 = BitConverter.ToBoolean (larger, 4);
137 catch (ArgumentException) {
140 Assert ("A4", exception);
143 v2 = BitConverter.ToBoolean ((byte[]) null, 77);
146 catch (ArgumentNullException) {
149 Assert ("A5", exception);
152 public void TestBool () {
153 PrivateTestBool(true);
154 PrivateTestBool(false);
157 private void PrivateTestChar (char v1)
161 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 };
164 b = BitConverter.GetBytes (v1);
165 AssertEquals("A1", 2, b.Length);
167 v2 = BitConverter.ToChar (b, 0);
168 AssertEquals("A2", v1, v2);
170 b.CopyTo (larger, 1);
171 v2 = BitConverter.ToChar (larger, 1);
172 AssertEquals("A3", v1, v2);
175 v2 = BitConverter.ToChar (larger, 3);
179 // the docs say it should be ArgumentOutOfRangeException, but
180 // the mscorlib throws an ArgumentException.
181 catch (ArgumentException) {
184 Assert ("A4", exception);
187 v2 = BitConverter.ToChar ((byte[]) null, 77);
190 catch (ArgumentNullException) {
193 Assert ("A5", exception);
196 public void TestChar ()
198 PrivateTestChar('A');
199 PrivateTestChar('\x01ff');
202 private void PrivateTestInt16 (short v1)
206 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 };
209 b = BitConverter.GetBytes (v1);
210 AssertEquals("A1", 2, b.Length);
212 v2 = BitConverter.ToInt16 (b, 0);
213 AssertEquals("A2", v1, v2);
215 b.CopyTo (larger, 1);
216 v2 = BitConverter.ToInt16 (larger, 1);
217 AssertEquals("A3", v1, v2);
220 v2 = BitConverter.ToInt16 (larger, 3);
223 catch (ArgumentException) {
226 Assert ("A4", exception);
229 v2 = BitConverter.ToInt16 ((byte[]) null, 77);
232 catch (ArgumentNullException) {
235 Assert ("A5", exception);
238 public void TestInt16 ()
240 PrivateTestInt16 (0);
241 PrivateTestInt16 (1000);
242 PrivateTestInt16 (-32768);
243 PrivateTestInt16 (32767);
246 private void PrivateTestUInt16 (ushort v1)
250 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 };
253 b = BitConverter.GetBytes (v1);
254 AssertEquals("A1", 2, b.Length);
256 v2 = BitConverter.ToUInt16 (b, 0);
257 AssertEquals("A2", v1, v2);
259 b.CopyTo (larger, 1);
260 v2 = BitConverter.ToUInt16 (larger, 1);
261 AssertEquals("A3", v1, v2);
264 v2 = BitConverter.ToUInt16 (larger, 3);
267 catch (ArgumentException) {
270 Assert ("A4", exception);
273 v2 = BitConverter.ToUInt16 ((byte[]) null, 77);
276 catch (ArgumentNullException) {
279 Assert ("A5", exception);
282 public void TestUInt16 ()
284 PrivateTestUInt16 (0);
285 PrivateTestUInt16 (1000);
286 PrivateTestUInt16 (65535);
289 private void PrivateTestInt32 (int v1)
293 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
296 b = BitConverter.GetBytes (v1);
297 AssertEquals("A1", 4, b.Length);
299 v2 = BitConverter.ToInt32 (b, 0);
300 AssertEquals("A2", v1, v2);
302 b.CopyTo (larger, 1);
303 v2 = BitConverter.ToInt32 (larger, 1);
304 AssertEquals("A3", v1, v2);
307 v2 = BitConverter.ToInt32 (larger, 8);
310 catch (ArgumentException) {
313 Assert ("A4", exception);
316 v2 = BitConverter.ToInt32 ((byte[]) null, 77);
319 catch (ArgumentNullException) {
322 Assert ("A5", exception);
325 public void TestInt32 ()
327 PrivateTestInt32 (0);
328 PrivateTestInt32 (1000);
329 PrivateTestInt32 (-2147483648);
330 PrivateTestInt32 (2147483647);
333 private void PrivateTestUInt32 (uint v1)
337 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
340 b = BitConverter.GetBytes (v1);
341 AssertEquals ("A1", 4, b.Length);
343 v2 = BitConverter.ToUInt32 (b, 0);
344 AssertEquals ("A2", v1, v2);
346 b.CopyTo (larger, 1);
347 v2 = BitConverter.ToUInt32 (larger, 1);
348 AssertEquals ("A3", v1, v2);
351 v2 = BitConverter.ToUInt32 (larger, 8);
354 catch (ArgumentException) {
357 Assert ("A4", exception);
360 v2 = BitConverter.ToUInt32 ((byte[]) null, 77);
363 catch (ArgumentNullException) {
366 Assert ("A5", exception);
369 public void TestUInt32 ()
371 PrivateTestUInt32 (0u);
372 PrivateTestUInt32 (1000u);
373 PrivateTestUInt32 (4294967295u);
376 private void PrivateTestInt64 (long v1)
380 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
383 b = BitConverter.GetBytes (v1);
384 AssertEquals ("A1", 8, b.Length);
386 v2 = BitConverter.ToInt64 (b, 0);
387 AssertEquals ("A2", v1, v2);
389 b.CopyTo (larger, 1);
390 v2 = BitConverter.ToInt64 (larger, 1);
391 AssertEquals ("A3", v1, v2);
394 v2 = BitConverter.ToInt64 (larger, 8);
397 catch (ArgumentException) {
400 Assert ("A4", exception);
403 v2 = BitConverter.ToInt64 ((byte[]) null, 77);
406 catch (ArgumentNullException) {
409 Assert ("A5", exception);
412 public void TestInt64 ()
414 PrivateTestInt64 (0);
415 PrivateTestInt64 (1000);
416 PrivateTestInt64 (-9223372036854775808);
417 PrivateTestInt64 (9223372036854775807);
420 private void PrivateTestUInt64 (ulong v1)
424 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
427 b = BitConverter.GetBytes (v1);
428 AssertEquals("A1", 8, b.Length);
430 v2 = BitConverter.ToUInt64 (b, 0);
431 AssertEquals("A2", v1, v2);
433 b.CopyTo (larger, 1);
434 v2 = BitConverter.ToUInt64 (larger, 1);
435 AssertEquals("A3", v1, v2);
438 v2 = BitConverter.ToUInt64 (larger, 8);
441 catch (ArgumentException) {
444 Assert ("A4", exception);
447 v2 = BitConverter.ToUInt64 ((byte[]) null, 77);
450 catch (ArgumentNullException) {
453 Assert ("A5", exception);
456 public void TestUInt64 ()
458 PrivateTestUInt64 (0);
459 PrivateTestUInt64 (1000);
460 PrivateTestUInt64 (18446744073709551615);
463 public void TestToString ()
468 byte[] b = new byte[] {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
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));
475 s = BitConverter.ToString ((byte[]) null);
478 catch (ArgumentNullException) {
481 Assert ("A4", exception);
484 s = BitConverter.ToString (b, 20);
487 catch (ArgumentException) {
490 Assert ("A5", exception);
493 s = BitConverter.ToString ((byte[]) null, 20);
496 catch (ArgumentNullException) {
499 Assert ("A6", exception);
502 s = BitConverter.ToString (b, 20, 3);
505 catch (ArgumentOutOfRangeException) {
508 Assert ("A7", exception);
511 s = BitConverter.ToString ((byte[]) null, 20, 3);
514 catch (ArgumentNullException) {
517 Assert ("A8", exception);
520 s = BitConverter.ToString (b, 16, 0);
523 catch (ArgumentOutOfRangeException) {
526 Assert ("A9", exception);
532 [ExpectedException (typeof (ArgumentOutOfRangeException))]
533 public void ToString_StartIndexOverflow ()
535 byte[] array = new byte [4];
536 BitConverter.ToString (array, Int32.MaxValue, 1);
540 [ExpectedException (typeof (ArgumentException))]
541 public void ToString_LengthOverflow ()
543 byte[] array = new byte [4];
544 BitConverter.ToString (array, 1, Int32.MaxValue);
548 [ExpectedException (typeof (ArgumentOutOfRangeException))]
549 public void ToUpperLimit ()
551 byte[] array = new byte [4];
552 BitConverter.ToInt32 (array, Int32.MaxValue);
556 [ExpectedException (typeof (ArgumentOutOfRangeException))]
557 public void ToLowerLimit ()
559 byte[] array = new byte [4];
560 BitConverter.ToInt32 (array, Int32.MinValue);
565 [ExpectedException (typeof (ArgumentOutOfRangeException))]
567 public void ToString_Empty ()
569 byte[] empty = new byte [0];
570 AssertEquals ("Empty", String.Empty, BitConverter.ToString (empty));
574 public void ToBoolean ()
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));