From 0d6e50f29fb5388f87d6be67f489c072ef4204a6 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Fri, 7 Apr 2023 19:21:23 +0200 Subject: [PATCH] Add Vp8Residual Serialization test --- .../Formats/WebP/Vp8ResidualTests.cs | 107 +++++++++++++++++- 1 file changed, 101 insertions(+), 6 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs b/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs index 1f9136e9ab..e9f0f31d75 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Text.Json; +using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.Formats.Webp.Lossy; using SixLabors.ImageSharp.Tests.TestUtilities; @@ -9,10 +11,106 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp; [Trait("Format", "Webp")] public class Vp8ResidualTests { + [Fact] + public void Serialization_Works() + { + // arrange + Vp8Residual expected = new(); + Vp8EncProba encProb = new(); + Random rand = new(439); + CreateRandomProbas(encProb, rand); + CreateCosts(encProb, rand); + expected.Init(1, 0, encProb); + for (int i = 0; i < expected.Coeffs.Length; i++) + { + expected.Coeffs[i] = (byte)rand.Next(255); + } + + // act + string jsonString = JsonSerializer.Serialize(expected); + Vp8Residual actual = JsonSerializer.Deserialize(jsonString); + + // assert + Assert.Equal(expected.CoeffType, actual.CoeffType); + Assert.Equal(expected.Coeffs, actual.Coeffs); + Assert.Equal(expected.Costs.Length, actual.Costs.Length); + Assert.Equal(expected.First, actual.First); + Assert.Equal(expected.Last, actual.Last); + Assert.Equal(expected.Stats.Length, actual.Stats.Length); + for (int i = 0; i < expected.Stats.Length; i++) + { + Vp8StatsArray[] expectedStats = expected.Stats[i].Stats; + Vp8StatsArray[] actualStats = actual.Stats[i].Stats; + Assert.Equal(expectedStats.Length, actualStats.Length); + for (int j = 0; j < expectedStats.Length; j++) + { + Assert.Equal(expectedStats[j].Stats, actualStats[j].Stats); + } + } + + Assert.Equal(expected.Prob.Length, actual.Prob.Length); + for (int i = 0; i < expected.Prob.Length; i++) + { + Vp8ProbaArray[] expectedProbabilities = expected.Prob[i].Probabilities; + Vp8ProbaArray[] actualProbabilities = actual.Prob[i].Probabilities; + Assert.Equal(expectedProbabilities.Length, actualProbabilities.Length); + for (int j = 0; j < expectedProbabilities.Length; j++) + { + Assert.Equal(expectedProbabilities[j].Probabilities, actualProbabilities[j].Probabilities); + } + } + + for (int i = 0; i < expected.Costs.Length; i++) + { + Vp8CostArray[] expectedCosts = expected.Costs[i].Costs; + Vp8CostArray[] actualCosts = actual.Costs[i].Costs; + Assert.Equal(expectedCosts.Length, actualCosts.Length); + for (int j = 0; j < expectedCosts.Length; j++) + { + Assert.Equal(expectedCosts[j].Costs, actualCosts[j].Costs); + } + } + } + + private static void CreateRandomProbas(Vp8EncProba probas, Random rand) + { + for (int t = 0; t < WebpConstants.NumTypes; ++t) + { + for (int b = 0; b < WebpConstants.NumBands; ++b) + { + for (int c = 0; c < WebpConstants.NumCtx; ++c) + { + for (int p = 0; p < WebpConstants.NumProbas; ++p) + { + probas.Coeffs[t][b].Probabilities[c].Probabilities[p] = (byte)rand.Next(255); + } + } + } + } + } + + private static void CreateCosts(Vp8EncProba probas, Random rand) + { + for (int i = 0; i < probas.RemappedCosts.Length; i++) + { + for (int j = 0; j < probas.RemappedCosts[i].Length; j++) + { + for (int k = 0; k < probas.RemappedCosts[i][j].Costs.Length; k++) + { + ushort[] costs = probas.RemappedCosts[i][j].Costs[k].Costs; + for (int m = 0; m < costs.Length; m++) + { + costs[m] = (byte)rand.Next(255); + } + } + } + } + } + private static void RunSetCoeffsTest() { // arrange - var residual = new Vp8Residual(); + Vp8Residual residual = new(); short[] coeffs = { 110, 0, -2, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0 }; // act @@ -23,11 +121,8 @@ public class Vp8ResidualTests } [Fact] - public void RunSetCoeffsTest_Works() => RunSetCoeffsTest(); - - [Fact] - public void RunSetCoeffsTest_WithHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunSetCoeffsTest, HwIntrinsics.AllowAll); + public void SetCoeffsTest_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunSetCoeffsTest, HwIntrinsics.AllowAll); [Fact] - public void RunSetCoeffsTest_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunSetCoeffsTest, HwIntrinsics.DisableHWIntrinsic); + public void SetCoeffsTest_WithoutSSE2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunSetCoeffsTest, HwIntrinsics.DisableSSE2); }