@@ -798,16 +798,55 @@ public void Invalid0xf50xffAvx2()
798
798
}
799
799
800
800
// Prints both hexadecimal and binary representations of a byte array
801
- static void PrintHexAndBinary ( byte [ ] bytes )
801
+ // static void PrintHexAndBinary(byte[] bytes)
802
+ // {
803
+ // // Convert to hexadecimal
804
+ // string hexRepresentation = BitConverter.ToString(bytes).Replace("-", " ");
805
+ // Console.WriteLine($"Hex: {hexRepresentation}");
806
+
807
+ // // Convert to binary
808
+ // string binaryRepresentation = string.Join(" ", Array.ConvertAll(bytes, byteValue => Convert.ToString(byteValue, 2).PadLeft(8, '0')));
809
+ // Console.WriteLine($"Binary: {binaryRepresentation}");
810
+ // }
811
+
812
+ static void PrintHexAndBinary ( byte [ ] bytes , int highlightIndex = - 1 )
813
+ {
814
+ // Convert to hexadecimal
815
+ Console . Write ( "Hex: " ) ;
816
+ for ( int i = 0 ; i < bytes . Length ; i ++ )
802
817
{
803
- // Convert to hexadecimal
804
- string hexRepresentation = BitConverter . ToString ( bytes ) . Replace ( "-" , " " ) ;
805
- Console . WriteLine ( $ "Hex: { hexRepresentation } ") ;
818
+ if ( i == highlightIndex )
819
+ {
820
+ Console . ForegroundColor = ConsoleColor . Red ;
821
+ Console . Write ( $ "{ bytes [ i ] : X2} ") ;
822
+ Console . ResetColor ( ) ;
823
+ }
824
+ else
825
+ {
826
+ Console . Write ( $ "{ bytes [ i ] : X2} ") ;
827
+ }
828
+ }
829
+ Console . WriteLine ( ) ; // New line for readability
806
830
807
- // Convert to binary
808
- string binaryRepresentation = string . Join ( " " , Array . ConvertAll ( bytes , byteValue => Convert . ToString ( byteValue , 2 ) . PadLeft ( 8 , '0' ) ) ) ;
809
- Console . WriteLine ( $ "Binary: { binaryRepresentation } ") ;
831
+ // Convert to binary
832
+ Console . Write ( "Binary: " ) ;
833
+ for ( int i = 0 ; i < bytes . Length ; i ++ )
834
+ {
835
+ string binaryString = Convert . ToString ( bytes [ i ] , 2 ) . PadLeft ( 8 , '0' ) ;
836
+ if ( i == highlightIndex )
837
+ {
838
+ Console . ForegroundColor = ConsoleColor . Red ;
839
+ Console . Write ( $ "{ binaryString } ") ;
840
+ Console . ResetColor ( ) ;
841
+ }
842
+ else
843
+ {
844
+ Console . Write ( $ "{ binaryString } ") ;
845
+ }
810
846
}
847
+ Console . WriteLine ( ) ; // New line for readability
848
+ }
849
+
811
850
812
851
813
852
public void TooLargeError ( Utf8ValidationDelegate utf8ValidationDelegate )
@@ -1255,43 +1294,85 @@ private bool ValidateUtf8(byte[] utf8,Utf8ValidationDelegate utf8ValidationDeleg
1255
1294
1256
1295
1257
1296
1258
- public void ValidateCount ( byte [ ] utf8 , Utf8ValidationDelegate utf8ValidationDelegate , Range range = default )
1259
- {
1260
- int DotnetUtf16Adjustment , DotnetScalarCountAdjustment ;
1261
- int SimdUnicodeUtf16Adjustment , SimdUnicodeScalarCountAdjustment ;
1297
+ // public void ValidateCount(byte[] utf8,Utf8ValidationDelegate utf8ValidationDelegate, Range range = default)
1298
+ // {
1299
+ // int DotnetUtf16Adjustment, DotnetScalarCountAdjustment;
1300
+ // int SimdUnicodeUtf16Adjustment, SimdUnicodeScalarCountAdjustment;
1301
+
1302
+ // var isDefaultRange = range.Equals(default(Range));
1303
+ // var (offset, length) = isDefaultRange ? (0, utf8.Length) : GetOffsetAndLength(utf8.Length, range);
1304
+
1305
+ // unsafe
1306
+ // {
1307
+ // fixed (byte* pInput = utf8)
1308
+ // {
1309
+ // byte* startPtr = pInput + offset;
1310
+ // // Invoke the method under test.
1311
+
1312
+ // DotnetUtf16Adjustment= 0;
1313
+ // DotnetScalarCountAdjustment= 0;
1314
+ // DotnetRuntime.Utf8Utility.GetPointerToFirstInvalidByte(pInput, length, out DotnetUtf16Adjustment, out DotnetScalarCountAdjustment);
1315
+
1316
+ // SimdUnicodeUtf16Adjustment= 0;
1317
+ // SimdUnicodeScalarCountAdjustment= 0;
1318
+ // utf8ValidationDelegate(pInput, length, out SimdUnicodeUtf16Adjustment, out SimdUnicodeScalarCountAdjustment);
1319
+
1320
+ // // Console.WriteLine("DotnetScalar:" + DotnetScalarCountAdjustment);
1321
+ // // Console.WriteLine("OurScalar:" + SimdUnicodeScalarCountAdjustment);
1322
+
1323
+ // // Console.WriteLine("Lenght:" + utf8.Length);
1324
+ // // Console.WriteLine("Dotnetutf16:" + DotnetUtf16Adjustment);
1325
+ // // Console.WriteLine("Ourutf16:" + SimdUnicodeUtf16Adjustment);
1326
+ // // Console.WriteLine("___________________________________________________");
1327
+
1328
+ // Assert.True(DotnetUtf16Adjustment == SimdUnicodeUtf16Adjustment, $"Expected UTF16 Adjustment: {DotnetUtf16Adjustment}, but got: {SimdUnicodeUtf16Adjustment}.");
1329
+ // Assert.True(DotnetScalarCountAdjustment == SimdUnicodeScalarCountAdjustment, $"Expected Scalar Count Adjustment: {DotnetScalarCountAdjustment}, but got: {SimdUnicodeScalarCountAdjustment}.");
1330
+ // }
1331
+ // }
1332
+ // // }
1333
+ // }
1262
1334
1263
- var isDefaultRange = range . Equals ( default ( Range ) ) ;
1264
- var ( offset , length ) = isDefaultRange ? ( 0 , utf8 . Length ) : GetOffsetAndLength ( utf8 . Length , range ) ;
1335
+ public void ValidateCount ( byte [ ] utf8 , Utf8ValidationDelegate utf8ValidationDelegate , Range range = default )
1336
+ {
1337
+ int DotnetUtf16Adjustment , DotnetScalarCountAdjustment ;
1338
+ int SimdUnicodeUtf16Adjustment , SimdUnicodeScalarCountAdjustment ;
1265
1339
1266
- unsafe
1267
- {
1268
- fixed ( byte * pInput = utf8 )
1269
- {
1270
- byte * startPtr = pInput + offset ;
1271
- // Invoke the method under test.
1340
+ var isDefaultRange = range . Equals ( default ( Range ) ) ;
1341
+ var ( offset , length ) = isDefaultRange ? ( 0 , utf8 . Length ) : GetOffsetAndLength ( utf8 . Length , range ) ;
1272
1342
1273
- DotnetUtf16Adjustment = 0 ;
1274
- DotnetScalarCountAdjustment = 0 ;
1275
- DotnetRuntime . Utf8Utility . GetPointerToFirstInvalidByte ( pInput , length , out DotnetUtf16Adjustment , out DotnetScalarCountAdjustment ) ;
1343
+ unsafe
1344
+ {
1345
+ fixed ( byte * pInput = utf8 )
1346
+ {
1347
+ byte * startPtr = pInput + offset ;
1276
1348
1277
- SimdUnicodeUtf16Adjustment = 0 ;
1278
- SimdUnicodeScalarCountAdjustment = 0 ;
1279
- utf8ValidationDelegate ( pInput , length , out SimdUnicodeUtf16Adjustment , out SimdUnicodeScalarCountAdjustment ) ;
1349
+ DotnetUtf16Adjustment = 0 ;
1350
+ DotnetScalarCountAdjustment = 0 ;
1351
+ DotnetRuntime . Utf8Utility . GetPointerToFirstInvalidByte ( pInput , length , out DotnetUtf16Adjustment , out DotnetScalarCountAdjustment ) ;
1280
1352
1281
- // Console.WriteLine("DotnetScalar:" + DotnetScalarCountAdjustment);
1282
- // Console.WriteLine("OurScalar:" + SimdUnicodeScalarCountAdjustment);
1353
+ SimdUnicodeUtf16Adjustment = 0 ;
1354
+ SimdUnicodeScalarCountAdjustment = 0 ;
1355
+ byte * simdResult = utf8ValidationDelegate ( pInput , length , out SimdUnicodeUtf16Adjustment , out SimdUnicodeScalarCountAdjustment ) ;
1283
1356
1284
- // Console.WriteLine("Lenght:" + utf8.Length);
1285
- // Console.WriteLine("Dotnetutf16:" + DotnetUtf16Adjustment);
1286
- // Console.WriteLine("Ourutf16:" + SimdUnicodeUtf16Adjustment);
1287
- // Console.WriteLine("___________________________________________________");
1357
+ // Determine the index of the invalid byte if simdResult doesn't point to the end.
1358
+ int failureIndex = simdResult != pInput + length ? ( int ) ( simdResult - pInput ) : - 1 ;
1288
1359
1289
- Assert . True ( DotnetUtf16Adjustment == SimdUnicodeUtf16Adjustment , $ "Expected UTF16 Adjustment: { DotnetUtf16Adjustment } , but got: { SimdUnicodeUtf16Adjustment } .") ;
1290
- Assert . True ( DotnetScalarCountAdjustment == SimdUnicodeScalarCountAdjustment , $ "Expected Scalar Count Adjustment: { DotnetScalarCountAdjustment } , but got: { SimdUnicodeScalarCountAdjustment } .") ;
1291
- }
1360
+ try
1361
+ {
1362
+ Assert . True ( DotnetUtf16Adjustment == SimdUnicodeUtf16Adjustment , $ "Expected UTF16 Adjustment: { DotnetUtf16Adjustment } , but got: { SimdUnicodeUtf16Adjustment } .") ;
1363
+ Assert . True ( DotnetScalarCountAdjustment == SimdUnicodeScalarCountAdjustment , $ "Expected Scalar Count Adjustment: { DotnetScalarCountAdjustment } , but got: { SimdUnicodeScalarCountAdjustment } .") ;
1292
1364
}
1293
- // }
1365
+ catch ( Exception )
1366
+ {
1367
+ // Upon failure, print the utf8 array for inspection
1368
+ Console . WriteLine ( "Assertion failed. Inspecting utf8 array:" ) ;
1369
+ PrintHexAndBinary ( utf8 , failureIndex ) ;
1370
+ throw ; // Re-throw the exception to preserve the failure state
1371
+ }
1372
+ }
1294
1373
}
1374
+ }
1375
+
1295
1376
1296
1377
[ Fact ]
1297
1378
[ Trait ( "Category" , "Scalar" ) ]
0 commit comments