Skip to content

Commit 244b741

Browse files
committed
Add both-endian peek read extensions
1 parent fb60f1f commit 244b741

File tree

6 files changed

+690
-0
lines changed

6 files changed

+690
-0
lines changed

SabreTools.IO.Test/Extensions/BinaryReaderExtensionsTests.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,17 @@ public void PeekByteTest()
10011001
Assert.Equal(0, br.BaseStream.Position);
10021002
}
10031003

1004+
[Fact]
1005+
public void PeekByteBothEndianTest()
1006+
{
1007+
var stream = new MemoryStream(_bytes);
1008+
var br = new BinaryReader(stream);
1009+
BothUInt8 read = br.PeekByteBothEndian();
1010+
Assert.Equal(0x00, read.LittleEndian);
1011+
Assert.Equal(0x01, read.BigEndian);
1012+
Assert.Equal(0, br.BaseStream.Position);
1013+
}
1014+
10041015
[Fact]
10051016
public void PeekBytesTest()
10061017
{
@@ -1022,6 +1033,17 @@ public void PeekSByteTest()
10221033
Assert.Equal(0x00, read);
10231034
}
10241035

1036+
[Fact]
1037+
public void PeekSByteBothEndianTest()
1038+
{
1039+
var stream = new MemoryStream(_bytes);
1040+
var br = new BinaryReader(stream);
1041+
BothInt8 read = br.PeekSByteBothEndian();
1042+
Assert.Equal(0x00, read.LittleEndian);
1043+
Assert.Equal(0x01, read.BigEndian);
1044+
Assert.Equal(0, br.BaseStream.Position);
1045+
}
1046+
10251047
[Fact]
10261048
public void PeekInt16Test()
10271049
{
@@ -1052,6 +1074,17 @@ public void PeekInt16LittleEndianTest()
10521074
Assert.Equal(0, br.BaseStream.Position);
10531075
}
10541076

1077+
[Fact]
1078+
public void PeekInt16BothEndianTest()
1079+
{
1080+
var stream = new MemoryStream(_bytes);
1081+
var br = new BinaryReader(stream);
1082+
BothInt16 read = br.PeekInt16BothEndian();
1083+
Assert.Equal(0x0100, read.LittleEndian);
1084+
Assert.Equal(0x0203, read.BigEndian);
1085+
Assert.Equal(0, br.BaseStream.Position);
1086+
}
1087+
10551088
[Fact]
10561089
public void PeekUInt16Test()
10571090
{
@@ -1082,6 +1115,17 @@ public void PeekUInt16LittleEndianTest()
10821115
Assert.Equal(0, br.BaseStream.Position);
10831116
}
10841117

1118+
[Fact]
1119+
public void PeekUInt16BothEndianTest()
1120+
{
1121+
var stream = new MemoryStream(_bytes);
1122+
var br = new BinaryReader(stream);
1123+
BothUInt16 read = br.PeekUInt16BothEndian();
1124+
Assert.Equal(0x0100, read.LittleEndian);
1125+
Assert.Equal(0x0203, read.BigEndian);
1126+
Assert.Equal(0, br.BaseStream.Position);
1127+
}
1128+
10851129
[Fact]
10861130
public void PeekWORDTest()
10871131
{
@@ -1112,6 +1156,17 @@ public void PeekWORDLittleEndianTest()
11121156
Assert.Equal(0, br.BaseStream.Position);
11131157
}
11141158

1159+
[Fact]
1160+
public void PeekWORDBothEndianTest()
1161+
{
1162+
var stream = new MemoryStream(_bytes);
1163+
var br = new BinaryReader(stream);
1164+
BothUInt16 read = br.PeekWORDBothEndian();
1165+
Assert.Equal(0x0100, read.LittleEndian);
1166+
Assert.Equal(0x0203, read.BigEndian);
1167+
Assert.Equal(0, br.BaseStream.Position);
1168+
}
1169+
11151170
[Fact]
11161171
public void PeekHalfTest()
11171172
{
@@ -1224,6 +1279,17 @@ public void PeekInt32LittleEndianTest()
12241279
Assert.Equal(0, br.BaseStream.Position);
12251280
}
12261281

1282+
[Fact]
1283+
public void PeekInt32BothEndianTest()
1284+
{
1285+
var stream = new MemoryStream(_bytes);
1286+
var br = new BinaryReader(stream);
1287+
BothInt32 read = br.PeekInt32BothEndian();
1288+
Assert.Equal(0x03020100, read.LittleEndian);
1289+
Assert.Equal(0x04050607, read.BigEndian);
1290+
Assert.Equal(0, br.BaseStream.Position);
1291+
}
1292+
12271293
[Fact]
12281294
public void PeekUInt32Test()
12291295
{
@@ -1254,6 +1320,17 @@ public void PeekUInt32LittleEndianTest()
12541320
Assert.Equal(0, br.BaseStream.Position);
12551321
}
12561322

1323+
[Fact]
1324+
public void PeekUInt32BothEndianTest()
1325+
{
1326+
var stream = new MemoryStream(_bytes);
1327+
var br = new BinaryReader(stream);
1328+
BothUInt32 read = br.PeekUInt32BothEndian();
1329+
Assert.Equal((uint)0x03020100, read.LittleEndian);
1330+
Assert.Equal((uint)0x04050607, read.BigEndian);
1331+
Assert.Equal(0, br.BaseStream.Position);
1332+
}
1333+
12571334
[Fact]
12581335
public void PeekDWORDTest()
12591336
{
@@ -1284,6 +1361,17 @@ public void PeekDWORDLittleEndianTest()
12841361
Assert.Equal(0, br.BaseStream.Position);
12851362
}
12861363

1364+
[Fact]
1365+
public void PeekDWORDBothEndianTest()
1366+
{
1367+
var stream = new MemoryStream(_bytes);
1368+
var br = new BinaryReader(stream);
1369+
BothUInt32 read = br.PeekDWORDBothEndian();
1370+
Assert.Equal((uint)0x03020100, read.LittleEndian);
1371+
Assert.Equal((uint)0x04050607, read.BigEndian);
1372+
Assert.Equal(0, br.BaseStream.Position);
1373+
}
1374+
12871375
[Fact]
12881376
public void PeekSingleTest()
12891377
{
@@ -1396,6 +1484,17 @@ public void PeekInt64LittleEndianTest()
13961484
Assert.Equal(0, br.BaseStream.Position);
13971485
}
13981486

1487+
[Fact]
1488+
public void PeekInt64BothEndianTest()
1489+
{
1490+
var stream = new MemoryStream(_bytes);
1491+
var br = new BinaryReader(stream);
1492+
BothInt64 read = br.PeekInt64BothEndian();
1493+
Assert.Equal(0x0706050403020100, read.LittleEndian);
1494+
Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian);
1495+
Assert.Equal(0, br.BaseStream.Position);
1496+
}
1497+
13991498
[Fact]
14001499
public void PeekUInt64Test()
14011500
{
@@ -1426,6 +1525,17 @@ public void PeekUInt64LittleEndianTest()
14261525
Assert.Equal(0, br.BaseStream.Position);
14271526
}
14281527

1528+
[Fact]
1529+
public void PeekUInt64BothEndianTest()
1530+
{
1531+
var stream = new MemoryStream(_bytes);
1532+
var br = new BinaryReader(stream);
1533+
BothUInt64 read = br.PeekUInt64BothEndian();
1534+
Assert.Equal((ulong)0x0706050403020100, read.LittleEndian);
1535+
Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian);
1536+
Assert.Equal(0, br.BaseStream.Position);
1537+
}
1538+
14291539
[Fact]
14301540
public void PeekQWORDTest()
14311541
{
@@ -1456,6 +1566,17 @@ public void PeekQWORDLittleEndianTest()
14561566
Assert.Equal(0, br.BaseStream.Position);
14571567
}
14581568

1569+
[Fact]
1570+
public void PeekQWORDBothEndianTest()
1571+
{
1572+
var stream = new MemoryStream(_bytes);
1573+
var br = new BinaryReader(stream);
1574+
BothUInt64 read = br.PeekQWORDBothEndian();
1575+
Assert.Equal((ulong)0x0706050403020100, read.LittleEndian);
1576+
Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian);
1577+
Assert.Equal(0, br.BaseStream.Position);
1578+
}
1579+
14591580
[Fact]
14601581
public void PeekDoubleTest()
14611582
{

SabreTools.IO.Test/Extensions/ByteArrayReaderExtensionsTests.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,16 @@ public void PeekByteValueTest()
897897
Assert.Equal(0, offset);
898898
}
899899

900+
[Fact]
901+
public void PeekByteBothEndianTest()
902+
{
903+
int offset = 0;
904+
BothUInt8 read = _bytes.PeekByteBothEndian(ref offset);
905+
Assert.Equal(0x00, read.LittleEndian);
906+
Assert.Equal(0x01, read.BigEndian);
907+
Assert.Equal(0, offset);
908+
}
909+
900910
[Fact]
901911
public void PeekBytesTest()
902912
{
@@ -916,6 +926,16 @@ public void PeekSByteTest()
916926
Assert.Equal(0, offset);
917927
}
918928

929+
[Fact]
930+
public void PeekSByteBothEndianTest()
931+
{
932+
int offset = 0;
933+
BothInt8 read = _bytes.PeekSByteBothEndian(ref offset);
934+
Assert.Equal(0x00, read.LittleEndian);
935+
Assert.Equal(0x01, read.BigEndian);
936+
Assert.Equal(0, offset);
937+
}
938+
919939
[Fact]
920940
public void PeekCharTest()
921941
{
@@ -952,6 +972,16 @@ public void PeekInt16LittleEndianTest()
952972
Assert.Equal(0, offset);
953973
}
954974

975+
[Fact]
976+
public void PeekInt16BothEndianTest()
977+
{
978+
int offset = 0;
979+
BothInt16 read = _bytes.PeekInt16BothEndian(ref offset);
980+
Assert.Equal(0x0100, read.LittleEndian);
981+
Assert.Equal(0x0203, read.BigEndian);
982+
Assert.Equal(0, offset);
983+
}
984+
955985
[Fact]
956986
public void PeekUInt16Test()
957987
{
@@ -979,6 +1009,16 @@ public void PeekUInt16LittleEndianTest()
9791009
Assert.Equal(0, offset);
9801010
}
9811011

1012+
[Fact]
1013+
public void PeekUInt16BothEndianTest()
1014+
{
1015+
int offset = 0;
1016+
BothUInt16 read = _bytes.PeekUInt16BothEndian(ref offset);
1017+
Assert.Equal(0x0100, read.LittleEndian);
1018+
Assert.Equal(0x0203, read.BigEndian);
1019+
Assert.Equal(0, offset);
1020+
}
1021+
9821022
[Fact]
9831023
public void PeekWORDTest()
9841024
{
@@ -1006,6 +1046,16 @@ public void PeekWORDLittleEndianTest()
10061046
Assert.Equal(0, offset);
10071047
}
10081048

1049+
[Fact]
1050+
public void PeekWORDBothEndianTest()
1051+
{
1052+
int offset = 0;
1053+
BothUInt16 read = _bytes.PeekWORDBothEndian(ref offset);
1054+
Assert.Equal(0x0100, read.LittleEndian);
1055+
Assert.Equal(0x0203, read.BigEndian);
1056+
Assert.Equal(0, offset);
1057+
}
1058+
10091059
[Fact]
10101060
public void PeekHalfTest()
10111061
{
@@ -1107,6 +1157,16 @@ public void PeekInt32LittleEndianTest()
11071157
Assert.Equal(0, offset);
11081158
}
11091159

1160+
[Fact]
1161+
public void PeekInt32BothEndianTest()
1162+
{
1163+
int offset = 0;
1164+
BothInt32 read = _bytes.PeekInt32BothEndian(ref offset);
1165+
Assert.Equal(0x03020100, read.LittleEndian);
1166+
Assert.Equal(0x04050607, read.BigEndian);
1167+
Assert.Equal(0, offset);
1168+
}
1169+
11101170
[Fact]
11111171
public void PeekUInt32Test()
11121172
{
@@ -1134,6 +1194,16 @@ public void PeekUInt32LittleEndianTest()
11341194
Assert.Equal(0, offset);
11351195
}
11361196

1197+
[Fact]
1198+
public void PeekUInt32BothEndianTest()
1199+
{
1200+
int offset = 0;
1201+
BothUInt32 read = _bytes.PeekUInt32BothEndian(ref offset);
1202+
Assert.Equal((uint)0x03020100, read.LittleEndian);
1203+
Assert.Equal((uint)0x04050607, read.BigEndian);
1204+
Assert.Equal(0, offset);
1205+
}
1206+
11371207
[Fact]
11381208
public void PeekDWORDTest()
11391209
{
@@ -1161,6 +1231,16 @@ public void PeekDWORDLittleEndianTest()
11611231
Assert.Equal(0, offset);
11621232
}
11631233

1234+
[Fact]
1235+
public void PeekDWORDBothEndianTest()
1236+
{
1237+
int offset = 0;
1238+
BothUInt32 read = _bytes.PeekDWORDBothEndian(ref offset);
1239+
Assert.Equal((uint)0x03020100, read.LittleEndian);
1240+
Assert.Equal((uint)0x04050607, read.BigEndian);
1241+
Assert.Equal(0, offset);
1242+
}
1243+
11641244
[Fact]
11651245
public void PeekSingleTest()
11661246
{
@@ -1262,6 +1342,16 @@ public void PeekInt64LittleEndianTest()
12621342
Assert.Equal(0, offset);
12631343
}
12641344

1345+
[Fact]
1346+
public void PeekInt64BothEndianTest()
1347+
{
1348+
int offset = 0;
1349+
BothInt64 read = _bytes.PeekInt64BothEndian(ref offset);
1350+
Assert.Equal(0x0706050403020100, read.LittleEndian);
1351+
Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian);
1352+
Assert.Equal(0, offset);
1353+
}
1354+
12651355
[Fact]
12661356
public void PeekUInt64Test()
12671357
{
@@ -1289,6 +1379,16 @@ public void PeekUInt64LittleEndianTest()
12891379
Assert.Equal(0, offset);
12901380
}
12911381

1382+
[Fact]
1383+
public void PeekUInt64BothEndianTest()
1384+
{
1385+
int offset = 0;
1386+
BothUInt64 read = _bytes.PeekUInt64BothEndian(ref offset);
1387+
Assert.Equal((ulong)0x0706050403020100, read.LittleEndian);
1388+
Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian);
1389+
Assert.Equal(0, offset);
1390+
}
1391+
12921392
[Fact]
12931393
public void PeekQWORDTest()
12941394
{
@@ -1316,6 +1416,16 @@ public void PeekQWORDLittleEndianTest()
13161416
Assert.Equal(0, offset);
13171417
}
13181418

1419+
[Fact]
1420+
public void PeekQWORDBothEndianTest()
1421+
{
1422+
int offset = 0;
1423+
BothUInt64 read = _bytes.PeekQWORDBothEndian(ref offset);
1424+
Assert.Equal((ulong)0x0706050403020100, read.LittleEndian);
1425+
Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian);
1426+
Assert.Equal(0, offset);
1427+
}
1428+
13191429
[Fact]
13201430
public void PeekDoubleTest()
13211431
{

0 commit comments

Comments
 (0)