2002-12-21 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / Test / System / BufferTest.cs
1 // BufferTest.cs - NUnit Test Cases for the Buffer class.
2 //
3 // Cesar Octavio Lopez Nataren (cesar@ciencias.unam.mx)
4 //
5 // (C) Cesar Octavio Lopez Nataren 2002
6 // 
7
8 using NUnit.Framework;
9 using System;
10
11
12 namespace MonoTests.System
13 {
14         public class BufferTest : TestCase
15         {
16                 const int SIZE = 10;
17                 byte [] byteArray  = new byte [SIZE];   // 8-bits unsigned integer array
18                 float [] floatArray = new float [SIZE];
19                 
20                 public BufferTest () {}
21
22                 protected override void SetUp () {}
23
24
25                 protected override void TearDown () {}
26
27                 public void TestBlockCopy ()
28                 {
29                         int SizeOfInt32 = 4;
30                         int [] myArray1 = new int [5] {1, 2, 3, 4, 5};
31                         int [] myArray2 = new int [10] { 0, 0, 0, 0, 0, 6, 7, 8, 9, 10 };
32                 
33                         Buffer.BlockCopy (myArray1, 0, myArray2, 0, SizeOfInt32  * myArray1.Length);
34                 
35                         for (int i = 0; i < myArray1.Length; i++) 
36                                 AssertEquals ("TestBlockCopy Error at i=" + i, i + 1, myArray2 [i]);            
37                 }
38         
39         
40                 public void TestByteLength ()
41                 {
42                         int numBytes;   
43                         float [] floatArray = new float [10];
44                         TestCase [] someArray = new TestCase [3];
45                 
46                         try {
47                                 Buffer.ByteLength (null);       
48                                 Fail ("TestByteLength: ArgumentNullException not thrown");
49                         } catch (ArgumentNullException) {
50                                 // do nothing, this is expected
51                         } catch (Exception e) {
52                                 Fail ("Unexpected exception on Buffer.ByteLength (null):" + e);
53                         }
54                 
55                         try {
56                                 Buffer.ByteLength (someArray);  
57                                 Fail ("TestByteLength: ArgumentException not thrown");
58                         } catch (ArgumentException) {
59                                 // do nothing, this is expected
60                         } catch (Exception e) {
61                                 Fail ("Unexpected exception on Buffer.ByteLength (non primitive array):" + e);
62                         }
63                 
64                         numBytes = Buffer.ByteLength (floatArray);
65                         AssertEquals ("TestByteLength: wrong byteLength", 40, numBytes);
66                 }
67                 
68                 public void TestGetByte () 
69                 {
70                         Byte [] byteArray;
71                         bool errorThrown = false;
72                         byteArray = new byte [10];
73                         byteArray [5] = 8;
74                         TestCase [] someArray = new TestCase [3];
75                 
76                         try {
77                                 Buffer.GetByte (null, 5);
78                         } catch (ArgumentNullException) {
79                                 errorThrown = true;
80                         }
81                         Assert ("TestGetByte: ArgumentNullException not thrown",
82                                 errorThrown);
83                 
84                         errorThrown = false;
85                 
86                         try {
87                                 Buffer.GetByte (byteArray, -1);
88                         } catch (ArgumentOutOfRangeException) {
89                                 errorThrown = true;
90                         }
91                         Assert ("TestGetByte: ArgumentOutOfRangeException (negative index) not implemented",
92                                 errorThrown);
93                 
94                         errorThrown = false;
95                 
96                         try {
97                                 Buffer.GetByte (byteArray, 12); 
98                         } catch (ArgumentOutOfRangeException) {
99                                 errorThrown = true;
100                         }
101                         Assert ("TestGetByte: ArgumentOutOfRangeException (index bigger/equal than array's size not thrown", errorThrown);
102                 
103                         errorThrown = false;
104                 
105                         try {
106                                 Buffer.GetByte (someArray, 0);  
107                                 Fail ("TestGetByte: ArgumentException not thrown");
108                         } catch (ArgumentException) {
109                                 // do nothing, this is expected
110                         } catch (Exception e) {
111                                 Fail ("Unexpected exception on Buffer.GetByte (non primitive array):" + e);
112                         }
113                 
114                         AssertEquals ("TestGetByte Error", (Byte)8, Buffer.GetByte (byteArray, 5));
115                 }
116         
117         
118                 public void TestSetByte ()
119                 {
120                         bool errorThrown = false;
121                         TestCase [] someArray = new TestCase [3];
122                 
123                         try {
124                                 Buffer.SetByte (null, 5, 12);
125                         } catch (ArgumentNullException) {
126                                 errorThrown = true;
127                         }
128                         Assert ("TestSetByte: ArgumentNullException not thrown", errorThrown);
129                         errorThrown = false;
130                 
131                         try {
132                                 Buffer.SetByte (byteArray, -1, 32);
133                         } catch (ArgumentOutOfRangeException) {
134                                 errorThrown = true;
135                         }
136                         Assert ("TestSetByte: ArgumentOutOfRangeException (case: negative index) not thrown",
137                                 errorThrown);
138                         errorThrown = false;
139                 
140                         try {
141                                 Buffer.SetByte (byteArray, 12, 43);
142                         } catch (ArgumentOutOfRangeException) {
143                                 errorThrown = true;
144                         }
145                         Assert ("TestSetByte: ArgumentOutOfRangeException (case: index bigger/equal than array'size",
146                                 errorThrown);
147                         errorThrown = false;
148                 
149                         try {
150                                 Buffer.SetByte (someArray, 0, 42);      
151                                 Fail ("TestSetByte: ArgumentException not thrown");
152                         } catch (ArgumentException) {
153                                 // do nothing, this is expected
154                         } catch (Exception e) {
155                                 Fail ("Unexpected exception on Buffer.SetByte (non primitive array):" + e);
156                         }
157                 
158                         Buffer.SetByte (byteArray, 3, (byte) 10);
159                         AssertEquals ("TestSetByte", (Byte)10, Buffer.GetByte (byteArray, 3));
160                 }
161         }
162 }