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 DefaultFileDecoder(WeightsTable weightsTable, IDecodingInputStream inputStream, long length, HuffmanDecoder decoder)
 {
     this.weightsTable = weightsTable;
     this.inputStream  = inputStream;
     this.length       = length;
     this.decoder      = decoder;
 }
Example #3
0
        public void ParseWeightsTableTest3()
        {
            IReadOnlyList <Bit> data = BitListHelper.CreateBuilder()
                                       .AddInt(0x2D)
                                       .AddByte(0x1)
                                       .AddLong(0x1)
                                       .AddByte(0x2)
                                       .AddLong(0xFFF)
                                       .AddByte(0xF)
                                       .AddLong(0x5)
                                       .AddByte(0xAF)
                                       .AddLong(0xFFA)
                                       .AddByte(0xFF)
                                       .AddLong(0x1).BitList;

            BootstrapSegment segment = streamParser.ParseWeightsTable(new TestIDecodingInputStream(data));

            Assert.IsNotNull(segment);
            WeightsTable weightsTable = segment.WeightsTable;

            Assert.IsNotNull(weightsTable);

            WeightedSymbol[] expectedSymbols =
            {
                new WeightedSymbol(0x1,    0x1),
                new WeightedSymbol(0x2,  0xFFF),
                new WeightedSymbol(0xF,    0x5),
                new WeightedSymbol(0xAF, 0xFFA),
                new WeightedSymbol(0xFF,     1),
            };
            Assert.AreEqual(expectedSymbols, weightsTable);
        }
Example #4
0
        public void BuildWeightsTableTest1()
        {
            HuffmanEncoder           encoder      = new HuffmanEncoder();
            TestIEncodingInputStream inputStream  = new TestIEncodingInputStream(new byte[0]);
            WeightsTable             weightsTable = encoder.BuildWeightsTable(inputStream, CancellationToken.None);

            Assert.IsNotNull(weightsTable);
            CollectionAssert.IsEmpty(weightsTable);
        }
Example #5
0
        public void BuildHuffmanTreeTest1()
        {
            HuffmanEncoder  encoder      = new HuffmanEncoder();
            WeightsTable    weightsTable = new WeightsTable();
            HuffmanTreeBase huffmanTree  = encoder.BuildHuffmanTree(weightsTable);

            Assert.IsNotNull(huffmanTree);
            CollectionAssert.IsEmpty(huffmanTree.FlattenValues());
        }
Example #6
0
        public void BuildWeightsTableTest2()
        {
            HuffmanEncoder           encoder      = new HuffmanEncoder();
            TestIEncodingInputStream inputStream  = new TestIEncodingInputStream(new byte[] { 1 });
            WeightsTable             weightsTable = encoder.BuildWeightsTable(inputStream, CancellationToken.None);

            Assert.IsNotNull(weightsTable);
            WeightedSymbol[] expected = { new WeightedSymbol(1, 1) };
            CollectionAssert.AreEqual(expected, weightsTable.ToArray());
        }
        public FileSegment ParseFile(IDecodingInputStream stream, WeightsTable weightsTable)
        {
            Guard.IsNotNull(stream, nameof(stream));
            Guard.IsNotNull(weightsTable, nameof(weightsTable));

            int    nameLength = stream.ReadInt() / 2;
            string name       = ReadString(stream, nameLength);
            long   dataLength = stream.ReadLong();

            return(new FileSegment(name, new DefaultFileDecoder(weightsTable, stream, dataLength, decoder)));
        }
Example #8
0
        public void ParseWeightsTableTest1()
        {
            IReadOnlyList <Bit> data = BitListHelper.CreateBuilder()
                                       .AddInt(0x0).BitList;

            BootstrapSegment segment = streamParser.ParseWeightsTable(new TestIDecodingInputStream(data));

            Assert.IsNotNull(segment);
            WeightsTable weightsTable = segment.WeightsTable;

            Assert.IsNotNull(weightsTable);
            Assert.AreEqual(0, weightsTable.Size);
        }
Example #9
0
        public void BuildHuffmanTreeTest4()
        {
            HuffmanEncoder encoder      = new HuffmanEncoder();
            WeightsTable   weightsTable = new WeightsTable();

            for (int n = 0; n < 13; n++)
            {
                weightsTable.TrackSymbol((byte)'A');
            }
            weightsTable.TrackSymbol((byte)'B');

            for (int n = 0; n < 2; n++)
            {
                weightsTable.TrackSymbol((byte)'C');
            }
            for (int n = 0; n < 15; n++)
            {
                weightsTable.TrackSymbol((byte)'D');
            }
            for (int n = 0; n < 11; n++)
            {
                weightsTable.TrackSymbol((byte)'E');
            }

            HuffmanTreeBase huffmanTree = encoder.BuildHuffmanTree(weightsTable);

            Assert.IsNotNull(huffmanTree);
            WeightedSymbol?[] expected =
            {
                new WeightedSymbol(0,         42),
                new WeightedSymbol((byte)'D', 15),
                new WeightedSymbol(0,         27),
                null,
                null,
                new WeightedSymbol((byte)'A', 13),
                new WeightedSymbol(0,         14),
                null,
                null,
                new WeightedSymbol(0,         03),
                new WeightedSymbol((byte)'E', 11),
                new WeightedSymbol((byte)'B', 01),
                new WeightedSymbol((byte)'C', 02),
                null,
                null,
                null,
                null,
                null,
                null,
            };
            Assert.AreEqual(expected, huffmanTree.FlattenValues());
        }
Example #10
0
        public void AddWeightsTable(BootstrapSegment segment)
        {
            Guard.IsNotNull(segment, nameof(segment));

            WeightsTable weightsTable = segment.WeightsTable;

            stream.Write(StreamKind.WT_CODE);
            stream.Write(9 * weightsTable.Size);
            foreach (WeightedSymbol symbol in weightsTable)
            {
                stream.Write(symbol.Symbol);
                stream.Write(symbol.Weight);
            }
        }
        public BootstrapSegment ParseWeightsTable(IDecodingInputStream stream)
        {
            Guard.IsNotNull(stream, nameof(stream));

            WeightsTable weightsTable = new WeightsTable();
            int          tableSize    = stream.ReadInt() / 9;

            for (int n = 0; n < tableSize; n++)
            {
                byte code      = stream.ReadByte();
                long frequency = stream.ReadLong();
                weightsTable.TrackSymbol(code, frequency);
            }
            return(new BootstrapSegment(weightsTable));
        }
Example #12
0
        public void BuildHuffmanTreeTest2()
        {
            WeightsTable weightsTable = new WeightsTable();

            weightsTable.TrackSymbol(1);
            HuffmanEncoder encoder = new HuffmanEncoder();

            HuffmanTreeBase huffmanTree = encoder.BuildHuffmanTree(weightsTable);

            Assert.IsNotNull(huffmanTree);
            WeightedSymbol?[] expected =
            {
                new WeightedSymbol(1, 1), null, null
            };
            Assert.AreEqual(expected, huffmanTree.FlattenValues());
        }
Example #13
0
        public void BuildWeightsTableTest3()
        {
            byte[]         data    = @"aaaabbbccedddddeeeaabdcefffeffadc".ToByteArray();
            HuffmanEncoder encoder = new HuffmanEncoder();

            TestIEncodingInputStream inputStream  = new TestIEncodingInputStream(data);
            WeightsTable             weightsTable = encoder.BuildWeightsTable(inputStream, CancellationToken.None);

            Assert.IsNotNull(weightsTable);
            WeightedSymbol[] expected =
            {
                new WeightedSymbol((byte)'a', 7),
                new WeightedSymbol((byte)'b', 4),
                new WeightedSymbol((byte)'c', 4),
                new WeightedSymbol((byte)'d', 7),
                new WeightedSymbol((byte)'e', 6),
                new WeightedSymbol((byte)'f', 5),
            };
            CollectionAssert.AreEqual(expected, weightsTable.ToArray());
        }
Example #14
0
        public void ParseWeightsTableTest2()
        {
            IReadOnlyList <Bit> data = BitListHelper.CreateBuilder()
                                       .AddInt(0x9)
                                       .AddByte(0x45)
                                       .AddLong(0x33).BitList;

            BootstrapSegment segment = streamParser.ParseWeightsTable(new TestIDecodingInputStream(data));

            Assert.IsNotNull(segment);
            WeightsTable weightsTable = segment.WeightsTable;

            Assert.IsNotNull(weightsTable);

            WeightedSymbol[] expectedSymbols =
            {
                new WeightedSymbol(0x45, 0x33)
            };
            Assert.AreEqual(expectedSymbols, weightsTable);
        }
Example #15
0
        public void ParseFileTest2()
        {
            IReadOnlyList <Bit> data = BitListHelper.CreateBuilder()
                                       .AddInt(0x10)
                                       .AddChar('d')
                                       .AddChar('a')
                                       .AddChar('t')
                                       .AddChar('a')
                                       .AddChar('.')
                                       .AddChar('b')
                                       .AddChar('i')
                                       .AddChar('n')
                                       .AddLong(0x19)
                                       .AddByte(0x20)
                                       .AddByte(0x55)
                                       .AddByte(0xFF)
                                       .AddByte(0x01).BitList;

            WeightsTable weightsTable = new WeightsTable();

            weightsTable.TrackSymbol(1, 1);
            weightsTable.TrackSymbol(2, 2);
            weightsTable.TrackSymbol(3, 4);
            weightsTable.TrackSymbol(4, 8);

            FileSegment segment = streamParser.ParseFile(new TestIDecodingInputStream(data), weightsTable);

            Assert.IsNotNull(segment);
            Assert.AreEqual("data.bin", segment.Name);
            Assert.IsNull(segment.Path);

            IFileDecoder decoder = segment.FileDecoder;

            Assert.IsNotNull(decoder);
            TestIDecodingOutputStream outputStream = new TestIDecodingOutputStream();

            decoder.Decode(outputStream, CancellationToken.None, null);
            byte[] expectedData = { 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
            Assert.AreEqual(expectedData, outputStream.ByteList);
        }
 public BootstrapSegment(WeightsTable weightsTable)
 {
     Guard.IsNotNull(weightsTable, nameof(weightsTable));
     WeightsTable = weightsTable;
 }
Example #17
0
 public void SetUp()
 {
     this.weightsTable = new WeightsTable();
 }
Example #18
0
        public void BuildHuffmanTreeTest3()
        {
            HuffmanEncoder encoder      = new HuffmanEncoder();
            WeightsTable   weightsTable = new WeightsTable();

            for (int n = 0; n < 12; n++)
            {
                weightsTable.TrackSymbol((byte)'a');
            }
            for (int n = 0; n < 2; n++)
            {
                weightsTable.TrackSymbol((byte)'b');
            }
            for (int n = 0; n < 7; n++)
            {
                weightsTable.TrackSymbol((byte)'c');
            }
            for (int n = 0; n < 13; n++)
            {
                weightsTable.TrackSymbol((byte)'d');
            }
            for (int n = 0; n < 14; n++)
            {
                weightsTable.TrackSymbol((byte)'e');
            }
            for (int n = 0; n < 85; n++)
            {
                weightsTable.TrackSymbol((byte)'f');
            }

            HuffmanTreeBase huffmanTree = encoder.BuildHuffmanTree(weightsTable);

            Assert.IsNotNull(huffmanTree);
            WeightedSymbol?[] expected =
            {
                new WeightedSymbol(0,         133),
                new WeightedSymbol(0,         048),
                new WeightedSymbol((byte)'f', 085),
                new WeightedSymbol(0,         021),
                new WeightedSymbol(0,         027),
                null,
                null,
                new WeightedSymbol(0,         009),
                new WeightedSymbol((byte)'a', 012),
                new WeightedSymbol((byte)'d', 013),
                new WeightedSymbol((byte)'e', 014),
                new WeightedSymbol((byte)'b', 002),
                new WeightedSymbol((byte)'c', 007),
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
            };
            Assert.AreEqual(expected, huffmanTree.FlattenValues());
        }
 FileSegment IStreamParser.ParseFile(IDecodingInputStream stream, WeightsTable weightsTable)
 {
     Trace += "->ParseFile;";
     return(new FileSegment(FileName, Decoder));
 }