-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathList.Generic.Tests.AddRange.cs
73 lines (63 loc) · 2.86 KB
/
List.Generic.Tests.AddRange.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
// 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.Linq;
using Xunit;
namespace System.Collections.Tests
{
/// <summary>
/// Contains tests that ensure the correctness of the List class.
/// </summary>
public abstract partial class List_Generic_Tests<T> : IList_Generic_Tests<T>
{
// Has tests that pass a variably sized TestCollection and MyEnumerable to the AddRange function
[Theory]
[MemberData(nameof(EnumerableTestData))]
public void AddRange(EnumerableType enumerableType, int listLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
{
List<T> list = GenericListFactory(listLength);
List<T> listBeforeAdd = list.ToList();
IEnumerable<T> enumerable = CreateEnumerable(enumerableType, list, enumerableLength, numberOfMatchingElements, numberOfDuplicateElements);
list.AddRange(enumerable);
// Check that the first section of the List is unchanged
Assert.All(Enumerable.Range(0, listLength), index =>
{
Assert.Equal(listBeforeAdd[index], list[index]);
});
// Check that the added elements are correct
Assert.All(Enumerable.Range(0, enumerableLength), index =>
{
Assert.Equal(enumerable.ElementAt(index), list[index + listLength]);
});
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void AddRange_NullEnumerable_ThrowsArgumentNullException(int count)
{
List<T> list = GenericListFactory(count);
List<T> listBeforeAdd = list.ToList();
Assert.Throws<ArgumentNullException>(() => list.AddRange(null));
Assert.Equal(listBeforeAdd, list);
}
[Fact]
public void AddRange_AddSelfAsEnumerable_ThrowsExceptionWhenNotEmpty()
{
List<T> list = GenericListFactory(0);
// Succeeds when list is empty.
list.AddRange(list);
list.AddRange(list.Where(_ => true));
// Succeeds when list has elements and is added as collection.
list.Add(default);
Assert.Equal(1, list.Count);
list.AddRange(list);
Assert.Equal(2, list.Count);
list.AddRange(list);
Assert.Equal(4, list.Count);
// Fails version check when list has elements and is added as non-collection.
Assert.Throws<InvalidOperationException>(() => list.AddRange(list.Where(_ => true)));
Assert.Equal(5, list.Count);
Assert.Throws<InvalidOperationException>(() => list.AddRange(list.Where(_ => true)));
Assert.Equal(6, list.Count);
}
}
}