2026-03-21 21:45:14 -04:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Text.Extensions.Test
|
|
|
|
|
{
|
|
|
|
|
public class BinaryReaderExtensionsTests
|
|
|
|
|
{
|
2026-10-15 09:16:01 -04:00
|
|
|
#region ReadNullTerminatedString
|
|
|
|
|
|
2026-03-21 21:45:14 -04:00
|
|
|
[Fact]
|
|
|
|
|
public void ReadNullTerminatedAnsiStringTest()
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = [0x41, 0x42, 0x43, 0x00];
|
|
|
|
|
var stream = new MemoryStream(bytes);
|
|
|
|
|
var br = new BinaryReader(stream);
|
|
|
|
|
string? actual = br.ReadNullTerminatedString(Encoding.ASCII);
|
|
|
|
|
Assert.Equal("ABC", actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ReadNullTerminatedLatin1StringTest()
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = [0x41, 0x42, 0x43, 0x00];
|
|
|
|
|
var stream = new MemoryStream(bytes);
|
|
|
|
|
var br = new BinaryReader(stream);
|
|
|
|
|
string? actual = br.ReadNullTerminatedString(Encoding.Latin1);
|
|
|
|
|
Assert.Equal("ABC", actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ReadNullTerminatedUTF8StringTest()
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = [0x41, 0x42, 0x43, 0x00];
|
|
|
|
|
var stream = new MemoryStream(bytes);
|
|
|
|
|
var br = new BinaryReader(stream);
|
|
|
|
|
string? actual = br.ReadNullTerminatedString(Encoding.UTF8);
|
|
|
|
|
Assert.Equal("ABC", actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ReadNullTerminatedUnicodeStringTest()
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = [0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x00, 0x00];
|
|
|
|
|
var stream = new MemoryStream(bytes);
|
|
|
|
|
var br = new BinaryReader(stream);
|
|
|
|
|
string? actual = br.ReadNullTerminatedString(Encoding.Unicode);
|
|
|
|
|
Assert.Equal("ABC", actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ReadNullTerminatedBigEndianUnicodeStringTest()
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = [0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x00];
|
|
|
|
|
var stream = new MemoryStream(bytes);
|
|
|
|
|
var br = new BinaryReader(stream);
|
|
|
|
|
string? actual = br.ReadNullTerminatedString(Encoding.BigEndianUnicode);
|
|
|
|
|
Assert.Equal("ABC", actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ReadNullTerminatedUTF32StringTest()
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = [0x41, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
|
|
|
|
var stream = new MemoryStream(bytes);
|
|
|
|
|
var br = new BinaryReader(stream);
|
|
|
|
|
string? actual = br.ReadNullTerminatedString(Encoding.UTF32);
|
|
|
|
|
Assert.Equal("ABC", actual);
|
|
|
|
|
}
|
2026-10-15 09:16:01 -04:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ReadPrefixedString
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ReadPrefixedAnsiStringTest()
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = [0x03, 0x41, 0x42, 0x43];
|
|
|
|
|
var stream = new MemoryStream(bytes);
|
|
|
|
|
var br = new BinaryReader(stream);
|
|
|
|
|
string? actual = br.ReadPrefixedAnsiString();
|
|
|
|
|
Assert.Equal("ABC", actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ReadPrefixedLatin1StringTest()
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = [0x03, 0x41, 0x42, 0x43];
|
|
|
|
|
var stream = new MemoryStream(bytes);
|
|
|
|
|
var br = new BinaryReader(stream);
|
|
|
|
|
string? actual = br.ReadPrefixedLatin1String();
|
|
|
|
|
Assert.Equal("ABC", actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ReadPrefixedUnicodeStringTest()
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = [0x03, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00];
|
|
|
|
|
var stream = new MemoryStream(bytes);
|
|
|
|
|
var br = new BinaryReader(stream);
|
|
|
|
|
string? actual = br.ReadPrefixedUnicodeString();
|
|
|
|
|
Assert.Equal("ABC", actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ReadPrefixedBigEndianUnicodeStringTest()
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = [0x00, 0x03, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43];
|
|
|
|
|
var stream = new MemoryStream(bytes);
|
|
|
|
|
var br = new BinaryReader(stream);
|
|
|
|
|
string? actual = br.ReadPrefixedBigEndianUnicodeString();
|
|
|
|
|
Assert.Equal("ABC", actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2026-03-21 21:45:14 -04:00
|
|
|
}
|
|
|
|
|
}
|