Browse Source

use RemoteExecutor in JpegDecoderTests

af/octree-no-pixelmap
Anton Firszov 6 years ago
parent
commit
967e4eabb1
  1. 17
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Baseline.cs
  2. 19
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Progressive.cs
  3. 2
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  4. 15
      tests/ImageSharp.Tests/TestUtilities/BasicSerializer.cs
  5. 7
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs
  6. 2
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs
  7. 29
      tests/ImageSharp.Tests/TestUtilities/Tests/BasicSerializerTests.cs

17
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Baseline.cs

@ -2,7 +2,9 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using Microsoft.DotNet.RemoteExecutor;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities;
using Xunit; using Xunit;
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
@ -15,22 +17,23 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
public void DecodeBaselineJpeg<TPixel>(TestImageProvider<TPixel> provider) public void DecodeBaselineJpeg<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
if (SkipTest(provider)) static void RunTest(string providerDump)
{ {
// skipping to avoid OutOfMemoryException on CI TestImageProvider<TPixel> provider =
return; BasicSerializer.Deserialize<TestImageProvider<TPixel>>(providerDump);
}
using (Image<TPixel> image = provider.GetImage(JpegDecoder)) using Image<TPixel> image = provider.GetImage(JpegDecoder);
{
image.DebugSave(provider); image.DebugSave(provider);
provider.Utility.TestName = DecodeBaselineJpegOutputName; provider.Utility.TestName = DecodeBaselineJpegOutputName;
image.CompareToReferenceOutput( image.CompareToReferenceOutput(
this.GetImageComparer(provider), GetImageComparer(provider),
provider, provider,
appendPixelTypeToFileName: false); appendPixelTypeToFileName: false);
} }
string providerDump = BasicSerializer.Serialize(provider);
RemoteExecutor.Invoke(RunTest, providerDump).Dispose();
} }
[Theory] [Theory]

19
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Progressive.cs

@ -1,7 +1,9 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using Microsoft.DotNet.RemoteExecutor;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities;
using Xunit; using Xunit;
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
@ -16,22 +18,23 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
public void DecodeProgressiveJpeg<TPixel>(TestImageProvider<TPixel> provider) public void DecodeProgressiveJpeg<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
if (SkipTest(provider)) static void RunTest(string providerDump)
{ {
// skipping to avoid OutOfMemoryException on CI TestImageProvider<TPixel> provider =
return; BasicSerializer.Deserialize<TestImageProvider<TPixel>>(providerDump);
}
using (Image<TPixel> image = provider.GetImage(JpegDecoder)) using Image<TPixel> image = provider.GetImage(JpegDecoder);
{
image.DebugSave(provider); image.DebugSave(provider);
provider.Utility.TestName = DecodeProgressiveJpegOutputName; provider.Utility.TestName = DecodeProgressiveJpegOutputName;
image.CompareToReferenceOutput( image.CompareToReferenceOutput(
this.GetImageComparer(provider), GetImageComparer(provider),
provider, provider,
appendPixelTypeToFileName: false); appendPixelTypeToFileName: false);
} }
string dump = BasicSerializer.Serialize(provider);
RemoteExecutor.Invoke(RunTest, dump).Dispose();
} }
} }
} }

2
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
private const float BaselineTolerance = 0.001F / 100; private const float BaselineTolerance = 0.001F / 100;
private const float ProgressiveTolerance = 0.2F / 100; private const float ProgressiveTolerance = 0.2F / 100;
private ImageComparer GetImageComparer<TPixel>(TestImageProvider<TPixel> provider) private static ImageComparer GetImageComparer<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
string file = provider.SourceFileOrDescription; string file = provider.SourceFileOrDescription;

15
tests/ImageSharp.Tests/TestUtilities/BasicSerializer.cs

@ -16,10 +16,11 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities
public const char Separator = ':'; public const char Separator = ':';
private string DumpToString() private string DumpToString(Type type)
{ {
using var ms = new MemoryStream(); using var ms = new MemoryStream();
using var writer = new StreamWriter(ms); using var writer = new StreamWriter(ms);
writer.WriteLine(type.FullName);
foreach (KeyValuePair<string, string> kv in this.map) foreach (KeyValuePair<string, string> kv in this.map)
{ {
writer.WriteLine($"{kv.Key}{Separator}{kv.Value}"); writer.WriteLine($"{kv.Key}{Separator}{kv.Value}");
@ -29,31 +30,35 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities
return System.Convert.ToBase64String(data); return System.Convert.ToBase64String(data);
} }
private void LoadDump(string dump) private Type LoadDump(string dump)
{ {
byte[] data = System.Convert.FromBase64String(dump); byte[] data = System.Convert.FromBase64String(dump);
using var ms = new MemoryStream(data); using var ms = new MemoryStream(data);
using var reader = new StreamReader(ms); using var reader = new StreamReader(ms);
var type = Type.GetType(reader.ReadLine());
for (string s = reader.ReadLine(); s != null ; s = reader.ReadLine()) for (string s = reader.ReadLine(); s != null ; s = reader.ReadLine())
{ {
string[] kv = s.Split(Separator); string[] kv = s.Split(Separator);
this.map[kv[0]] = kv[1]; this.map[kv[0]] = kv[1];
} }
return type;
} }
public static string Serialize(IXunitSerializable serializable) public static string Serialize(IXunitSerializable serializable)
{ {
var serializer = new BasicSerializer(); var serializer = new BasicSerializer();
serializable.Serialize(serializer); serializable.Serialize(serializer);
return serializer.DumpToString(); return serializer.DumpToString(serializable.GetType());
} }
public static T Deserialize<T>(string dump) where T : IXunitSerializable public static T Deserialize<T>(string dump) where T : IXunitSerializable
{ {
T result = Activator.CreateInstance<T>();
var serializer = new BasicSerializer(); var serializer = new BasicSerializer();
serializer.LoadDump(dump); Type type = serializer.LoadDump(dump);
var result = (T) Activator.CreateInstance(type);
result.Deserialize(serializer); result.Deserialize(serializer);
return result; return result;
} }

7
tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs

@ -18,6 +18,11 @@ namespace SixLabors.ImageSharp.Tests
{ {
private readonly Func<Image<TPixel>> factoryFunc; private readonly Func<Image<TPixel>> factoryFunc;
public LambdaProvider()
{
throw new NotSupportedException();
}
public LambdaProvider(Func<Image<TPixel>> factoryFunc) public LambdaProvider(Func<Image<TPixel>> factoryFunc)
{ {
this.factoryFunc = factoryFunc; this.factoryFunc = factoryFunc;
@ -26,4 +31,4 @@ namespace SixLabors.ImageSharp.Tests
public override Image<TPixel> GetImage() => this.factoryFunc(); public override Image<TPixel> GetImage() => this.factoryFunc();
} }
} }
} }

2
tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs

@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.Tests
/// Provides <see cref="Image{TPixel}" /> instances for parametric unit tests. /// Provides <see cref="Image{TPixel}" /> instances for parametric unit tests.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The pixel format of the image</typeparam> /// <typeparam name="TPixel">The pixel format of the image</typeparam>
public abstract partial class TestImageProvider<TPixel> : ITestImageProvider public abstract partial class TestImageProvider<TPixel> : ITestImageProvider, IXunitSerializable
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
public PixelTypes PixelType { get; private set; } = typeof(TPixel).GetPixelType(); public PixelTypes PixelType { get; private set; } = typeof(TPixel).GetPixelType();

29
tests/ImageSharp.Tests/TestUtilities/Tests/BasicSerializerTests.cs

@ -6,20 +6,20 @@ namespace SixLabors.ImageSharp.Tests
{ {
public class BasicSerializerTests public class BasicSerializerTests
{ {
class TestObj : IXunitSerializable class BaseObj : IXunitSerializable
{ {
public double Length { get; set; } public double Length { get; set; }
public string Name { get; set; } public string Name { get; set; }
public int Lives { get; set; } public int Lives { get; set; }
public void Deserialize(IXunitSerializationInfo info) public virtual void Deserialize(IXunitSerializationInfo info)
{ {
info.AddValue(nameof(Length), Length); info.AddValue(nameof(Length), Length);
info.AddValue(nameof(Name), Name); info.AddValue(nameof(Name), Name);
info.AddValue(nameof(this.Lives), Lives); info.AddValue(nameof(this.Lives), Lives);
} }
public void Serialize(IXunitSerializationInfo info) public virtual void Serialize(IXunitSerializationInfo info)
{ {
this.Length = info.GetValue<double>(nameof(Length)); this.Length = info.GetValue<double>(nameof(Length));
this.Name = info.GetValue<string>(nameof(Name)); this.Name = info.GetValue<string>(nameof(Name));
@ -27,17 +27,36 @@ namespace SixLabors.ImageSharp.Tests
} }
} }
class DerivedObj : BaseObj
{
public double Strength { get; set; }
public override void Deserialize(IXunitSerializationInfo info)
{
this.Strength = info.GetValue<double>(nameof(Strength));
base.Deserialize(info);
}
public override void Serialize(IXunitSerializationInfo info)
{
base.Serialize(info);
info.AddValue(nameof(Strength), Strength);
}
}
[Fact] [Fact]
public void SerializeDeserialize_ShouldPreserveValues() public void SerializeDeserialize_ShouldPreserveValues()
{ {
var obj = new TestObj() {Length = 123, Name = "Lol123!", Lives = 7}; var obj = new DerivedObj() {Length = 123.1, Name = "Lol123!", Lives = 7, Strength = 4.8};
string str = BasicSerializer.Serialize(obj); string str = BasicSerializer.Serialize(obj);
TestObj mirror = BasicSerializer.Deserialize<TestObj>(str); BaseObj mirrorBase = BasicSerializer.Deserialize<BaseObj>(str);
DerivedObj mirror = Assert.IsType<DerivedObj>(mirrorBase);
Assert.Equal(obj.Length, mirror.Length); Assert.Equal(obj.Length, mirror.Length);
Assert.Equal(obj.Name, mirror.Name); Assert.Equal(obj.Name, mirror.Name);
Assert.Equal(obj.Lives, mirror.Lives); Assert.Equal(obj.Lives, mirror.Lives);
Assert.Equal(obj.Strength, mirror.Strength);
} }
} }
} }

Loading…
Cancel
Save