public void CompressAndDecompressShouldBeAssociativeOperations() { // Given var compressor = new GZipDataCompressor(); var originalData = new MemoryStream(new byte[] { 1, 2, 3 }); // When var compressSource1 = originalData; var compressDestination1 = new MemoryStream(); compressor.Compress(compressSource1, compressDestination1); var decompressSource1 = compressDestination1; var decompressDestination1 = new MemoryStream(); compressor.Decompress(decompressSource1, decompressDestination1); var compressSource2 = decompressDestination1; var compressDestination2 = new MemoryStream(); compressor.Compress(compressSource2, compressDestination2); var decompressSource2 = compressDestination2; var decompressDestination2 = new MemoryStream(); compressor.Decompress(decompressSource2, decompressDestination2); // Then CollectionAssert.AreEquivalent(originalData.ToArray(), decompressDestination1.ToArray()); CollectionAssert.AreEquivalent(originalData.ToArray(), decompressDestination2.ToArray()); CollectionAssert.AreEquivalent(compressDestination1.ToArray(), compressDestination2.ToArray()); }
public void GZipCompressorTest(int len) { var compressor = new GZipDataCompressor(); var str = new string('a', len); var bytes = Encoding.UTF8.GetBytes(str); var compressedBytes = compressor.Compress(bytes); var decompressedBytes = compressor.Decompress(compressedBytes); var text = Encoding.UTF8.GetString(decompressedBytes); Assert.Equal(str, text); }
public void DecompressPerformanceTest(long size, int iterations) { // Given var compressor = new GZipDataCompressor(); var stopwatch = new Stopwatch(); var compressSource = new byte[size]; new Random(DateTime.Now.Millisecond).NextBytes(compressSource); var compressSourceStream = new MemoryStream(compressSource); var decompressSourceStream = new MemoryStream(); compressor.Compress(compressSourceStream, decompressSourceStream); // When compressor.Decompress(new MemoryStream(), new MemoryStream()); // JIT for (var i = 0; i < iterations; ++i) { using (var decompressResultStream = new MemoryStream()) { stopwatch.Start(); compressor.Decompress(decompressSourceStream, decompressResultStream); stopwatch.Stop(); } } // Then Console.WriteLine("Size: {0:N0} Kb", size / 1024.0); Console.WriteLine("Iterations: {0} times", iterations); Console.WriteLine("Compress speed: {0:N4} Kbytes/sec", (size * iterations / 1024.0) / stopwatch.Elapsed.TotalSeconds); }
public void DecompressShouldNotCloseSourceStream() { // Given var source = new MemoryStream(); var compressor = new GZipDataCompressor(); // When TestDelegate decompressSourceStreamTwice = () => { var destination1 = new MemoryStream(); compressor.Decompress(source, destination1); var destination2 = new MemoryStream(); compressor.Decompress(source, destination2); }; // Then Assert.DoesNotThrow(decompressSourceStreamTwice); }
public void DecompressResultShouldBeImmutable() { // Given var source = new MemoryStream(); var compressor = new GZipDataCompressor(); // When compressor.Compress(new MemoryStream(new byte[] { 1, 2, 3 }), source); source.Position = 0; var destination1 = new MemoryStream(); compressor.Decompress(source, destination1); var destination2 = new MemoryStream(); compressor.Decompress(source, destination2); // Then CollectionAssert.AreEquivalent(destination1.ToArray(), destination2.ToArray()); }
public void CanDecompressEmptyStream() { // Given var source = new MemoryStream(); var compressor = new GZipDataCompressor(); // When var destination = new MemoryStream(); TestDelegate decompressEmptyStream = () => compressor.Decompress(source, destination); // Then Assert.DoesNotThrow(decompressEmptyStream); Assert.AreEqual(0, destination.Length); }
public void DecompressMemoryTest(long size, int iterations) { // Given var compressor = new GZipDataCompressor(); var compressSource = new byte[size]; new Random(DateTime.Now.Millisecond).NextBytes(compressSource); var compressSourceStream = new MemoryStream(compressSource); var decompressSourceStream = new MemoryStream(); compressor.Compress(compressSourceStream, decompressSourceStream); // When var memoryBefore = GC.GetTotalMemory(true); for (var i = 0; i < iterations; ++i) { using (var decompressResultStream = new MemoryStream()) { compressor.Decompress(decompressSourceStream, decompressResultStream); } } GC.Collect(); GC.WaitForPendingFinalizers(); var memoryAfter = GC.GetTotalMemory(true); // Then var memoryLeak = memoryAfter - memoryBefore; var memeryLeakFactor = (double)memoryLeak / size; Console.WriteLine(@"Size: {0:N0} Kb", size / 1024.0); Console.WriteLine(@"Iterations: {0} times", iterations); Console.WriteLine(@"Memory leak: {0:N4} Kb", memoryLeak / 1024.0); Console.WriteLine(@"Memory leak factor: {0:N4}", memeryLeakFactor); Assert.Less(memeryLeakFactor, 2.5); }
public void DecompressShouldNotCloseDestinationStream() { // Given var source = new MemoryStream(); var compressor = new GZipDataCompressor(); // When compressor.Compress(new MemoryStream(new byte[] { 1, 2, 3 }), source); source.Position = 0; var destination = new MemoryStream(); compressor.Decompress(source, destination); // Then Assert.IsTrue(destination.CanSeek); Assert.IsTrue(destination.CanRead); Assert.IsTrue(destination.CanWrite); }