Browse Source

Test code cleanup: removed EnumHelper, moved MeasureFixture to TestUtilities, Jpeg test formatting

af/merge-core
Anton Firszov 9 years ago
parent
commit
3c2f64d681
  1. 4
      tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs
  2. 39
      tests/ImageSharp.Tests/Formats/Jpg/JpegTestBase.cs
  3. 14
      tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementations.cs
  4. 16
      tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementationsTests.cs
  5. 19
      tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs
  6. 50
      tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs
  7. 8
      tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs
  8. 6
      tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

4
tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs

@ -12,14 +12,12 @@ namespace ImageSharp.Tests
{
using System.Diagnostics;
using System.Numerics;
using ImageSharp.Formats;
using ImageSharp.Formats.Jpg;
using Xunit;
using Xunit.Abstractions;
public class Block8x8FTests : UtilityTestClassBase
public class Block8x8FTests : JpegTestBase
{
#if BENCHMARKING
public const int Times = 1000000;

39
tests/ImageSharp.Tests/Formats/Jpg/UtilityTestClassBase.cs → tests/ImageSharp.Tests/Formats/Jpg/JpegTestBase.cs

@ -1,4 +1,4 @@
// <copyright file="UtilityTestClassBase.cs" company="James Jackson-South">
// <copyright file="JpegTestBase.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
@ -13,46 +13,15 @@ namespace ImageSharp.Tests
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using ImageSharp.Formats.Jpg;
/// <summary>
/// Utility class to measure the execution of an operation.
/// </summary>
public class MeasureFixture : TestBase
public class JpegTestBase : MeasureFixture
{
protected bool EnablePrinting = true;
protected void Measure(int times, Action action, [CallerMemberName] string operationName = null)
{
if (this.EnablePrinting) this.Output?.WriteLine($"{operationName} X {times} ...");
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < times; i++)
{
action();
}
sw.Stop();
if (this.EnablePrinting) this.Output?.WriteLine($"{operationName} finished in {sw.ElapsedMilliseconds} ms");
}
public MeasureFixture(ITestOutputHelper output)
public JpegTestBase(ITestOutputHelper output) : base(output)
{
this.Output = output;
}
protected ITestOutputHelper Output { get; }
}
public class UtilityTestClassBase : MeasureFixture
{
public UtilityTestClassBase(ITestOutputHelper output) : base(output)
{
}
// ReSharper disable once InconsistentNaming
public static float[] Create8x8FloatData()
{
@ -66,7 +35,7 @@ namespace ImageSharp.Tests
}
return result;
}
// ReSharper disable once InconsistentNaming
public static int[] Create8x8IntData()
{

14
tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementations.cs

@ -13,7 +13,7 @@ namespace ImageSharp.Tests
using ImageSharp.Formats;
using ImageSharp.Formats.Jpg;
/// <summary>
/// This class contains simplified (unefficient) reference implementations to produce verification data for unit tests
/// Floating point DCT code Ported from https://github.com/norishigefukushima/dct_simd
@ -546,8 +546,8 @@ namespace ImageSharp.Tests
//y[1] = c0 + c3; y[7] = c0 - c3;
/*for(i = 0;i < 8;i++)
{
y[i] *= invsqrt2h;
{
y[i] *= invsqrt2h;
}*/
}
@ -643,10 +643,10 @@ namespace ImageSharp.Tests
0: 1.414214
1: 1.387040
2: 1.306563
3:
3:
4: 1.000000
5: 0.785695
6:
6:
7: 0.275899
*/
@ -852,7 +852,7 @@ namespace ImageSharp.Tests
bool offsetSourceByNeg128 = false)
{
MutableSpan<float> sWorker = offsetSourceByNeg128 ? s.AddScalarToAllValues(-128f) : s;
for (int j = 0; j < 8; j++)
{
fDCT1Dllm_32f(sWorker.Slice(j * 8), temp.Slice(j * 8));
@ -866,7 +866,7 @@ namespace ImageSharp.Tests
}
Transpose8x8(temp, d);
if (downscaleBy8)
{
for (int j = 0; j < 64; j++)

16
tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementationsTests.cs

@ -13,7 +13,7 @@ namespace ImageSharp.Tests.Formats.Jpg
using Xunit;
using Xunit.Abstractions;
public class ReferenceImplementationsTests : UtilityTestClassBase
public class ReferenceImplementationsTests : JpegTestBase
{
public ReferenceImplementationsTests(ITestOutputHelper output)
: base(output)
@ -31,7 +31,7 @@ namespace ImageSharp.Tests.Formats.Jpg
MutableSpan<float> floatSrc = intData.ConvertToFloat32MutableSpan();
ReferenceImplementations.IntegerReferenceDCT.TransformIDCTInplace(intData);
MutableSpan<float> dest = new MutableSpan<float>(64);
MutableSpan<float> temp = new MutableSpan<float>(64);
@ -57,14 +57,14 @@ namespace ImageSharp.Tests.Formats.Jpg
var block = original.AddScalarToAllValues(128);
ReferenceImplementations.IntegerReferenceDCT.TransformFDCTInplace(block);
for (int i = 0; i < 64; i++)
{
block[i] /= 8;
}
ReferenceImplementations.IntegerReferenceDCT.TransformIDCTInplace(block);
for (int i = startAt; i < 64; i++)
{
float expected = original[i];
@ -93,7 +93,7 @@ namespace ImageSharp.Tests.Formats.Jpg
{
float expected = data[i];
float actual = (float)src[i];
Assert.Equal(expected, actual, new ApproximateFloatComparer(2f));
}
}
@ -112,9 +112,9 @@ namespace ImageSharp.Tests.Formats.Jpg
{
MutableSpan<int> intData = Create8x8RandomIntData(-200, 200, seed);
MutableSpan<float> floatSrc = intData.ConvertToFloat32MutableSpan();
ReferenceImplementations.IntegerReferenceDCT.TransformFDCTInplace(intData);
MutableSpan<float> dest = new MutableSpan<float>(64);
MutableSpan<float> temp = new MutableSpan<float>(64);
@ -127,6 +127,6 @@ namespace ImageSharp.Tests.Formats.Jpg
Assert.Equal(expected, actual, new ApproximateFloatComparer(1f));
}
}
}
}
}

19
tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs

@ -1,19 +0,0 @@
// <copyright file="EnumHelper.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
public class EnumHelper
{
public static T[] GetSortedValues<T>()
{
T[] vals = (T[])Enum.GetValues(typeof(T));
Array.Sort(vals);
return vals;
}
}
}

50
tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs

@ -0,0 +1,50 @@
namespace ImageSharp.Tests
{
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Xunit.Abstractions;
/// <summary>
/// Utility class to measure the execution of an operation. It can be used either by inheritance or by composition.
/// </summary>
public class MeasureFixture : TestBase
{
/// <summary>
/// Value indicating whether priniting is enabled.
/// </summary>
protected bool EnablePrinting = true;
/// <summary>
/// Measures and prints the execution time of an <see cref="Action{T}"/>, executed multiple times.
/// </summary>
/// <param name="times">A value indicating how many times to run the action</param>
/// <param name="action">The <see cref="Action{T}"/> to execute</param>
/// <param name="operationName">The name of the operation to print to the output</param>
public void Measure(int times, Action action, [CallerMemberName] string operationName = null)
{
if (this.EnablePrinting) this.Output?.WriteLine($"{operationName} X {times} ...");
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < times; i++)
{
action();
}
sw.Stop();
if (this.EnablePrinting) this.Output?.WriteLine($"{operationName} finished in {sw.ElapsedMilliseconds} ms");
}
/// <summary>
/// Initializes a new instance of <see cref="MeasureFixture"/>
/// </summary>
/// <param name="output">A <see cref="ITestOutputHelper"/> instance to print the results </param>
public MeasureFixture(ITestOutputHelper output)
{
this.Output = output;
}
protected ITestOutputHelper Output { get; }
}
}

8
tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs

@ -22,7 +22,7 @@ namespace ImageSharp.Tests
private static readonly Dictionary<PixelTypes, Type> PixelTypes2ClrTypes = new Dictionary<PixelTypes, Type>();
private static readonly PixelTypes[] AllConcretePixelTypes = EnumHelper.GetSortedValues<PixelTypes>()
private static readonly PixelTypes[] AllConcretePixelTypes = GetAllPixelTypes()
.Except(new [] {PixelTypes.Undefined, PixelTypes.All })
.ToArray();
@ -130,5 +130,11 @@ namespace ImageSharp.Tests
.Where(pt => pixelTypes.HasFlag(pt))
.Select(pt => new KeyValuePair<PixelTypes, Type>(pt, pt.ToType()));
}
/// <summary>
/// Enumerate all available <see cref="PixelTypes"/>-s
/// </summary>
/// <returns>The pixel types</returns>
internal static PixelTypes[] GetAllPixelTypes() => (PixelTypes[])Enum.GetValues(typeof(PixelTypes));
}
}

6
tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

@ -114,7 +114,7 @@ namespace ImageSharp.Tests
IEnumerable<KeyValuePair<PixelTypes, Type>> pixelTypesExp)
{
Assert.Contains(new KeyValuePair<PixelTypes, Type>(pt, typeof(T)), pixelTypesExp);
}
[Fact]
@ -125,7 +125,7 @@ namespace ImageSharp.Tests
var expanded = pixelTypes.ExpandAllTypes();
Assert.Equal(expanded.Count(), 5);
AssertContainsPixelType<Alpha8>(PixelTypes.Alpha8, expanded);
AssertContainsPixelType<Bgr565>(PixelTypes.Bgr565, expanded);
AssertContainsPixelType<Color>(PixelTypes.Color, expanded);
@ -138,7 +138,7 @@ namespace ImageSharp.Tests
{
var expanded = PixelTypes.All.ExpandAllTypes().ToArray();
Assert.True(expanded.Length >= EnumHelper.GetSortedValues<PixelTypes>().Length - 2);
Assert.True(expanded.Length >= TestUtilityExtensions.GetAllPixelTypes().Length - 2);
AssertContainsPixelType<Color>(PixelTypes.Color, expanded);
AssertContainsPixelType<Color>(PixelTypes.StandardImageClass, expanded);
}

Loading…
Cancel
Save