Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for IndexOfAnyValues.GetValues #80038

Merged
merged 2 commits into from
Dec 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 70 additions & 36 deletions src/libraries/System.Memory/tests/Span/IndexOfAnyValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
Expand All @@ -13,6 +15,60 @@ namespace System.SpanTests
{
public static partial class SpanTests
{
private static readonly Func<IndexOfAnyValues<byte>, byte[]> s_getValuesByteMethod =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess you're intentionally getting it from IndexOfAnyValues<T> rather than IndexOfAnyValuesInRange<T>, as future refactorings might introduce new subclasses that also should work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, never mind I mis-grepped. But this made me notice you're missing "" to exercise IndexOfEmptyValues.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the case for empty values as well

typeof(IndexOfAnyValues<byte>).GetMethod("GetValues", BindingFlags.NonPublic | BindingFlags.Instance).CreateDelegate<Func<IndexOfAnyValues<byte>, byte[]>>();

private static readonly Func<IndexOfAnyValues<char>, char[]> s_getValuesCharMethod =
typeof(IndexOfAnyValues<char>).GetMethod("GetValues", BindingFlags.NonPublic | BindingFlags.Instance).CreateDelegate<Func<IndexOfAnyValues<char>, char[]>>();

public static IEnumerable<object[]> Values_MemberData()
{
string[] values = new[]
{
"",
"\0",
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
"a",
"ab",
"ac",
"abc",
"aei",
"abcd",
"aeio",
"aeiou",
"abceiou",
"123456789",
"123456789123",
"abcdefgh",
"abcdefghIJK",
"aa",
"aaa",
"aaaa",
"aaaaa",
"\uFFF0",
"\uFFF0\uFFF2",
"\uFFF0\uFFF2\uFFF4",
"\uFFF0\uFFF2\uFFF4\uFFF6",
"\uFFF0\uFFF2\uFFF4\uFFF6\uFFF8",
"\uFFF0\uFFF2\uFFF4\uFFF6\uFFF8\uFFFA",
"\u0000\u0001\u0002\u0003\u0004\u0005",
"\u0001\u0002\u0003\u0004\u0005\u0006",
"\u0001\u0002\u0003\u0004\u0005\u0007",
"\u007E\u007F\u0080\u0081\u0082\u0083",
"\u007E\u007F\u0080\u0081\u0082\u0084",
"\u007E\u007F\u0080\u0081\u0082",
"\u007E\u007F\u0080\u0081\u0083",
"\u00FE\u00FF\u0100\u0101\u0102\u0103",
"\u00FE\u00FF\u0100\u0101\u0102\u0104",
"\u00FE\u00FF\u0100\u0101\u0102",
"\u00FE\u00FF\u0100\u0101\u0103",
"\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFFA",
"\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFFB",
"\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFF9",
};

return values.Select(v => new object[] { v, Encoding.Latin1.GetBytes(v) });
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -124,44 +180,11 @@ public static void AsciiNeedle_ProperlyHandlesEdgeCases_Byte(bool needleContains
}

[Theory]
[InlineData("\0")]
[InlineData("a")]
[InlineData("ab")]
[InlineData("ac")]
[InlineData("abc")]
[InlineData("aei")]
[InlineData("abcd")]
[InlineData("aeio")]
[InlineData("aeiou")]
[InlineData("abceiou")]
[InlineData("123456789")]
[InlineData("123456789123")]
[InlineData("abcdefgh")]
[InlineData("abcdefghIJK")]
[InlineData("aa")]
[InlineData("aaa")]
[InlineData("aaaa")]
[InlineData("aaaaa")]
[InlineData("\u0000\u0001\u0002\u0003\u0004\u0005")]
[InlineData("\u0001\u0002\u0003\u0004\u0005\u0006")]
[InlineData("\u0001\u0002\u0003\u0004\u0005\u0007")]
[InlineData("\u007E\u007F\u0080\u0081\u0082\u0083")]
[InlineData("\u007E\u007F\u0080\u0081\u0082\u0084")]
[InlineData("\u007E\u007F\u0080\u0081\u0082")]
[InlineData("\u007E\u007F\u0080\u0081\u0083")]
[InlineData("\u00FE\u00FF\u0100\u0101\u0102\u0103")]
[InlineData("\u00FE\u00FF\u0100\u0101\u0102\u0104")]
[InlineData("\u00FE\u00FF\u0100\u0101\u0102")]
[InlineData("\u00FE\u00FF\u0100\u0101\u0103")]
[InlineData("\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFFA")]
[InlineData("\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFFB")]
[InlineData("\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFF9")]
public static void IndexOfAnyValues_Contains(string needle)
[MemberData(nameof(Values_MemberData))]
public static void IndexOfAnyValues_Contains(string needle, byte[] byteNeedle)
{
Test(needle, IndexOfAnyValues.Create(needle));

byte[] needleBytes = Encoding.Latin1.GetBytes(needle);
Test(needleBytes, IndexOfAnyValues.Create(needleBytes));
Test(byteNeedle, IndexOfAnyValues.Create(byteNeedle));

static void Test<T>(ReadOnlySpan<T> needle, IndexOfAnyValues<T> values) where T : struct, INumber<T>, IMinMaxValue<T>
{
Expand All @@ -173,6 +196,17 @@ static void Test<T>(ReadOnlySpan<T> needle, IndexOfAnyValues<T> values) where T
}
}

[Theory]
[MemberData(nameof(Values_MemberData))]
public static void IndexOfAnyValues_GetValues(string needle, byte[] byteNeedle)
{
char[] charValuesActual = s_getValuesCharMethod(IndexOfAnyValues.Create(needle));
byte[] byteValuesActual = s_getValuesByteMethod(IndexOfAnyValues.Create(byteNeedle));

Assert.Equal(new HashSet<char>(needle).Order().ToArray(), new HashSet<char>(charValuesActual).Order().ToArray());
Assert.Equal(new HashSet<byte>(byteNeedle).Order().ToArray(), new HashSet<byte>(byteValuesActual).Order().ToArray());
}

[Fact]
[OuterLoop("Takes about a second to execute")]
public static void TestIndexOfAny_RandomInputs_Byte()
Expand Down