-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
AnyTests.cs
157 lines (138 loc) · 4.97 KB
/
AnyTests.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
// 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 AnyTests
{
public static IEnumerable<object[]> OnlyOneData(int[] counts)
{
foreach (int count in counts.DefaultIfEmpty(Sources.OuterLoopCount))
{
foreach (int position in new[] { 0, count / 2, Math.Max(0, count - 1) }.Distinct())
{
yield return new object[] { count, position };
}
}
}
//
// Any
//
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Any_Contents(int count)
{
Assert.Equal(count > 0, UnorderedSources.Default(count).Any());
}
[Fact]
[OuterLoop]
public static void Any_Contents_Longrunning()
{
Any_Contents(Sources.OuterLoopCount);
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Any_AllFalse(int count)
{
IntegerRangeSet seen = new IntegerRangeSet(0, count);
Assert.False(UnorderedSources.Default(count).Any(x => !seen.Add(x)));
seen.AssertComplete();
}
[Fact]
[OuterLoop]
public static void Any_AllFalse_Longrunning()
{
Any_AllFalse(Sources.OuterLoopCount);
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
[InlineData(16)]
public static void Any_AllTrue(int count)
{
Assert.Equal(count > 0, UnorderedSources.Default(count).Any(x => x >= 0));
}
[Fact]
[OuterLoop]
public static void Any_AllTrue_Longrunning()
{
Any_AllTrue(Sources.OuterLoopCount);
}
[Theory]
[MemberData(nameof(OnlyOneData), new[] { 2, 16 })]
public static void Any_OneFalse(int count, int position)
{
Assert.True(UnorderedSources.Default(count).Any(x => !(x == position)));
}
[Theory]
[OuterLoop]
[MemberData(nameof(OnlyOneData), new int[] { /* Sources.OuterLoopCount */ })]
public static void Any_OneFalse_Longrunning(int count, int position)
{
Any_OneFalse(count, position);
}
[Theory]
[MemberData(nameof(OnlyOneData), new[] { 2, 16 })]
public static void Any_OneTrue(int count, int position)
{
Assert.True(UnorderedSources.Default(count).Any(x => x == position));
}
[Theory]
[OuterLoop]
[MemberData(nameof(OnlyOneData), new int[] { /* Sources.OuterLoopCount */ })]
public static void Any_OneTrue_Longrunning(int count, int position)
{
Any_OneTrue(count, position);
}
//
// Tests the Any() operator applied to infinite enumerables
//
[Fact]
public static void Any_Infinite()
{
Assert.True(InfiniteEnumerable().AsParallel().Any());
Assert.True(InfiniteEnumerable().AsParallel().Any(x => true));
}
[Fact]
public static void Any_OperationCanceledException()
{
AssertThrows.EventuallyCanceled((source, canceler) => source.Any(x => { canceler(); return false; }));
}
[Fact]
public static void Any_AggregateException_Wraps_OperationCanceledException()
{
AssertThrows.OtherTokenCanceled((source, canceler) => source.Any(x => { canceler(); return false; }));
AssertThrows.SameTokenNotCanceled((source, canceler) => source.Any(x => { canceler(); return false; }));
}
[Fact]
public static void Any_OperationCanceledException_PreCanceled()
{
AssertThrows.AlreadyCanceled(source => source.Any());
AssertThrows.AlreadyCanceled(source => source.Any(x => true));
}
[Fact]
public static void Any_AggregateException()
{
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Any(x => { throw new DeliberateTestException(); }));
AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Select((Func<int, int>)(x => { throw new DeliberateTestException(); })).Any());
}
[Fact]
public static void Any_ArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<bool>)null).Any(x => x));
AssertExtensions.Throws<ArgumentNullException>("predicate", () => ParallelEnumerable.Empty<bool>().Any(null));
}
public static IEnumerable<int> InfiniteEnumerable()
{
while (true) yield return 0;
}
}
}