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 
7 namespace System.SpanTests
8 {
9     public static partial class SpanTests
10     {
11         [Fact]
ZeroLengthEndsWith()12         public static void ZeroLengthEndsWith()
13         {
14             int[] a = new int[3];
15 
16             Span<int> first = new Span<int>(a, 1, 0);
17             ReadOnlySpan<int> second = new ReadOnlySpan<int>(a, 2, 0);
18             bool b = first.EndsWith(second);
19             Assert.True(b);
20         }
21 
22         [Fact]
SameSpanEndsWith()23         public static void SameSpanEndsWith()
24         {
25             int[] a = { 4, 5, 6 };
26             Span<int> span = new Span<int>(a);
27             bool b = span.EndsWith(span);
28             Assert.True(b);
29         }
30 
31         [Fact]
LengthMismatchEndsWith()32         public static void LengthMismatchEndsWith()
33         {
34             int[] a = { 4, 5, 6 };
35             Span<int> first = new Span<int>(a, 0, 2);
36             ReadOnlySpan<int> second = new ReadOnlySpan<int>(a, 0, 3);
37             bool b = first.EndsWith(second);
38             Assert.False(b);
39         }
40 
41         [Fact]
EndsWithMatch()42         public static void EndsWithMatch()
43         {
44             int[] a = { 4, 5, 6 };
45             Span<int> span = new Span<int>(a, 0, 3);
46             ReadOnlySpan<int> slice = new ReadOnlySpan<int>(a, 1, 2);
47             bool b = span.EndsWith(slice);
48             Assert.True(b);
49         }
50 
51         [Fact]
EndsWithMatchDifferentSpans()52         public static void EndsWithMatchDifferentSpans()
53         {
54             int[] a = { 4, 5, 6 };
55             int[] b = { 4, 5, 6 };
56             Span<int> span = new Span<int>(a, 0, 3);
57             ReadOnlySpan<int> slice = new ReadOnlySpan<int>(b, 0, 3);
58             bool c = span.EndsWith(slice);
59             Assert.True(c);
60         }
61 
62         [Fact]
OnEndsWithOfEqualSpansMakeSureEveryElementIsCompared()63         public static void OnEndsWithOfEqualSpansMakeSureEveryElementIsCompared()
64         {
65             for (int length = 0; length < 100; length++)
66             {
67                 TIntLog log = new TIntLog();
68 
69                 TInt[] first = new TInt[length];
70                 TInt[] second = new TInt[length];
71                 for (int i = 0; i < length; i++)
72                 {
73                     first[i] = second[i] = new TInt(10 * (i + 1), log);
74                 }
75 
76                 Span<TInt> firstSpan = new Span<TInt>(first);
77                 ReadOnlySpan<TInt> secondSpan = new ReadOnlySpan<TInt>(second);
78                 bool b = firstSpan.EndsWith(secondSpan);
79                 Assert.True(b);
80 
81                 // Make sure each element of the array was compared once. (Strictly speaking, it would not be illegal for
82                 // EndsWith to compare an element more than once but that would be a non-optimal implementation and
83                 // a red flag. So we'll stick with the stricter test.)
84                 Assert.Equal(first.Length, log.Count);
85                 foreach (TInt elem in first)
86                 {
87                     int numCompares = log.CountCompares(elem.Value, elem.Value);
88                     Assert.True(numCompares == 1, $"Expected {numCompares} == 1 for element {elem.Value}.");
89                 }
90             }
91         }
92     }
93 }
94