1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Collections.Generic;
6 using Xunit;
7 
8 namespace System.Linq.Parallel.Tests
9 {
10     public static class AnyTests
11     {
OnlyOneData(int[] counts)12         public static IEnumerable<object[]> OnlyOneData(int[] counts)
13         {
14             foreach (int count in counts.DefaultIfEmpty(Sources.OuterLoopCount))
15             {
16                 foreach (int position in new[] { 0, count / 2, Math.Max(0, count - 1) }.Distinct())
17                 {
18                     yield return new object[] { count, position };
19                 }
20             }
21         }
22 
23         //
24         // Any
25         //
26         [Theory]
27         [InlineData(0)]
28         [InlineData(1)]
29         [InlineData(2)]
30         [InlineData(16)]
Any_Contents(int count)31         public static void Any_Contents(int count)
32         {
33             Assert.Equal(count > 0, UnorderedSources.Default(count).Any());
34         }
35 
36         [Fact]
37         [OuterLoop]
Any_Contents_Longrunning()38         public static void Any_Contents_Longrunning()
39         {
40             Any_Contents(Sources.OuterLoopCount);
41         }
42 
43         [Theory]
44         [InlineData(0)]
45         [InlineData(1)]
46         [InlineData(2)]
47         [InlineData(16)]
Any_AllFalse(int count)48         public static void Any_AllFalse(int count)
49         {
50             IntegerRangeSet seen = new IntegerRangeSet(0, count);
51             Assert.False(UnorderedSources.Default(count).Any(x => !seen.Add(x)));
52             seen.AssertComplete();
53         }
54 
55         [Fact]
56         [OuterLoop]
Any_AllFalse_Longrunning()57         public static void Any_AllFalse_Longrunning()
58         {
59             Any_AllFalse(Sources.OuterLoopCount);
60         }
61 
62         [Theory]
63         [InlineData(0)]
64         [InlineData(1)]
65         [InlineData(2)]
66         [InlineData(16)]
Any_AllTrue(int count)67         public static void Any_AllTrue(int count)
68         {
69             Assert.Equal(count > 0, UnorderedSources.Default(count).Any(x => x >= 0));
70         }
71 
72         [Fact]
73         [OuterLoop]
Any_AllTrue_Longrunning()74         public static void Any_AllTrue_Longrunning()
75         {
76             Any_AllTrue(Sources.OuterLoopCount);
77         }
78 
79         [Theory]
80         [MemberData(nameof(OnlyOneData), new[] { 2, 16 })]
Any_OneFalse(int count, int position)81         public static void Any_OneFalse(int count, int position)
82         {
83             Assert.True(UnorderedSources.Default(count).Any(x => !(x == position)));
84         }
85 
86         [Theory]
87         [OuterLoop]
88         [MemberData(nameof(OnlyOneData), new int[] { /* Sources.OuterLoopCount */ })]
Any_OneFalse_Longrunning(int count, int position)89         public static void Any_OneFalse_Longrunning(int count, int position)
90         {
91             Any_OneFalse(count, position);
92         }
93 
94         [Theory]
95         [MemberData(nameof(OnlyOneData), new[] { 2, 16 })]
Any_OneTrue(int count, int position)96         public static void Any_OneTrue(int count, int position)
97         {
98             Assert.True(UnorderedSources.Default(count).Any(x => x == position));
99         }
100 
101         [Theory]
102         [OuterLoop]
103         [MemberData(nameof(OnlyOneData), new int[] { /* Sources.OuterLoopCount */ })]
Any_OneTrue_Longrunning(int count, int position)104         public static void Any_OneTrue_Longrunning(int count, int position)
105         {
106             Any_OneTrue(count, position);
107         }
108 
109         //
110         // Tests the Any() operator applied to infinite enumerables
111         //
112         [Fact]
Any_Infinite()113         public static void Any_Infinite()
114         {
115             Assert.True(InfiniteEnumerable().AsParallel().Any());
116             Assert.True(InfiniteEnumerable().AsParallel().Any(x => true));
117         }
118 
119         [Fact]
Any_OperationCanceledException()120         public static void Any_OperationCanceledException()
121         {
122             AssertThrows.EventuallyCanceled((source, canceler) => source.Any(x => { canceler(); return false; }));
123         }
124 
125         [Fact]
Any_AggregateException_Wraps_OperationCanceledException()126         public static void Any_AggregateException_Wraps_OperationCanceledException()
127         {
128             AssertThrows.OtherTokenCanceled((source, canceler) => source.Any(x => { canceler(); return false; }));
129             AssertThrows.SameTokenNotCanceled((source, canceler) => source.Any(x => { canceler(); return false; }));
130         }
131 
132         [Fact]
Any_OperationCanceledException_PreCanceled()133         public static void Any_OperationCanceledException_PreCanceled()
134         {
135             AssertThrows.AlreadyCanceled(source => source.Any());
136             AssertThrows.AlreadyCanceled(source => source.Any(x => true));
137         }
138 
139         [Fact]
Any_AggregateException()140         public static void Any_AggregateException()
141         {
142             AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Any(x => { throw new DeliberateTestException(); }));
143             AssertThrows.Wrapped<DeliberateTestException>(() => UnorderedSources.Default(1).Select((Func<int, int>)(x => { throw new DeliberateTestException(); })).Any());
144         }
145 
146         [Fact]
Any_ArgumentNullException()147         public static void Any_ArgumentNullException()
148         {
149             AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<bool>)null).Any(x => x));
150             AssertExtensions.Throws<ArgumentNullException>("predicate", () => ParallelEnumerable.Empty<bool>().Any(null));
151         }
152 
InfiniteEnumerable()153         private static IEnumerable<int> InfiniteEnumerable()
154         {
155             while (true) yield return 0;
156         }
157     }
158 }
159