Browse Source

move tests

af/merge-core
Anton Firszov 7 years ago
parent
commit
ad659fb731
  1. 2
      src/ImageSharp/Common/Extensions/SimdUtils.cs
  2. 2
      tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs
  3. 40
      tests/ImageSharp.Tests/Common/SimdUtilsTests.cs
  4. 48
      tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs
  5. 7
      tests/ImageSharp.Tests/TestUtilities/TestDataGenerator.cs

2
src/ImageSharp/Common/Extensions/SimdUtils.cs

@ -111,7 +111,7 @@ namespace SixLabors.ImageSharp
/// <cref>https://github.com/dotnet/coreclr/pull/10662</cref>
/// </see>
/// </summary>
internal static void BulkConvertByteToNormalizedFloatFast(ReadOnlySpan<byte> source, Span<float> dest)
internal static void BulkConvertByteToNormalizedFloatWithExtendedIntrinsics(ReadOnlySpan<byte> source, Span<float> dest)
{
Guard.IsTrue(
source.Length % Vector<byte>.Count == 0,

2
tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs

@ -94,7 +94,7 @@ namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk
Span<byte> sBytes = MemoryMarshal.Cast<Rgba32, byte>(this.source.GetSpan());
Span<float> dFloats = MemoryMarshal.Cast<Vector4, float>(this.destination.GetSpan());
SimdUtils.BulkConvertByteToNormalizedFloatFast(sBytes, dFloats);
SimdUtils.BulkConvertByteToNormalizedFloatWithExtendedIntrinsics(sBytes, dFloats);
}
}

40
tests/ImageSharp.Tests/Common/SimdUtilsTests.cs

@ -186,6 +186,46 @@ namespace SixLabors.ImageSharp.Tests.Common
Assert.Equal(expected, dest);
}
[Theory]
[InlineData(1, 0)]
[InlineData(2, 32)]
[InlineData(3, 128)]
public void BulkConvertByteToNormalizedFloat(int seed, int count)
{
if (this.SkipOnNonAvx2())
{
return;
}
byte[] source = new Random(seed).GenerateRandomByteArray(count);
float[] result = new float[count];
float[] expected = source.Select(b => (float)b / 255f).ToArray();
SimdUtils.BulkConvertByteToNormalizedFloat(source, result);
Assert.Equal(expected, result, new ApproximateFloatComparer(1e-5f));
}
[Theory]
[InlineData(1, 0)]
[InlineData(2, 32)]
[InlineData(3, 128)]
public void BulkConvertByteToNormalizedFloatWithExtendedIntrinsics(int seed, int count)
{
if (!Vector.IsHardwareAccelerated)
{
return;
}
byte[] source = new Random(seed).GenerateRandomByteArray(count);
float[] result = new float[count];
float[] expected = source.Select(b => (float)b / 255f).ToArray();
SimdUtils.BulkConvertByteToNormalizedFloatWithExtendedIntrinsics(source, result);
Assert.Equal(expected, result, new ApproximateFloatComparer(1e-5f));
}
[Theory]
[InlineData(0)]
[InlineData(7)]

48
tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs

@ -49,54 +49,6 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats
);
}
[Fact]
public void BulkConvertByteToNormalizedFloat()
{
if (!Vector.IsHardwareAccelerated)
{
return;
}
ImageSharp.PixelFormats.Rgba32[] source = CreatePixelTestData(64);
Vector4[] expected = CreateExpectedVector4Data(source);
TestOperation(
source,
expected,
(s, d) =>
{
ReadOnlySpan<byte> sBytes = MemoryMarshal.Cast<ImageSharp.PixelFormats.Rgba32, byte>(s);
Span<float> dFloats = MemoryMarshal.Cast<Vector4, float>(d.Memory.Span);
SimdUtils.BulkConvertByteToNormalizedFloat(sBytes, dFloats);
}
);
}
[Fact]
public void BulkConvertByteToNormalizedFloatFast()
{
if (!Vector.IsHardwareAccelerated)
{
return;
}
ImageSharp.PixelFormats.Rgba32[] source = CreatePixelTestData(128);
Vector4[] expected = CreateExpectedVector4Data(source);
TestOperation(
source,
expected,
(s, d) =>
{
ReadOnlySpan<byte> sBytes = MemoryMarshal.Cast<ImageSharp.PixelFormats.Rgba32, byte>(s);
Span<float> dFloats = MemoryMarshal.Cast<Vector4, float>(d.Memory.Span);
SimdUtils.BulkConvertByteToNormalizedFloatFast(sBytes, dFloats);
}
);
}
// [Fact] // Profiling benchmark - enable manually!
#pragma warning disable xUnit1013 // Public method should be marked as test

7
tests/ImageSharp.Tests/TestUtilities/TestDataGenerator.cs

@ -46,6 +46,13 @@ namespace SixLabors.ImageSharp.Tests
return values;
}
public static byte[] GenerateRandomByteArray(this Random rnd, int length)
{
byte[] values = new byte[length];
rnd.NextBytes(values);
return values;
}
private static float GetRandomFloat(Random rnd, float minVal, float maxVal)
{
return (float)rnd.NextDouble() * (maxVal - minVal) + minVal;

Loading…
Cancel
Save