1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Buffers ;
4
5
using System . Collections . Generic ;
5
6
using System . Linq ;
6
7
using System . Runtime . Intrinsics ;
7
8
using Xunit ;
8
9
9
10
namespace System . Text . Tests
10
11
{
11
- public abstract class AsciiEqualityTests
12
+ public abstract class AsciiEqualityTests < TLeft , TRight >
13
+ where TLeft : unmanaged
14
+ where TRight : unmanaged
12
15
{
13
16
protected abstract bool Equals ( string left , string right ) ;
14
17
protected abstract bool EqualsIgnoreCase ( string left , string right ) ;
15
18
protected abstract bool Equals ( byte [ ] left , byte [ ] right ) ;
16
19
protected abstract bool EqualsIgnoreCase ( byte [ ] left , byte [ ] right ) ;
20
+ protected abstract bool Equals ( ReadOnlySpan < TLeft > left , ReadOnlySpan < TRight > right ) ;
21
+ protected abstract bool EqualsIgnoreCase ( ReadOnlySpan < TLeft > left , ReadOnlySpan < TRight > right ) ;
17
22
18
23
public static IEnumerable < object [ ] > ValidAsciiInputs
19
24
{
@@ -140,9 +145,32 @@ public void Equals_EqualValues_ButNonAscii_ReturnsFalse(byte[] input)
140
145
[ MemberData ( nameof ( ContainingNonAsciiCharactersBuffers ) ) ]
141
146
public void EqualsIgnoreCase_EqualValues_ButNonAscii_ReturnsFalse ( byte [ ] input )
142
147
=> Assert . False ( EqualsIgnoreCase ( input , input ) ) ;
148
+
149
+ [ Theory ]
150
+ [ InlineData ( PoisonPagePlacement . After , PoisonPagePlacement . After ) ]
151
+ [ InlineData ( PoisonPagePlacement . After , PoisonPagePlacement . Before ) ]
152
+ [ InlineData ( PoisonPagePlacement . Before , PoisonPagePlacement . After ) ]
153
+ [ InlineData ( PoisonPagePlacement . Before , PoisonPagePlacement . Before ) ]
154
+ public void Boundaries_Are_Respected ( PoisonPagePlacement leftPoison , PoisonPagePlacement rightPoison )
155
+ {
156
+ for ( int size = 1 ; size < 129 ; size ++ )
157
+ {
158
+ using BoundedMemory < TLeft > left = BoundedMemory . Allocate < TLeft > ( size , leftPoison ) ;
159
+ using BoundedMemory < TRight > right = BoundedMemory . Allocate < TRight > ( size , rightPoison ) ;
160
+
161
+ left . Span . Fill ( default ) ;
162
+ right . Span . Fill ( default ) ;
163
+
164
+ left . MakeReadonly ( ) ;
165
+ right . MakeReadonly ( ) ;
166
+
167
+ Assert . True ( Equals ( left . Span , right . Span ) ) ;
168
+ Assert . True ( EqualsIgnoreCase ( left . Span , right . Span ) ) ;
169
+ }
170
+ }
143
171
}
144
172
145
- public class AsciiEqualityTests_Byte_Byte : AsciiEqualityTests
173
+ public class AsciiEqualityTests_Byte_Byte : AsciiEqualityTests < byte , byte >
146
174
{
147
175
protected override bool Equals ( string left , string right )
148
176
=> Ascii . Equals ( Encoding . ASCII . GetBytes ( left ) , Encoding . ASCII . GetBytes ( right ) ) ;
@@ -155,9 +183,15 @@ protected override bool Equals(byte[] left, byte[] right)
155
183
156
184
protected override bool EqualsIgnoreCase ( byte [ ] left , byte [ ] right )
157
185
=> Ascii . EqualsIgnoreCase ( left , right ) ;
186
+
187
+ protected override bool Equals ( ReadOnlySpan < byte > left , ReadOnlySpan < byte > right )
188
+ => Ascii . Equals ( left , right ) ;
189
+
190
+ protected override bool EqualsIgnoreCase ( ReadOnlySpan < byte > left , ReadOnlySpan < byte > right )
191
+ => Ascii . EqualsIgnoreCase ( left , right ) ;
158
192
}
159
193
160
- public class AsciiEqualityTests_Byte_Char : AsciiEqualityTests
194
+ public class AsciiEqualityTests_Byte_Char : AsciiEqualityTests < byte , char >
161
195
{
162
196
protected override bool Equals ( string left , string right )
163
197
=> Ascii . Equals ( Encoding . ASCII . GetBytes ( left ) , right ) ;
@@ -170,9 +204,15 @@ protected override bool Equals(byte[] left, byte[] right)
170
204
171
205
protected override bool EqualsIgnoreCase ( byte [ ] left , byte [ ] right )
172
206
=> Ascii . EqualsIgnoreCase ( left , right . Select ( b => ( char ) b ) . ToArray ( ) ) ;
207
+
208
+ protected override bool Equals ( ReadOnlySpan < byte > left , ReadOnlySpan < char > right )
209
+ => Ascii . Equals ( left , right ) ;
210
+
211
+ protected override bool EqualsIgnoreCase ( ReadOnlySpan < byte > left , ReadOnlySpan < char > right )
212
+ => Ascii . EqualsIgnoreCase ( left , right ) ;
173
213
}
174
214
175
- public class AsciiEqualityTests_Char_Byte : AsciiEqualityTests
215
+ public class AsciiEqualityTests_Char_Byte : AsciiEqualityTests < char , byte >
176
216
{
177
217
protected override bool Equals ( string left , string right )
178
218
=> Ascii . Equals ( left , Encoding . ASCII . GetBytes ( right ) ) ;
@@ -185,9 +225,15 @@ protected override bool Equals(byte[] left, byte[] right)
185
225
186
226
protected override bool EqualsIgnoreCase ( byte [ ] left , byte [ ] right )
187
227
=> Ascii . EqualsIgnoreCase ( left . Select ( b => ( char ) b ) . ToArray ( ) , right ) ;
228
+
229
+ protected override bool Equals ( ReadOnlySpan < char > left , ReadOnlySpan < byte > right )
230
+ => Ascii . Equals ( left , right ) ;
231
+
232
+ protected override bool EqualsIgnoreCase ( ReadOnlySpan < char > left , ReadOnlySpan < byte > right )
233
+ => Ascii . EqualsIgnoreCase ( left , right ) ;
188
234
}
189
235
190
- public class AsciiEqualityTests_Char_Char : AsciiEqualityTests
236
+ public class AsciiEqualityTests_Char_Char : AsciiEqualityTests < char , char >
191
237
{
192
238
protected override bool Equals ( string left , string right )
193
239
=> Ascii . Equals ( left , right ) ;
@@ -200,5 +246,11 @@ protected override bool Equals(byte[] left, byte[] right)
200
246
201
247
protected override bool EqualsIgnoreCase ( byte [ ] left , byte [ ] right )
202
248
=> Ascii . EqualsIgnoreCase ( left . Select ( b => ( char ) b ) . ToArray ( ) , right . Select ( b => ( char ) b ) . ToArray ( ) ) ;
249
+
250
+ protected override bool Equals ( ReadOnlySpan < char > left , ReadOnlySpan < char > right )
251
+ => Ascii . Equals ( left , right ) ;
252
+
253
+ protected override bool EqualsIgnoreCase ( ReadOnlySpan < char > left , ReadOnlySpan < char > right )
254
+ => Ascii . EqualsIgnoreCase ( left , right ) ;
203
255
}
204
256
}
0 commit comments