public void ConstructorSetsPublicPropertiesAsExpected()
        {
            const int limit = 300;
            const OnFullDictionaryOption onFullDictionaryOption = OnFullDictionaryOption.Empty;

            var lzWDictionary = new LzWDictionary(limit, onFullDictionaryOption);

            Assert.AreEqual(limit, lzWDictionary.Limit);
            Assert.AreEqual(onFullDictionaryOption, lzWDictionary.OnFullDictionaryOption);
        }
Exemplo n.º 2
0
        private void InterpretHeader(IFileReader fileReader)
        {
            numberOfBitsForIndex = (byte)fileReader.ReadBits(4);

            var onFullDictionaryOption = fileReader.ReadBit()
                ? OnFullDictionaryOption.Freeze
                : OnFullDictionaryOption.Empty;

            lzWDictionary = new LzWDictionary((int)Math.Pow(2, numberOfBitsForIndex) - 1, onFullDictionaryOption);
        }
        public void ContainsIndexReturnsTrueForValuesFrom0To255()
        {
            var allAreContained = true;
            var lzWDictionary   = new LzWDictionary(258, OnFullDictionaryOption.Empty);

            for (uint i = 0; i < 256; i++)
            {
                if (!lzWDictionary.ContainsIndex(i))
                {
                    allAreContained = false;
                    break;
                }
            }

            Assert.IsTrue(allAreContained);
        }
        public void ContainsStringReturnsTrueForStringsComposedOfASingleCharacter()
        {
            var allAreContained = true;
            var lzWDictionary   = new LzWDictionary(258, OnFullDictionaryOption.Empty);

            for (int i = 0; i < 256; i++)
            {
                var str = ((char)((byte)i)).ToString();

                if (!lzWDictionary.ContainsString(str))
                {
                    allAreContained = false;
                    break;
                }
            }

            Assert.IsTrue(allAreContained);
        }
 public void ConstructorThrowsArgumentExceptionForLimitSmallerThan256()
 {
     var lzWDictionary = new LzWDictionary(200, OnFullDictionaryOption.Empty);
 }
Exemplo n.º 6
0
        public void EncodeFile(IFileReader fileReader, IFileWriter fileWriter, OnFullDictionaryOption onFullDictionaryOption, int numberOfBitsIndex)
        {
            if (fileReader == null)
            {
                throw new ArgumentNullException(nameof(fileReader));
            }

            if (fileWriter == null)
            {
                throw new ArgumentNullException(nameof(fileWriter));
            }

            if (numberOfBitsIndex < 9 || numberOfBitsIndex > 15)
            {
                throw new ArgumentException($"{nameof(numberOfBitsIndex)} must be at least 9, and at most 15");
            }

            WriteHeader(fileWriter, onFullDictionaryOption, numberOfBitsIndex);
            IndexesFromLastRun.Clear();

            LzWDictionary = new LzWDictionary((int)Math.Pow(2, numberOfBitsIndex) - 1, onFullDictionaryOption);

            var lastCharacter = (char)fileReader.ReadBits(8);
            var shouldStop = false;

            while (true)
            {
                var currentString = lastCharacter.ToString();
                uint lastIndex = 0;

                if (shouldStop)
                {
                    break;
                }

                while (true)
                {
                    if (LzWDictionary.ContainsString(currentString))
                    {
                        lastIndex = LzWDictionary.GetIndexByString(currentString);

                        if (shouldStop)
                        {
                            fileWriter.WriteValueOnBits(lastIndex, (byte)numberOfBitsIndex);
                            IndexesFromLastRun.Add(lastIndex);

                            break;
                        }
                    }
                    else
                    {
                        LzWDictionary.Add(currentString);

                        fileWriter.WriteValueOnBits(lastIndex, (byte)numberOfBitsIndex);
                        IndexesFromLastRun.Add(lastIndex);

                        break;
                    }

                    if (!fileReader.ReachedEndOfFile)
                    {
                        var readByte = (byte)fileReader.ReadBits(8);
                        currentString += (char)readByte;
                        lastCharacter = (char)readByte;
                    }
                    else
                    {
                        shouldStop = true;
                    }
                }
            }

            fileWriter.Flush();
        }