**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System / BitConverterTest.cs
blob126abd922753f9e3fe924a8bdb46e8ff4c468684
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 //
11 using NUnit.Framework;
12 using System;
14 namespace MonoTests.System
17 [TestFixture]
18 public class BitConverterTest : Assertion {
20 public void TestIsLittleEndian ()
22 byte[] b;
24 b = BitConverter.GetBytes (1);
25 AssertEquals ("A1", b[0] == 1, BitConverter.IsLittleEndian );
28 private void PrivateTestSingle (float v1)
30 float v2;
31 byte[] b;
32 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
33 bool exception;
35 b = BitConverter.GetBytes (v1);
36 AssertEquals("A1", 4, b.Length);
38 v2 = BitConverter.ToSingle (b, 0);
39 AssertEquals("A2", v1, v2);
41 b.CopyTo (larger, 1);
42 v2 = BitConverter.ToSingle (larger, 1);
43 AssertEquals("A3", v1, v2);
45 try {
46 v2 = BitConverter.ToSingle (larger, 8);
47 exception = false;
49 catch (ArgumentException) {
50 exception = true;
52 Assert ("A4", exception);
54 try {
55 v2 = BitConverter.ToSingle ((byte[]) null, 77);
56 exception = false;
58 catch (ArgumentNullException) {
59 exception = true;
61 Assert ("A5", exception);
64 public void TestSingle()
66 PrivateTestSingle (0.1f);
67 PrivateTestSingle (24.1e30f);
70 private void PrivateTestDouble (double v1)
72 double v2;
73 byte[] b;
74 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
75 bool exception;
77 b = BitConverter.GetBytes (v1);
78 AssertEquals("A1", 8, b.Length);
80 v2 = BitConverter.ToDouble (b, 0);
81 AssertEquals("A2", v1, v2);
83 b.CopyTo (larger, 1);
84 v2 = BitConverter.ToDouble (larger, 1);
85 AssertEquals("A3", v1, v2);
87 try {
88 v2 = BitConverter.ToDouble (larger, 3);
89 exception = false;
91 catch (ArgumentException) {
92 exception = true;
94 Assert ("A4", exception);
96 try {
97 v2 = BitConverter.ToDouble ((byte[]) null, 77);
98 exception = false;
100 catch (ArgumentNullException) {
101 exception = true;
103 Assert ("A5", exception);
106 public void TestDouble ()
108 double d = 123.321;
110 AssertEquals("A1", d, BitConverter.Int64BitsToDouble (BitConverter.DoubleToInt64Bits (d)));
112 PrivateTestDouble (0.1);
113 PrivateTestDouble (24.1e77);
116 private void PrivateTestBool (bool v1)
118 bool v2;
119 byte[] b;
120 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 };
121 bool exception;
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);
133 try {
134 v2 = BitConverter.ToBoolean (larger, 4);
135 exception = false;
137 catch (ArgumentException) {
138 exception = true;
140 Assert ("A4", exception);
142 try {
143 v2 = BitConverter.ToBoolean ((byte[]) null, 77);
144 exception = false;
146 catch (ArgumentNullException) {
147 exception = true;
149 Assert ("A5", exception);
152 public void TestBool () {
153 PrivateTestBool(true);
154 PrivateTestBool(false);
157 private void PrivateTestChar (char v1)
159 char v2;
160 byte[] b;
161 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 };
162 bool exception;
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);
174 try {
175 v2 = BitConverter.ToChar (larger, 3);
176 exception = false;
178 // LAMESPEC:
179 // the docs say it should be ArgumentOutOfRangeException, but
180 // the mscorlib throws an ArgumentException.
181 catch (ArgumentException) {
182 exception = true;
184 Assert ("A4", exception);
186 try {
187 v2 = BitConverter.ToChar ((byte[]) null, 77);
188 exception = false;
190 catch (ArgumentNullException) {
191 exception = true;
193 Assert ("A5", exception);
196 public void TestChar ()
198 PrivateTestChar('A');
199 PrivateTestChar('\x01ff');
202 private void PrivateTestInt16 (short v1)
204 short v2;
205 byte[] b;
206 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 };
207 bool exception;
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);
219 try {
220 v2 = BitConverter.ToInt16 (larger, 3);
221 exception = false;
223 catch (ArgumentException) {
224 exception = true;
226 Assert ("A4", exception);
228 try {
229 v2 = BitConverter.ToInt16 ((byte[]) null, 77);
230 exception = false;
232 catch (ArgumentNullException) {
233 exception = true;
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)
248 ushort v2;
249 byte[] b;
250 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03 };
251 bool exception;
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);
263 try {
264 v2 = BitConverter.ToUInt16 (larger, 3);
265 exception = false;
267 catch (ArgumentException) {
268 exception = true;
270 Assert ("A4", exception);
272 try {
273 v2 = BitConverter.ToUInt16 ((byte[]) null, 77);
274 exception = false;
276 catch (ArgumentNullException) {
277 exception = true;
279 Assert ("A5", exception);
282 public void TestUInt16 ()
284 PrivateTestUInt16 (0);
285 PrivateTestUInt16 (1000);
286 PrivateTestUInt16 (65535);
289 private void PrivateTestInt32 (int v1)
291 int v2;
292 byte[] b;
293 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
294 bool exception;
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);
306 try {
307 v2 = BitConverter.ToInt32 (larger, 8);
308 exception = false;
310 catch (ArgumentException) {
311 exception = true;
313 Assert ("A4", exception);
315 try {
316 v2 = BitConverter.ToInt32 ((byte[]) null, 77);
317 exception = false;
319 catch (ArgumentNullException) {
320 exception = true;
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)
335 uint v2;
336 byte[] b;
337 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
338 bool exception;
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);
350 try {
351 v2 = BitConverter.ToUInt32 (larger, 8);
352 exception = false;
354 catch (ArgumentException) {
355 exception = true;
357 Assert ("A4", exception);
359 try {
360 v2 = BitConverter.ToUInt32 ((byte[]) null, 77);
361 exception = false;
363 catch (ArgumentNullException) {
364 exception = true;
366 Assert ("A5", exception);
369 public void TestUInt32 ()
371 PrivateTestUInt32 (0u);
372 PrivateTestUInt32 (1000u);
373 PrivateTestUInt32 (4294967295u);
376 private void PrivateTestInt64 (long v1)
378 long v2;
379 byte[] b;
380 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
381 bool exception;
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);
393 try {
394 v2 = BitConverter.ToInt64 (larger, 8);
395 exception = false;
397 catch (ArgumentException) {
398 exception = true;
400 Assert ("A4", exception);
402 try {
403 v2 = BitConverter.ToInt64 ((byte[]) null, 77);
404 exception = false;
406 catch (ArgumentNullException) {
407 exception = true;
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)
422 ulong v2;
423 byte[] b;
424 byte[] larger = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
425 bool exception;
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);
437 try {
438 v2 = BitConverter.ToUInt64 (larger, 8);
439 exception = false;
441 catch (ArgumentException) {
442 exception = true;
444 Assert ("A4", exception);
446 try {
447 v2 = BitConverter.ToUInt64 ((byte[]) null, 77);
448 exception = false;
450 catch (ArgumentNullException) {
451 exception = true;
453 Assert ("A5", exception);
456 public void TestUInt64 ()
458 PrivateTestUInt64 (0);
459 PrivateTestUInt64 (1000);
460 PrivateTestUInt64 (18446744073709551615);
463 public void TestToString ()
465 string s;
466 bool exception;
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));
474 try {
475 s = BitConverter.ToString ((byte[]) null);
476 exception = false;
478 catch (ArgumentNullException) {
479 exception = true;
481 Assert ("A4", exception);
483 try {
484 s = BitConverter.ToString (b, 20);
485 exception = false;
487 catch (ArgumentException) {
488 exception = true;
490 Assert ("A5", exception);
492 try {
493 s = BitConverter.ToString ((byte[]) null, 20);
494 exception = false;
496 catch (ArgumentNullException) {
497 exception = true;
499 Assert ("A6", exception);
501 try {
502 s = BitConverter.ToString (b, 20, 3);
503 exception = false;
505 catch (ArgumentOutOfRangeException) {
506 exception = true;
508 Assert ("A7", exception);
510 try {
511 s = BitConverter.ToString ((byte[]) null, 20, 3);
512 exception = false;
514 catch (ArgumentNullException) {
515 exception = true;
517 Assert ("A8", exception);
519 try {
520 s = BitConverter.ToString (b, 16, 0);
521 exception = false;
523 catch (ArgumentOutOfRangeException) {
524 exception = true;
526 Assert ("A9", exception);
531 [Test]
532 [ExpectedException (typeof (ArgumentOutOfRangeException))]
533 public void ToString_StartIndexOverflow ()
535 byte[] array = new byte [4];
536 BitConverter.ToString (array, Int32.MaxValue, 1);
539 [Test]
540 [ExpectedException (typeof (ArgumentException))]
541 public void ToString_LengthOverflow ()
543 byte[] array = new byte [4];
544 BitConverter.ToString (array, 1, Int32.MaxValue);
547 [Test]
548 [ExpectedException (typeof (ArgumentOutOfRangeException))]
549 public void ToUpperLimit ()
551 byte[] array = new byte [4];
552 BitConverter.ToInt32 (array, Int32.MaxValue);
555 [Test]
556 [ExpectedException (typeof (ArgumentOutOfRangeException))]
557 public void ToLowerLimit ()
559 byte[] array = new byte [4];
560 BitConverter.ToInt32 (array, Int32.MinValue);
563 [Test]
564 public void ToBoolean ()
566 byte[] array = new byte [2] { 0x02, 0x00 };
567 Assert ("True", BitConverter.ToBoolean (array, 0));
568 AssertEquals ("True==True", true, BitConverter.ToBoolean (array, 0));
569 Assert ("False", !BitConverter.ToBoolean (array, 1));