2 // BufferTest.cs - NUnit Test Cases for the Buffer class.
5 // Cesar Octavio Lopez Nataren (cesar@ciencias.unam.mx)
6 // Sebastien Pouliot (sebastien@ximian.com)
8 // (C) Cesar Octavio Lopez Nataren 2002
9 // Copyright (C) 2004 Novell (http://www.novell.com)
12 using NUnit.Framework;
15 namespace MonoTests.System {
18 public class BufferTest : Assertion {
21 byte [] byteArray = new byte [SIZE]; // 8-bits unsigned integer array
22 float [] floatArray = new float [SIZE];
25 public void BlockCopy ()
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 };
31 Buffer.BlockCopy (myArray1, 0, myArray2, 0, SizeOfInt32 * myArray1.Length);
33 for (int i = 0; i < myArray1.Length; i++)
34 AssertEquals ("TestBlockCopy Error at i=" + i, i + 1, myArray2 [i]);
38 [ExpectedException (typeof (ArgumentNullException))]
39 public void BlockCopy_NullSource ()
41 byte[] dest = new byte [8];
42 Buffer.BlockCopy (null, 0, dest, 0, dest.Length);
46 [ExpectedException (typeof (ArgumentNullException))]
47 public void BlockCopy_NullDest ()
49 byte[] src = new byte [8];
50 Buffer.BlockCopy (src, 0, null, 0, src.Length);
54 [ExpectedException (typeof (ArgumentException))]
55 public void BlockCopy_ObjectSource ()
57 object[] src = new object [8];
58 byte[] dest = new byte [8];
59 Buffer.BlockCopy (src, 0, dest, 0, src.Length);
63 [ExpectedException (typeof (ArgumentException))]
64 public void BlockCopy_ObjectDest ()
66 byte[] src = new byte [8];
67 object[] dest = new object [8];
68 Buffer.BlockCopy (src, 0, dest, 0, src.Length);
72 [ExpectedException (typeof (ArgumentException))]
73 public void BlockCopy_SourceTooShort ()
75 byte[] src = new byte [8];
76 byte[] dest = new byte [8];
77 Buffer.BlockCopy (src, 4, dest, 0, src.Length);
81 [ExpectedException (typeof (ArgumentException))]
82 public void BlockCopy_DestTooShort ()
84 byte[] src = new byte [8];
85 byte[] dest = new byte [8];
86 Buffer.BlockCopy (src, 0, dest, 4, src.Length);
90 [ExpectedException (typeof (ArgumentOutOfRangeException))]
91 public void BlockCopy_SourceOffsetNegative ()
93 byte[] src = new byte [8];
94 byte[] dest = new byte [8];
95 Buffer.BlockCopy (src, -1, dest, 0, src.Length);
99 [ExpectedException (typeof (ArgumentOutOfRangeException))]
100 public void BlockCopy_DestOffsetNegative ()
102 byte[] src = new byte [8];
103 byte[] dest = new byte [8];
104 Buffer.BlockCopy (src, 0, dest, -1, src.Length);
108 [ExpectedException (typeof (ArgumentException))]
109 public void BlockCopy_SourceOffsetOverflow ()
111 byte[] src = new byte [8];
112 byte[] dest = new byte [8];
113 Buffer.BlockCopy (src, Int32.MaxValue, dest, 0, src.Length);
117 [ExpectedException (typeof (ArgumentException))]
118 public void BlockCopy_DestOffsetOverflow ()
120 byte[] src = new byte [8];
121 byte[] dest = new byte [8];
122 Buffer.BlockCopy (src, 0, dest, Int32.MaxValue, src.Length);
126 [ExpectedException (typeof (ArgumentOutOfRangeException))]
127 public void BlockCopy_LengthNegative ()
129 byte[] src = new byte [8];
130 byte[] dest = new byte [8];
131 Buffer.BlockCopy (src, 0, dest, 0, -1);
135 [ExpectedException (typeof (ArgumentException))]
136 public void BlockCopy_LengthOverflow ()
138 byte[] src = new byte [8];
139 byte[] dest = new byte [8];
140 Buffer.BlockCopy (src, 0, dest, 0, Int32.MaxValue);
144 public void ByteLength ()
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];
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);
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);
173 numBytes = Buffer.ByteLength (floatArray);
174 AssertEquals ("TestByteLength: wrong byteLength for floatArray", 40, numBytes);
176 numBytes = Buffer.ByteLength (floatArray2);
177 AssertEquals ("TestByteLength: wrong byteLength for floatArray2", 400, numBytes);
179 numBytes = Buffer.ByteLength (floatArray3);
180 AssertEquals ("TestByteLength: wrong byteLength for floatArray3", 4000, numBytes);
182 numBytes = Buffer.ByteLength (floatArray4);
183 AssertEquals ("TestByteLength: wrong byteLength for floatArray4", 0, numBytes);
185 numBytes = Buffer.ByteLength (floatArray5);
186 AssertEquals ("TestByteLength: wrong byteLength for floatArray5", 0, numBytes);
188 numBytes = Buffer.ByteLength (floatArray6);
189 AssertEquals ("TestByteLength: wrong byteLength for floatArray6", 0, numBytes);
193 public void GetByte ()
196 bool errorThrown = false;
197 byteArray = new byte [10];
199 TestCase [] someArray = new TestCase [3];
202 Buffer.GetByte (null, 5);
203 } catch (ArgumentNullException) {
206 Assert ("TestGetByte: ArgumentNullException not thrown",
212 Buffer.GetByte (byteArray, -1);
213 } catch (ArgumentOutOfRangeException) {
216 Assert ("TestGetByte: ArgumentOutOfRangeException (negative index) not implemented",
222 Buffer.GetByte (byteArray, 12);
223 } catch (ArgumentOutOfRangeException) {
226 Assert ("TestGetByte: ArgumentOutOfRangeException (index bigger/equal than array's size not thrown", errorThrown);
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);
239 AssertEquals ("TestGetByte Error", (Byte)8, Buffer.GetByte (byteArray, 5));
243 public void SetByte ()
245 bool errorThrown = false;
246 TestCase [] someArray = new TestCase [3];
249 Buffer.SetByte (null, 5, 12);
250 } catch (ArgumentNullException) {
253 Assert ("TestSetByte: ArgumentNullException not thrown", errorThrown);
257 Buffer.SetByte (byteArray, -1, 32);
258 } catch (ArgumentOutOfRangeException) {
261 Assert ("TestSetByte: ArgumentOutOfRangeException (case: negative index) not thrown",
266 Buffer.SetByte (byteArray, 12, 43);
267 } catch (ArgumentOutOfRangeException) {
270 Assert ("TestSetByte: ArgumentOutOfRangeException (case: index bigger/equal than array'size",
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);
283 Buffer.SetByte (byteArray, 3, (byte) 10);
284 AssertEquals ("TestSetByte", (Byte)10, Buffer.GetByte (byteArray, 3));