Skip to content

Commit

Permalink
Add test for Kaitai.Struct.Async runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
pluskal committed Jan 8, 2020
1 parent a91e516 commit 9812fe7
Show file tree
Hide file tree
Showing 13 changed files with 843 additions and 7 deletions.
166 changes: 166 additions & 0 deletions Kaitai.Struct.Runtime.Async.Tests/Data/BitsData.cs

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions Kaitai.Struct.Runtime.Async.Tests/Data/DecimalData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Kaitai.Struct.Runtime.Async.Tests
{
public class DecimalData
{
public static IEnumerable<object[]> Decimal4Data =>
new List<(float expected, byte[] streamContent)>
{
(0f, BitConverter.GetBytes(0f)),
(1f, BitConverter.GetBytes(1f)),
(0.1f, BitConverter.GetBytes(0.1f)),
(1.1f, BitConverter.GetBytes(1.1f)),
(float.MinValue, BitConverter.GetBytes(float.MinValue)),
(float.MaxValue, BitConverter.GetBytes(float.MaxValue))
}.Select(t => new object[] {t.expected, t.streamContent});

public static IEnumerable<object[]> Decimal8Data =>
new List<(double expected, byte[] streamContent)>
{
(0d, BitConverter.GetBytes(0d)),
(1d, BitConverter.GetBytes(1d)),
(0.1d, BitConverter.GetBytes(0.1d)),
(1.1d, BitConverter.GetBytes(1.1d)),
(double.MinValue, BitConverter.GetBytes(double.MinValue)),
(double.MaxValue, BitConverter.GetBytes(double.MaxValue))
}.Select(t => new object[] {t.expected, t.streamContent});
}
}
82 changes: 82 additions & 0 deletions Kaitai.Struct.Runtime.Async.Tests/Data/IntegralData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.Linq;

namespace Kaitai.Struct.Runtime.Async.Tests
{
public class IntegralData
{
public static IEnumerable<object[]> Integral1Data =>
new List<(sbyte expected, byte[] streamContent)>
{
(0x00, new byte[] {0x00}),
(0x01, new byte[] {0x01}),
(0x7F, new byte[] {0x7F}),
(unchecked((sbyte) 0xFF), new byte[] {0xFF})
}.Select(t => new object[] {t.expected, t.streamContent});

public static IEnumerable<object[]> Integral2Data =>
new List<(short expected, byte[] streamContent)>
{
(0x00, new byte[] {0x00, 0x00}),
(0x01, new byte[] {0x00, 0x01}),
(0xFF, new byte[] {0x00, 0xFF}),
(0x01_FF, new byte[] {0x01, 0xFF}),
(0x7F_FF, new byte[] {0x7F, 0xFF}),
(unchecked((short) 0xFF_FF), new byte[] {0xFF, 0xFF})
}.Select(t => new object[] {t.expected, t.streamContent});

public static IEnumerable<object[]> Integral4Data =>
new List<(int expected, byte[] streamContent)>
{
(0x00, new byte[] {0x00, 0x00, 0x00, 0x00}),
(0x01, new byte[] {0x00, 0x00, 0x00, 0x01}),
(0xFF, new byte[] {0x00, 0x00, 0x00, 0xFF}),
(0x01_FF, new byte[] {0x00, 0x00, 0x01, 0xFF}),
(0x7F_FF, new byte[] {0x00, 0x00, 0x7F, 0xFF}),
(0xFF_FF, new byte[] {0x00, 0x00, 0xFF, 0xFF}),
(0x01_FF_FF, new byte[] {0x00, 0x01, 0xFF, 0xFF}),
(0x7F_FF_FF, new byte[] {0x00, 0x7F, 0xFF, 0xFF}),
(0xFF_FF_FF, new byte[] {0x00, 0xFF, 0xFF, 0xFF}),
(0x01_FF_FF_FF, new byte[] {0x01, 0xFF, 0xFF, 0xFF}),
(0x7F_FF_FF_FF, new byte[] {0x7F, 0xFF, 0xFF, 0xFF}),
(unchecked((int) 0xFF_FF_FF_FF), new byte[] {0xFF, 0xFF, 0xFF, 0xFF})
}.Select(t => new object[] {t.expected, t.streamContent});

public static IEnumerable<object[]> Integral8Data =>
new List<(long expected, byte[] streamContent)>
{
(0, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
(1, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}),
(0xFF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}),

(0x01_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF}),
(0x7F_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF}),
(0xFF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF}),

(0x01_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF}),
(0x7F_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF}),
(0xFF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF}),

(0x01_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF}),
(0x7F_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF}),
(0xFF_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}),

(0x01_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF}),
(0x7F_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF}),
(0xFF_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),

(0x01_FF_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
(0x7F_FF_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
(0xFF_FF_FF_FF_FF_FF, new byte[] {0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),

(0x01_FF_FF_FF_FF_FF_FF, new byte[] {0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
(0x7F_FF_FF_FF_FF_FF_FF, new byte[] {0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
(0xFF_FF_FF_FF_FF_FF_FF, new byte[] {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),

(0x01_FF_FF_FF_FF_FF_FF_FF, new byte[] {0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
(0x7F_FF_FF_FF_FF_FF_FF_FF, new byte[] {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
(unchecked((long) 0xFF_FF_FF_FF_FF_FF_FF_FF),
new byte[] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
}.Select(t => new object[] {t.expected, t.streamContent});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kaitai.Struct.Runtime.Async\Kaitai.Struct.Runtime.Async.csproj" />
</ItemGroup>

</Project>
97 changes: 97 additions & 0 deletions Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Threading.Tasks;
using Kaitai.Async;
using Xunit;

namespace Kaitai.Struct.Runtime.Async.Tests
{
public class KaitaiAsyncStreamBaseTests
{
[Fact]
public async Task AlignToByte_Test()
{
//Arrange
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[]{0b_1000_0000});

var read = await kaitaiStreamSUT.ReadBitsIntAsync(1);
Assert.Equal(1u, read);

//Act
kaitaiStreamSUT.AlignToByte();
//Assert
Assert.Equal(1, kaitaiStreamSUT.Pos);
}

[Theory]
[InlineData(true, 0, 0)]
[InlineData(false, 1, 0)]
[InlineData(false, 1, 1)]
[InlineData(false, 1, 2)]
[InlineData(false, 1, 3)]
[InlineData(false, 1, 4)]
[InlineData(false, 1, 5)]
[InlineData(false, 1, 6)]
[InlineData(false, 1, 7)]
[InlineData(true, 1, 8)]
public async Task Eof_Test(bool shouldBeEof, int streamSize, int readBitsAmount)
{
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[streamSize]);

await kaitaiStreamSUT.ReadBitsIntAsync(readBitsAmount);

if (shouldBeEof)
Assert.True(kaitaiStreamSUT.IsEof);
else
Assert.False(kaitaiStreamSUT.IsEof);
}

[Theory]
[InlineData(0, 0)]
[InlineData(1, 1)]
public async Task Pos_ByRead_Test(int expectedPos, int readBitsAmount)
{
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[1]);

await kaitaiStreamSUT.ReadBytesAsync(readBitsAmount);

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

[Theory]
[InlineData(0, 0)]
[InlineData(1, 1)]
public async Task Pos_BySeek_Test(int expectedPos, int position)
{
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[1]);

await kaitaiStreamSUT.SeekAsync(position);

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

[Theory]
[InlineData(0)]
[InlineData(1)]
public void Size_Test(int streamSize)
{
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[streamSize]);

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

[Fact]
public void EmptyStream_NoRead_NoSeek_IsEof_ShouldBe_True()
{
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[0]);

Assert.True(kaitaiStreamSUT.IsEof);
}

[Fact]
public void EmptyStream_NoRead_NoSeek_Pos_ShouldBe_0()
{
var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[0]);

Assert.Equal(0, kaitaiStreamSUT.Pos);
}
}
}
28 changes: 28 additions & 0 deletions Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStructTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
using Kaitai.Async;
using Xunit;

namespace Kaitai.Struct.Runtime.Async.Tests
{

public class KaitaiAsyncStructTests
{
[Fact]
public void M_Io_IsSet()
{
var kaitaiAsyncStream = new KaitaiAsyncStream(new byte[0]);
var kaitaiAsyncStructSUT = new FooKaitaiAsyncStruct(kaitaiAsyncStream);

Assert.Equal(kaitaiAsyncStream, kaitaiAsyncStructSUT.M_Io);
}

private class FooKaitaiAsyncStruct : KaitaiAsyncStruct
{
public FooKaitaiAsyncStruct(KaitaiAsyncStream kaitaiStream) : base(kaitaiStream)
{
}
}
}
}
30 changes: 30 additions & 0 deletions Kaitai.Struct.Runtime.Async.Tests/ReadBits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kaitai.Async;
using Xunit;

namespace Kaitai.Struct.Runtime.Async.Tests
{
public class ReadBits
{
[Theory]
[MemberData(nameof(BitsData.BitsBeData), MemberType = typeof(BitsData))]
public async Task ReadBitsIntAsync_Test(ulong expected, byte[] streamContent, int bitsCount)
{
var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent);

Assert.Equal(expected, await kaitaiStreamSUT.ReadBitsIntAsync(bitsCount));
}

[Theory]
[MemberData(nameof(BitsData.BitsLeData), MemberType = typeof(BitsData))]
public async Task ReadBitsIntLeAsync_Test(ulong expected, byte[] streamContent, int bitsCount)
{
var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent);

Assert.Equal(expected, await kaitaiStreamSUT.ReadBitsIntLeAsync(bitsCount));
}
}
}
Loading

0 comments on commit 9812fe7

Please sign in to comment.