Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Commit

Permalink
Added argument validation, compatibility with OrderBy behaviour for n…
Browse files Browse the repository at this point in the history
…ull 'comparer'.
  • Loading branch information
bgrainger committed Apr 9, 2010
1 parent a444540 commit 1c7dcfa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Logos.Utility/EnumerableUtility.cs
Expand Up @@ -31,7 +31,7 @@ public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
/// elements that are requested from the return value.</remarks>
public static IOrderedEnumerable<TSource> LazyOrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
return LazyOrderBy(source, keySelector, Comparer<TKey>.Default);
return LazyOrderBy(source, keySelector, null, false);
}

/// <summary>
Expand All @@ -45,7 +45,7 @@ public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
/// elements that are requested from the return value.</remarks>
public static IOrderedEnumerable<TSource> LazyOrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
{
return new OrderedEnumerable<TSource>(source, new ElementComparer<TSource, TKey>(keySelector, comparer, false, null));
return LazyOrderBy(source, keySelector, comparer, false);
}

/// <summary>
Expand All @@ -58,7 +58,7 @@ public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
/// elements that are requested from the return value.</remarks>
public static IOrderedEnumerable<TSource> LazyOrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
return LazyOrderByDescending(source, keySelector, Comparer<TKey>.Default);
return LazyOrderBy(source, keySelector, null, true);
}

/// <summary>
Expand All @@ -72,7 +72,17 @@ public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
/// elements that are requested from the return value.</remarks>
public static IOrderedEnumerable<TSource> LazyOrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
{
return new OrderedEnumerable<TSource>(source, new ElementComparer<TSource, TKey>(keySelector, comparer, true, null));
return LazyOrderBy(source, keySelector, comparer, true);
}

private static IOrderedEnumerable<TSource> LazyOrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, bool descending)
{
if (source == null)
throw new ArgumentNullException("source");
if (keySelector == null)
throw new ArgumentNullException("keySelector");

return new OrderedEnumerable<TSource>(source, new ElementComparer<TSource, TKey>(keySelector, comparer ?? Comparer<TKey>.Default, descending, null));
}

/// <summary>
Expand Down
40 changes: 40 additions & 0 deletions tests/Logos.Utility.Tests/OrderedEnumerableTests.cs
Expand Up @@ -138,6 +138,46 @@ public void SortFirstDescendingCustom()
CollectionAssert.AreEqual(sorted1, sorted2);
}

[Test]
public void AscendingNullArguments()
{
Assert.Throws<ArgumentNullException>(() => ((IEnumerable<int>) null).OrderBy(x => x));
Assert.Throws<ArgumentNullException>(() => Enumerable.Range(0, 10).OrderBy((Func<int, int>) null));
}

[Test]
public void DescendingNullArguments()
{
Assert.Throws<ArgumentNullException>(() => ((IEnumerable<int>) null).OrderByDescending(x => x));
Assert.Throws<ArgumentNullException>(() => Enumerable.Range(0, 10).OrderByDescending((Func<int, int>) null));
}

[Test]
public void DefaultComparerAscending()
{
Random random = new Random(9);
int[] numbers = new int[100000];
for (int index = 0; index < numbers.Length; index++)
numbers[index] = random.Next();

var sorted1 = numbers.OrderBy(x => x, null);
var sorted2 = numbers.LazyOrderBy(x => x, null);
CollectionAssert.AreEqual(sorted1, sorted2);
}

[Test]
public void DefaultComparerDescending()
{
Random random = new Random(9);
int[] numbers = new int[100000];
for (int index = 0; index < numbers.Length; index++)
numbers[index] = random.Next();

var sorted1 = numbers.OrderByDescending(x => x, null);
var sorted2 = numbers.LazyOrderByDescending(x => x, null);
CollectionAssert.AreEqual(sorted1, sorted2);
}

// The Triple struct can be used to verify that the sort is stable by sorting by the First (and Second)
// properties, then using the Third property to verify that objects are in the right order.
[DebuggerDisplay("F={m_first} S={m_second} T={m_third}")]
Expand Down

0 comments on commit 1c7dcfa

Please sign in to comment.