Skip to content

Commit c4af369

Browse files
committed
Merge branch 'Async' into 'master'
Add Async support to Runtime Closes #1 See merge request marta/kaitai_struct_csharp_runtime!3
2 parents 7e8b72a + 9812fe7 commit c4af369

38 files changed

+2465
-874
lines changed

Kaitai.Struct.Runtime.Async.Tests/Data/BitsData.cs

Lines changed: 166 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Kaitai.Struct.Runtime.Async.Tests
6+
{
7+
public class DecimalData
8+
{
9+
public static IEnumerable<object[]> Decimal4Data =>
10+
new List<(float expected, byte[] streamContent)>
11+
{
12+
(0f, BitConverter.GetBytes(0f)),
13+
(1f, BitConverter.GetBytes(1f)),
14+
(0.1f, BitConverter.GetBytes(0.1f)),
15+
(1.1f, BitConverter.GetBytes(1.1f)),
16+
(float.MinValue, BitConverter.GetBytes(float.MinValue)),
17+
(float.MaxValue, BitConverter.GetBytes(float.MaxValue))
18+
}.Select(t => new object[] {t.expected, t.streamContent});
19+
20+
public static IEnumerable<object[]> Decimal8Data =>
21+
new List<(double expected, byte[] streamContent)>
22+
{
23+
(0d, BitConverter.GetBytes(0d)),
24+
(1d, BitConverter.GetBytes(1d)),
25+
(0.1d, BitConverter.GetBytes(0.1d)),
26+
(1.1d, BitConverter.GetBytes(1.1d)),
27+
(double.MinValue, BitConverter.GetBytes(double.MinValue)),
28+
(double.MaxValue, BitConverter.GetBytes(double.MaxValue))
29+
}.Select(t => new object[] {t.expected, t.streamContent});
30+
}
31+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Kaitai.Struct.Runtime.Async.Tests
5+
{
6+
public class IntegralData
7+
{
8+
public static IEnumerable<object[]> Integral1Data =>
9+
new List<(sbyte expected, byte[] streamContent)>
10+
{
11+
(0x00, new byte[] {0x00}),
12+
(0x01, new byte[] {0x01}),
13+
(0x7F, new byte[] {0x7F}),
14+
(unchecked((sbyte) 0xFF), new byte[] {0xFF})
15+
}.Select(t => new object[] {t.expected, t.streamContent});
16+
17+
public static IEnumerable<object[]> Integral2Data =>
18+
new List<(short expected, byte[] streamContent)>
19+
{
20+
(0x00, new byte[] {0x00, 0x00}),
21+
(0x01, new byte[] {0x00, 0x01}),
22+
(0xFF, new byte[] {0x00, 0xFF}),
23+
(0x01_FF, new byte[] {0x01, 0xFF}),
24+
(0x7F_FF, new byte[] {0x7F, 0xFF}),
25+
(unchecked((short) 0xFF_FF), new byte[] {0xFF, 0xFF})
26+
}.Select(t => new object[] {t.expected, t.streamContent});
27+
28+
public static IEnumerable<object[]> Integral4Data =>
29+
new List<(int expected, byte[] streamContent)>
30+
{
31+
(0x00, new byte[] {0x00, 0x00, 0x00, 0x00}),
32+
(0x01, new byte[] {0x00, 0x00, 0x00, 0x01}),
33+
(0xFF, new byte[] {0x00, 0x00, 0x00, 0xFF}),
34+
(0x01_FF, new byte[] {0x00, 0x00, 0x01, 0xFF}),
35+
(0x7F_FF, new byte[] {0x00, 0x00, 0x7F, 0xFF}),
36+
(0xFF_FF, new byte[] {0x00, 0x00, 0xFF, 0xFF}),
37+
(0x01_FF_FF, new byte[] {0x00, 0x01, 0xFF, 0xFF}),
38+
(0x7F_FF_FF, new byte[] {0x00, 0x7F, 0xFF, 0xFF}),
39+
(0xFF_FF_FF, new byte[] {0x00, 0xFF, 0xFF, 0xFF}),
40+
(0x01_FF_FF_FF, new byte[] {0x01, 0xFF, 0xFF, 0xFF}),
41+
(0x7F_FF_FF_FF, new byte[] {0x7F, 0xFF, 0xFF, 0xFF}),
42+
(unchecked((int) 0xFF_FF_FF_FF), new byte[] {0xFF, 0xFF, 0xFF, 0xFF})
43+
}.Select(t => new object[] {t.expected, t.streamContent});
44+
45+
public static IEnumerable<object[]> Integral8Data =>
46+
new List<(long expected, byte[] streamContent)>
47+
{
48+
(0, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
49+
(1, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}),
50+
(0xFF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}),
51+
52+
(0x01_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF}),
53+
(0x7F_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF}),
54+
(0xFF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF}),
55+
56+
(0x01_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF}),
57+
(0x7F_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF}),
58+
(0xFF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF}),
59+
60+
(0x01_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF}),
61+
(0x7F_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF}),
62+
(0xFF_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}),
63+
64+
(0x01_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF}),
65+
(0x7F_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF}),
66+
(0xFF_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
67+
68+
(0x01_FF_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
69+
(0x7F_FF_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
70+
(0xFF_FF_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
71+
72+
(0x01_FF_FF_FF_FF_FF_FF, new byte[] {0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
73+
(0x7F_FF_FF_FF_FF_FF_FF, new byte[] {0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
74+
(0xFF_FF_FF_FF_FF_FF_FF, new byte[] {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
75+
76+
(0x01_FF_FF_FF_FF_FF_FF_FF, new byte[] {0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
77+
(0x7F_FF_FF_FF_FF_FF_FF_FF, new byte[] {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
78+
(unchecked((long) 0xFF_FF_FF_FF_FF_FF_FF_FF),
79+
new byte[] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
80+
}.Select(t => new object[] {t.expected, t.streamContent});
81+
}
82+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
11+
<PackageReference Include="xunit" Version="2.4.0" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
13+
<PackageReference Include="coverlet.collector" Version="1.0.1" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\Kaitai.Struct.Runtime.Async\Kaitai.Struct.Runtime.Async.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System.Threading.Tasks;
2+
using Kaitai.Async;
3+
using Xunit;
4+
5+
namespace Kaitai.Struct.Runtime.Async.Tests
6+
{
7+
public class KaitaiAsyncStreamBaseTests
8+
{
9+
[Fact]
10+
public async Task AlignToByte_Test()
11+
{
12+
//Arrange
13+
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[]{0b_1000_0000});
14+
15+
var read = await kaitaiStreamSUT.ReadBitsIntAsync(1);
16+
Assert.Equal(1u, read);
17+
18+
//Act
19+
kaitaiStreamSUT.AlignToByte();
20+
//Assert
21+
Assert.Equal(1, kaitaiStreamSUT.Pos);
22+
}
23+
24+
[Theory]
25+
[InlineData(true, 0, 0)]
26+
[InlineData(false, 1, 0)]
27+
[InlineData(false, 1, 1)]
28+
[InlineData(false, 1, 2)]
29+
[InlineData(false, 1, 3)]
30+
[InlineData(false, 1, 4)]
31+
[InlineData(false, 1, 5)]
32+
[InlineData(false, 1, 6)]
33+
[InlineData(false, 1, 7)]
34+
[InlineData(true, 1, 8)]
35+
public async Task Eof_Test(bool shouldBeEof, int streamSize, int readBitsAmount)
36+
{
37+
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[streamSize]);
38+
39+
await kaitaiStreamSUT.ReadBitsIntAsync(readBitsAmount);
40+
41+
if (shouldBeEof)
42+
Assert.True(kaitaiStreamSUT.IsEof);
43+
else
44+
Assert.False(kaitaiStreamSUT.IsEof);
45+
}
46+
47+
[Theory]
48+
[InlineData(0, 0)]
49+
[InlineData(1, 1)]
50+
public async Task Pos_ByRead_Test(int expectedPos, int readBitsAmount)
51+
{
52+
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[1]);
53+
54+
await kaitaiStreamSUT.ReadBytesAsync(readBitsAmount);
55+
56+
Assert.Equal(expectedPos, kaitaiStreamSUT.Pos);
57+
}
58+
59+
[Theory]
60+
[InlineData(0, 0)]
61+
[InlineData(1, 1)]
62+
public async Task Pos_BySeek_Test(int expectedPos, int position)
63+
{
64+
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[1]);
65+
66+
await kaitaiStreamSUT.SeekAsync(position);
67+
68+
Assert.Equal(expectedPos, kaitaiStreamSUT.Pos);
69+
}
70+
71+
[Theory]
72+
[InlineData(0)]
73+
[InlineData(1)]
74+
public void Size_Test(int streamSize)
75+
{
76+
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[streamSize]);
77+
78+
Assert.Equal(streamSize, kaitaiStreamSUT.Size);
79+
}
80+
81+
[Fact]
82+
public void EmptyStream_NoRead_NoSeek_IsEof_ShouldBe_True()
83+
{
84+
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[0]);
85+
86+
Assert.True(kaitaiStreamSUT.IsEof);
87+
}
88+
89+
[Fact]
90+
public void EmptyStream_NoRead_NoSeek_Pos_ShouldBe_0()
91+
{
92+
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[0]);
93+
94+
Assert.Equal(0, kaitaiStreamSUT.Pos);
95+
}
96+
}
97+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Kaitai.Async;
5+
using Xunit;
6+
7+
namespace Kaitai.Struct.Runtime.Async.Tests
8+
{
9+
10+
public class KaitaiAsyncStructTests
11+
{
12+
[Fact]
13+
public void M_Io_IsSet()
14+
{
15+
var kaitaiAsyncStream = new KaitaiAsyncStream(new byte[0]);
16+
var kaitaiAsyncStructSUT = new FooKaitaiAsyncStruct(kaitaiAsyncStream);
17+
18+
Assert.Equal(kaitaiAsyncStream, kaitaiAsyncStructSUT.M_Io);
19+
}
20+
21+
private class FooKaitaiAsyncStruct : KaitaiAsyncStruct
22+
{
23+
public FooKaitaiAsyncStruct(KaitaiAsyncStream kaitaiStream) : base(kaitaiStream)
24+
{
25+
}
26+
}
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Kaitai.Async;
6+
using Xunit;
7+
8+
namespace Kaitai.Struct.Runtime.Async.Tests
9+
{
10+
public class ReadBits
11+
{
12+
[Theory]
13+
[MemberData(nameof(BitsData.BitsBeData), MemberType = typeof(BitsData))]
14+
public async Task ReadBitsIntAsync_Test(ulong expected, byte[] streamContent, int bitsCount)
15+
{
16+
var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent);
17+
18+
Assert.Equal(expected, await kaitaiStreamSUT.ReadBitsIntAsync(bitsCount));
19+
}
20+
21+
[Theory]
22+
[MemberData(nameof(BitsData.BitsLeData), MemberType = typeof(BitsData))]
23+
public async Task ReadBitsIntLeAsync_Test(ulong expected, byte[] streamContent, int bitsCount)
24+
{
25+
var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent);
26+
27+
Assert.Equal(expected, await kaitaiStreamSUT.ReadBitsIntLeAsync(bitsCount));
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)