Skip to content

Commit

Permalink
Fix bug in LastOrDefault for extrema sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Aug 14, 2020
1 parent dae766c commit 4674730
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 57 deletions.
56 changes: 28 additions & 28 deletions MoreLinq.Test/MaxByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,32 @@ public class First
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).First(), Is.EqualTo("hello"));
var maxima = strings.MaxBy(s => s.Length);
Assert.That(MoreEnumerable.First(maxima), Is.EqualTo("hello"));
}

[Test]
public void WithComparerReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.First(),
Is.EqualTo("ax"));
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.First(maxima), Is.EqualTo("ax"));
}

[Test]
public void WithEmptySourceThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
strings.MaxBy(s => s.Length).First());
MoreEnumerable.First(strings.MaxBy(s => s.Length)));
}

[Test]
public void WithEmptySourceWithComparerThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer).First());
MoreEnumerable.First(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)));
}
}

Expand All @@ -103,32 +103,32 @@ public class FirstOrDefault
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).FirstOrDefault(), Is.EqualTo("hello"));
var maxima = strings.MaxBy(s => s.Length);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.EqualTo("hello"));
}

[Test]
public void WithComparerReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.FirstOrDefault(),
Is.EqualTo("ax"));
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.EqualTo("ax"));
}

[Test]
public void WithEmptySourceReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).FirstOrDefault(), Is.Null);
var maxima = strings.MaxBy(s => s.Length);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.Null);
}

[Test]
public void WithEmptySourceWithComparerReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.FirstOrDefault(),
Is.Null);
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.Null);
}
}

Expand All @@ -138,32 +138,32 @@ public class Last
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).Last(), Is.EqualTo("world"));
var maxima = strings.MaxBy(s => s.Length);
Assert.That(MoreEnumerable.Last(maxima), Is.EqualTo("world"));
}

[Test]
public void WithComparerReturnsMaximumPerComparer()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.Last(),
Is.EqualTo("az"));
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.Last(maxima), Is.EqualTo("az"));
}

[Test]
public void WithEmptySourceThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
strings.MaxBy(s => s.Length).Last());
MoreEnumerable.Last(strings.MaxBy(s => s.Length)));
}

[Test]
public void WithEmptySourceWithComparerThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer).Last());
MoreEnumerable.Last(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)));
}
}

Expand All @@ -173,32 +173,32 @@ public class LastOrDefault
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).LastOrDefault(), Is.EqualTo("world"));
var maxima = strings.MaxBy(s => s.Length);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.EqualTo("world"));
}

[Test]
public void WithComparerReturnsMaximumPerComparer()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.LastOrDefault(),
Is.EqualTo("az"));
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.EqualTo("az"));
}

[Test]
public void WithEmptySourceReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).LastOrDefault(), Is.Null);
var maxima = strings.MaxBy(s => s.Length);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.Null);
}

[Test]
public void WithEmptySourceWithComparerReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.LastOrDefault(),
Is.Null);
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.Null);
}
}

Expand Down
56 changes: 28 additions & 28 deletions MoreLinq.Test/MinByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,32 @@ public class First
public void ReturnsMinimum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length).First(), Is.EqualTo("ax"));
var minima = MoreEnumerable.First(strings.MinBy(s => s.Length));
Assert.That(minima, Is.EqualTo("ax"));
}

[Test]
public void WithComparerReturnsMinimum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.First(),
Is.EqualTo("hello"));
var minima = strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.First(minima), Is.EqualTo("hello"));
}

[Test]
public void WithEmptySourceThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
strings.MinBy(s => s.Length).First());
MoreEnumerable.First(strings.MinBy(s => s.Length)));
}

[Test]
public void WithEmptySourceWithComparerThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer).First());
MoreEnumerable.First(strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer)));
}
}

Expand All @@ -103,32 +103,32 @@ public class FirstOrDefault
public void ReturnsMinimum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length).FirstOrDefault(), Is.EqualTo("ax"));
var minima = strings.MinBy(s => s.Length);
Assert.That(MoreEnumerable.FirstOrDefault(minima), Is.EqualTo("ax"));
}

[Test]
public void WithComparerReturnsMinimum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.FirstOrDefault(),
Is.EqualTo("hello"));
var minima = strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.FirstOrDefault(minima), Is.EqualTo("hello"));
}

[Test]
public void WithEmptySourceReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length).FirstOrDefault(), Is.Null);
var minima = strings.MinBy(s => s.Length);
Assert.That(MoreEnumerable.FirstOrDefault(minima), Is.Null);
}

[Test]
public void WithEmptySourceWithComparerReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.FirstOrDefault(),
Is.Null);
var minima = strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.FirstOrDefault(minima), Is.Null);
}
}

Expand All @@ -138,32 +138,32 @@ public class Last
public void ReturnsMinimum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length).Last(), Is.EqualTo("az"));
var minima = strings.MinBy(s => s.Length);
Assert.That(MoreEnumerable.Last(minima), Is.EqualTo("az"));
}

[Test]
public void WithComparerReturnsMinimumPerComparer()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.Last(),
Is.EqualTo("world"));
var minima = strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.Last(minima), Is.EqualTo("world"));
}

[Test]
public void WithEmptySourceThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
strings.MinBy(s => s.Length).Last());
MoreEnumerable.Last(strings.MinBy(s => s.Length)));
}

[Test]
public void WithEmptySourceWithComparerThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer).Last());
MoreEnumerable.Last(strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer)));
}
}

Expand All @@ -173,32 +173,32 @@ public class LastOrDefault
public void ReturnsMinimum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length).LastOrDefault(), Is.EqualTo("az"));
var minima = strings.MinBy(s => s.Length);
Assert.That(MoreEnumerable.LastOrDefault(minima), Is.EqualTo("az"));
}

[Test]
public void WithComparerReturnsMinimumPerComparer()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.LastOrDefault(),
Is.EqualTo("world"));
var minima = strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.LastOrDefault(minima), Is.EqualTo("world"));
}

[Test]
public void WithEmptySourceReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length).LastOrDefault(), Is.Null);
var minima = strings.MinBy(s => s.Length);
Assert.That(MoreEnumerable.LastOrDefault(minima), Is.Null);
}

[Test]
public void WithEmptySourceWithComparerReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer)
.LastOrDefault(),
Is.Null);
var minima = strings.MinBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.LastOrDefault(minima), Is.Null);
}
}

Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/MaxBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static T Last<T>(this IExtremaEnumerable<T> source)
public static T LastOrDefault<T>(this IExtremaEnumerable<T> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source.Take(1).AsEnumerable().LastOrDefault();
return source.TakeLast(1).AsEnumerable().LastOrDefault();
}

/// <summary>
Expand Down

0 comments on commit 4674730

Please sign in to comment.