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 OfTypeTests 10 { 11 [Theory] 12 [InlineData(0)] 13 [InlineData(1)] 14 [InlineData(2)] 15 [InlineData(16)] OfType_Unordered_AllValid(int count)16 public static void OfType_Unordered_AllValid(int count) 17 { 18 IntegerRangeSet seen = new IntegerRangeSet(0, count); 19 foreach (int i in UnorderedSources.Default(count).Select(x => (object)x).OfType<int>()) 20 { 21 seen.Add(i); 22 } 23 seen.AssertComplete(); 24 } 25 26 [Fact] 27 [OuterLoop] OfType_Unordered_AllValid_Longrunning()28 public static void OfType_Unordered_AllValid_Longrunning() 29 { 30 OfType_Unordered_AllValid(Sources.OuterLoopCount); 31 } 32 33 [Theory] 34 [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] OfType_AllValid(Labeled<ParallelQuery<int>> labeled, int count)35 public static void OfType_AllValid(Labeled<ParallelQuery<int>> labeled, int count) 36 { 37 ParallelQuery<int> query = labeled.Item; 38 int seen = 0; 39 foreach (int i in query.Select(x => (object)x).OfType<int>()) 40 { 41 Assert.Equal(seen++, i); 42 } 43 Assert.Equal(count, seen); 44 } 45 46 [Theory] 47 [OuterLoop] 48 [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))] OfType_AllValid_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)49 public static void OfType_AllValid_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 50 { 51 OfType_AllValid(labeled, count); 52 } 53 54 [Theory] 55 [InlineData(0)] 56 [InlineData(1)] 57 [InlineData(2)] 58 [InlineData(16)] OfType_Unordered_AllValid_NotPipelined(int count)59 public static void OfType_Unordered_AllValid_NotPipelined(int count) 60 { 61 IntegerRangeSet seen = new IntegerRangeSet(0, count); 62 Assert.All(UnorderedSources.Default(count).Select(x => (object)x).OfType<int>().ToList(), x => seen.Add(x)); 63 seen.AssertComplete(); 64 } 65 66 [Fact] 67 [OuterLoop] OfType_Unordered_AllValid_NotPipelined_Longrunning()68 public static void OfType_Unordered_AllValid_NotPipelined_Longrunning() 69 { 70 OfType_Unordered_AllValid_NotPipelined(Sources.OuterLoopCount); 71 } 72 73 [Theory] 74 [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] OfType_AllValid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count)75 public static void OfType_AllValid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) 76 { 77 ParallelQuery<int> query = labeled.Item; 78 int seen = 0; 79 Assert.All(query.Select(x => (object)x).OfType<int>().ToList(), x => Assert.Equal(seen++, x)); 80 Assert.Equal(count, seen); 81 } 82 83 [Theory] 84 [OuterLoop] 85 [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))] OfType_AllValid_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)86 public static void OfType_AllValid_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 87 { 88 OfType_AllValid_NotPipelined(labeled, count); 89 } 90 91 [Theory] 92 [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] 93 [MemberData(nameof(UnorderedSources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] OfType_NoneValid(Labeled<ParallelQuery<int>> labeled, int count)94 public static void OfType_NoneValid(Labeled<ParallelQuery<int>> labeled, int count) 95 { 96 ParallelQuery<int> query = labeled.Item; 97 Assert.Empty(query.OfType<long>()); 98 } 99 100 [Theory] 101 [OuterLoop] 102 [MemberData(nameof(Sources.Ranges), new[] { 1024 * 64 }, MemberType = typeof(Sources))] 103 [MemberData(nameof(UnorderedSources.Ranges), new[] { 1024 * 64 }, MemberType = typeof(UnorderedSources))] OfType_NoneValid_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)104 public static void OfType_NoneValid_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 105 { 106 OfType_NoneValid(labeled, count); 107 } 108 109 [Theory] 110 [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] 111 [MemberData(nameof(UnorderedSources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] OfType_NoneValid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count)112 public static void OfType_NoneValid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) 113 { 114 ParallelQuery<int> query = labeled.Item; 115 Assert.Empty(query.OfType<long>().ToList()); 116 } 117 118 [Theory] 119 [OuterLoop] 120 [MemberData(nameof(Sources.Ranges), new[] { 1024 * 64 }, MemberType = typeof(Sources))] 121 [MemberData(nameof(UnorderedSources.Ranges), new[] { 1024 * 64 }, MemberType = typeof(UnorderedSources))] OfType_NoneValid_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)122 public static void OfType_NoneValid_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 123 { 124 OfType_NoneValid_NotPipelined(labeled, count); 125 } 126 127 [Theory] 128 [InlineData(0)] 129 [InlineData(1)] 130 [InlineData(2)] 131 [InlineData(16)] OfType_Unordered_SomeValid(int count)132 public static void OfType_Unordered_SomeValid(int count) 133 { 134 IntegerRangeSet seen = new IntegerRangeSet(count / 2, (count + 1) / 2); 135 foreach (int i in UnorderedSources.Default(count).Select(x => x >= count / 2 ? (object)x : x.ToString()).OfType<int>()) 136 { 137 seen.Add(i); 138 } 139 seen.AssertComplete(); 140 } 141 142 [Fact] 143 [OuterLoop] OfType_Unordered_SomeValid_Longrunning()144 public static void OfType_Unordered_SomeValid_Longrunning() 145 { 146 OfType_Unordered_SomeValid(Sources.OuterLoopCount); 147 } 148 149 [Theory] 150 [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] OfType_SomeValid(Labeled<ParallelQuery<int>> labeled, int count)151 public static void OfType_SomeValid(Labeled<ParallelQuery<int>> labeled, int count) 152 { 153 ParallelQuery<int> query = labeled.Item; 154 int seen = count / 2; 155 foreach (int i in query.Select(x => x >= count / 2 ? (object)x : x.ToString()).OfType<int>()) 156 { 157 Assert.Equal(seen++, i); 158 } 159 Assert.Equal(count, seen); 160 } 161 162 [Theory] 163 [OuterLoop] 164 [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))] OfType_SomeValid_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)165 public static void OfType_SomeValid_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 166 { 167 OfType_SomeValid(labeled, count); 168 } 169 170 [Theory] 171 [InlineData(0)] 172 [InlineData(1)] 173 [InlineData(2)] 174 [InlineData(16)] OfType_Unordered_SomeValid_NotPipelined(int count)175 public static void OfType_Unordered_SomeValid_NotPipelined(int count) 176 { 177 IntegerRangeSet seen = new IntegerRangeSet(count / 2, (count + 1) / 2); 178 Assert.All(UnorderedSources.Default(count).Select(x => x >= count / 2 ? (object)x : x.ToString()).OfType<int>().ToList(), x => seen.Add(x)); 179 seen.AssertComplete(); 180 } 181 182 [Fact] 183 [OuterLoop] OfType_Unordered_SomeValid_NotPipelined_Longrunning()184 public static void OfType_Unordered_SomeValid_NotPipelined_Longrunning() 185 { 186 OfType_Unordered_SomeValid_NotPipelined(Sources.OuterLoopCount); 187 } 188 189 [Theory] 190 [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] OfType_SomeValid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count)191 public static void OfType_SomeValid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) 192 { 193 ParallelQuery<int> query = labeled.Item; 194 int seen = count / 2; 195 Assert.All(query.Select(x => x >= count / 2 ? (object)x : x.ToString()).OfType<int>().ToList(), x => Assert.Equal(seen++, x)); 196 Assert.Equal(count, seen); 197 } 198 199 [Theory] 200 [OuterLoop] 201 [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))] OfType_SomeValid_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)202 public static void OfType_SomeValid_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 203 { 204 OfType_SomeValid_NotPipelined(labeled, count); 205 } 206 207 [Theory] 208 [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] OfType_SomeInvalidNull(Labeled<ParallelQuery<int>> labeled, int count)209 public static void OfType_SomeInvalidNull(Labeled<ParallelQuery<int>> labeled, int count) 210 { 211 ParallelQuery<int> query = labeled.Item; 212 int seen = count / 2; 213 Assert.All(query.Select(x => x >= count / 2 ? (object)x : null).OfType<int>(), x => Assert.Equal(seen++, x)); 214 Assert.Equal(count, seen); 215 } 216 217 [Theory] 218 [OuterLoop] 219 [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))] OfType_SomeInvalidNull_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)220 public static void OfType_SomeInvalidNull_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 221 { 222 OfType_SomeInvalidNull(labeled, count); 223 } 224 225 [Theory] 226 [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] OfType_SomeValidNull(Labeled<ParallelQuery<int>> labeled, int count)227 public static void OfType_SomeValidNull(Labeled<ParallelQuery<int>> labeled, int count) 228 { 229 ParallelQuery<int> query = labeled.Item; 230 int nullCount = 0; 231 int seen = count / 2; 232 Assert.All(query.Select(x => x >= count / 2 ? (object)(x.ToString()) : (object)(string)null).OfType<string>(), 233 x => 234 { 235 if (string.IsNullOrEmpty(x)) nullCount++; 236 else Assert.Equal(seen++, int.Parse(x)); 237 }); 238 Assert.Equal(count, seen); 239 Assert.Equal(0, nullCount); 240 } 241 242 [Theory] 243 [OuterLoop] 244 [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))] OfType_SomeValidNull_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)245 public static void OfType_SomeValidNull_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 246 { 247 OfType_SomeValidNull(labeled, count); 248 } 249 250 [Theory] 251 [MemberData(nameof(Sources.Ranges), new[] { 0, 1, 2, 16 }, MemberType = typeof(Sources))] OfType_SomeNull(Labeled<ParallelQuery<int>> labeled, int count)252 public static void OfType_SomeNull(Labeled<ParallelQuery<int>> labeled, int count) 253 { 254 ParallelQuery<int> query = labeled.Item; 255 int nullCount = 0; 256 int seen = count / 2; 257 Assert.All(query.Select(x => x >= count / 2 ? (object)(int?)x : (int?)null).OfType<int?>(), 258 x => 259 { 260 if (!x.HasValue) nullCount++; 261 else Assert.Equal(seen++, x.Value); 262 }); 263 Assert.Equal(count, seen); 264 Assert.Equal(0, nullCount); 265 } 266 267 [Theory] 268 [OuterLoop] 269 [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))] OfType_SomeNull_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)270 public static void OfType_SomeNull_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 271 { 272 OfType_SomeNull(labeled, count); 273 } 274 275 [Fact] OfType_ArgumentNullException()276 public static void OfType_ArgumentNullException() 277 { 278 AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<object>)null).OfType<int>()); 279 } 280 } 281 } 282