Tests for nullable reference types in Span apis (dotnet/corefx#38468)
authorAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Thu, 13 Jun 2019 22:14:00 +0000 (15:14 -0700)
committerGitHub <noreply@github.com>
Thu, 13 Jun 2019 22:14:00 +0000 (15:14 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/159fbc07c938f5ee6f136c60559ca16dc295d859

src/libraries/System.Memory/tests/ReadOnlySpan/IndexOf.T.cs
src/libraries/System.Memory/tests/ReadOnlySpan/IndexOfSequence.T.cs
src/libraries/System.Memory/tests/ReadOnlySpan/LastIndexOf.T.cs
src/libraries/System.Memory/tests/ReadOnlySpan/LastIndexOfSequence.T.cs
src/libraries/System.Memory/tests/Span/IndexOf.T.cs
src/libraries/System.Memory/tests/Span/IndexOfSequence.T.cs
src/libraries/System.Memory/tests/Span/LastIndexOf.T.cs
src/libraries/System.Memory/tests/Span/LastIndexOfSequence.T.cs
src/libraries/System.Memory/tests/TestHelpers.cs

index 2c80369..db9eaad 100644 (file)
@@ -185,5 +185,13 @@ namespace System.SpanTests
                 Assert.Equal(length - 2, idx);
             }
         }
+
+        [Theory]
+        [MemberData(nameof(TestHelpers.IndexOfNullData), MemberType = typeof(TestHelpers))]
+        public static void IndexOfNull_String(string[] spanInput, int expected)
+        {
+            ReadOnlySpan<string> theStrings = spanInput;
+            Assert.Equal(expected, theStrings.IndexOf((string)null));
+        }
     }
 }
index 401e7c6..796a0f6 100644 (file)
@@ -231,5 +231,13 @@ namespace System.SpanTests
             int index = span.IndexOf(value);
             Assert.Equal(-1, index);
         }
+
+        [Theory]
+        [MemberData(nameof(TestHelpers.IndexOfNullSequenceData), MemberType = typeof(TestHelpers))]
+        public static void IndexOfNullSequence_String(string[] spanInput, string[] searchInput, int expected)
+        {
+            ReadOnlySpan<string> theStrings = spanInput;
+            Assert.Equal(expected, theStrings.IndexOf(searchInput));
+        }
     }
 }
index 9738142..f1913ba 100644 (file)
@@ -185,5 +185,13 @@ namespace System.SpanTests
                 Assert.Equal(length - 1, idx);
             }
         }
+
+        [Theory]
+        [MemberData(nameof(TestHelpers.LastIndexOfNullData), MemberType = typeof(TestHelpers))]
+        public static void LastIndexOfNull_String(string[] spanInput, int expected)
+        {
+            ReadOnlySpan<string> theStrings = spanInput;
+            Assert.Equal(expected, theStrings.LastIndexOf((string)null));
+        }
     }
 }
index 2dde13b..4885b0b 100644 (file)
@@ -241,5 +241,13 @@ namespace System.SpanTests
             int index = span.LastIndexOf(value);
             Assert.Equal(-1, index);
         }
+
+        [Theory]
+        [MemberData(nameof(TestHelpers.LastIndexOfNullSequenceData), MemberType = typeof(TestHelpers))]
+        public static void LastIndexOfNullSequence_String(string[] spanInput, string[] searchInput, int expected)
+        {
+            ReadOnlySpan<string> theStrings = spanInput;
+            Assert.Equal(expected, theStrings.LastIndexOf(searchInput));
+        }
     }
 }
index 0da12ed..e58cb71 100644 (file)
@@ -185,5 +185,13 @@ namespace System.SpanTests
                 Assert.Equal(length - 2, idx);
             }
         }
+
+        [Theory]
+        [MemberData(nameof(TestHelpers.IndexOfNullData), MemberType = typeof(TestHelpers))]
+        public static void IndexOfNull_String(string[] spanInput, int expected)
+        {
+            Span<string> theStrings = spanInput;
+            Assert.Equal(expected, theStrings.IndexOf((string)null));
+        }
     }
 }
index e8c270c..697d5ce 100644 (file)
@@ -231,5 +231,13 @@ namespace System.SpanTests
             int index = span.IndexOf(value);
             Assert.Equal(-1, index);
         }
+
+        [Theory]
+        [MemberData(nameof(TestHelpers.IndexOfNullSequenceData), MemberType = typeof(TestHelpers))]
+        public static void IndexOfNullSequence_String(string[] spanInput, string[] searchInput, int expected)
+        {
+            Span<string> theStrings = spanInput;
+            Assert.Equal(expected, theStrings.IndexOf(searchInput));
+        }
     }
 }
index e15858e..404be57 100644 (file)
@@ -185,5 +185,13 @@ namespace System.SpanTests
                 Assert.Equal(length - 1, idx);
             }
         }
+
+        [Theory]
+        [MemberData(nameof(TestHelpers.LastIndexOfNullData), MemberType = typeof(TestHelpers))]
+        public static void LastIndexOfNull_String(string[] spanInput, int expected)
+        {
+            Span<string> theStrings = spanInput;
+            Assert.Equal(expected, theStrings.LastIndexOf((string)null));
+        }
     }
 }
index 833d80c..90134c4 100644 (file)
@@ -241,5 +241,13 @@ namespace System.SpanTests
             int index = span.LastIndexOf(value);
             Assert.Equal(-1, index);
         }
+
+        [Theory]
+        [MemberData(nameof(TestHelpers.LastIndexOfNullSequenceData), MemberType = typeof(TestHelpers))]
+        public static void LastIndexOfNullSequence_String(string[] spanInput, string[] searchInput, int expected)
+        {
+            Span<string> theStrings = spanInput;
+            Assert.Equal(expected, theStrings.LastIndexOf(searchInput));
+        }
     }
 }
index 1d8abb5..f68a235 100644 (file)
@@ -410,5 +410,57 @@ namespace System
         /// <summary>Creates a <see cref="ReadOnlyMemory{T}"/> with the specified values in its backing field.</summary>
         public static ReadOnlyMemory<T> DangerousCreateReadOnlyMemory<T>(object obj, int offset, int length) =>
             DangerousCreateMemory<T>(obj, offset, length);
+
+        public static TheoryData<string[], int> IndexOfNullData => new TheoryData<string[], int>()
+        {
+            { new string[] { "1", null, "2" }, 1},
+            { new string[] { "1", "3", "2" }, -1},
+            { null, -1},
+            { new string[] { "1", null, null }, 1},
+            { new string[] { null, null, null }, 0},
+        };
+
+        public static TheoryData<string[], string[], int> IndexOfNullSequenceData => new TheoryData<string[], string[], int>()
+        {
+            { new string[] { "1", null, "2" }, new string[] { "1", null, "2" }, 0},
+            { new string[] { "1", null, "2" }, new string[] { null }, 1},
+            { new string[] { "1", null, "2" }, (string[])null, 0},
+
+            { new string[] { "1", "3", "2" }, new string[] { "1", null, "2" }, -1},
+            { new string[] { "1", "3", "2" }, new string[] { null }, -1},
+            { new string[] { "1", "3", "2" }, (string[])null, 0},
+
+            { null, new string[] { "1", null, "2" }, -1},
+
+            { new string[] { "1", null, null }, new string[] { null, null, "2" }, -1},
+            { new string[] { null, null, null }, new string[] { null, null }, 0},
+        };
+
+        public static TheoryData<string[], int> LastIndexOfNullData => new TheoryData<string[], int>()
+        {
+            { new string[] { "1", null, "2" }, 1},
+            { new string[] { "1", "3", "2" }, -1},
+            { null, -1},
+            { new string[] { "1", null, null }, 2},
+            { new string[] { null, null, null }, 2},
+            { new string[] { null, null, "3" }, 1},
+        };
+
+        public static TheoryData<string[], string[], int> LastIndexOfNullSequenceData => new TheoryData<string[], string[], int>()
+        {
+            { new string[] { "1", null, "2" }, new string[] { "1", null, "2" }, 0},
+            { new string[] { "1", null, "2" }, new string[] { null }, 1},
+            { new string[] { "1", null, "2" }, (string[])null, 0},
+
+            { new string[] { "1", "3", "1" }, new string[] { "1", null, "2" }, -1},
+            { new string[] { "1", "3", "1" }, new string[] { "1" }, 2},
+            { new string[] { "1", "3", "1" }, new string[] { null }, -1},
+            { new string[] { "1", "3", "1" }, (string[])null, 0},
+
+            { null, new string[] { "1", null, "2" }, -1},
+
+            { new string[] { "1", null, null }, new string[] { null, null, "2" }, -1},
+            { new string[] { null, null, null }, new string[] { null, null }, 1},
+        };
     }
 }