2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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                 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                                 Assert.AreEqual (i + 1, myArray2 [i], "TestBlockCopy Error at i=" + 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                                 Assert.Fail ("TestByteLength: ArgumentNullException not thrown");
158                         } catch (ArgumentNullException) {
159                                 // do nothing, this is expected
160                         } catch (Exception e) {
161                                 Assert.Fail ("Unexpected exception on Buffer.ByteLength (null):" + e);
162                         }
163                 
164                         try {
165                                 Buffer.ByteLength (someArray);  
166                                 Assert.Fail ("TestByteLength: ArgumentException not thrown");
167                         } catch (ArgumentException) {
168                                 // do nothing, this is expected
169                         } catch (Exception e) {
170                                 Assert.Fail ("Unexpected exception on Buffer.ByteLength (non primitive array):" + e);
171                         }
172                 
173                         numBytes = Buffer.ByteLength (floatArray);
174                         Assert.AreEqual (40, numBytes, "TestByteLength: wrong byteLength for floatArray");
175
176                         numBytes = Buffer.ByteLength (floatArray2);
177                         Assert.AreEqual (400, numBytes, "TestByteLength: wrong byteLength for floatArray2");
178
179                         numBytes = Buffer.ByteLength (floatArray3);
180                         Assert.AreEqual (4000, numBytes, "TestByteLength: wrong byteLength for floatArray3");
181
182                         numBytes = Buffer.ByteLength (floatArray4);
183                         Assert.AreEqual (0, numBytes, "TestByteLength: wrong byteLength for floatArray4");
184
185                         numBytes = Buffer.ByteLength (floatArray5);
186                         Assert.AreEqual (0, numBytes, "TestByteLength: wrong byteLength for floatArray5");
187
188                         numBytes = Buffer.ByteLength (floatArray6);
189                         Assert.AreEqual (0, numBytes, "TestByteLength: wrong byteLength for floatArray6");
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.IsTrue (errorThrown, "TestGetByte: ArgumentNullException not thrown");
207                 
208                         errorThrown = false;
209                 
210                         try {
211                                 Buffer.GetByte (byteArray, -1);
212                         } catch (ArgumentOutOfRangeException) {
213                                 errorThrown = true;
214                         }
215                         Assert.IsTrue (errorThrown, "TestGetByte: ArgumentOutOfRangeException (negative index) not implemented");
216                 
217                         errorThrown = false;
218                 
219                         try {
220                                 Buffer.GetByte (byteArray, 12); 
221                         } catch (ArgumentOutOfRangeException) {
222                                 errorThrown = true;
223                         }
224                         Assert.IsTrue (errorThrown, "TestGetByte: ArgumentOutOfRangeException (index bigger/equal than array's size not thrown");
225                 
226                         errorThrown = false;
227                 
228                         try {
229                                 Buffer.GetByte (someArray, 0);  
230                                 Assert.Fail ("TestGetByte: ArgumentException not thrown");
231                         } catch (ArgumentException) {
232                                 // do nothing, this is expected
233                         } catch (Exception e) {
234                                 Assert.Fail ("Unexpected exception on Buffer.GetByte (non primitive array):" + e);
235                         }
236                 
237                         Assert.AreEqual ((Byte)8, Buffer.GetByte (byteArray, 5), "TestGetByte Error");
238                 }
239         
240                 [Test]
241                 public void SetByte ()
242                 {
243                         bool errorThrown = false;
244                         TestCase [] someArray = new TestCase [3];
245                 
246                         try {
247                                 Buffer.SetByte (null, 5, 12);
248                         } catch (ArgumentNullException) {
249                                 errorThrown = true;
250                         }
251                         Assert.IsTrue (errorThrown, "TestSetByte: ArgumentNullException not thrown");
252                         errorThrown = false;
253                 
254                         try {
255                                 Buffer.SetByte (byteArray, -1, 32);
256                         } catch (ArgumentOutOfRangeException) {
257                                 errorThrown = true;
258                         }
259                         Assert.IsTrue (errorThrown, "TestSetByte: ArgumentOutOfRangeException (case: negative index) not thrown");
260                         errorThrown = false;
261                 
262                         try {
263                                 Buffer.SetByte (byteArray, 12, 43);
264                         } catch (ArgumentOutOfRangeException) {
265                                 errorThrown = true;
266                         }
267                         Assert.IsTrue (errorThrown, "TestSetByte: ArgumentOutOfRangeException (case: index bigger/equal than array'size");
268                         errorThrown = false;
269                 
270                         try {
271                                 Buffer.SetByte (someArray, 0, 42);      
272                                 Assert.Fail ("TestSetByte: ArgumentException not thrown");
273                         } catch (ArgumentException) {
274                                 // do nothing, this is expected
275                         } catch (Exception e) {
276                                 Assert.Fail ("Unexpected exception on Buffer.SetByte (non primitive array):" + e);
277                         }
278                 
279                         Buffer.SetByte (byteArray, 3, (byte) 10);
280                         Assert.AreEqual ((Byte)10, Buffer.GetByte (byteArray, 3), "TestSetByte");
281                 }
282         }
283 }