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 #pragma warning disable 1718 //Comparison made to same variable; did you mean to compare something else?
8 
9 namespace System.SpanTests
10 {
11     public static partial class SpanTests
12     {
13         [Fact]
EqualityTrue()14         public static void EqualityTrue()
15         {
16             int[] a = { 91, 92, 93, 94, 95 };
17             Span<int> left = new Span<int>(a, 2, 3);
18             Span<int> right = new Span<int>(a, 2, 3);
19 
20             Assert.True(left == right);
21             Assert.True(!(left != right));
22         }
23 
24         [Fact]
EqualityReflexivity()25         public static void EqualityReflexivity()
26         {
27             int[] a = { 91, 92, 93, 94, 95 };
28             Span<int> left = new Span<int>(a, 2, 3);
29 
30             Assert.True(left == left);
31             Assert.True(!(left != left));
32         }
33 
34         [Fact]
EqualityIncludesLength()35         public static void EqualityIncludesLength()
36         {
37             int[] a = { 91, 92, 93, 94, 95 };
38             Span<int> left = new Span<int>(a, 2, 1);
39             Span<int> right = new Span<int>(a, 2, 3);
40 
41             Assert.False(left == right);
42             Assert.False(!(left != right));
43         }
44 
45         [Fact]
EqualityIncludesBase()46         public static void EqualityIncludesBase()
47         {
48             int[] a = { 91, 92, 93, 94, 95 };
49             Span<int> left = new Span<int>(a, 1, 3);
50             Span<int> right = new Span<int>(a, 2, 3);
51 
52             Assert.False(left == right);
53             Assert.False(!(left != right));
54         }
55 
56         [Fact]
EqualityBasedOnLocationNotConstructor()57         public static void EqualityBasedOnLocationNotConstructor()
58         {
59             unsafe
60             {
61                 int[] a = { 91, 92, 93, 94, 95 };
62                 fixed (int* pa = a)
63                 {
64                     Span<int> left = new Span<int>(a, 2, 3);
65                     Span<int> right = new Span<int>(pa + 2, 3);
66 
67                     Assert.True(left == right);
68                     Assert.True(!(left != right));
69                 }
70             }
71         }
72 
73         [Fact]
EqualityComparesRangeNotContent()74         public static void EqualityComparesRangeNotContent()
75         {
76             Span<int> left = new Span<int>(new int[] { 0, 1, 2 }, 1, 1);
77             Span<int> right = new Span<int>(new int[] { 0, 1, 2 }, 1, 1);
78 
79             Assert.False(left == right);
80             Assert.False(!(left != right));
81         }
82 
83         [Fact]
EmptySpansNotUnified()84         public static void EmptySpansNotUnified()
85         {
86             Span<int> left = new Span<int>(new int[0]);
87             Span<int> right = new Span<int>(new int[0]);
88 
89             Assert.False(left == right);
90             Assert.False(!(left != right));
91         }
92 
93         [Fact]
CannotCallEqualsOnSpan()94         public static void CannotCallEqualsOnSpan()
95         {
96             Span<int> left = new Span<int>(new int[0]);
97 
98             try
99             {
100 #pragma warning disable 0618
101                 bool result = left.Equals(new object());
102 #pragma warning restore 0618
103                 Assert.True(false);
104             }
105             catch (Exception ex)
106             {
107                 Assert.True(ex is NotSupportedException);
108             }
109         }
110     }
111 }
112