Skip to content

Commit 25b2471

Browse files
authored
Tests for nullable reference types in Span apis (dotnet/corefx#38468)
Commit migrated from dotnet/corefx@159fbc0
1 parent 5cc27cf commit 25b2471

9 files changed

Lines changed: 116 additions & 0 deletions

File tree

src/libraries/System.Memory/tests/ReadOnlySpan/IndexOf.T.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,13 @@ public static void TestMultipleMatchIndexOf_String()
185185
Assert.Equal(length - 2, idx);
186186
}
187187
}
188+
189+
[Theory]
190+
[MemberData(nameof(TestHelpers.IndexOfNullData), MemberType = typeof(TestHelpers))]
191+
public static void IndexOfNull_String(string[] spanInput, int expected)
192+
{
193+
ReadOnlySpan<string> theStrings = spanInput;
194+
Assert.Equal(expected, theStrings.IndexOf((string)null));
195+
}
188196
}
189197
}

src/libraries/System.Memory/tests/ReadOnlySpan/IndexOfSequence.T.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,13 @@ public static void IndexOfSequenceLengthOneValueJustPasttVeryEnd_String()
231231
int index = span.IndexOf(value);
232232
Assert.Equal(-1, index);
233233
}
234+
235+
[Theory]
236+
[MemberData(nameof(TestHelpers.IndexOfNullSequenceData), MemberType = typeof(TestHelpers))]
237+
public static void IndexOfNullSequence_String(string[] spanInput, string[] searchInput, int expected)
238+
{
239+
ReadOnlySpan<string> theStrings = spanInput;
240+
Assert.Equal(expected, theStrings.IndexOf(searchInput));
241+
}
234242
}
235243
}

src/libraries/System.Memory/tests/ReadOnlySpan/LastIndexOf.T.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,13 @@ public static void TestMultipleMatchLastIndexOf_String()
185185
Assert.Equal(length - 1, idx);
186186
}
187187
}
188+
189+
[Theory]
190+
[MemberData(nameof(TestHelpers.LastIndexOfNullData), MemberType = typeof(TestHelpers))]
191+
public static void LastIndexOfNull_String(string[] spanInput, int expected)
192+
{
193+
ReadOnlySpan<string> theStrings = spanInput;
194+
Assert.Equal(expected, theStrings.LastIndexOf((string)null));
195+
}
188196
}
189197
}

src/libraries/System.Memory/tests/ReadOnlySpan/LastIndexOfSequence.T.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,13 @@ public static void LastIndexOfSequenceLengthOneValueJustPasttVeryEnd_String()
241241
int index = span.LastIndexOf(value);
242242
Assert.Equal(-1, index);
243243
}
244+
245+
[Theory]
246+
[MemberData(nameof(TestHelpers.LastIndexOfNullSequenceData), MemberType = typeof(TestHelpers))]
247+
public static void LastIndexOfNullSequence_String(string[] spanInput, string[] searchInput, int expected)
248+
{
249+
ReadOnlySpan<string> theStrings = spanInput;
250+
Assert.Equal(expected, theStrings.LastIndexOf(searchInput));
251+
}
244252
}
245253
}

src/libraries/System.Memory/tests/Span/IndexOf.T.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,13 @@ public static void TestMultipleMatchIndexOf_String()
185185
Assert.Equal(length - 2, idx);
186186
}
187187
}
188+
189+
[Theory]
190+
[MemberData(nameof(TestHelpers.IndexOfNullData), MemberType = typeof(TestHelpers))]
191+
public static void IndexOfNull_String(string[] spanInput, int expected)
192+
{
193+
Span<string> theStrings = spanInput;
194+
Assert.Equal(expected, theStrings.IndexOf((string)null));
195+
}
188196
}
189197
}

src/libraries/System.Memory/tests/Span/IndexOfSequence.T.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,13 @@ public static void IndexOfSequenceLengthOneValueJustPasttVeryEnd_String()
231231
int index = span.IndexOf(value);
232232
Assert.Equal(-1, index);
233233
}
234+
235+
[Theory]
236+
[MemberData(nameof(TestHelpers.IndexOfNullSequenceData), MemberType = typeof(TestHelpers))]
237+
public static void IndexOfNullSequence_String(string[] spanInput, string[] searchInput, int expected)
238+
{
239+
Span<string> theStrings = spanInput;
240+
Assert.Equal(expected, theStrings.IndexOf(searchInput));
241+
}
234242
}
235243
}

src/libraries/System.Memory/tests/Span/LastIndexOf.T.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,13 @@ public static void TestMultipleMatchLastIndexOf_String()
185185
Assert.Equal(length - 1, idx);
186186
}
187187
}
188+
189+
[Theory]
190+
[MemberData(nameof(TestHelpers.LastIndexOfNullData), MemberType = typeof(TestHelpers))]
191+
public static void LastIndexOfNull_String(string[] spanInput, int expected)
192+
{
193+
Span<string> theStrings = spanInput;
194+
Assert.Equal(expected, theStrings.LastIndexOf((string)null));
195+
}
188196
}
189197
}

src/libraries/System.Memory/tests/Span/LastIndexOfSequence.T.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,13 @@ public static void LastIndexOfSequenceLengthOneValueJustPasttVeryEnd_String()
241241
int index = span.LastIndexOf(value);
242242
Assert.Equal(-1, index);
243243
}
244+
245+
[Theory]
246+
[MemberData(nameof(TestHelpers.LastIndexOfNullSequenceData), MemberType = typeof(TestHelpers))]
247+
public static void LastIndexOfNullSequence_String(string[] spanInput, string[] searchInput, int expected)
248+
{
249+
Span<string> theStrings = spanInput;
250+
Assert.Equal(expected, theStrings.LastIndexOf(searchInput));
251+
}
244252
}
245253
}

src/libraries/System.Memory/tests/TestHelpers.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,5 +410,57 @@ public static Memory<T> DangerousCreateMemory<T>(object obj, int offset, int len
410410
/// <summary>Creates a <see cref="ReadOnlyMemory{T}"/> with the specified values in its backing field.</summary>
411411
public static ReadOnlyMemory<T> DangerousCreateReadOnlyMemory<T>(object obj, int offset, int length) =>
412412
DangerousCreateMemory<T>(obj, offset, length);
413+
414+
public static TheoryData<string[], int> IndexOfNullData => new TheoryData<string[], int>()
415+
{
416+
{ new string[] { "1", null, "2" }, 1},
417+
{ new string[] { "1", "3", "2" }, -1},
418+
{ null, -1},
419+
{ new string[] { "1", null, null }, 1},
420+
{ new string[] { null, null, null }, 0},
421+
};
422+
423+
public static TheoryData<string[], string[], int> IndexOfNullSequenceData => new TheoryData<string[], string[], int>()
424+
{
425+
{ new string[] { "1", null, "2" }, new string[] { "1", null, "2" }, 0},
426+
{ new string[] { "1", null, "2" }, new string[] { null }, 1},
427+
{ new string[] { "1", null, "2" }, (string[])null, 0},
428+
429+
{ new string[] { "1", "3", "2" }, new string[] { "1", null, "2" }, -1},
430+
{ new string[] { "1", "3", "2" }, new string[] { null }, -1},
431+
{ new string[] { "1", "3", "2" }, (string[])null, 0},
432+
433+
{ null, new string[] { "1", null, "2" }, -1},
434+
435+
{ new string[] { "1", null, null }, new string[] { null, null, "2" }, -1},
436+
{ new string[] { null, null, null }, new string[] { null, null }, 0},
437+
};
438+
439+
public static TheoryData<string[], int> LastIndexOfNullData => new TheoryData<string[], int>()
440+
{
441+
{ new string[] { "1", null, "2" }, 1},
442+
{ new string[] { "1", "3", "2" }, -1},
443+
{ null, -1},
444+
{ new string[] { "1", null, null }, 2},
445+
{ new string[] { null, null, null }, 2},
446+
{ new string[] { null, null, "3" }, 1},
447+
};
448+
449+
public static TheoryData<string[], string[], int> LastIndexOfNullSequenceData => new TheoryData<string[], string[], int>()
450+
{
451+
{ new string[] { "1", null, "2" }, new string[] { "1", null, "2" }, 0},
452+
{ new string[] { "1", null, "2" }, new string[] { null }, 1},
453+
{ new string[] { "1", null, "2" }, (string[])null, 0},
454+
455+
{ new string[] { "1", "3", "1" }, new string[] { "1", null, "2" }, -1},
456+
{ new string[] { "1", "3", "1" }, new string[] { "1" }, 2},
457+
{ new string[] { "1", "3", "1" }, new string[] { null }, -1},
458+
{ new string[] { "1", "3", "1" }, (string[])null, 0},
459+
460+
{ null, new string[] { "1", null, "2" }, -1},
461+
462+
{ new string[] { "1", null, null }, new string[] { null, null, "2" }, -1},
463+
{ new string[] { null, null, null }, new string[] { null, null }, 1},
464+
};
413465
}
414466
}

0 commit comments

Comments
 (0)