Skip to content

Commit

Permalink
Add missing Min/MaxBy tests
Browse files Browse the repository at this point in the history
- MinBy followed by First
- MinBy followed by FirstOrDefault
- MinBy followed by Last
- MinBy followed by LastOrDefault
- MaxBy followed by First
- MaxBy followed by FirstOrDefault
- MaxBy followed by Last
- MaxBy followed by LastOrDefault
  • Loading branch information
atifaziz committed Aug 14, 2020
1 parent 8b8cac2 commit 86f6f58
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 69 deletions.
28 changes: 28 additions & 0 deletions MoreLinq.Test/Comparable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2020 Atif Aziz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

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

static class Comparable<T> where T : IComparable<T>
{
public static readonly IComparer<T> DescendingOrderComparer =
Comparer<T>.Create((x, y) => -Math.Sign(x.CompareTo(y)));
}
}
215 changes: 181 additions & 34 deletions MoreLinq.Test/MaxByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

[TestFixture]
Expand Down Expand Up @@ -60,56 +61,202 @@ 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)
public class First
{
using (var strings = SampleData.Strings.AsTestingSequence())
return strings.MaxBy(s => s.Length).Take(count).ToArray();
[Test]
public void ReturnsMaxima()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).First(), Is.EqualTo("hello"));
}

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

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

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

[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)
public class FirstOrDefault
{
using (var strings = SampleData.Strings.AsTestingSequence())
return strings.MaxBy(s => s.Length).TakeLast(count).ToArray();
[Test]
public void ReturnsMaxima()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).FirstOrDefault(), Is.EqualTo("hello"));
}

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

[Test]
public void WithEmptySourceReturnDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).FirstOrDefault(), 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);
}
}

public class Last
{
[Test]
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).Last(), 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"));
}

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

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

public class LastOrDefault
{
[Test]
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).LastOrDefault(), 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"));
}

[Test]
public void WithEmptySourceReturnDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.That(strings.MaxBy(s => s.Length).LastOrDefault(), 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);
}
}

[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)
public class Take
{
using (var strings = SampleData.Strings.AsTestingSequence())
[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[] ReturnsMaxima(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.MaxBy(s => s.Length).Take(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[] WithComparerReturnsMaximaPerComparer(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)
public class TakeLast
{
using (var strings = SampleData.Strings.AsTestingSequence())
[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[] TakeLastReturnsMaxima(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[] { "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[] WithComparerReturnsMaximaPerComparer(int count, int index)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.MaxBy(s => s[index], SampleData.ReverseCharComparer)
.TakeLast(count)
.ToArray();
}
}
}
}
Loading

0 comments on commit 86f6f58

Please sign in to comment.