-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DefaultIfEmptyTests.cs
146 lines (131 loc) · 5.21 KB
/
DefaultIfEmptyTests.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
// 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 DefaultIfEmptyTests
{
public static IEnumerable<object[]> EmptyData()
{
foreach (object[] query in UnorderedSources.Ranges(new[] { 0 }))
{
yield return new object[] { query[0], 1 };
}
foreach (object[] query in Sources.Ranges(new[] { 0 }))
{
yield return new object[] { query[0], 1 };
}
yield return new object[] { Labeled.Label("Empty-Int", ParallelEnumerable.Empty<int>()), 1 };
yield return new object[] { Labeled.Label("Empty-Decimal", ParallelEnumerable.Empty<decimal>()), 1.5M };
yield return new object[] { Labeled.Label("Empty-String", ParallelEnumerable.Empty<string>()), "default" };
}
//
// DefaultIfEmpty
//
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void DefaultIfEmpty_Unordered_NotEmpty(int count)
{
IntegerRangeSet seen = new IntegerRangeSet(0, count);
foreach (int i in UnorderedSources.Default(count).DefaultIfEmpty())
{
seen.Add(i);
}
seen.AssertComplete();
}
[Fact]
[OuterLoop]
public static void DefaultIfEmpty_Unordered_NotEmpty_Longrunning()
{
DefaultIfEmpty_Unordered_NotEmpty(Sources.OuterLoopCount);
}
[Theory]
[MemberData(nameof(Sources.Ranges), new[] { 1, 2, 16 }, MemberType = typeof(Sources))]
public static void DefaultIfEmpty_NotEmpty(Labeled<ParallelQuery<int>> labeled, int count)
{
ParallelQuery<int> query = labeled.Item;
int seen = 0;
foreach (int i in query.DefaultIfEmpty())
{
Assert.Equal(seen++, i);
}
Assert.Equal(seen, count);
}
[Theory]
[OuterLoop]
[MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))]
public static void DefaultIfEmpty_NotEmpty_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)
{
DefaultIfEmpty_NotEmpty(labeled, count);
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void DefaultIfEmpty_Unordered_NotEmpty_NotPipelined(int count)
{
IntegerRangeSet seen = new IntegerRangeSet(0, count);
Assert.All(UnorderedSources.Default(count).DefaultIfEmpty().ToList(), x => seen.Add(x));
seen.AssertComplete();
}
[Fact]
[OuterLoop]
public static void DefaultIfEmpty_Unordered_NotEmpty_NotPipelined_Longrunning()
{
DefaultIfEmpty_Unordered_NotEmpty_NotPipelined(Sources.OuterLoopCount);
}
[Theory]
[MemberData(nameof(Sources.Ranges), new[] { 1, 2, 16 }, MemberType = typeof(Sources))]
public static void DefaultIfEmpty_NotEmpty_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count)
{
ParallelQuery<int> query = labeled.Item;
int seen = 0;
foreach (int i in query.DefaultIfEmpty().ToList())
{
Assert.Equal(seen++, i);
}
Assert.Equal(seen, count);
}
[Theory]
[OuterLoop]
[MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))]
public static void DefaultIfEmpty_NotEmpty_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)
{
DefaultIfEmpty_NotEmpty_NotPipelined(labeled, count);
}
[Theory]
[MemberData(nameof(EmptyData))]
public static void DefaultIfEmpty_Empty<T>(Labeled<ParallelQuery<T>> labeled, T def)
{
ParallelQuery<T> notEmpty = labeled.Item.DefaultIfEmpty();
Assert.NotEmpty(notEmpty);
Assert.Equal(1, notEmpty.Count());
Assert.Single(notEmpty, default(T));
ParallelQuery<T> specified = labeled.Item.DefaultIfEmpty(def);
Assert.NotEmpty(specified);
Assert.Equal(1, specified.Count());
Assert.Single(specified, def);
}
[Theory]
[MemberData(nameof(EmptyData))]
public static void DefaultIfEmpty_Empty_NotPipelined<T>(Labeled<ParallelQuery<T>> labeled, T def)
{
IList<T> notEmpty = labeled.Item.DefaultIfEmpty().ToList();
Assert.NotEmpty(notEmpty);
Assert.Equal(1, notEmpty.Count());
Assert.Single(notEmpty, default(T));
IList<T> specified = labeled.Item.DefaultIfEmpty(def).ToList();
Assert.NotEmpty(specified);
Assert.Equal(1, specified.Count());
Assert.Single(specified, def);
}
[Fact]
public static void DefaultIfEmpty_ArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<bool>)null).DefaultIfEmpty());
}
}
}