File: QueueTest.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (382 lines) | stat: -rw-r--r-- 9,005 bytes parent folder | download | duplicates (4)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// Queue.cs
//
// Author:
//  Ben Maurer (bmaurer@ximian.com)
//

#if NET_2_0
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using NUnit.Framework;

namespace MonoTests.System.Collections.Generic {
	[TestFixture]
	public class QueueTest
	{
		[Test]
		public void TestCtor ()
		{
			Queue <int> a = new Queue <int> ();
			Queue <int> b = new Queue <int> (1);
			Queue <object> c = new Queue <object> ();
			Queue <object> d = new Queue <object> (1);
			Queue <object> e = new Queue <object> (0);
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void TestCtorEx ()
		{
			Queue <int> a = new Queue <int> (-1);
		}
		
		[Test]
		public void TestCtorEnum ()
		{
			List <int> l = new List <int> ();
			l.Add (1);
			l.Add (2);
			l.Add (3);
			
			Queue <int> s = new Queue <int> (l);
			
			AssertDequeue (s, 1);
			AssertDequeue (s, 2);
			AssertDequeue (s, 3);
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void TestCtorEnumNull ()
		{
			Queue <int> s = new Queue <int> (null);
		}
		
		[Test]
		public void TestClear()
		{
			Queue <int> s = new Queue <int> ();
			s.Clear ();
			
			Assert.AreEqual (0, s.Count, "#1");
			
			s.Enqueue (1);
			s.Enqueue (2);
			
			Assert.AreEqual (2, s.Count, "#2");
			
			s.Clear ();
			
			Assert.AreEqual (0, s.Count, "#3");

			IEnumerator enumerator = s.GetEnumerator();
			s.Clear();

			try {
				enumerator.Reset();
				Assert.Fail ("#4");
			} catch(InvalidOperationException) {
			}
		}
		
		[Test]
		public void TestContains ()
		{
			Stack <int> s = new Stack <int> ();
			
			Assert.IsFalse (s.Contains (1), "#1");
			
			s.Push (1);
			
			Assert.IsTrue (s.Contains (1), "#2");
			Assert.IsFalse (s.Contains (0), "#3");
		}

		[Test]
		public void TestCopyTo ()
		{
			int [] x = new int [3];
			Queue <int> z = new Queue <int> ();
			z.Enqueue (1);
			z.Enqueue (2);
			x [0] = 10;
			z.CopyTo (x, 1);
			
			Assert.AreEqual (10, x [0], "#1");
			Assert.AreEqual (1, x [1], "#2");
			Assert.AreEqual (2, x [2], "#3");
			
			z = new Queue <int> ();
			x = new int [z.Count];
			z.CopyTo (x, 0);
		}

		[Test]
		public void TestICollectionCopyTo ()
		{
			var queue = new Queue<int> ();

			((ICollection) queue).CopyTo (new int [0], 0);

			queue.Enqueue (1);
			queue.Enqueue (2);

			var array = new int [queue.Count];

			((ICollection) queue).CopyTo (array, 0);

			Assert.AreEqual (1, array [0]);
			Assert.AreEqual (2, array [1]);

			array = new int [queue.Count + 1];
			array [0] = 42;

			((ICollection) queue).CopyTo (array, 1);

			Assert.AreEqual (42, array [0]);
			Assert.AreEqual (1, array [1]);
			Assert.AreEqual (2, array [2]);
		}

		[Test]
		public void TestPeek ()
		{
			Queue <int> s = new Queue <int> ();
			s.Enqueue (1);
			
			Assert.AreEqual (1, s.Peek (), "#1");
			Assert.AreEqual (1, s.Count, "#2");
		}
		
		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void TestPeekEx ()
		{
			Queue <int> s = new Queue <int> ();
			s.Peek ();
		}
		
		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void TestPeekEx2 ()
		{
			Queue <int> s = new Queue <int> ();
			s.Enqueue (1);
			s.Dequeue ();
			s.Peek ();
		}
		
		[Test]
		public void TestDequeue ()
		{
			Queue <int> s = new Queue <int> ();
			s.Enqueue (1);
			
			Assert.AreEqual (1, s.Dequeue (), "#1");
			Assert.AreEqual (0, s.Count, "#2");
		}
		
		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void TestDequeueEx ()
		{
			Queue <int> s = new Queue <int> ();
			s.Dequeue ();
		}
		
		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void TestDequeueEx2 ()
		{
			Queue <int> s = new Queue <int> ();
			s.Enqueue (1);
			s.Dequeue ();
			s.Dequeue ();
		}
		
		[Test]
		public void TestEnqueue ()
		{
			Queue <int> s = new Queue <int> ();
			s.Enqueue (1);
			Assert.AreEqual (1, s.Count, "#1");
			s.Enqueue (2);
			Assert.AreEqual (2, s.Count, "#2");
			
			for (int i = 0; i < 100; i ++)
				s.Enqueue (i);
			
			Assert.AreEqual (102, s.Count, "#3");
		}
		
		[Test]
		public void TestToArray ()
		{
			Queue <int> s = new Queue <int> ();
			
			int [] x = s.ToArray ();
			
			Assert.AreEqual (0, x.Length, "#1");
			
			s.Enqueue (1);
			x = s.ToArray ();
			Assert.AreEqual (1, x.Length, "#2");
			Assert.AreEqual (1, x [0], "#3");
		}

		[Test]
		public void TestEnumerator ()
		{
			Queue <int> s = new Queue <int> ();
			
			foreach (int x in s)
				Assert.Fail ("#1" + x);
			
			s.Enqueue (1);
			
			int i = 0;
			
			foreach (int x in s) {
				Assert.AreEqual  (0, i, "#2");
				Assert.AreEqual  (1, x, "#3");
				i ++;
			}
			
			for (i = 2; i < 100; i ++)
				s.Enqueue (i);
			
			i = 1;
			
			foreach (int x in s) {
				Assert.AreEqual (i, x, "#4");
				i ++;
			}
		}

		[Test]
		public void TrimExcessTest ()
		{
			Queue <int> s = new Queue <int> ();
			s.TrimExcess ();
			Assert.AreEqual (0, s.Count, "#1");

			s.Enqueue (1);
			s.Enqueue (3);
			Assert.AreEqual (1, s.Dequeue (), "#2");
			Assert.AreEqual (3, s.Peek (), "#3");

			s.TrimExcess ();
			Assert.AreEqual (1, s.Count, "#4");
			Assert.AreEqual (3, s.Peek (), "#5");

			s.Enqueue (2);
			Assert.AreEqual (3, s.Dequeue (), "#6");
			Assert.AreEqual (2, s.Dequeue (), "#7");

			s.TrimExcess ();
			Assert.AreEqual (0, s.Count, "#8");
		}

		[Test]
		public void TrimExcessDequeueEnqueue ()
		{
			var queue = new Queue<int> ();
			queue.Enqueue (1);
			queue.Enqueue (2);
			queue.Enqueue (3);

			queue.TrimExcess ();

			Assert.AreEqual (1, queue.Dequeue ());

			queue.Enqueue (4);

			Assert.AreEqual (2, queue.Dequeue ());
			Assert.AreEqual (3, queue.Dequeue ());
			Assert.AreEqual (4, queue.Dequeue ());

			Assert.AreEqual (0, queue.Count);
		}

		[Test]
		[Category ("NotWorking")] // bug #80649
		public void SerializeTest ()
		{
			Queue <int> s = new Queue <int> ();
			s.Enqueue (1);
			s.Enqueue (3);
			s.Enqueue (2);
			s.Dequeue ();

			BinaryFormatter bf = new BinaryFormatter ();
			MemoryStream ms = new MemoryStream ();
			bf.Serialize (ms, s);

			byte [] buffer = new byte [ms.Length];
			ms.Position = 0;
			ms.Read (buffer, 0, buffer.Length);

			Assert.AreEqual (_serializedQueue, buffer);
		}

		[Test]
#if TARGET_JVM
		[Category ("NotWorking")]
#endif
		public void DeserializeTest ()
		{
			MemoryStream ms = new MemoryStream ();
			ms.Write (_serializedQueue, 0, _serializedQueue.Length);
			ms.Position = 0;

			BinaryFormatter bf = new BinaryFormatter ();
			Queue<int> s = (Queue<int>) bf.Deserialize (ms);
			Assert.AreEqual (2, s.Count, "#1");
			Assert.AreEqual (3, s.Dequeue (), "#2");
			Assert.AreEqual (2, s.Dequeue (), "#3");
		}

		void AssertDequeue <T> (Queue <T> s, T t)
		{
			Assert.AreEqual (t, s.Dequeue ());
		}

		static byte [] _serializedQueue = new byte [] {
			0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
			0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
			0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
			0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
			0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
			0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
			0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
			0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
			0x00, 0x00, 0x7f, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43,
			0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
			0x47, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x51, 0x75, 0x65,
			0x75, 0x65, 0x60, 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65,
			0x6d, 0x2e, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73,
			0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72,
			0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
			0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d,
			0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75,
			0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65,
			0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31,
			0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x05, 0x00,
			0x00, 0x00, 0x06, 0x5f, 0x61, 0x72, 0x72, 0x61, 0x79, 0x05, 0x5f,
			0x68, 0x65, 0x61, 0x64, 0x05, 0x5f, 0x74, 0x61, 0x69, 0x6c, 0x05,
			0x5f, 0x73, 0x69, 0x7a, 0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73,
			0x69, 0x6f, 0x6e, 0x07, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08,
			0x08, 0x08, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00,
			0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
			0x00, 0x05, 0x00, 0x00, 0x00, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x04,
			0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
			0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b };
	}
}
#endif