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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.Collections.Tests
{
public static class QueueTests
{
[Fact]
public static void Ctor_Empty()
{
const int DefaultCapactiy = 32;
var queue = new Queue();
Assert.Equal(0, queue.Count);
for (int i = 0; i <= DefaultCapactiy; i++)
{
queue.Enqueue(i);
}
Assert.Equal(DefaultCapactiy + 1, queue.Count);
}
[Theory]
[InlineData(1)]
[InlineData(32)]
[InlineData(77)]
public static void Ctor_Int(int capacity)
{
var queue = new Queue(capacity);
for (int i = 0; i <= capacity; i++)
{
queue.Enqueue(i);
}
Assert.Equal(capacity + 1, queue.Count);
}
[Fact]
public static void Ctor_Int_NegativeCapacity_ThrowsArgumentOutOfRangeException()
{
Assert.Throws<ArgumentOutOfRangeException>("capacity", () => new Queue(-1)); // Capacity < 0
}
[Theory]
[InlineData(1, 2.0)]
[InlineData(32, 1.0)]
[InlineData(77, 5.0)]
public static void Ctor_Int_Int(int capacity, float growFactor)
{
var queue = new Queue(capacity, growFactor);
for (int i = 0; i <= capacity; i++)
{
queue.Enqueue(i);
}
Assert.Equal(capacity + 1, queue.Count);
}
[Fact]
public static void Ctor_Int_Int_Invalid()
{
Assert.Throws<ArgumentOutOfRangeException>("capacity", () => new Queue(-1, 1)); // Capacity < 0
Assert.Throws<ArgumentOutOfRangeException>("growFactor", () => new Queue(1, (float)0.99)); // Grow factor < 1
Assert.Throws<ArgumentOutOfRangeException>("growFactor", () => new Queue(1, (float)10.01)); // Grow factor > 10
}
[Fact]
public static void Ctor_ICollection()
{
ArrayList arrList = Helpers.CreateIntArrayList(100);
var queue = new Queue(arrList);
Assert.Equal(arrList.Count, queue.Count);
for (int i = 0; i < queue.Count; i++)
{
Assert.Equal(i, queue.Dequeue());
}
}
[Fact]
public static void Ctor_ICollection_NullCollection_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>("col", () => new Queue(null)); // Collection is null
}
[Fact]
public static void DebuggerAttribute()
{
DebuggerAttributes.ValidateDebuggerDisplayReferences(new Queue());
var testQueue = new Queue();
testQueue.Enqueue("a");
testQueue.Enqueue(1);
testQueue.Enqueue("b");
testQueue.Enqueue(2);
DebuggerAttributes.ValidateDebuggerTypeProxyProperties(testQueue);
bool threwNull = false;
try
{
DebuggerAttributes.ValidateDebuggerTypeProxyProperties(typeof(Queue), null);
}
catch (TargetInvocationException ex)
{
threwNull = ex.InnerException is ArgumentNullException;
}
Assert.True(threwNull);
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(32)]
public static void Clear(int capacity)
{
var queue1 = new Queue(capacity);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
queue2.Enqueue(1);
queue2.Clear();
Assert.Equal(0, queue2.Count);
});
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(32)]
public static void Clear_Empty(int capacity)
{
var queue1 = new Queue(capacity);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
queue2.Clear();
Assert.Equal(0, queue2.Count);
queue2.Clear();
Assert.Equal(0, queue2.Count);
});
}
[Fact]
public static void Clone()
{
Queue queue1 = Helpers.CreateIntQueue(100);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
Queue clone = (Queue)queue2.Clone();
Assert.Equal(queue2.IsSynchronized, clone.IsSynchronized);
Assert.Equal(queue2.Count, clone.Count);
for (int i = 0; i < queue2.Count; i++)
{
Assert.True(clone.Contains(i));
}
});
}
[Fact]
public static void Clone_IsShallowCopy()
{
var queue1 = new Queue();
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
queue2.Enqueue(new Foo(10));
Queue clone = (Queue)queue2.Clone();
var foo = (Foo)queue2.Dequeue();
foo.IntValue = 50;
var fooClone = (Foo)clone.Dequeue();
Assert.Equal(50, fooClone.IntValue);
});
}
[Fact]
public static void Clone_Empty()
{
var queue1 = new Queue();
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
Queue clone = (Queue)queue2.Clone();
Assert.Equal(0, clone.Count);
// Can change the clone queue
clone.Enqueue(500);
Assert.Equal(500, clone.Dequeue());
});
}
[Fact]
public static void Clone_Clear()
{
Queue queue1 = Helpers.CreateIntQueue(100);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
queue2.Clear();
Queue clone = (Queue)queue2.Clone();
Assert.Equal(0, clone.Count);
// Can change clone queue
clone.Enqueue(500);
Assert.Equal(500, clone.Dequeue());
});
}
[Fact]
public static void Clone_DequeueUntilEmpty()
{
Queue queue1 = Helpers.CreateIntQueue(100);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
for (int i = 0; i < 100; i++)
{
queue2.Dequeue();
}
Queue clone = (Queue)queue2.Clone();
Assert.Equal(0, queue2.Count);
// Can change clone the queue
clone.Enqueue(500);
Assert.Equal(500, clone.Dequeue());
});
}
[Fact]
public static void Clone_DequeueThenEnqueue()
{
var queue1 = new Queue(100);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
// Insert 50 items in the Queue
for (int i = 0; i < 50; i++)
{
queue2.Enqueue(i);
}
// Insert and Remove 75 items in the Queue. This should wrap the queue
// where there is 25 at the end of the array and 25 at the beginning
for (int i = 0; i < 75; i++)
{
queue2.Enqueue(i + 50);
queue2.Dequeue();
}
Queue queClone = (Queue)queue2.Clone();
Assert.Equal(50, queClone.Count);
Assert.Equal(75, queClone.Dequeue());
// Add an item to the Queue
queClone.Enqueue(100);
Assert.Equal(50, queClone.Count);
Assert.Equal(76, queClone.Dequeue());
});
}
[Fact]
public static void Contains()
{
Queue queue1 = Helpers.CreateIntQueue(100);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
for (int i = 0; i < queue2.Count; i++)
{
Assert.True(queue2.Contains(i));
}
queue2.Enqueue(null);
Assert.True(queue2.Contains(null));
});
}
[Fact]
public static void Contains_NonExistentObject()
{
Queue queue1 = Helpers.CreateIntQueue(100);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
Assert.False(queue2.Contains(101));
Assert.False(queue2.Contains("hello world"));
Assert.False(queue2.Contains(null));
queue2.Enqueue(null);
Assert.False(queue2.Contains(-1)); // We have a null item in the list, so the algorithm may use a different branch
});
}
[Fact]
public static void Contains_EmptyQueue()
{
var queue1 = new Queue();
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
Assert.False(queue2.Contains(101));
Assert.False(queue2.Contains("hello world"));
Assert.False(queue2.Contains(null));
});
}
[Theory]
[InlineData(0, 0)]
[InlineData(1, 0)]
[InlineData(10, 0)]
[InlineData(100, 0)]
[InlineData(1000, 0)]
[InlineData(1, 50)]
[InlineData(10, 50)]
[InlineData(100, 50)]
[InlineData(1000, 50)]
public static void CopyTo(int count, int index)
{
Queue queue1 = Helpers.CreateIntQueue(count);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
var array = new object[count + index];
queue2.CopyTo(array, index);
Assert.Equal(count + index, array.Length);
for (int i = index; i < index + count; i++)
{
Assert.Equal(queue2.Dequeue(), array[i]);
}
});
}
[Fact]
public static void CopyTo_DequeueThenEnqueue()
{
var queue1 = new Queue(100);
// Insert 50 items in the Queue
for (int i = 0; i < 50; i++)
{
queue1.Enqueue(i);
}
// Insert and Remove 75 items in the Queue. This should wrap the queue
// where there is 25 at the end of the array and 25 at the beginning
for (int i = 0; i < 75; i++)
{
queue1.Enqueue(i + 50);
queue1.Dequeue();
}
var array = new object[queue1.Count];
queue1.CopyTo(array, 0);
Assert.Equal(queue1.Count, array.Length);
for (int i = 0; i < queue1.Count; i++)
{
Assert.Equal(queue1.Dequeue(), array[i]);
}
}
[Fact]
public static void CopyTo_Invalid()
{
Queue queue1 = Helpers.CreateIntQueue(100);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
Assert.Throws<ArgumentNullException>("array", () => queue2.CopyTo(null, 0)); // Array is null
Assert.Throws<ArgumentException>(() => queue2.CopyTo(new object[150, 150], 0)); // Array is multidimensional
Assert.Throws<ArgumentOutOfRangeException>("index", () => queue2.CopyTo(new object[150], -1)); // Index < 0
Assert.Throws<ArgumentException>(null, () => queue2.CopyTo(new object[150], 51)); // Index + queue.Count > array.Length
});
}
[Theory]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
public static void Dequeue(int count)
{
Queue queue1 = Helpers.CreateIntQueue(count);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
for (int i = 1; i <= count; i++)
{
int obj = (int)queue2.Dequeue();
Assert.Equal(i - 1, obj);
Assert.Equal(count - i, queue2.Count);
}
});
}
[Fact]
public static void Dequeue_EmptyQueue_ThrowsInvalidOperationException()
{
var queue1 = new Queue();
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
Assert.Throws<InvalidOperationException>(() => queue2.Dequeue()); // Queue is empty
});
}
[Theory]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
public static void Dequeue_UntilEmpty(int count)
{
Queue queue1 = Helpers.CreateIntQueue(count);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
for (int i = 0; i < count; i++)
{
queue2.Dequeue();
}
Assert.Throws<InvalidOperationException>(() => queue2.Dequeue());
});
}
[Theory]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
[InlineData(10000)]
public static void Enqueue(int count)
{
var queue1 = new Queue();
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
for (int i = 1; i <= count; i++)
{
queue2.Enqueue(i);
Assert.Equal(i, queue2.Count);
}
Assert.Equal(count, queue2.Count);
});
}
[Fact]
public static void Enqueue_Null()
{
var queue1 = new Queue();
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
queue2.Enqueue(null);
Assert.Equal(1, queue2.Count);
});
}
[Theory]
[InlineData(0)]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
public static void GetEnumerator(int count)
{
var queue1 = Helpers.CreateIntQueue(count);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
Assert.NotSame(queue2.GetEnumerator(), queue2.GetEnumerator());
IEnumerator enumerator = queue2.GetEnumerator();
for (int i = 0; i < 2; i++)
{
int counter = 0;
while (enumerator.MoveNext())
{
Assert.Equal(counter, enumerator.Current);
counter++;
}
Assert.Equal(count, counter);
enumerator.Reset();
}
});
}
[Fact]
public static void GetEnumerator_Invalid()
{
var queue1 = Helpers.CreateIntQueue(100);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
IEnumerator enumerator = queue2.GetEnumerator();
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
// If the underlying collection is modified, MoveNext and Reset throw, but Current doesn't
enumerator.MoveNext();
object dequeued = queue2.Dequeue();
Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
Assert.Throws<InvalidOperationException>(() => enumerator.Reset());
Assert.Equal(dequeued, enumerator.Current);
// Current throws if the current index is < 0 or >= count
enumerator = queue2.GetEnumerator();
while (enumerator.MoveNext()) ;
Assert.False(enumerator.MoveNext());
Assert.False(enumerator.MoveNext());
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
// Current throws after resetting
enumerator = queue2.GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.True(enumerator.MoveNext());
enumerator.Reset();
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
});
}
[Fact]
public static void Peek()
{
string s1 = "hello";
string s2 = "world";
char c = '\0';
bool b = false;
byte i8 = 0;
short i16 = 0;
int i32 = 0;
long i64 = 0L;
float f = (float)0.0;
double d = 0.0;
var queue1 = new Queue();
queue1.Enqueue(s1);
queue1.Enqueue(s2);
queue1.Enqueue(c);
queue1.Enqueue(b);
queue1.Enqueue(i8);
queue1.Enqueue(i16);
queue1.Enqueue(i32);
queue1.Enqueue(i64);
queue1.Enqueue(f);
queue1.Enqueue(d);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
Assert.Same(s1, queue2.Peek());
queue2.Dequeue();
Assert.Same(s2, queue2.Peek());
queue2.Dequeue();
Assert.Equal(c, queue2.Peek());
queue2.Dequeue();
Assert.Equal(b, queue2.Peek());
queue2.Dequeue();
Assert.Equal(i8, queue2.Peek());
queue2.Dequeue();
Assert.Equal(i16, queue2.Peek());
queue2.Dequeue();
Assert.Equal(i32, queue2.Peek());
queue2.Dequeue();
Assert.Equal(i64, queue2.Peek());
queue2.Dequeue();
Assert.Equal(f, queue2.Peek());
queue2.Dequeue();
Assert.Equal(d, queue2.Peek());
queue2.Dequeue();
});
}
[Fact]
public static void Peek_EmptyQueue_ThrowsInvalidOperationException()
{
var queue1 = new Queue();
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
Assert.Throws<InvalidOperationException>(() => queue2.Peek()); // Queue is empty
});
}
[Theory]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
public static void ToArray(int count)
{
Queue queue1 = Helpers.CreateIntQueue(count);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
object[] arr = queue2.ToArray();
Assert.Equal(count, arr.Length);
for (int i = 0; i < count; i++)
{
Assert.Equal(queue2.Dequeue(), arr[i]);
}
});
}
[Fact]
public static void ToArray_Wrapped()
{
var queue1 = new Queue(1);
queue1.Enqueue(1);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
object[] arr = queue2.ToArray();
Assert.Equal(1, arr.Length);
Assert.Equal(1, arr[0]);
});
}
[Fact]
public static void ToArrayDiffentObjectTypes()
{
string s1 = "hello";
string s2 = "world";
char c = '\0';
bool b = false;
byte i8 = 0;
short i16 = 0;
int i32 = 0;
long i64 = 0L;
float f = (float)0.0;
double d = 0.0;
var queue1 = new Queue();
queue1.Enqueue(s1);
queue1.Enqueue(s2);
queue1.Enqueue(c);
queue1.Enqueue(b);
queue1.Enqueue(i8);
queue1.Enqueue(i16);
queue1.Enqueue(i32);
queue1.Enqueue(i64);
queue1.Enqueue(f);
queue1.Enqueue(d);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
object[] arr = queue2.ToArray();
Assert.Same(s1, arr[0]);
Assert.Same(s2, arr[1]);
Assert.Equal(c, arr[2]);
Assert.Equal(b, arr[3]);
Assert.Equal(i8, arr[4]);
Assert.Equal(i16, arr[5]);
Assert.Equal(i32, arr[6]);
Assert.Equal(i64, arr[7]);
Assert.Equal(f, arr[8]);
Assert.Equal(d, arr[9]);
});
}
[Fact]
public static void ToArray_EmptyQueue()
{
var queue1 = new Queue();
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
object[] arr = queue2.ToArray();
Assert.Equal(0, arr.Length);
});
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
public static void TrimToSize(int count)
{
Queue queue1 = Helpers.CreateIntQueue(count);
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
queue2.TrimToSize();
Assert.Equal(count, queue2.Count);
// Can change the queue after trimming
queue2.Enqueue(100);
Assert.Equal(count + 1, queue2.Count);
if (count == 0)
{
Assert.Equal(100, queue2.Dequeue());
}
else
{
Assert.Equal(0, queue2.Dequeue());
}
});
}
[Theory]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
public static void TrimToSize_DequeueAll(int count)
{
Queue queue1 = Helpers.CreateIntQueue(count);
for (int i = 0; i < count; i++)
{
queue1.Dequeue();
}
Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
{
queue2.TrimToSize();
Assert.Equal(0, queue2.Count);
// Can change the queue after trimming
queue2.Enqueue(1);
Assert.Equal(1, queue2.Dequeue());
});
}
[Fact]
public static void TrimToSize_Wrapped()
{
var queue = new Queue(100);
// Insert 50 items in the Queue
for (int i = 0; i < 50; i++)
{
queue.Enqueue(i);
}
// Insert and Remove 75 items in the Queue. This should wrap the queue
// where there is 25 at the end of the array and 25 at the beginning
for (int i = 0; i < 75; i++)
{
queue.Enqueue(i + 50);
queue.Dequeue();
}
queue.TrimToSize();
Assert.Equal(50, queue.Count);
Assert.Equal(75, queue.Dequeue());
queue.Enqueue(100);
Assert.Equal(50, queue.Count);
Assert.Equal(76, queue.Dequeue());
}
private class Foo
{
public Foo(int intValue)
{
IntValue = intValue;
}
public int IntValue { get; set; }
}
}
public class Queue_SyncRootTests
{
private const int NumberOfElements = 1000;
private const int NumberOfWorkers = 1000;
private Queue _queueDaughter;
private Queue _queueGrandDaughter;
[Fact]
public void SyncRoot()
{
var queueMother = new Queue();
for (int i = 0; i < NumberOfElements; i++)
{
queueMother.Enqueue(i);
}
Assert.Equal(queueMother.SyncRoot.GetType(), typeof(object));
var queueSon = Queue.Synchronized(queueMother);
_queueGrandDaughter = Queue.Synchronized(queueSon);
_queueDaughter = Queue.Synchronized(queueMother);
Assert.Equal(queueMother.SyncRoot, queueSon.SyncRoot);
Assert.Equal(queueSon.SyncRoot, queueMother.SyncRoot);
Assert.Equal(queueMother.SyncRoot, _queueGrandDaughter.SyncRoot);
Assert.Equal(queueMother.SyncRoot, _queueDaughter.SyncRoot);
Task[] workers = new Task[NumberOfWorkers];
Action action1 = SortElements;
Action action2 = ReverseElements;
for (int i = 0; i < NumberOfWorkers; i += 2)
{
workers[i] = Task.Run(action1);
workers[i + 1] = Task.Run(action2);
}
Task.WaitAll(workers);
}
private void SortElements()
{
_queueGrandDaughter.Clear();
for (int i = 0; i < NumberOfElements; i++)
{
_queueGrandDaughter.Enqueue(i);
}
}
private void ReverseElements()
{
_queueDaughter.Clear();
for (int i = 0; i < NumberOfElements; i++)
{
_queueDaughter.Enqueue(i);
}
}
}
public class Queue_SynchronizedTests
{
public Queue _queue;
public int _threadsToUse = 8;
public int _threadAge = 5; // 5000;
public int _threadCount;
[Fact]
public static void Synchronized()
{
Queue queue = Helpers.CreateIntQueue(100);
Queue syncQueue = Queue.Synchronized(queue);
Assert.True(syncQueue.IsSynchronized);
Assert.Equal(queue.Count, syncQueue.Count);
for (int i = 0; i < queue.Count; i++)
{
Assert.True(syncQueue.Contains(i));
}
}
[Fact]
public void SynchronizedEnqueue()
{
// Enqueue
_queue = Queue.Synchronized(new Queue());
PerformTest(StartEnqueueThread, 40);
// Dequeue
Queue queue = Helpers.CreateIntQueue(_threadAge);
_queue = Queue.Synchronized(queue);
PerformTest(StartDequeueThread, 0);
// Enqueue, dequeue
_queue = Queue.Synchronized(new Queue());
PerformTest(StartEnqueueDequeueThread, 0);
// Dequeue, enqueue
queue = Helpers.CreateIntQueue(_threadAge);
_queue = Queue.Synchronized(queue);
PerformTest(StartDequeueEnqueueThread, _threadAge);
}
private void PerformTest(Action action, int expected)
{
var tasks = new Task[_threadsToUse];
for (int i = 0; i < _threadsToUse; i++)
{
tasks[i] = Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
_threadCount = _threadsToUse;
Task.WaitAll(tasks);
Assert.Equal(expected, _queue.Count);
}
[Fact]
public static void Synchronized_NullQueue_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>("queue", () => Queue.Synchronized(null)); // Queue is null
}
public void StartEnqueueThread()
{
int t_age = _threadAge;
while (t_age > 0)
{
_queue.Enqueue(t_age);
Interlocked.Decrement(ref t_age);
}
Interlocked.Decrement(ref _threadCount);
}
private void StartDequeueThread()
{
int t_age = _threadAge;
while (t_age > 0)
{
try
{
_queue.Dequeue();
}
catch { }
Interlocked.Decrement(ref t_age);
}
Interlocked.Decrement(ref _threadCount);
}
private void StartEnqueueDequeueThread()
{
int t_age = _threadAge;
while (t_age > 0)
{
_queue.Enqueue(2);
_queue.Dequeue();
Interlocked.Decrement(ref t_age);
}
Interlocked.Decrement(ref _threadCount);
}
private void StartDequeueEnqueueThread()
{
int t_age = _threadAge;
while (t_age > 0)
{
_queue.Dequeue();
_queue.Enqueue(2);
Interlocked.Decrement(ref t_age);
}
Interlocked.Decrement(ref _threadCount);
}
}
}
|