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