Selaa lähdekoodia

Comparers can be null.

Bart De Smet 7 vuotta sitten
vanhempi
sitoutus
9ab0315d61

+ 0 - 2
Ix.NET/Source/System.Interactive.Async.Tests/System/Linq/Operators/DistinctUntilChanged.cs

@@ -18,14 +18,12 @@ namespace Tests
             AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.DistinctUntilChanged(default(IAsyncEnumerable<int>)));
 
             AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.DistinctUntilChanged(default, EqualityComparer<int>.Default));
-            AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.DistinctUntilChanged(Return42, default));
 
             AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.DistinctUntilChanged(default(IAsyncEnumerable<int>), x => x));
             AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.DistinctUntilChanged(Return42, default(Func<int, int>)));
 
             AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.DistinctUntilChanged(default(IAsyncEnumerable<int>), x => x, EqualityComparer<int>.Default));
             AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.DistinctUntilChanged(Return42, default(Func<int, int>), EqualityComparer<int>.Default));
-            AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.DistinctUntilChanged(Return42, x => x, default));
         }
 
         [Fact]

+ 0 - 2
Ix.NET/Source/System.Interactive.Async.Tests/System/Linq/Operators/Max.cs

@@ -17,10 +17,8 @@ namespace Tests
         public async Task Max_Null()
         {
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.Max(default, Comparer<DateTime>.Default));
-            await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.Max(AsyncEnumerable.Empty<DateTime>(), default));
 
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.Max(default, Comparer<DateTime>.Default, CancellationToken.None));
-            await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.Max(AsyncEnumerable.Empty<DateTime>(), default, CancellationToken.None));
         }
     }
 }

+ 0 - 2
Ix.NET/Source/System.Interactive.Async.Tests/System/Linq/Operators/MaxBy.cs

@@ -21,14 +21,12 @@ namespace Tests
 
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxBy(default(IAsyncEnumerable<int>), x => x, Comparer<int>.Default));
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxBy(Return42, default(Func<int, int>), Comparer<int>.Default));
-            await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxBy(Return42, x => x, default(IComparer<int>)));
 
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxBy(default(IAsyncEnumerable<int>), x => x, CancellationToken.None));
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxBy(Return42, default(Func<int, int>), CancellationToken.None));
 
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxBy(default(IAsyncEnumerable<int>), x => x, Comparer<int>.Default, CancellationToken.None));
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxBy(Return42, default(Func<int, int>), Comparer<int>.Default, CancellationToken.None));
-            await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxBy(Return42, x => x, default, CancellationToken.None));
         }
 
         [Fact]

+ 0 - 2
Ix.NET/Source/System.Interactive.Async.Tests/System/Linq/Operators/Min.cs

@@ -17,10 +17,8 @@ namespace Tests
         public async Task Min_Null()
         {
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.Min(default, Comparer<DateTime>.Default));
-            await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.Min(AsyncEnumerable.Empty<DateTime>(), default));
 
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.Min(default, Comparer<DateTime>.Default, CancellationToken.None));
-            await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.Min(AsyncEnumerable.Empty<DateTime>(), default, CancellationToken.None));
         }
     }
 }

+ 0 - 2
Ix.NET/Source/System.Interactive.Async.Tests/System/Linq/Operators/MinBy.cs

@@ -21,14 +21,12 @@ namespace Tests
 
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable<int>), x => x, Comparer<int>.Default));
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, default(Func<int, int>), Comparer<int>.Default));
-            await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, x => x, default(IComparer<int>)));
 
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable<int>), x => x, CancellationToken.None));
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, default(Func<int, int>), CancellationToken.None));
 
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable<int>), x => x, Comparer<int>.Default, CancellationToken.None));
             await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, default(Func<int, int>), Comparer<int>.Default, CancellationToken.None));
-            await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, x => x, default, CancellationToken.None));
         }
 
         [Fact]

+ 2 - 6
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Distinct.cs

@@ -18,7 +18,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return DistinctCore(source, keySelector, EqualityComparer<TKey>.Default);
+            return DistinctCore(source, keySelector, comparer: null);
         }
 
         public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
@@ -27,8 +27,6 @@ namespace System.Linq
                 throw new ArgumentNullException(nameof(source));
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return DistinctCore(source, keySelector, comparer);
         }
@@ -40,7 +38,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return DistinctCore(source, keySelector, EqualityComparer<TKey>.Default);
+            return DistinctCore<TSource, TKey>(source, keySelector, comparer: null);
         }
 
         public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
@@ -78,7 +76,6 @@ namespace System.Linq
             {
                 Debug.Assert(source != null);
                 Debug.Assert(keySelector != null);
-                Debug.Assert(comparer != null);
 
                 _source = source;
                 _keySelector = keySelector;
@@ -224,7 +221,6 @@ namespace System.Linq
             {
                 Debug.Assert(source != null);
                 Debug.Assert(keySelector != null);
-                Debug.Assert(comparer != null);
 
                 _source = source;
                 _keySelector = keySelector;

+ 6 - 11
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/DistinctUntilChanged.cs

@@ -16,15 +16,13 @@ namespace System.Linq
             if (source == null)
                 throw new ArgumentNullException(nameof(source));
 
-            return DistinctUntilChangedCore(source, EqualityComparer<TSource>.Default);
+            return DistinctUntilChangedCore(source, comparer: null);
         }
 
         public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
         {
             if (source == null)
                 throw new ArgumentNullException(nameof(source));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return DistinctUntilChangedCore(source, comparer);
         }
@@ -36,7 +34,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return DistinctUntilChangedCore(source, keySelector, EqualityComparer<TKey>.Default);
+            return DistinctUntilChangedCore(source, keySelector, comparer: null);
         }
 
         public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
@@ -45,8 +43,6 @@ namespace System.Linq
                 throw new ArgumentNullException(nameof(source));
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return DistinctUntilChangedCore(source, keySelector, comparer);
         }
@@ -58,7 +54,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return DistinctUntilChangedCore(source, keySelector, EqualityComparer<TKey>.Default);
+            return DistinctUntilChangedCore<TSource, TKey>(source, keySelector, comparer: null);
         }
 
         public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
@@ -99,11 +95,10 @@ namespace System.Linq
 
             public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
             {
-                Debug.Assert(comparer != null);
                 Debug.Assert(source != null);
 
                 _source = source;
-                _comparer = comparer;
+                _comparer = comparer ?? EqualityComparer<TSource>.Default;
             }
 
             public override AsyncIterator<TSource> Clone()
@@ -174,7 +169,7 @@ namespace System.Linq
             {
                 _source = source;
                 _keySelector = keySelector;
-                _comparer = comparer;
+                _comparer = comparer ?? EqualityComparer<TKey>.Default;
             }
 
             public override AsyncIterator<TSource> Clone()
@@ -245,7 +240,7 @@ namespace System.Linq
             {
                 _source = source;
                 _keySelector = keySelector;
-                _comparer = comparer;
+                _comparer = comparer ?? EqualityComparer<TKey>.Default;
             }
 
             public override AsyncIterator<TSource> Clone()

+ 6 - 5
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Max.cs

@@ -14,8 +14,6 @@ namespace System.Linq
         {
             if (source == null)
                 throw new ArgumentNullException(nameof(source));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MaxCore(source, comparer, CancellationToken.None);
         }
@@ -24,14 +22,17 @@ namespace System.Linq
         {
             if (source == null)
                 throw new ArgumentNullException(nameof(source));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
-
+            
             return MaxCore(source, comparer, cancellationToken);
         }
 
         private static async Task<TSource> MaxCore<TSource>(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
         {
+            if (comparer == null)
+            {
+                comparer = Comparer<TSource>.Default;
+            }
+
             var e = source.GetAsyncEnumerator(cancellationToken);
 
             try

+ 14 - 12
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/MaxBy.cs

@@ -17,7 +17,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return MaxByCore(source, keySelector, Comparer<TKey>.Default, CancellationToken.None);
+            return MaxByCore(source, keySelector, comparer: null, CancellationToken.None);
         }
 
         public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
@@ -27,7 +27,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return MaxByCore(source, keySelector, Comparer<TKey>.Default, cancellationToken);
+            return MaxByCore(source, keySelector, comparer: null, cancellationToken);
         }
 
         public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
@@ -36,8 +36,6 @@ namespace System.Linq
                 throw new ArgumentNullException(nameof(source));
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MaxByCore(source, keySelector, comparer, CancellationToken.None);
         }
@@ -48,8 +46,6 @@ namespace System.Linq
                 throw new ArgumentNullException(nameof(source));
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MaxByCore(source, keySelector, comparer, cancellationToken);
         }
@@ -61,7 +57,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return MaxByCore(source, keySelector, Comparer<TKey>.Default, CancellationToken.None);
+            return MaxByCore<TSource, TKey>(source, keySelector, comparer: null, CancellationToken.None);
         }
 
         public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, CancellationToken cancellationToken)
@@ -71,7 +67,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return MaxByCore(source, keySelector, Comparer<TKey>.Default, cancellationToken);
+            return MaxByCore<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
         }
 
         public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IComparer<TKey> comparer)
@@ -80,8 +76,6 @@ namespace System.Linq
                 throw new ArgumentNullException(nameof(source));
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MaxByCore(source, keySelector, comparer, CancellationToken.None);
         }
@@ -92,19 +86,27 @@ namespace System.Linq
                 throw new ArgumentNullException(nameof(source));
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MaxByCore(source, keySelector, comparer, cancellationToken);
         }
 
         private static Task<IList<TSource>> MaxByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
         {
+            if (comparer == null)
+            {
+                comparer = Comparer<TKey>.Default;
+            }
+
             return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
         }
 
         private static Task<IList<TSource>> MaxByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
         {
+            if (comparer == null)
+            {
+                comparer = Comparer<TKey>.Default;
+            }
+
             return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
         }
     }

+ 5 - 4
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Min.cs

@@ -14,8 +14,6 @@ namespace System.Linq
         {
             if (source == null)
                 throw new ArgumentNullException(nameof(source));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MinCore(source, comparer, CancellationToken.None);
         }
@@ -24,14 +22,17 @@ namespace System.Linq
         {
             if (source == null)
                 throw new ArgumentNullException(nameof(source));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MinCore(source, comparer, cancellationToken);
         }
 
         private static async Task<TSource> MinCore<TSource>(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
         {
+            if (comparer == null)
+            {
+                comparer = Comparer<TSource>.Default;
+            }
+
             var e = source.GetAsyncEnumerator(cancellationToken);
 
             try

+ 14 - 12
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/MinBy.cs

@@ -17,7 +17,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return MinByCore(source, keySelector, Comparer<TKey>.Default, CancellationToken.None);
+            return MinByCore(source, keySelector, comparer: null, CancellationToken.None);
         }
 
         public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
@@ -27,7 +27,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return MinByCore(source, keySelector, Comparer<TKey>.Default, cancellationToken);
+            return MinByCore(source, keySelector, comparer: null, cancellationToken);
         }
 
         public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
@@ -36,8 +36,6 @@ namespace System.Linq
                 throw new ArgumentNullException(nameof(source));
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MinByCore(source, keySelector, comparer, CancellationToken.None);
         }
@@ -48,8 +46,6 @@ namespace System.Linq
                 throw new ArgumentNullException(nameof(source));
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MinByCore(source, keySelector, comparer, cancellationToken);
         }
@@ -61,7 +57,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return MinByCore(source, keySelector, Comparer<TKey>.Default, CancellationToken.None);
+            return MinByCore<TSource, TKey>(source, keySelector, comparer: null, CancellationToken.None);
         }
 
         public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, CancellationToken cancellationToken)
@@ -71,7 +67,7 @@ namespace System.Linq
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
 
-            return MinByCore(source, keySelector, Comparer<TKey>.Default, cancellationToken);
+            return MinByCore<TSource, TKey>(source, keySelector, comparer: null, cancellationToken);
         }
 
         public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IComparer<TKey> comparer)
@@ -80,8 +76,6 @@ namespace System.Linq
                 throw new ArgumentNullException(nameof(source));
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MinByCore(source, keySelector, comparer, CancellationToken.None);
         }
@@ -92,19 +86,27 @@ namespace System.Linq
                 throw new ArgumentNullException(nameof(source));
             if (keySelector == null)
                 throw new ArgumentNullException(nameof(keySelector));
-            if (comparer == null)
-                throw new ArgumentNullException(nameof(comparer));
 
             return MinByCore(source, keySelector, comparer, cancellationToken);
         }
 
         private static Task<IList<TSource>> MinByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
         {
+            if (comparer == null)
+            {
+                comparer = Comparer<TKey>.Default;
+            }
+
             return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
         }
 
         private static Task<IList<TSource>> MinByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
         {
+            if (comparer == null)
+            {
+                comparer = Comparer<TKey>.Default;
+            }
+
             return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
         }