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 Xunit;
6 
7 namespace System.Linq.Parallel.Tests
8 {
9     public static class WhereTests
10     {
11         [Theory]
12         [InlineData(0)]
13         [InlineData(1)]
14         [InlineData(2)]
15         [InlineData(15)]
16         [InlineData(16)]
Where_Unordered(int count)17         public static void Where_Unordered(int count)
18         {
19             IntegerRangeSet seen = new IntegerRangeSet(0, (count + 1) / 2);
20             foreach (int i in UnorderedSources.Default(count).Where(x => x % 2 == 0))
21             {
22                 Assert.Equal(0, i % 2);
23                 seen.Add(i / 2);
24             }
25             seen.AssertComplete();
26         }
27 
28         [Fact]
29         [OuterLoop]
Where_Unordered_Longrunning()30         public static void Where_Unordered_Longrunning()
31         {
32             Where_Unordered(Sources.OuterLoopCount);
33         }
34 
35         [Theory]
36         [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 15, 16 }, MemberType = typeof(Sources))]
Where(Labeled<ParallelQuery<int>> labeled, int count)37         public static void Where(Labeled<ParallelQuery<int>> labeled, int count)
38         {
39             ParallelQuery<int> query = labeled.Item;
40             int seen = 0;
41             foreach (int i in query.Where(x => x % 2 == 0))
42             {
43                 Assert.Equal(seen, i);
44                 seen += 2;
45             }
46             Assert.Equal(count + (count % 2), seen);
47         }
48 
49         [Theory]
50         [OuterLoop]
51         [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))]
Where_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)52         public static void Where_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)
53         {
54             Where(labeled, count);
55         }
56 
57         [Theory]
58         [InlineData(0)]
59         [InlineData(1)]
60         [InlineData(2)]
61         [InlineData(15)]
62         [InlineData(16)]
Where_Unordered_NotPipelined(int count)63         public static void Where_Unordered_NotPipelined(int count)
64         {
65             IntegerRangeSet seen = new IntegerRangeSet(0, (count + 1) / 2);
66             Assert.All(UnorderedSources.Default(count).Where(x => x % 2 == 0).ToList(), x => seen.Add(x / 2));
67             seen.AssertComplete();
68         }
69 
70         [Fact]
71         [OuterLoop]
Where_Unordered_NotPipelined_Longrunning()72         public static void Where_Unordered_NotPipelined_Longrunning()
73         {
74             Where_Unordered_NotPipelined(Sources.OuterLoopCount);
75         }
76 
77         [Theory]
78         [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 15, 16 }, MemberType = typeof(Sources))]
Where_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count)79         public static void Where_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count)
80         {
81             ParallelQuery<int> query = labeled.Item;
82             int seen = 0;
83             Assert.All(query.Where(x => x % 2 == 0).ToList(), x => { Assert.Equal(seen, x); seen += 2; });
84             Assert.Equal(count + (count % 2), seen);
85         }
86 
87         [Theory]
88         [OuterLoop]
89         [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))]
Where_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)90         public static void Where_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)
91         {
92             Where_NotPipelined(labeled, count);
93         }
94 
95         // Uses an element's index to calculate an output value.  If order preservation isn't
96         // working, this would PROBABLY fail.  Unfortunately, this isn't deterministic.  But choosing
97         // larger input sizes increases the probability that it will.
98 
99         [Theory]
100         [InlineData(0)]
101         [InlineData(1)]
102         [InlineData(2)]
103         [InlineData(15)]
104         [InlineData(16)]
Where_Indexed_Unordered(int count)105         public static void Where_Indexed_Unordered(int count)
106         {
107             IntegerRangeSet seen = new IntegerRangeSet(0, count);
108             foreach (int i in UnorderedSources.Default(count).Where((x, index) => x == index))
109             {
110                 seen.Add(i);
111             }
112             seen.AssertComplete();
113         }
114 
115         [Fact]
116         [OuterLoop]
Where_Indexed_Unordered_Longrunning()117         public static void Where_Indexed_Unordered_Longrunning()
118         {
119             Where_Indexed_Unordered(Sources.OuterLoopCount);
120         }
121 
122         [Theory]
123         [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 15, 16 }, MemberType = typeof(Sources))]
Where_Indexed(Labeled<ParallelQuery<int>> labeled, int count)124         public static void Where_Indexed(Labeled<ParallelQuery<int>> labeled, int count)
125         {
126             ParallelQuery<int> query = labeled.Item;
127             int seen = 0;
128             foreach (int i in query.Where((x, index) => x == index))
129             {
130                 Assert.Equal(seen++, i);
131             }
132             Assert.Equal(count, seen);
133         }
134 
135         [Theory]
136         [OuterLoop]
137         [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))]
Where_Indexed_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)138         public static void Where_Indexed_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)
139         {
140             Where_Indexed(labeled, count);
141         }
142 
143         [Theory]
144         [InlineData(0)]
145         [InlineData(1)]
146         [InlineData(2)]
147         [InlineData(15)]
148         [InlineData(16)]
Where_Indexed_Unordered_NotPipelined(int count)149         public static void Where_Indexed_Unordered_NotPipelined(int count)
150         {
151             IntegerRangeSet seen = new IntegerRangeSet(0, count);
152             Assert.All(UnorderedSources.Default(count).Where((x, index) => x == index).ToList(), x => seen.Add(x));
153             seen.AssertComplete();
154         }
155 
156         [Fact]
157         [OuterLoop]
Where_Indexed_Unordered_NotPipelined_Longrunning()158         public static void Where_Indexed_Unordered_NotPipelined_Longrunning()
159         {
160             Where_Indexed_Unordered_NotPipelined(Sources.OuterLoopCount);
161         }
162 
163         [Theory]
164         [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 15, 16 }, MemberType = typeof(Sources))]
Where_Indexed_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count)165         public static void Where_Indexed_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count)
166         {
167             ParallelQuery<int> query = labeled.Item;
168             int seen = 0;
169             Assert.All(query.Where((x, index) => x == index).ToList(), x => Assert.Equal(seen++, x));
170             Assert.Equal(count, seen);
171         }
172 
173         [Theory]
174         [OuterLoop]
175         [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))]
Where_Indexed_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)176         public static void Where_Indexed_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)
177         {
178             Where_Indexed_NotPipelined(labeled, count);
179         }
180 
181         [Fact]
Where_ArgumentNullException()182         public static void Where_ArgumentNullException()
183         {
184             AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<bool>)null).Where(x => x));
185             AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<bool>)null).Where((x, index) => x));
186             AssertExtensions.Throws<ArgumentNullException>("predicate", () => ParallelEnumerable.Empty<bool>().Where((Func<bool, bool>)null));
187             AssertExtensions.Throws<ArgumentNullException>("predicate", () => ParallelEnumerable.Empty<bool>().Where((Func<bool, int, bool>)null));
188         }
189     }
190 }
191