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
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 Assert.AreEqual (i + 1, myArray2 [i], "TestBlockCopy Error at i=" + 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 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);
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);
173 numBytes = Buffer.ByteLength (floatArray);
174 Assert.AreEqual (40, numBytes, "TestByteLength: wrong byteLength for floatArray");
176 numBytes = Buffer.ByteLength (floatArray2);
177 Assert.AreEqual (400, numBytes, "TestByteLength: wrong byteLength for floatArray2");
179 numBytes = Buffer.ByteLength (floatArray3);
180 Assert.AreEqual (4000, numBytes, "TestByteLength: wrong byteLength for floatArray3");
182 numBytes = Buffer.ByteLength (floatArray4);
183 Assert.AreEqual (0, numBytes, "TestByteLength: wrong byteLength for floatArray4");
185 numBytes = Buffer.ByteLength (floatArray5);
186 Assert.AreEqual (0, numBytes, "TestByteLength: wrong byteLength for floatArray5");
188 numBytes = Buffer.ByteLength (floatArray6);
189 Assert.AreEqual (0, numBytes, "TestByteLength: wrong byteLength for floatArray6");
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.IsTrue (errorThrown, "TestGetByte: ArgumentNullException not thrown");
211 Buffer.GetByte (byteArray, -1);
212 } catch (ArgumentOutOfRangeException) {
215 Assert.IsTrue (errorThrown, "TestGetByte: ArgumentOutOfRangeException (negative index) not implemented");
220 Buffer.GetByte (byteArray, 12);
221 } catch (ArgumentOutOfRangeException) {
224 Assert.IsTrue (errorThrown, "TestGetByte: ArgumentOutOfRangeException (index bigger/equal than array's size not thrown");
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);
237 Assert.AreEqual ((Byte)8, Buffer.GetByte (byteArray, 5), "TestGetByte Error");
241 public void SetByte ()
243 bool errorThrown = false;
244 TestCase [] someArray = new TestCase [3];
247 Buffer.SetByte (null, 5, 12);
248 } catch (ArgumentNullException) {
251 Assert.IsTrue (errorThrown, "TestSetByte: ArgumentNullException not thrown");
255 Buffer.SetByte (byteArray, -1, 32);
256 } catch (ArgumentOutOfRangeException) {
259 Assert.IsTrue (errorThrown, "TestSetByte: ArgumentOutOfRangeException (case: negative index) not thrown");
263 Buffer.SetByte (byteArray, 12, 43);
264 } catch (ArgumentOutOfRangeException) {
267 Assert.IsTrue (errorThrown, "TestSetByte: ArgumentOutOfRangeException (case: index bigger/equal than array'size");
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);
279 Buffer.SetByte (byteArray, 3, (byte) 10);
280 Assert.AreEqual ((Byte)10, Buffer.GetByte (byteArray, 3), "TestSetByte");