-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
JoinTests.cs
546 lines (488 loc) · 27.4 KB
/
JoinTests.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// 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.Threading;
using Xunit;
namespace System.Linq.Parallel.Tests
{
public static class JoinTests
{
private const int KeyFactor = 8;
public static IEnumerable<object[]> JoinUnorderedData(int[] counts)
{
foreach (int leftCount in counts)
{
foreach (int rightCount in counts)
{
yield return new object[] { leftCount, rightCount };
}
}
}
public static IEnumerable<object[]> JoinData(int[] counts)
{
// When dealing with joins, if there aren't multiple matches the ordering of the second operand is immaterial.
foreach (object[] parms in Sources.Ranges(counts, i => counts))
{
yield return parms;
}
}
public static IEnumerable<object[]> JoinMultipleData(int[] counts)
{
foreach (object[] parms in UnorderedSources.BinaryRanges(counts, counts))
{
yield return new object[] { ((Labeled<ParallelQuery<int>>)parms[0]).Order(), parms[1], ((Labeled<ParallelQuery<int>>)parms[2]).Order(), parms[3] };
}
}
public static IEnumerable<object[]> JoinOrderedLeftUnorderedRightData(int[] counts)
{
foreach (object[] parms in UnorderedSources.BinaryRanges(counts, counts))
{
yield return new object[] { ((Labeled<ParallelQuery<int>>)parms[0]).Order(), parms[1], ((Labeled<ParallelQuery<int>>)parms[2]), parms[3] };
}
}
//
// Join
//
[Theory]
[MemberData(nameof(JoinUnorderedData), new[] { 0, 1, 2, KeyFactor * 2 })]
public static void Join_Unordered(int leftCount, int rightCount)
{
ParallelQuery<int> leftQuery = UnorderedSources.Default(leftCount);
ParallelQuery<int> rightQuery = UnorderedSources.Default(rightCount);
IntegerRangeSet seen = new IntegerRangeSet(0, Math.Min(leftCount, (rightCount + (KeyFactor - 1)) / KeyFactor));
foreach (var p in leftQuery.Join(rightQuery, x => x * KeyFactor, y => y, (x, y) => KeyValuePair.Create(x, y)))
{
Assert.Equal(p.Key * KeyFactor, p.Value);
seen.Add(p.Key);
}
seen.AssertComplete();
}
[Fact]
[OuterLoop]
public static void Join_Unordered_Longrunning()
{
Join_Unordered(Sources.OuterLoopCount / 64, Sources.OuterLoopCount / 64);
}
[Theory]
[MemberData(nameof(JoinData), new[] { 0, 1, 2, KeyFactor * 2 })]
public static void Join(Labeled<ParallelQuery<int>> left, int leftCount, int rightCount)
{
ParallelQuery<int> leftQuery = left.Item;
ParallelQuery<int> rightQuery = UnorderedSources.Default(rightCount);
int seen = 0;
foreach (var p in leftQuery.Join(rightQuery, x => x * KeyFactor, y => y, (x, y) => KeyValuePair.Create(x, y)))
{
Assert.Equal(seen++, p.Key);
Assert.Equal(p.Key * KeyFactor, p.Value);
}
Assert.Equal(Math.Min(leftCount, (rightCount + (KeyFactor - 1)) / KeyFactor), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(JoinData), new[] { 512, 1024 })]
public static void Join_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, int rightCount)
{
Join(left, leftCount, rightCount);
}
[Theory]
[MemberData(nameof(JoinUnorderedData), new[] { 0, 1, 2, KeyFactor * 2 })]
public static void Join_Unordered_NotPipelined(int leftCount, int rightCount)
{
ParallelQuery<int> leftQuery = UnorderedSources.Default(leftCount);
ParallelQuery<int> rightQuery = UnorderedSources.Default(rightCount);
IntegerRangeSet seen = new IntegerRangeSet(0, Math.Min(leftCount, (rightCount + (KeyFactor - 1)) / KeyFactor));
Assert.All(leftQuery.Join(rightQuery, x => x * KeyFactor, y => y, (x, y) => KeyValuePair.Create(x, y)).ToList(),
p => { Assert.Equal(p.Key * KeyFactor, p.Value); seen.Add(p.Key); });
seen.AssertComplete();
}
[Fact]
[OuterLoop]
public static void Join_Unordered_NotPipelined_Longrunning()
{
Join_Unordered_NotPipelined(Sources.OuterLoopCount / 64, Sources.OuterLoopCount / 64);
}
[Theory]
[MemberData(nameof(JoinData), new[] { 0, 1, 2, KeyFactor * 2 })]
public static void Join_NotPipelined(Labeled<ParallelQuery<int>> left, int leftCount, int rightCount)
{
ParallelQuery<int> leftQuery = left.Item;
ParallelQuery<int> rightQuery = UnorderedSources.Default(rightCount);
int seen = 0;
Assert.All(leftQuery.Join(rightQuery, x => x * KeyFactor, y => y, (x, y) => KeyValuePair.Create(x, y)).ToList(),
p => { Assert.Equal(seen++, p.Key); Assert.Equal(p.Key * KeyFactor, p.Value); });
Assert.Equal(Math.Min(leftCount, (rightCount + (KeyFactor - 1)) / KeyFactor), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(JoinData), new[] { 512, 1024 })]
public static void Join_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, int rightCount)
{
Join_NotPipelined(left, leftCount, rightCount);
}
[Theory]
[MemberData(nameof(JoinUnorderedData), new[] { 0, 1, 2, KeyFactor * 2 })]
public static void Join_Unordered_Multiple(int leftCount, int rightCount)
{
ParallelQuery<int> leftQuery = UnorderedSources.Default(leftCount);
ParallelQuery<int> rightQuery = UnorderedSources.Default(rightCount);
IntegerRangeSet seenOuter = new IntegerRangeSet(0, Math.Min(leftCount, (rightCount + (KeyFactor - 1)) / KeyFactor));
IntegerRangeSet seenInner = new IntegerRangeSet(0, Math.Min(leftCount * KeyFactor, rightCount));
Assert.All(leftQuery.Join(rightQuery, x => x, y => y / KeyFactor, (x, y) => KeyValuePair.Create(x, y)),
p =>
{
Assert.Equal(p.Key, p.Value / KeyFactor);
seenInner.Add(p.Value);
if (p.Value % KeyFactor == 0) seenOuter.Add(p.Key);
});
seenOuter.AssertComplete();
seenInner.AssertComplete();
}
[Fact]
[OuterLoop]
public static void Join_Unordered_Multiple_Longrunning()
{
Join_Unordered_Multiple(Sources.OuterLoopCount / 64, Sources.OuterLoopCount / 64);
}
[Theory]
[MemberData(nameof(JoinMultipleData), new[] { 0, 1, 2, KeyFactor * 2 })]
public static void Join_Multiple(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
ParallelQuery<int> leftQuery = left.Item;
ParallelQuery<int> rightQuery = right.Item;
int seen = 0;
Assert.All(leftQuery.Join(rightQuery, x => x, y => y / KeyFactor, (x, y) => KeyValuePair.Create(x, y)),
p =>
{
Assert.Equal(p.Key, p.Value / KeyFactor);
Assert.Equal(seen++, p.Value);
});
Assert.Equal(Math.Min(leftCount * KeyFactor, rightCount), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(JoinMultipleData), new[] { 512, 1024 })]
public static void Join_Multiple_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
Join_Multiple(left, leftCount, right, rightCount);
}
private class LeftOrderingCollisionTestWithOrderedRight : LeftOrderingCollisionTest
{
protected override ParallelQuery<KeyValuePair<int, int>> Join(ParallelQuery<int> left, ParallelQuery<int> right)
{
return ReorderLeft(left).Join(right, x => x, y => y % KeyFactor, (x, y) => KeyValuePair.Create(x, y));
}
protected override void ValidateRightValue(int left, int right, int seenRightCount)
{
Assert.Equal(left, right % KeyFactor);
Assert.Equal(left + seenRightCount * KeyFactor, right);
}
protected override int GetExpectedSeenLeftCount(int leftCount, int rightCount)
{
return Math.Min(KeyFactor, Math.Min(rightCount, leftCount));
}
}
[Theory]
[MemberData(nameof(JoinMultipleData), new[] { 2, KeyFactor - 1, KeyFactor, KeyFactor + 1, KeyFactor * 2 - 1, KeyFactor * 2, KeyFactor * 2 + 1 })]
public static void Join_Multiple_LeftWithOrderingColisions(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
LeftOrderingCollisionTestWithOrderedRight validator = new LeftOrderingCollisionTestWithOrderedRight();
validator.Validate(left.Item, leftCount, right.Item, rightCount);
}
[Theory]
[OuterLoop]
[MemberData(nameof(JoinMultipleData), new[] { 512, 1024 })]
public static void Join_Multiple_LeftWithOrderingColisions_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
Join_Multiple_LeftWithOrderingColisions(left, leftCount, right, rightCount);
}
private class LeftOrderingCollisionTestWithUnorderedRight : LeftOrderingCollisionTest
{
protected override ParallelQuery<KeyValuePair<int, int>> Join(ParallelQuery<int> left, ParallelQuery<int> right)
{
return ReorderLeft(left).Join(right, x => x, y => y % KeyFactor, (x, y) => KeyValuePair.Create(x, y)).Distinct();
}
protected override void ValidateRightValue(int left, int right, int seenRightCount)
{
Assert.Equal(left, right % KeyFactor);
}
protected override int GetExpectedSeenLeftCount(int leftCount, int rightCount)
{
return Math.Min(KeyFactor, Math.Min(rightCount, leftCount));
}
}
[Theory]
[MemberData(nameof(JoinOrderedLeftUnorderedRightData), new[] { 2, KeyFactor - 1, KeyFactor, KeyFactor + 1, KeyFactor * 2 - 1, KeyFactor * 2, KeyFactor * 2 + 1 })]
public static void Join_Multiple_LeftWithOrderingColisions_UnorderedRight(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
LeftOrderingCollisionTestWithUnorderedRight validator = new LeftOrderingCollisionTestWithUnorderedRight();
validator.Validate(left.Item, leftCount, right.Item, rightCount);
}
[Theory]
[OuterLoop]
[MemberData(nameof(JoinOrderedLeftUnorderedRightData), new[] { 256, 512 })]
public static void Join_Multiple_LeftWithOrderingColisions_UnorderedRight_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
Join_Multiple_LeftWithOrderingColisions_UnorderedRight(left, leftCount, right, rightCount);
}
[Theory]
[MemberData(nameof(JoinUnorderedData), new[] { 0, 1, 2, KeyFactor * 2 })]
public static void Join_Unordered_CustomComparator(int leftCount, int rightCount)
{
ParallelQuery<int> leftQuery = UnorderedSources.Default(leftCount);
ParallelQuery<int> rightQuery = UnorderedSources.Default(rightCount);
IntegerRangeCounter seenOuter = new IntegerRangeCounter(0, leftCount);
IntegerRangeCounter seenInner = new IntegerRangeCounter(0, rightCount);
Assert.All(leftQuery.Join(rightQuery, x => x, y => y,
(x, y) => KeyValuePair.Create(x, y), new ModularCongruenceComparer(KeyFactor)),
p =>
{
Assert.Equal(p.Key % KeyFactor, p.Value % KeyFactor);
seenOuter.Add(p.Key);
seenInner.Add(p.Value);
});
if (leftCount == 0 || rightCount == 0)
{
seenOuter.AssertEncountered(0);
seenInner.AssertEncountered(0);
}
else
{
int Cartesian(int key, int other) => (other + (KeyFactor - 1) - key % KeyFactor) / KeyFactor;
Assert.All(seenOuter, kv => Assert.Equal(Cartesian(kv.Key, rightCount), kv.Value));
Assert.All(seenInner, kv => Assert.Equal(Cartesian(kv.Key, leftCount), kv.Value));
}
}
[Fact]
[OuterLoop]
public static void Join_Unordered_CustomComparator_Longrunning()
{
Join_Unordered_CustomComparator(Sources.OuterLoopCount / 64, Sources.OuterLoopCount / 64);
}
[Theory]
[MemberData(nameof(JoinMultipleData), new[] { 0, 1, 2, KeyFactor * 2 })]
public static void Join_CustomComparator(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
ParallelQuery<int> leftQuery = left.Item;
ParallelQuery<int> rightQuery = right.Item;
int seenOuter = 0;
int previousOuter = -1;
int seenInner = Math.Max(previousOuter % KeyFactor, rightCount - KeyFactor + previousOuter % KeyFactor);
Assert.All(leftQuery.Join(rightQuery, x => x, y => y,
(x, y) => KeyValuePair.Create(x, y), new ModularCongruenceComparer(KeyFactor)),
p =>
{
if (p.Key != previousOuter)
{
Assert.Equal(seenOuter, p.Key);
Assert.Equal(p.Key % 8, p.Value);
// If there aren't sufficient elements in the RHS (< 8), the LHS skips an entry at the end of the mod cycle.
seenOuter = Math.Min(leftCount, seenOuter + (p.Key % KeyFactor + 1 == rightCount ? KeyFactor - p.Key % KeyFactor : 1));
Assert.Equal(Math.Max(previousOuter % KeyFactor, rightCount - KeyFactor + previousOuter % KeyFactor), seenInner);
previousOuter = p.Key;
seenInner = (p.Key % KeyFactor) - KeyFactor;
}
Assert.Equal(p.Key % KeyFactor, p.Value % KeyFactor);
Assert.Equal(seenInner += KeyFactor, p.Value);
});
Assert.Equal(rightCount == 0 ? 0 : leftCount, seenOuter);
Assert.Equal(Math.Max(previousOuter % KeyFactor, rightCount - KeyFactor + previousOuter % KeyFactor), seenInner);
}
[Theory]
[OuterLoop]
[MemberData(nameof(JoinMultipleData), new[] { 512, 1024 })]
public static void Join_CustomComparator_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
Join_CustomComparator(left, leftCount, right, rightCount);
}
private class LeftOrderingCollisionTestWithOrderedRightAndCustomComparator : LeftOrderingCollisionTest
{
protected override ParallelQuery<KeyValuePair<int, int>> Join(ParallelQuery<int> left, ParallelQuery<int> right)
{
return ReorderLeft(left).Join(right, x => x, y => y, (x, y) => KeyValuePair.Create(x, y), new ModularCongruenceComparer(KeyFactor));
}
protected override void ValidateRightValue(int left, int right, int seenRightCount)
{
Assert.Equal(left % KeyFactor, right % KeyFactor);
Assert.Equal(left % KeyFactor + seenRightCount * KeyFactor, right);
}
protected override int GetExpectedSeenLeftCount(int leftCount, int rightCount)
{
if (rightCount >= KeyFactor)
{
return leftCount;
}
else {
return leftCount / KeyFactor * rightCount + Math.Min(leftCount % KeyFactor, rightCount);
}
}
}
[Theory]
[MemberData(nameof(JoinMultipleData), new[] { 2, KeyFactor - 1, KeyFactor, KeyFactor + 1, KeyFactor * 2 - 1, KeyFactor * 2, KeyFactor * 2 + 1 })]
public static void Join_CustomComparator_LeftWithOrderingColisions(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
LeftOrderingCollisionTestWithOrderedRightAndCustomComparator validator = new LeftOrderingCollisionTestWithOrderedRightAndCustomComparator();
validator.Validate(left.Item, leftCount, right.Item, rightCount);
}
[Theory]
[OuterLoop]
[MemberData(nameof(JoinMultipleData), new[] { 512, 1024 })]
public static void Join_CustomComparator_LeftWithOrderingColisions_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
Join_CustomComparator_LeftWithOrderingColisions(left, leftCount, right, rightCount);
}
private class LeftOrderingCollisionTestWithUnorderedRightAndCustomComparator : LeftOrderingCollisionTest
{
protected override ParallelQuery<KeyValuePair<int, int>> Join(ParallelQuery<int> left, ParallelQuery<int> right)
{
return ReorderLeft(left).Join(right, x => x, y => y, (x, y) => KeyValuePair.Create(x, y), new ModularCongruenceComparer(KeyFactor)).Distinct();
}
protected override void ValidateRightValue(int left, int right, int seenRightCount)
{
Assert.Equal(left % KeyFactor, right % KeyFactor);
}
protected override int GetExpectedSeenLeftCount(int leftCount, int rightCount)
{
if (rightCount >= KeyFactor)
{
return leftCount;
}
else
{
return leftCount / KeyFactor * rightCount + Math.Min(leftCount % KeyFactor, rightCount);
}
}
}
[Theory]
[MemberData(nameof(JoinOrderedLeftUnorderedRightData), new[] { 2, KeyFactor - 1, KeyFactor, KeyFactor + 1, KeyFactor * 2 - 1, KeyFactor * 2, KeyFactor * 2 + 1 })]
public static void Join_CustomComparator_LeftWithOrderingColisions_UnorderedRight(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
LeftOrderingCollisionTestWithUnorderedRightAndCustomComparator validator = new LeftOrderingCollisionTestWithUnorderedRightAndCustomComparator();
validator.Validate(left.Item, leftCount, right.Item, rightCount);
}
[Theory]
[OuterLoop]
[MemberData(nameof(JoinOrderedLeftUnorderedRightData), new[] { 256 })]
public static void Join_CustomComparator_LeftWithOrderingColisions_UnorderedRight_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
Join_CustomComparator_LeftWithOrderingColisions_UnorderedRight(left, leftCount, right, rightCount);
}
[Theory]
[MemberData(nameof(JoinData), new[] { 0, 1, 2, KeyFactor * 2 })]
public static void Join_InnerJoin_Ordered(Labeled<ParallelQuery<int>> left, int leftCount, int rightCount)
{
ParallelQuery<int> leftQuery = left.Item;
ParallelQuery<int> rightQuery = UnorderedSources.Default(rightCount);
ParallelQuery<int> middleQuery = ParallelEnumerable.Range(0, leftCount).AsOrdered();
int seen = 0;
Assert.All(leftQuery.Join(middleQuery.Join(rightQuery, x => x * KeyFactor / 2, y => y, (x, y) => KeyValuePair.Create(x, y)),
z => z * 2, p => p.Key, (x, p) => KeyValuePair.Create(x, p)),
pOuter =>
{
KeyValuePair<int, int> pInner = pOuter.Value;
Assert.Equal(seen++, pOuter.Key);
Assert.Equal(pOuter.Key * 2, pInner.Key);
Assert.Equal(pOuter.Key * KeyFactor, pInner.Value);
});
Assert.Equal(Math.Min((leftCount + 1) / 2, (rightCount + (KeyFactor - 1)) / KeyFactor), seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(JoinData), new[] { 512, 1024 })]
public static void Join_InnerJoin_Ordered_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, int rightCount)
{
Join_InnerJoin_Ordered(left, leftCount, rightCount);
}
[Fact]
public static void Join_NotSupportedException()
{
#pragma warning disable 618
Assert.Throws<NotSupportedException>(() => ParallelEnumerable.Range(0, 1).Join(Enumerable.Range(0, 1), i => i, i => i, (i, j) => i));
Assert.Throws<NotSupportedException>(() => ParallelEnumerable.Range(0, 1).Join(Enumerable.Range(0, 1), i => i, i => i, (i, j) => i, null));
#pragma warning restore 618
}
[Fact]
// Should not get the same setting from both operands.
public static void Join_NoDuplicateSettings()
{
CancellationToken t = new CancellationTokenSource().Token;
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithCancellation(t).Join(ParallelEnumerable.Range(0, 1).WithCancellation(t), x => x, y => y, (l, r) => l));
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithDegreeOfParallelism(1).Join(ParallelEnumerable.Range(0, 1).WithDegreeOfParallelism(1), x => x, y => y, (l, r) => l));
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithExecutionMode(ParallelExecutionMode.Default).Join(ParallelEnumerable.Range(0, 1).WithExecutionMode(ParallelExecutionMode.Default), x => x, y => y, (l, r) => l));
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithMergeOptions(ParallelMergeOptions.Default).Join(ParallelEnumerable.Range(0, 1).WithMergeOptions(ParallelMergeOptions.Default), x => x, y => y, (l, r) => l));
}
[Fact]
public static void Join_ArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("outer", () => ((ParallelQuery<int>)null).Join(ParallelEnumerable.Range(0, 1), i => i, i => i, (i, j) => i));
AssertExtensions.Throws<ArgumentNullException>("inner", () => ParallelEnumerable.Range(0, 1).Join((ParallelQuery<int>)null, i => i, i => i, (i, j) => i));
AssertExtensions.Throws<ArgumentNullException>("outerKeySelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), (Func<int, int>)null, i => i, (i, j) => i));
AssertExtensions.Throws<ArgumentNullException>("innerKeySelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), i => i, (Func<int, int>)null, (i, j) => i));
AssertExtensions.Throws<ArgumentNullException>("resultSelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), i => i, i => i, (Func<int, int, int>)null));
AssertExtensions.Throws<ArgumentNullException>("outer", () => ((ParallelQuery<int>)null).Join(ParallelEnumerable.Range(0, 1), i => i, i => i, (i, j) => i, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentNullException>("inner", () => ParallelEnumerable.Range(0, 1).Join((ParallelQuery<int>)null, i => i, i => i, (i, j) => i, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentNullException>("outerKeySelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), (Func<int, int>)null, i => i, (i, j) => i, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentNullException>("innerKeySelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), i => i, (Func<int, int>)null, (i, j) => i, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentNullException>("resultSelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), i => i, i => i, (Func<int, int, int>)null, EqualityComparer<int>.Default));
}
private abstract class LeftOrderingCollisionTest
{
protected ParallelQuery<int> ReorderLeft(ParallelQuery<int> left)
{
return left.AsUnordered().OrderBy(x => x % 2);
}
protected abstract ParallelQuery<KeyValuePair<int, int>> Join(ParallelQuery<int> left, ParallelQuery<int> right);
protected abstract void ValidateRightValue(int left, int right, int seenRightCount);
protected abstract int GetExpectedSeenLeftCount(int leftCount, int rightCount);
public void Validate(ParallelQuery<int> left, int leftCount, ParallelQuery<int> right, int rightCount)
{
HashSet<int> seenLeft = new HashSet<int>();
HashSet<int> seenRight = new HashSet<int>();
int currentLeft = -1;
bool seenOdd = false;
Assert.All(Join(left, right),
p =>
{
try
{
if (currentLeft != p.Key)
{
try
{
if (p.Key % 2 == 1)
{
seenOdd = true;
}
else
{
Assert.False(seenOdd, "Key out of order! " + p.Key.ToString());
}
Assert.True(seenLeft.Add(p.Key), "Key already seen! " + p.Key.ToString());
if (currentLeft != -1)
{
Assert.Equal((rightCount / KeyFactor) + (((rightCount % KeyFactor) > (currentLeft % KeyFactor)) ? 1 : 0), seenRight.Count);
}
}
finally
{
currentLeft = p.Key;
seenRight.Clear();
}
}
ValidateRightValue(p.Key, p.Value, seenRight.Count);
Assert.True(seenRight.Add(p.Value), "Value already seen! " + p.Value.ToString());
}
catch (Exception ex)
{
throw new Exception(string.Format("Key: {0}, Value: {1}", p.Key, p.Value), ex);
}
finally
{
seenRight.Add(p.Value);
}
});
Assert.Equal((rightCount / KeyFactor) + (((rightCount % KeyFactor) > (currentLeft % KeyFactor)) ? 1 : 0), seenRight.Count);
Assert.Equal(GetExpectedSeenLeftCount(leftCount, rightCount), seenLeft.Count);
}
}
}
}