New test.
[mono.git] / mcs / class / corlib / Test / System / BufferTest.cs
1 //
2 // BufferTest.cs - NUnit Test Cases for the Buffer class.
3 //
4 // Authors
5 //      Cesar Octavio Lopez Nataren (cesar@ciencias.unam.mx)
6 //      Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // (C) Cesar Octavio Lopez Nataren 2002
9 // Copyright (C) 2004 Novell (http://www.novell.com)
10 // 
11
12 using NUnit.Framework;
13 using System;
14
15 namespace MonoTests.System {
16
17         [TestFixture]
18         public class BufferTest : Assertion {
19
20                 const int SIZE = 10;
21                 byte [] byteArray  = new byte [SIZE];   // 8-bits unsigned integer array
22                 float [] floatArray = new float [SIZE];
23                 
24                 [Test]
25                 public void BlockCopy ()
26                 {
27                         int SizeOfInt32 = 4;
28                         int [] myArray1 = new int [5] {1, 2, 3, 4, 5};
29                         int [] myArray2 = new int [10] { 0, 0, 0, 0, 0, 6, 7, 8, 9, 10 };
30                 
31                         Buffer.BlockCopy (myArray1, 0, myArray2, 0, SizeOfInt32  * myArray1.Length);
32                 
33                         for (int i = 0; i < myArray1.Length; i++) 
34                                 AssertEquals ("TestBlockCopy Error at i=" + i, i + 1, myArray2 [i]);            
35                 }
36
37                 [Test]
38                 [ExpectedException (typeof (ArgumentNullException))]
39                 public void BlockCopy_NullSource ()
40                 {
41                         byte[] dest = new byte [8];
42                         Buffer.BlockCopy (null, 0, dest, 0, dest.Length);
43                 }
44
45                 [Test]
46                 [ExpectedException (typeof (ArgumentNullException))]
47                 public void BlockCopy_NullDest ()
48                 {
49                         byte[] src = new byte [8];
50                         Buffer.BlockCopy (src, 0, null, 0, src.Length);
51                 }
52
53                 [Test]
54                 [ExpectedException (typeof (ArgumentException))]
55                 public void BlockCopy_ObjectSource ()
56                 {
57                         object[] src = new object [8];
58                         byte[] dest = new byte [8];
59                         Buffer.BlockCopy (src, 0, dest, 0, src.Length);
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (ArgumentException))]
64                 public void BlockCopy_ObjectDest ()
65                 {
66                         byte[] src = new byte [8];
67                         object[] dest = new object [8];
68                         Buffer.BlockCopy (src, 0, dest, 0, src.Length);
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (ArgumentException))]
73                 public void BlockCopy_SourceTooShort ()
74                 {
75                         byte[] src = new byte [8];
76                         byte[] dest = new byte [8];
77                         Buffer.BlockCopy (src, 4, dest, 0, src.Length);
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (ArgumentException))]
82                 public void BlockCopy_DestTooShort ()
83                 {
84                         byte[] src = new byte [8];
85                         byte[] dest = new byte [8];
86                         Buffer.BlockCopy (src, 0, dest, 4, src.Length);
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
91                 public void BlockCopy_SourceOffsetNegative ()
92                 {
93                         byte[] src = new byte [8];
94                         byte[] dest = new byte [8];
95                         Buffer.BlockCopy (src, -1, dest, 0, src.Length);
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
100                 public void BlockCopy_DestOffsetNegative ()
101                 {
102                         byte[] src = new byte [8];
103                         byte[] dest = new byte [8];
104                         Buffer.BlockCopy (src, 0, dest, -1, src.Length);
105                 }
106
107                 [Test]
108                 [ExpectedException (typeof (ArgumentException))]
109                 public void BlockCopy_SourceOffsetOverflow ()
110                 {
111                         byte[] src = new byte [8];
112                         byte[] dest = new byte [8];
113                         Buffer.BlockCopy (src, Int32.MaxValue, dest, 0, src.Length);
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (ArgumentException))]
118                 public void BlockCopy_DestOffsetOverflow ()
119                 {
120                         byte[] src = new byte [8];
121                         byte[] dest = new byte [8];
122                         Buffer.BlockCopy (src, 0, dest, Int32.MaxValue, src.Length);
123                 }
124
125                 [Test]
126                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
127                 public void BlockCopy_LengthNegative ()
128                 {
129                         byte[] src = new byte [8];
130                         byte[] dest = new byte [8];
131                         Buffer.BlockCopy (src, 0, dest, 0, -1);
132                 }
133
134                 [Test]
135                 [ExpectedException (typeof (ArgumentException))]
136                 public void BlockCopy_LengthOverflow ()
137                 {
138                         byte[] src = new byte [8];
139                         byte[] dest = new byte [8];
140                         Buffer.BlockCopy (src, 0, dest, 0, Int32.MaxValue);
141                 }
142
143                 [Test]
144                 public void ByteLength ()
145                 {
146                         int numBytes;   
147                         float [] floatArray = new float [10];
148                         float [,] floatArray2 = new float [10,10];
149                         float [,,] floatArray3 = new float [10,10,10];
150                         float [,,,] floatArray4 = new float [10,0,10,10];
151                         float [,,,] floatArray5 = new float [0,0,0,0];
152                         float [] floatArray6 = new float [0];
153                         TestCase [] someArray = new TestCase [3];
154                 
155                         try {
156                                 Buffer.ByteLength (null);       
157                                 Fail ("TestByteLength: ArgumentNullException not thrown");
158                         } catch (ArgumentNullException) {
159                                 // do nothing, this is expected
160                         } catch (Exception e) {
161                                 Fail ("Unexpected exception on Buffer.ByteLength (null):" + e);
162                         }
163                 
164                         try {
165                                 Buffer.ByteLength (someArray);  
166                                 Fail ("TestByteLength: ArgumentException not thrown");
167                         } catch (ArgumentException) {
168                                 // do nothing, this is expected
169                         } catch (Exception e) {
170                                 Fail ("Unexpected exception on Buffer.ByteLength (non primitive array):" + e);
171                         }
172                 
173                         numBytes = Buffer.ByteLength (floatArray);
174                         AssertEquals ("TestByteLength: wrong byteLength for floatArray", 40, numBytes);
175
176                         numBytes = Buffer.ByteLength (floatArray2);
177                         AssertEquals ("TestByteLength: wrong byteLength for floatArray2", 400, numBytes);
178
179                         numBytes = Buffer.ByteLength (floatArray3);
180                         AssertEquals ("TestByteLength: wrong byteLength for floatArray3", 4000, numBytes);
181
182                         numBytes = Buffer.ByteLength (floatArray4);
183                         AssertEquals ("TestByteLength: wrong byteLength for floatArray4", 0, numBytes);
184
185                         numBytes = Buffer.ByteLength (floatArray5);
186                         AssertEquals ("TestByteLength: wrong byteLength for floatArray5", 0, numBytes);
187
188                         numBytes = Buffer.ByteLength (floatArray6);
189                         AssertEquals ("TestByteLength: wrong byteLength for floatArray6", 0, numBytes);
190                 }
191                 
192                 [Test]
193                 public void GetByte () 
194                 {
195                         Byte [] byteArray;
196                         bool errorThrown = false;
197                         byteArray = new byte [10];
198                         byteArray [5] = 8;
199                         TestCase [] someArray = new TestCase [3];
200                 
201                         try {
202                                 Buffer.GetByte (null, 5);
203                         } catch (ArgumentNullException) {
204                                 errorThrown = true;
205                         }
206                         Assert ("TestGetByte: ArgumentNullException not thrown",
207                                 errorThrown);
208                 
209                         errorThrown = false;
210                 
211                         try {
212                                 Buffer.GetByte (byteArray, -1);
213                         } catch (ArgumentOutOfRangeException) {
214                                 errorThrown = true;
215                         }
216                         Assert ("TestGetByte: ArgumentOutOfRangeException (negative index) not implemented",
217                                 errorThrown);
218                 
219                         errorThrown = false;
220                 
221                         try {
222                                 Buffer.GetByte (byteArray, 12); 
223                         } catch (ArgumentOutOfRangeException) {
224                                 errorThrown = true;
225                         }
226                         Assert ("TestGetByte: ArgumentOutOfRangeException (index bigger/equal than array's size not thrown", errorThrown);
227                 
228                         errorThrown = false;
229                 
230                         try {
231                                 Buffer.GetByte (someArray, 0);  
232                                 Fail ("TestGetByte: ArgumentException not thrown");
233                         } catch (ArgumentException) {
234                                 // do nothing, this is expected
235                         } catch (Exception e) {
236                                 Fail ("Unexpected exception on Buffer.GetByte (non primitive array):" + e);
237                         }
238                 
239                         AssertEquals ("TestGetByte Error", (Byte)8, Buffer.GetByte (byteArray, 5));
240                 }
241         
242                 [Test]
243                 public void SetByte ()
244                 {
245                         bool errorThrown = false;
246                         TestCase [] someArray = new TestCase [3];
247                 
248                         try {
249                                 Buffer.SetByte (null, 5, 12);
250                         } catch (ArgumentNullException) {
251                                 errorThrown = true;
252                         }
253                         Assert ("TestSetByte: ArgumentNullException not thrown", errorThrown);
254                         errorThrown = false;
255                 
256                         try {
257                                 Buffer.SetByte (byteArray, -1, 32);
258                         } catch (ArgumentOutOfRangeException) {
259                                 errorThrown = true;
260                         }
261                         Assert ("TestSetByte: ArgumentOutOfRangeException (case: negative index) not thrown",
262                                 errorThrown);
263                         errorThrown = false;
264                 
265                         try {
266                                 Buffer.SetByte (byteArray, 12, 43);
267                         } catch (ArgumentOutOfRangeException) {
268                                 errorThrown = true;
269                         }
270                         Assert ("TestSetByte: ArgumentOutOfRangeException (case: index bigger/equal than array'size",
271                                 errorThrown);
272                         errorThrown = false;
273                 
274                         try {
275                                 Buffer.SetByte (someArray, 0, 42);      
276                                 Fail ("TestSetByte: ArgumentException not thrown");
277                         } catch (ArgumentException) {
278                                 // do nothing, this is expected
279                         } catch (Exception e) {
280                                 Fail ("Unexpected exception on Buffer.SetByte (non primitive array):" + e);
281                         }
282                 
283                         Buffer.SetByte (byteArray, 3, (byte) 10);
284                         AssertEquals ("TestSetByte", (Byte)10, Buffer.GetByte (byteArray, 3));
285                 }
286         }
287 }