Skip to content

Commit

Permalink
Add support for partial Min/MaxBy
Browse files Browse the repository at this point in the history
Merge of PR morelinq#513 that closes morelinq#380
  • Loading branch information
atifaziz authored Jun 22, 2018
1 parent 40a5849 commit fb08d80
Show file tree
Hide file tree
Showing 4 changed files with 362 additions and 11 deletions.
52 changes: 52 additions & 0 deletions MoreLinq.Test/MaxByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,57 @@ public void MaxByWithComparer()
{
Assert.AreEqual(new[] { "aa" }, SampleData.Strings.MaxBy(x => x[1], SampleData.ReverseCharComparer));
}

[TestCase(0, ExpectedResult = new string[0] )]
[TestCase(1, ExpectedResult = new[] { "hello" })]
[TestCase(2, ExpectedResult = new[] { "hello", "world" })]
[TestCase(3, ExpectedResult = new[] { "hello", "world" })]
public string[] MaxByTakeReturnsMaxima(int count)
{
using (var strings = SampleData.Strings.AsTestingSequence())
return strings.MaxBy(s => s.Length).Take(count).ToArray();
}

[TestCase(0, ExpectedResult = new string[0] )]
[TestCase(1, ExpectedResult = new[] { "world" })]
[TestCase(2, ExpectedResult = new[] { "hello", "world" })]
[TestCase(3, ExpectedResult = new[] { "hello", "world" })]
public string[] MaxByTakeLastReturnsMaxima(int count)
{
using (var strings = SampleData.Strings.AsTestingSequence())
return strings.MaxBy(s => s.Length).TakeLast(count).ToArray();
}

[TestCase(0, 0, ExpectedResult = new string[0] )]
[TestCase(3, 1, ExpectedResult = new[] { "aa" })]
[TestCase(1, 0, ExpectedResult = new[] { "ax" })]
[TestCase(2, 0, ExpectedResult = new[] { "ax", "aa" })]
[TestCase(3, 0, ExpectedResult = new[] { "ax", "aa", "ab" })]
[TestCase(4, 0, ExpectedResult = new[] { "ax", "aa", "ab", "ay" })]
[TestCase(5, 0, ExpectedResult = new[] { "ax", "aa", "ab", "ay", "az" })]
[TestCase(6, 0, ExpectedResult = new[] { "ax", "aa", "ab", "ay", "az" })]
public string[] MaxByTakeWithComparerReturnsMaxima(int count, int index)
{
using (var strings = SampleData.Strings.AsTestingSequence())
return strings.MaxBy(s => s[index], SampleData.ReverseCharComparer)
.Take(count)
.ToArray();
}

[TestCase(0, 0, ExpectedResult = new string[0] )]
[TestCase(3, 1, ExpectedResult = new[] { "aa" })]
[TestCase(1, 0, ExpectedResult = new[] { "az" })]
[TestCase(2, 0, ExpectedResult = new[] { "ay", "az" })]
[TestCase(3, 0, ExpectedResult = new[] { "ab", "ay", "az" })]
[TestCase(4, 0, ExpectedResult = new[] { "aa", "ab", "ay", "az" })]
[TestCase(5, 0, ExpectedResult = new[] { "ax", "aa", "ab", "ay", "az" })]
[TestCase(6, 0, ExpectedResult = new[] { "ax", "aa", "ab", "ay", "az" })]
public string[] MaxByTakeLastWithComparerReturnsMaxima(int count, int index)
{
using (var strings = SampleData.Strings.AsTestingSequence())
return strings.MaxBy(s => s[index], SampleData.ReverseCharComparer)
.TakeLast(count)
.ToArray();
}
}
}
52 changes: 52 additions & 0 deletions MoreLinq.Test/MinByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace MoreLinq.Test
{
using System;
using System.Collections.Generic;
using NUnit.Framework;

[TestFixture]
Expand Down Expand Up @@ -59,5 +61,55 @@ public void MinByWithComparer()
{
Assert.AreEqual(new[] { "az" }, SampleData.Strings.MinBy(x => x[1], SampleData.ReverseCharComparer));
}

[TestCase(0, ExpectedResult = new string[0] )]
[TestCase(1, ExpectedResult = new[] { "ax" })]
[TestCase(2, ExpectedResult = new[] { "ax", "aa" })]
[TestCase(3, ExpectedResult = new[] { "ax", "aa", "ab" })]
[TestCase(4, ExpectedResult = new[] { "ax", "aa", "ab", "ay" })]
[TestCase(5, ExpectedResult = new[] { "ax", "aa", "ab", "ay", "az" })]
[TestCase(6, ExpectedResult = new[] { "ax", "aa", "ab", "ay", "az" })]
public string[] MinByTakeReturnsMinima(int count)
{
using (var strings = SampleData.Strings.AsTestingSequence())
return strings.MinBy(s => s.Length).Take(count).ToArray();
}

[TestCase(0, ExpectedResult = new string[0] )]
[TestCase(1, ExpectedResult = new[] { "az" })]
[TestCase(2, ExpectedResult = new[] { "ay", "az" })]
[TestCase(3, ExpectedResult = new[] { "ab", "ay", "az" })]
[TestCase(4, ExpectedResult = new[] { "aa", "ab", "ay", "az" })]
[TestCase(5, ExpectedResult = new[] { "ax", "aa", "ab", "ay", "az" })]
[TestCase(6, ExpectedResult = new[] { "ax", "aa", "ab", "ay", "az" })]
public string[] MinByTakeLastReturnsMinima(int count)
{
using (var strings = SampleData.Strings.AsTestingSequence())
return strings.MinBy(s => s.Length).TakeLast(count).ToArray();
}

[TestCase(0, ExpectedResult = new string[0] )]
[TestCase(1, ExpectedResult = new[] { "hello", })]
[TestCase(2, ExpectedResult = new[] { "hello", "world" })]
[TestCase(3, ExpectedResult = new[] { "hello", "world" })]
public string[] MinByTakeWithComparerReturnsMinima(int count)
{
using (var strings = SampleData.Strings.AsTestingSequence())
return strings.MinBy(s => s.Length, Comparer<int>.Create((x, y) => -Math.Sign(x.CompareTo(y))))
.Take(count)
.ToArray();
}

[TestCase(0, ExpectedResult = new string[0] )]
[TestCase(1, ExpectedResult = new[] { "world", })]
[TestCase(2, ExpectedResult = new[] { "hello", "world" })]
[TestCase(3, ExpectedResult = new[] { "hello", "world" })]
public string[] MinByTakeLastWithComparerReturnsMinima(int count)
{
using (var strings = SampleData.Strings.AsTestingSequence())
return strings.MinBy(s => s.Length, Comparer<int>.Create((x, y) => -Math.Sign(x.CompareTo(y))))
.TakeLast(count)
.ToArray();
}
}
}
Loading

0 comments on commit fb08d80

Please sign in to comment.