From 9812fe778ea420f87baf2e6eb792ca451a5ab535 Mon Sep 17 00:00:00 2001 From: Jan Pluskal Date: Wed, 8 Jan 2020 18:24:43 +0100 Subject: [PATCH] Add test for Kaitai.Struct.Async runtime. --- .../Data/BitsData.cs | 166 ++++++++++++++ .../Data/DecimalData.cs | 31 +++ .../Data/IntegralData.cs | 82 +++++++ .../Kaitai.Struct.Runtime.Async.Tests.csproj | 4 + .../KaitaiAsyncStreamBaseTests.cs | 97 ++++++++ .../KaitaiAsyncStructTests.cs | 28 +++ Kaitai.Struct.Runtime.Async.Tests/ReadBits.cs | 30 +++ .../ReadBytesAsync.cs | 208 ++++++++++++++++++ .../ReadDecimal.cs | 46 ++++ .../ReadSigned.cs | 73 ++++++ .../ReadUnSigned.cs | 73 ++++++ .../KaitaiAsyncStream.cs | 11 +- Kaitai.Struct.sln.DotSettings | 1 + 13 files changed, 843 insertions(+), 7 deletions(-) create mode 100644 Kaitai.Struct.Runtime.Async.Tests/Data/BitsData.cs create mode 100644 Kaitai.Struct.Runtime.Async.Tests/Data/DecimalData.cs create mode 100644 Kaitai.Struct.Runtime.Async.Tests/Data/IntegralData.cs create mode 100644 Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs create mode 100644 Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStructTests.cs create mode 100644 Kaitai.Struct.Runtime.Async.Tests/ReadBits.cs create mode 100644 Kaitai.Struct.Runtime.Async.Tests/ReadBytesAsync.cs create mode 100644 Kaitai.Struct.Runtime.Async.Tests/ReadDecimal.cs create mode 100644 Kaitai.Struct.Runtime.Async.Tests/ReadSigned.cs create mode 100644 Kaitai.Struct.Runtime.Async.Tests/ReadUnSigned.cs diff --git a/Kaitai.Struct.Runtime.Async.Tests/Data/BitsData.cs b/Kaitai.Struct.Runtime.Async.Tests/Data/BitsData.cs new file mode 100644 index 0000000..2f26fde --- /dev/null +++ b/Kaitai.Struct.Runtime.Async.Tests/Data/BitsData.cs @@ -0,0 +1,166 @@ +using System.Collections.Generic; +using System.Linq; + +namespace Kaitai.Struct.Runtime.Async.Tests +{ + public class BitsData + { + public static IEnumerable BitsBeData => + new List<(ulong expected, byte[] streamContent, int bitsCount)> + { + (0b_0000_0000, new byte[] {0x00}, 0), + (0b_0000_0000, new byte[] {0x00}, 1), + (0b_0000_0001, new byte[] {0b_1000_0000}, 1), + (0b_0000_0001, new byte[] {0b_1100_0000}, 1), + (0b_0000_0011, new byte[] {0b_1100_0000}, 2), + (0b_0000_0111, new byte[] {0b_1110_0000}, 3), + (0b_0000_1111, new byte[] {0b_1111_0000}, 4), + (0b_0001_1111, new byte[] {0b_1111_1000}, 5), + (0b_0011_1111, new byte[] {0b_1111_1100}, 6), + (0b_0111_1111, new byte[] {0b_1111_1110}, 7), + (0b_1111_1111, new byte[] {0b_1111_1111}, 8), + + (0b_0000_0001_1111_1111, new byte[] {0b_1111_1111, 0b_1000_0000}, 9), + (0b_0000_0011_1111_1111, new byte[] {0b_1111_1111, 0b_1100_0000}, 10), + (0b_0000_0111_1111_1111, new byte[] {0b_1111_1111, 0b_1110_0000}, 11), + (0b_0000_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_0000}, 12), + (0b_0001_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1000}, 13), + (0b_0011_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1100}, 14), + (0b_0111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1110}, 15), + (0b_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111}, 16), + + (0b_0000_0001_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1000_0000}, 17), + (0b_0000_0011_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1100_0000}, 18), + (0b_0000_0111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1110_0000}, 19), + (0b_0000_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_0000}, 20), + (0b_0001_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1000}, 21), + (0b_0011_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1100}, 22), + (0b_0111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1110}, 23), + (0b_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 24), + + (0b_0000_0001_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1000_0000}, 25), + (0b_0000_0011_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1100_0000}, 26), + (0b_0000_0111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1110_0000}, 27), + (0b_0000_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_0000}, 28), + (0b_0001_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1000}, 29), + (0b_0011_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1100}, 30), + (0b_0111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1110}, 31), + (0b_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 32), + + (0b_0000_0001_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1000_0000}, 33), + (0b_0000_0011_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1100_0000}, 34), + (0b_0000_0111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1110_0000}, 35), + (0b_0000_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_0000}, 36), + (0b_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1000}, 37), + (0b_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1100}, 38), + (0b_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1110}, 39), + (0b_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 40), + + (0b_0000_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1000_0000}, 41), + (0b_0000_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1100_0000}, 42), + (0b_0000_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1110_0000}, 43), + (0b_0000_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_0000}, 44), + (0b_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1000}, 45), + (0b_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1100}, 46), + (0b_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1110}, 47), + (0b_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 48), + + (0b_0000_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1000_0000}, 49), + (0b_0000_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1100_0000}, 50), + (0b_0000_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1110_0000}, 51), + (0b_0000_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_0000}, 52), + (0b_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1000}, 53), + (0b_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1100}, 54), + (0b_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1110}, 55), + (0b_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 56), + + (0b_0000_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1000_0000}, 57), + (0b_0000_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1100_0000}, 58), + (0b_0000_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1110_0000}, 59), + (0b_0000_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_0000}, 60), + (0b_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1000}, 61), + (0b_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1100}, 62), + (0b_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1110}, 63), + (0b_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 64), + }.Select(t => new object[] { t.expected, t.streamContent, t.bitsCount }); + + public static IEnumerable BitsLeData => + new List<(ulong expected, byte[] streamContent, int bitsCount)> + { + (0b_0000_0000, new byte[] {0x00}, 0), + (0b_0000_0000, new byte[] {0x00}, 1), + (0b_0000_0001, new byte[] {0b_0000_0001}, 1), + (0b_0000_0001, new byte[] {0b_0000_0011}, 1), + (0b_0000_0011, new byte[] {0b_0000_0011}, 2), + (0b_0000_0111, new byte[] {0b_0000_0111}, 3), + (0b_0000_1111, new byte[] {0b_0000_1111}, 4), + (0b_0001_1111, new byte[] {0b_0001_1111}, 5), + (0b_0011_1111, new byte[] {0b_0011_1111}, 6), + (0b_0111_1111, new byte[] {0b_0111_1111}, 7), + (0b_1111_1111, new byte[] {0b_1111_1111}, 8), + + (0b_0000_0001_1111_1111, new byte[] {0b_1111_1111, 0b_0000_0001}, 9), + (0b_0000_0011_1111_1111, new byte[] {0b_1111_1111, 0b_0000_0011}, 10), + (0b_0000_0111_1111_1111, new byte[] {0b_1111_1111, 0b_0000_0111}, 11), + (0b_0000_1111_1111_1111, new byte[] {0b_1111_1111, 0b_0000_1111}, 12), + (0b_0001_1111_1111_1111, new byte[] {0b_1111_1111, 0b_0001_1111}, 13), + (0b_0011_1111_1111_1111, new byte[] {0b_1111_1111, 0b_0011_1111}, 14), + (0b_0111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_0111_1111}, 15), + (0b_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111}, 16), + + (0b_0000_0001_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_0000_0001}, 17), + (0b_0000_0011_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_0000_0011}, 18), + (0b_0000_0111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_0000_0111}, 19), + (0b_0000_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_0000_1111}, 20), + (0b_0001_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_0001_1111}, 21), + (0b_0011_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_0011_1111}, 22), + (0b_0111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_0111_1111}, 23), + (0b_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 24), + + (0b_0000_0001_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0001}, 25), + (0b_0000_0011_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0011}, 26), + (0b_0000_0111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0111}, 27), + (0b_0000_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_1111}, 28), + (0b_0001_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0001_1111}, 29), + (0b_0011_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0011_1111}, 30), + (0b_0111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0111_1111}, 31), + (0b_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 32), + + (0b_0000_0001_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0001}, 33), + (0b_0000_0011_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0011}, 34), + (0b_0000_0111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0111}, 35), + (0b_0000_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_1111}, 36), + (0b_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0001_1111}, 37), + (0b_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0011_1111}, 38), + (0b_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0111_1111}, 39), + (0b_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 40), + + (0b_0000_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0001}, 41), + (0b_0000_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0011}, 42), + (0b_0000_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0111}, 43), + (0b_0000_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_1111}, 44), + (0b_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0001_1111}, 45), + (0b_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0011_1111}, 46), + (0b_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0111_1111}, 47), + (0b_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 48), + + (0b_0000_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0001}, 49), + (0b_0000_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0011}, 50), + (0b_0000_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0111}, 51), + (0b_0000_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_1111}, 52), + (0b_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0001_1111}, 53), + (0b_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0011_1111}, 54), + (0b_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0111_1111}, 55), + (0b_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 56), + + (0b_0000_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0001}, 57), + (0b_0000_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0011}, 58), + (0b_0000_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_0111}, 59), + (0b_0000_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0000_1111}, 60), + (0b_0001_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0001_1111}, 61), + (0b_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0011_1111}, 62), + (0b_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_0111_1111}, 63), + (0b_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111, new byte[] {0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111, 0b_1111_1111}, 64), + }.Select(t => new object[] { t.expected, t.streamContent, t.bitsCount }); + } +} \ No newline at end of file diff --git a/Kaitai.Struct.Runtime.Async.Tests/Data/DecimalData.cs b/Kaitai.Struct.Runtime.Async.Tests/Data/DecimalData.cs new file mode 100644 index 0000000..4cec9a0 --- /dev/null +++ b/Kaitai.Struct.Runtime.Async.Tests/Data/DecimalData.cs @@ -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 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 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}); + } +} \ No newline at end of file diff --git a/Kaitai.Struct.Runtime.Async.Tests/Data/IntegralData.cs b/Kaitai.Struct.Runtime.Async.Tests/Data/IntegralData.cs new file mode 100644 index 0000000..ff5c52f --- /dev/null +++ b/Kaitai.Struct.Runtime.Async.Tests/Data/IntegralData.cs @@ -0,0 +1,82 @@ +using System.Collections.Generic; +using System.Linq; + +namespace Kaitai.Struct.Runtime.Async.Tests +{ + public class IntegralData + { + public static IEnumerable 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 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 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 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}); + } +} \ No newline at end of file diff --git a/Kaitai.Struct.Runtime.Async.Tests/Kaitai.Struct.Runtime.Async.Tests.csproj b/Kaitai.Struct.Runtime.Async.Tests/Kaitai.Struct.Runtime.Async.Tests.csproj index a2352e4..847abad 100644 --- a/Kaitai.Struct.Runtime.Async.Tests/Kaitai.Struct.Runtime.Async.Tests.csproj +++ b/Kaitai.Struct.Runtime.Async.Tests/Kaitai.Struct.Runtime.Async.Tests.csproj @@ -13,4 +13,8 @@ + + + + diff --git a/Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs b/Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs new file mode 100644 index 0000000..3241584 --- /dev/null +++ b/Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStructTests.cs b/Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStructTests.cs new file mode 100644 index 0000000..9803a62 --- /dev/null +++ b/Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStructTests.cs @@ -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) + { + } + } + } +} diff --git a/Kaitai.Struct.Runtime.Async.Tests/ReadBits.cs b/Kaitai.Struct.Runtime.Async.Tests/ReadBits.cs new file mode 100644 index 0000000..bb89c64 --- /dev/null +++ b/Kaitai.Struct.Runtime.Async.Tests/ReadBits.cs @@ -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)); + } + } +} \ No newline at end of file diff --git a/Kaitai.Struct.Runtime.Async.Tests/ReadBytesAsync.cs b/Kaitai.Struct.Runtime.Async.Tests/ReadBytesAsync.cs new file mode 100644 index 0000000..f1fad8f --- /dev/null +++ b/Kaitai.Struct.Runtime.Async.Tests/ReadBytesAsync.cs @@ -0,0 +1,208 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Kaitai.Async; +using Xunit; + +namespace Kaitai.Struct.Runtime.Async.Tests +{ + public class ReadBytesAsync + { + public static IEnumerable BytesData => + new List<(byte[] streamContent, int bytesCount)> + { + (new byte[] {0b_1101_0101}, 0), + (new byte[] {0b_1101_0101}, 1), + (new byte[] {0b_1101_0101, 0b_1101_0101}, 1), + (new byte[] {0b_1101_0101, 0b_1101_0101}, 2), + }.Select(t => new object[] { t.streamContent, t.bytesCount }); + + + [Theory] + [MemberData(nameof(BytesData))] + public async Task ReadBytesAsync_long_Test(byte[] streamContent, long bytesCount) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(streamContent.Take((int)bytesCount), await kaitaiStreamSUT.ReadBytesAsync(bytesCount)); + } + + [Theory] + [MemberData(nameof(BytesData))] + public async Task ReadBytesAsync_ulong_Test(byte[] streamContent, ulong bytesCount) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(streamContent.Take((int)bytesCount), await kaitaiStreamSUT.ReadBytesAsync(bytesCount)); + } + + [Fact] + public async Task ReadBytesAsyncLong_NegativeInvoke_ThrowsArgumentOutOfRangeException() + { + var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[0]); + + await Assert.ThrowsAsync(async () => + await kaitaiStreamSUT.ReadBytesAsync((long) -1)); + } + + [Fact] + public async Task ReadBytesAsyncLong_LargerThanInt32Invoke_ThrowsArgumentOutOfRangeException() + { + var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[0]); + + await Assert.ThrowsAsync(async () => + await kaitaiStreamSUT.ReadBytesAsync((long)Int32.MaxValue+1)); + } + + [Fact] + public async Task ReadBytesAsyncLong_LargerThanBufferInvoke_ThrowsArgumentOutOfRangeException() + { + var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[0]); + + await Assert.ThrowsAsync(async () => + await kaitaiStreamSUT.ReadBytesAsync((long)1)); + } + + [Fact] + public async Task ReadBytesAsyncULong_LargerThanInt32Invoke_ThrowsArgumentOutOfRangeException() + { + var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[0]); + + await Assert.ThrowsAsync(async () => + await kaitaiStreamSUT.ReadBytesAsync((ulong)Int32.MaxValue + 1)); + } + + [Fact] + public async Task ReadBytesAsyncULong_LargerThanBufferInvoke_ThrowsArgumentOutOfRangeException() + { + var kaitaiStreamSUT = new KaitaiAsyncStream(new byte[0]); + + await Assert.ThrowsAsync(async () => + await kaitaiStreamSUT.ReadBytesAsync((ulong)1)); + } + + public static IEnumerable StringData => + new List + { + "", + "ABC", + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", + }.Select(t => new []{ Encoding.ASCII.GetBytes(t)}); + + + [Theory] + [MemberData(nameof(StringData))] + public async Task ReadBytesFullAsync_Test(byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(streamContent, await kaitaiStreamSUT.ReadBytesFullAsync()); + } + + [Theory] + [MemberData(nameof(StringData))] + public async Task EnsureFixedContentsAsync_Test(byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(streamContent, await kaitaiStreamSUT.EnsureFixedContentsAsync(streamContent)); + } + + [Theory] + [MemberData(nameof(StringData))] + public async Task EnsureFixedContentsAsync_ThrowsIfByteIsChanged(byte[] streamContent) + { + if(streamContent.Length == 0) return; + + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + var expected = streamContent.ToArray(); + expected[0] = (byte)~expected[0]; + + await Assert.ThrowsAsync(async () => await kaitaiStreamSUT.EnsureFixedContentsAsync(expected)); + } + + public static IEnumerable StringWithTerminatorsData => + new List<(string streamContent, string expected, char terminator, bool isPresent, bool shouldInclude)> + { + ("", "", '\0', false, false), + ("", "", '\0', false, true), + + ("ABC", "ABC", '\0', false, false), + ("ABC", "ABC", '\0', false, true), + + ("ABC", "", 'A', true, false), + ("ABC", "A", 'A', true, true), + + ("ABC", "A", 'B', true, false), + ("ABC", "AB", 'B', true, true), + + ("ABC", "AB", 'C', true, false), + ("ABC", "ABC", 'C', true, true), + }.Select(t => new[] { Encoding.ASCII.GetBytes(t.streamContent), Encoding.ASCII.GetBytes(t.expected), (object)(byte)t.terminator, t.isPresent, t.shouldInclude}); + + [Theory] + [MemberData(nameof(StringWithTerminatorsData))] + public async Task ReadBytesTermAsync(byte[] streamContent, byte[] expected, byte terminator, bool _, bool shouldInclude) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadBytesTermAsync(terminator, shouldInclude, false, false)); + } + + [Theory] + [MemberData(nameof(StringWithTerminatorsData))] + public async Task ReadBytesTermAsync_ThrowsIsTerminatorNotPresent(byte[] streamContent, byte[] expected, byte terminator, bool terminatorIsPresent, bool shouldInclude) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + if(terminatorIsPresent) return; + + await Assert.ThrowsAsync(async()=> await kaitaiStreamSUT.ReadBytesTermAsync(terminator, shouldInclude, false, true)); + } + + [Theory] + [MemberData(nameof(StringWithTerminatorsData))] + public async Task ReadBytesTermAsync_ShouldNotConsumeTerminator(byte[] streamContent, byte[] expected, byte terminator, bool terminatorIsPresent, bool shouldInclude) + { + //Arrange + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + //Act + await kaitaiStreamSUT.ReadBytesTermAsync(terminator, shouldInclude, false, false); + + //Assert + var amountToConsume = expected.Length; + if (expected.Length > 0 && shouldInclude && terminatorIsPresent) + { + amountToConsume--; + } + + Assert.Equal(amountToConsume, kaitaiStreamSUT.Pos); + } + + [Theory] + [MemberData(nameof(StringWithTerminatorsData))] + public async Task ReadBytesTermAsync_ShouldConsumeTerminator(byte[] streamContent, byte[] expected, byte terminator, bool terminatorIsPresent, bool shouldInclude) + { + //Arrange + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + //Act + await kaitaiStreamSUT.ReadBytesTermAsync(terminator, shouldInclude, true, false); + + //Assert + var amountToConsume = expected.Length; + if (!shouldInclude && terminatorIsPresent) + { + amountToConsume++; + } + + Assert.Equal(amountToConsume, kaitaiStreamSUT.Pos); + } + + } +} \ No newline at end of file diff --git a/Kaitai.Struct.Runtime.Async.Tests/ReadDecimal.cs b/Kaitai.Struct.Runtime.Async.Tests/ReadDecimal.cs new file mode 100644 index 0000000..9a9487c --- /dev/null +++ b/Kaitai.Struct.Runtime.Async.Tests/ReadDecimal.cs @@ -0,0 +1,46 @@ +using System.Linq; +using System.Threading.Tasks; +using Kaitai.Async; +using Xunit; + +namespace Kaitai.Struct.Runtime.Async.Tests +{ + public class ReadDecimal + { + [Theory] + [MemberData(nameof(DecimalData.Decimal4Data), MemberType = typeof(DecimalData))] + public async Task ReadF4beAsync_Test(float expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent.Reverse().ToArray()); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadF4beAsync()); + } + + [Theory] + [MemberData(nameof(DecimalData.Decimal4Data), MemberType = typeof(DecimalData))] + public async Task ReadF4leAsync_Test(float expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadF4leAsync()); + } + + [Theory] + [MemberData(nameof(DecimalData.Decimal8Data), MemberType = typeof(DecimalData))] + public async Task ReadF8beAsync_Test(double expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent.Reverse().ToArray()); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadF8beAsync()); + } + + [Theory] + [MemberData(nameof(DecimalData.Decimal8Data), MemberType = typeof(DecimalData))] + public async Task ReadF8leAsync_Test(double expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadF8leAsync()); + } + } +} \ No newline at end of file diff --git a/Kaitai.Struct.Runtime.Async.Tests/ReadSigned.cs b/Kaitai.Struct.Runtime.Async.Tests/ReadSigned.cs new file mode 100644 index 0000000..0207c42 --- /dev/null +++ b/Kaitai.Struct.Runtime.Async.Tests/ReadSigned.cs @@ -0,0 +1,73 @@ +using System.Linq; +using System.Threading.Tasks; +using Kaitai.Async; +using Xunit; + +namespace Kaitai.Struct.Runtime.Async.Tests +{ + public class ReadSigned + { + [Theory] + [MemberData(nameof(IntegralData.Integral1Data), MemberType = typeof(IntegralData))] + public async Task ReadS1Async_Test(sbyte expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadS1Async()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral2Data), MemberType = typeof(IntegralData))] + public async Task ReadS2beAsync_Test(short expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadS2beAsync()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral4Data), MemberType = typeof(IntegralData))] + public async Task ReadS4beAsync_Test(int expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadS4beAsync()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral8Data), MemberType = typeof(IntegralData))] + public async Task ReadS8beAsync_Test(long expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadS8beAsync()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral2Data), MemberType = typeof(IntegralData))] + public async Task ReadS2leAsync_Test(short expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent.Reverse().ToArray()); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadS2leAsync()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral4Data), MemberType = typeof(IntegralData))] + public async Task ReadS4leAsync_Test(int expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent.Reverse().ToArray()); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadS4leAsync()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral8Data), MemberType = typeof(IntegralData))] + public async Task ReadS8leAsync_Test(long expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent.Reverse().ToArray()); + + Assert.Equal(expected, await kaitaiStreamSUT.ReadS8leAsync()); + } + } +} \ No newline at end of file diff --git a/Kaitai.Struct.Runtime.Async.Tests/ReadUnSigned.cs b/Kaitai.Struct.Runtime.Async.Tests/ReadUnSigned.cs new file mode 100644 index 0000000..9428724 --- /dev/null +++ b/Kaitai.Struct.Runtime.Async.Tests/ReadUnSigned.cs @@ -0,0 +1,73 @@ +using System.Linq; +using System.Threading.Tasks; +using Kaitai.Async; +using Xunit; + +namespace Kaitai.Struct.Runtime.Async.Tests +{ + public class ReadUnSigned + { + [Theory] + [MemberData(nameof(IntegralData.Integral1Data), MemberType = typeof(IntegralData))] + public async Task ReadU1Async_Test( /*u*/ sbyte expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal((byte) expected, await kaitaiStreamSUT.ReadU1Async()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral2Data), MemberType = typeof(IntegralData))] + public async Task ReadU2beAsync_Test( /*u*/ short expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal((ushort) expected, await kaitaiStreamSUT.ReadU2beAsync()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral4Data), MemberType = typeof(IntegralData))] + public async Task ReadU4beAsync_Test( /*u*/ int expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal((uint) expected, await kaitaiStreamSUT.ReadU4beAsync()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral8Data), MemberType = typeof(IntegralData))] + public async Task ReadU8beAsync_Test( /*u*/ long expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent); + + Assert.Equal((ulong) expected, await kaitaiStreamSUT.ReadU8beAsync()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral2Data), MemberType = typeof(IntegralData))] + public async Task ReadU2leAsync_Test( /*u*/ short expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent.Reverse().ToArray()); + + Assert.Equal((ushort) expected, await kaitaiStreamSUT.ReadU2leAsync()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral4Data), MemberType = typeof(IntegralData))] + public async Task ReadU4leAsync_Test( /*u*/ int expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent.Reverse().ToArray()); + + Assert.Equal((uint) expected, await kaitaiStreamSUT.ReadU4leAsync()); + } + + [Theory] + [MemberData(nameof(IntegralData.Integral8Data), MemberType = typeof(IntegralData))] + public async Task ReadU8leAsync_Test( /*u*/ long expected, byte[] streamContent) + { + var kaitaiStreamSUT = new KaitaiAsyncStream(streamContent.Reverse().ToArray()); + + Assert.Equal((ulong) expected, await kaitaiStreamSUT.ReadU8leAsync()); + } + } +} \ No newline at end of file diff --git a/Kaitai.Struct.Runtime.Async/KaitaiAsyncStream.cs b/Kaitai.Struct.Runtime.Async/KaitaiAsyncStream.cs index e2500d8..9227593 100644 --- a/Kaitai.Struct.Runtime.Async/KaitaiAsyncStream.cs +++ b/Kaitai.Struct.Runtime.Async/KaitaiAsyncStream.cs @@ -13,19 +13,16 @@ public partial class KaitaiAsyncStream : KaitaiStreamBase, IKaitaiAsyncStream private ulong _bits = 0; private int _bitsLeft = 0; - private AsyncBinaryReader _asyncBinaryReader; - protected AsyncBinaryReader AsyncBinaryReader - { - get => _asyncBinaryReader ?? (_asyncBinaryReader = new AsyncBinaryReader(BaseStream)); - set => _asyncBinaryReader = value; - } + protected AsyncBinaryReader AsyncBinaryReader { get; } #region Constructors public KaitaiAsyncStream(Stream stream) { BaseStream = stream; + AsyncBinaryReader = new AsyncBinaryReader(BaseStream); + } /// @@ -306,7 +303,7 @@ public async Task EnsureFixedContentsAsync(byte[] expected) { byte[] bytes = await ReadBytesAsync(expected.Length); - if (bytes.Length != expected.Length) + if (bytes.Length != expected.Length) //TODO Is this necessary? { throw new Exception( $"Expected bytes: {Convert.ToBase64String(expected)} ({expected.Length} bytes), Instead got: {Convert.ToBase64String(bytes)} ({bytes.Length} bytes)"); diff --git a/Kaitai.Struct.sln.DotSettings b/Kaitai.Struct.sln.DotSettings index 72e13e3..2c5120e 100644 --- a/Kaitai.Struct.sln.DotSettings +++ b/Kaitai.Struct.sln.DotSettings @@ -1,3 +1,4 @@  + SUT True True \ No newline at end of file