From 9e232fe57a4e43b7e1c64de83a60680c322125d3 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 19 Nov 2019 14:29:28 +1100 Subject: [PATCH] Improve code coverage. --- src/ImageSharp/Formats/Png/Zlib/Deflater.cs | 14 ++---- .../Formats/Png/Zlib/DeflaterHuffman.cs | 47 ++++++++----------- .../Formats/Png/Zlib/DeflaterOutputStream.cs | 24 +++++----- .../Formats/Png/Zlib/ZlibDeflateStream.cs | 34 +++----------- .../Formats/Png/PngEncoderTests.cs | 2 +- 5 files changed, 41 insertions(+), 80 deletions(-) diff --git a/src/ImageSharp/Formats/Png/Zlib/Deflater.cs b/src/ImageSharp/Formats/Png/Zlib/Deflater.cs index fb2538f8c..6c4ea44d1 100644 --- a/src/ImageSharp/Formats/Png/Zlib/Deflater.cs +++ b/src/ImageSharp/Formats/Png/Zlib/Deflater.cs @@ -281,23 +281,15 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib /// public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize(this); - } - - private void Dispose(bool disposing) { if (!this.isDisposed) { - if (disposing) - { - this.engine.Dispose(); - } - + this.engine.Dispose(); this.engine = null; this.isDisposed = true; } + + GC.SuppressFinalize(this); } } } diff --git a/src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs b/src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs index 7118703d0..003e4fbb7 100644 --- a/src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs +++ b/src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs @@ -372,7 +372,25 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib /// public void Dispose() { - this.Dispose(true); + if (!this.isDisposed) + { + this.Pending.Dispose(); + this.distanceBufferHandle.Dispose(); + this.distanceManagedBuffer.Dispose(); + this.literalBufferHandle.Dispose(); + this.literalManagedBuffer.Dispose(); + + this.literalTree.Dispose(); + this.blTree.Dispose(); + this.distTree.Dispose(); + + this.Pending = null; + this.literalTree = null; + this.blTree = null; + this.distTree = null; + this.isDisposed = true; + } + GC.SuppressFinalize(this); } @@ -407,33 +425,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib return code + distance; } - private void Dispose(bool disposing) - { - if (!this.isDisposed) - { - if (disposing) - { - this.Pending.Dispose(); - this.distanceBufferHandle.Dispose(); - this.distanceManagedBuffer.Dispose(); - this.literalBufferHandle.Dispose(); - this.literalManagedBuffer.Dispose(); - - this.literalTree.Dispose(); - this.blTree.Dispose(); - this.distTree.Dispose(); - } - - this.Pending = null; - - this.literalTree = null; - this.blTree = null; - this.distTree = null; - - this.isDisposed = true; - } - } - private sealed class Tree : IDisposable { private readonly int minNumCodes; diff --git a/src/ImageSharp/Formats/Png/Zlib/DeflaterOutputStream.cs b/src/ImageSharp/Formats/Png/Zlib/DeflaterOutputStream.cs index eb214aae2..9eeb12cb0 100644 --- a/src/ImageSharp/Formats/Png/Zlib/DeflaterOutputStream.cs +++ b/src/ImageSharp/Formats/Png/Zlib/DeflaterOutputStream.cs @@ -134,22 +134,20 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib /// protected override void Dispose(bool disposing) { - if (this.isDisposed) + if (!this.isDisposed) { - return; - } + if (disposing) + { + this.Finish(); + this.deflater.Dispose(); + this.memoryOwner.Dispose(); + } - if (disposing) - { - this.Finish(); - this.deflater.Dispose(); - this.memoryOwner.Dispose(); + this.deflater = null; + this.memoryOwner = null; + this.isDisposed = true; + base.Dispose(disposing); } - - this.deflater = null; - this.memoryOwner = null; - this.isDisposed = true; - base.Dispose(disposing); } } } diff --git a/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs b/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs index 36bacc5ec..3c52d306f 100644 --- a/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs +++ b/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs @@ -120,28 +120,16 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib } /// - public override void Flush() - { - this.deflateStream?.Flush(); - } + public override void Flush() => this.deflateStream.Flush(); /// - public override int Read(byte[] buffer, int offset, int count) - { - throw new NotSupportedException(); - } + public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException(); /// - public override long Seek(long offset, SeekOrigin origin) - { - throw new NotSupportedException(); - } + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); /// - public override void SetLength(long value) - { - throw new NotSupportedException(); - } + public override void SetLength(long value) => throw new NotSupportedException(); /// public override void Write(byte[] buffer, int offset, int count) @@ -161,17 +149,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib if (disposing) { // dispose managed resources - if (this.deflateStream != null) - { - this.deflateStream.Dispose(); - this.deflateStream = null; - } - else - { - // Hack: empty input? - this.rawStream.WriteByte(3); - this.rawStream.WriteByte(0); - } + this.deflateStream.Dispose(); // Add the crc uint crc = (uint)this.adler32.Value; @@ -181,6 +159,8 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib this.rawStream.WriteByte((byte)(crc & 0xFF)); } + this.deflateStream = null; + base.Dispose(disposing); this.isDisposed = true; } diff --git a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs index 2584391bb..8a0cdbfba 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs @@ -63,7 +63,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png /// public static readonly TheoryData CompressionLevels = new TheoryData { - 1, 2, 3, 4, 5, 6, 7, 8, 9 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; public static readonly TheoryData PaletteSizes = new TheoryData