1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using Xunit;
6 using System.Runtime.CompilerServices;
7 using System.Runtime.InteropServices;
8 
9 namespace System.SpanTests
10 {
11     public static partial class SpanTests
12     {
13         [Fact]
SliceInt()14         public static void SliceInt()
15         {
16             int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
17             Span<int> span = new Span<int>(a).Slice(6);
18             Assert.Equal(4, span.Length);
19             Assert.True(Unsafe.AreSame(ref a[6], ref MemoryMarshal.GetReference(span)));
20         }
21 
22         [Fact]
SliceIntPastEnd()23         public static void SliceIntPastEnd()
24         {
25             int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
26             Span<int> span = new Span<int>(a).Slice(a.Length);
27             Assert.Equal(0, span.Length);
28             Assert.True(Unsafe.AreSame(ref a[a.Length - 1], ref Unsafe.Subtract<int>(ref MemoryMarshal.GetReference(span), 1)));
29         }
30 
31         [Fact]
SliceIntInt()32         public static void SliceIntInt()
33         {
34             int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
35             Span<int> span = new Span<int>(a).Slice(3, 5);
36             Assert.Equal(5, span.Length);
37             Assert.True(Unsafe.AreSame(ref a[3], ref MemoryMarshal.GetReference(span)));
38         }
39 
40         [Fact]
SliceIntIntUpToEnd()41         public static void SliceIntIntUpToEnd()
42         {
43             int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
44             Span<int> span = new Span<int>(a).Slice(4, 6);
45             Assert.Equal(6, span.Length);
46             Assert.True(Unsafe.AreSame(ref a[4], ref MemoryMarshal.GetReference(span)));
47         }
48 
49         [Fact]
SliceIntIntPastEnd()50         public static void SliceIntIntPastEnd()
51         {
52             int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
53             Span<int> span = new Span<int>(a).Slice(a.Length, 0);
54             Assert.Equal(0, span.Length);
55             Assert.True(Unsafe.AreSame(ref a[a.Length - 1], ref Unsafe.Subtract<int>(ref MemoryMarshal.GetReference(span), 1)));
56         }
57 
58         [Fact]
SliceIntRangeChecksd()59         public static void SliceIntRangeChecksd()
60         {
61             int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
62             Assert.Throws<ArgumentOutOfRangeException>(() => new Span<int>(a).Slice(-1).DontBox());
63             Assert.Throws<ArgumentOutOfRangeException>(() => new Span<int>(a).Slice(a.Length + 1).DontBox());
64             Assert.Throws<ArgumentOutOfRangeException>(() => new Span<int>(a).Slice(-1, 0).DontBox());
65             Assert.Throws<ArgumentOutOfRangeException>(() => new Span<int>(a).Slice(0, a.Length + 1).DontBox());
66             Assert.Throws<ArgumentOutOfRangeException>(() => new Span<int>(a).Slice(2, a.Length + 1 - 2).DontBox());
67             Assert.Throws<ArgumentOutOfRangeException>(() => new Span<int>(a).Slice(a.Length + 1, 0).DontBox());
68             Assert.Throws<ArgumentOutOfRangeException>(() => new Span<int>(a).Slice(a.Length, 1).DontBox());
69         }
70     }
71 }
72