From 3c2f64d68139bd445a8e24c1b41fa5073fb22251 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 14 Jan 2017 22:37:17 +0100 Subject: [PATCH] Test code cleanup: removed EnumHelper, moved MeasureFixture to TestUtilities, Jpeg test formatting --- .../Formats/Jpg/Block8x8FTests.cs | 4 +- ...tilityTestClassBase.cs => JpegTestBase.cs} | 39 ++------------- .../Formats/Jpg/ReferenceImplementations.cs | 14 +++--- .../Jpg/ReferenceImplementationsTests.cs | 16 +++--- .../TestUtilities/EnumHelper.cs | 19 ------- .../TestUtilities/MeasureFixture.cs | 50 +++++++++++++++++++ .../TestUtilities/TestUtilityExtensions.cs | 8 ++- .../Tests/TestUtilityExtensionsTests.cs | 6 +-- 8 files changed, 80 insertions(+), 76 deletions(-) rename tests/ImageSharp.Tests/Formats/Jpg/{UtilityTestClassBase.cs => JpegTestBase.cs} (75%) delete mode 100644 tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs index 6736548e6..1f055bab4 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs +++ b/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; diff --git a/tests/ImageSharp.Tests/Formats/Jpg/UtilityTestClassBase.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegTestBase.cs similarity index 75% rename from tests/ImageSharp.Tests/Formats/Jpg/UtilityTestClassBase.cs rename to tests/ImageSharp.Tests/Formats/Jpg/JpegTestBase.cs index c92c6aa9a..2e568ba34 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/UtilityTestClassBase.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegTestBase.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // @@ -13,46 +13,15 @@ namespace ImageSharp.Tests using System; using System.Collections.Generic; using System.Diagnostics; - using System.Runtime.CompilerServices; using ImageSharp.Formats.Jpg; - /// - /// Utility class to measure the execution of an operation. - /// - 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() { diff --git a/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementations.cs b/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementations.cs index 6aed0d3fa..60c136674 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementations.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementations.cs @@ -13,7 +13,7 @@ namespace ImageSharp.Tests using ImageSharp.Formats; using ImageSharp.Formats.Jpg; - + /// /// 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 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++) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementationsTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementationsTests.cs index 66ddeabc0..63878ea34 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/ReferenceImplementationsTests.cs +++ b/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 floatSrc = intData.ConvertToFloat32MutableSpan(); ReferenceImplementations.IntegerReferenceDCT.TransformIDCTInplace(intData); - + MutableSpan dest = new MutableSpan(64); MutableSpan temp = new MutableSpan(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 intData = Create8x8RandomIntData(-200, 200, seed); MutableSpan floatSrc = intData.ConvertToFloat32MutableSpan(); - + ReferenceImplementations.IntegerReferenceDCT.TransformFDCTInplace(intData); - + MutableSpan dest = new MutableSpan(64); MutableSpan temp = new MutableSpan(64); @@ -127,6 +127,6 @@ namespace ImageSharp.Tests.Formats.Jpg Assert.Equal(expected, actual, new ApproximateFloatComparer(1f)); } - } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs b/tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs deleted file mode 100644 index 7b6b26e94..000000000 --- a/tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs +++ /dev/null @@ -1,19 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Tests -{ - using System; - - public class EnumHelper - { - public static T[] GetSortedValues() - { - T[] vals = (T[])Enum.GetValues(typeof(T)); - Array.Sort(vals); - return vals; - } - } -} diff --git a/tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs b/tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs new file mode 100644 index 000000000..1dadc4884 --- /dev/null +++ b/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; + + /// + /// Utility class to measure the execution of an operation. It can be used either by inheritance or by composition. + /// + public class MeasureFixture : TestBase + { + /// + /// Value indicating whether priniting is enabled. + /// + protected bool EnablePrinting = true; + + /// + /// Measures and prints the execution time of an , executed multiple times. + /// + /// A value indicating how many times to run the action + /// The to execute + /// The name of the operation to print to the output + 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"); + } + + /// + /// Initializes a new instance of + /// + /// A instance to print the results + public MeasureFixture(ITestOutputHelper output) + { + this.Output = output; + } + + protected ITestOutputHelper Output { get; } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs index 8b0c65a3c..d2726c16a 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs @@ -22,7 +22,7 @@ namespace ImageSharp.Tests private static readonly Dictionary PixelTypes2ClrTypes = new Dictionary(); - private static readonly PixelTypes[] AllConcretePixelTypes = EnumHelper.GetSortedValues() + 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(pt, pt.ToType())); } + + /// + /// Enumerate all available -s + /// + /// The pixel types + internal static PixelTypes[] GetAllPixelTypes() => (PixelTypes[])Enum.GetValues(typeof(PixelTypes)); } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index 93d6eab16..23a42c1a6 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -114,7 +114,7 @@ namespace ImageSharp.Tests IEnumerable> pixelTypesExp) { Assert.Contains(new KeyValuePair(pt, typeof(T)), pixelTypesExp); - + } [Fact] @@ -125,7 +125,7 @@ namespace ImageSharp.Tests var expanded = pixelTypes.ExpandAllTypes(); Assert.Equal(expanded.Count(), 5); - + AssertContainsPixelType(PixelTypes.Alpha8, expanded); AssertContainsPixelType(PixelTypes.Bgr565, expanded); AssertContainsPixelType(PixelTypes.Color, expanded); @@ -138,7 +138,7 @@ namespace ImageSharp.Tests { var expanded = PixelTypes.All.ExpandAllTypes().ToArray(); - Assert.True(expanded.Length >= EnumHelper.GetSortedValues().Length - 2); + Assert.True(expanded.Length >= TestUtilityExtensions.GetAllPixelTypes().Length - 2); AssertContainsPixelType(PixelTypes.Color, expanded); AssertContainsPixelType(PixelTypes.StandardImageClass, expanded); }