Skip to content

Commit

Permalink
refactor string tests.
Browse files Browse the repository at this point in the history
Commit migrated from dotnet/corefx@4327a5f
  • Loading branch information
nietras committed Dec 7, 2017
1 parent f8ac9ec commit b9c40eb
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions src/libraries/System.Memory/tests/ReadOnlySpan/BinarySearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,74 @@ public static partial class ReadOnlySpanTests
public static TheoryData<(double[] Array, double Value, int ExpectedIndex)> DoubleCases =>
new TheoryData<(double[] Array, double Value, int ExpectedIndex)> {
(new double[] { }, 0u, -1),
(new double[] { 1u, 2u, 4u, 5u }, 0u, -1),
(new double[] { 1u, 2u, 4u, 5u }, 1u, 0),
(new double[] { 1u, 2u, 4u, 5u }, 2u, 1),
(new double[] { 1u, 2u, 4u, 5u }, 3u, -3),
(new double[] { 1u, 2u, 4u, 5u }, 4u, 2),
(new double[] { 1u, 2u, 4u, 5u }, 5u, 3),
(new double[] { 1u, 2u, 4u, 5u }, 6u, -5),
(new double[] { 1.0, 2.0, 4.0, 5u }, 0.0, -1),
(new double[] { 1.0, 2.0, 4.0, 5u }, 1.0, 0),
(new double[] { 1.0, 2.0, 4.0, 5u }, 2.0, 1),
(new double[] { 1.0, 2.0, 4.0, 5u }, 3.0, -3),
(new double[] { 1.0, 2.0, 4.0, 5u }, 4.0, 2),
(new double[] { 1.0, 2.0, 4.0, 5u }, 5.0, 3),
(new double[] { 1.0, 2.0, 4.0, 5u }, 6.0, -5),
};
public static TheoryData<(string[] Array, string Value, int ExpectedIndex)> StringCases =>
new TheoryData<(string[] Array, string Value, int ExpectedIndex)> {
(new string[] { "b", "c", "e", "f" }, "a", -1),
(new string[] { "b", "c", "e", "f" }, "b", 0),
(new string[] { "b", "c", "e", "f" }, "c", 1),
(new string[] { "b", "c", "e", "f" }, "d", -3),
(new string[] { "b", "c", "e", "f" }, "e", 2),
(new string[] { "b", "c", "e", "f" }, "f", 3),
(new string[] { "b", "c", "e", "f" }, "g", -5),
};

[Theory, MemberData(nameof(UIntCases))]
public static void BinarySearch_UInt_Span(
(uint[] Array, uint Value, int ExpectedIndex) c)
{
var index = new Span<uint>(c.Array).BinarySearch(c.Value);
var span = new Span<uint>(c.Array);
var index = span.BinarySearch(c.Value);
Assert.Equal(c.ExpectedIndex, index);
}
[Theory, MemberData(nameof(UIntCases))]
public static void BinarySearch_UInt_ReadOnlySpan(
(uint[] Array, uint Value, int ExpectedIndex) c)
{
var index = new ReadOnlySpan<uint>(c.Array).BinarySearch(c.Value);
var span = new ReadOnlySpan<uint>(c.Array);
var index = span.BinarySearch(c.Value);
Assert.Equal(c.ExpectedIndex, index);
}

[Theory, MemberData(nameof(DoubleCases))]
public static void BinarySearch_Double_Span(
(double[] Array, double Value, int ExpectedIndex) c)
{
var index = new Span<double>(c.Array).BinarySearch(c.Value);
var span = new Span<double>(c.Array);
var index = span.BinarySearch(c.Value);
Assert.Equal(c.ExpectedIndex, index);
}
[Theory, MemberData(nameof(DoubleCases))]
public static void BinarySearch_Double_ReadOnlySpan(
(double[] Array, double Value, int ExpectedIndex) c)
{
var index = new ReadOnlySpan<double>(c.Array).BinarySearch(c.Value);
var span = new ReadOnlySpan<double>(c.Array);
var index = span.BinarySearch(c.Value);
Assert.Equal(c.ExpectedIndex, index);
}

[Theory]
[InlineData(new string[] { }, null, -1)]
[InlineData(new string[] { "b", "c", "e", "f" }, "a", -1)]
[InlineData(new string[] { "b", "c", "e", "f" }, "b", 0)]
[InlineData(new string[] { "b", "c", "e", "f" }, "c", 1)]
[InlineData(new string[] { "b", "c", "e", "f" }, "d", -3)]
[InlineData(new string[] { "b", "c", "e", "f" }, "e", 2)]
[InlineData(new string[] { "b", "c", "e", "f" }, "f", 3)]
[InlineData(new string[] { "b", "c", "e", "f" }, "g", -5)]
public static void BinarySearch_String(string[] a, string value, int expectedIndex)
[Theory, MemberData(nameof(StringCases))]
public static void BinarySearch_String_Span(
(string[] Array, string Value, int ExpectedIndex) c)
{
// Implicitly tests ReadOnlySpan
var span = new Span<string>(a);

var index = span.BinarySearch(value);

Assert.Equal(expectedIndex, index);
var span = new Span<string>(c.Array);
var index = span.BinarySearch(c.Value);
Assert.Equal(c.ExpectedIndex, index);
}
[Theory, MemberData(nameof(StringCases))]
public static void BinarySearch_String_ReadOnlySpan(
(string[] Array, string Value, int ExpectedIndex) c)
{
var span = new ReadOnlySpan<string>(c.Array);
var index = span.BinarySearch(c.Value);
Assert.Equal(c.ExpectedIndex, index);
}
}
}

0 comments on commit b9c40eb

Please sign in to comment.