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 System.Numerics;
6 using System.Text;
7 using Xunit;
8 
9 namespace System.SpanTests
10 {
11     public static partial class SpanTests
12     {
13         [Fact]
ZeroLengthIndexOf_Byte()14         public static void ZeroLengthIndexOf_Byte()
15         {
16             Span<byte> sp = new Span<byte>(Array.Empty<byte>());
17             int idx = sp.IndexOf<byte>(0);
18             Assert.Equal(-1, idx);
19         }
20 
21         [Fact]
DefaultFilledIndexOf_Byte()22         public static void DefaultFilledIndexOf_Byte()
23         {
24             for (int length = 0; length <= byte.MaxValue; length++)
25             {
26                 byte[] a = new byte[length];
27                 Span<byte> span = new Span<byte>(a);
28 
29                 for (int i = 0; i < length; i++)
30                 {
31                     byte target0 = default;
32                     int idx = span.IndexOf(target0);
33                     Assert.Equal(0, idx);
34                 }
35             }
36         }
37 
38         [Fact]
TestMatch_Byte()39         public static void TestMatch_Byte()
40         {
41             for (int length = 0; length <= byte.MaxValue; length++)
42             {
43                 byte[] a = new byte[length];
44                 for (int i = 0; i < length; i++)
45                 {
46                     a[i] = (byte)(i + 1);
47                 }
48                 Span<byte> span = new Span<byte>(a);
49 
50                 for (int targetIndex = 0; targetIndex < length; targetIndex++)
51                 {
52                     byte target = a[targetIndex];
53                     int idx = span.IndexOf(target);
54                     Assert.Equal(targetIndex, idx);
55                 }
56             }
57         }
58 
59         [Fact]
TestNoMatch_Byte()60         public static void TestNoMatch_Byte()
61         {
62             var rnd = new Random(42);
63             for (int length = 0; length <= byte.MaxValue; length++)
64             {
65                 byte[] a = new byte[length];
66                 byte target = (byte)rnd.Next(0, 256);
67                 for (int i = 0; i < length; i++)
68                 {
69                     byte val = (byte)(i + 1);
70                     a[i] = val == target ? (byte)(target + 1) : val;
71                 }
72                 Span<byte> span = new Span<byte>(a);
73 
74                 int idx = span.IndexOf(target);
75                 Assert.Equal(-1, idx);
76             }
77         }
78 
79         [Fact]
TestAllignmentNoMatch_Byte()80         public static void TestAllignmentNoMatch_Byte()
81         {
82             byte[] array = new byte[4 * Vector<byte>.Count];
83             for (var i = 0; i < Vector<byte>.Count; i++)
84             {
85                 var span = new Span<byte>(array, i, 3 * Vector<byte>.Count);
86                 int idx = span.IndexOf((byte)'1');
87                 Assert.Equal(-1, idx);
88 
89                 span = new Span<byte>(array, i, 3 * Vector<byte>.Count - 3);
90                 idx = span.IndexOf((byte)'1');
91                 Assert.Equal(-1, idx);
92             }
93         }
94 
95         [Fact]
TestAllignmentMatch_Byte()96         public static void TestAllignmentMatch_Byte()
97         {
98             byte[] array = new byte[4 * Vector<byte>.Count];
99             for (int i = 0; i < array.Length; i++)
100             {
101                 array[i] = 5;
102             }
103             for (var i = 0; i < Vector<byte>.Count; i++)
104             {
105                 var span = new Span<byte>(array, i, 3 * Vector<byte>.Count);
106                 int idx = span.IndexOf<byte>(5);
107                 Assert.Equal(0, idx);
108 
109                 span = new Span<byte>(array, i, 3 * Vector<byte>.Count - 3);
110                 idx = span.IndexOf<byte>(5);
111                 Assert.Equal(0, idx);
112             }
113         }
114 
115         [Fact]
TestMultipleMatch_Byte()116         public static void TestMultipleMatch_Byte()
117         {
118             for (int length = 2; length <= byte.MaxValue; length++)
119             {
120                 byte[] a = new byte[length];
121                 for (int i = 0; i < length; i++)
122                 {
123                     byte val = (byte)(i + 1);
124                     a[i] = val == 200 ? (byte)201 : val;
125                 }
126 
127                 a[length - 1] = 200;
128                 a[length - 2] = 200;
129 
130                 Span<byte> span = new Span<byte>(a);
131                 int idx = span.IndexOf<byte>(200);
132                 Assert.Equal(length - 2, idx);
133             }
134         }
135 
136         [Fact]
MakeSureNoChecksGoOutOfRange_Byte()137         public static void MakeSureNoChecksGoOutOfRange_Byte()
138         {
139             for (int length = 0; length <= byte.MaxValue; length++)
140             {
141                 byte[] a = new byte[length + 2];
142                 a[0] = 99;
143                 a[length + 1] = 99;
144                 Span<byte> span = new Span<byte>(a, 1, length);
145                 int index = span.IndexOf<byte>(99);
146                 Assert.Equal(-1, index);
147             }
148         }
149     }
150 }
151