static private void checkCompress(String filename) { CompressionPlugin.CompressionPlugin classe = new CompressionPlugin.CompressionPlugin(); byte[] data = File.ReadAllBytes(filename); List <KeyValuePair <byte, int> > frequency = CompressionPlugin.CompressionPlugin.frequency(data); Node treeTop = CompressionPlugin.CompressionPlugin.createBinaryTree(frequency); classe.createDictionary(treeTop, new List <bool>()); byte[] compressedData = classe.storeContentToByteArray(data); int sizeofUnCompressData = data.Count(); // Clear dictionary for (int i = 0; i < 256; i++) { classe.dictionary[i] = null; } Node treeTop2 = CompressionPlugin.CompressionPlugin.createBinaryTree(frequency); classe.createDictionary(treeTop2, new List <bool>()); byte[] dataDecompressed = CompressionPlugin.CompressionPlugin.decodeBitArray(new BitArray(compressedData), ref treeTop2, ref sizeofUnCompressData); Debug.Assert(data.Count() == dataDecompressed.Count(), "La taille de l'entrée ne correspond pas à celle de la sortie"); for (int i = 0; i < data.Count(); i++) { Debug.Assert(data[i] == dataDecompressed[i], "Erreur dans la tout le programme - checkCompress"); } File.WriteAllBytes(@"E:\TestfrequencyResult.txt", dataDecompressed); }
static private void checkDictionary2() { // Create and intiate an awesome List<KeyValuePair> List <KeyValuePair <byte, int> > listPair = new List <KeyValuePair <byte, int> >(); listPair.Add(new KeyValuePair <byte, int>(0, 5)); listPair.Add(new KeyValuePair <byte, int>(1, 5)); listPair.Add(new KeyValuePair <byte, int>(2, 5)); listPair.Add(new KeyValuePair <byte, int>(3, 5)); listPair.Add(new KeyValuePair <byte, int>(4, 5)); Node treeTop = CompressionPlugin.CompressionPlugin.createBinaryTree(listPair); CompressionPlugin.CompressionPlugin classe = new CompressionPlugin.CompressionPlugin(); classe.createDictionary(treeTop, new List <bool>()); // Something wrong with this List <bool>[] dictionary = classe.dictionary; int k = 0; for (int i = 0; i < 256; i++) { if (dictionary[i] != null) { k++; } } Debug.Assert(k == listPair.Count(), "Erreur dans la création du dico"); }
/***************************************************************************************************************/ static private void benchmark(String filename) { CompressionPlugin.CompressionPlugin classe = new CompressionPlugin.CompressionPlugin(); byte[] data = File.ReadAllBytes(filename); // Begin benchmark Compress Stopwatch s1 = Stopwatch.StartNew(); List <KeyValuePair <byte, int> > frequency = CompressionPlugin.CompressionPlugin.frequency(data); Node treeTop = CompressionPlugin.CompressionPlugin.createBinaryTree(frequency); classe.createDictionary(treeTop, new List <bool>()); byte[] compressedData = classe.storeContentToByteArray(data); int sizeofUncompressData = data.Count(); s1.Stop(); // end benchmark // Clear dictionary for (int i = 0; i < 256; i++) { classe.dictionary[i] = null; } // Begin benchmark Decompress Stopwatch s2 = Stopwatch.StartNew(); Node treeTop2 = CompressionPlugin.CompressionPlugin.createBinaryTree(frequency); classe.createDictionary(treeTop2, new List <bool>()); byte[] dataDecompressed = CompressionPlugin.CompressionPlugin.decodeBitArray(new BitArray(compressedData), ref treeTop2, ref sizeofUncompressData); s2.Stop(); // End benchmark Console.WriteLine("Compression :"); Console.Write(" Durée du test :"); Console.Write(s1.ElapsedMilliseconds); Console.WriteLine(" ms"); Console.WriteLine("Décompression :"); Console.Write(" Durée du test :"); Console.Write(s2.ElapsedMilliseconds); Console.WriteLine(" ms"); Console.Write(" Durée total du test :"); Console.Write(s2.ElapsedMilliseconds + s1.ElapsedMilliseconds); Console.WriteLine(" ms"); Console.Write(" Pourcentage de compression :"); Console.WriteLine(1 - (float)compressedData.Count() / (float)data.Count()); }
/***************************************************************************************************************/ static private void checkstoreContentToByteArray() { byte[] a = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4 }; // Create and intiate an awesome List<KeyValuePair> List <KeyValuePair <byte, int> > listPair = new List <KeyValuePair <byte, int> >(); listPair.Add(new KeyValuePair <byte, int>(0, 5)); listPair.Add(new KeyValuePair <byte, int>(1, 5)); listPair.Add(new KeyValuePair <byte, int>(2, 5)); listPair.Add(new KeyValuePair <byte, int>(3, 5)); listPair.Add(new KeyValuePair <byte, int>(4, 5)); Node treeTop = CompressionPlugin.CompressionPlugin.createBinaryTree(listPair); CompressionPlugin.CompressionPlugin classe = new CompressionPlugin.CompressionPlugin(); classe.createDictionary(treeTop, new List <bool>()); List <bool>[] dictionary = classe.dictionary; byte[] res = classe.storeContentToByteArray(a); Debug.Assert(res.Count() == 8, "Erreur dans sotreContentToByteArray"); // Taille de la bitArray = 60, 60/8 = 7.5 donc 60%8 différent de 0 donc 60/8 = 7 + 1 = 8 ! }
/***************************************************************************************************************/ static private void checkDictionary1() { // Create and intiate an awesome List<KeyValuePair> List <KeyValuePair <byte, int> > listPair = new List <KeyValuePair <byte, int> >(); listPair.Add(new KeyValuePair <byte, int>(0, 5)); listPair.Add(new KeyValuePair <byte, int>(1, 5)); listPair.Add(new KeyValuePair <byte, int>(2, 5)); listPair.Add(new KeyValuePair <byte, int>(3, 5)); listPair.Add(new KeyValuePair <byte, int>(4, 5)); Node treeTop = CompressionPlugin.CompressionPlugin.createBinaryTree(listPair); CompressionPlugin.CompressionPlugin classe = new CompressionPlugin.CompressionPlugin(); classe.createDictionary(treeTop, new List <bool>()); // Something wrong with this List <bool>[] dictionary = classe.dictionary; Debug.Assert(dictionary[0][0] && !dictionary[0][1], "Erreur dans la création du dico"); Debug.Assert(dictionary[1][0] && dictionary[1][1] && dictionary[1][2], "Erreur dans la création du dico"); // Check for 0 Debug.Assert(dictionary[2][0] && dictionary[2][1] && !dictionary[2][2], "Erreur dans la création du dico"); Debug.Assert(!dictionary[3][0] && dictionary[3][1], "Erreur dans la création du dico"); Debug.Assert(!dictionary[4][0] && !dictionary[4][1], "Erreur dans la création du dico"); }
static void Main(string[] args) { CompressionPlugin.CompressionPlugin classe = new CompressionPlugin.CompressionPlugin(); // Test Frequency checkFrequency1(); checkFrequency2(); checkFrequency3(@"E:\Text.txt"); // Test Minimum checkMinimum1(); checkMinimum2(); checkMinimum3(); checkMinimum4(); // test Tree checkTree1(); checkTree2(); checkTree3(); // test Dictionnary checkDictionary1(); checkDictionary2(); // test storeContentToByteArray checkstoreContentToByteArray(); // test compress checkDecompress1(); checkDecompress2(); checkCompress(@"E:\Text.txt"); // Benchmark benchmark(@"E:\Text.txt"); //benchmark(@"E:\amd-catalyst-14-9-win7-win8.1-64bit-dd-ccc-whql.exe"); }