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 CastTests 10 { 11 [Theory] 12 [InlineData(1)] 13 [InlineData(2)] 14 [InlineData(16)] Cast_Unordered_Valid(int count)15 public static void Cast_Unordered_Valid(int count) 16 { 17 IntegerRangeSet seen = new IntegerRangeSet(0, count); 18 foreach (int i in UnorderedSources.Default(count).Select(x => (object)x).Cast<int>()) 19 { 20 seen.Add(i); 21 } 22 seen.AssertComplete(); 23 } 24 25 [Fact] 26 [OuterLoop] Cast_Unordered_Valid_Longrunning()27 public static void Cast_Unordered_Valid_Longrunning() 28 { 29 Cast_Unordered_Valid(Sources.OuterLoopCount); 30 } 31 32 [Theory] 33 [MemberData(nameof(Sources.Ranges), new[] { 1, 2, 16 }, MemberType = typeof(Sources))] Cast_Valid(Labeled<ParallelQuery<int>> labeled, int count)34 public static void Cast_Valid(Labeled<ParallelQuery<int>> labeled, int count) 35 { 36 ParallelQuery<int> query = labeled.Item; 37 int seen = 0; 38 foreach (int i in query.Select(x => (object)x).Cast<int>()) 39 { 40 Assert.Equal(seen++, i); 41 } 42 Assert.Equal(count, seen); 43 } 44 45 [Theory] 46 [OuterLoop] 47 [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))] Cast_Valid_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)48 public static void Cast_Valid_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 49 { 50 Cast_Valid(labeled, count); 51 } 52 53 [Theory] 54 [InlineData(1)] 55 [InlineData(2)] 56 [InlineData(16)] Cast_Unordered_Valid_NotPipelined(int count)57 public static void Cast_Unordered_Valid_NotPipelined(int count) 58 { 59 IntegerRangeSet seen = new IntegerRangeSet(0, count); 60 Assert.All(UnorderedSources.Default(count).Select(x => (object)x).Cast<int>().ToList(), x => seen.Add(x)); 61 seen.AssertComplete(); 62 } 63 64 [Fact] 65 [OuterLoop] Cast_Unordered_Valid_NotPipelined_Longrunning()66 public static void Cast_Unordered_Valid_NotPipelined_Longrunning() 67 { 68 Cast_Unordered_Valid_NotPipelined(Sources.OuterLoopCount); 69 } 70 71 [Theory] 72 [MemberData(nameof(Sources.Ranges), new[] { 1, 2, 16 }, MemberType = typeof(Sources))] Cast_Valid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count)73 public static void Cast_Valid_NotPipelined(Labeled<ParallelQuery<int>> labeled, int count) 74 { 75 ParallelQuery<int> query = labeled.Item; 76 int seen = 0; 77 Assert.All(query.Select(x => (object)x).Cast<int>().ToList(), x => Assert.Equal(seen++, x)); 78 Assert.Equal(count, seen); 79 } 80 81 [Theory] 82 [OuterLoop] 83 [MemberData(nameof(Sources.OuterLoopRanges), MemberType = typeof(Sources))] Cast_Valid_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count)84 public static void Cast_Valid_NotPipelined_Longrunning(Labeled<ParallelQuery<int>> labeled, int count) 85 { 86 Cast_Valid(labeled, count); 87 } 88 89 [Fact] Cast_Unordered_Empty()90 public static void Cast_Unordered_Empty() 91 { 92 ParallelQuery<int> empty = UnorderedSources.Default(0); 93 94 Assert.IsAssignableFrom<ParallelQuery<int>>(empty.Cast<string>().Cast<int>()); 95 Assert.Empty(empty.Cast<string>().Cast<int>()); 96 Assert.Empty(empty.Cast<string>().Cast<int>().ToList()); 97 } 98 99 [Theory] 100 [MemberData(nameof(Sources.Ranges), new[] { 0 }, MemberType = typeof(Sources))] Cast_Empty(Labeled<ParallelQuery<int>> labeled, int count)101 public static void Cast_Empty(Labeled<ParallelQuery<int>> labeled, int count) 102 { 103 ParallelQuery<int> empty = labeled.Item; 104 105 Assert.IsAssignableFrom<ParallelQuery<int>>(empty.Cast<string>().Cast<int>()); 106 Assert.Empty(empty.Cast<string>().Cast<int>()); 107 Assert.Empty(empty.Cast<string>().Cast<int>().ToList()); 108 } 109 110 [Theory] 111 [InlineData(1)] 112 [InlineData(2)] 113 [InlineData(16)] Cast_Unordered_InvalidCastException(int count)114 public static void Cast_Unordered_InvalidCastException(int count) 115 { 116 AssertThrows.Wrapped<InvalidCastException>(() => UnorderedSources.Default(count).Cast<double>().ForAll(x => {; })); 117 AssertThrows.Wrapped<InvalidCastException>(() => UnorderedSources.Default(count).Cast<double>().ToList()); 118 } 119 120 [Theory] 121 [MemberData(nameof(Sources.Ranges), new[] { 1, 2, 16 }, MemberType = typeof(Sources))] Cast_InvalidCastException(Labeled<ParallelQuery<int>> labeled, int count)122 public static void Cast_InvalidCastException(Labeled<ParallelQuery<int>> labeled, int count) 123 { 124 AssertThrows.Wrapped<InvalidCastException>(() => labeled.Item.Cast<double>().ForAll(x => {; })); 125 AssertThrows.Wrapped<InvalidCastException>(() => labeled.Item.Cast<double>().ToList()); 126 } 127 128 [Theory] 129 [InlineData(1)] 130 [InlineData(2)] 131 [InlineData(16)] Cast_Unordered_Assignable_InvalidCastException(int count)132 public static void Cast_Unordered_Assignable_InvalidCastException(int count) 133 { 134 AssertThrows.Wrapped<InvalidCastException>(() => UnorderedSources.Default(count).Select(x => (Int32)x).Cast<Castable>().ForAll(x => {; })); 135 AssertThrows.Wrapped<InvalidCastException>(() => UnorderedSources.Default(count).Select(x => (Int32)x).Cast<Castable>().ToList()); 136 } 137 138 [Theory] 139 [MemberData(nameof(Sources.Ranges), new[] { 1, 2, 16 }, MemberType = typeof(Sources))] Cast_Assignable_InvalidCastException(Labeled<ParallelQuery<int>> labeled, int count)140 public static void Cast_Assignable_InvalidCastException(Labeled<ParallelQuery<int>> labeled, int count) 141 { 142 AssertThrows.Wrapped<InvalidCastException>(() => labeled.Item.Select(x => (Int32)x).Cast<Castable>().ForAll(x => {; })); 143 AssertThrows.Wrapped<InvalidCastException>(() => labeled.Item.Select(x => (Int32)x).Cast<Castable>().ToList()); 144 } 145 146 [Fact] 147 [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, ".NET Core bug fix https://github.com/dotnet/corefx/pull/1970 not available in desktop")] Cast_ArgumentNullException()148 public static void Cast_ArgumentNullException() 149 { 150 AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<object>)null).Cast<int>()); 151 } 152 153 private class Castable 154 { 155 private readonly int _value; 156 Castable(int value)157 private Castable(int value) 158 { 159 _value = value; 160 } 161 162 public int Value { get { return _value; } } 163 operator Castable(Int32 value)164 public static explicit operator Castable(Int32 value) 165 { 166 return new Castable(value); 167 } 168 } 169 } 170 } 171