Skip to content

Commit 4dcf856

Browse files
committed
Add Vp8CostArray Serialization test, add stub for Vp8CostArray JsonConverter
1 parent 79448ec commit 4dcf856

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
using SixLabors.ImageSharp.Formats.Webp.Lossy;
7+
8+
namespace SixLabors.ImageSharp.Tests.Formats.WebP.Serialization;
9+
10+
internal class Vp8CostArrayJsonConverter : JsonConverter<Vp8CostArray>
11+
{
12+
public override Vp8CostArray Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException();
13+
14+
public override void Write(Utf8JsonWriter writer, Vp8CostArray value, JsonSerializerOptions options) => throw new NotImplementedException();
15+
}

tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,40 @@
55
using System.Text.Json;
66
using SixLabors.ImageSharp.Formats.Webp;
77
using SixLabors.ImageSharp.Formats.Webp.Lossy;
8+
using SixLabors.ImageSharp.Tests.Formats.WebP.Serialization;
89
using SixLabors.ImageSharp.Tests.TestUtilities;
10+
using JsonSerializer = System.Text.Json.JsonSerializer;
911

1012
namespace SixLabors.ImageSharp.Tests.Formats.Webp;
1113

1214
[Trait("Format", "Webp")]
1315
public class Vp8ResidualTests
1416
{
1517
[Fact]
16-
public void GetResidualCost_Works()
18+
public void Vp8CostArray_Serialization_Works()
1719
{
18-
if (!Sse2.IsSupported)
20+
// arrange
21+
Vp8CostArray expected = new();
22+
for (ushort i = 0; i < expected.Costs.Length; i++)
1923
{
20-
// JsonSerializer without SSE2 does not seem to work, skip test then.
21-
return;
24+
expected.Costs[i] = i;
2225
}
2326

24-
// arrange
25-
int ctx0 = 0;
26-
int expected = 20911;
27-
string jsonString = File.ReadAllText(Path.Combine("TestDataWebp", "Vp8Residual.json"));
28-
Vp8Residual residual = JsonSerializer.Deserialize<Vp8Residual>(jsonString);
27+
JsonSerializerOptions options = new()
28+
{
29+
Converters = { new Vp8CostArrayJsonConverter() }
30+
};
2931

3032
// act
31-
int actual = residual.GetResidualCost(ctx0);
33+
string jsonString = JsonSerializer.Serialize(expected);
34+
Vp8CostArray actual = JsonSerializer.Deserialize<Vp8CostArray>(jsonString, options);
3235

3336
// assert
34-
Assert.Equal(expected, actual);
37+
Assert.Equal(expected.Costs, actual.Costs);
3538
}
3639

3740
[Fact]
38-
public void Serialization_Works()
41+
public void Vp8Residual_Serialization_Works()
3942
{
4043
if (!Sse2.IsSupported)
4144
{
@@ -101,6 +104,30 @@ public void Serialization_Works()
101104
}
102105
}
103106

107+
[Fact]
108+
public void GetResidualCost_Works()
109+
{
110+
if (!Sse2.IsSupported)
111+
{
112+
// JsonSerializer without SSE2 does not seem to work, skip test then.
113+
return;
114+
}
115+
116+
// arrange
117+
int ctx0 = 0;
118+
int expected = 20911;
119+
string jsonString = File.ReadAllText(Path.Combine("TestDataWebp", "Vp8Residual.json"));
120+
Vp8Residual residual = JsonSerializer.Deserialize<Vp8Residual>(jsonString);
121+
122+
// act
123+
int actual = residual.GetResidualCost(ctx0);
124+
125+
// assert
126+
Assert.Equal(expected, actual);
127+
}
128+
129+
130+
104131
private static void CreateRandomProbas(Vp8EncProba probas, Random rand)
105132
{
106133
for (int t = 0; t < WebpConstants.NumTypes; ++t)

0 commit comments

Comments
 (0)