-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for Kaitai.Struct.Async runtime.
- Loading branch information
Showing
13 changed files
with
843 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStructTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.