-
Notifications
You must be signed in to change notification settings - Fork 868
Expand file tree
/
Copy pathRemoveRange.Extensions.Tests.cs
More file actions
163 lines (139 loc) · 5.24 KB
/
RemoveRange.Extensions.Tests.cs
File metadata and controls
163 lines (139 loc) · 5.24 KB
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
using System;
using System.Collections;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Rendering;
namespace UnityEditor.Rendering.Tests
{
public class RemoveRangeExtensionsTests
{
static TestCaseData[] s_ListTestsCaseDatas =
{
new TestCaseData(new int[] {1,2,3,4,5,6}, 1, 2).SetName("Remove middle"),
new TestCaseData(new int[] {1,2,3,4,5,6}, 0, 2).SetName("Remove front"),
new TestCaseData(new int[] {1,2,3,4,5,6}, 0, 6).SetName("Remove all"),
new TestCaseData(new int[] {1,2,3,4,5,6}, 5, 1).SetName("Remove back"),
new TestCaseData(new int[] {1,2,3,4,5,6}, 0, 0).SetName("Index 0"),
new TestCaseData(new int[] {1,2,3,4,5,6}, 5, 0).SetName("Count 0")
};
bool ItemInRangeAreRemovedAfterRemoveRange<TList>(TList list, int startIndex, int count)
where TList : IList<int>
{
using (ListPool<int>.Get(out var copy))
{
copy.AddRange(list);
if (list.TryRemoveElementsInRange(startIndex, count, out var exception))
{
copy.RemoveRange(startIndex, count);
return copy.SequenceEqual(list);
}
return false;
}
}
[Test, TestCaseSource(nameof(s_ListTestsCaseDatas))]
public void ItemInRangeAreRemovedAfterRemoveRangeForList(int[] ints, int startIndex, int count)
{
using (GenericPool<SimpleList>.Get(out var copy))
{
copy.AddRange(ints);
Assert.IsTrue(ItemInRangeAreRemovedAfterRemoveRange<IList<int>>(copy as IList<int>, startIndex, count));
}
}
[Test, TestCaseSource(nameof(s_ListTestsCaseDatas))]
public void ItemInRangeAreRemovedAfterRemoveRangeForSimpleList(int[] ints, int startIndex, int count)
{
using (ListPool<int>.Get(out var copy))
{
copy.AddRange(ints);
Assert.IsTrue(ItemInRangeAreRemovedAfterRemoveRange<List<int>>(copy, startIndex, count));
}
}
static TestCaseData[] s_ListTestsCaseDatasExceptions =
{
new TestCaseData(new int[] {1,2,3,4,5,6}, 5, -1).SetName("Count negative").Returns(typeof(ArgumentOutOfRangeException)),
new TestCaseData(new int[] {1,2,3,4,5,6}, -1, 2).SetName("Index negative").Returns(typeof(ArgumentOutOfRangeException)),
new TestCaseData(new int[] {1,2,3,4,5,6}, 5, 5).SetName("Count exceeds list size").Returns(typeof(ArgumentException)),
};
Exception ExceptionsAreCorrect<TList>(TList list, int startIndex, int count)
where TList : IList<int>
{
list.TryRemoveElementsInRange(startIndex, count, out var error);
return error;
}
[Test, TestCaseSource(nameof(s_ListTestsCaseDatasExceptions))]
public Type ExceptionsAreCorrectForList(int[] ints, int startIndex, int count)
{
using (ListPool<int>.Get(out var copy))
{
copy.AddRange(ints);
return ExceptionsAreCorrect(copy, startIndex, count).GetType();
}
}
[Test, TestCaseSource(nameof(s_ListTestsCaseDatasExceptions))]
public Type ExceptionsAreCorrectForSimpleList(int[] ints, int startIndex, int count)
{
using (GenericPool<SimpleList>.Get(out var copy))
{
copy.AddRange(ints);
return ExceptionsAreCorrect(copy, startIndex, count).GetType();
}
}
class SimpleList : IList<int>
{
private List<int> m_List = new List<int>();
public void AddRange(int[] ints)
{
m_List.Clear();
m_List.AddRange(ints);
}
public IEnumerator<int> GetEnumerator()
{
return m_List.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(int item)
{
m_List.Add(item);
}
public void Clear()
{
m_List.Clear();
}
public bool Contains(int item)
{
return m_List.Contains(item);
}
public void CopyTo(int[] array, int arrayIndex)
{
m_List.CopyTo(array, arrayIndex);
}
public bool Remove(int item)
{
return m_List.Remove(item);
}
public int Count => m_List.Count;
public bool IsReadOnly => false;
public int IndexOf(int item)
{
return m_List.IndexOf(item);
}
public void Insert(int index, int item)
{
m_List.Insert(index, item);
}
public void RemoveAt(int index)
{
m_List.RemoveAt(index);
}
public int this[int index]
{
get => m_List[index];
set => m_List[index] = value;
}
}
}
}