/Utilities/Tests/CompressionTests.cs
C# | 219 lines | 144 code | 22 blank | 53 comment | 1 complexity | bf586151bd16b8139cb9a38a606b1881 MD5 | raw file
Possible License(s): Apache-2.0
- using System;
- using System.Diagnostics;
- using System.IO;
- using Delta.Utilities.Compression;
- using Delta.Utilities.Helpers;
- using NUnit.Framework;
-
- namespace Delta.Utilities.Tests
- {
- internal class CompressionTests
- {
- #region PackAndUnpackTextStream (LongRunning)
- /// <summary>
- /// Pack and unpack text stream
- /// </summary>
- [Test, Category("LongRunning")]
- public static void PackAndUnpackTextStream()
- {
- // Just execute the existing test
- new Zip.ZipTests().PackAndUnpackTextStream();
- }
- #endregion
-
- #region PackAndUnpackZipFile (LongRunning)
- /// <summary>
- /// Pack and unpack zip file
- /// </summary>
- [Test, Category("LongRunning")]
- public static void PackAndUnpackZipFile()
- {
- const string DataFileName = "TestData.txt";
- const string PackedDataFileName = "PackedTestData.zip";
-
- // First clean up the (possible) rest of the last test call
- FileHelper.SafeDelete(DataFileName);
- FileHelper.SafeDelete(PackedDataFileName);
-
- // Define an arbitrary text we want to save into a packed file and
- // unpack it later again
- string fileData =
- // This text is 1029 bytes and will be compressed to 747 bytes
- "This is just a simple test message for the " +
- "'PackAndUnpackTextStream' unit test of Zip class in the '" +
- "Delta.Utilities.Compressions' assembly. Adding some spaces to go " +
- "over the 256 bytes limit (else compression will be disabled). " +
- "Now it's just some dummy data following to simply increase the " +
- "amount of data to compress for the current test because we need to " +
- "the minimum limit of at least 256 bytes (for the moment). " +
- "Otherwise if we would be below that border we would get a " +
- "warning in the log. 1. Forsaking monastic tradition, twelve " +
- "jovial friars gave up their vocation for a questionable " +
- "existence on the flying trapeze. 2. No kidding -- Lorenzo called " +
- "off his trip to visit Mexico City just because they told him the " +
- "conquistadores were extinct. 3. Waltz, nymph, for quick jigs vex " +
- "Bud. 4. Jelly-like above the high wire, six quaking pachyderms " +
- "kept the climax of the extravaganza in a dazzling state of flux. " +
- "Oh, wet Alex, a jar, a fag! Up, disk, curve by! Man Oz, Iraq, " +
- "Arizona, my Bev? Ruck's id-pug, a far Ajax, elate? Who?";
-
- // Test:
- //byte[] dataBytes = StringHelper.ToByteArray(fileData);
- //Log.Test("fileData has '" + dataBytes.Length +
- // "' bytes which is compressed '" + Zip.Compress(dataBytes).Length +
- // "' bytes big.");
-
- // Create the data file
- File.WriteAllText(DataFileName, fileData);
-
- // and check
- FileInfo dataFileInfo = new FileInfo(DataFileName);
- // that is was created successfully
- Assert.True(dataFileInfo.Exists);
- // and is also not 0 bytes big
- Assert.NotEqual(dataFileInfo.Length, 0);
- Log.Test("DataFile.SizeInBytes = '" + dataFileInfo.Length + "'.");
-
- // Now create the compressed version of it
- Assert.True(Zip.Compress(DataFileName, PackedDataFileName));
- // After that do the same file checks as above
- FileInfo packedDataFileInfo = new FileInfo(PackedDataFileName);
- Assert.True(packedDataFileInfo.Exists);
- Assert.NotEqual(packedDataFileInfo.Length, 0);
- Log.Test("PackedDataFile.SizeInBytes = '" + packedDataFileInfo.Length +
- "'.");
-
- // As last step try to unpack the packed file again
- byte[] uncompressedData = Zip.Decompress(PackedDataFileName);
- // and check if the data are still the same
- Assert.Equal(StringHelper.FromByteArray(uncompressedData), fileData);
- }
- #endregion
-
- #region ProfilePackAndUnpackTextMessage (LongRunning)
- /// <summary>
- /// Profile pack and unpack text stream
- ///
- /// Judge: Win7 x64, i7 920 @ 2,66 GHZ, 6 GB Ram, Intel G2 SSD 80 GB
- /// - 1000 loops = 775 ms
- /// </summary>
- [Test, Category("LongRunning")]
- public static void ProfilePackAndUnpackTextMessage()
- {
- // Loop for 1 thousand times.
- const int LoopCount = 1000;
-
- // Define an arbitrary text we want to compress and uncompress again
- string textMessage =
- // This text is 788 bytes and will be compressed to 598 bytes which
- // is nearly 32% smaller
- "This is just a simple test message for the " +
- "'PackAndUnpackTextStream' unit test of Zip class in the '" +
- "Delta.Utilities.Compressions' assembly. Adding some spaces to go " +
- "over the 256 bytes limit (else compression will be disabled). " +
- "Now it's just some dummy data following to simply increase the " +
- "amount of data to compress for the current test because we need to " +
- "the minimum limit of at least 256 bytes (for the moment). " +
- "Otherwise if we would be below that border we would get a " +
- "warning in the log. 1. Forsaking monastic tradition, twelve " +
- "jovial friars gave up their vocation for a questionable " +
- "existence on the flying trapeze. 2. No kidding -- Lorenzo called " +
- "off his trip to visit Mexico City just because they told him the " +
- "conquistadores were extinct. 3. Waltz, nymph, for quick jigs vex " +
- "Bud. " +
- " " +
- " " +
- " " +
- " " +
- " " +
- " ";
-
- // First split the text into byte packages
- byte[] originalData = StringHelper.ToByteArray(textMessage);
- // because we are only interested in the pure pack and unpack
- // performance
- byte[] compressedData = null;
- byte[] uncompressedData = null;
-
- long startTime = Stopwatch.GetTimestamp();
- for (int index = 0; index < LoopCount; index++)
- {
- compressedData = Zip.Compress(originalData);
- uncompressedData = Zip.Decompress(compressedData);
- } // for
- long endTime = Stopwatch.GetTimestamp();
-
- Log.Test(LoopCount + " loops of 'Zip.Compress()' and 'Zip.Decompress' " +
- "took " + ((endTime - startTime) * 1000 / Stopwatch.Frequency) +
- "ms");
-
- Assert.NotNull(uncompressedData);
- }
- #endregion
-
- #region PackAndUnpackZipFiles (LongRunning)
- /// <summary>
- /// Pack and unpack zip file
- /// </summary>
- [Test, Category("LongRunning")]
- public static void PackAndUnpackZipFiles()
- {
- // First define some (text) files which every (Windows) user should have,
- // for that some file in the system OS directory should be fine
- string[] systemFiles = new[]
- {
- "system.ini",
- "win.ini",
- };
-
- // Short test to make sure that the test runs on a Windows system
- string winDir = Environment.GetFolderPath(
- Environment.SpecialFolder.Windows);
- Assert.NotEqual(winDir, null);
-
- // Now create the zip file where the files will be packed
- const string PackedDataFileName = "PackedTestFiles.zip";
- using (ZipFile zipFile = ZipFile.Create(PackedDataFileName, true))
- {
- foreach (string filename in systemFiles)
- {
- string fullFilePath = Path.Combine(winDir, filename);
- // Every file have to exist before we can add it to the zip file
- Assert.True(FileHelper.Exists(fullFilePath));
- // We always need the full filepath for loading the data and the pure
- // filename to produce no senseless subfolder because of the full
- // path
- zipFile.Append(fullFilePath, filename);
- }
- }
-
- // The zip file should now be created, so do some sanity checks
- FileInfo packedDataFileInfo = new FileInfo(PackedDataFileName);
- Assert.True(packedDataFileInfo.Exists);
- Assert.NotEqual(packedDataFileInfo.Length, 0);
- Log.Test("PackedDataFile.SizeInBytes = '" + packedDataFileInfo.Length +
- "'.");
-
- // Last try to unpack the packed file again and check if the data is
- // still the same
- using (ZipFile zipFile = ZipFile.Open(PackedDataFileName))
- {
- foreach (string filename in systemFiles)
- {
- // Every entry must exists
- int entryIndex = zipFile.FindEntry(filename, false);
- Assert.NotEqual(entryIndex, MathHelper.InvalidIndex);
-
- Stream unpackedFileData = zipFile.GetInputStream(entryIndex);
- StreamReader dataReader = new StreamReader(unpackedFileData);
-
- // and the unpacked data should be the same as the source from above
- string text = dataReader.ReadToEnd();
- string absFilePath = Path.Combine(winDir, filename);
- Assert.Equal(text, FileHelper.GetText(absFilePath));
- }
- }
- }
- #endregion
- }
- }