Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/System.Memory/src/ApiCompatBaseline.netcoreappaot.txt
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.
49 changes: 49 additions & 0 deletions src/System.Memory/tests/Span/Indexer.cs
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)]);

Copy link
Copy Markdown

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.

}

[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))]; });
}
}
}
4 changes: 4 additions & 0 deletions src/System.Memory/tests/System.Memory.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@
<Compile Include="ParsersAndFormatters\TestData.cs" />
<Compile Include="ParsersAndFormatters\TestUtils.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' == 'netcoreapp'">
<Compile Include="Span\Indexer.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="Base64\Base64DecoderUnitTests.cs" />
<Compile Include="Base64\Base64EncoderUnitTests.cs" />
Expand Down
33 changes: 32 additions & 1 deletion src/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,33 @@ void System.Runtime.Serialization.IDeserializationCallback.OnDeserialization(obj
public static bool TryParse(string s, out System.Decimal result) { throw null; }
public static bool TryParse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out System.Decimal result) { throw null; }
}
public readonly partial struct Index : System.IEquatable<System.Index>
{
private readonly int _dummyPrimitive;
public Index(int value, bool fromEnd) { throw null; }
public int Value { get { throw null; } }
public bool FromEnd { get { throw null; } }
public override bool Equals(object value) { throw null; }
public bool Equals(Index other) { throw null; }
public override int GetHashCode() { throw null; }
public override string ToString() { throw null; }
public static implicit operator Index(int value) { throw null; }
}
public readonly partial struct Range : System.IEquatable<System.Range>
{
private readonly int _dummyPrimitive;
public Index Start { get { throw null; } }
public Index End { get { throw null; } }
public override bool Equals(object value) { throw null; }
public bool Equals(Range other) { throw null; }
public override int GetHashCode() { throw null; }
public override string ToString() { throw null; }
public static Range Create(Index start, Index end) { throw null; }
public static Range FromStart(Index start) { throw null; }
public static Range ToEnd(Index end) { throw null; }
public static Range All() { throw null; }
}

public abstract partial class Delegate : System.ICloneable, System.Runtime.Serialization.ISerializable
{
protected Delegate(object target, string method) { }
Expand Down Expand Up @@ -1983,6 +2010,8 @@ public readonly ref partial struct ReadOnlySpan<T>
public static System.ReadOnlySpan<T> Empty { get { throw null; } }
public bool IsEmpty { get { throw null; } }
public ref readonly T this[int index] { get { throw null; } }
public ref readonly T this[Index index] { get { throw null; } }
public ReadOnlySpan<T> this[Range range] { get { throw null; } }
public int Length { get { throw null; } }
public void CopyTo(System.Span<T> destination) { }
[System.ComponentModel.EditorBrowsableAttribute((System.ComponentModel.EditorBrowsableState)(1))]
Expand Down Expand Up @@ -2185,6 +2214,8 @@ public readonly ref partial struct Span<T>
public static System.Span<T> Empty { get { throw null; } }
public bool IsEmpty { get { throw null; } }
public ref T this[int index] { get { throw null; } }
public ref T this[Index index] { get { throw null; } }
public Span<T> this[Range range] { get { throw null; } }
public int Length { get { throw null; } }
public void Clear() { }
public void CopyTo(System.Span<T> destination) { }
Expand Down Expand Up @@ -7003,7 +7034,7 @@ public StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind layoutKin
namespace System.Runtime.Remoting
{
public class ObjectHandle : MarshalByRefObject
{
{
private ObjectHandle() { }
public ObjectHandle(Object o) { }
public Object Unwrap() { throw null; }
Expand Down
6 changes: 6 additions & 0 deletions src/System.Runtime/src/ApiCompatBaseline.netcoreappaot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ MembersMustExist : Member 'System.String System.Runtime.CompilerServices.Runtime
TypeCannotChangeClassification : Type 'System.DateTimeOffset' is marked as readonly in the contract so it must also be marked readonly in the implementation.
TypeCannotChangeClassification : Type 'System.TimeSpan' is marked as readonly in the contract so it must also be marked readonly in the implementation.
TypeCannotChangeClassification : Type 'System.Runtime.Serialization.SerializationEntry' is marked as readonly in the contract so it must also be marked readonly in the implementation.
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.
6 changes: 6 additions & 0 deletions src/System.Runtime/src/ApiCompatBaseline.uapaot.txt
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.
2 changes: 2 additions & 0 deletions src/System.Runtime/tests/System.Runtime.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,14 @@
<Compile Include="System\FormattableStringTests.netcoreapp.cs" />
<Compile Include="System\GCTests.netcoreapp.cs" />
<Compile Include="System\GuidTests.netcoreapp.cs" />
<Compile Include="System\IndexTests.cs" />
<Compile Include="System\Int16Tests.netcoreapp.cs" />
<Compile Include="System\Int32Tests.netcoreapp.cs" />
<Compile Include="System\Int64Tests.netcoreapp.cs" />
<Compile Include="System\IntPtrTests.netcoreapp.cs" />
<Compile Include="System\Collections\Generic\KeyValuePairTests.netcoreapp.cs" />
<Compile Include="System\LazyTests.netcoreapp.cs" />
<Compile Include="System\RangeTests.cs" />
<Compile Include="System\SByteTests.netcoreapp.cs" />
<Compile Include="System\StringComparerTests.netcoreapp.cs" />
<Compile Include="System\StringGetHashCodeTests.netcoreapp.cs" />
Expand Down
75 changes: 75 additions & 0 deletions src/System.Runtime/tests/System/IndexTests.cs
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());
}
}
}
81 changes: 81 additions & 0 deletions src/System.Runtime/tests/System/RangeTests.cs
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());
}
}
}