Skip to content

Commit

Permalink
Merge branch '5-position-should-remain-the-same-on-size-pos-iseof-cal…
Browse files Browse the repository at this point in the history
…ls' into 'master'

Resolve "Position should remain the same on Size, Pos, IsEof calls"

Closes #5

See merge request marta/kaitai_struct_csharp_runtime!11
  • Loading branch information
pluskal committed Jan 30, 2020
2 parents bd7655e + 885971b commit e048edf
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 170 deletions.
18 changes: 14 additions & 4 deletions Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using System.IO;
using System.IO.Pipelines;
using System.Threading.Tasks;
using Kaitai.Async;
using Xunit;

namespace Kaitai.Struct.Runtime.Async.Tests
{
public class StreamKaitaiAsyncStreamBaseTests : KaitaiAsyncStreamBaseTests
{
{
protected override KaitaiAsyncStream Create(byte[] data) => new KaitaiAsyncStream(data);
}

public class PipeReaderKaitaiAsyncStreamBaseTests : KaitaiAsyncStreamBaseTests
{
{
protected override KaitaiAsyncStream Create(byte[] data) =>
new KaitaiAsyncStream(System.IO.Pipelines.PipeReader.Create(new MemoryStream(data)));
new KaitaiAsyncStream(PipeReader.Create(new MemoryStream(data)));
}

public abstract class KaitaiAsyncStreamBaseTests
Expand All @@ -34,8 +35,8 @@ public abstract class KaitaiAsyncStreamBaseTests
public async Task Eof_Test(bool shouldBeEof, int streamSize, int readBitsAmount)
{
var kaitaiStreamSUT = Create(new byte[streamSize]);

await kaitaiStreamSUT.ReadBitsIntAsync(readBitsAmount);
long positionBeforeIsEof = kaitaiStreamSUT.Pos;

if (shouldBeEof)
{
Expand All @@ -45,6 +46,8 @@ public async Task Eof_Test(bool shouldBeEof, int streamSize, int readBitsAmount)
{
Assert.False(kaitaiStreamSUT.IsEof);
}

Assert.Equal(positionBeforeIsEof, kaitaiStreamSUT.Pos);
}

[Theory]
Expand Down Expand Up @@ -74,11 +77,18 @@ public async Task Pos_BySeek_Test(int expectedPos, int position)
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(100)]
[InlineData(1_000)]
[InlineData(10_000)]
[InlineData(100_000)]
[InlineData(1_000_000)]
public void Size_Test(int streamSize)
{
var kaitaiStreamSUT = Create(new byte[streamSize]);
long positionBeforeIsEof = kaitaiStreamSUT.Pos;

Assert.Equal(streamSize, kaitaiStreamSUT.Size);
Assert.Equal(positionBeforeIsEof, kaitaiStreamSUT.Pos);
}

[Fact]
Expand Down
329 changes: 171 additions & 158 deletions Kaitai.Struct.Runtime.Async/Interface/IKaitaiAsyncStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,161 +2,174 @@

namespace Kaitai.Async
{
public interface IKaitaiAsyncStream : IKaitaiStreamBase
{
/// <summary>
/// Seek to a specific position from the beginning of the stream
/// </summary>
/// <param name="position">The position to seek to</param>
Task SeekAsync(long position);

/// <summary>
/// Read a signed byte from the stream
/// </summary>
/// <returns></returns>
Task<sbyte> ReadS1Async();

/// <summary>
/// Read a signed short from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<short> ReadS2beAsync();

/// <summary>
/// Read a signed int from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<int> ReadS4beAsync();

/// <summary>
/// Read a signed long from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<long> ReadS8beAsync();

/// <summary>
/// Read a signed short from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<short> ReadS2leAsync();

/// <summary>
/// Read a signed int from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<int> ReadS4leAsync();

/// <summary>
/// Read a signed long from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<long> ReadS8leAsync();

/// <summary>
/// Read an unsigned byte from the stream
/// </summary>
/// <returns></returns>
Task<byte> ReadU1Async();

/// <summary>
/// Read an unsigned short from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<ushort> ReadU2beAsync();

/// <summary>
/// Read an unsigned int from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<uint> ReadU4beAsync();

/// <summary>
/// Read an unsigned long from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<ulong> ReadU8beAsync();

/// <summary>
/// Read an unsigned short from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<ushort> ReadU2leAsync();

/// <summary>
/// Read an unsigned int from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<uint> ReadU4leAsync();

/// <summary>
/// Read an unsigned long from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<ulong> ReadU8leAsync();

/// <summary>
/// Read a single-precision floating point value from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<float> ReadF4beAsync();

/// <summary>
/// Read a double-precision floating point value from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<double> ReadF8beAsync();

/// <summary>
/// Read a single-precision floating point value from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<float> ReadF4leAsync();

/// <summary>
/// Read a double-precision floating point value from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<double> ReadF8leAsync();

Task<ulong> ReadBitsIntAsync(int n);

Task<ulong> ReadBitsIntLeAsync(int n);

/// <summary>
/// Read a fixed number of bytes from the stream
/// </summary>
/// <param name="count">The number of bytes to read</param>
/// <returns></returns>
Task<byte[]> ReadBytesAsync(long count);

/// <summary>
/// Read a fixed number of bytes from the stream
/// </summary>
/// <param name="count">The number of bytes to read</param>
/// <returns></returns>
Task<byte[]> ReadBytesAsync(ulong count);

/// <summary>
/// Read all the remaining bytes from the stream until the end is reached
/// </summary>
/// <returns></returns>
Task<byte[]> ReadBytesFullAsync();

/// <summary>
/// Read a terminated string from the stream
/// </summary>
/// <param name="terminator">The string terminator value</param>
/// <param name="includeTerminator">True to include the terminator in the returned string</param>
/// <param name="consumeTerminator">True to consume the terminator byte before returning</param>
/// <param name="eosError">True to throw an error when the EOS was reached before the terminator</param>
/// <returns></returns>
Task<byte[]> ReadBytesTermAsync(byte terminator, bool includeTerminator, bool consumeTerminator, bool eosError);

/// <summary>
/// Read a specific set of bytes and assert that they are the same as an expected result
/// </summary>
/// <param name="expected">The expected result</param>
/// <returns></returns>
Task<byte[]> EnsureFixedContentsAsync(byte[] expected);
}
}
public interface IKaitaiAsyncStream : IKaitaiStreamBase
{
/// <summary>
/// Check if the stream position is at the end of the stream
/// </summary>
ValueTask<bool> IsEofAsync();

/// <summary>
/// Get the total length of the stream (ie. file size)
/// </summary>
ValueTask<long> GetSizeAsync();

/// <summary>
/// Seek to a specific position from the beginning of the stream
/// </summary>
/// <param name="position">The position to seek to</param>
Task SeekAsync(long position);

/// <summary>
/// Read a signed byte from the stream
/// </summary>
/// <returns></returns>
Task<sbyte> ReadS1Async();

/// <summary>
/// Read a signed short from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<short> ReadS2beAsync();

/// <summary>
/// Read a signed int from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<int> ReadS4beAsync();

/// <summary>
/// Read a signed long from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<long> ReadS8beAsync();

/// <summary>
/// Read a signed short from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<short> ReadS2leAsync();

/// <summary>
/// Read a signed int from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<int> ReadS4leAsync();

/// <summary>
/// Read a signed long from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<long> ReadS8leAsync();

/// <summary>
/// Read an unsigned byte from the stream
/// </summary>
/// <returns></returns>
Task<byte> ReadU1Async();

/// <summary>
/// Read an unsigned short from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<ushort> ReadU2beAsync();

/// <summary>
/// Read an unsigned int from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<uint> ReadU4beAsync();

/// <summary>
/// Read an unsigned long from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<ulong> ReadU8beAsync();

/// <summary>
/// Read an unsigned short from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<ushort> ReadU2leAsync();

/// <summary>
/// Read an unsigned int from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<uint> ReadU4leAsync();

/// <summary>
/// Read an unsigned long from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<ulong> ReadU8leAsync();

/// <summary>
/// Read a single-precision floating point value from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<float> ReadF4beAsync();

/// <summary>
/// Read a double-precision floating point value from the stream (big endian)
/// </summary>
/// <returns></returns>
Task<double> ReadF8beAsync();

/// <summary>
/// Read a single-precision floating point value from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<float> ReadF4leAsync();

/// <summary>
/// Read a double-precision floating point value from the stream (little endian)
/// </summary>
/// <returns></returns>
Task<double> ReadF8leAsync();

Task<ulong> ReadBitsIntAsync(int n);

Task<ulong> ReadBitsIntLeAsync(int n);

/// <summary>
/// Read a fixed number of bytes from the stream
/// </summary>
/// <param name="count">The number of bytes to read</param>
/// <returns></returns>
Task<byte[]> ReadBytesAsync(long count);

/// <summary>
/// Read a fixed number of bytes from the stream
/// </summary>
/// <param name="count">The number of bytes to read</param>
/// <returns></returns>
Task<byte[]> ReadBytesAsync(ulong count);

/// <summary>
/// Read all the remaining bytes from the stream until the end is reached
/// </summary>
/// <returns></returns>
Task<byte[]> ReadBytesFullAsync();

/// <summary>
/// Read a terminated string from the stream
/// </summary>
/// <param name="terminator">The string terminator value</param>
/// <param name="includeTerminator">True to include the terminator in the returned string</param>
/// <param name="consumeTerminator">True to consume the terminator byte before returning</param>
/// <param name="eosError">True to throw an error when the EOS was reached before the terminator</param>
/// <returns></returns>
Task<byte[]> ReadBytesTermAsync(byte terminator,
bool includeTerminator,
bool consumeTerminator,
bool eosError);

/// <summary>
/// Read a specific set of bytes and assert that they are the same as an expected result
/// </summary>
/// <param name="expected">The expected result</param>
/// <returns></returns>
Task<byte[]> EnsureFixedContentsAsync(byte[] expected);
}
}
Loading

0 comments on commit e048edf

Please sign in to comment.