forked from dotnet/runtime
/
IEnumerable.Generic.Tests.cs
944 lines (876 loc) · 36.7 KB
/
IEnumerable.Generic.Tests.cs
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
939
940
941
942
943
944
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Xunit;
namespace System.Collections.Tests
{
/// <summary>
/// Contains tests that ensure the correctness of any class that implements the generic
/// IEnumerable interface.
/// </summary>
public abstract partial class IEnumerable_Generic_Tests<T> : TestBase<T>
{
#region IEnumerable<T> Helper Methods
/// <summary>
/// Creates an instance of an IEnumerable{T} that can be used for testing.
/// </summary>
/// <param name="count">The number of unique items that the returned IEnumerable{T} contains.</param>
/// <returns>An instance of an IEnumerable{T} that can be used for testing.</returns>
protected abstract IEnumerable<T> GenericIEnumerableFactory(int count);
/// <summary>
/// Modifies the given IEnumerable such that any enumerators for that IEnumerable will be
/// invalidated.
/// </summary>
/// <param name="enumerable">An IEnumerable to modify</param>
/// <returns>true if the enumerable was successfully modified. Else false.</returns>
protected delegate bool ModifyEnumerable(IEnumerable<T> enumerable);
/// <summary>
/// To be implemented in the concrete collections test classes. Returns a set of ModifyEnumerable delegates
/// that modify the enumerable passed to them.
/// </summary>
protected abstract IEnumerable<ModifyEnumerable> GetModifyEnumerables(ModifyOperation operations);
protected virtual ModifyOperation ModifyEnumeratorThrows => ModifyOperation.Add | ModifyOperation.Insert | ModifyOperation.Overwrite | ModifyOperation.Remove | ModifyOperation.Clear;
protected virtual ModifyOperation ModifyEnumeratorAllowed => ModifyOperation.None;
/// <summary>
/// The Reset method is provided for COM interoperability. It does not necessarily need to be
/// implemented; instead, the implementer can simply throw a NotSupportedException.
///
/// If Reset is not implemented, this property must return False. The default value is true.
/// </summary>
protected virtual bool ResetImplemented => true;
/// <summary>
/// When calling Current of the enumerator before the first MoveNext, after the end of the collection,
/// or after modification of the enumeration, the resulting behavior is undefined. Tests are included
/// to cover two behavioral scenarios:
/// - Throwing an InvalidOperationException
/// - Returning an undefined value.
///
/// If this property is set to true, the tests ensure that the exception is thrown. The default value is
/// false.
/// </summary>
protected virtual bool Enumerator_Current_UndefinedOperation_Throws => false;
/// <summary>
/// When calling Current of the empty enumerator before the first MoveNext, after the end of the collection,
/// or after modification of the enumeration, the resulting behavior is undefined. Tests are included
/// to cover two behavioral scenarios:
/// - Throwing an InvalidOperationException
/// - Returning an undefined value.
///
/// If this property is set to true, the tests ensure that the exception is thrown. The default value is
/// <see cref="Enumerator_Current_UndefinedOperation_Throws"/>.
/// </summary>
protected virtual bool Enumerator_Empty_Current_UndefinedOperation_Throws => Enumerator_Current_UndefinedOperation_Throws;
/// <summary>
/// When calling MoveNext or Reset after modification of the enumeration, the resulting behavior is
/// undefined. Tests are included to cover two behavioral scenarios:
/// - Throwing an InvalidOperationException
/// - Execute MoveNext or Reset.
///
/// If this property is set to true, the tests ensure that the exception is thrown. The default value is
/// true.
/// </summary>
protected virtual bool Enumerator_ModifiedDuringEnumeration_ThrowsInvalidOperationException => true;
/// <summary>
/// When calling MoveNext or Reset after modification of an empty enumeration, the resulting behavior is
/// undefined. Tests are included to cover two behavioral scenarios:
/// - Throwing an InvalidOperationException
/// - Execute MoveNext or Reset.
///
/// If this property is set to true, the tests ensure that the exception is thrown. The default value is
/// <see cref="Enumerator_ModifiedDuringEnumeration_ThrowsInvalidOperationException"/>.
/// </summary>
protected virtual bool Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException => Enumerator_ModifiedDuringEnumeration_ThrowsInvalidOperationException;
/// <summary>Whether the enumerator returned from GetEnumerator is a singleton instance when the collection is empty.</summary>
protected virtual bool Enumerator_Empty_UsesSingletonInstance => false;
/// <summary>
/// Specifies whether this IEnumerable follows some sort of ordering pattern.
/// </summary>
protected virtual EnumerableOrder Order => EnumerableOrder.Sequential;
/// <summary>
/// An enum to allow specification of the order of the Enumerable. Used in validation for enumerables.
/// </summary>
protected enum EnumerableOrder
{
Unspecified,
Sequential
}
#endregion
#region Validation
private void RepeatTest(
Action<IEnumerator<T>, T[], int> testCode,
int iters = 3)
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(32);
T[] items = enumerable.ToArray();
IEnumerator<T> enumerator = enumerable.GetEnumerator();
for (int i = 0; i < iters; i++)
{
testCode(enumerator, items, i);
if (!ResetImplemented)
{
enumerator = enumerable.GetEnumerator();
}
else
{
enumerator.Reset();
}
}
}
private void RepeatTest(
Action<IEnumerator<T>, T[]> testCode,
int iters = 3)
{
RepeatTest((e, i, it) => testCode(e, i), iters);
}
private void VerifyModifiedEnumerator(
IEnumerator<T> enumerator,
object expectedCurrent,
bool expectCurrentThrow,
bool atEnd)
{
if (expectCurrentThrow)
{
Assert.Throws<InvalidOperationException>(
() => enumerator.Current);
}
else
{
object current = enumerator.Current;
for (int i = 0; i < 3; i++)
{
Assert.Equal(expectedCurrent, current);
current = enumerator.Current;
}
}
Assert.Throws<InvalidOperationException>(
() => enumerator.MoveNext());
if (!!ResetImplemented)
{
Assert.Throws<InvalidOperationException>(
() => enumerator.Reset());
}
}
private void VerifyEnumerator(
IEnumerator<T> enumerator,
T[] expectedItems)
{
VerifyEnumerator(
enumerator,
expectedItems,
0,
expectedItems.Length,
true,
true);
}
private void VerifyEnumerator(
IEnumerator<T> enumerator,
T[] expectedItems,
int startIndex,
int count,
bool validateStart,
bool validateEnd)
{
bool needToMatchAllExpectedItems = count - startIndex == expectedItems.Length;
if (validateStart)
{
for (int i = 0; i < 3; i++)
{
if (Enumerator_Current_UndefinedOperation_Throws)
{
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
}
else
{
_ = enumerator.Current;
}
}
}
int iterations;
if (Order == EnumerableOrder.Unspecified)
{
var itemsVisited =
new BitArray(
needToMatchAllExpectedItems
? count
: expectedItems.Length,
false);
for (iterations = 0;
iterations < count && enumerator.MoveNext();
iterations++)
{
object currentItem = enumerator.Current;
bool itemFound = false;
for (int i = 0; i < itemsVisited.Length; ++i)
{
if (!itemsVisited[i]
&& Equals(
currentItem,
expectedItems[
i
+ (needToMatchAllExpectedItems
? startIndex
: 0)]))
{
itemsVisited[i] = true;
itemFound = true;
break;
}
}
Assert.True(itemFound, "itemFound");
for (int i = 0; i < 3; i++)
{
object tempItem = enumerator.Current;
Assert.Equal(currentItem, tempItem);
}
}
if (needToMatchAllExpectedItems)
{
for (int i = 0; i < itemsVisited.Length; i++)
{
Assert.True(itemsVisited[i]);
}
}
else
{
int visitedItemCount = 0;
for (int i = 0; i < itemsVisited.Length; i++)
{
if (itemsVisited[i])
{
++visitedItemCount;
}
}
Assert.Equal(count, visitedItemCount);
}
}
else if (Order == EnumerableOrder.Sequential)
{
for (iterations = 0;
iterations < count && enumerator.MoveNext();
iterations++)
{
object currentItem = enumerator.Current;
Assert.Equal(expectedItems[iterations], currentItem);
for (int i = 0; i < 3; i++)
{
object tempItem = enumerator.Current;
Assert.Equal(currentItem, tempItem);
}
}
}
else
{
throw new ArgumentException(
"EnumerableOrder is invalid.");
}
Assert.Equal(count, iterations);
if (validateEnd)
{
for (int i = 0; i < 3; i++)
{
Assert.False(enumerator.MoveNext(), "enumerator.MoveNext() returned true past the expected end.");
if (Enumerator_Current_UndefinedOperation_Throws)
{
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
}
else
{
_ = enumerator.Current;
}
}
}
}
#endregion
#region GetEnumerator()
[Fact]
public void IEnumerable_NonGeneric_GetEnumerator_EmptyCollection_UsesSingleton()
{
IEnumerable enumerable = GenericIEnumerableFactory(0);
IEnumerator enumerator1 = enumerable.GetEnumerator();
try
{
IEnumerator enumerator2 = enumerable.GetEnumerator();
try
{
Assert.Equal(Enumerator_Empty_UsesSingletonInstance, ReferenceEquals(enumerator1, enumerator2));
}
finally
{
if (enumerator2 is IDisposable d2) d2.Dispose();
}
}
finally
{
if (enumerator1 is IDisposable d1) d1.Dispose();
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_GetEnumerator_NoExceptionsWhileGetting(int count)
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
enumerable.GetEnumerator().Dispose();
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_GetEnumerator_ReturnsUniqueEnumerator(int count)
{
//Tests that the enumerators returned by GetEnumerator operate independently of one another
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
int iterations = 0;
foreach (T item in enumerable)
foreach (T item2 in enumerable)
foreach (T item3 in enumerable)
iterations++;
Assert.Equal(count * count * count, iterations);
}
#endregion
#region Enumerator.MoveNext
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_MoveNext_FromStartToFinish(int count)
{
int iterations = 0;
using (IEnumerator<T> enumerator = GenericIEnumerableFactory(count).GetEnumerator())
{
while (enumerator.MoveNext())
iterations++;
Assert.Equal(count, iterations);
}
}
/// <summary>
/// For most collections, all calls to MoveNext after disposal of an enumerator will return false.
/// Some collections (SortedList), however, treat a call to dispose as if it were a call to Reset. Since the docs
/// specify neither of these as being strictly correct, we leave the method virtual.
/// </summary>
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public virtual void Enumerator_MoveNext_AfterDisposal(int count)
{
IEnumerator<T> enumerator = GenericIEnumerableFactory(count).GetEnumerator();
for (int i = 0; i < count; i++)
enumerator.MoveNext();
enumerator.Dispose();
Assert.False(enumerator.MoveNext());
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_MoveNext_AfterEndOfCollection(int count)
{
using (IEnumerator<T> enumerator = GenericIEnumerableFactory(count).GetEnumerator())
{
for (int i = 0; i < count; i++)
enumerator.MoveNext();
Assert.False(enumerator.MoveNext());
Assert.False(enumerator.MoveNext());
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_MoveNext_ModifiedBeforeEnumeration_ThrowsInvalidOperationException(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorThrows), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
if (ModifyEnumerable(enumerable))
{
if (count == 0 ? Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException : Enumerator_ModifiedDuringEnumeration_ThrowsInvalidOperationException)
{
Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
}
else
{
enumerator.MoveNext();
}
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_MoveNext_ModifiedBeforeEnumeration_Succeeds(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorAllowed), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
if (ModifyEnumerable(enumerable))
{
if (Enumerator_ModifiedDuringEnumeration_ThrowsInvalidOperationException)
{
enumerator.MoveNext();
}
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_MoveNext_ModifiedDuringEnumeration_ThrowsInvalidOperationException(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorThrows), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
for (int i = 0; i < count / 2; i++)
enumerator.MoveNext();
if (ModifyEnumerable(enumerable))
{
if (count == 0 ? Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException : Enumerator_ModifiedDuringEnumeration_ThrowsInvalidOperationException)
{
Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
}
else
{
enumerator.MoveNext();
}
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_MoveNext_ModifiedDuringEnumeration_Succeeds(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorAllowed), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
for (int i = 0; i < count / 2; i++)
enumerator.MoveNext();
if (ModifyEnumerable(enumerable))
{
enumerator.MoveNext();
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_MoveNext_ModifiedAfterEnumeration_ThrowsInvalidOperationException(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorThrows), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
while (enumerator.MoveNext()) ;
if (ModifyEnumerable(enumerable))
{
if (count == 0 ? Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException : Enumerator_ModifiedDuringEnumeration_ThrowsInvalidOperationException)
{
Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
}
else
{
enumerator.MoveNext();
}
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_MoveNext_ModifiedAfterEnumeration_Succeeds(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorAllowed), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
while (enumerator.MoveNext())
;
if (ModifyEnumerable(enumerable))
{
enumerator.MoveNext();
}
}
});
}
[Fact]
public void IEnumerable_Generic_Enumerator_MoveNextHitsAllItems()
{
RepeatTest(
(enumerator, items) =>
{
int iterations = 0;
while (enumerator.MoveNext())
{
iterations++;
}
Assert.Equal(items.Length, iterations);
});
}
[Fact]
public void IEnumerable_Generic_Enumerator_MoveNextFalseAfterEndOfCollection()
{
RepeatTest(
(enumerator, items) =>
{
while (enumerator.MoveNext())
{
}
Assert.False(enumerator.MoveNext());
});
}
#endregion
#region Enumerator.Current
[Fact]
public void IEnumerable_Generic_Enumerator_Current()
{
// Verify that current returns proper result.
RepeatTest(
(enumerator, items, iteration) =>
{
if (iteration == 1)
{
VerifyEnumerator(
enumerator,
items,
0,
items.Length / 2,
true,
false);
}
else
{
VerifyEnumerator(enumerator, items);
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Current_ReturnsSameValueOnRepeatedCalls(int count)
{
using (IEnumerator<T> enumerator = GenericIEnumerableFactory(count).GetEnumerator())
{
while (enumerator.MoveNext())
{
T current = enumerator.Current;
Assert.Equal(current, enumerator.Current);
Assert.Equal(current, enumerator.Current);
Assert.Equal(current, enumerator.Current);
}
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Current_ReturnsSameObjectsOnDifferentEnumerators(int count)
{
// Ensures that the elements returned from enumeration are exactly the same collection of
// elements returned from a previous enumeration
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
Dictionary<T, int> firstValues = new Dictionary<T, int>(count);
Dictionary<T, int> secondValues = new Dictionary<T, int>(count);
foreach (T item in enumerable)
firstValues[item] = firstValues.ContainsKey(item) ? firstValues[item]++ : 1;
foreach (T item in enumerable)
secondValues[item] = secondValues.ContainsKey(item) ? secondValues[item]++ : 1;
Assert.Equal(firstValues.Count, secondValues.Count);
foreach (T key in firstValues.Keys)
Assert.Equal(firstValues[key], secondValues[key]);
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Current_BeforeFirstMoveNext_UndefinedBehavior(int count)
{
T current;
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
if (count == 0 ? Enumerator_Empty_Current_UndefinedOperation_Throws : Enumerator_Current_UndefinedOperation_Throws)
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
else
current = enumerator.Current;
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Current_AfterEndOfEnumerable_UndefinedBehavior(int count)
{
T current;
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
while (enumerator.MoveNext()) ;
if (count == 0 ? Enumerator_Empty_Current_UndefinedOperation_Throws : Enumerator_Current_UndefinedOperation_Throws)
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
else
current = enumerator.Current;
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Current_ModifiedDuringEnumeration_UndefinedBehavior(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorThrows), ModifyEnumerable =>
{
T current;
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
if (ModifyEnumerable(enumerable))
{
if (count == 0 ? Enumerator_Empty_Current_UndefinedOperation_Throws : Enumerator_Current_UndefinedOperation_Throws)
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
else
current = enumerator.Current;
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Current_ModifiedDuringEnumeration_Succeeds(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorAllowed), ModifyEnumerable =>
{
T current;
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
if (ModifyEnumerable(enumerable))
{
current = enumerator.Current;
}
}
});
}
#endregion
#region Enumerator.Reset
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Reset_BeforeIteration_Support(int count)
{
using (IEnumerator<T> enumerator = GenericIEnumerableFactory(count).GetEnumerator())
{
if (ResetImplemented)
enumerator.Reset();
else
Assert.Throws<NotSupportedException>(() => enumerator.Reset());
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Reset_ModifiedBeforeEnumeration_ThrowsInvalidOperationException(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorThrows), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
if (ModifyEnumerable(enumerable))
{
if (count == 0 ? Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException : Enumerator_ModifiedDuringEnumeration_ThrowsInvalidOperationException)
{
Assert.Throws<InvalidOperationException>(() => enumerator.Reset());
}
else
{
enumerator.Reset();
}
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Reset_ModifiedBeforeEnumeration_Succeeds(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorAllowed), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
if (ModifyEnumerable(enumerable))
{
enumerator.Reset();
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Reset_ModifiedDuringEnumeration_ThrowsInvalidOperationException(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorThrows), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
for (int i = 0; i < count / 2; i++)
enumerator.MoveNext();
if (ModifyEnumerable(enumerable))
{
if (count == 0 ? Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException : Enumerator_ModifiedDuringEnumeration_ThrowsInvalidOperationException)
{
Assert.Throws<InvalidOperationException>(() => enumerator.Reset());
}
else
{
enumerator.Reset();
}
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Reset_ModifiedDuringEnumeration_Succeeds(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorAllowed), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
for (int i = 0; i < count / 2; i++)
enumerator.MoveNext();
if (ModifyEnumerable(enumerable))
{
enumerator.Reset();
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Reset_ModifiedAfterEnumeration_ThrowsInvalidOperationException(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorThrows), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
while (enumerator.MoveNext()) ;
if (ModifyEnumerable(enumerable))
{
if (count == 0 ? Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException : Enumerator_ModifiedDuringEnumeration_ThrowsInvalidOperationException)
{
Assert.Throws<InvalidOperationException>(() => enumerator.Reset());
}
else
{
enumerator.Reset();
}
}
}
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IEnumerable_Generic_Enumerator_Reset_ModifiedAfterEnumeration_Succeeds(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorAllowed), ModifyEnumerable =>
{
IEnumerable<T> enumerable = GenericIEnumerableFactory(count);
using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
{
while (enumerator.MoveNext())
;
if (ModifyEnumerable(enumerable))
{
enumerator.Reset();
}
}
});
}
[Fact]
public void IEnumerable_Generic_Enumerator_Reset()
{
if (!ResetImplemented)
{
RepeatTest(
(enumerator, items) =>
{
Assert.Throws<NotSupportedException>(
() => enumerator.Reset());
});
RepeatTest(
(enumerator, items, iter) =>
{
if (iter == 1)
{
VerifyEnumerator(
enumerator,
items,
0,
items.Length / 2,
true,
false);
for (int i = 0; i < 3; i++)
{
Assert.Throws<NotSupportedException>(
() => enumerator.Reset());
}
VerifyEnumerator(
enumerator,
items,
items.Length / 2,
items.Length - (items.Length / 2),
false,
true);
}
else if (iter == 2)
{
VerifyEnumerator(enumerator, items);
for (int i = 0; i < 3; i++)
{
Assert.Throws<NotSupportedException>(
() => enumerator.Reset());
}
VerifyEnumerator(
enumerator,
items,
0,
0,
false,
true);
}
else
{
VerifyEnumerator(enumerator, items);
}
});
}
else
{
RepeatTest(
(enumerator, items, iter) =>
{
if (iter == 1)
{
VerifyEnumerator(
enumerator,
items,
0,
items.Length / 2,
true,
false);
enumerator.Reset();
enumerator.Reset();
}
else if (iter == 3)
{
VerifyEnumerator(enumerator, items);
enumerator.Reset();
enumerator.Reset();
}
else
{
VerifyEnumerator(enumerator, items);
}
},
5);
}
}
#endregion
}
}