-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
TakeTakeWhileTests.cs
373 lines (333 loc) · 15.3 KB
/
TakeTakeWhileTests.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
// 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 Xunit;
namespace System.Linq.Parallel.Tests
{
public static class TakeTakeWhileTests
{
private static readonly Func<int, IEnumerable<int>> TakePosition = x => new[] { -x, -1, 0, 1, x / 2, x, x * 2 }.Distinct();
//
// Take
//
public static IEnumerable<object[]> TakeUnorderedData(int[] counts)
{
foreach (int count in counts.DefaultIfEmpty(Sources.OuterLoopCount / 4))
{
foreach (int position in TakePosition(count))
{
yield return new object[] { count, position };
}
}
}
public static IEnumerable<object[]> TakeData(int[] counts)
{
foreach (object[] results in Sources.Ranges(counts.DefaultIfEmpty(Sources.OuterLoopCount / 4), TakePosition)) yield return results;
}
[Theory]
[MemberData(nameof(TakeUnorderedData), new[] { 0, 1, 2, 16 })]
public static void Take_Unordered(int count, int take)
{
// For unordered collections, which elements (if any) are taken isn't actually guaranteed, but an effect of the implementation.
// If this test starts failing it should be updated, and possibly mentioned in release notes.
IntegerRangeSet seen = new IntegerRangeSet(0, Math.Min(count, Math.Max(0, take)));
foreach (int i in UnorderedSources.Default(count).Take(take))
{
seen.Add(i);
}
seen.AssertComplete();
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeUnorderedData), new int[] { /* Sources.OuterLoopCount */ })]
public static void Take_Unordered_Longrunning(int count, int take)
{
Take_Unordered(count, take);
}
[Theory]
[MemberData(nameof(TakeData), new[] { 0, 1, 2, 16 })]
public static void Take(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
ParallelQuery<int> query = labeled.Item;
int seen = 0;
foreach (int i in query.Take(take))
{
Assert.Equal(seen++, i);
}
Assert.Equal(Math.Min(count, Math.Max(0, take)), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeData), new int[] { /* Sources.OuterLoopCount */ })]
public static void Take_Longrunning(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
Take(labeled, count, take);
}
[Theory]
[MemberData(nameof(TakeUnorderedData), new[] { 0, 1, 2, 16 })]
public static void Take_Unordered_NotPipelined(int count, int take)
{
// For unordered collections, which elements (if any) are taken isn't actually guaranteed, but an effect of the implementation.
// If this test starts failing it should be updated, and possibly mentioned in release notes.
IntegerRangeSet seen = new IntegerRangeSet(0, Math.Min(count, Math.Max(0, take)));
Assert.All(UnorderedSources.Default(count).Take(take).ToList(), x => seen.Add(x));
seen.AssertComplete();
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeUnorderedData), new int[] { /* Sources.OuterLoopCount */ })]
public static void Take_Unordered_NotPipelined_Longrunning(int count, int take)
{
Take_Unordered_NotPipelined(count, take);
}
[Theory]
[MemberData(nameof(TakeData), new[] { 0, 1, 2, 16 })]
public static void Take_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
ParallelQuery<int> query = labeled.Item;
int seen = 0;
Assert.All(query.Take(take).ToList(), x => Assert.Equal(seen++, x));
Assert.Equal(Math.Min(count, Math.Max(0, take)), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeData), new int[] { /* Sources.OuterLoopCount */ })]
public static void Take_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
Take_NotPipelined(labeled, count, take);
}
[Fact]
public static void Take_ArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<bool>)null).Take(0));
}
//
// TakeWhile
//
public static IEnumerable<object[]> TakeWhileData(int[] counts)
{
foreach (object[] results in Sources.Ranges(counts.DefaultIfEmpty(Sources.OuterLoopCount / 4)))
{
yield return new[] { results[0], results[1], new[] { 0 } };
yield return new[] { results[0], results[1], Enumerable.Range((int)results[1] / 2, ((int)results[1] - 1) / 2 + 1).ToArray() };
yield return new[] { results[0], results[1], new[] { (int)results[1] - 1 } };
}
}
[Theory]
[MemberData(nameof(TakeUnorderedData), new[] { 0, 1, 2, 16 })]
public static void TakeWhile_Unordered(int count, int take)
{
// For unordered collections, which elements (if any) are taken isn't actually guaranteed, but an effect of the implementation.
// If this test starts failing it should be updated, and possibly mentioned in release notes.
IntegerRangeSet seen = new IntegerRangeSet(0, Math.Min(count, Math.Max(0, take)));
foreach (int i in UnorderedSources.Default(count).TakeWhile(x => x < take))
{
seen.Add(i);
}
seen.AssertComplete();
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeUnorderedData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_Unordered_Longrunning(int count, int take)
{
TakeWhile_Unordered(count, take);
}
[Theory]
[MemberData(nameof(TakeData), new[] { 0, 1, 2, 16 })]
public static void TakeWhile(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
ParallelQuery<int> query = labeled.Item;
int seen = 0;
foreach (int i in query.TakeWhile(x => x < take))
{
Assert.Equal(seen++, i);
}
Assert.Equal(Math.Min(count, Math.Max(0, take)), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_Longrunning(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
TakeWhile(labeled, count, take);
}
[Theory]
[MemberData(nameof(TakeUnorderedData), new[] { 0, 1, 2, 16 })]
public static void TakeWhile_Unordered_NotPipelined(int count, int take)
{
// For unordered collections, which elements (if any) are taken isn't actually guaranteed, but an effect of the implementation.
// If this test starts failing it should be updated, and possibly mentioned in release notes.
IntegerRangeSet seen = new IntegerRangeSet(0, Math.Min(count, Math.Max(0, take)));
Assert.All(UnorderedSources.Default(count).TakeWhile(x => x < take).ToList(), x => seen.Add(x));
seen.AssertComplete();
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeUnorderedData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_Unordered_NotPipelined_Longrunning(int count, int take)
{
TakeWhile_Unordered_NotPipelined(count, take);
}
[Theory]
[MemberData(nameof(TakeData), new[] { 0, 1, 2, 16 })]
public static void TakeWhile_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
ParallelQuery<int> query = labeled.Item;
int seen = 0;
Assert.All(query.TakeWhile(x => x < take).ToList(), x => Assert.Equal(seen++, x));
Assert.Equal(Math.Min(count, Math.Max(0, take)), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
TakeWhile_NotPipelined(labeled, count, take);
}
[Theory]
[MemberData(nameof(TakeUnorderedData), new[] { 0, 1, 2, 16 })]
public static void TakeWhile_Indexed_Unordered(int count, int take)
{
// For unordered collections, which elements (if any) are taken isn't actually guaranteed, but an effect of the implementation.
// If this test starts failing it should be updated, and possibly mentioned in release notes.
IntegerRangeSet seen = new IntegerRangeSet(0, Math.Min(count, Math.Max(0, take)));
foreach (int i in UnorderedSources.Default(count).TakeWhile((x, index) => index < take))
{
seen.Add(i);
}
seen.AssertComplete();
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeUnorderedData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_Indexed_Unordered_Longrunning(int count, int take)
{
TakeWhile_Indexed_Unordered(count, take);
}
[Theory]
[MemberData(nameof(TakeData), new[] { 0, 1, 2, 16 })]
public static void TakeWhile_Indexed(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
ParallelQuery<int> query = labeled.Item;
int seen = 0;
foreach (int i in query.TakeWhile((x, index) => index < take))
{
Assert.Equal(seen++, i);
}
Assert.Equal(Math.Min(count, Math.Max(0, take)), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_Indexed_Longrunning(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
TakeWhile_Indexed(labeled, count, take);
}
[Theory]
[MemberData(nameof(TakeUnorderedData), new[] { 0, 1, 2, 16 })]
public static void TakeWhile_Indexed_Unordered_NotPipelined(int count, int take)
{
// For unordered collections, which elements (if any) are taken isn't actually guaranteed, but an effect of the implementation.
// If this test starts failing it should be updated, and possibly mentioned in release notes.
IntegerRangeSet seen = new IntegerRangeSet(0, Math.Min(count, Math.Max(0, take)));
Assert.All(UnorderedSources.Default(count).TakeWhile((x, index) => index < take).ToList(), x => seen.Add(x));
seen.AssertComplete();
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeUnorderedData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_Indexed_Unordered_NotPipelined_Longrunning(int count, int take)
{
TakeWhile_Indexed_Unordered_NotPipelined(count, take);
}
[Theory]
[MemberData(nameof(TakeData), new[] { 0, 1, 2, 16 })]
public static void TakeWhile_Indexed_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
ParallelQuery<int> query = labeled.Item;
int seen = 0;
Assert.All(query.TakeWhile((x, index) => index < take).ToList(), x => Assert.Equal(seen++, x));
Assert.Equal(Math.Min(count, Math.Max(0, take)), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_Indexed_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
TakeWhile_Indexed_NotPipelined(labeled, count, take);
}
[Theory]
[MemberData(nameof(TakeUnorderedData), new[] { 0, 1, 2, 16 })]
public static void TakeWhile_AllFalse(int count, int take)
{
_ = take;
Assert.Empty(UnorderedSources.Default(count).TakeWhile(x => false));
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeUnorderedData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_AllFalse_Longrunning(int count, int take)
{
TakeWhile_AllFalse(count, take);
}
[Theory]
[MemberData(nameof(TakeData), new[] { 0, 1, 2, 16 })]
public static void TakeWhile_AllTrue(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
_ = take;
ParallelQuery<int> query = labeled.Item;
int seen = 0;
Assert.All(query.TakeWhile(x => true), x => Assert.Equal(seen++, x));
Assert.Equal(count, seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_AllTrue_Longrunning(Labeled<ParallelQuery<int>> labeled, int count, int take)
{
TakeWhile_AllTrue(labeled, count, take);
}
[Theory]
[MemberData(nameof(TakeWhileData), new[] { 2, 16 })]
public static void TakeWhile_SomeTrue(Labeled<ParallelQuery<int>> labeled, int count, int[] take)
{
_ = count;
ParallelQuery<int> query = labeled.Item;
int seen = 0;
Assert.All(query.TakeWhile(x => take.Contains(x)), x => Assert.Equal(seen++, x));
Assert.Equal(take.Min() > 0 ? 0 : take.Max() + 1, seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeWhileData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_SomeTrue_Longrunning(Labeled<ParallelQuery<int>> labeled, int count, int[] take)
{
TakeWhile_SomeTrue(labeled, count, take);
}
[Theory]
[MemberData(nameof(TakeWhileData), new[] { 2, 16 })]
public static void TakeWhile_SomeFalse(Labeled<ParallelQuery<int>> labeled, int count, int[] take)
{
_ = count;
ParallelQuery<int> query = labeled.Item;
int seen = 0;
Assert.All(query.TakeWhile(x => !take.Contains(x)), x => Assert.Equal(seen++, x));
Assert.Equal(take.Min(), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(TakeWhileData), new int[] { /* Sources.OuterLoopCount */ })]
public static void TakeWhile_SomeFalse_Longrunning(Labeled<ParallelQuery<int>> labeled, int count, int[] take)
{
TakeWhile_SomeFalse(labeled, count, take);
}
[Fact]
public static void TakeWhile_ArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<bool>)null).TakeWhile(x => true));
AssertExtensions.Throws<ArgumentNullException>("predicate", () => ParallelEnumerable.Empty<bool>().TakeWhile((Func<bool, bool>)null));
AssertExtensions.Throws<ArgumentNullException>("predicate", () => ParallelEnumerable.Empty<bool>().TakeWhile((Func<bool, int, bool>)null));
}
}
}