Skip to content

Commit e048edf

Browse files
committed
Merge branch '5-position-should-remain-the-same-on-size-pos-iseof-calls' into 'master'
Resolve "Position should remain the same on Size, Pos, IsEof calls" Closes #5 See merge request marta/kaitai_struct_csharp_runtime!11
2 parents bd7655e + 885971b commit e048edf

File tree

6 files changed

+197
-170
lines changed

6 files changed

+197
-170
lines changed

Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
using System.IO;
2+
using System.IO.Pipelines;
23
using System.Threading.Tasks;
34
using Kaitai.Async;
45
using Xunit;
56

67
namespace Kaitai.Struct.Runtime.Async.Tests
78
{
89
public class StreamKaitaiAsyncStreamBaseTests : KaitaiAsyncStreamBaseTests
9-
{
10+
{
1011
protected override KaitaiAsyncStream Create(byte[] data) => new KaitaiAsyncStream(data);
1112
}
1213

1314
public class PipeReaderKaitaiAsyncStreamBaseTests : KaitaiAsyncStreamBaseTests
14-
{
15+
{
1516
protected override KaitaiAsyncStream Create(byte[] data) =>
16-
new KaitaiAsyncStream(System.IO.Pipelines.PipeReader.Create(new MemoryStream(data)));
17+
new KaitaiAsyncStream(PipeReader.Create(new MemoryStream(data)));
1718
}
1819

1920
public abstract class KaitaiAsyncStreamBaseTests
@@ -34,8 +35,8 @@ public abstract class KaitaiAsyncStreamBaseTests
3435
public async Task Eof_Test(bool shouldBeEof, int streamSize, int readBitsAmount)
3536
{
3637
var kaitaiStreamSUT = Create(new byte[streamSize]);
37-
3838
await kaitaiStreamSUT.ReadBitsIntAsync(readBitsAmount);
39+
long positionBeforeIsEof = kaitaiStreamSUT.Pos;
3940

4041
if (shouldBeEof)
4142
{
@@ -45,6 +46,8 @@ public async Task Eof_Test(bool shouldBeEof, int streamSize, int readBitsAmount)
4546
{
4647
Assert.False(kaitaiStreamSUT.IsEof);
4748
}
49+
50+
Assert.Equal(positionBeforeIsEof, kaitaiStreamSUT.Pos);
4851
}
4952

5053
[Theory]
@@ -74,11 +77,18 @@ public async Task Pos_BySeek_Test(int expectedPos, int position)
7477
[Theory]
7578
[InlineData(0)]
7679
[InlineData(1)]
80+
[InlineData(100)]
81+
[InlineData(1_000)]
82+
[InlineData(10_000)]
83+
[InlineData(100_000)]
84+
[InlineData(1_000_000)]
7785
public void Size_Test(int streamSize)
7886
{
7987
var kaitaiStreamSUT = Create(new byte[streamSize]);
88+
long positionBeforeIsEof = kaitaiStreamSUT.Pos;
8089

8190
Assert.Equal(streamSize, kaitaiStreamSUT.Size);
91+
Assert.Equal(positionBeforeIsEof, kaitaiStreamSUT.Pos);
8292
}
8393

8494
[Fact]

Kaitai.Struct.Runtime.Async/Interface/IKaitaiAsyncStream.cs

Lines changed: 171 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -2,161 +2,174 @@
22

33
namespace Kaitai.Async
44
{
5-
public interface IKaitaiAsyncStream : IKaitaiStreamBase
6-
{
7-
/// <summary>
8-
/// Seek to a specific position from the beginning of the stream
9-
/// </summary>
10-
/// <param name="position">The position to seek to</param>
11-
Task SeekAsync(long position);
12-
13-
/// <summary>
14-
/// Read a signed byte from the stream
15-
/// </summary>
16-
/// <returns></returns>
17-
Task<sbyte> ReadS1Async();
18-
19-
/// <summary>
20-
/// Read a signed short from the stream (big endian)
21-
/// </summary>
22-
/// <returns></returns>
23-
Task<short> ReadS2beAsync();
24-
25-
/// <summary>
26-
/// Read a signed int from the stream (big endian)
27-
/// </summary>
28-
/// <returns></returns>
29-
Task<int> ReadS4beAsync();
30-
31-
/// <summary>
32-
/// Read a signed long from the stream (big endian)
33-
/// </summary>
34-
/// <returns></returns>
35-
Task<long> ReadS8beAsync();
36-
37-
/// <summary>
38-
/// Read a signed short from the stream (little endian)
39-
/// </summary>
40-
/// <returns></returns>
41-
Task<short> ReadS2leAsync();
42-
43-
/// <summary>
44-
/// Read a signed int from the stream (little endian)
45-
/// </summary>
46-
/// <returns></returns>
47-
Task<int> ReadS4leAsync();
48-
49-
/// <summary>
50-
/// Read a signed long from the stream (little endian)
51-
/// </summary>
52-
/// <returns></returns>
53-
Task<long> ReadS8leAsync();
54-
55-
/// <summary>
56-
/// Read an unsigned byte from the stream
57-
/// </summary>
58-
/// <returns></returns>
59-
Task<byte> ReadU1Async();
60-
61-
/// <summary>
62-
/// Read an unsigned short from the stream (big endian)
63-
/// </summary>
64-
/// <returns></returns>
65-
Task<ushort> ReadU2beAsync();
66-
67-
/// <summary>
68-
/// Read an unsigned int from the stream (big endian)
69-
/// </summary>
70-
/// <returns></returns>
71-
Task<uint> ReadU4beAsync();
72-
73-
/// <summary>
74-
/// Read an unsigned long from the stream (big endian)
75-
/// </summary>
76-
/// <returns></returns>
77-
Task<ulong> ReadU8beAsync();
78-
79-
/// <summary>
80-
/// Read an unsigned short from the stream (little endian)
81-
/// </summary>
82-
/// <returns></returns>
83-
Task<ushort> ReadU2leAsync();
84-
85-
/// <summary>
86-
/// Read an unsigned int from the stream (little endian)
87-
/// </summary>
88-
/// <returns></returns>
89-
Task<uint> ReadU4leAsync();
90-
91-
/// <summary>
92-
/// Read an unsigned long from the stream (little endian)
93-
/// </summary>
94-
/// <returns></returns>
95-
Task<ulong> ReadU8leAsync();
96-
97-
/// <summary>
98-
/// Read a single-precision floating point value from the stream (big endian)
99-
/// </summary>
100-
/// <returns></returns>
101-
Task<float> ReadF4beAsync();
102-
103-
/// <summary>
104-
/// Read a double-precision floating point value from the stream (big endian)
105-
/// </summary>
106-
/// <returns></returns>
107-
Task<double> ReadF8beAsync();
108-
109-
/// <summary>
110-
/// Read a single-precision floating point value from the stream (little endian)
111-
/// </summary>
112-
/// <returns></returns>
113-
Task<float> ReadF4leAsync();
114-
115-
/// <summary>
116-
/// Read a double-precision floating point value from the stream (little endian)
117-
/// </summary>
118-
/// <returns></returns>
119-
Task<double> ReadF8leAsync();
120-
121-
Task<ulong> ReadBitsIntAsync(int n);
122-
123-
Task<ulong> ReadBitsIntLeAsync(int n);
124-
125-
/// <summary>
126-
/// Read a fixed number of bytes from the stream
127-
/// </summary>
128-
/// <param name="count">The number of bytes to read</param>
129-
/// <returns></returns>
130-
Task<byte[]> ReadBytesAsync(long count);
131-
132-
/// <summary>
133-
/// Read a fixed number of bytes from the stream
134-
/// </summary>
135-
/// <param name="count">The number of bytes to read</param>
136-
/// <returns></returns>
137-
Task<byte[]> ReadBytesAsync(ulong count);
138-
139-
/// <summary>
140-
/// Read all the remaining bytes from the stream until the end is reached
141-
/// </summary>
142-
/// <returns></returns>
143-
Task<byte[]> ReadBytesFullAsync();
144-
145-
/// <summary>
146-
/// Read a terminated string from the stream
147-
/// </summary>
148-
/// <param name="terminator">The string terminator value</param>
149-
/// <param name="includeTerminator">True to include the terminator in the returned string</param>
150-
/// <param name="consumeTerminator">True to consume the terminator byte before returning</param>
151-
/// <param name="eosError">True to throw an error when the EOS was reached before the terminator</param>
152-
/// <returns></returns>
153-
Task<byte[]> ReadBytesTermAsync(byte terminator, bool includeTerminator, bool consumeTerminator, bool eosError);
154-
155-
/// <summary>
156-
/// Read a specific set of bytes and assert that they are the same as an expected result
157-
/// </summary>
158-
/// <param name="expected">The expected result</param>
159-
/// <returns></returns>
160-
Task<byte[]> EnsureFixedContentsAsync(byte[] expected);
161-
}
162-
}
5+
public interface IKaitaiAsyncStream : IKaitaiStreamBase
6+
{
7+
/// <summary>
8+
/// Check if the stream position is at the end of the stream
9+
/// </summary>
10+
ValueTask<bool> IsEofAsync();
11+
12+
/// <summary>
13+
/// Get the total length of the stream (ie. file size)
14+
/// </summary>
15+
ValueTask<long> GetSizeAsync();
16+
17+
/// <summary>
18+
/// Seek to a specific position from the beginning of the stream
19+
/// </summary>
20+
/// <param name="position">The position to seek to</param>
21+
Task SeekAsync(long position);
22+
23+
/// <summary>
24+
/// Read a signed byte from the stream
25+
/// </summary>
26+
/// <returns></returns>
27+
Task<sbyte> ReadS1Async();
28+
29+
/// <summary>
30+
/// Read a signed short from the stream (big endian)
31+
/// </summary>
32+
/// <returns></returns>
33+
Task<short> ReadS2beAsync();
34+
35+
/// <summary>
36+
/// Read a signed int from the stream (big endian)
37+
/// </summary>
38+
/// <returns></returns>
39+
Task<int> ReadS4beAsync();
40+
41+
/// <summary>
42+
/// Read a signed long from the stream (big endian)
43+
/// </summary>
44+
/// <returns></returns>
45+
Task<long> ReadS8beAsync();
46+
47+
/// <summary>
48+
/// Read a signed short from the stream (little endian)
49+
/// </summary>
50+
/// <returns></returns>
51+
Task<short> ReadS2leAsync();
52+
53+
/// <summary>
54+
/// Read a signed int from the stream (little endian)
55+
/// </summary>
56+
/// <returns></returns>
57+
Task<int> ReadS4leAsync();
58+
59+
/// <summary>
60+
/// Read a signed long from the stream (little endian)
61+
/// </summary>
62+
/// <returns></returns>
63+
Task<long> ReadS8leAsync();
64+
65+
/// <summary>
66+
/// Read an unsigned byte from the stream
67+
/// </summary>
68+
/// <returns></returns>
69+
Task<byte> ReadU1Async();
70+
71+
/// <summary>
72+
/// Read an unsigned short from the stream (big endian)
73+
/// </summary>
74+
/// <returns></returns>
75+
Task<ushort> ReadU2beAsync();
76+
77+
/// <summary>
78+
/// Read an unsigned int from the stream (big endian)
79+
/// </summary>
80+
/// <returns></returns>
81+
Task<uint> ReadU4beAsync();
82+
83+
/// <summary>
84+
/// Read an unsigned long from the stream (big endian)
85+
/// </summary>
86+
/// <returns></returns>
87+
Task<ulong> ReadU8beAsync();
88+
89+
/// <summary>
90+
/// Read an unsigned short from the stream (little endian)
91+
/// </summary>
92+
/// <returns></returns>
93+
Task<ushort> ReadU2leAsync();
94+
95+
/// <summary>
96+
/// Read an unsigned int from the stream (little endian)
97+
/// </summary>
98+
/// <returns></returns>
99+
Task<uint> ReadU4leAsync();
100+
101+
/// <summary>
102+
/// Read an unsigned long from the stream (little endian)
103+
/// </summary>
104+
/// <returns></returns>
105+
Task<ulong> ReadU8leAsync();
106+
107+
/// <summary>
108+
/// Read a single-precision floating point value from the stream (big endian)
109+
/// </summary>
110+
/// <returns></returns>
111+
Task<float> ReadF4beAsync();
112+
113+
/// <summary>
114+
/// Read a double-precision floating point value from the stream (big endian)
115+
/// </summary>
116+
/// <returns></returns>
117+
Task<double> ReadF8beAsync();
118+
119+
/// <summary>
120+
/// Read a single-precision floating point value from the stream (little endian)
121+
/// </summary>
122+
/// <returns></returns>
123+
Task<float> ReadF4leAsync();
124+
125+
/// <summary>
126+
/// Read a double-precision floating point value from the stream (little endian)
127+
/// </summary>
128+
/// <returns></returns>
129+
Task<double> ReadF8leAsync();
130+
131+
Task<ulong> ReadBitsIntAsync(int n);
132+
133+
Task<ulong> ReadBitsIntLeAsync(int n);
134+
135+
/// <summary>
136+
/// Read a fixed number of bytes from the stream
137+
/// </summary>
138+
/// <param name="count">The number of bytes to read</param>
139+
/// <returns></returns>
140+
Task<byte[]> ReadBytesAsync(long count);
141+
142+
/// <summary>
143+
/// Read a fixed number of bytes from the stream
144+
/// </summary>
145+
/// <param name="count">The number of bytes to read</param>
146+
/// <returns></returns>
147+
Task<byte[]> ReadBytesAsync(ulong count);
148+
149+
/// <summary>
150+
/// Read all the remaining bytes from the stream until the end is reached
151+
/// </summary>
152+
/// <returns></returns>
153+
Task<byte[]> ReadBytesFullAsync();
154+
155+
/// <summary>
156+
/// Read a terminated string from the stream
157+
/// </summary>
158+
/// <param name="terminator">The string terminator value</param>
159+
/// <param name="includeTerminator">True to include the terminator in the returned string</param>
160+
/// <param name="consumeTerminator">True to consume the terminator byte before returning</param>
161+
/// <param name="eosError">True to throw an error when the EOS was reached before the terminator</param>
162+
/// <returns></returns>
163+
Task<byte[]> ReadBytesTermAsync(byte terminator,
164+
bool includeTerminator,
165+
bool consumeTerminator,
166+
bool eosError);
167+
168+
/// <summary>
169+
/// Read a specific set of bytes and assert that they are the same as an expected result
170+
/// </summary>
171+
/// <param name="expected">The expected result</param>
172+
/// <returns></returns>
173+
Task<byte[]> EnsureFixedContentsAsync(byte[] expected);
174+
}
175+
}

0 commit comments

Comments
 (0)