Browse Source

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

af/merge-core
Anton Firszov 10 years ago
parent
commit
3c2f64d681
  1. 4
      tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs
  2. 37
      tests/ImageSharp.Tests/Formats/Jpg/JpegTestBase.cs
  3. 2
      tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementationsTests.cs
  4. 19
      tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs
  5. 50
      tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs
  6. 8
      tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs
  7. 2
      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.Diagnostics;
using System.Numerics; using System.Numerics;
using ImageSharp.Formats;
using ImageSharp.Formats.Jpg; using ImageSharp.Formats.Jpg;
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
public class Block8x8FTests : UtilityTestClassBase public class Block8x8FTests : JpegTestBase
{ {
#if BENCHMARKING #if BENCHMARKING
public const int Times = 1000000; public const int Times = 1000000;

37
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. // Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// </copyright> // </copyright>
@ -13,43 +13,12 @@ namespace ImageSharp.Tests
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.CompilerServices;
using ImageSharp.Formats.Jpg; using ImageSharp.Formats.Jpg;
/// <summary> public class JpegTestBase : MeasureFixture
/// Utility class to measure the execution of an operation.
/// </summary>
public class MeasureFixture : TestBase
{ {
protected bool EnablePrinting = true; public JpegTestBase(ITestOutputHelper output) : base(output)
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)
{
this.Output = output;
}
protected ITestOutputHelper Output { get; }
}
public class UtilityTestClassBase : MeasureFixture
{
public UtilityTestClassBase(ITestOutputHelper output) : base(output)
{ {
} }

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

@ -13,7 +13,7 @@ namespace ImageSharp.Tests.Formats.Jpg
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
public class ReferenceImplementationsTests : UtilityTestClassBase public class ReferenceImplementationsTests : JpegTestBase
{ {
public ReferenceImplementationsTests(ITestOutputHelper output) public ReferenceImplementationsTests(ITestOutputHelper output)
: base(output) : base(output)

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 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 }) .Except(new [] {PixelTypes.Undefined, PixelTypes.All })
.ToArray(); .ToArray();
@ -130,5 +130,11 @@ namespace ImageSharp.Tests
.Where(pt => pixelTypes.HasFlag(pt)) .Where(pt => pixelTypes.HasFlag(pt))
.Select(pt => new KeyValuePair<PixelTypes, Type>(pt, pt.ToType())); .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));
} }
} }

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

@ -138,7 +138,7 @@ namespace ImageSharp.Tests
{ {
var expanded = PixelTypes.All.ExpandAllTypes().ToArray(); 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.Color, expanded);
AssertContainsPixelType<Color>(PixelTypes.StandardImageClass, expanded); AssertContainsPixelType<Color>(PixelTypes.StandardImageClass, expanded);
} }

Loading…
Cancel
Save