This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
IndexOfTests.cs
169 lines (146 loc) · 9.08 KB
/
IndexOfTests.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
// 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.Collections.Generic;
using System.Linq;
using Xunit;
namespace System.Collections.Immutable.Tests
{
public static class IndexOfTests
{
public static void IndexOfTest<TCollection>(
Func<IEnumerable<int>, TCollection> factory,
Func<TCollection, int, int> indexOfItem,
Func<TCollection, int, int, int> indexOfItemIndex,
Func<TCollection, int, int, int, int> indexOfItemIndexCount,
Func<TCollection, int, int, int, IEqualityComparer<int>, int> indexOfItemIndexCountEQ)
{
var emptyCollection = factory(new int[0]);
var collection1256 = factory(new[] { 1, 2, 5, 6 });
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(collection1256, 100, 1, 20, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(collection1256, 100, 1, -1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, new CustomComparer(50)));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, new CustomComparer(50)));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(collection1256, 100, 1, 20, new CustomComparer(1)));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(collection1256, 100, 1, -1, new CustomComparer(1)));
Assert.Equal(-1, indexOfItem(emptyCollection, 5));
Assert.Equal(-1, indexOfItemIndex(emptyCollection, 5, 0));
Assert.Equal(2, indexOfItemIndex(collection1256, 5, 1));
Assert.Equal(-1, indexOfItemIndexCount(emptyCollection, 5, 0, 0));
Assert.Equal(-1, indexOfItemIndexCount(collection1256, 5, 1, 1));
Assert.Equal(2, indexOfItemIndexCount(collection1256, 5, 1, 2));
// Create a list with contents: 100,101,102,103,104,100,101,102,103,104
var list = ImmutableList<int>.Empty.AddRange(Enumerable.Range(100, 5).Concat(Enumerable.Range(100, 5)));
var bclList = list.ToList();
Assert.Equal(-1, indexOfItem(factory(list), 6));
Assert.Equal(2, indexOfItemIndexCountEQ(factory(list), 102, 0, 4, null));
if (factory(list) is IList)
{
Assert.Equal(-1, ((IList)factory(list)).IndexOf(6));
}
for (int idx = 0; idx < list.Count; idx++)
{
for (int count = 0; count <= list.Count - idx; count++)
{
foreach (int match in list.Concat(new[] { 88 }))
{
int expected = bclList.IndexOf(match, idx, count);
int actual = indexOfItemIndexCount(factory(list), match, idx, count);
Assert.Equal(expected, actual);
actual = indexOfItemIndexCountEQ(factory(list), match, idx, count, new CustomComparer(count));
Assert.Equal(count > 0 ? idx + count - 1 : -1, actual);
if (count == list.Count)
{
// Also test the IndexOf overload that takes no count parameter.
actual = indexOfItemIndex(factory(list), match, idx);
Assert.Equal(expected, actual);
if (idx == 0)
{
// Also test the IndexOf overload that takes no index parameter.
actual = indexOfItem(factory(list), match);
Assert.Equal(expected, actual);
}
}
}
}
}
}
public static void LastIndexOfTest<TCollection>(
Func<IEnumerable<int>, TCollection> factory,
Func<TCollection, int, int> lastIndexOfItem,
Func<TCollection, int, IEqualityComparer<int>, int> lastIndexOfItemEQ,
Func<TCollection, int, int, int> lastIndexOfItemIndex,
Func<TCollection, int, int, int, int> lastIndexOfItemIndexCount,
Func<TCollection, int, int, int, IEqualityComparer<int>, int> lastIndexOfItemIndexCountEQ)
{
var emptyCollection = factory(new int[0]);
var collection1256 = factory(new[] { 1, 2, 5, 6 });
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, new CustomComparer(50)));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, new CustomComparer(50)));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, new CustomComparer(1)));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, new CustomComparer(1)));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndex(collection1256, 2, 5));
Assert.Equal(-1, lastIndexOfItem(emptyCollection, 5));
Assert.Equal(-1, lastIndexOfItemEQ(emptyCollection, 5, EqualityComparer<int>.Default));
Assert.Equal(-1, lastIndexOfItemIndex(emptyCollection, 5, 0));
Assert.Equal(-1, lastIndexOfItemIndexCount(emptyCollection, 5, 0, 0));
// Create a list with contents: 100,101,102,103,104,100,101,102,103,104
var list = ImmutableList<int>.Empty.AddRange(Enumerable.Range(100, 5).Concat(Enumerable.Range(100, 5)));
var bclList = list.ToList();
Assert.Equal(-1, lastIndexOfItem(factory(list), 6));
Assert.Equal(2, lastIndexOfItemIndexCountEQ(factory(list), 102, 6, 5, null));
for (int idx = 0; idx < list.Count; idx++)
{
for (int count = 0; count <= idx + 1; count++)
{
foreach (int match in list.Concat(new[] { 88 }))
{
int expected = bclList.LastIndexOf(match, idx, count);
int actual = lastIndexOfItemIndexCount(factory(list), match, idx, count);
Assert.Equal(expected, actual);
expected = bclList.LastIndexOf(match);
actual = lastIndexOfItemEQ(factory(list), match, EqualityComparer<int>.Default);
Assert.Equal(expected, actual);
actual = lastIndexOfItemIndexCountEQ(factory(list), match, idx, count, new CustomComparer(count));
Assert.Equal(count > 0 ? (idx - count + 1) : -1, actual);
if (count == list.Count)
{
// Also test the LastIndexOf overload that takes no count parameter.
actual = lastIndexOfItemIndex(factory(list), match, idx);
Assert.Equal(expected, actual);
if (idx == list.Count - 1)
{
// Also test the LastIndexOf overload that takes no index parameter.
actual = lastIndexOfItem(factory(list), match);
Assert.Equal(expected, actual);
}
}
}
}
}
}
private class CustomComparer : IEqualityComparer<int>
{
private readonly int _matchOnXIteration;
private int _iteration;
public CustomComparer(int matchOnXIteration)
{
_matchOnXIteration = matchOnXIteration;
}
public bool Equals(int x, int y)
{
return ++_iteration == _matchOnXIteration;
}
public int GetHashCode(int obj)
{
throw new NotImplementedException();
}
}
}
}