Skip to content

Commit 5e45fb1

Browse files
authored
Add a test for IndexOfAnyValues.GetValues (#80038)
* Add a test for IndexOfAnyValues.GetValues * Add test case for IndexOfEmptyValues
1 parent 96b849e commit 5e45fb1

File tree

1 file changed

+70
-36
lines changed

1 file changed

+70
-36
lines changed

src/libraries/System.Memory/tests/Span/IndexOfAnyValues.cs

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Buffers;
5+
using System.Collections.Generic;
56
using System.Linq;
67
using System.Numerics;
8+
using System.Reflection;
79
using System.Runtime.CompilerServices;
810
using System.Runtime.InteropServices;
911
using System.Text;
@@ -13,6 +15,60 @@ namespace System.SpanTests
1315
{
1416
public static partial class SpanTests
1517
{
18+
private static readonly Func<IndexOfAnyValues<byte>, byte[]> s_getValuesByteMethod =
19+
typeof(IndexOfAnyValues<byte>).GetMethod("GetValues", BindingFlags.NonPublic | BindingFlags.Instance).CreateDelegate<Func<IndexOfAnyValues<byte>, byte[]>>();
20+
21+
private static readonly Func<IndexOfAnyValues<char>, char[]> s_getValuesCharMethod =
22+
typeof(IndexOfAnyValues<char>).GetMethod("GetValues", BindingFlags.NonPublic | BindingFlags.Instance).CreateDelegate<Func<IndexOfAnyValues<char>, char[]>>();
23+
24+
public static IEnumerable<object[]> Values_MemberData()
25+
{
26+
string[] values = new[]
27+
{
28+
"",
29+
"\0",
30+
"a",
31+
"ab",
32+
"ac",
33+
"abc",
34+
"aei",
35+
"abcd",
36+
"aeio",
37+
"aeiou",
38+
"abceiou",
39+
"123456789",
40+
"123456789123",
41+
"abcdefgh",
42+
"abcdefghIJK",
43+
"aa",
44+
"aaa",
45+
"aaaa",
46+
"aaaaa",
47+
"\uFFF0",
48+
"\uFFF0\uFFF2",
49+
"\uFFF0\uFFF2\uFFF4",
50+
"\uFFF0\uFFF2\uFFF4\uFFF6",
51+
"\uFFF0\uFFF2\uFFF4\uFFF6\uFFF8",
52+
"\uFFF0\uFFF2\uFFF4\uFFF6\uFFF8\uFFFA",
53+
"\u0000\u0001\u0002\u0003\u0004\u0005",
54+
"\u0001\u0002\u0003\u0004\u0005\u0006",
55+
"\u0001\u0002\u0003\u0004\u0005\u0007",
56+
"\u007E\u007F\u0080\u0081\u0082\u0083",
57+
"\u007E\u007F\u0080\u0081\u0082\u0084",
58+
"\u007E\u007F\u0080\u0081\u0082",
59+
"\u007E\u007F\u0080\u0081\u0083",
60+
"\u00FE\u00FF\u0100\u0101\u0102\u0103",
61+
"\u00FE\u00FF\u0100\u0101\u0102\u0104",
62+
"\u00FE\u00FF\u0100\u0101\u0102",
63+
"\u00FE\u00FF\u0100\u0101\u0103",
64+
"\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFFA",
65+
"\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFFB",
66+
"\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFF9",
67+
};
68+
69+
return values.Select(v => new object[] { v, Encoding.Latin1.GetBytes(v) });
70+
}
71+
1672
[Theory]
1773
[InlineData(true)]
1874
[InlineData(false)]
@@ -124,44 +180,11 @@ public static void AsciiNeedle_ProperlyHandlesEdgeCases_Byte(bool needleContains
124180
}
125181

126182
[Theory]
127-
[InlineData("\0")]
128-
[InlineData("a")]
129-
[InlineData("ab")]
130-
[InlineData("ac")]
131-
[InlineData("abc")]
132-
[InlineData("aei")]
133-
[InlineData("abcd")]
134-
[InlineData("aeio")]
135-
[InlineData("aeiou")]
136-
[InlineData("abceiou")]
137-
[InlineData("123456789")]
138-
[InlineData("123456789123")]
139-
[InlineData("abcdefgh")]
140-
[InlineData("abcdefghIJK")]
141-
[InlineData("aa")]
142-
[InlineData("aaa")]
143-
[InlineData("aaaa")]
144-
[InlineData("aaaaa")]
145-
[InlineData("\u0000\u0001\u0002\u0003\u0004\u0005")]
146-
[InlineData("\u0001\u0002\u0003\u0004\u0005\u0006")]
147-
[InlineData("\u0001\u0002\u0003\u0004\u0005\u0007")]
148-
[InlineData("\u007E\u007F\u0080\u0081\u0082\u0083")]
149-
[InlineData("\u007E\u007F\u0080\u0081\u0082\u0084")]
150-
[InlineData("\u007E\u007F\u0080\u0081\u0082")]
151-
[InlineData("\u007E\u007F\u0080\u0081\u0083")]
152-
[InlineData("\u00FE\u00FF\u0100\u0101\u0102\u0103")]
153-
[InlineData("\u00FE\u00FF\u0100\u0101\u0102\u0104")]
154-
[InlineData("\u00FE\u00FF\u0100\u0101\u0102")]
155-
[InlineData("\u00FE\u00FF\u0100\u0101\u0103")]
156-
[InlineData("\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFFA")]
157-
[InlineData("\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFFB")]
158-
[InlineData("\uFFFF\uFFFE\uFFFD\uFFFC\uFFFB\uFFF9")]
159-
public static void IndexOfAnyValues_Contains(string needle)
183+
[MemberData(nameof(Values_MemberData))]
184+
public static void IndexOfAnyValues_Contains(string needle, byte[] byteNeedle)
160185
{
161186
Test(needle, IndexOfAnyValues.Create(needle));
162-
163-
byte[] needleBytes = Encoding.Latin1.GetBytes(needle);
164-
Test(needleBytes, IndexOfAnyValues.Create(needleBytes));
187+
Test(byteNeedle, IndexOfAnyValues.Create(byteNeedle));
165188

166189
static void Test<T>(ReadOnlySpan<T> needle, IndexOfAnyValues<T> values) where T : struct, INumber<T>, IMinMaxValue<T>
167190
{
@@ -173,6 +196,17 @@ static void Test<T>(ReadOnlySpan<T> needle, IndexOfAnyValues<T> values) where T
173196
}
174197
}
175198

199+
[Theory]
200+
[MemberData(nameof(Values_MemberData))]
201+
public static void IndexOfAnyValues_GetValues(string needle, byte[] byteNeedle)
202+
{
203+
char[] charValuesActual = s_getValuesCharMethod(IndexOfAnyValues.Create(needle));
204+
byte[] byteValuesActual = s_getValuesByteMethod(IndexOfAnyValues.Create(byteNeedle));
205+
206+
Assert.Equal(new HashSet<char>(needle).Order().ToArray(), new HashSet<char>(charValuesActual).Order().ToArray());
207+
Assert.Equal(new HashSet<byte>(byteNeedle).Order().ToArray(), new HashSet<byte>(byteValuesActual).Order().ToArray());
208+
}
209+
176210
[Fact]
177211
[OuterLoop("Takes about a second to execute")]
178212
public static void TestIndexOfAny_RandomInputs_Byte()

0 commit comments

Comments
 (0)