This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expose Index and Range with adding tests #33352
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
00066df
Expose Index and Range with adding tests
tarekgh 3953dd5
More tests and more exposed APIs
tarekgh 1564fde
More changes
tarekgh bf2e574
address feedback
tarekgh e1fc9fc
Merge branch 'master' of https://github.com/dotnet/corefx into IndexA…
tarekgh 4fde6ee
Fix ApiCompat break
tarekgh 92f7b8f
Fix UAPAOT app compat
tarekgh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Compat issues with assembly System.Memory: | ||
| MembersMustExist : Member 'System.ReadOnlySpan<T>.Item.get(System.Index)' does not exist in the implementation but it does exist in the contract. | ||
| MembersMustExist : Member 'System.ReadOnlySpan<T>.Item.get(System.Range)' does not exist in the implementation but it does exist in the contract. | ||
| MembersMustExist : Member 'System.Span<T>.Item.get(System.Index)' does not exist in the implementation but it does exist in the contract. | ||
| MembersMustExist : Member 'System.Span<T>.Item.get(System.Range)' does not exist in the implementation but it does exist in the contract. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Numerics; | ||
| using Xunit; | ||
|
|
||
| namespace System.SpanTests | ||
| { | ||
| public static partial class IndexerTests | ||
| { | ||
| [Fact] | ||
| public static void IndexerWithIndexTest() | ||
| { | ||
| ReadOnlySpan<char> span = "Hello".AsSpan(); | ||
| Assert.Equal('H', span[new Index(0, fromEnd: false)]); | ||
| Assert.Equal('o', span[new Index(1, fromEnd: true)]); | ||
| Assert.Equal(span[new Index(2, fromEnd: false)], span[new Index(span.Length - 2, fromEnd: true)]); | ||
|
|
||
| Assert.Throws<IndexOutOfRangeException>(() => "Hello".AsSpan()[new Index(0, fromEnd: true)]); | ||
|
|
||
| Span<char> span1 = new Span<char>(new char [] { 'H', 'e', 'l', 'l', 'o'}); | ||
| Assert.Equal('e', span1[new Index(1, fromEnd: false)]); | ||
| Assert.Equal('l', span1[new Index(2, fromEnd: true)]); | ||
| Assert.Equal(span1[new Index(2, fromEnd: false)], span1[new Index(span.Length - 2, fromEnd: true)]); | ||
|
|
||
| Assert.Throws<IndexOutOfRangeException>(() => | ||
| new Span<char>(new char [] { 'H', 'e', 'l', 'l', 'o'})[new Index(0, fromEnd: true)]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void IndexerWithRangeTest() | ||
| { | ||
| ReadOnlySpan<char> span = "Hello".AsSpan(); | ||
| ReadOnlySpan<char> sliced = span[Range.Create(new Index(1, fromEnd: false), new Index(1, fromEnd: true))]; | ||
| Assert.True(span.Slice(1, 3) == sliced); | ||
|
|
||
| Assert.Throws<ArgumentOutOfRangeException>(() => | ||
| { ReadOnlySpan<char> s = "Hello".AsSpan()[Range.Create(new Index(1, fromEnd: true), new Index(1, fromEnd: false))]; }); | ||
|
|
||
| Span<char> span1 = new Span<char>(new char [] { 'H', 'e', 'l', 'l', 'o'}); | ||
| Span<char> sliced1 = span1[Range.Create(new Index(2, fromEnd: false), new Index(1, fromEnd: true))]; | ||
| Assert.True(span1.Slice(2, 2) == sliced1); | ||
|
|
||
| Assert.Throws<ArgumentOutOfRangeException>(() => | ||
| { Span<char> s = new Span<char>(new char [] { 'H', 'i' })[Range.Create(new Index(0, fromEnd: true), new Index(1, fromEnd: false))]; }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| TypesMustExist : Type 'System.Index' does not exist in the implementation but it does exist in the contract. | ||
| TypesMustExist : Type 'System.Range' does not exist in the implementation but it does exist in the contract. | ||
| MembersMustExist : Member 'System.ReadOnlySpan<T>.Item.get(System.Index)' does not exist in the implementation but it does exist in the contract. | ||
| MembersMustExist : Member 'System.ReadOnlySpan<T>.Item.get(System.Range)' does not exist in the implementation but it does exist in the contract. | ||
| MembersMustExist : Member 'System.Span<T>.Item.get(System.Index)' does not exist in the implementation but it does exist in the contract. | ||
| MembersMustExist : Member 'System.Span<T>.Item.get(System.Range)' does not exist in the implementation but it does exist in the contract. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using Xunit; | ||
|
|
||
| namespace System.Tests | ||
| { | ||
| public static class IndexTests | ||
| { | ||
| [Fact] | ||
| public static void CreationTest() | ||
| { | ||
| Index index = new Index(1, fromEnd: false); | ||
| Assert.Equal(1, index.Value); | ||
| Assert.False(index.FromEnd); | ||
|
|
||
| index = new Index(11, fromEnd: true); | ||
| Assert.Equal(11, index.Value); | ||
| Assert.True(index.FromEnd); | ||
|
|
||
| AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => new Index(-1, fromEnd: false)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ImplicitCastTest() | ||
| { | ||
| Index index = 10; | ||
| Assert.Equal(10, index.Value); | ||
| Assert.False(index.FromEnd); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void EqualityTest() | ||
| { | ||
| Index index1 = 10; | ||
| Index index2 = 10; | ||
| Assert.True(index1.Equals(index2)); | ||
| Assert.True(index1.Equals((object)index2)); | ||
|
|
||
| index2 = new Index(10, fromEnd: true); | ||
| Assert.False(index1.Equals(index2)); | ||
| Assert.False(index1.Equals((object)index2)); | ||
|
|
||
| index2 = new Index(9, fromEnd: false); | ||
| Assert.False(index1.Equals(index2)); | ||
| Assert.False(index1.Equals((object)index2)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void HashCodeTest() | ||
| { | ||
| Index index1 = 10; | ||
| Index index2 = 10; | ||
| Assert.Equal(index1.GetHashCode(), index2.GetHashCode()); | ||
|
|
||
| index2 = new Index(10, fromEnd: true); | ||
| Assert.NotEqual(index1.GetHashCode(), index2.GetHashCode()); | ||
|
|
||
| index2 = new Index(99999, fromEnd: false); | ||
| Assert.NotEqual(index1.GetHashCode(), index2.GetHashCode()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ToStringTest() | ||
| { | ||
| Index index1 = 100; | ||
| Assert.Equal(100.ToString(), index1.ToString()); | ||
|
|
||
| index1 = new Index(50, fromEnd: true); | ||
| Assert.Equal("^" + 50.ToString(), index1.ToString()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using Xunit; | ||
|
|
||
| namespace System.Tests | ||
| { | ||
| public static class RangeTests | ||
| { | ||
| [Fact] | ||
| public static void CreationTest() | ||
| { | ||
| Range range = Range.Create(new Index(10, fromEnd: false), new Index(2, fromEnd: true)); | ||
| Assert.Equal(10, range.Start.Value); | ||
| Assert.False(range.Start.FromEnd); | ||
| Assert.Equal(2, range.End.Value); | ||
| Assert.True(range.End.FromEnd); | ||
|
|
||
| range = Range.FromStart(new Index(7, fromEnd: false)); | ||
| Assert.Equal(7, range.Start.Value); | ||
| Assert.False(range.Start.FromEnd); | ||
| Assert.Equal(0, range.End.Value); | ||
| Assert.True(range.End.FromEnd); | ||
|
|
||
| range = Range.ToEnd(new Index(3, fromEnd: true)); | ||
| Assert.Equal(0, range.Start.Value); | ||
| Assert.False(range.Start.FromEnd); | ||
| Assert.Equal(3, range.End.Value); | ||
| Assert.True(range.End.FromEnd); | ||
|
|
||
| range = Range.All(); | ||
| Assert.Equal(0, range.Start.Value); | ||
| Assert.False(range.Start.FromEnd); | ||
| Assert.Equal(0, range.End.Value); | ||
| Assert.True(range.End.FromEnd); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void EqualityTest() | ||
| { | ||
| Range range1 = Range.Create(new Index(10, fromEnd: false), new Index(20, fromEnd: false)); | ||
| Range range2 = Range.Create(new Index(10, fromEnd: false), new Index(20, fromEnd: false)); | ||
| Assert.True(range1.Equals(range2)); | ||
| Assert.True(range1.Equals((object)range2)); | ||
|
|
||
| range2 = Range.Create(new Index(10, fromEnd: false), new Index(20, fromEnd: true)); | ||
| Assert.False(range1.Equals(range2)); | ||
| Assert.False(range1.Equals((object)range2)); | ||
|
|
||
| range2 = Range.Create(new Index(10, fromEnd: false), new Index(21, fromEnd: false)); | ||
| Assert.False(range1.Equals(range2)); | ||
| Assert.False(range1.Equals((object)range2)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void HashCodeTest() | ||
| { | ||
| Range range1 = Range.Create(new Index(10, fromEnd: false), new Index(20, fromEnd: false)); | ||
| Range range2 = Range.Create(new Index(10, fromEnd: false), new Index(20, fromEnd: false)); | ||
| Assert.Equal(range1.GetHashCode(), range2.GetHashCode()); | ||
|
|
||
| range2 = Range.Create(new Index(10, fromEnd: false), new Index(20, fromEnd: true)); | ||
| Assert.NotEqual(range1.GetHashCode(), range2.GetHashCode()); | ||
|
|
||
| range2 = Range.Create(new Index(10, fromEnd: false), new Index(21, fromEnd: false)); | ||
| Assert.NotEqual(range1.GetHashCode(), range2.GetHashCode()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ToStringTest() | ||
| { | ||
| Range range1 = Range.Create(new Index(10, fromEnd: false), new Index(20, fromEnd: false)); | ||
| Assert.Equal(10.ToString() + ".." + 20.ToString(), range1.ToString()); | ||
|
|
||
| range1 = Range.Create(new Index(10, fromEnd: false), new Index(20, fromEnd: true)); | ||
| Assert.Equal(10.ToString() + "..^" + 20.ToString(), range1.ToString()); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't like this semantic in the slightest. It's like going backwards to VB6-esque programming models.
