-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathEnumerableExtensions.cs
203 lines (181 loc) · 7.82 KB
/
EnumerableExtensions.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
using System.Collections.Generic;
using System.Linq;
using Nito.Comparers.Linq;
using Xunit;
using System;
using Nito.Comparers;
#pragma warning disable CA1861 // Avoid constant arrays as arguments
namespace Linq.UnitTests
{
public class EnumerableExtensionsUnitTests
{
[Fact]
public void OrderBy_UsesComparer()
{
IEnumerable<int?> input = new int?[] { int.MaxValue, int.MinValue, null, 0, 1, 2, 3 };
var result = input.OrderBy(x => x, c => c.OrderBy(x => x % 2 == 0).ThenBy(x => x));
Assert.Equal(new int?[] { null, 1, 3, int.MaxValue, int.MinValue, 0, 2 }, result);
}
[Fact]
public void OrderByDescending_UsesComparer()
{
IEnumerable<int?> input = new int?[] { int.MaxValue, int.MinValue, null, 0, 1, 2, 3 };
var result = input.OrderByDescending(x => x, c => c.OrderBy(x => x % 2 == 0).ThenBy(x => x));
Assert.Equal(new int?[] { 2, 0, int.MinValue, int.MaxValue, 3, 1, null }, result);
}
[Fact]
public void ThenBy_UsesComparer()
{
IEnumerable<int?> input = new int?[] { int.MaxValue, int.MinValue, null, 0, 3, 2, 1 };
var result = input.OrderBy(x => x, c => c.Null()).ThenBy(x => x, c => c.OrderBy(x => x));
Assert.Equal(new int?[] { null, int.MinValue, 0, 1, 2, 3, int.MaxValue }, result);
}
[Fact]
public void ThenByDescending_UsesComparer()
{
IEnumerable<int?> input = new int?[] { int.MaxValue, int.MinValue, null, 0, 3, 2, 1 };
var result = input.OrderBy(x => x, c => c.Null()).ThenByDescending(x => x, c => c.OrderBy(x => x));
Assert.Equal(new int?[] { int.MaxValue, 3, 2, 1, 0, int.MinValue, null }, result);
}
[Fact]
public void Contains_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4, 5 };
Assert.True(values.Contains(14, c => c.EquateBy(x => x % 13)));
}
[Fact]
public void Distinct_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4, 5 };
var result = values.Distinct(c => c.EquateBy(x => x % 3));
Assert.Equal(new int[] { 1, 2, 3 }, result);
}
[Fact]
public void Except_UsesComparer()
{
IEnumerable<int> values = new int[] { 3, 4, 5 };
var result = values.Except(new int[] { 1 }, c => c.EquateBy(x => x % 3));
Assert.Equal(new int[] { 3, 5 }, result);
}
[Fact]
public void GroupBy_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4, 5 };
var result = values.GroupBy(x => x, c => c.EquateBy(x => x % 3)).ToList();
Assert.Equal(3, result.Count);
Assert.Equal(new[] { 1, 4 }, result[0]);
Assert.Equal(new[] { 2, 5 }, result[1]);
Assert.Equal(new[] { 3 }, result[2]);
}
[Fact]
public void GroupBy_ElementSelector_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4, 5 };
var result = values.GroupBy(x => x, x => x, c => c.EquateBy(x => x % 3)).ToList();
Assert.Equal(3, result.Count);
Assert.Equal(new[] { 1, 4 }, result[0]);
Assert.Equal(new[] { 2, 5 }, result[1]);
Assert.Equal(new[] { 3 }, result[2]);
}
[Fact]
public void GroupBy_ResultSelector_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4, 5 };
var result = values.GroupBy(x => x, (key, source) => source.ToList(), c => c.EquateBy(x => x % 3)).ToList();
Assert.Equal(3, result.Count);
Assert.Equal(new[] { 1, 4 }, result[0]);
Assert.Equal(new[] { 2, 5 }, result[1]);
Assert.Equal(new[] { 3 }, result[2]);
}
[Fact]
public void GroupBy_ElementAndResultSelector_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4, 5 };
var result = values.GroupBy(x => x, x => x, (key, source) => source.ToList(), c => c.EquateBy(x => x % 3)).ToList();
Assert.Equal(3, result.Count);
Assert.Equal(new[] { 1, 4 }, result[0]);
Assert.Equal(new[] { 2, 5 }, result[1]);
Assert.Equal(new[] { 3 }, result[2]);
}
[Fact]
public void GroupJoin_UsesComparer()
{
IEnumerable<int> outer = new int[] { 0, 1, 2 };
IEnumerable<int> inner = new int[] { 1, 2, 3, 4, 5 };
var result = outer.GroupJoin(inner, x => x, x => x, (outerValue, innerValues) => innerValues.ToList(), c => c.EquateBy(x => x % 3)).ToList();
Assert.Equal(3, result.Count);
Assert.Equal(new[] { 3 }, result[0]);
Assert.Equal(new[] { 1, 4 }, result[1]);
Assert.Equal(new[] { 2, 5 }, result[2]);
}
[Fact]
public void Intersect_UsesComparer()
{
IEnumerable<int> first = new int[] { 0, 1 };
IEnumerable<int> second = new int[] { 1, 2, 3, 4, 5 };
var result = first.Intersect(second, c => c.EquateBy(x => x % 3));
Assert.Equal(new[] { 0, 1 }, result);
}
[Fact]
public void Join_UsesComparer()
{
IEnumerable<int> outer = new int[] { 0, 2 };
IEnumerable<int> inner = new int[] { 1, 2, 3, 4, 5 };
var result = outer.Join(inner, x => x, x => x, (outerValue, innerValue) => innerValue, c => c.EquateBy(x => x % 3)).ToList();
Assert.Equal(new[] { 3, 2, 5 }, result);
}
[Fact]
public void SequenceEqual_UsesComparer()
{
IEnumerable<int> first = new int[] { 0, 1, 2 };
IEnumerable<int> second = new int[] { 15, 4, 2 };
var result = first.SequenceEqual(second, c => c.EquateBy(x => x % 3));
Assert.True(result);
}
[Fact]
public void ToDictionary_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4 };
Assert.ThrowsAny<ArgumentException>(() => values.ToDictionary(x => x, c => c.EquateBy(x => x % 3)));
}
[Fact]
public void ToDictionary_ElementSelector_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4 };
Assert.ThrowsAny<ArgumentException>(() => values.ToDictionary(x => x, x => x, c => c.EquateBy(x => x % 3)));
}
[Fact]
public void ToHashSet_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4 };
var result = values.ToHashSet(c => c.EquateBy(x => x % 3));
Assert.Equal(new[] { 1, 2, 3 }, result.OrderBy(x => x));
}
[Fact]
public void ToLookup_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4 };
var result = values.ToLookup(x => x, c => c.EquateBy(x => x % 3));
Assert.Equal(new[] { 3 }, result[0]);
Assert.Equal(new[] { 1, 4 }, result[1]);
Assert.Equal(new[] { 2 }, result[2]);
}
[Fact]
public void ToLookup_ElementSelector_UsesComparer()
{
IEnumerable<int> values = new int[] { 1, 2, 3, 4 };
var result = values.ToLookup(x => x, x => x, c => c.EquateBy(x => x % 3));
Assert.Equal(new[] { 3 }, result[0]);
Assert.Equal(new[] { 1, 4 }, result[1]);
Assert.Equal(new[] { 2 }, result[2]);
}
[Fact]
public void Union_UsesComparer()
{
IEnumerable<int> first = new int[] { 0, 1 };
IEnumerable<int> second = new int[] { 1, 2, 3, 4, 5 };
var result = first.Union(second, c => c.EquateBy(x => x % 3));
Assert.Equal(new[] { 0, 1, 2 }, result);
}
}
}