Пример #1
0
        public void Lz4Legacy_Flush()
        {
            // Verify behavior of flushing a compression stream
            using (MemoryStream compressed = new MemoryStream())
            {
                using (Lz4LegacyWriter stream = new Lz4LegacyWriter(compressed, true))
                {
                    stream.Write(s_sampledata, 0, s_sampledata.Length);

                    // Get the unflushed length of the compressed stream and flush it
                    long unflushed = compressed.Length;
                    stream.Flush();

                    // The expectation is that the output stream will be longer after the flush
                    long flushedonce = compressed.Length;
                    Assert.IsTrue(compressed.Length > unflushed);

                    // Flushing the same data a second time should not have any impact at all
                    stream.Flush();
                    Assert.AreEqual(compressed.Length, flushedonce);

                    // The stream should still be writable after a flush operation
                    stream.Write(s_sampledata, 0, s_sampledata.Length / 10);
                }
            }

            // Verify behavior of flushing a decompression stream
            using (Lz4LegacyReader stream = new Lz4LegacyReader(new MemoryStream(s_sampledata)))
            {
                // Flush has no effect on decompression streams, just ensure it doesn't throw
                stream.Flush();
            }
        }
Пример #2
0
        public void Lz4Legacy_Read()
        {
            byte[] buffer = new byte[8192];                     // 8KiB data buffer

            using (MemoryStream compressed = new MemoryStream())
            {
                // Start with a compressed MemoryStream created from the sample data
                using (Lz4LegacyWriter compressor = new Lz4LegacyWriter(compressed, CompressionLevel.Optimal, true))
                {
                    try { compressor.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }

                    compressor.Write(s_sampledata, 0, s_sampledata.Length);
                    compressor.Flush();
                }

                // Check the constructor for ArgumentNullException while we're here
                try { using (Lz4LegacyReader decompressor = new Lz4LegacyReader(null, false)) { }; Assert.Fail("Constructor should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

                // Create a decompressor to test some of the error cases
                using (Lz4LegacyReader decompressor = new Lz4LegacyReader(compressed, true))
                {
                    // Send in some bum arguments to Read() to check they are caught
                    try { decompressor.Read(null, 0, 0); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

                    try { decompressor.Read(buffer, -1, 0); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

                    try { decompressor.Read(buffer, 0, -1); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

                    try { decompressor.Read(buffer, 0, buffer.Length + 1024); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentException)); }

                    // Attempting to read from the end of the compressed stream should throw an InvalidDataException
                    try { decompressor.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(InvalidDataException)); }

                    // Attempting to read from the middle of the compressed stream should throw an InvalidDataException
                    compressed.Position = compressed.Position / 2;
                    try { decompressor.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(InvalidDataException)); }
                }

                // Create a new decompressor against the same stream and make sure it doesn't throw
                compressed.Position = 0;
                using (Lz4LegacyReader decompressor = new Lz4LegacyReader(compressed, true))
                {
                    // Reading zero bytes should not throw an exception
                    decompressor.Read(buffer, 0, 0);

                    while (decompressor.Read(buffer, 0, 8192) != 0)
                    {
                    }
                }
            }
        }
Пример #3
0
 public void Lz4Legacy_CompressExternal()
 {
     // This method generates an output file that can be tested externally; "thethreemusketeers.txt" is
     // set to Copy Always to the output directory, it can be diffed after running the external tool
     using (Lz4LegacyWriter writer = new Lz4LegacyWriter(File.Create(Path.Combine(Environment.CurrentDirectory, "thethreemusketeers.lz4-legacy"))))
     {
         writer.Write(s_sampledata);
         writer.Flush();
     }
 }
Пример #4
0
        public void Lz4Legacy_Write()
        {
            byte[] buffer = new byte[8192];                     // 8KiB data buffer

            // Compress the sample data using a call to Write directly
            using (MemoryStream compressed = new MemoryStream())
            {
                // Check the constructor for ArgumentNullException while we're here
                try { using (Lz4LegacyWriter compressor = new Lz4LegacyWriter(null)) { }; Assert.Fail("Constructor should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

                using (Lz4LegacyWriter compressor = new Lz4LegacyWriter(compressed, CompressionLevel.Optimal, true))
                {
                    // Send in some bum arguments to Write() to check they are caught
                    try { compressor.Write(null); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

                    try { compressor.Write(null, 0, 0); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

                    try { compressor.Write(s_sampledata, -1, 0); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

                    try { compressor.Write(s_sampledata, 0, -1); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

                    try { compressor.Write(s_sampledata, 0, s_sampledata.Length + 1024); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentException)); }

                    // Not writing anything shouldn't throw an exception
                    compressor.Write(s_sampledata, 0, 0);

                    // Compress the data; there really isn't much that can go wrong with Write() itself
                    compressor.Write(s_sampledata, 0, s_sampledata.Length);
                    compressor.Flush();
                }

                using (Lz4LegacyReader reader = new Lz4LegacyReader(compressed, true))
                {
                    try { reader.Write(buffer, 0, buffer.Length); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }
                }
            }
        }
Пример #5
0
        public void Lz4Legacy_CompressionLevel()
        {
            using (MemoryStream source = new MemoryStream(s_sampledata))
            {
                long fastest, optimal;                                  // Size of compressed streams

                // CompressionLevel.NoCompression is not valid
                try { using (Lz4LegacyWriter stream = new Lz4LegacyWriter(source, CompressionLevel.NoCompression)) Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

                // Compress using CompressionLevel.Fastest
                using (MemoryStream compressed = new MemoryStream())
                {
                    using (Lz4LegacyWriter compressor = new Lz4LegacyWriter(compressed, CompressionLevel.Fastest))
                    {
                        compressor.Write(s_sampledata, 0, s_sampledata.Length);
                        compressor.Flush();
                        fastest = compressed.Length;
                    }
                }

                // Compress using CompressionLevel.Optimal
                using (MemoryStream compressed = new MemoryStream())
                {
                    using (Lz4LegacyWriter compressor = new Lz4LegacyWriter(compressed, CompressionLevel.Optimal))
                    {
                        compressor.Write(s_sampledata, 0, s_sampledata.Length);
                        compressor.Flush();
                        optimal = compressed.Length;
                    }
                }

                // Optimal compression should result in a smaller output stream than fastest
                // and fastest should result in a smaller output stream than uncompressed
                Assert.IsTrue(optimal < fastest);
                Assert.IsTrue(fastest < source.Length);
            }
        }
Пример #6
0
        public void Lz4Legacy_WriterDispose()
        {
            byte[] buffer = new byte[8192];                     // 8KiB data buffer

            // Create a dummy stream and immediately dispose of it
            Lz4LegacyWriter stream = new Lz4LegacyWriter(new MemoryStream(s_sampledata), CompressionLevel.Optimal);

            stream.Dispose();

            // Test double dispose
            stream.Dispose();

            // All properties and methods should throw an ObjectDisposedException
            try { var bs = stream.BaseStream; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { var b = stream.CanRead; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { var b = stream.CanSeek; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { var b = stream.CanWrite; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Flush(); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { var l = stream.Length; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { var l = stream.Position; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Position = 12345L; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Seek(0, SeekOrigin.Current); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.SetLength(12345L); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Write(buffer); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Write(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            // Ensure that an underlying stream is disposed of properly if leaveopen is not set
            MemoryStream ms = new MemoryStream(s_sampledata);

            using (Lz4LegacyWriter compressor = new Lz4LegacyWriter(ms, CompressionLevel.Fastest)) { }
            try { ms.Write(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            // Ensure that an underlying stream is not disposed of if leaveopen is set
            ms = new MemoryStream(s_sampledata);
            using (Lz4LegacyWriter compressor = new Lz4LegacyWriter(ms, CompressionLevel.Fastest, true)) { }
            ms.Write(buffer, 0, 8192);
            ms.Dispose();
        }