PageRenderTime 124ms CodeModel.GetById 61ms app.highlight 9ms RepoModel.GetById 51ms app.codeStats 0ms

/Utilities/Tests/CompressionTests.cs

#
C# | 219 lines | 144 code | 22 blank | 53 comment | 1 complexity | bf586151bd16b8139cb9a38a606b1881 MD5 | raw file
  1using System;
  2using System.Diagnostics;
  3using System.IO;
  4using Delta.Utilities.Compression;
  5using Delta.Utilities.Helpers;
  6using NUnit.Framework;
  7
  8namespace Delta.Utilities.Tests
  9{
 10	internal class CompressionTests
 11	{
 12		#region PackAndUnpackTextStream (LongRunning)
 13		/// <summary>
 14		/// Pack and unpack text stream
 15		/// </summary>
 16		[Test, Category("LongRunning")]
 17		public static void PackAndUnpackTextStream()
 18		{
 19			// Just execute the existing test
 20			new Zip.ZipTests().PackAndUnpackTextStream();
 21		}
 22		#endregion
 23
 24		#region PackAndUnpackZipFile (LongRunning)
 25		/// <summary>
 26		/// Pack and unpack zip file
 27		/// </summary>
 28		[Test, Category("LongRunning")]
 29		public static void PackAndUnpackZipFile()
 30		{
 31			const string DataFileName = "TestData.txt";
 32			const string PackedDataFileName = "PackedTestData.zip";
 33
 34			// First clean up the (possible) rest of the last test call
 35			FileHelper.SafeDelete(DataFileName);
 36			FileHelper.SafeDelete(PackedDataFileName);
 37
 38			// Define an arbitrary text we want to save into a packed file and
 39			// unpack it later again
 40			string fileData =
 41				// This text is 1029 bytes and will be compressed to 747 bytes
 42				"This is just a simple test message for the " +
 43				"'PackAndUnpackTextStream' unit test of Zip class in the '" +
 44				"Delta.Utilities.Compressions' assembly. Adding some spaces to go " +
 45				"over the 256 bytes limit (else compression will be disabled). " +
 46				"Now it's just some dummy data following to simply increase the " +
 47				"amount of data to compress for the current test because we need to " +
 48				"the minimum limit of at least 256 bytes (for the moment). " +
 49				"Otherwise if we would be below that border we would get a " +
 50				"warning in the log. 1. Forsaking monastic tradition, twelve " +
 51				"jovial friars gave up their vocation for a questionable " +
 52				"existence on the flying trapeze. 2. No kidding -- Lorenzo called " +
 53				"off his trip to visit Mexico City just because they told him the " +
 54				"conquistadores were extinct. 3. Waltz, nymph, for quick jigs vex " +
 55				"Bud. 4. Jelly-like above the high wire, six quaking pachyderms " +
 56				"kept the climax of the extravaganza in a dazzling state of flux. " +
 57				"Oh, wet Alex, a jar, a fag! Up, disk, curve by! Man Oz, Iraq, " +
 58				"Arizona, my Bev? Ruck's id-pug, a far Ajax, elate? Who?";
 59
 60			// Test:
 61			//byte[] dataBytes = StringHelper.ToByteArray(fileData);
 62			//Log.Test("fileData has '" + dataBytes.Length +
 63			//  "' bytes which is compressed '" + Zip.Compress(dataBytes).Length +
 64			//  "' bytes big.");
 65
 66			// Create the data file
 67			File.WriteAllText(DataFileName, fileData);
 68
 69			// and check 
 70			FileInfo dataFileInfo = new FileInfo(DataFileName);
 71			// that is was created successfully
 72			Assert.True(dataFileInfo.Exists);
 73			// and is also not 0 bytes big
 74			Assert.NotEqual(dataFileInfo.Length, 0);
 75			Log.Test("DataFile.SizeInBytes = '" + dataFileInfo.Length + "'.");
 76
 77			// Now create the compressed version of it
 78			Assert.True(Zip.Compress(DataFileName, PackedDataFileName));
 79			// After that do the same file checks as above
 80			FileInfo packedDataFileInfo = new FileInfo(PackedDataFileName);
 81			Assert.True(packedDataFileInfo.Exists);
 82			Assert.NotEqual(packedDataFileInfo.Length, 0);
 83			Log.Test("PackedDataFile.SizeInBytes = '" + packedDataFileInfo.Length +
 84			         "'.");
 85
 86			// As last step try to unpack the packed file again
 87			byte[] uncompressedData = Zip.Decompress(PackedDataFileName);
 88			// and check if the data are still the same
 89			Assert.Equal(StringHelper.FromByteArray(uncompressedData), fileData);
 90		}
 91		#endregion
 92
 93		#region ProfilePackAndUnpackTextMessage (LongRunning)
 94		/// <summary>
 95		/// Profile pack and unpack text stream
 96		/// 
 97		/// Judge: Win7 x64, i7 920 @ 2,66 GHZ, 6 GB Ram, Intel G2 SSD 80 GB
 98		/// - 1000 loops = 775 ms
 99		/// </summary>
100		[Test, Category("LongRunning")]
101		public static void ProfilePackAndUnpackTextMessage()
102		{
103			// Loop for 1 thousand times.
104			const int LoopCount = 1000;
105
106			// Define an arbitrary text we want to compress and uncompress again
107			string textMessage =
108				// This text is 788 bytes and will be compressed to 598 bytes which
109				// is nearly 32% smaller
110				"This is just a simple test message for the " +
111				"'PackAndUnpackTextStream' unit test of Zip class in the '" +
112				"Delta.Utilities.Compressions' assembly. Adding some spaces to go " +
113				"over the 256 bytes limit (else compression will be disabled). " +
114				"Now it's just some dummy data following to simply increase the " +
115				"amount of data to compress for the current test because we need to " +
116				"the minimum limit of at least 256 bytes (for the moment). " +
117				"Otherwise if we would be below that border we would get a " +
118				"warning in the log. 1. Forsaking monastic tradition, twelve " +
119				"jovial friars gave up their vocation for a questionable " +
120				"existence on the flying trapeze. 2. No kidding -- Lorenzo called " +
121				"off his trip to visit Mexico City just because they told him the " +
122				"conquistadores were extinct. 3. Waltz, nymph, for quick jigs vex " +
123				"Bud.                                                             " +
124				"                                                                 " +
125				"                                                                 " +
126				"                                                                 " +
127				"                                                                 " +
128				"                                                                 " +
129				"                                                                 ";
130
131			// First split the text into byte packages
132			byte[] originalData = StringHelper.ToByteArray(textMessage);
133			// because we are only interested in the pure pack and unpack
134			// performance
135			byte[] compressedData = null;
136			byte[] uncompressedData = null;
137
138			long startTime = Stopwatch.GetTimestamp();
139			for (int index = 0; index < LoopCount; index++)
140			{
141				compressedData = Zip.Compress(originalData);
142				uncompressedData = Zip.Decompress(compressedData);
143			} // for
144			long endTime = Stopwatch.GetTimestamp();
145
146			Log.Test(LoopCount + " loops of 'Zip.Compress()' and 'Zip.Decompress' " +
147			         "took " + ((endTime - startTime) * 1000 / Stopwatch.Frequency) +
148			         "ms");
149
150			Assert.NotNull(uncompressedData);
151		}
152		#endregion
153
154		#region PackAndUnpackZipFiles (LongRunning)
155		/// <summary>
156		/// Pack and unpack zip file
157		/// </summary>
158		[Test, Category("LongRunning")]
159		public static void PackAndUnpackZipFiles()
160		{
161			// First define some (text) files which every (Windows) user should have,
162			// for that some file in the system OS directory should be fine
163			string[] systemFiles = new[]
164			{
165				"system.ini",
166				"win.ini",
167			};
168
169			// Short test to make sure that the test runs on a Windows system
170			string winDir = Environment.GetFolderPath(
171				Environment.SpecialFolder.Windows);
172			Assert.NotEqual(winDir, null);
173
174			// Now create the zip file where the files will be packed
175			const string PackedDataFileName = "PackedTestFiles.zip";
176			using (ZipFile zipFile = ZipFile.Create(PackedDataFileName, true))
177			{
178				foreach (string filename in systemFiles)
179				{
180					string fullFilePath = Path.Combine(winDir, filename);
181					// Every file have to exist before we can add it to the zip file
182					Assert.True(FileHelper.Exists(fullFilePath));
183					// We always need the full filepath for loading the data and the pure
184					// filename to produce no senseless subfolder because of the full
185					// path
186					zipFile.Append(fullFilePath, filename);
187				}
188			}
189
190			// The zip file should now be created, so do some sanity checks
191			FileInfo packedDataFileInfo = new FileInfo(PackedDataFileName);
192			Assert.True(packedDataFileInfo.Exists);
193			Assert.NotEqual(packedDataFileInfo.Length, 0);
194			Log.Test("PackedDataFile.SizeInBytes = '" + packedDataFileInfo.Length +
195			         "'.");
196
197			// Last try to unpack the packed file again and check if the data is
198			// still the same
199			using (ZipFile zipFile = ZipFile.Open(PackedDataFileName))
200			{
201				foreach (string filename in systemFiles)
202				{
203					// Every entry must exists
204					int entryIndex = zipFile.FindEntry(filename, false);
205					Assert.NotEqual(entryIndex, MathHelper.InvalidIndex);
206
207					Stream unpackedFileData = zipFile.GetInputStream(entryIndex);
208					StreamReader dataReader = new StreamReader(unpackedFileData);
209
210					// and the unpacked data should be the same as the source from above
211					string text = dataReader.ReadToEnd();
212					string absFilePath = Path.Combine(winDir, filename);
213					Assert.Equal(text, FileHelper.GetText(absFilePath));
214				}
215			}
216		}
217		#endregion
218	}
219}