public static void CompressingSingleWord() { //Arrange const string word = "Hello"; var huffman = new HuffmanAlgorithm(); //Act var wordResult = huffman.Compress(word); //Assert Assert.IsNotEmpty(wordResult); Assert.AreEqual("011000000001", wordResult); }
/// <summary>Конструктор</summary> /// <param name="view">Элемент представления для формы</param> /// <param name="service">Обработчик действий с моделью</param> public HuffmanPresenter(IHuffmanView view, IHuffmanService service) { _view = view; _service = service; /// <summary>Инициализируем пустые поля</summary> _huffmanAlgorithm = new HuffmanAlgorithm(); _fileStream = null; /// <summary>Прикрепляем обработчики</summary> _view.Open += () => Open(_view.FilePathIn); _view.Code += () => Code(_view.FilePathOut); }
public static void CompressingPhrase() { //Arrange const string phrase = "This is a string"; var huffman = new HuffmanAlgorithm(); //Act var phraseResult = huffman.Compress(phrase); //Assert Assert.IsNotEmpty(phraseResult); Assert.AreEqual( "000001000010000000000000010000000100000000000000100000001001000000010000001000001100000000010001", phraseResult); }
public void HuffmanGreedyTestMethod() { var alphabetCharacters = new Dictionary <char, int> { ['a'] = 3, ['b'] = 2, ['c'] = 6, ['d'] = 8, ['e'] = 2, ['f'] = 6 }; void AssertCorrectHuffmanCode(IDictionary <char, int> alphabet, IDictionary <char, string> huffmanCodes) { Assert.AreEqual(alphabet.Count, huffmanCodes.Count, "Invalid huffman codes length"); foreach (var alphabetKey in alphabet.Keys) { Assert.IsTrue(huffmanCodes.ContainsKey(alphabetKey), $"alphabet key: '{alphabetKey}' not found int huffman codes"); } foreach (var code in huffmanCodes.Values) { Assert.IsTrue(huffmanCodes.Values.Count(x => x.StartsWith(code)) == 1, $"Code: {code} is prefix of another code"); } } var huffman = new HuffmanAlgorithm(); var codes = huffman.Encode(alphabetCharacters); AssertCorrectHuffmanCode(alphabetCharacters, codes); void AssertHuffmanCodeLength(int length, char symbol) { Assert.AreEqual(length, codes[symbol].Length, $"Invalid code length ofr symbol: '{symbol}'"); } AssertHuffmanCodeLength(3, 'a'); AssertHuffmanCodeLength(4, 'b'); AssertHuffmanCodeLength(2, 'c'); AssertHuffmanCodeLength(2, 'd'); AssertHuffmanCodeLength(4, 'e'); AssertHuffmanCodeLength(2, 'f'); }
/// <summary> /// Starts packaging /// </summary> /// <exception cref="IOException"></exception> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="PathTooLongException"></exception> /// <exception cref="DirectoryNotFoundException"></exception> /// <exception cref="UnauthorizedAccessException"></exception> /// <exception cref="NotSupportedException"></exception> /// <exception cref="System.Security.SecurityException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <exception cref="ObjectDisposedException"></exception> /// <exception cref="EndOfStreamException"></exception> public void Pack() { PackingEvent?.Invoke(this, _pea); PackingDoneEventArgs _pdea = new PackingDoneEventArgs() { IsSuccessful = false }; string filepath = Path.Combine(Directory, Package.PackageName + ".cmpkg"); try { if (!System.IO.Directory.Exists(Directory)) { System.IO.Directory.CreateDirectory(Directory); } // 0x02, 0x1d, 0x1e - Start of Contents // 0x1c, 0x1d - File Separator // 0x1f, 0x1f, 0xfd - Filename/Content Separator long _totalAllBytes = 0; foreach (var ent in Package.Files) { _totalAllBytes += ent.Contents.LongLength; } _pea.TotalAllBytes = _totalAllBytes; PackingEvent?.Invoke(this, _pea); using (BinaryWriter writer = new BinaryWriter(File.Create(filepath))) { writer.Write(new byte[] { 0x00, 0x01, 0xc7, 0x56, 0x43, 0x4d, 0x50, 0x4b, 0x47, 0x00 }); writer.Write(Package.PackageName); //writer.Write(new byte[] { 0xcd, 0xb0 }); writer.Write(Package.PackageCreationTime.ToBinary()); writer.Write(Package.Files.Length); //writer.Write(new byte[] { 0x02, 0x1d, 0x1e }); //for (int i = 0; i < Package.Files.Length; i++) //{ // _pea.CurrentFilename = Package.Files[i].Filename; // _pea.TotalFileByte = Package.Files[i].Contents.LongLength; // PackingEvent?.Invoke(this, _pea); // writer.Write(Package.Files[i].Filename + Package.Files[i].Extention); // writer.Write(new byte[] { 0x1f, 0x1f, 0xfd }); // for (int curByte = 0; curByte < Package.Files[i].Contents.Length; curByte++) // { // _pea.CurrentFileByte = curByte; // PackingEvent?.Invoke(this, _pea); // writer.Write(Package.Files[i].Contents[curByte]); // } // if (i != Package.Files.Length) // { // writer.Write(new byte[] { 0x1c, 0x1d }); // } //} //writer.Write(new byte[] { 0xed, 0x0f, 0x11, 0xe0 }); HuffmanAlgorithm shrinker = new HuffmanAlgorithm(); shrinker.PercentCompleted += Shrinker_PercentCompleted; Dictionary <string, byte[]> _files = new Dictionary <string, byte[]>(); for (int l = 0; l < Package.Files.Length; l++) { Stream _shrinked = shrinker.ShrinkWithProgress(new MemoryStream(Package.Files[l].Contents), new char[] { }); List <byte> _fl = new List <byte>(); for (long i = 0; i < _shrinked.Length; i++) { _fl.Add((byte)_shrinked.ReadByte()); } _files.Add(Package.Files[l].Filename, _fl.ToArray()); _shrinked.Close(); } for (int i = 0; i < Package.Files.Length; i++) { writer.Write(Package.Files[i].Filename + Package.Files[i].Extention); writer.Write(_files[Package.Files[i].Filename].Length); } writer.Write(new byte[] { 0x1f, 0x1f, 0xfd }); for (int j = 0; j < Package.Files.Length; j++) { _pea.CurrentFilename = Package.Files[j].Filename; _pea.TotalFileByte = _files[Package.Files[j].Filename].Length; _pea.CurrentFileIndex = j; PackingEvent?.Invoke(this, _pea); for (long curbyte = 0; curbyte < _files[Package.Files[j].Filename].Length; curbyte++) { _pea.CurrentFileByte = curbyte; PackingEvent?.Invoke(this, _pea); writer.Write(_files[Package.Files[j].Filename][curbyte]); } } } _pdea.IsSuccessful = true; PackingDoneEvent?.Invoke(this, _pdea); } catch (Exception e) { _pdea.InnerException = e; PackingDoneEvent?.Invoke(this, _pdea); } }
/// <summary> /// Cunstractur /// </summary> /// <param name="controller">IController for the MVC architectural pattern </param> public MyModel() { m_Huffman = new HuffmanAlgorithm(); Return_Statment = ""; }