-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
CastTests.cs
171 lines (149 loc) · 6.2 KB
/
CastTests.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Xunit;
namespace System.Linq.Parallel.Tests
{
public static class CastTests
{
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Cast_Unordered_Valid(int count)
{
IntegerRangeSet seen = new IntegerRangeSet(0, count);
foreach (int i in UnorderedSources.Default(count).Select(x => (object)x).Cast<int>())
{
seen.Add(i);
}
seen.AssertComplete();
}
[Fact]
[OuterLoop]
public static void Cast_Unordered_Valid_Longrunning()
{
Cast_Unordered_Valid(Sources.OuterLoopCount);
}
[Theory]
[MemberData(nameof(Sources.Ranges), new[] { 1, 2, 16 }, MemberType = typeof(Sources))]
public static void Cast_Valid(Labeled<ParallelQuery<int>> labeled, int count)
{
ParallelQuery<int> query = labeled.Item;
int seen = 0;
foreach (int i in query.Select(x => (object)x).Cast<int>())
{
Assert.Equal(seen++, i);
}
Assert.Equal(count, seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))]
public static void Cast_Valid_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)
{
Cast_Valid(labeled, count);
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Cast_Unordered_Valid_NotPipelined(int count)
{
IntegerRangeSet seen = new IntegerRangeSet(0, count);
Assert.All(UnorderedSources.Default(count).Select(x => (object)x).Cast<int>().ToList(), x => seen.Add(x));
seen.AssertComplete();
}
[Fact]
[OuterLoop]
public static void Cast_Unordered_Valid_NotPipelined_Longrunning()
{
Cast_Unordered_Valid_NotPipelined(Sources.OuterLoopCount);
}
[Theory]
[MemberData(nameof(Sources.Ranges), new[] { 1, 2, 16 }, MemberType = typeof(Sources))]
public static void Cast_Valid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count)
{
ParallelQuery<int> query = labeled.Item;
int seen = 0;
Assert.All(query.Select(x => (object)x).Cast<int>().ToList(), x => Assert.Equal(seen++, x));
Assert.Equal(count, seen);
}
[Theory]
[OuterLoop]
[MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))]
public static void Cast_Valid_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)
{
Cast_Valid(labeled, count);
}
[Fact]
public static void Cast_Unordered_Empty()
{
ParallelQuery<int> empty = UnorderedSources.Default(0);
Assert.IsAssignableFrom<ParallelQuery<int>>(empty.Cast<string>().Cast<int>());
Assert.Empty(empty.Cast<string>().Cast<int>());
Assert.Empty(empty.Cast<string>().Cast<int>().ToList());
}
[Theory]
[MemberData(nameof(Sources.Ranges), new[] { 0 }, MemberType = typeof(Sources))]
public static void Cast_Empty(Labeled<ParallelQuery<int>> labeled, int count)
{
_ = count;
ParallelQuery<int> empty = labeled.Item;
Assert.IsAssignableFrom<ParallelQuery<int>>(empty.Cast<string>().Cast<int>());
Assert.Empty(empty.Cast<string>().Cast<int>());
Assert.Empty(empty.Cast<string>().Cast<int>().ToList());
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Cast_Unordered_InvalidCastException(int count)
{
AssertThrows.Wrapped<InvalidCastException>(() => UnorderedSources.Default(count).Cast<double>().ForAll(x => {; }));
AssertThrows.Wrapped<InvalidCastException>(() => UnorderedSources.Default(count).Cast<double>().ToList());
}
[Theory]
[MemberData(nameof(Sources.Ranges), new[] { 1, 2, 16 }, MemberType = typeof(Sources))]
public static void Cast_InvalidCastException(Labeled<ParallelQuery<int>> labeled, int count)
{
_ = count;
AssertThrows.Wrapped<InvalidCastException>(() => labeled.Item.Cast<double>().ForAll(x => {; }));
AssertThrows.Wrapped<InvalidCastException>(() => labeled.Item.Cast<double>().ToList());
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Cast_Unordered_Assignable_InvalidCastException(int count)
{
AssertThrows.Wrapped<InvalidCastException>(() => UnorderedSources.Default(count).Select(x => (Int32)x).Cast<Castable>().ForAll(x => {; }));
AssertThrows.Wrapped<InvalidCastException>(() => UnorderedSources.Default(count).Select(x => (Int32)x).Cast<Castable>().ToList());
}
[Theory]
[MemberData(nameof(Sources.Ranges), new[] { 1, 2, 16 }, MemberType = typeof(Sources))]
public static void Cast_Assignable_InvalidCastException(Labeled<ParallelQuery<int>> labeled, int count)
{
_ = count;
AssertThrows.Wrapped<InvalidCastException>(() => labeled.Item.Select(x => (Int32)x).Cast<Castable>().ForAll(x => {; }));
AssertThrows.Wrapped<InvalidCastException>(() => labeled.Item.Select(x => (Int32)x).Cast<Castable>().ToList());
}
[Fact]
public static void Cast_ArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<object>)null).Cast<int>());
}
private class Castable
{
private readonly int _value;
private Castable(int value)
{
_value = value;
}
public int Value { get { return _value; } }
public static explicit operator Castable(int value)
{
return new Castable(value);
}
}
}
}