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 ToDictionaryTests 11 { 12 [Theory] 13 [InlineData(0)] 14 [InlineData(1)] 15 [InlineData(2)] 16 [InlineData(16)] ToDictionary(int count)17 public static void ToDictionary(int count) 18 { 19 IntegerRangeSet seen = new IntegerRangeSet(0, count); 20 Assert.All(UnorderedSources.Default(count).ToDictionary(x => x * 2), 21 p => { seen.Add(p.Key / 2); Assert.Equal(p.Key, p.Value * 2); }); 22 seen.AssertComplete(); 23 } 24 25 [Fact] 26 [OuterLoop] ToDictionary_Longrunning()27 public static void ToDictionary_Longrunning() 28 { 29 ToDictionary(Sources.OuterLoopCount); 30 } 31 32 [Theory] 33 [InlineData(0)] 34 [InlineData(1)] 35 [InlineData(2)] 36 [InlineData(16)] ToDictionary_ElementSelector(int count)37 public static void ToDictionary_ElementSelector(int count) 38 { 39 IntegerRangeSet seen = new IntegerRangeSet(0, count); 40 Assert.All(UnorderedSources.Default(count).ToDictionary(x => x, y => y * 2), 41 p => { seen.Add(p.Key); Assert.Equal(p.Key * 2, p.Value); }); 42 seen.AssertComplete(); 43 } 44 45 [Fact] 46 [OuterLoop] ToDictionary_ElementSelector_Longrunning()47 public static void ToDictionary_ElementSelector_Longrunning() 48 { 49 ToDictionary_ElementSelector(Sources.OuterLoopCount); 50 } 51 52 [Theory] 53 [InlineData(0)] 54 [InlineData(1)] 55 [InlineData(2)] 56 [InlineData(16)] ToDictionary_CustomComparator(int count)57 public static void ToDictionary_CustomComparator(int count) 58 { 59 IntegerRangeSet seen = new IntegerRangeSet(0, count); 60 Assert.All(UnorderedSources.Default(count).ToDictionary(x => x * 2, new ModularCongruenceComparer(count * 2)), 61 p => { seen.Add(p.Key / 2); Assert.Equal(p.Key, p.Value * 2); }); 62 seen.AssertComplete(); 63 } 64 65 [Fact] 66 [OuterLoop] ToDictionary_CustomComparator_Longrunning()67 public static void ToDictionary_CustomComparator_Longrunning() 68 { 69 ToDictionary_CustomComparator(Sources.OuterLoopCount); 70 } 71 72 [Theory] 73 [InlineData(0)] 74 [InlineData(1)] 75 [InlineData(2)] 76 [InlineData(16)] ToDictionary_ElementSelector_CustomComparator(int count)77 public static void ToDictionary_ElementSelector_CustomComparator(int count) 78 { 79 IntegerRangeSet seen = new IntegerRangeSet(0, count); 80 Assert.All(UnorderedSources.Default(count).ToDictionary(x => x, y => y * 2, new ModularCongruenceComparer(count)), 81 p => { seen.Add(p.Key); Assert.Equal(p.Key * 2, p.Value); }); 82 seen.AssertComplete(); 83 } 84 85 [Fact] 86 [OuterLoop] ToDictionary_ElementSelector_CustomComparator_Longrunning()87 public static void ToDictionary_ElementSelector_CustomComparator_Longrunning() 88 { 89 ToDictionary_ElementSelector_CustomComparator(Sources.OuterLoopCount); 90 } 91 92 [Theory] 93 [InlineData(0)] 94 [InlineData(1)] 95 [InlineData(2)] 96 [InlineData(16)] ToDictionary_UniqueKeys_CustomComparator(int count)97 public static void ToDictionary_UniqueKeys_CustomComparator(int count) 98 { 99 if (count > 2) 100 { 101 ArgumentException e = AssertThrows.Wrapped<ArgumentException>(() => UnorderedSources.Default(count).ToDictionary(x => x, new ModularCongruenceComparer(2))); 102 } 103 else if (count == 1 || count == 2) 104 { 105 IntegerRangeSet seen = new IntegerRangeSet(0, count); 106 foreach (KeyValuePair<int, int> entry in UnorderedSources.Default(count).ToDictionary(x => x, new ModularCongruenceComparer(2))) 107 { 108 seen.Add(entry.Key); 109 Assert.Equal(entry.Key, entry.Value); 110 } 111 seen.AssertComplete(); 112 } 113 else 114 { 115 Assert.Empty(UnorderedSources.Default(count).ToDictionary(x => x, new ModularCongruenceComparer(2))); 116 } 117 } 118 119 [Fact] 120 [OuterLoop] ToDictionary_UniqueKeys_CustomComparator_Longrunning()121 public static void ToDictionary_UniqueKeys_CustomComparator_Longrunning() 122 { 123 ToDictionary_UniqueKeys_CustomComparator(Sources.OuterLoopCount); 124 } 125 126 [Theory] 127 [InlineData(0)] 128 [InlineData(1)] 129 [InlineData(2)] 130 [InlineData(16)] ToDictionary_ElementSelector_UniqueKeys_CustomComparator(int count)131 public static void ToDictionary_ElementSelector_UniqueKeys_CustomComparator(int count) 132 { 133 if (count > 2) 134 { 135 AssertThrows.Wrapped<ArgumentException>(() => UnorderedSources.Default(count).ToDictionary(x => x, y => y, new ModularCongruenceComparer(2))); 136 } 137 else if (count == 1 || count == 2) 138 { 139 IntegerRangeSet seen = new IntegerRangeSet(0, count); 140 foreach (KeyValuePair<int, int> entry in UnorderedSources.Default(count).ToDictionary(x => x, y => y, new ModularCongruenceComparer(2))) 141 { 142 seen.Add(entry.Key); 143 Assert.Equal(entry.Key, entry.Value); 144 } 145 seen.AssertComplete(); 146 } 147 else 148 { 149 Assert.Empty(UnorderedSources.Default(count).ToDictionary(x => x, y => y, new ModularCongruenceComparer(2))); 150 } 151 } 152 153 [Fact] 154 [OuterLoop] ToDictionary_ElementSelector_UniqueKeys_CustomComparator_Longrunning()155 public static void ToDictionary_ElementSelector_UniqueKeys_CustomComparator_Longrunning() 156 { 157 ToDictionary_ElementSelector_UniqueKeys_CustomComparator(Sources.OuterLoopCount); 158 } 159 160 [Fact] ToDictionary_DuplicateKeys()161 public static void ToDictionary_DuplicateKeys() 162 { 163 AssertThrows.Wrapped<ArgumentException>(() => ParallelEnumerable.Repeat(0, 2).ToDictionary(x => x)); 164 } 165 166 [Fact] ToDictionary_DuplicateKeys_ElementSelector()167 public static void ToDictionary_DuplicateKeys_ElementSelector() 168 { 169 AssertThrows.Wrapped<ArgumentException>(() => ParallelEnumerable.Repeat(0, 2).ToDictionary(x => x, y => y)); 170 } 171 172 [Fact] ToDictionary_DuplicateKeys_CustomComparator()173 public static void ToDictionary_DuplicateKeys_CustomComparator() 174 { 175 AssertThrows.Wrapped<ArgumentException>(() => ParallelEnumerable.Repeat(0, 2).ToDictionary(x => x, new ModularCongruenceComparer(2))); 176 } 177 178 [Fact] ToDictionary_DuplicateKeys_ElementSelector_CustomComparator()179 public static void ToDictionary_DuplicateKeys_ElementSelector_CustomComparator() 180 { 181 AssertThrows.Wrapped<ArgumentException>(() => ParallelEnumerable.Repeat(0, 2).ToDictionary(x => x, y => y, new ModularCongruenceComparer(2))); 182 } 183 184 [Fact] ToDictionary_OperationCanceledException()185 public static void ToDictionary_OperationCanceledException() 186 { 187 AssertThrows.EventuallyCanceled((source, canceler) => source.ToDictionary(x => x, new CancelingEqualityComparer<int>(canceler))); 188 AssertThrows.EventuallyCanceled((source, canceler) => source.ToDictionary(x => x, y => y, new CancelingEqualityComparer<int>(canceler))); 189 } 190 191 [Fact] ToDictionary_AggregateException_Wraps_OperationCanceledException()192 public static void ToDictionary_AggregateException_Wraps_OperationCanceledException() 193 { 194 AssertThrows.OtherTokenCanceled((source, canceler) => source.ToDictionary(x => x, new CancelingEqualityComparer<int>(canceler))); 195 AssertThrows.OtherTokenCanceled((source, canceler) => source.ToDictionary(x => x, y => y, new CancelingEqualityComparer<int>(canceler))); 196 AssertThrows.SameTokenNotCanceled((source, canceler) => source.ToDictionary(x => x, new CancelingEqualityComparer<int>(canceler))); 197 AssertThrows.SameTokenNotCanceled((source, canceler) => source.ToDictionary(x => x, y => y, new CancelingEqualityComparer<int>(canceler))); 198 } 199 200 [Fact] ToDictionary_OperationCanceledException_PreCanceled()201 public static void ToDictionary_OperationCanceledException_PreCanceled() 202 { 203 AssertThrows.AlreadyCanceled(source => source.ToDictionary(x => x)); 204 AssertThrows.AlreadyCanceled(source => source.ToDictionary(x => x, EqualityComparer<int>.Default)); 205 AssertThrows.AlreadyCanceled(source => source.ToDictionary(x => x, y => y)); 206 AssertThrows.AlreadyCanceled(source => source.ToDictionary(x => x, y => y, EqualityComparer<int>.Default)); 207 } 208 209 [Theory] 210 [MemberData(nameof(UnorderedSources.Ranges), new[] { 1 }, MemberType = typeof(UnorderedSources))] ToDictionary_AggregateException(Labeled<ParallelQuery<int>> labeled, int count)211 public static void ToDictionary_AggregateException(Labeled<ParallelQuery<int>> labeled, int count) 212 { 213 AssertThrows.Wrapped<DeliberateTestException>(() => labeled.Item.ToDictionary((Func<int, int>)(x => { throw new DeliberateTestException(); }))); 214 AssertThrows.Wrapped<DeliberateTestException>(() => labeled.Item.ToDictionary((Func<int, int>)(x => { throw new DeliberateTestException(); }), y => y)); 215 AssertThrows.Wrapped<DeliberateTestException>(() => labeled.Item.ToDictionary(x => x, (Func<int, int>)(y => { throw new DeliberateTestException(); }))); 216 217 AssertThrows.Wrapped<DeliberateTestException>(() => labeled.Item.ToDictionary((Func<int, int>)(x => { throw new DeliberateTestException(); }), EqualityComparer<int>.Default)); 218 AssertThrows.Wrapped<DeliberateTestException>(() => labeled.Item.ToDictionary((Func<int, int>)(x => { throw new DeliberateTestException(); }), y => y, EqualityComparer<int>.Default)); 219 AssertThrows.Wrapped<DeliberateTestException>(() => labeled.Item.ToDictionary(x => x, (Func<int, int>)(y => { throw new DeliberateTestException(); }), EqualityComparer<int>.Default)); 220 221 AssertThrows.Wrapped<DeliberateTestException>(() => labeled.Item.ToDictionary(x => x, new FailingEqualityComparer<int>())); 222 AssertThrows.Wrapped<DeliberateTestException>(() => labeled.Item.ToDictionary(x => x, y => y, new FailingEqualityComparer<int>())); 223 } 224 225 [Fact] ToDictionary_ArgumentNullException()226 public static void ToDictionary_ArgumentNullException() 227 { 228 AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<int>)null).ToDictionary(x => x)); 229 AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<int>)null).ToDictionary(x => x, EqualityComparer<int>.Default)); 230 AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<int>)null).ToDictionary(x => x, y => y)); 231 AssertExtensions.Throws<ArgumentNullException>("source", () => ((ParallelQuery<int>)null).ToDictionary(x => x, y => y, EqualityComparer<int>.Default)); 232 AssertExtensions.Throws<ArgumentNullException>("keySelector", () => ParallelEnumerable.Empty<int>().ToDictionary((Func<int, int>)null)); 233 AssertExtensions.Throws<ArgumentNullException>("keySelector", () => ParallelEnumerable.Empty<int>().ToDictionary((Func<int, int>)null, EqualityComparer<int>.Default)); 234 AssertExtensions.Throws<ArgumentNullException>("keySelector", () => ParallelEnumerable.Empty<int>().ToDictionary((Func<int, int>)null, y => y)); 235 AssertExtensions.Throws<ArgumentNullException>("keySelector", () => ParallelEnumerable.Empty<int>().ToDictionary((Func<int, int>)null, y => y, EqualityComparer<int>.Default)); 236 AssertExtensions.Throws<ArgumentNullException>("elementSelector", () => ParallelEnumerable.Empty<int>().ToDictionary(x => x, (Func<int, int>)null)); 237 AssertExtensions.Throws<ArgumentNullException>("elementSelector", () => ParallelEnumerable.Empty<int>().ToDictionary(x => x, (Func<int, int>)null, EqualityComparer<int>.Default)); 238 } 239 } 240 } 241