Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Closed
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
8 changes: 4 additions & 4 deletions src/System.Memory/tests/Memory/IndexAndRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public static void SlicingUsingIndexAndRangeTest()
Assert.Equal(memory.Slice(i, a.Length - i), memory[range]);
Assert.Equal(roMemory.Slice(i, a.Length - i), roMemory[range]);

Assert.Equal(memory.Slice(i), memory.Slice(Index.FromStart(i)));
Assert.Equal(roMemory.Slice(i), roMemory.Slice(Index.FromStart(i)));
Assert.Equal(memory.Slice(i), memory[i..]);
Assert.Equal(roMemory.Slice(i), roMemory[i..]);

Assert.Equal(memory.Slice(i, a.Length - i), memory.Slice(range));
Assert.Equal(roMemory.Slice(i, a.Length - i), roMemory.Slice(range));
Assert.Equal(memory.Slice(i, a.Length - i), memory[range]);
Assert.Equal(roMemory.Slice(i, a.Length - i), roMemory[range]);
}

range = new Range(Index.FromStart(0), Index.FromStart(a.Length + 1));
Expand Down
17 changes: 9 additions & 8 deletions src/System.Memory/tests/Span/Indexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,21 @@ public static void SlicingUsingIndexAndRangeTest()

for (int i = 0; i < span.Length; i++)
{
Assert.True(span.Slice(i) == span.Slice(Index.FromStart(i)));
Assert.True(span.Slice(span.Length - i - 1) == span.Slice(Index.FromEnd(i + 1)));
Assert.True(span.Slice(i) == span[i..]);
Assert.True(span.Slice(span.Length - i - 1) == span[^(i + 1)..]);

Assert.True(roSpan.Slice(i) == roSpan.Slice(Index.FromStart(i)));
Assert.True(roSpan.Slice(roSpan.Length - i - 1) == roSpan.Slice(Index.FromEnd(i + 1)));
Assert.True(roSpan.Slice(i) == roSpan[i..]);
Assert.True(roSpan.Slice(roSpan.Length - i - 1) == roSpan[^(i + 1)..]);

range = new Range(Index.FromStart(i), Index.FromEnd(0));
Assert.True(span.Slice(i, span.Length - i) == span.Slice(range));
Assert.True(roSpan.Slice(i, roSpan.Length - i) == roSpan.Slice(range));
Assert.True(span.Slice(i, span.Length - i) == span[range]);
Assert.True(roSpan.Slice(i, roSpan.Length - i) == roSpan[range]);
}

range = new Range(Index.FromStart(0), Index.FromStart(span.Length + 1));
Assert.Throws<ArgumentOutOfRangeException>(() => new Span<char>(s.ToCharArray()).Slice(range));
Assert.Throws<ArgumentOutOfRangeException>(() => s.AsSpan().Slice(range));

Assert.Throws<ArgumentOutOfRangeException>(delegate() { var spp = new Span<char>(s.ToCharArray())[range]; });
Assert.Throws<ArgumentOutOfRangeException>(delegate() { var spp = s.AsSpan()[range]; });
}
}
}
20 changes: 0 additions & 20 deletions src/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,6 @@ public MemberAccessException(string message, System.Exception inner) { }
public Memory(T[] array, int start, int length) { throw null; }
public static System.Memory<T> Empty { get { throw null; } }
public bool IsEmpty { get { throw null; } }
public System.Memory<T> this[System.Range range] { get { throw null; } }
public int Length { get { throw null; } }
public System.Span<T> Span { get { throw null; } }
public void CopyTo(System.Memory<T> destination) { }
Expand All @@ -1745,10 +1744,8 @@ public void CopyTo(System.Memory<T> destination) { }
public static implicit operator System.ReadOnlyMemory<T> (System.Memory<T> memory) { throw null; }
public static implicit operator System.Memory<T> (T[] array) { throw null; }
public System.Buffers.MemoryHandle Pin() { throw null; }
public System.Memory<T> Slice(System.Index startIndex) { throw null; }
public System.Memory<T> Slice(int start) { throw null; }
public System.Memory<T> Slice(int start, int length) { throw null; }
public System.Memory<T> Slice(System.Range range) { throw null; }
public T[] ToArray() { throw null; }
public override string ToString() { throw null; }
public bool TryCopyTo(System.Memory<T> destination) { throw null; }
Expand Down Expand Up @@ -2003,7 +2000,6 @@ public RankException(string message, System.Exception innerException) { }
public ReadOnlyMemory(T[] array, int start, int length) { throw null; }
public static System.ReadOnlyMemory<T> Empty { get { throw null; } }
public bool IsEmpty { get { throw null; } }
public System.ReadOnlyMemory<T> this[System.Range range] { get { throw null; } }
public int Length { get { throw null; } }
public System.ReadOnlySpan<T> Span { get { throw null; } }
public void CopyTo(System.Memory<T> destination) { }
Expand All @@ -2015,10 +2011,8 @@ public void CopyTo(System.Memory<T> destination) { }
public static implicit operator System.ReadOnlyMemory<T> (System.ArraySegment<T> segment) { throw null; }
public static implicit operator System.ReadOnlyMemory<T> (T[] array) { throw null; }
public System.Buffers.MemoryHandle Pin() { throw null; }
public System.ReadOnlyMemory<T> Slice(System.Index startIndex) { throw null; }
public System.ReadOnlyMemory<T> Slice(int start) { throw null; }
public System.ReadOnlyMemory<T> Slice(int start, int length) { throw null; }
public System.ReadOnlyMemory<T> Slice(System.Range range) { throw null; }
public T[] ToArray() { throw null; }
public override string ToString() { throw null; }
public bool TryCopyTo(System.Memory<T> destination) { throw null; }
Expand All @@ -2033,9 +2027,7 @@ public readonly ref partial struct ReadOnlySpan<T>
public ReadOnlySpan(T[] array, int start, int length) { throw null; }
public static System.ReadOnlySpan<T> Empty { get { throw null; } }
public bool IsEmpty { get { throw null; } }
public ref readonly T this[System.Index index] { get { throw null; } }
public ref readonly T this[int index] { get { throw null; } }
public System.ReadOnlySpan<T> this[System.Range range] { get { throw null; } }
public int Length { get { throw null; } }
public void CopyTo(System.Span<T> destination) { }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
Expand All @@ -2051,10 +2043,8 @@ public void CopyTo(System.Span<T> destination) { }
public static implicit operator System.ReadOnlySpan<T> (System.ArraySegment<T> segment) { throw null; }
public static implicit operator System.ReadOnlySpan<T> (T[] array) { throw null; }
public static bool operator !=(System.ReadOnlySpan<T> left, System.ReadOnlySpan<T> right) { throw null; }
public System.ReadOnlySpan<T> Slice(System.Index startIndex) { throw null; }
public System.ReadOnlySpan<T> Slice(int start) { throw null; }
public System.ReadOnlySpan<T> Slice(int start, int length) { throw null; }
public System.ReadOnlySpan<T> Slice(System.Range range) { throw null; }
public T[] ToArray() { throw null; }
public override string ToString() { throw null; }
public bool TryCopyTo(System.Span<T> destination) { throw null; }
Expand Down Expand Up @@ -2233,9 +2223,7 @@ public readonly ref partial struct Span<T>
public Span(T[] array, int start, int length) { throw null; }
public static System.Span<T> Empty { get { throw null; } }
public bool IsEmpty { get { throw null; } }
public ref T this[System.Index index] { get { throw null; } }
public ref T this[int index] { get { throw null; } }
public System.Span<T> this[System.Range range] { get { throw null; } }
public int Length { get { throw null; } }
public void Clear() { }
public void CopyTo(System.Span<T> destination) { }
Expand All @@ -2254,10 +2242,8 @@ public void Fill(T value) { }
public static implicit operator System.ReadOnlySpan<T> (System.Span<T> span) { throw null; }
public static implicit operator System.Span<T> (T[] array) { throw null; }
public static bool operator !=(System.Span<T> left, System.Span<T> right) { throw null; }
public System.Span<T> Slice(System.Index startIndex) { throw null; }
public System.Span<T> Slice(int start) { throw null; }
public System.Span<T> Slice(int start, int length) { throw null; }
public System.Span<T> Slice(System.Range range) { throw null; }
public T[] ToArray() { throw null; }
public override string ToString() { throw null; }
public bool TryCopyTo(System.Span<T> destination) { throw null; }
Expand Down Expand Up @@ -2298,11 +2284,7 @@ public unsafe String(sbyte* value, int startIndex, int length) { }
[System.CLSCompliantAttribute(false)]
public unsafe String(sbyte* value, int startIndex, int length, System.Text.Encoding enc) { }
[System.Runtime.CompilerServices.IndexerName("Chars")]
public char this[System.Index index] { get { throw null; } }
[System.Runtime.CompilerServices.IndexerName("Chars")]
public char this[int index] { get { throw null; } }
[System.Runtime.CompilerServices.IndexerName("Chars")]
public string this[System.Range range] { get { throw null; } }
public int Length { get { throw null; } }
public object Clone() { throw null; }
public static int Compare(System.String strA, int indexA, System.String strB, int indexB, int length) { throw null; }
Expand Down Expand Up @@ -2437,10 +2419,8 @@ public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, in
public bool StartsWith(System.String value) { throw null; }
public bool StartsWith(System.String value, bool ignoreCase, System.Globalization.CultureInfo culture) { throw null; }
public bool StartsWith(System.String value, System.StringComparison comparisonType) { throw null; }
public System.String Substring(System.Index startIndex) { throw null; }
public System.String Substring(int startIndex) { throw null; }
public System.String Substring(int startIndex, int length) { throw null; }
public System.String Substring(System.Range range) { throw null; }
System.Collections.Generic.IEnumerator<char> System.Collections.Generic.IEnumerable<System.Char>.GetEnumerator() { throw null; }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
bool System.IConvertible.ToBoolean(System.IFormatProvider provider) { throw null; }
Expand Down
19 changes: 19 additions & 0 deletions src/System.Runtime/tests/System/IndexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using Xunit;

namespace System.Tests
Expand Down Expand Up @@ -111,5 +112,23 @@ public static void ToStringTest()
index1 = new Index(50, fromEnd: true);
Assert.Equal("^" + 50.ToString(), index1.ToString());
}

[Fact]
public static void CollectionTest()
{
int [] array = new int [] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<int> list = new List<int>(array);

for (int i = 0; i < list.Count; i++)
{
Assert.Equal(i, list[Index.FromStart(i)]);
Assert.Equal(list.Count - i - 1, list[^(i + 1)]);

Assert.Equal(i, array[Index.FromStart(i)]);
Assert.Equal(list.Count - i - 1, array[^(i + 1)]);

Assert.Equal(array.AsSpan().Slice(i, array.Length - i).ToArray(), array[i..]);
}
}
}
}
63 changes: 63 additions & 0 deletions src/System.Runtime/tests/System/RangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,68 @@ public static void ToStringTest()
range1 = new Range(new Index(10, fromEnd: false), new Index(20, fromEnd: true));
Assert.Equal(10.ToString() + "..^" + 20.ToString(), range1.ToString());
}

[Fact]
public static void CustomTypeTest()
{
CustomRangeTester crt = new CustomRangeTester(new int [] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
for (int i = 0; i < crt.Length; i++)
{
Assert.Equal(crt[i], crt[Index.FromStart(i)]);
Assert.Equal(crt[crt.Length - i - 1], crt[^(i + 1)]);

Assert.True(crt.Slice(i, crt.Length - i).Equals(crt[i..^0]), $"Index = {i} and {crt.Slice(i, crt.Length - i)} != {crt[i..^0]}");
}
}

// CustomRangeTester is a custom class which containing the members Length, Slice and int indexer.
// Having these members allow the C# compiler to support
// this[Index]
// this[Range]
private class CustomRangeTester : IEquatable<CustomRangeTester>
{
private int [] _data;

public CustomRangeTester(int [] data) => _data = data;
public int Length => _data.Length;
public int this[int index] => _data[index];
public CustomRangeTester Slice(int start, int length) => new CustomRangeTester(_data.AsSpan().Slice(start, length).ToArray());

public int [] Data => _data;

public bool Equals (CustomRangeTester other)
{
if (_data.Length == other.Data.Length)
{
for (int i = 0; i < _data.Length; i++)
{
if (_data[i] != other.Data[i])
{
return false;
}
}
return true;
}

return false;
}

public override string ToString()
{
if (Length == 0)
{
return "[]";
}

string s = "[" + _data[0];

for (int i = 1; i < Length; i++)
{
s = s + ", " + _data[i];
}

return s + "]";
}
}
}
}
108 changes: 54 additions & 54 deletions src/System.Runtime/tests/System/StringTests.netcoreapp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,51 +1151,51 @@ public static void Concat_Spans(string[] values, string expected)
Assert.Equal(expected, result);
}

// [Fact]
// public static void IndexerUsingIndexTest()
// {
// Index index;
// string s = "0123456789ABCDEF";

// for (int i = 0; i < s.Length; i++)
// {
// index = Index.FromStart(i);
// Assert.Equal(s[i], s[index]);

// index = Index.FromEnd(i + 1);
// Assert.Equal(s[s.Length - i - 1], s[index]);
// }

// index = Index.FromStart(s.Length + 1);
// char c;
// Assert.Throws<IndexOutOfRangeException>(() => c = s[index]);

// index = Index.FromEnd(s.Length + 1);
// Assert.Throws<IndexOutOfRangeException>(() => c = s[index]);
// }

// [Fact]
// public static void IndexerUsingRangeTest()
// {
// Range range;
// string s = "0123456789ABCDEF";

// for (int i = 0; i < s.Length; i++)
// {
// range = new Range(Index.FromStart(0), Index.FromStart(i));
// Assert.Equal(s.Substring(0, i), s[range]);

// range = new Range(Index.FromEnd(s.Length), Index.FromEnd(i));
// Assert.Equal(s.Substring(0, s.Length - i), s[range]);
// }

// range = new Range(Index.FromStart(s.Length - 2), Index.FromStart(s.Length + 1));
// string s1;
// Assert.Throws<ArgumentOutOfRangeException>(() => s1 = s[range]);

// range = new Range(Index.FromEnd(s.Length + 1), Index.FromEnd(0));
// Assert.Throws<ArgumentOutOfRangeException>(() => s1 = s[range]);
// }
[Fact]
public static void IndexerUsingIndexTest()
{
Index index;
string s = "0123456789ABCDEF";

for (int i = 0; i < s.Length; i++)
{
index = Index.FromStart(i);
Assert.Equal(s[i], s[index]);

index = Index.FromEnd(i + 1);
Assert.Equal(s[s.Length - i - 1], s[index]);
}

index = Index.FromStart(s.Length + 1);
char c;
Assert.Throws<IndexOutOfRangeException>(() => c = s[index]);

index = Index.FromEnd(s.Length + 1);
Assert.Throws<IndexOutOfRangeException>(() => c = s[index]);
}

[Fact]
public static void IndexerUsingRangeTest()
{
Range range;
string s = "0123456789ABCDEF";

for (int i = 0; i < s.Length; i++)
{
range = new Range(Index.FromStart(0), Index.FromStart(i));
Assert.Equal(s.Substring(0, i), s[range]);

range = new Range(Index.FromEnd(s.Length), Index.FromEnd(i));
Assert.Equal(s.Substring(0, s.Length - i), s[range]);
}

range = new Range(Index.FromStart(s.Length - 2), Index.FromStart(s.Length + 1));
string s1;
Assert.Throws<ArgumentOutOfRangeException>(() => s1 = s[range]);

range = new Range(Index.FromEnd(s.Length + 1), Index.FromEnd(0));
Assert.Throws<ArgumentOutOfRangeException>(() => s1 = s[range]);
}

[Fact]
public static void SubstringUsingIndexTest()
Expand All @@ -1204,15 +1204,15 @@ public static void SubstringUsingIndexTest()

for (int i = 0; i < s.Length; i++)
{
Assert.Equal(s.Substring(i), s.Substring(Index.FromStart(i)));
Assert.Equal(s.Substring(s.Length - i - 1), s.Substring(Index.FromEnd(i + 1)));
Assert.Equal(s.Substring(i), s[i..]);
Assert.Equal(s.Substring(s.Length - i - 1), s[^(i + 1)..]);
}

// String.Substring allows the string length as a valid input.
Assert.Equal(s.Substring(s.Length), s.Substring(Index.FromStart(s.Length)));
Assert.Equal(s.Substring(s.Length), s[s.Length..]);

Assert.Throws<ArgumentOutOfRangeException>(() => s.Substring(Index.FromStart(s.Length + 1)));
Assert.Throws<ArgumentOutOfRangeException>(() => s.Substring(Index.FromEnd(s.Length + 1)));
Assert.Throws<ArgumentOutOfRangeException>(() => s[(s.Length + 1)..]);
Assert.Throws<ArgumentOutOfRangeException>(() => s[^(s.Length + 1)..]);
}

[Fact]
Expand All @@ -1224,18 +1224,18 @@ public static void SubstringUsingRangeTest()
for (int i = 0; i < s.Length; i++)
{
range = new Range(Index.FromStart(0), Index.FromStart(i));
Assert.Equal(s.Substring(0, i), s.Substring(range));
Assert.Equal(s.Substring(0, i), s[range]);

range = new Range(Index.FromEnd(s.Length), Index.FromEnd(i));
Assert.Equal(s.Substring(0, s.Length - i), s.Substring(range));
Assert.Equal(s.Substring(0, s.Length - i), s[range]);
}

range = new Range(Index.FromStart(s.Length - 2), Index.FromStart(s.Length + 1));
string s1;
Assert.Throws<ArgumentOutOfRangeException>(() => s1 = s.Substring(range));
Assert.Throws<ArgumentOutOfRangeException>(() => s1 = s[range]);

range = new Range(Index.FromEnd(s.Length + 1), Index.FromEnd(0));
Assert.Throws<ArgumentOutOfRangeException>(() => s1 = s.Substring(range));
Assert.Throws<ArgumentOutOfRangeException>(() => s1 = s[range]);
}

/// <summary>
Expand Down
Loading