-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ConcatTests.cs
155 lines (139 loc) · 7.28 KB
/
ConcatTests.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
// 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 ConcatTests
{
public static IEnumerable<object[]> ConcatUnorderedData(int[] counts)
{
foreach (int leftCount in counts)
{
foreach (int rightCount in counts)
{
yield return new object[] { leftCount, rightCount };
}
}
}
public static IEnumerable<object[]> ConcatData(int[] counts)
{
foreach (object[] parms in UnorderedSources.BinaryRanges(counts.DefaultIfEmpty(Sources.OuterLoopCount), (left, right) => left, counts))
{
yield return new object[] { ((Labeled<ParallelQuery<int>>)parms[0]).Order(), parms[1], ((Labeled<ParallelQuery<int>>)parms[2]).Order(), parms[3] };
yield return new object[] { ((Labeled<ParallelQuery<int>>)parms[0]).Order(), parms[1], parms[2], parms[3] };
yield return new object[] { parms[0], parms[1], ((Labeled<ParallelQuery<int>>)parms[2]).Order(), parms[3] };
}
}
//
// Concat
//
[Theory]
[MemberData(nameof(ConcatUnorderedData), new[] { 0, 1, 2, 16 })]
public static void Concat_Unordered(int leftCount, int rightCount)
{
IntegerRangeSet seen = new IntegerRangeSet(0, leftCount + rightCount);
foreach (int i in UnorderedSources.Default(0, leftCount).Concat(UnorderedSources.Default(leftCount, rightCount)))
{
seen.Add(i);
}
seen.AssertComplete();
}
[Fact]
[OuterLoop]
public static void Concat_Unordered_Longrunning()
{
Concat_Unordered(Sources.OuterLoopCount, Sources.OuterLoopCount);
}
[Theory]
[MemberData(nameof(ConcatData), new[] { 0, 1, 2, 16 })]
public static void Concat(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
// The ordering of Concat is only guaranteed when both operands are ordered,
// however the current implementation manages to perform ordering if either operand is ordered _in most cases_.
// If this test starts failing, consider revising the operators and mention the change in release notes.
ParallelQuery<int> leftQuery = left.Item;
ParallelQuery<int> rightQuery = right.Item;
int seen = 0;
foreach (int i in leftQuery.Concat(rightQuery))
{
Assert.Equal(seen++, i);
}
Assert.Equal(seen, leftCount + rightCount);
}
[Theory]
[OuterLoop]
[MemberData(nameof(ConcatData), new int[] { /* Sources.OuterLoopCount */ })]
public static void Concat_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
Concat(left, leftCount, right, rightCount);
}
[Theory]
[MemberData(nameof(ConcatUnorderedData), new[] { 0, 1, 2, 16 })]
public static void Concat_Unordered_NotPipelined(int leftCount, int rightCount)
{
IntegerRangeSet seen = new IntegerRangeSet(0, leftCount + rightCount);
Assert.All(UnorderedSources.Default(leftCount).Concat(UnorderedSources.Default(leftCount, rightCount)).ToList(), x => seen.Add(x));
seen.AssertComplete();
}
[Fact]
[OuterLoop]
public static void Concat_Unordered_NotPipelined_Longrunning()
{
Concat_Unordered_NotPipelined(Sources.OuterLoopCount, Sources.OuterLoopCount);
}
[Theory]
[MemberData(nameof(ConcatData), new[] { 0, 1, 2, 16 })]
public static void Concat_NotPipelined(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
// The ordering of Concat is only guaranteed when both operands are ordered,
// however the current implementation manages to perform ordering if either operand is ordered _in most cases_.
// If this test starts failing, consider revising the operators and mention the change in release notes.
ParallelQuery<int> leftQuery = left.Item;
ParallelQuery<int> rightQuery = right.Item;
int seen = 0;
Assert.All(leftQuery.Concat(rightQuery).ToList(), x => Assert.Equal(seen++, x));
Assert.Equal(seen, leftCount + rightCount);
}
[Theory]
[OuterLoop]
[MemberData(nameof(ConcatData), new int[] { /* Sources.OuterLoopCount */ })]
public static void Concat_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> left, int leftCount, Labeled<ParallelQuery<int>> right, int rightCount)
{
Concat_NotPipelined(left, leftCount, right, rightCount);
}
[Fact]
public static void Concat_NotSupportedException()
{
#pragma warning disable 618
Assert.Throws<NotSupportedException>(() => ParallelEnumerable.Range(0, 1).Concat(Enumerable.Range(0, 1)));
#pragma warning restore 618
}
[Fact]
// Should not get the same setting from both operands.
public static void Concat_NoDuplicateSettings()
{
CancellationToken t = new CancellationTokenSource().Token;
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithCancellation(t).Concat(ParallelEnumerable.Range(0, 1).WithCancellation(t)));
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithDegreeOfParallelism(1).Concat(ParallelEnumerable.Range(0, 1).WithDegreeOfParallelism(1)));
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithExecutionMode(ParallelExecutionMode.Default).Concat(ParallelEnumerable.Range(0, 1).WithExecutionMode(ParallelExecutionMode.Default)));
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithMergeOptions(ParallelMergeOptions.Default).Concat(ParallelEnumerable.Range(0, 1).WithMergeOptions(ParallelMergeOptions.Default)));
}
[Fact]
public static void Concat_ArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("first", () => ((ParallelQuery<int>)null).Concat(ParallelEnumerable.Range(0, 1)));
AssertExtensions.Throws<ArgumentNullException>("second", () => ParallelEnumerable.Range(0, 1).Concat(null));
}
[Fact]
public static void Concat_UnionSources_PrematureMerges()
{
const int ElementCount = 2048;
ParallelQuery<int> leftQuery = ParallelEnumerable.Range(0, ElementCount / 4).Union(ParallelEnumerable.Range(ElementCount / 4, ElementCount / 4));
ParallelQuery<int> rightQuery = ParallelEnumerable.Range(2 * ElementCount / 4, ElementCount / 4).Union(ParallelEnumerable.Range(3 * ElementCount / 4, ElementCount / 4));
var results = new HashSet<int>(leftQuery.Concat(rightQuery));
Assert.Equal(ElementCount, results.Count);
}
}
}