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 {
21 byte [] byteArray = new byte [SIZE]; // 8-bits unsigned integer array
24 public void BlockCopy ()
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 };
30 Buffer.BlockCopy (myArray1, 0, myArray2, 0, SizeOfInt32 * myArray1.Length);
32 for (int i = 0; i < myArray1.Length; i++)
33 Assert.AreEqual (i + 1, myArray2 [i], "TestBlockCopy Error at i=" + i);
37 [ExpectedException (typeof (ArgumentNullException))]
38 public void BlockCopy_NullSource ()
40 byte[] dest = new byte [8];
41 Buffer.BlockCopy (null, 0, dest, 0, dest.Length);
45 [ExpectedException (typeof (ArgumentNullException))]
46 public void BlockCopy_NullDest ()
48 byte[] src = new byte [8];
49 Buffer.BlockCopy (src, 0, null, 0, src.Length);
53 [ExpectedException (typeof (ArgumentException))]
54 public void BlockCopy_ObjectSource ()
56 object[] src = new object [8];
57 byte[] dest = new byte [8];
58 Buffer.BlockCopy (src, 0, dest, 0, src.Length);
62 [ExpectedException (typeof (ArgumentException))]
63 public void BlockCopy_ObjectDest ()
65 byte[] src = new byte [8];
66 object[] dest = new object [8];
67 Buffer.BlockCopy (src, 0, dest, 0, src.Length);
71 [ExpectedException (typeof (ArgumentException))]
72 public void BlockCopy_SourceTooShort ()
74 byte[] src = new byte [8];
75 byte[] dest = new byte [8];
76 Buffer.BlockCopy (src, 4, dest, 0, src.Length);
80 [ExpectedException (typeof (ArgumentException))]
81 public void BlockCopy_DestTooShort ()
83 byte[] src = new byte [8];
84 byte[] dest = new byte [8];
85 Buffer.BlockCopy (src, 0, dest, 4, src.Length);
89 [ExpectedException (typeof (ArgumentOutOfRangeException))]
90 public void BlockCopy_SourceOffsetNegative ()
92 byte[] src = new byte [8];
93 byte[] dest = new byte [8];
94 Buffer.BlockCopy (src, -1, dest, 0, src.Length);
98 [ExpectedException (typeof (ArgumentOutOfRangeException))]
99 public void BlockCopy_DestOffsetNegative ()
101 byte[] src = new byte [8];
102 byte[] dest = new byte [8];
103 Buffer.BlockCopy (src, 0, dest, -1, src.Length);
107 [ExpectedException (typeof (ArgumentException))]
108 public void BlockCopy_SourceOffsetOverflow ()
110 byte[] src = new byte [8];
111 byte[] dest = new byte [8];
112 Buffer.BlockCopy (src, Int32.MaxValue, dest, 0, src.Length);
116 [ExpectedException (typeof (ArgumentException))]
117 public void BlockCopy_DestOffsetOverflow ()
119 byte[] src = new byte [8];
120 byte[] dest = new byte [8];
121 Buffer.BlockCopy (src, 0, dest, Int32.MaxValue, src.Length);
125 [ExpectedException (typeof (ArgumentOutOfRangeException))]
126 public void BlockCopy_LengthNegative ()
128 byte[] src = new byte [8];
129 byte[] dest = new byte [8];
130 Buffer.BlockCopy (src, 0, dest, 0, -1);
134 [ExpectedException (typeof (ArgumentException))]
135 public void BlockCopy_LengthOverflow ()
137 byte[] src = new byte [8];
138 byte[] dest = new byte [8];
139 Buffer.BlockCopy (src, 0, dest, 0, Int32.MaxValue);
143 public void ByteLength ()
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];
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);
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);
172 numBytes = Buffer.ByteLength (floatArray);
173 Assert.AreEqual (40, numBytes, "TestByteLength: wrong byteLength for floatArray");
175 numBytes = Buffer.ByteLength (floatArray2);
176 Assert.AreEqual (400, numBytes, "TestByteLength: wrong byteLength for floatArray2");
178 numBytes = Buffer.ByteLength (floatArray3);
179 Assert.AreEqual (4000, numBytes, "TestByteLength: wrong byteLength for floatArray3");
181 numBytes = Buffer.ByteLength (floatArray4);
182 Assert.AreEqual (0, numBytes, "TestByteLength: wrong byteLength for floatArray4");
184 numBytes = Buffer.ByteLength (floatArray5);
185 Assert.AreEqual (0, numBytes, "TestByteLength: wrong byteLength for floatArray5");
187 numBytes = Buffer.ByteLength (floatArray6);
188 Assert.AreEqual (0, numBytes, "TestByteLength: wrong byteLength for floatArray6");
192 public void GetByte ()
195 bool errorThrown = false;
196 byteArray = new byte [10];
198 BufferTest [] someArray = new BufferTest [3];
201 Buffer.GetByte (null, 5);
202 } catch (ArgumentNullException) {
205 Assert.IsTrue (errorThrown, "TestGetByte: ArgumentNullException not thrown");
210 Buffer.GetByte (byteArray, -1);
211 } catch (ArgumentOutOfRangeException) {
214 Assert.IsTrue (errorThrown, "TestGetByte: ArgumentOutOfRangeException (negative index) not implemented");
219 Buffer.GetByte (byteArray, 12);
220 } catch (ArgumentOutOfRangeException) {
223 Assert.IsTrue (errorThrown, "TestGetByte: ArgumentOutOfRangeException (index bigger/equal than array's size not thrown");
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);
236 Assert.AreEqual ((Byte)8, Buffer.GetByte (byteArray, 5), "TestGetByte Error");
240 public void SetByte ()
242 bool errorThrown = false;
243 BufferTest [] someArray = new BufferTest [3];
246 Buffer.SetByte (null, 5, 12);
247 } catch (ArgumentNullException) {
250 Assert.IsTrue (errorThrown, "TestSetByte: ArgumentNullException not thrown");
254 Buffer.SetByte (byteArray, -1, 32);
255 } catch (ArgumentOutOfRangeException) {
258 Assert.IsTrue (errorThrown, "TestSetByte: ArgumentOutOfRangeException (case: negative index) not thrown");
262 Buffer.SetByte (byteArray, 12, 43);
263 } catch (ArgumentOutOfRangeException) {
266 Assert.IsTrue (errorThrown, "TestSetByte: ArgumentOutOfRangeException (case: index bigger/equal than array'size");
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);
278 Buffer.SetByte (byteArray, 3, (byte) 10);
279 Assert.AreEqual ((Byte)10, Buffer.GetByte (byteArray, 3), "TestSetByte");
283 public void MemoryCopy_Simple ()
288 Buffer.MemoryCopy (&a, &b, 4, 2);
291 Assert.AreEqual (0xBC614E, a, "#1");
292 Assert.AreEqual (0x614E, b, "#2");