-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
IEnumerableTests.cs
40 lines (31 loc) · 1.18 KB
/
IEnumerableTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text;
using Xunit;
namespace System.Collections.Generic.Tests
{
public class IEnumerableTests
{
[Fact]
public void SupportsRefStructT()
{
StringBuilder builder = new();
foreach (ReadOnlySpan<char> c in new MySpanReturningEnumerable("hello"))
{
builder.Append(c);
}
Assert.Equal("hello", builder.ToString());
}
}
internal sealed class MySpanReturningEnumerable(string value) : IEnumerable<ReadOnlySpan<char>>, IEnumerator<ReadOnlySpan<char>>
{
private int _index = -1;
public IEnumerator<ReadOnlySpan<char>> GetEnumerator() => this;
IEnumerator IEnumerable.GetEnumerator() => this;
public ReadOnlySpan<char> Current => value.AsSpan(_index, 1);
public bool MoveNext() => ++_index < value.Length;
public void Dispose() { }
object IEnumerator.Current => throw new NotSupportedException();
void IEnumerator.Reset() => throw new NotSupportedException();
}
}