11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System . Buffers ;
45using System . Collections . Generic ;
56using System . Linq ;
67using System . Runtime . Intrinsics ;
78using Xunit ;
89
910namespace System . Text . Tests
1011{
11- public abstract class AsciiEqualityTests
12+ public abstract class AsciiEqualityTests < TLeft , TRight >
13+ where TLeft : unmanaged
14+ where TRight : unmanaged
1215 {
1316 protected abstract bool Equals ( string left , string right ) ;
1417 protected abstract bool EqualsIgnoreCase ( string left , string right ) ;
1518 protected abstract bool Equals ( byte [ ] left , byte [ ] right ) ;
1619 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 ) ;
1722
1823 public static IEnumerable < object [ ] > ValidAsciiInputs
1924 {
@@ -140,9 +145,32 @@ public void Equals_EqualValues_ButNonAscii_ReturnsFalse(byte[] input)
140145 [ MemberData ( nameof ( ContainingNonAsciiCharactersBuffers ) ) ]
141146 public void EqualsIgnoreCase_EqualValues_ButNonAscii_ReturnsFalse ( byte [ ] input )
142147 => 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+ }
143171 }
144172
145- public class AsciiEqualityTests_Byte_Byte : AsciiEqualityTests
173+ public class AsciiEqualityTests_Byte_Byte : AsciiEqualityTests < byte , byte >
146174 {
147175 protected override bool Equals ( string left , string right )
148176 => Ascii . Equals ( Encoding . ASCII . GetBytes ( left ) , Encoding . ASCII . GetBytes ( right ) ) ;
@@ -155,9 +183,15 @@ protected override bool Equals(byte[] left, byte[] right)
155183
156184 protected override bool EqualsIgnoreCase ( byte [ ] left , byte [ ] right )
157185 => 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 ) ;
158192 }
159193
160- public class AsciiEqualityTests_Byte_Char : AsciiEqualityTests
194+ public class AsciiEqualityTests_Byte_Char : AsciiEqualityTests < byte , char >
161195 {
162196 protected override bool Equals ( string left , string right )
163197 => Ascii . Equals ( Encoding . ASCII . GetBytes ( left ) , right ) ;
@@ -170,9 +204,15 @@ protected override bool Equals(byte[] left, byte[] right)
170204
171205 protected override bool EqualsIgnoreCase ( byte [ ] left , byte [ ] right )
172206 => 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 ) ;
173213 }
174214
175- public class AsciiEqualityTests_Char_Byte : AsciiEqualityTests
215+ public class AsciiEqualityTests_Char_Byte : AsciiEqualityTests < char , byte >
176216 {
177217 protected override bool Equals ( string left , string right )
178218 => Ascii . Equals ( left , Encoding . ASCII . GetBytes ( right ) ) ;
@@ -185,9 +225,15 @@ protected override bool Equals(byte[] left, byte[] right)
185225
186226 protected override bool EqualsIgnoreCase ( byte [ ] left , byte [ ] right )
187227 => 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 ) ;
188234 }
189235
190- public class AsciiEqualityTests_Char_Char : AsciiEqualityTests
236+ public class AsciiEqualityTests_Char_Char : AsciiEqualityTests < char , char >
191237 {
192238 protected override bool Equals ( string left , string right )
193239 => Ascii . Equals ( left , right ) ;
@@ -200,5 +246,11 @@ protected override bool Equals(byte[] left, byte[] right)
200246
201247 protected override bool EqualsIgnoreCase ( byte [ ] left , byte [ ] right )
202248 => 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 ) ;
203255 }
204256}
0 commit comments