File: BufferTest.cs

package info (click to toggle)
mono 1.2.2.1-1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 142,728 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,304; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (287 lines) | stat: -rw-r--r-- 7,932 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
// BufferTest.cs - NUnit Test Cases for the Buffer class.
//
// Authors
//	Cesar Octavio Lopez Nataren (cesar@ciencias.unam.mx)
//	Sebastien Pouliot (sebastien@ximian.com)
//
// (C) Cesar Octavio Lopez Nataren 2002
// Copyright (C) 2004 Novell (http://www.novell.com)
// 

using NUnit.Framework;
using System;

namespace MonoTests.System {

	[TestFixture]
	public class BufferTest : Assertion {

		const int SIZE = 10;
		byte [] byteArray  = new byte [SIZE];   // 8-bits unsigned integer array
		float [] floatArray = new float [SIZE];
		
		[Test]
		public void BlockCopy ()
		{
			int SizeOfInt32 = 4;
			int [] myArray1 = new int [5] {1, 2, 3, 4, 5};
			int [] myArray2 = new int [10] { 0, 0, 0, 0, 0, 6, 7, 8, 9, 10 };
		
			Buffer.BlockCopy (myArray1, 0, myArray2, 0, SizeOfInt32  * myArray1.Length);
		
			for (int i = 0; i < myArray1.Length; i++) 
				AssertEquals ("TestBlockCopy Error at i=" + i, i + 1, myArray2 [i]);		
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void BlockCopy_NullSource ()
		{
			byte[] dest = new byte [8];
			Buffer.BlockCopy (null, 0, dest, 0, dest.Length);
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void BlockCopy_NullDest ()
		{
			byte[] src = new byte [8];
			Buffer.BlockCopy (src, 0, null, 0, src.Length);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void BlockCopy_ObjectSource ()
		{
			object[] src = new object [8];
			byte[] dest = new byte [8];
			Buffer.BlockCopy (src, 0, dest, 0, src.Length);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void BlockCopy_ObjectDest ()
		{
			byte[] src = new byte [8];
			object[] dest = new object [8];
			Buffer.BlockCopy (src, 0, dest, 0, src.Length);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void BlockCopy_SourceTooShort ()
		{
			byte[] src = new byte [8];
			byte[] dest = new byte [8];
			Buffer.BlockCopy (src, 4, dest, 0, src.Length);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void BlockCopy_DestTooShort ()
		{
			byte[] src = new byte [8];
			byte[] dest = new byte [8];
			Buffer.BlockCopy (src, 0, dest, 4, src.Length);
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void BlockCopy_SourceOffsetNegative ()
		{
			byte[] src = new byte [8];
			byte[] dest = new byte [8];
			Buffer.BlockCopy (src, -1, dest, 0, src.Length);
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void BlockCopy_DestOffsetNegative ()
		{
			byte[] src = new byte [8];
			byte[] dest = new byte [8];
			Buffer.BlockCopy (src, 0, dest, -1, src.Length);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void BlockCopy_SourceOffsetOverflow ()
		{
			byte[] src = new byte [8];
			byte[] dest = new byte [8];
			Buffer.BlockCopy (src, Int32.MaxValue, dest, 0, src.Length);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void BlockCopy_DestOffsetOverflow ()
		{
			byte[] src = new byte [8];
			byte[] dest = new byte [8];
			Buffer.BlockCopy (src, 0, dest, Int32.MaxValue, src.Length);
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void BlockCopy_LengthNegative ()
		{
			byte[] src = new byte [8];
			byte[] dest = new byte [8];
			Buffer.BlockCopy (src, 0, dest, 0, -1);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void BlockCopy_LengthOverflow ()
		{
			byte[] src = new byte [8];
			byte[] dest = new byte [8];
			Buffer.BlockCopy (src, 0, dest, 0, Int32.MaxValue);
		}

		[Test]
		public void ByteLength ()
		{
			int numBytes;	
			float [] floatArray = new float [10];
			float [,] floatArray2 = new float [10,10];
			float [,,] floatArray3 = new float [10,10,10];
			float [,,,] floatArray4 = new float [10,0,10,10];
			float [,,,] floatArray5 = new float [0,0,0,0];
			float [] floatArray6 = new float [0];
			TestCase [] someArray = new TestCase [3];
		
			try {
				Buffer.ByteLength (null);	
				Fail ("TestByteLength: ArgumentNullException not thrown");
			} catch (ArgumentNullException) {
				// do nothing, this is expected
			} catch (Exception e) {
				Fail ("Unexpected exception on Buffer.ByteLength (null):" + e);
			}
		
			try {
				Buffer.ByteLength (someArray);	
				Fail ("TestByteLength: ArgumentException not thrown");
			} catch (ArgumentException) {
				// do nothing, this is expected
			} catch (Exception e) {
				Fail ("Unexpected exception on Buffer.ByteLength (non primitive array):" + e);
			}
		
			numBytes = Buffer.ByteLength (floatArray);
			AssertEquals ("TestByteLength: wrong byteLength for floatArray", 40, numBytes);

			numBytes = Buffer.ByteLength (floatArray2);
			AssertEquals ("TestByteLength: wrong byteLength for floatArray2", 400, numBytes);

			numBytes = Buffer.ByteLength (floatArray3);
			AssertEquals ("TestByteLength: wrong byteLength for floatArray3", 4000, numBytes);

			numBytes = Buffer.ByteLength (floatArray4);
			AssertEquals ("TestByteLength: wrong byteLength for floatArray4", 0, numBytes);

			numBytes = Buffer.ByteLength (floatArray5);
			AssertEquals ("TestByteLength: wrong byteLength for floatArray5", 0, numBytes);

			numBytes = Buffer.ByteLength (floatArray6);
			AssertEquals ("TestByteLength: wrong byteLength for floatArray6", 0, numBytes);
		}
		
		[Test]
		public void GetByte () 
		{
			Byte [] byteArray;
			bool errorThrown = false;
			byteArray = new byte [10];
			byteArray [5] = 8;
			TestCase [] someArray = new TestCase [3];
		
			try {
				Buffer.GetByte (null, 5);
			} catch (ArgumentNullException) {
				errorThrown = true;
			}
			Assert ("TestGetByte: ArgumentNullException not thrown",
				errorThrown);
		
			errorThrown = false;
		
			try {
				Buffer.GetByte (byteArray, -1);
			} catch (ArgumentOutOfRangeException) {
				errorThrown = true;
			}
			Assert ("TestGetByte: ArgumentOutOfRangeException (negative index) not implemented",
				errorThrown);
		
			errorThrown = false;
		
			try {
				Buffer.GetByte (byteArray, 12); 
			} catch (ArgumentOutOfRangeException) {
				errorThrown = true;
			}
			Assert ("TestGetByte: ArgumentOutOfRangeException (index bigger/equal than array's size not thrown", errorThrown);
		
			errorThrown = false;
		
			try {
				Buffer.GetByte (someArray, 0);	
				Fail ("TestGetByte: ArgumentException not thrown");
			} catch (ArgumentException) {
				// do nothing, this is expected
			} catch (Exception e) {
				Fail ("Unexpected exception on Buffer.GetByte (non primitive array):" + e);
			}
		
			AssertEquals ("TestGetByte Error", (Byte)8, Buffer.GetByte (byteArray, 5));
		}
	
		[Test]
		public void SetByte ()
		{
			bool errorThrown = false;
			TestCase [] someArray = new TestCase [3];
		
			try {
				Buffer.SetByte (null, 5, 12);
			} catch (ArgumentNullException) {
				errorThrown = true;
			}
			Assert ("TestSetByte: ArgumentNullException not thrown", errorThrown);
			errorThrown = false;
		
			try {
				Buffer.SetByte (byteArray, -1, 32);
			} catch (ArgumentOutOfRangeException) {
				errorThrown = true;
			}
			Assert ("TestSetByte: ArgumentOutOfRangeException (case: negative index) not thrown",
				errorThrown);
			errorThrown = false;
		
			try {
				Buffer.SetByte (byteArray, 12, 43);
			} catch (ArgumentOutOfRangeException) {
				errorThrown = true;
			}
			Assert ("TestSetByte: ArgumentOutOfRangeException (case: index bigger/equal than array'size",
				errorThrown);
			errorThrown = false;
		
			try {
				Buffer.SetByte (someArray, 0, 42);	
				Fail ("TestSetByte: ArgumentException not thrown");
			} catch (ArgumentException) {
				// do nothing, this is expected
			} catch (Exception e) {
				Fail ("Unexpected exception on Buffer.SetByte (non primitive array):" + e);
			}
		
			Buffer.SetByte (byteArray, 3, (byte) 10);
			AssertEquals ("TestSetByte", (Byte)10, Buffer.GetByte (byteArray, 3));
		}
	}
}