1
|
/*
|
2
|
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
|
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
of this software and associated documentation files (the "Software"), to deal
|
5
|
in the Software without restriction, including without limitation the rights
|
6
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
copies of the Software, and to permit persons to whom the Software is
|
8
|
furnished to do so, subject to the following conditions:
|
9
|
|
10
|
The above copyright notice and this permission notice shall be included in
|
11
|
all copies or substantial portions of the Software.
|
12
|
|
13
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
SOFTWARE.
|
20
|
*/
|
21
|
|
22
|
using System;
|
23
|
using C5;
|
24
|
using NUnit.Framework;
|
25
|
using SCG = System.Collections.Generic;
|
26
|
|
27
|
namespace C5UnitTests.heaps
|
28
|
{
|
29
|
using CollectionOfInt = IntervalHeap<int>;
|
30
|
|
31
|
[TestFixture]
|
32
|
public class GenericTesters
|
33
|
{
|
34
|
[Test]
|
35
|
public void TestEvents()
|
36
|
{
|
37
|
Fun<CollectionOfInt> factory = delegate() { return new CollectionOfInt(TenEqualityComparer.Default); };
|
38
|
new C5UnitTests.Templates.Events.PriorityQueueTester<CollectionOfInt>().Test(factory);
|
39
|
}
|
40
|
|
41
|
[Test]
|
42
|
public void Extensible()
|
43
|
{
|
44
|
C5UnitTests.Templates.Extensible.Clone.Tester<CollectionOfInt>();
|
45
|
C5UnitTests.Templates.Extensible.Serialization.Tester<CollectionOfInt>();
|
46
|
}
|
47
|
}
|
48
|
|
49
|
[TestFixture]
|
50
|
public class Events
|
51
|
{
|
52
|
IPriorityQueue<int> queue;
|
53
|
ArrayList<KeyValuePair<Acts, int>> events;
|
54
|
|
55
|
|
56
|
[SetUp]
|
57
|
public void Init()
|
58
|
{
|
59
|
queue = new IntervalHeap<int>();
|
60
|
events = new ArrayList<KeyValuePair<Acts, int>>();
|
61
|
}
|
62
|
|
63
|
|
64
|
[TearDown]
|
65
|
public void Dispose() { queue = null; events = null; }
|
66
|
|
67
|
[Test]
|
68
|
public void Listenable()
|
69
|
{
|
70
|
Assert.AreEqual(EventTypeEnum.Basic, queue.ListenableEvents);
|
71
|
}
|
72
|
|
73
|
enum Acts
|
74
|
{
|
75
|
Add, Remove, Changed
|
76
|
}
|
77
|
|
78
|
[Test]
|
79
|
public void Direct()
|
80
|
{
|
81
|
CollectionChangedHandler<int> cch;
|
82
|
ItemsAddedHandler<int> iah;
|
83
|
ItemsRemovedHandler<int> irh;
|
84
|
Assert.AreEqual(EventTypeEnum.None, queue.ActiveEvents);
|
85
|
queue.CollectionChanged += (cch = new CollectionChangedHandler<int>(queue_CollectionChanged));
|
86
|
Assert.AreEqual(EventTypeEnum.Changed, queue.ActiveEvents);
|
87
|
queue.ItemsAdded += (iah = new ItemsAddedHandler<int>(queue_ItemAdded));
|
88
|
Assert.AreEqual(EventTypeEnum.Changed | EventTypeEnum.Added, queue.ActiveEvents);
|
89
|
queue.ItemsRemoved += (irh = new ItemsRemovedHandler<int>(queue_ItemRemoved));
|
90
|
Assert.AreEqual(EventTypeEnum.Changed | EventTypeEnum.Added | EventTypeEnum.Removed, queue.ActiveEvents);
|
91
|
queue.Add(34);
|
92
|
queue.Add(56);
|
93
|
queue.AddAll<int>(new int[] { });
|
94
|
queue.Add(34);
|
95
|
queue.Add(12);
|
96
|
queue.DeleteMax();
|
97
|
queue.DeleteMin();
|
98
|
queue.AddAll<int>(new int[] { 4, 5, 6, 2 });
|
99
|
Assert.AreEqual(17, events.Count);
|
100
|
int[] vals = { 34, 0, 56, 0, 34, 0, 12, 0, 56, 0, 12, 0, 4, 5, 6, 2, 0 };
|
101
|
Acts[] acts = { Acts.Add, Acts.Changed, Acts.Add, Acts.Changed, Acts.Add, Acts.Changed, Acts.Add, Acts.Changed,
|
102
|
Acts.Remove, Acts.Changed, Acts.Remove, Acts.Changed, Acts.Add, Acts.Add, Acts.Add, Acts.Add, Acts.Changed };
|
103
|
for (int i = 0; i < vals.Length; i++)
|
104
|
{
|
105
|
//Console.WriteLine("{0}", events[cell]);
|
106
|
Assert.AreEqual(acts[i], events[i].Key, "Action " + i);
|
107
|
Assert.AreEqual(vals[i], events[i].Value, "Value " + i);
|
108
|
}
|
109
|
queue.CollectionChanged -= cch;
|
110
|
Assert.AreEqual(EventTypeEnum.Added | EventTypeEnum.Removed, queue.ActiveEvents);
|
111
|
queue.ItemsAdded -= iah;
|
112
|
Assert.AreEqual(EventTypeEnum.Removed, queue.ActiveEvents);
|
113
|
queue.ItemsRemoved -= irh;
|
114
|
Assert.AreEqual(EventTypeEnum.None, queue.ActiveEvents);
|
115
|
}
|
116
|
|
117
|
[Test]
|
118
|
public void Guarded()
|
119
|
{
|
120
|
ICollectionValue<int> guarded = new GuardedCollectionValue<int>(queue);
|
121
|
guarded.CollectionChanged += new CollectionChangedHandler<int>(queue_CollectionChanged);
|
122
|
guarded.ItemsAdded += new ItemsAddedHandler<int>(queue_ItemAdded);
|
123
|
guarded.ItemsRemoved += new ItemsRemovedHandler<int>(queue_ItemRemoved);
|
124
|
queue.Add(34);
|
125
|
queue.Add(56);
|
126
|
queue.Add(34);
|
127
|
queue.Add(12);
|
128
|
queue.DeleteMax();
|
129
|
queue.DeleteMin();
|
130
|
queue.AddAll<int>(new int[] { 4, 5, 6, 2 });
|
131
|
Assert.AreEqual(17, events.Count);
|
132
|
int[] vals = { 34, 0, 56, 0, 34, 0, 12, 0, 56, 0, 12, 0, 4, 5, 6, 2, 0 };
|
133
|
Acts[] acts = { Acts.Add, Acts.Changed, Acts.Add, Acts.Changed, Acts.Add, Acts.Changed, Acts.Add, Acts.Changed,
|
134
|
Acts.Remove, Acts.Changed, Acts.Remove, Acts.Changed, Acts.Add, Acts.Add, Acts.Add, Acts.Add, Acts.Changed };
|
135
|
for (int i = 0; i < vals.Length; i++)
|
136
|
{
|
137
|
//Console.WriteLine("{0}", events[cell]);
|
138
|
Assert.AreEqual(vals[i], events[i].Value);
|
139
|
Assert.AreEqual(acts[i], events[i].Key);
|
140
|
}
|
141
|
}
|
142
|
|
143
|
|
144
|
void queue_CollectionChanged(object sender)
|
145
|
{
|
146
|
events.Add(new KeyValuePair<Acts, int>(Acts.Changed, 0));
|
147
|
}
|
148
|
void queue_ItemAdded(object sender, ItemCountEventArgs<int> e)
|
149
|
{
|
150
|
events.Add(new KeyValuePair<Acts, int>(Acts.Add, e.Item));
|
151
|
}
|
152
|
void queue_ItemRemoved(object sender, ItemCountEventArgs<int> e)
|
153
|
{
|
154
|
events.Add(new KeyValuePair<Acts, int>(Acts.Remove, e.Item));
|
155
|
}
|
156
|
}
|
157
|
|
158
|
[TestFixture]
|
159
|
public class Formatting
|
160
|
{
|
161
|
IntervalHeap<int> coll;
|
162
|
IFormatProvider rad16;
|
163
|
[SetUp]
|
164
|
public void Init() { coll = new IntervalHeap<int>(); rad16 = new RadixFormatProvider(16); }
|
165
|
[TearDown]
|
166
|
public void Dispose() { coll = null; rad16 = null; }
|
167
|
[Test]
|
168
|
public void Format()
|
169
|
{
|
170
|
Assert.AreEqual("{ }", coll.ToString());
|
171
|
coll.AddAll<int>(new int[] { -4, 28, 129, 65530 });
|
172
|
Assert.AreEqual("{ -4, 65530, 28, 129 }", coll.ToString());
|
173
|
Assert.AreEqual("{ -4, FFFA, 1C, 81 }", coll.ToString(null, rad16));
|
174
|
Assert.AreEqual("{ -4, 65530, ... }", coll.ToString("L14", null));
|
175
|
Assert.AreEqual("{ -4, FFFA, ... }", coll.ToString("L14", rad16));
|
176
|
}
|
177
|
}
|
178
|
|
179
|
|
180
|
[TestFixture]
|
181
|
public class IntervalHeapTests
|
182
|
{
|
183
|
IPriorityQueue<int> queue;
|
184
|
|
185
|
|
186
|
[SetUp]
|
187
|
public void Init() { queue = new IntervalHeap<int>(); }
|
188
|
|
189
|
|
190
|
[TearDown]
|
191
|
public void Dispose() { queue = null; }
|
192
|
|
193
|
[Test]
|
194
|
[ExpectedException(typeof(NullReferenceException))]
|
195
|
public void NullEqualityComparerinConstructor1()
|
196
|
{
|
197
|
new IntervalHeap<int>(null);
|
198
|
}
|
199
|
|
200
|
[Test]
|
201
|
[ExpectedException(typeof(NullReferenceException))]
|
202
|
public void NullEqualityComparerinConstructor2()
|
203
|
{
|
204
|
new IntervalHeap<int>(5, null);
|
205
|
}
|
206
|
|
207
|
[Test]
|
208
|
public void Handles()
|
209
|
{
|
210
|
IPriorityQueueHandle<int>[] handles = new IPriorityQueueHandle<int>[10];
|
211
|
|
212
|
queue.Add(ref handles[0], 7);
|
213
|
Assert.IsTrue(queue.Check());
|
214
|
queue.Add(ref handles[1], 72);
|
215
|
Assert.IsTrue(queue.Check());
|
216
|
queue.Add(ref handles[2], 27);
|
217
|
Assert.IsTrue(queue.Check());
|
218
|
queue.Add(ref handles[3], 17);
|
219
|
Assert.IsTrue(queue.Check());
|
220
|
queue.Add(ref handles[4], 70);
|
221
|
Assert.IsTrue(queue.Check());
|
222
|
queue.Add(ref handles[5], 1);
|
223
|
Assert.IsTrue(queue.Check());
|
224
|
queue.Add(ref handles[6], 2);
|
225
|
Assert.IsTrue(queue.Check());
|
226
|
queue.Add(ref handles[7], 7);
|
227
|
Assert.IsTrue(queue.Check());
|
228
|
queue.Add(ref handles[8], 8);
|
229
|
Assert.IsTrue(queue.Check());
|
230
|
queue.Add(ref handles[9], 9);
|
231
|
Assert.IsTrue(queue.Check());
|
232
|
queue.Delete(handles[2]);
|
233
|
Assert.IsTrue(queue.Check());
|
234
|
queue.Delete(handles[0]);
|
235
|
Assert.IsTrue(queue.Check());
|
236
|
queue.Delete(handles[8]);
|
237
|
Assert.IsTrue(queue.Check());
|
238
|
queue.Delete(handles[4]);
|
239
|
Assert.IsTrue(queue.Check());
|
240
|
queue.Delete(handles[6]);
|
241
|
Assert.IsTrue(queue.Check());
|
242
|
Assert.AreEqual(5, queue.Count);
|
243
|
}
|
244
|
|
245
|
[Test]
|
246
|
public void Replace()
|
247
|
{
|
248
|
IPriorityQueueHandle<int> handle = null;
|
249
|
queue.Add(6);
|
250
|
queue.Add(10);
|
251
|
queue.Add(ref handle, 7);
|
252
|
queue.Add(21);
|
253
|
Assert.AreEqual(7, queue.Replace(handle, 12));
|
254
|
Assert.AreEqual(21, queue.FindMax());
|
255
|
Assert.AreEqual(12, queue.Replace(handle, 34));
|
256
|
Assert.AreEqual(34, queue.FindMax());
|
257
|
Assert.IsTrue(queue.Check());
|
258
|
//replace max
|
259
|
Assert.AreEqual(34, queue.Replace(handle, 60));
|
260
|
Assert.AreEqual(60, queue.FindMax());
|
261
|
Assert.AreEqual(60, queue.Replace(handle, queue[handle] + 80));
|
262
|
Assert.AreEqual(140, queue.FindMax());
|
263
|
Assert.IsTrue(queue.Check());
|
264
|
}
|
265
|
|
266
|
[Test]
|
267
|
public void Replace2()
|
268
|
{
|
269
|
IPriorityQueueHandle<int> handle = null;
|
270
|
queue.Add(6);
|
271
|
queue.Add(10);
|
272
|
queue.Add(ref handle, 7);
|
273
|
//Replace last item in queue with something large
|
274
|
Assert.AreEqual(7, queue.Replace(handle, 12));
|
275
|
Assert.IsTrue(queue.Check());
|
276
|
}
|
277
|
|
278
|
/// <summary>
|
279
|
/// Bug by Viet Yen Nguyen <v.y.nguyen@alumnus.utwente.nl>
|
280
|
/// </summary>
|
281
|
[Test]
|
282
|
public void Replace3()
|
283
|
{
|
284
|
IPriorityQueueHandle<int> handle = null;
|
285
|
queue.Add(ref handle, 10);
|
286
|
Assert.AreEqual(10, queue.Replace(handle, 12));
|
287
|
Assert.IsTrue(queue.Check());
|
288
|
}
|
289
|
|
290
|
[Test]
|
291
|
public void ReuseHandle()
|
292
|
{
|
293
|
IPriorityQueueHandle<int> handle = null;
|
294
|
queue.Add(ref handle, 7);
|
295
|
queue.Delete(handle);
|
296
|
queue.Add(ref handle, 8);
|
297
|
}
|
298
|
|
299
|
[Test]
|
300
|
[ExpectedException(typeof(InvalidPriorityQueueHandleException))]
|
301
|
public void ErrorAddValidHandle()
|
302
|
{
|
303
|
IPriorityQueueHandle<int> handle = null;
|
304
|
queue.Add(ref handle, 7);
|
305
|
queue.Add(ref handle, 8);
|
306
|
}
|
307
|
|
308
|
[Test]
|
309
|
[ExpectedException(typeof(InvalidPriorityQueueHandleException))]
|
310
|
public void ErrorDeleteInvalidHandle()
|
311
|
{
|
312
|
IPriorityQueueHandle<int> handle = null;
|
313
|
queue.Add(ref handle, 7);
|
314
|
queue.Delete(handle);
|
315
|
queue.Delete(handle);
|
316
|
}
|
317
|
|
318
|
[Test]
|
319
|
[ExpectedException(typeof(InvalidPriorityQueueHandleException))]
|
320
|
public void ErrorReplaceInvalidHandle()
|
321
|
{
|
322
|
IPriorityQueueHandle<int> handle = null;
|
323
|
queue.Add(ref handle, 7);
|
324
|
queue.Delete(handle);
|
325
|
queue.Replace(handle, 13);
|
326
|
}
|
327
|
|
328
|
[Test]
|
329
|
public void Simple()
|
330
|
{
|
331
|
Assert.IsTrue(queue.AllowsDuplicates);
|
332
|
Assert.AreEqual(0, queue.Count);
|
333
|
queue.Add(8); queue.Add(18); queue.Add(8); queue.Add(3);
|
334
|
Assert.AreEqual(4, queue.Count);
|
335
|
Assert.AreEqual(18, queue.DeleteMax());
|
336
|
Assert.AreEqual(3, queue.Count);
|
337
|
Assert.AreEqual(3, queue.DeleteMin());
|
338
|
Assert.AreEqual(2, queue.Count);
|
339
|
Assert.AreEqual(8, queue.FindMax());
|
340
|
Assert.AreEqual(8, queue.DeleteMax());
|
341
|
Assert.AreEqual(8, queue.FindMax());
|
342
|
queue.Add(15);
|
343
|
Assert.AreEqual(15, queue.FindMax());
|
344
|
Assert.AreEqual(8, queue.FindMin());
|
345
|
Assert.IsTrue(queue.Comparer.Compare(2, 3) < 0);
|
346
|
Assert.IsTrue(queue.Comparer.Compare(4, 3) > 0);
|
347
|
Assert.IsTrue(queue.Comparer.Compare(3, 3) == 0);
|
348
|
|
349
|
}
|
350
|
|
351
|
|
352
|
[Test]
|
353
|
public void Enumerate()
|
354
|
{
|
355
|
int[] a = new int[4];
|
356
|
int siz = 0;
|
357
|
foreach (int i in queue)
|
358
|
siz++;
|
359
|
Assert.AreEqual(0, siz);
|
360
|
|
361
|
queue.Add(8); queue.Add(18); queue.Add(8); queue.Add(3);
|
362
|
|
363
|
foreach (int i in queue)
|
364
|
a[siz++] = i;
|
365
|
Assert.AreEqual(4, siz);
|
366
|
Array.Sort(a, 0, siz);
|
367
|
Assert.AreEqual(3, a[0]);
|
368
|
Assert.AreEqual(8, a[1]);
|
369
|
Assert.AreEqual(8, a[2]);
|
370
|
Assert.AreEqual(18, a[3]);
|
371
|
|
372
|
siz = 0;
|
373
|
Assert.AreEqual(18, queue.DeleteMax());
|
374
|
foreach (int i in queue)
|
375
|
a[siz++] = i;
|
376
|
Assert.AreEqual(3, siz);
|
377
|
Array.Sort(a, 0, siz);
|
378
|
Assert.AreEqual(3, a[0]);
|
379
|
Assert.AreEqual(8, a[1]);
|
380
|
Assert.AreEqual(8, a[2]);
|
381
|
|
382
|
siz = 0;
|
383
|
Assert.AreEqual(8, queue.DeleteMax());
|
384
|
foreach (int i in queue)
|
385
|
a[siz++] = i;
|
386
|
Assert.AreEqual(2, siz);
|
387
|
Array.Sort(a, 0, siz);
|
388
|
Assert.AreEqual(3, a[0]);
|
389
|
Assert.AreEqual(8, a[1]);
|
390
|
|
391
|
siz = 0;
|
392
|
Assert.AreEqual(8, queue.DeleteMax());
|
393
|
foreach (int i in queue)
|
394
|
a[siz++] = i;
|
395
|
Assert.AreEqual(1, siz);
|
396
|
Assert.AreEqual(3, a[0]);
|
397
|
}
|
398
|
|
399
|
[Test]
|
400
|
public void Random()
|
401
|
{
|
402
|
int length = 1000;
|
403
|
int[] a = new int[length];
|
404
|
Random ran = new Random(6754);
|
405
|
|
406
|
for (int i = 0; i < length; i++)
|
407
|
queue.Add(a[i] = ran.Next());
|
408
|
|
409
|
Assert.IsTrue(queue.Check());
|
410
|
Array.Sort(a);
|
411
|
for (int i = 0; i < length / 2; i++)
|
412
|
{
|
413
|
Assert.AreEqual(a[length - i - 1], queue.DeleteMax());
|
414
|
Assert.IsTrue(queue.Check());
|
415
|
Assert.AreEqual(a[i], queue.DeleteMin());
|
416
|
Assert.IsTrue(queue.Check());
|
417
|
}
|
418
|
|
419
|
Assert.IsTrue(queue.IsEmpty);
|
420
|
}
|
421
|
|
422
|
[Test]
|
423
|
public void RandomWithHandles()
|
424
|
{
|
425
|
int length = 1000;
|
426
|
int[] a = new int[length];
|
427
|
Random ran = new Random(6754);
|
428
|
|
429
|
for (int i = 0; i < length; i++)
|
430
|
{
|
431
|
IPriorityQueueHandle<int> h = null;
|
432
|
queue.Add(ref h, a[i] = ran.Next());
|
433
|
Assert.IsTrue(queue.Check());
|
434
|
}
|
435
|
|
436
|
Assert.IsTrue(queue.Check());
|
437
|
Array.Sort(a);
|
438
|
for (int i = 0; i < length / 2; i++)
|
439
|
{
|
440
|
Assert.AreEqual(a[length - i - 1], queue.DeleteMax());
|
441
|
Assert.IsTrue(queue.Check());
|
442
|
Assert.AreEqual(a[i], queue.DeleteMin());
|
443
|
Assert.IsTrue(queue.Check());
|
444
|
}
|
445
|
|
446
|
Assert.IsTrue(queue.IsEmpty);
|
447
|
}
|
448
|
|
449
|
[Test]
|
450
|
public void RandomWithDeleteHandles()
|
451
|
{
|
452
|
Random ran = new Random(6754);
|
453
|
int length = 1000;
|
454
|
int[] a = new int[length];
|
455
|
ArrayList<int> shuffle = new ArrayList<int>(length);
|
456
|
IPriorityQueueHandle<int>[] h = new IPriorityQueueHandle<int>[length];
|
457
|
|
458
|
for (int i = 0; i < length; i++)
|
459
|
{
|
460
|
shuffle.Add(i);
|
461
|
queue.Add(ref h[i], a[i] = ran.Next());
|
462
|
Assert.IsTrue(queue.Check());
|
463
|
}
|
464
|
|
465
|
Assert.IsTrue(queue.Check());
|
466
|
shuffle.Shuffle(ran);
|
467
|
for (int i = 0; i < length; i++)
|
468
|
{
|
469
|
int j = shuffle[i];
|
470
|
Assert.AreEqual(a[j], queue.Delete(h[j]));
|
471
|
Assert.IsTrue(queue.Check());
|
472
|
}
|
473
|
|
474
|
Assert.IsTrue(queue.IsEmpty);
|
475
|
}
|
476
|
|
477
|
[Test]
|
478
|
public void RandomIndexing()
|
479
|
{
|
480
|
Random ran = new Random(6754);
|
481
|
int length = 1000;
|
482
|
int[] a = new int[length];
|
483
|
int[] b = new int[length];
|
484
|
ArrayList<int> shuffle = new ArrayList<int>(length);
|
485
|
IPriorityQueueHandle<int>[] h = new IPriorityQueueHandle<int>[length];
|
486
|
|
487
|
for (int i = 0; i < length; i++)
|
488
|
{
|
489
|
shuffle.Add(i);
|
490
|
queue.Add(ref h[i], a[i] = ran.Next());
|
491
|
b[i] = ran.Next();
|
492
|
Assert.IsTrue(queue.Check());
|
493
|
}
|
494
|
|
495
|
Assert.IsTrue(queue.Check());
|
496
|
shuffle.Shuffle(ran);
|
497
|
for (int i = 0; i < length; i++)
|
498
|
{
|
499
|
int j = shuffle[i];
|
500
|
Assert.AreEqual(a[j], queue[h[j]]);
|
501
|
queue[h[j]] = b[j];
|
502
|
Assert.AreEqual(b[j], queue[h[j]]);
|
503
|
Assert.IsTrue(queue.Check());
|
504
|
}
|
505
|
}
|
506
|
|
507
|
|
508
|
|
509
|
[Test]
|
510
|
public void RandomDuplicates()
|
511
|
{
|
512
|
int length = 1000;
|
513
|
int s;
|
514
|
int[] a = new int[length];
|
515
|
Random ran = new Random(6754);
|
516
|
|
517
|
for (int i = 0; i < length; i++)
|
518
|
queue.Add(a[i] = ran.Next(3, 13));
|
519
|
Assert.IsTrue(queue.Check());
|
520
|
|
521
|
Array.Sort(a);
|
522
|
|
523
|
for (int i = 0; i < length / 2; i++)
|
524
|
{
|
525
|
Assert.AreEqual(a[i], queue.DeleteMin());
|
526
|
Assert.IsTrue(queue.Check());
|
527
|
Assert.AreEqual(a[length - i - 1], s = queue.DeleteMax());
|
528
|
Assert.IsTrue(queue.Check());
|
529
|
}
|
530
|
|
531
|
Assert.IsTrue(queue.IsEmpty);
|
532
|
}
|
533
|
|
534
|
|
535
|
[Test]
|
536
|
public void AddAll()
|
537
|
{
|
538
|
int length = 1000;
|
539
|
int[] a = new int[length];
|
540
|
Random ran = new Random(6754);
|
541
|
|
542
|
LinkedList<int> lst = new LinkedList<int>();
|
543
|
for (int i = 0; i < length; i++)
|
544
|
lst.Add(a[i] = ran.Next());
|
545
|
|
546
|
queue.AddAll(lst);
|
547
|
Assert.IsTrue(queue.Check());
|
548
|
Array.Sort(a);
|
549
|
for (int i = 0; i < length / 2; i++)
|
550
|
{
|
551
|
Assert.AreEqual(a[length - i - 1], queue.DeleteMax());
|
552
|
Assert.IsTrue(queue.Check());
|
553
|
Assert.AreEqual(a[i], queue.DeleteMin());
|
554
|
Assert.IsTrue(queue.Check());
|
555
|
}
|
556
|
|
557
|
Assert.IsTrue(queue.IsEmpty);
|
558
|
}
|
559
|
|
560
|
}
|
561
|
|
562
|
|
563
|
}
|