Browse Source

Improve code coverage.

pull/1054/head
James Jackson-South 7 years ago
parent
commit
ab93b0a497
  1. 14
      src/ImageSharp/Formats/Png/Zlib/Deflater.cs
  2. 47
      src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs
  3. 24
      src/ImageSharp/Formats/Png/Zlib/DeflaterOutputStream.cs
  4. 34
      src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs
  5. 2
      tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs

14
src/ImageSharp/Formats/Png/Zlib/Deflater.cs

@ -281,23 +281,15 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <inheritdoc/> /// <inheritdoc/>
public void Dispose() public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{ {
if (!this.isDisposed) if (!this.isDisposed)
{ {
if (disposing) this.engine.Dispose();
{
this.engine.Dispose();
}
this.engine = null; this.engine = null;
this.isDisposed = true; this.isDisposed = true;
} }
GC.SuppressFinalize(this);
} }
} }
} }

47
src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs

@ -372,7 +372,25 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <inheritdoc/> /// <inheritdoc/>
public void Dispose() 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); GC.SuppressFinalize(this);
} }
@ -407,33 +425,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
return code + distance; 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 sealed class Tree : IDisposable
{ {
private readonly int minNumCodes; private readonly int minNumCodes;

24
src/ImageSharp/Formats/Png/Zlib/DeflaterOutputStream.cs

@ -134,22 +134,20 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <inheritdoc/> /// <inheritdoc/>
protected override void Dispose(bool disposing) 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.deflater = null;
{ this.memoryOwner = null;
this.Finish(); this.isDisposed = true;
this.deflater.Dispose(); base.Dispose(disposing);
this.memoryOwner.Dispose();
} }
this.deflater = null;
this.memoryOwner = null;
this.isDisposed = true;
base.Dispose(disposing);
} }
} }
} }

34
src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs

@ -120,28 +120,16 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
} }
/// <inheritdoc/> /// <inheritdoc/>
public override void Flush() public override void Flush() => this.deflateStream.Flush();
{
this.deflateStream?.Flush();
}
/// <inheritdoc/> /// <inheritdoc/>
public override int Read(byte[] buffer, int offset, int count) public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
{
throw new NotSupportedException();
}
/// <inheritdoc/> /// <inheritdoc/>
public override long Seek(long offset, SeekOrigin origin) public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
{
throw new NotSupportedException();
}
/// <inheritdoc/> /// <inheritdoc/>
public override void SetLength(long value) public override void SetLength(long value) => throw new NotSupportedException();
{
throw new NotSupportedException();
}
/// <inheritdoc/> /// <inheritdoc/>
public override void Write(byte[] buffer, int offset, int count) public override void Write(byte[] buffer, int offset, int count)
@ -161,17 +149,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
if (disposing) if (disposing)
{ {
// dispose managed resources // dispose managed resources
if (this.deflateStream != null) this.deflateStream.Dispose();
{
this.deflateStream.Dispose();
this.deflateStream = null;
}
else
{
// Hack: empty input?
this.rawStream.WriteByte(3);
this.rawStream.WriteByte(0);
}
// Add the crc // Add the crc
uint crc = (uint)this.adler32.Value; uint crc = (uint)this.adler32.Value;
@ -181,6 +159,8 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
this.rawStream.WriteByte((byte)(crc & 0xFF)); this.rawStream.WriteByte((byte)(crc & 0xFF));
} }
this.deflateStream = null;
base.Dispose(disposing); base.Dispose(disposing);
this.isDisposed = true; this.isDisposed = true;
} }

2
tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs

@ -63,7 +63,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
/// </summary> /// </summary>
public static readonly TheoryData<int> CompressionLevels = new TheoryData<int> public static readonly TheoryData<int> CompressionLevels = new TheoryData<int>
{ {
1, 2, 3, 4, 5, 6, 7, 8, 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}; };
public static readonly TheoryData<int> PaletteSizes = new TheoryData<int> public static readonly TheoryData<int> PaletteSizes = new TheoryData<int>

Loading…
Cancel
Save