Skip to content

Commit

Permalink
Add "Minima" & "Maxima" that supersede "MinBy" & "MaxBy"
Browse files Browse the repository at this point in the history
This is a squashed merge of PR #1019 that closes #1018.
  • Loading branch information
atifaziz authored Oct 24, 2023
1 parent 137564f commit 66f7cab
Show file tree
Hide file tree
Showing 14 changed files with 734 additions and 380 deletions.
68 changes: 34 additions & 34 deletions MoreLinq.Test/MaxByTest.cs → MoreLinq.Test/MaximaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,44 @@ namespace MoreLinq.Test
using NUnit.Framework;

[TestFixture]
public class MaxByTest
public class MaximaTest
{
[Test]
public void MaxByIsLazy()
public void MaximaIsLazy()
{
_ = new BreakingSequence<int>().MaxBy(BreakingFunc.Of<int, int>());
_ = new BreakingSequence<int>().Maxima(BreakingFunc.Of<int, int>());
}

[Test]
public void MaxByReturnsMaxima()
public void MaximaReturnsMaxima()
{
Assert.That(SampleData.Strings.MaxBy(x => x.Length),
Assert.That(SampleData.Strings.Maxima(x => x.Length),
Is.EqualTo(new[] { "hello", "world" }));
}

[Test]
public void MaxByNullComparer()
public void MaximaNullComparer()
{
Assert.That(SampleData.Strings.MaxBy(x => x.Length, null),
Is.EqualTo(SampleData.Strings.MaxBy(x => x.Length)));
Assert.That(SampleData.Strings.Maxima(x => x.Length, null),
Is.EqualTo(SampleData.Strings.Maxima(x => x.Length)));
}

[Test]
public void MaxByEmptySequence()
public void MaximaEmptySequence()
{
Assert.That(new string[0].MaxBy(x => x.Length), Is.Empty);
Assert.That(new string[0].Maxima(x => x.Length), Is.Empty);
}

[Test]
public void MaxByWithNaturalComparer()
public void MaximaWithNaturalComparer()
{
Assert.That(SampleData.Strings.MaxBy(x => x[1]), Is.EqualTo(new[] { "az" }));
Assert.That(SampleData.Strings.Maxima(x => x[1]), Is.EqualTo(new[] { "az" }));
}

[Test]
public void MaxByWithComparer()
public void MaximaWithComparer()
{
Assert.That(SampleData.Strings.MaxBy(x => x[1], Comparable<char>.DescendingOrderComparer), Is.EqualTo(new[] { "aa" }));
Assert.That(SampleData.Strings.Maxima(x => x[1], Comparable<char>.DescendingOrderComparer), Is.EqualTo(new[] { "aa" }));
}

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

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

Expand All @@ -83,7 +83,7 @@ public void WithEmptySourceThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(() =>
MoreEnumerable.First(strings.MaxBy(s => s.Length)),
MoreEnumerable.First(strings.Maxima(s => s.Length)),
Throws.InvalidOperationException);
}

Expand All @@ -92,7 +92,7 @@ public void WithEmptySourceWithComparerThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(() =>
MoreEnumerable.First(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)),
MoreEnumerable.First(strings.Maxima(s => s.Length, Comparable<int>.DescendingOrderComparer)),
Throws.InvalidOperationException);
}
}
Expand All @@ -103,31 +103,31 @@ public class FirstOrDefault
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.Maxima(s => s.Length);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.EqualTo("hello"));
}

[Test]
public void WithComparerReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
var maxima = strings.Maxima(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();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.Maxima(s => s.Length);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.Null);
}

[Test]
public void WithEmptySourceWithComparerReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
var maxima = strings.Maxima(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.Null);
}
}
Expand All @@ -138,15 +138,15 @@ public class Last
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.Maxima(s => s.Length);
Assert.That(MoreEnumerable.Last(maxima), Is.EqualTo("world"));
}

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

Expand All @@ -155,7 +155,7 @@ public void WithEmptySourceThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(() =>
MoreEnumerable.Last(strings.MaxBy(s => s.Length)),
MoreEnumerable.Last(strings.Maxima(s => s.Length)),
Throws.InvalidOperationException);
}

Expand All @@ -164,7 +164,7 @@ public void WithEmptySourceWithComparerThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(() =>
MoreEnumerable.Last(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)),
MoreEnumerable.Last(strings.Maxima(s => s.Length, Comparable<int>.DescendingOrderComparer)),
Throws.InvalidOperationException);
}
}
Expand All @@ -175,31 +175,31 @@ public class LastOrDefault
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.Maxima(s => s.Length);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.EqualTo("world"));
}

[Test]
public void WithComparerReturnsMaximumPerComparer()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
var maxima = strings.Maxima(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();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.Maxima(s => s.Length);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.Null);
}

[Test]
public void WithEmptySourceWithComparerReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
var maxima = strings.Maxima(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.Null);
}
}
Expand All @@ -213,7 +213,7 @@ public class Take
public string[] ReturnsMaxima(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.MaxBy(s => s.Length).Take(count).ToArray();
return strings.Maxima(s => s.Length).Take(count).ToArray();
}

[TestCase(0, 0, ExpectedResult = new string[0] )]
Expand All @@ -227,7 +227,7 @@ public string[] ReturnsMaxima(int count)
public string[] WithComparerReturnsMaximaPerComparer(int count, int index)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.MaxBy(s => s[index], Comparable<char>.DescendingOrderComparer)
return strings.Maxima(s => s[index], Comparable<char>.DescendingOrderComparer)
.Take(count)
.ToArray();
}
Expand All @@ -242,7 +242,7 @@ public class TakeLast
public string[] TakeLastReturnsMaxima(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.MaxBy(s => s.Length).TakeLast(count).ToArray();
return strings.Maxima(s => s.Length).TakeLast(count).ToArray();
}

[TestCase(0, 0, ExpectedResult = new string[0] )]
Expand All @@ -256,7 +256,7 @@ public string[] TakeLastReturnsMaxima(int count)
public string[] WithComparerReturnsMaximaPerComparer(int count, int index)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.MaxBy(s => s[index], Comparable<char>.DescendingOrderComparer)
return strings.Maxima(s => s[index], Comparable<char>.DescendingOrderComparer)
.TakeLast(count)
.ToArray();
}
Expand Down
Loading

0 comments on commit 66f7cab

Please sign in to comment.