public void CompressUncompressEmpty() { var compressed = CompressTool.Compress(new byte[0]); Assert.IsTrue(compressed.Length > 0); Assert.AreEqual(0, CompressTool.Uncompress(compressed).Length); }
public void CompressUncompressTest() { var data = _dataCreator.GenerateTestData(1024 * 1024); var compressed = CompressTool.Compress(data); Assert.IsTrue(compressed.Length > 0); Assert.IsTrue(data.SequenceEqual(CompressTool.Uncompress(compressed))); }
public void CompressRange() { var input = Encoding.ASCII.GetBytes("ByeHelloBye"); var output = new byte[100]; var length = CompressTool.Compress(input, 3, 5, output, 10); Assert.AreEqual("Hello", Encoding.ASCII.GetString(CompressTool.Uncompress(output.Skip(10).Take(length).ToArray()))); }
public void UncompressRange() { var howdy = Encoding.ASCII.GetBytes("Howdy"); var padded = howdy.Take(3).Concat(CompressTool.Compress(Encoding.ASCII.GetBytes("Hello"))).Concat(howdy.Skip(3)).ToArray(); var output = new byte[100]; var length = CompressTool.Uncompress(padded, 3, padded.Length - 5, output, 10); Assert.AreEqual(5, length); Assert.AreEqual("Hello", Encoding.ASCII.GetString(output.Skip(10).Take(5).ToArray())); }
private void DoUncompressionBenchmark(int size) { Benchmark.Run("Uncompressing", size, benchmark => { var compressed = CompressTool.Compress(benchmark.Input); var roundtrip = new byte[benchmark.Input.Length]; int length = 0; benchmark.Stopwatch.Start(); for (int i = 0; i < benchmark.Iterations; ++i) { length = CompressTool.Uncompress(compressed, 0, compressed.Length, roundtrip, 0); } benchmark.Stopwatch.Stop(); CollectionAssert.AreEqual(benchmark.Input, roundtrip); }); }
private void DoCompressionBenchmark(int size) { Benchmark.Run("Compressing", size, benchmark => { var output = new byte[CompressTool.GetMaxCompressedLength(benchmark.Input.Length)]; int length = 0; benchmark.Stopwatch.Start(); for (int i = 0; i < benchmark.Iterations; ++i) { length = CompressTool.Compress(benchmark.Input, 0, benchmark.Input.Length, output, 0); } benchmark.Stopwatch.Stop(); var roundtrip = new byte[benchmark.Input.Length]; var roundtripLength = CompressTool.Uncompress(output, 0, length, roundtrip, 0); Assert.IsTrue(benchmark.Input.SequenceEqual(roundtrip.Take(roundtripLength))); benchmark.Note = String.Format(" ({0:0.00 %})", length / (double)benchmark.Input.Length); }); }
private Task DoAsyncUncompressionOneParaBenchmark(int size) { return(Task.Factory.StartNew(() => { Benchmark.Run("Uncompressing", size, benchmark => { var compressed = CompressTool.Compress(benchmark.Input); var roundtrip = new byte[benchmark.Input.Length]; int length = 0; benchmark.Stopwatch.Start(); for (int i = 0; i < benchmark.Iterations; ++i) { roundtrip = CompressTool.Uncompress(compressed); length = roundtrip.Length; } benchmark.Stopwatch.Stop(); CollectionAssert.AreEqual(benchmark.Input, roundtrip); }); })); }
private Task DoAsyncCompressionOneParaBenchmark(int size) { return(Task.Factory.StartNew(() => { Benchmark.Run("Compressing", size, benchmark => { var output = new byte[CompressTool.GetMaxCompressedLength(benchmark.Input.Length)]; int length = 0; benchmark.Stopwatch.Start(); for (int i = 0; i < benchmark.Iterations; ++i) { output = CompressTool.Compress(benchmark.Input); length = output.Length; } benchmark.Stopwatch.Stop(); var roundtrip = CompressTool.Uncompress(output); Assert.IsTrue(benchmark.Input.SequenceEqual(roundtrip)); benchmark.Note = String.Format(" ({0:0.00 %})", length / (double)benchmark.Input.Length); }); })); }
public void UncompressExceptions() { var uncompressed = Encoding.ASCII.GetBytes("Hello, hello, howdy?"); var compressed = CompressTool.Compress(uncompressed); var buffer = new byte[100]; TestException(() => { CompressTool.Uncompress(null); }, typeof(ArgumentNullException)); TestException(() => { CompressTool.Uncompress(null, 0, 3, buffer, 0); }, typeof(ArgumentNullException)); TestException(() => { CompressTool.Uncompress(compressed, 0, compressed.Length, null, 0); }, typeof(ArgumentNullException)); TestException(() => { CompressTool.Uncompress(compressed, -1, uncompressed.Length, buffer, 0); }, typeof(ArgumentOutOfRangeException)); TestException(() => { CompressTool.Uncompress(compressed, 0, -1, buffer, 0); }, typeof(ArgumentOutOfRangeException)); TestException(() => { CompressTool.Uncompress(compressed, compressed.Length - 2, 4, buffer, 0); }, typeof(ArgumentOutOfRangeException)); TestException(() => { CompressTool.Uncompress(compressed, 0, 0, buffer, 0); }, typeof(System.IO.IOException)); TestException(() => { CompressTool.Uncompress(compressed, compressed.Length, 0, buffer, 0); }, typeof(System.IO.IOException)); TestException(() => { CompressTool.Uncompress(compressed, 0, compressed.Length, buffer, -1); }, typeof(ArgumentOutOfRangeException)); TestException(() => { CompressTool.Uncompress(compressed, 0, compressed.Length, buffer, 101); }, typeof(ArgumentOutOfRangeException)); TestException(() => { CompressTool.Uncompress(compressed, 0, compressed.Length, buffer, 97); }, typeof(ArgumentOutOfRangeException)); }
/// <summary> /// Retrieves data from the frame. Data is uncompressed before being stored in the output buffer. /// CRC of the data is checked and CRC failure results in exception. /// </summary> /// <param name="buffer">Output buffer where uncompressed data is stored. Buffer length minus offset must be at least DataLength bytes.</param> /// <param name="offset">Offset into the output buffer where uncompressed data will be stored.</param> public void GetData(byte[] buffer, int offset) { if (Type == SnappyFrameType.Compressed) { var count = CompressTool.Uncompress(Buffer, 0, BufferUsage, buffer, offset); if (ComputeMasked(buffer, offset, count) != Checksum) { throw new InvalidDataException(); } } else if (Type == SnappyFrameType.Uncompressed) { if (ComputeMasked(Buffer, offset, BufferUsage) != Checksum) { throw new InvalidDataException(); } Array.Copy(Buffer, 0, buffer, offset, BufferUsage); } else { throw new InvalidOperationException(); } }
public void CompressUncompressSimple() { Assert.AreEqual("Hello", Encoding.ASCII.GetString( CompressTool.Uncompress(CompressTool.Compress(Encoding.ASCII.GetBytes("Hello"))))); }