public void EncodeTest3() { byte[] data = { 1, 2, 2, 1, 1, 2, 2 }; TestIEncodingInputStream inputStream = new TestIEncodingInputStream(data); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); long length = encoder.Encode(inputStream, outputStream, token, CancellationToken.None, null); Assert.IsNotNull(token.HuffmanTree); Assert.IsNotNull(token.CodingTable); WeightedSymbol?[] expected = { new WeightedSymbol(0, 7), new WeightedSymbol(1, 3), new WeightedSymbol(2, 4), null, null, null, null, }; Assert.AreEqual(expected, token.HuffmanTree.FlattenValues()); Assert.AreEqual(7, length); AssertOutputStream(outputStream, "0110011"); }
public void AddWeightsTableTest3() { TestIEncodingOutputStream stream = new TestIEncodingOutputStream(); builder.Initialize(new TestIPlatformService(), EmptyEncodingToken.Instance, stream); WeightsTable weightsTable = new WeightsTable(); weightsTable.TrackSymbol(0x1); weightsTable.TrackSymbol(symbol: 0xF, frequency: 0x5); weightsTable.TrackSymbol(symbol: 0x2, frequency: 0xFFF); weightsTable.TrackSymbol(symbol: 0xAF, frequency: 0xFFA); weightsTable.TrackSymbol(0xFF); builder.AddWeightsTable(new BootstrapSegment(weightsTable)); IReadOnlyList <Bit> bitList = BitListHelper.CreateBuilder() .AddByte(0x2) .AddInt(0x2D) .AddByte(0x1) .AddLong(0x1) .AddByte(0x2) .AddLong(0xFFF) .AddByte(0xF) .AddLong(0x5) .AddByte(0xAF) .AddLong(0xFFA) .AddByte(0xFF) .AddLong(0x1).BitList; Assert.AreEqual(bitList, stream.BitList); }
public void AddFileTest2() { byte[] data = { 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; TestIPlatformService platform = new TestIPlatformService { ReadFileFunc = x => new MemoryStream(data) }; TestIEncodingOutputStream stream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); builder.Initialize(platform, CreateEncodingToken(encoder, data), stream); builder.AddFile(new FileSegment(@"data.bin", @"C:\data.bin"), CancellationToken.None, null); IReadOnlyList <Bit> bitList = BitListHelper.CreateBuilder() .AddByte(0x00) .AddInt(0x10) .AddChar('d') .AddChar('a') .AddChar('t') .AddChar('a') .AddChar('.') .AddChar('b') .AddChar('i') .AddChar('n') .AddLong(0x19)//bits .AddByte(0x20) .AddByte(0x55) .AddByte(0xFF) .AddByte(0x01).BitList; Assert.AreEqual(bitList, stream.BitList); }
public void AddFileTest1() { TestIPlatformService platform = new TestIPlatformService { ReadFileFunc = x => new MemoryStream() }; TestIEncodingOutputStream stream = new TestIEncodingOutputStream(); builder.Initialize(platform, EmptyEncodingToken.Instance, stream); builder.AddFile(new FileSegment(@"file.bin", @"C:\file.bin"), CancellationToken.None, null); IReadOnlyList <Bit> bitList = BitListHelper.CreateBuilder() .AddByte(0x00) .AddInt(0x10) .AddChar('f') .AddChar('i') .AddChar('l') .AddChar('e') .AddChar('.') .AddChar('b') .AddChar('i') .AddChar('n') .AddLong(0x0).BitList; Assert.AreEqual(bitList, stream.BitList); }
public void EncodingProgressTest1(int size) { byte[] data = new byte[size]; TestIEncodingInputStream inputStream = new TestIEncodingInputStream(data); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); TestIProgressHandler progress = new TestIProgressHandler(); encoder.Encode(inputStream, outputStream, token, CancellationToken.None, progress); CollectionAssert.IsEmpty(progress.ValueList); }
public void AddWeightsTableTest1() { TestIEncodingOutputStream stream = new TestIEncodingOutputStream(); builder.Initialize(new TestIPlatformService(), EmptyEncodingToken.Instance, stream); builder.AddWeightsTable(new BootstrapSegment(new WeightsTable())); IReadOnlyList <Bit> bitList = BitListHelper.CreateBuilder() .AddByte(0x2) .AddInt(0x0).BitList; Assert.AreEqual(bitList, stream.BitList); }
public void EncodeTest4() { byte[] data = { 1, 2, 2, 2, 2, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; TestIEncodingInputStream inputStream = new TestIEncodingInputStream(data); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); long length = encoder.Encode(inputStream, outputStream, token, CancellationToken.None, null); Assert.IsNotNull(token.HuffmanTree); Assert.IsNotNull(token.CodingTable); Assert.AreEqual(54, length); AssertOutputStream(outputStream, "110111111111111110111111111101010101010001010000000000"); }
public void EncodeTest1() { TestIEncodingInputStream inputStream = new TestIEncodingInputStream(new byte[0]); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); long length = encoder.Encode(inputStream, outputStream, token, CancellationToken.None, null); Assert.IsNotNull(token.HuffmanTree); Assert.IsNotNull(token.CodingTable); Assert.AreEqual(0, length); CollectionAssert.IsEmpty(token.HuffmanTree.FlattenValues()); CollectionAssert.IsEmpty(outputStream.BitList); }
public void AddDirectoryTest2() { TestIEncodingOutputStream stream = new TestIEncodingOutputStream(); builder.Initialize(new TestIPlatformService(), EmptyEncodingToken.Instance, stream); builder.AddDirectory(new DirectorySegment("Directory", 3)); IReadOnlyList <Bit> bitList = BitListHelper.CreateBuilder() .AddByte(0x1) .AddInt(0x12) .AddString("Directory") .AddInt(0x3).BitList; Assert.AreEqual(bitList, stream.BitList); }
private void AssertOutputStream(TestIEncodingOutputStream stream, string expected) { IReadOnlyList <Bit> bitList = stream.BitList; if (bitList.Count != expected.Length) { throw new AssertionException(nameof(stream)); } for (int n = 0; n < bitList.Count; n++) { if (bitList[n] != CharToBit(expected[n])) { throw new AssertionException(nameof(stream)); } } }
public void EncodingProgressTest2() { byte[] data = new byte[27 * 1024]; TestIEncodingInputStream inputStream = new TestIEncodingInputStream(data); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); TestIProgressHandler progress = new TestIProgressHandler(); for (int n = 0; n < 10; n++) { encoder.Encode(inputStream, outputStream, token, CancellationToken.None, progress); inputStream.Reset(); } CollectionAssert.AreEqual(new long[] { 128 * 1024, 128 * 1024 }, progress.ValueList); }
public void EncodeTest2() { TestIEncodingInputStream inputStream = new TestIEncodingInputStream(new byte[] { 1 }); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); long length = encoder.Encode(inputStream, outputStream, token, CancellationToken.None, null); Assert.IsNotNull(token.HuffmanTree); Assert.IsNotNull(token.CodingTable); WeightedSymbol?[] expected = { new WeightedSymbol(1, 1), null, null, }; Assert.AreEqual(expected, token.HuffmanTree.FlattenValues()); Assert.AreEqual(1, length); AssertOutputStream(outputStream, "0"); }