-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
AverageTests.cs
354 lines (299 loc) · 20.1 KB
/
AverageTests.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
// 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 AverageTests
{
//
// Average
//
// Get a set of ranges from 0 to each count, with an extra parameter containing the expected average.
public static IEnumerable<object[]> AverageData(int[] counts)
{
foreach (int count in counts)
{
yield return new object[] { count, (count - 1) / 2.0 };
}
}
[Theory]
[MemberData(nameof(AverageData), new[] { 1, 2, 16 })]
public static void Average_Int(int count, double average)
{
Assert.Equal(average, UnorderedSources.Default(count).Average());
Assert.Equal((double?)average, UnorderedSources.Default(count).Select(x => (int?)x).Average());
Assert.Equal(-average, UnorderedSources.Default(count).Average(x => -x));
Assert.Equal(-(double?)average, UnorderedSources.Default(count).Average(x => -(int?)x));
}
[Fact]
[OuterLoop]
public static void Average_Int_Longrunning()
{
Average_Int(Sources.OuterLoopCount, (Sources.OuterLoopCount - 1) / 2.0);
}
[Theory]
[MemberData(nameof(AverageData), new[] { 1, 2, 16 })]
public static void Average_Int_SomeNull(int count, double average)
{
Assert.Equal(Math.Truncate(average), UnorderedSources.Default(count).Select(x => (x % 2 == 0) ? (int?)x : null).Average());
Assert.Equal(Math.Truncate(-average), UnorderedSources.Default(count).Average(x => (x % 2 == 0) ? -(int?)x : null));
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Average_Int_AllNull(int count)
{
Assert.Null(UnorderedSources.Default(count).Select(x => (int?)null).Average());
Assert.Null(UnorderedSources.Default(count).Average(x => (int?)null));
}
[Theory]
[MemberData(nameof(AverageData), new[] { 1, 2, 16 })]
public static void Average_Long(int count, double average)
{
Assert.Equal(average, UnorderedSources.Default(count).Select(x => (long)x).Average());
Assert.Equal((double?)average, UnorderedSources.Default(count).Select(x => (long?)x).Average());
Assert.Equal(-average, UnorderedSources.Default(count).Average(x => -(long)x));
Assert.Equal(-(double?)average, UnorderedSources.Default(count).Average(x => -(long?)x));
}
[Fact]
[OuterLoop]
public static void Average_Long_Longrunning()
{
Average_Long(Sources.OuterLoopCount, (Sources.OuterLoopCount - 1) / 2.0);
}
[Fact]
public static void Average_Long_Overflow()
{
AssertThrows.Wrapped<OverflowException>(() => UnorderedSources.Default(2).Select(x => x == 0 ? 1 : long.MaxValue).Average());
AssertThrows.Wrapped<OverflowException>(() => UnorderedSources.Default(2).Select(x => x == 0 ? (long?)1 : long.MaxValue).Average());
AssertThrows.Wrapped<OverflowException>(() => UnorderedSources.Default(2).Average(x => x == 0 ? -1 : long.MinValue));
AssertThrows.Wrapped<OverflowException>(() => UnorderedSources.Default(2).Average(x => x == 0 ? (long?)-1 : long.MinValue));
}
[Theory]
[MemberData(nameof(AverageData), new[] { 1, 2, 16 })]
public static void Average_Long_SomeNull(int count, double average)
{
Assert.Equal(Math.Truncate(average), UnorderedSources.Default(count).Select(x => (x % 2 == 0) ? (long?)x : null).Average());
Assert.Equal(Math.Truncate(-average), UnorderedSources.Default(count).Average(x => (x % 2 == 0) ? -(long?)x : null));
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Average_Long_AllNull(int count)
{
Assert.Null(UnorderedSources.Default(count).Select(x => (long?)null).Average());
Assert.Null(UnorderedSources.Default(count).Average(x => (long?)null));
}
[Theory]
[MemberData(nameof(AverageData), new[] { 1, 2, 16 })]
public static void Average_Float(int count, float average)
{
Assert.Equal(average, UnorderedSources.Default(count).Select(x => (float)x).Average());
Assert.Equal((float?)average, UnorderedSources.Default(count).Select(x => (float?)x).Average());
Assert.Equal(-average, UnorderedSources.Default(count).Average(x => -(float)x));
Assert.Equal(-(float?)average, UnorderedSources.Default(count).Average(x => -(float?)x));
}
[Fact]
[OuterLoop]
public static void Average_Float_Longrunning()
{
Average_Float(Sources.OuterLoopCount, (Sources.OuterLoopCount - 1) / 2.0F);
}
[Theory]
[MemberData(nameof(AverageData), new[] { 1, 2, 16 })]
public static void Average_Float_SomeNull(int count, float average)
{
Assert.Equal((float?)Math.Truncate(average), UnorderedSources.Default(count).Select(x => (x % 2 == 0) ? (float?)x : null).Average());
Assert.Equal((float?)Math.Truncate(-average), UnorderedSources.Default(count).Average(x => (x % 2 == 0) ? -(float?)x : null));
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Average_Float_AllNull(int count)
{
Assert.Null(UnorderedSources.Default(count).Select(x => (float?)null).Average());
Assert.Null(UnorderedSources.Default(count).Average(x => (float?)null));
}
[Theory]
[MemberData(nameof(AverageData), new[] { 1, 2, 16 })]
public static void Average_Double(int count, double average)
{
Assert.Equal(average, UnorderedSources.Default(count).Select(x => (double)x).Average());
Assert.Equal((double?)average, UnorderedSources.Default(count).Select(x => (double?)x).Average());
Assert.Equal(-average, UnorderedSources.Default(count).Average(x => -(double)x));
Assert.Equal(-(double?)average, UnorderedSources.Default(count).Average(x => -(double?)x));
}
[Fact]
[OuterLoop]
public static void Average_Double_Longrunning()
{
Average_Double(Sources.OuterLoopCount, (Sources.OuterLoopCount - 1) / 2.0);
}
[Theory]
[MemberData(nameof(AverageData), new[] { 1, 2, 16 })]
public static void Average_Double_SomeNull(int count, double average)
{
Assert.Equal(Math.Truncate(average), UnorderedSources.Default(count).Select(x => (x % 2 == 0) ? (double?)x : null).Average());
Assert.Equal(Math.Truncate(-average), UnorderedSources.Default(count).Average(x => (x % 2 == 0) ? -(double?)x : null));
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Average_Double_AllNull(int count)
{
Assert.Null(UnorderedSources.Default(count).Select(x => (double?)null).Average());
Assert.Null(UnorderedSources.Default(count).Average(x => (double?)null));
}
[Theory]
[MemberData(nameof(AverageData), new[] { 1, 2, 16 })]
public static void Average_Decimal(int count, decimal average)
{
Assert.Equal(average, UnorderedSources.Default(count).Select(x => (decimal)x).Average());
Assert.Equal((decimal?)average, UnorderedSources.Default(count).Select(x => (decimal?)x).Average());
Assert.Equal(-average, UnorderedSources.Default(count).Average(x => -(decimal)x));
Assert.Equal(-(decimal?)average, UnorderedSources.Default(count).Average(x => -(decimal?)x));
}
[Fact]
[OuterLoop]
public static void Average_Decimal_Longrunning()
{
Average_Decimal(Sources.OuterLoopCount, (Sources.OuterLoopCount - 1) / 2.0M);
}
[Theory]
[MemberData(nameof(AverageData), new[] { 1, 2, 16 })]
public static void Average_Decimal_SomeNull(int count, decimal average)
{
Assert.Equal(Math.Truncate(average), UnorderedSources.Default(count).Select(x => (x % 2 == 0) ? (decimal?)x : null).Average());
Assert.Equal(Math.Truncate(-average), UnorderedSources.Default(count).Average(x => (x % 2 == 0) ? -(decimal?)x : null));
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Average_Decimal_AllNull(int count)
{
Assert.Null(UnorderedSources.Default(count).Select(x => (decimal?)null).Average());
Assert.Null(UnorderedSources.Default(count).Average(x => (decimal?)null));
}
[Fact]
public static void Average_InvalidOperationException()
{
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Empty<int>().Average());
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Empty<int>().Average(x => x));
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Empty<long>().Average());
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Empty<long>().Average(x => x));
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Empty<float>().Average());
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Empty<float>().Average(x => x));
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Empty<double>().Average());
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Empty<double>().Average(x => x));
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Empty<decimal>().Average());
Assert.Throws<InvalidOperationException>(() => ParallelEnumerable.Empty<decimal>().Average(x => x));
// Nullables return null when empty
Assert.Null(ParallelEnumerable.Empty<int?>().Average());
Assert.Null(ParallelEnumerable.Empty<int?>().Average(x => x));
Assert.Null(ParallelEnumerable.Empty<long?>().Average());
Assert.Null(ParallelEnumerable.Empty<long?>().Average(x => x));
Assert.Null(ParallelEnumerable.Empty<float?>().Average());
Assert.Null(ParallelEnumerable.Empty<float?>().Average(x => x));
Assert.Null(ParallelEnumerable.Empty<double?>().Average());
Assert.Null(ParallelEnumerable.Empty<double?>().Average(x => x));
Assert.Null(ParallelEnumerable.Empty<decimal?>().Average());
Assert.Null(ParallelEnumerable.Empty<decimal?>().Average(x => x));
}
[Fact]
public static void Average_OperationCanceledException()
{
AssertThrows.EventuallyCanceled((source, canceler) => source.Average(x => { canceler(); return x; }));
AssertThrows.EventuallyCanceled((source, canceler) => source.Average(x => { canceler(); return (int?)x; }));
AssertThrows.EventuallyCanceled((source, canceler) => source.Average(x => { canceler(); return (long)x; }));
AssertThrows.EventuallyCanceled((source, canceler) => source.Average(x => { canceler(); return (long?)x; }));
AssertThrows.EventuallyCanceled((source, canceler) => source.Average(x => { canceler(); return (float)x; }));
AssertThrows.EventuallyCanceled((source, canceler) => source.Average(x => { canceler(); return (float?)x; }));
AssertThrows.EventuallyCanceled((source, canceler) => source.Average(x => { canceler(); return (double)x; }));
AssertThrows.EventuallyCanceled((source, canceler) => source.Average(x => { canceler(); return (double?)x; }));
AssertThrows.EventuallyCanceled((source, canceler) => source.Average(x => { canceler(); return (decimal)x; }));
AssertThrows.EventuallyCanceled((source, canceler) => source.Average(x => { canceler(); return (decimal?)x; }));
}
[Fact]
public static void Average_AggregateException_Wraps_OperationCanceledException()
{
AssertThrows.OtherTokenCanceled((source, canceler) => source.Average(x => { canceler(); return x; }));
AssertThrows.OtherTokenCanceled((source, canceler) => source.Average(x => { canceler(); return (int?)x; }));
AssertThrows.OtherTokenCanceled((source, canceler) => source.Average(x => { canceler(); return (long)x; }));
AssertThrows.OtherTokenCanceled((source, canceler) => source.Average(x => { canceler(); return (long?)x; }));
AssertThrows.OtherTokenCanceled((source, canceler) => source.Average(x => { canceler(); return (float)x; }));
AssertThrows.OtherTokenCanceled((source, canceler) => source.Average(x => { canceler(); return (float?)x; }));
AssertThrows.OtherTokenCanceled((source, canceler) => source.Average(x => { canceler(); return (double)x; }));
AssertThrows.OtherTokenCanceled((source, canceler) => source.Average(x => { canceler(); return (double?)x; }));
AssertThrows.OtherTokenCanceled((source, canceler) => source.Average(x => { canceler(); return (decimal)x; }));
AssertThrows.OtherTokenCanceled((source, canceler) => source.Average(x => { canceler(); return (decimal?)x; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Average(x => { canceler(); return x; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Average(x => { canceler(); return (int?)x; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Average(x => { canceler(); return (long)x; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Average(x => { canceler(); return (long?)x; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Average(x => { canceler(); return (float)x; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Average(x => { canceler(); return (float?)x; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Average(x => { canceler(); return (double)x; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Average(x => { canceler(); return (double?)x; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Average(x => { canceler(); return (decimal)x; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Average(x => { canceler(); return (decimal?)x; }));
}
[Fact]
public static void Average_OperationCanceledException_PreCanceled()
{
AssertThrows.AlreadyCanceled(source => source.Average(x => x));
AssertThrows.AlreadyCanceled(source => source.Average(x => (int?)x));
AssertThrows.AlreadyCanceled(source => source.Average(x => (long)x));
AssertThrows.AlreadyCanceled(source => source.Average(x => (long?)x));
AssertThrows.AlreadyCanceled(source => source.Average(x => (float)x));
AssertThrows.AlreadyCanceled(source => source.Average(x => (float?)x));
AssertThrows.AlreadyCanceled(source => source.Average(x => (double)x));
AssertThrows.AlreadyCanceled(source => source.Average(x => (double?)x));
AssertThrows.AlreadyCanceled(source => source.Average(x => (decimal)x));
AssertThrows.AlreadyCanceled(source => source.Average(x => (decimal?)x));
}
[Fact]
public static void Average_AggregateException()
{
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Average((Func<int, int>)(x => { throw new DeliberateTestException(); })));
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Average((Func<int, int?>)(x => { throw new DeliberateTestException(); })));
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Average((Func<int, long>)(x => { throw new DeliberateTestException(); })));
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Average((Func<int, long?>)(x => { throw new DeliberateTestException(); })));
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Average((Func<int, float>)(x => { throw new DeliberateTestException(); })));
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Average((Func<int, float?>)(x => { throw new DeliberateTestException(); })));
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Average((Func<int, double>)(x => { throw new DeliberateTestException(); })));
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Average((Func<int, double?>)(x => { throw new DeliberateTestException(); })));
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Average((Func<int, decimal>)(x => { throw new DeliberateTestException(); })));
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Average((Func<int, decimal?>)(x => { throw new DeliberateTestException(); })));
}
[Fact]
public static void Average_ArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<int>)null).Average());
AssertExtensions.Throws<ArgumentNullException>("selector", () => ParallelEnumerable.Repeat(0, 1).Average((Func<int, int>)null));
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<int?>)null).Average());
AssertExtensions.Throws<ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((int?)0, 1).Average((Func<int?, int?>)null));
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<long>)null).Average());
AssertExtensions.Throws<ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((long)0, 1).Average((Func<long, long>)null));
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<long?>)null).Average());
AssertExtensions.Throws<ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((long?)0, 1).Average((Func<long?, long?>)null));
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<float>)null).Average());
AssertExtensions.Throws<ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((float)0, 1).Average((Func<float, float>)null));
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<float?>)null).Average());
AssertExtensions.Throws<ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((float?)0, 1).Average((Func<float?, float?>)null));
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<double>)null).Average());
AssertExtensions.Throws<ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((double)0, 1).Average((Func<double, double>)null));
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<double?>)null).Average());
AssertExtensions.Throws<ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((double?)0, 1).Average((Func<double?, double?>)null));
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<decimal>)null).Average());
AssertExtensions.Throws<ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((decimal)0, 1).Average((Func<decimal, decimal>)null));
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<decimal?>)null).Average());
AssertExtensions.Throws<ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((decimal?)0, 1).Average((Func<decimal?, decimal?>)null));
}
}
}