From b4991e93a6c84063ca875aba3d3cb1a1f9494a6e Mon Sep 17 00:00:00 2001 From: "Ringler, Moritz" Date: Mon, 13 Nov 2023 13:18:25 +0100 Subject: [PATCH] Adapt tests --- .../Models/PInvokeFileProcessorTests.cs | 65 ++++++++++++++++-- czishrink/netczicompressTests/mandelbrot.czi | Bin 101600 -> 101600 bytes 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/czishrink/netczicompressTests/Models/PInvokeFileProcessorTests.cs b/czishrink/netczicompressTests/Models/PInvokeFileProcessorTests.cs index c89cc99..66afb62 100644 --- a/czishrink/netczicompressTests/Models/PInvokeFileProcessorTests.cs +++ b/czishrink/netczicompressTests/Models/PInvokeFileProcessorTests.cs @@ -5,6 +5,9 @@ namespace netczicompressTests.Models; using System.Reactive.Disposables; +using System.Text; +using System.Xml.Linq; +using System.Xml.XPath; /// /// Tests for . @@ -23,7 +26,7 @@ public void Ctr_WhenCalledWithNoOp_Throws() [InlineData(CompressionMode.CompressUncompressed)] [InlineData(CompressionMode.CompressAll)] [InlineData(CompressionMode.CompressUncompressedAndZstd)] - public void CompressAndDecompressOneFile_FilesHaveCorrectSize( + public void CompressAndDecompressOneFile_FilesHaveCorrectSizeAndMetadata( CompressionMode mode) { // ARRANGE @@ -43,13 +46,17 @@ public void CompressAndDecompressOneFile_FilesHaveCorrectSize( decompressor.ProcessFile(compressed, uncompressed, _ => { }, CancellationToken.None); var compressedSize = GetLength(compressed); + var decompressedSize = GetLength(uncompressed); var originalSize = GetLength(testFile); // ASSERT - compressedSize.Should().Be(58432L); + compressedSize.Should().Be(58528L); decompressedSize.Should().BeCloseTo(originalSize, 2048); - decompressedSize.Should().Be(99552L); + decompressedSize.Should().Be(99648L); + + Metadata.FromFile(compressed).CurrentCompressionParameters.Should().Be("Lossless: True"); + Metadata.FromFile(uncompressed).CurrentCompressionParameters.Should().BeEmpty(); } [Theory] @@ -114,9 +121,9 @@ public void GetLibFullName_WhenCalled_ReturnsExpected() } [Theory] - [InlineData(0, 99552L, 57760L)] - [InlineData(2, 99552L, 57920L)] - [InlineData(4, 99552L, 57472L)] + [InlineData(0, 99648L, 57856L)] + [InlineData(2, 99648L, 58016L)] + [InlineData(4, 99648L, 57568L)] public void CompressWithLevel_FilesHaveCorrectSize( int compressionLevel, long expectedUncompressedSize, long expectedCompressedSize) { @@ -160,4 +167,50 @@ private static string GetTestFilePath() var testFile = Path.Combine(dirname ?? ".", "mandelbrot.czi"); return testFile; } + + private class Metadata + { + public required XDocument Xml { get; init; } + + public string? CurrentCompressionParameters => this.Xml + .XPathSelectElement( + "ImageDocument/Metadata/Information/Image/CurrentCompressionParameters") + ?.Value; + + public static Metadata FromFile(string path) => new() { Xml = ParseMetadataXml(path) }; + + private static XDocument ParseMetadataXml(string filePath) + { + return XDocument.Parse(ReadMetadataXml(filePath)); + } + + private static string ReadMetadataXml(string filePath) + { + // These numbers are from the ZISRAW file specification. + const int SegmentHeaderSize = 16 + 8 + 8; + const int MetadataPositionInFileHeader = 60; + const int MetadataHeaderSize = 256; + + using var stream = File.OpenRead(filePath); + + // BinaryReader uses little-endian as required. + using var reader = new BinaryReader(stream); + + // Read metadataPosition from ZISRAW file header + stream.Position = SegmentHeaderSize + MetadataPositionInFileHeader; + long metadataHeaderPosition = reader.ReadInt64() + SegmentHeaderSize; + + // Read size of XML from ZISRAWMETADATA header + stream.Position = metadataHeaderPosition; + int metadataSize = reader.ReadInt32(); + + // Read XML bytes from ZISRAWMETADATA segment + stream.Position = metadataHeaderPosition + MetadataHeaderSize; + var xmlBytes = reader.ReadBytes(metadataSize); + + // Decode (always UTF-8 according to the spec) + var result = Encoding.UTF8.GetString(xmlBytes); + return result; + } + } } diff --git a/czishrink/netczicompressTests/mandelbrot.czi b/czishrink/netczicompressTests/mandelbrot.czi index 94600acf1e4bdd4723418b3f17bd3bf4dc90cb35..173accaa87d73e63b73b048530529323db913b00 100644 GIT binary patch delta 124 zcmaDblkLGwwhc2_ITvs;fWd;z3t1mBN*WsK8XANc8dw>bTA7&Y85*0K8JJsY8yHTW z&92580Ro$kvHP-5wincGcHrOcz|Y9xAh2AAcZQ(<8PA2q_s!0^d{){X;m??0jVXMh Ii&26J0PW=`*#H0l delta 129 zcmaDblkLGwwhc2_IVW&3fWd^#3t1mBN*Y+`8XANcSXh~wTA3Q?85x;a7#Nyq8yHQV z&9258Y3Li|xA_>mFZ*O=e%Z||yf#eDIfC1B1R3wx3P>g#VwGpx?XqvDXBpokO_}Wx P{)`FMn8GKz7$ukhRaPdV