PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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