From 5ec9310d7378ffecfc137297ffdb403053943fd4 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 27 Aug 2017 17:07:37 +0200 Subject: [PATCH] introduce BufferArea2D, move Memory utility tests to proper place --- .../Formats/Jpeg/Common/Block8x8.cs | 2 +- .../Decoder/ComponentPostprocessor.cs | 33 ++++ ...Processor.cs => JpegBlockPostProcessor.cs} | 14 +- .../Components/Decoder/OrigJpegScanDecoder.cs | 11 -- .../Jpeg/GolangPort/OrigJpegDecoderCore.cs | 14 +- src/ImageSharp/Memory/Buffer2DExtensions.cs | 25 +++ src/ImageSharp/Memory/BufferArea2D.cs | 30 +++ .../{Common => Memory}/Buffer2DTests.cs | 38 ++-- .../{Common => Memory}/BufferTests.cs | 77 ++++---- .../{Common => Memory}/Fast2DArrayTests.cs | 12 +- .../{Common => Memory}/PixelDataPoolTests.cs | 11 +- .../SpanUtilityTests.cs} | 185 +++++++++--------- .../{Common => Memory}/TestStructs.cs | 6 +- 13 files changed, 269 insertions(+), 189 deletions(-) create mode 100644 src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/ComponentPostprocessor.cs rename src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/{JpegBlockProcessor.cs => JpegBlockPostProcessor.cs} (92%) create mode 100644 src/ImageSharp/Memory/BufferArea2D.cs rename tests/ImageSharp.Tests/{Common => Memory}/Buffer2DTests.cs (72%) rename tests/ImageSharp.Tests/{Common => Memory}/BufferTests.cs (69%) rename tests/ImageSharp.Tests/{Common => Memory}/Fast2DArrayTests.cs (95%) rename tests/ImageSharp.Tests/{Common => Memory}/PixelDataPoolTests.cs (89%) rename tests/ImageSharp.Tests/{Common/BufferSpanTests.cs => Memory/SpanUtilityTests.cs} (63%) rename tests/ImageSharp.Tests/{Common => Memory}/TestStructs.cs (96%) diff --git a/src/ImageSharp/Formats/Jpeg/Common/Block8x8.cs b/src/ImageSharp/Formats/Jpeg/Common/Block8x8.cs index c24bab9b8..8cd3004c3 100644 --- a/src/ImageSharp/Formats/Jpeg/Common/Block8x8.cs +++ b/src/ImageSharp/Formats/Jpeg/Common/Block8x8.cs @@ -141,7 +141,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Common short* fp = blockPtr->data; fp[idx] = value; } - + public Block8x8F AsFloatBlock() { // TODO: Optimize this diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/ComponentPostprocessor.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/ComponentPostprocessor.cs new file mode 100644 index 000000000..6fe07df02 --- /dev/null +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/ComponentPostprocessor.cs @@ -0,0 +1,33 @@ +using System; +using SixLabors.ImageSharp.Memory; +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder +{ + internal class ComponentPostProcessor : IDisposable + { + public Size ImageSizeInBlocks { get; } + + public int NumberOfRowGroupScans + { + get; + + } + + class RowGroupProcessor : IDisposable + { + public Buffer2D ColorBuffer { get; } + + public void Dispose() + { + } + } + + + + public void Dispose() + { + + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/JpegBlockProcessor.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/JpegBlockPostProcessor.cs similarity index 92% rename from src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/JpegBlockProcessor.cs rename to src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/JpegBlockPostProcessor.cs index 77ad268ed..ea55f0b93 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/JpegBlockProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/JpegBlockPostProcessor.cs @@ -12,7 +12,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// Encapsulates the implementation of processing "raw" -s into Jpeg image channels. /// [StructLayout(LayoutKind.Sequential)] - internal unsafe struct JpegBlockProcessor + internal unsafe struct JpegBlockPostProcessor { /// /// The @@ -30,15 +30,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder private int componentIndex; /// - /// Initialize the instance on the stack. + /// Initialize the instance on the stack. /// - /// The instance + /// The instance /// The current component index - public static void Init(JpegBlockProcessor* processor, int componentIndex) + public static void Init(JpegBlockPostProcessor* postProcessor, int componentIndex) { - processor->componentIndex = componentIndex; - processor->data = ComputationData.Create(); - processor->pointers = new DataPointers(&processor->data); + postProcessor->componentIndex = componentIndex; + postProcessor->data = ComputationData.Create(); + postProcessor->pointers = new DataPointers(&postProcessor->data); } /// diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.cs index a7e2e41c9..ec673b6d9 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.cs @@ -669,17 +669,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder int val = bu >= 0 ? bu + delta : bu - delta; Block8x8.SetScalarAt(b, u, (short)val); - - //if (bu >= 0) - //{ - // // b[u] += delta; - // Block8x8.SetScalarAt(b, u, bu + delta); - //} - //else - //{ - // // b[u] -= delta; - // Block8x8.SetScalarAt(b, u, bu - delta); - //} } return zig; diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs index 705a571cd..7b7bf000c 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs @@ -3,23 +3,21 @@ using System; using System.IO; +using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; +using SixLabors.ImageSharp.Formats.Jpeg.Common; using SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.MetaData.Profiles.Exif; using SixLabors.ImageSharp.MetaData.Profiles.Icc; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; using Block8x8F = SixLabors.ImageSharp.Formats.Jpeg.Common.Block8x8F; namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort { - using System.Linq; - - using SixLabors.ImageSharp.Formats.Jpeg.Common; - using SixLabors.Primitives; - /// /// Performs the jpeg decoding operation. /// @@ -476,9 +474,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort this.ComponentCount, componentIndex => { - JpegBlockProcessor processor = default(JpegBlockProcessor); - JpegBlockProcessor.Init(&processor, componentIndex); - processor.ProcessAllBlocks(this); + JpegBlockPostProcessor postProcessor = default(JpegBlockPostProcessor); + JpegBlockPostProcessor.Init(&postProcessor, componentIndex); + postProcessor.ProcessAllBlocks(this); }); } diff --git a/src/ImageSharp/Memory/Buffer2DExtensions.cs b/src/ImageSharp/Memory/Buffer2DExtensions.cs index d9c6801a1..019bf7369 100644 --- a/src/ImageSharp/Memory/Buffer2DExtensions.cs +++ b/src/ImageSharp/Memory/Buffer2DExtensions.cs @@ -3,6 +3,7 @@ using System; using System.Runtime.CompilerServices; +using SixLabors.Primitives; namespace SixLabors.ImageSharp.Memory { @@ -39,5 +40,29 @@ namespace SixLabors.ImageSharp.Memory { return buffer.Span.Slice(y * buffer.Width, buffer.Width); } + + /// + /// Returns the size of the buffer. + /// + /// The element type + /// The + /// The of the buffer + public static Size Size(this IBuffer2D buffer) + where T : struct + { + return new Size(buffer.Width, buffer.Height); + } + + /// + /// Returns a representing the full area of the buffer. + /// + /// The element type + /// The + /// The + public static Rectangle FullRectangle(this IBuffer2D buffer) + where T : struct + { + return new Rectangle(0, 0, buffer.Width, buffer.Height); + } } } \ No newline at end of file diff --git a/src/ImageSharp/Memory/BufferArea2D.cs b/src/ImageSharp/Memory/BufferArea2D.cs new file mode 100644 index 000000000..cb7cc1c63 --- /dev/null +++ b/src/ImageSharp/Memory/BufferArea2D.cs @@ -0,0 +1,30 @@ +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp.Memory +{ + /// + /// Represents a rectangular area inside a 2D memory buffer. (Most commonly ) + /// This type is kind-of 2D Span. + /// + /// The element type + internal struct BufferArea2D + where T : struct + { + public IBuffer2D DestinationBuffer { get; } + + public readonly Rectangle Rectangle; + + public BufferArea2D(IBuffer2D destinationBuffer, Rectangle rectangle) + { + this.DestinationBuffer = destinationBuffer; + this.Rectangle = rectangle; + } + + public BufferArea2D(Buffer2D destinationBuffer) + : this(destinationBuffer, destinationBuffer.FullRectangle()) + { + } + + public Size Size => this.Rectangle.Size; + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Common/Buffer2DTests.cs b/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs similarity index 72% rename from tests/ImageSharp.Tests/Common/Buffer2DTests.cs rename to tests/ImageSharp.Tests/Memory/Buffer2DTests.cs index 2f275b754..0353057ed 100644 --- a/tests/ImageSharp.Tests/Common/Buffer2DTests.cs +++ b/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs @@ -1,14 +1,16 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.Memory; -using Xunit; -using static SixLabors.ImageSharp.Tests.Common.TestStructs; - -namespace SixLabors.ImageSharp.Tests.Common +namespace SixLabors.ImageSharp.Tests.Memory { + using System; + using System.Runtime.CompilerServices; + + using SixLabors.ImageSharp.Memory; + using SixLabors.ImageSharp.Tests.Common; + + using Xunit; + public unsafe class Buffer2DTests { // ReSharper disable once ClassNeverInstantiated.Local @@ -29,7 +31,7 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(1025, 17)] public void Construct(int width, int height) { - using (Buffer2D buffer = new Buffer2D(width, height)) + using (Buffer2D buffer = new Buffer2D(width, height)) { Assert.Equal(width, buffer.Width); Assert.Equal(height, buffer.Height); @@ -42,8 +44,8 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(1025, 17)] public void Construct_FromExternalArray(int width, int height) { - Foo[] array = new Foo[width * height + 10]; - using (Buffer2D buffer = new Buffer2D(array, width, height)) + TestStructs.Foo[] array = new TestStructs.Foo[width * height + 10]; + using (Buffer2D buffer = new Buffer2D(array, width, height)) { Assert.Equal(width, buffer.Width); Assert.Equal(height, buffer.Height); @@ -74,9 +76,9 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(17, 42, 41)] public void GetRowSpanY(int width, int height, int y) { - using (Buffer2D buffer = new Buffer2D(width, height)) + using (Buffer2D buffer = new Buffer2D(width, height)) { - Span span = buffer.GetRowSpan(y); + Span span = buffer.GetRowSpan(y); // Assert.Equal(width * y, span.Start); Assert.Equal(width, span.Length); @@ -90,9 +92,9 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(17, 42, 0, 41)] public void GetRowSpanXY(int width, int height, int x, int y) { - using (Buffer2D buffer = new Buffer2D(width, height)) + using (Buffer2D buffer = new Buffer2D(width, height)) { - Span span = buffer.GetRowSpan(x, y); + Span span = buffer.GetRowSpan(x, y); // Assert.Equal(width * y + x, span.Start); Assert.Equal(width - x, span.Length); @@ -106,13 +108,13 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(99, 88, 98, 87)] public void Indexer(int width, int height, int x, int y) { - using (Buffer2D buffer = new Buffer2D(width, height)) + using (Buffer2D buffer = new Buffer2D(width, height)) { - Foo[] array = buffer.Array; + TestStructs.Foo[] array = buffer.Array; - ref Foo actual = ref buffer[x, y]; + ref TestStructs.Foo actual = ref buffer[x, y]; - ref Foo expected = ref array[y * width + x]; + ref TestStructs.Foo expected = ref array[y * width + x]; Assert.True(Unsafe.AreSame(ref expected, ref actual)); } diff --git a/tests/ImageSharp.Tests/Common/BufferTests.cs b/tests/ImageSharp.Tests/Memory/BufferTests.cs similarity index 69% rename from tests/ImageSharp.Tests/Common/BufferTests.cs rename to tests/ImageSharp.Tests/Memory/BufferTests.cs index e1883ec7f..e1efeb24e 100644 --- a/tests/ImageSharp.Tests/Common/BufferTests.cs +++ b/tests/ImageSharp.Tests/Memory/BufferTests.cs @@ -1,16 +1,15 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Threading.Tasks; -using SixLabors.ImageSharp.Memory; -using Xunit; -using static SixLabors.ImageSharp.Tests.Common.TestStructs; - -namespace SixLabors.ImageSharp.Tests.Common +namespace SixLabors.ImageSharp.Tests.Memory { + using System; + using System.Runtime.CompilerServices; + + using SixLabors.ImageSharp.Memory; + + using Xunit; + public unsafe class BufferTests { // ReSharper disable once ClassNeverInstantiated.Local @@ -36,7 +35,7 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(1111)] public void ConstructWithOwnArray(int count) { - using (Buffer buffer = new Buffer(count)) + using (Buffer buffer = new Buffer(count)) { Assert.False(buffer.IsDisposedOrLostArrayOwnership); Assert.NotNull(buffer.Array); @@ -50,8 +49,8 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(1111)] public void ConstructWithExistingArray(int count) { - Foo[] array = new Foo[count]; - using (Buffer buffer = new Buffer(array)) + TestStructs.Foo[] array = new TestStructs.Foo[count]; + using (Buffer buffer = new Buffer(array)) { Assert.False(buffer.IsDisposedOrLostArrayOwnership); Assert.Equal(array, buffer.Array); @@ -62,13 +61,13 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void Clear() { - Foo[] a = { new Foo() { A = 1, B = 2 }, new Foo() { A = 3, B = 4 } }; - using (Buffer buffer = new Buffer(a)) + TestStructs.Foo[] a = { new TestStructs.Foo() { A = 1, B = 2 }, new TestStructs.Foo() { A = 3, B = 4 } }; + using (Buffer buffer = new Buffer(a)) { buffer.Clear(); - Assert.Equal(default(Foo), a[0]); - Assert.Equal(default(Foo), a[1]); + Assert.Equal(default(TestStructs.Foo), a[0]); + Assert.Equal(default(TestStructs.Foo), a[1]); } } @@ -102,11 +101,11 @@ namespace SixLabors.ImageSharp.Tests.Common [MemberData(nameof(IndexerData))] public void Read(int length, int index) { - Foo[] a = Foo.CreateArray(length); + TestStructs.Foo[] a = TestStructs.Foo.CreateArray(length); - using (Buffer buffer = new Buffer(a)) + using (Buffer buffer = new Buffer(a)) { - Foo element = buffer[index]; + TestStructs.Foo element = buffer[index]; Assert.Equal(a[index], element); } @@ -116,13 +115,13 @@ namespace SixLabors.ImageSharp.Tests.Common [MemberData(nameof(IndexerData))] public void Write(int length, int index) { - Foo[] a = Foo.CreateArray(length); + TestStructs.Foo[] a = TestStructs.Foo.CreateArray(length); - using (Buffer buffer = new Buffer(a)) + using (Buffer buffer = new Buffer(a)) { - buffer[index] = new Foo(666, 666); + buffer[index] = new TestStructs.Foo(666, 666); - Assert.Equal(new Foo(666, 666), a[index]); + Assert.Equal(new TestStructs.Foo(666, 666), a[index]); } } } @@ -130,7 +129,7 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void Dispose() { - Buffer buffer = new Buffer(42); + Buffer buffer = new Buffer(42); buffer.Dispose(); Assert.True(buffer.IsDisposedOrLostArrayOwnership); @@ -141,9 +140,9 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(123)] public void CastToSpan(int bufferLength) { - using (Buffer buffer = new Buffer(bufferLength)) + using (Buffer buffer = new Buffer(bufferLength)) { - Span span = buffer; + Span span = buffer; //Assert.Equal(buffer.Array, span.ToArray()); //Assert.Equal(0, span.Start); @@ -155,9 +154,9 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void Span() { - using (Buffer buffer = new Buffer(42)) + using (Buffer buffer = new Buffer(42)) { - Span span = buffer.Span; + Span span = buffer.Span; // Assert.Equal(buffer.Array, span.ToArray()); // Assert.Equal(0, span.Start); @@ -174,9 +173,9 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(123, 17)] public void WithStartOnly(int bufferLength, int start) { - using (Buffer buffer = new Buffer(bufferLength)) + using (Buffer buffer = new Buffer(bufferLength)) { - Span span = buffer.Slice(start); + Span span = buffer.Slice(start); Assert.SpanPointsTo(span, buffer, start); Assert.Equal(span.Length, bufferLength - start); @@ -188,9 +187,9 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(123, 17, 42)] public void WithStartAndLength(int bufferLength, int start, int spanLength) { - using (Buffer buffer = new Buffer(bufferLength)) + using (Buffer buffer = new Buffer(bufferLength)) { - Span span = buffer.Slice(start, spanLength); + Span span = buffer.Slice(start, spanLength); Assert.SpanPointsTo(span, buffer, start); Assert.Equal(span.Length, spanLength); @@ -201,8 +200,8 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void UnPinAndTakeArrayOwnership() { - Foo[] data = null; - using (Buffer buffer = new Buffer(42)) + TestStructs.Foo[] data = null; + using (Buffer buffer = new Buffer(42)) { data = buffer.TakeArrayOwnership(); Assert.True(buffer.IsDisposedOrLostArrayOwnership); @@ -217,10 +216,10 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void ReturnsPinnedPointerToTheBeginningOfArray() { - using (Buffer buffer = new Buffer(42)) + using (Buffer buffer = new Buffer(42)) { - Foo* actual = (Foo*)buffer.Pin(); - fixed (Foo* expected = buffer.Array) + TestStructs.Foo* actual = (TestStructs.Foo*)buffer.Pin(); + fixed (TestStructs.Foo* expected = buffer.Array) { Assert.Equal(expected, actual); } @@ -230,7 +229,7 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void SecondCallReturnsTheSamePointer() { - using (Buffer buffer = new Buffer(42)) + using (Buffer buffer = new Buffer(42)) { IntPtr ptr1 = buffer.Pin(); IntPtr ptr2 = buffer.Pin(); @@ -242,7 +241,7 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void WhenCalledOnDisposedBuffer_ThrowsInvalidOperationException() { - Buffer buffer = new Buffer(42); + Buffer buffer = new Buffer(42); buffer.Dispose(); Assert.Throws(() => buffer.Pin()); diff --git a/tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs b/tests/ImageSharp.Tests/Memory/Fast2DArrayTests.cs similarity index 95% rename from tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs rename to tests/ImageSharp.Tests/Memory/Fast2DArrayTests.cs index 88d8a73e8..5cdbe638a 100644 --- a/tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs +++ b/tests/ImageSharp.Tests/Memory/Fast2DArrayTests.cs @@ -1,12 +1,14 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using SixLabors.ImageSharp.Memory; -using Xunit; - -namespace SixLabors.ImageSharp.Tests.Common +namespace SixLabors.ImageSharp.Tests.Memory { + using System; + + using SixLabors.ImageSharp.Memory; + + using Xunit; + public class Fast2DArrayTests { private static readonly float[,] FloydSteinbergMatrix = diff --git a/tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs b/tests/ImageSharp.Tests/Memory/PixelDataPoolTests.cs similarity index 89% rename from tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs rename to tests/ImageSharp.Tests/Memory/PixelDataPoolTests.cs index 7b3d337ba..fdfd4c4b7 100644 --- a/tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs +++ b/tests/ImageSharp.Tests/Memory/PixelDataPoolTests.cs @@ -1,14 +1,15 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Linq; -using SixLabors.ImageSharp.Memory; -using SixLabors.ImageSharp.PixelFormats; -using Xunit; + // ReSharper disable InconsistentNaming -namespace SixLabors.ImageSharp.Tests +namespace SixLabors.ImageSharp.Tests.Memory { + using SixLabors.ImageSharp.Memory; + + using Xunit; + /// /// Tests the class. /// diff --git a/tests/ImageSharp.Tests/Common/BufferSpanTests.cs b/tests/ImageSharp.Tests/Memory/SpanUtilityTests.cs similarity index 63% rename from tests/ImageSharp.Tests/Common/BufferSpanTests.cs rename to tests/ImageSharp.Tests/Memory/SpanUtilityTests.cs index fb51880f3..395c32546 100644 --- a/tests/ImageSharp.Tests/Common/BufferSpanTests.cs +++ b/tests/ImageSharp.Tests/Memory/SpanUtilityTests.cs @@ -1,17 +1,18 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.Memory; -using SixLabors.ImageSharp.PixelFormats; -using Xunit; -using static SixLabors.ImageSharp.Tests.Common.TestStructs; - -namespace SixLabors.ImageSharp.Tests.Common +namespace SixLabors.ImageSharp.Tests.Memory { - public unsafe class SpanTests + using System; + using System.Numerics; + using System.Runtime.CompilerServices; + + using SixLabors.ImageSharp.Memory; + using SixLabors.ImageSharp.Tests.Common; + + using Xunit; + + public unsafe class SpanUtilityTests { // ReSharper disable once ClassNeverInstantiated.Local private class Assert : Xunit.Assert @@ -20,7 +21,7 @@ namespace SixLabors.ImageSharp.Tests.Common { ref T1 bb = ref Unsafe.As(ref b); - True(Unsafe.AreSame(ref a, ref bb), "References are not same!"); + Assert.True(Unsafe.AreSame(ref a, ref bb), "References are not same!"); } } @@ -42,15 +43,15 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void AsBytes() { - Foo[] fooz = { new Foo(1, 2), new Foo(3, 4), new Foo(5, 6) }; + TestStructs.Foo[] fooz = { new TestStructs.Foo(1, 2), new TestStructs.Foo(3, 4), new TestStructs.Foo(5, 6) }; - using (Buffer colorBuf = new Buffer(fooz)) + using (Buffer colorBuf = new Buffer(fooz)) { - Span orig = colorBuf.Slice(1); + Span orig = colorBuf.Slice(1); Span asBytes = orig.AsBytes(); // Assert.Equal(asBytes.Start, sizeof(Foo)); - Assert.Equal(orig.Length * Unsafe.SizeOf(), asBytes.Length); + Assert.Equal(orig.Length * Unsafe.SizeOf(), asBytes.Length); Assert.SameRefs(ref orig.DangerousGetPinnableReference(), ref asBytes.DangerousGetPinnableReference()); } } @@ -60,10 +61,10 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void Basic() { - Foo[] array = Foo.CreateArray(3); + TestStructs.Foo[] array = TestStructs.Foo.CreateArray(3); // Act: - Span span = new Span(array); + Span span = new Span(array); // Assert: Assert.Equal(array, span.ToArray()); @@ -74,11 +75,11 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void WithStart() { - Foo[] array = Foo.CreateArray(4); + TestStructs.Foo[] array = TestStructs.Foo.CreateArray(4); int start = 2; // Act: - Span span = new Span(array, start); + Span span = new Span(array, start); // Assert: Assert.SameRefs(ref array[start], ref span.DangerousGetPinnableReference()); @@ -88,11 +89,11 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void WithStartAndLength() { - Foo[] array = Foo.CreateArray(10); + TestStructs.Foo[] array = TestStructs.Foo.CreateArray(10); int start = 2; int length = 3; // Act: - Span span = new Span(array, start, length); + Span span = new Span(array, start, length); // Assert: Assert.SameRefs(ref array[start], ref span.DangerousGetPinnableReference()); @@ -105,12 +106,12 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void StartOnly() { - Foo[] array = Foo.CreateArray(5); + TestStructs.Foo[] array = TestStructs.Foo.CreateArray(5); int start0 = 2; int start1 = 2; int totalOffset = start0 + start1; - Span span = new Span(array, start0); + Span span = new Span(array, start0); // Act: span = span.Slice(start1); @@ -123,13 +124,13 @@ namespace SixLabors.ImageSharp.Tests.Common [Fact] public void StartAndLength() { - Foo[] array = Foo.CreateArray(10); + TestStructs.Foo[] array = TestStructs.Foo.CreateArray(10); int start0 = 2; int start1 = 2; int totalOffset = start0 + start1; int sliceLength = 3; - Span span = new Span(array, start0); + Span span = new Span(array, start0); // Act: span = span.Slice(start1, sliceLength); @@ -176,10 +177,10 @@ namespace SixLabors.ImageSharp.Tests.Common [MemberData(nameof(IndexerData))] public void Read(int length, int start, int index) { - Foo[] a = Foo.CreateArray(length); - Span span = new Span(a, start); + TestStructs.Foo[] a = TestStructs.Foo.CreateArray(length); + Span span = new Span(a, start); - Foo element = span[index]; + TestStructs.Foo element = span[index]; Assert.Equal(a[start + index], element); } @@ -188,12 +189,12 @@ namespace SixLabors.ImageSharp.Tests.Common [MemberData(nameof(IndexerData))] public void Write(int length, int start, int index) { - Foo[] a = Foo.CreateArray(length); - Span span = new Span(a, start); + TestStructs.Foo[] a = TestStructs.Foo.CreateArray(length); + Span span = new Span(a, start); - span[index] = new Foo(666, 666); + span[index] = new TestStructs.Foo(666, 666); - Assert.Equal(new Foo(666, 666), a[start + index]); + Assert.Equal(new TestStructs.Foo(666, 666), a[start + index]); } [Theory] @@ -203,15 +204,15 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(10, 1, 1, 7)] public void AsBytes_Read(int length, int start, int index, int byteOffset) { - Foo[] a = Foo.CreateArray(length); - Span span = new Span(a, start); + TestStructs.Foo[] a = TestStructs.Foo.CreateArray(length); + Span span = new Span(a, start); Span bytes = span.AsBytes(); - byte actual = bytes[index * Unsafe.SizeOf() + byteOffset]; + byte actual = bytes[index * Unsafe.SizeOf() + byteOffset]; - ref byte baseRef = ref Unsafe.As(ref a[0]); - byte expected = Unsafe.Add(ref baseRef, (start + index) * Unsafe.SizeOf() + byteOffset); + ref byte baseRef = ref Unsafe.As(ref a[0]); + byte expected = Unsafe.Add(ref baseRef, (start + index) * Unsafe.SizeOf() + byteOffset); Assert.Equal(expected, actual); } @@ -223,9 +224,9 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(3, 4)] public void DangerousGetPinnableReference(int start, int length) { - Foo[] a = Foo.CreateArray(length); - Span span = new Span(a, start); - ref Foo r = ref span.DangerousGetPinnableReference(); + TestStructs.Foo[] a = TestStructs.Foo.CreateArray(length); + Span span = new Span(a, start); + ref TestStructs.Foo r = ref span.DangerousGetPinnableReference(); Assert.True(Unsafe.AreSame(ref a[start], ref r)); } @@ -263,11 +264,11 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(1500)] public void GenericToOwnType(int count) { - Foo[] source = Foo.CreateArray(count + 2); - Foo[] dest = new Foo[count + 5]; + TestStructs.Foo[] source = TestStructs.Foo.CreateArray(count + 2); + TestStructs.Foo[] dest = new TestStructs.Foo[count + 5]; - Span apSource = new Span(source, 1); - Span apDest = new Span(dest, 1); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, 1); SpanHelper.Copy(apSource, apDest, count - 1); @@ -286,11 +287,11 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(1500)] public void GenericToOwnType_Aligned(int count) { - AlignedFoo[] source = AlignedFoo.CreateArray(count + 2); - AlignedFoo[] dest = new AlignedFoo[count + 5]; + TestStructs.AlignedFoo[] source = TestStructs.AlignedFoo.CreateArray(count + 2); + TestStructs.AlignedFoo[] dest = new TestStructs.AlignedFoo[count + 5]; - Span apSource = new Span(source, 1); - Span apDest = new Span(dest, 1); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, 1); SpanHelper.Copy(apSource, apDest, count - 1); @@ -332,22 +333,22 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(1500)] public void GenericToBytes(int count) { - int destCount = count * sizeof(Foo); - Foo[] source = Foo.CreateArray(count + 2); - byte[] dest = new byte[destCount + sizeof(Foo) * 2]; + int destCount = count * sizeof(TestStructs.Foo); + TestStructs.Foo[] source = TestStructs.Foo.CreateArray(count + 2); + byte[] dest = new byte[destCount + sizeof(TestStructs.Foo) * 2]; - Span apSource = new Span(source, 1); - Span apDest = new Span(dest, sizeof(Foo)); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, sizeof(TestStructs.Foo)); - SpanHelper.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(Foo)); + SpanHelper.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(TestStructs.Foo)); AssertNotDefault(source, 1); - Assert.False(ElementsAreEqual(source, dest, 0)); - Assert.True(ElementsAreEqual(source, dest, 1)); - Assert.True(ElementsAreEqual(source, dest, 2)); - Assert.True(ElementsAreEqual(source, dest, count - 1)); - Assert.False(ElementsAreEqual(source, dest, count)); + Assert.False((bool)ElementsAreEqual(source, dest, 0)); + Assert.True((bool)ElementsAreEqual(source, dest, 1)); + Assert.True((bool)ElementsAreEqual(source, dest, 2)); + Assert.True((bool)ElementsAreEqual(source, dest, count - 1)); + Assert.False((bool)ElementsAreEqual(source, dest, count)); } [Theory] @@ -355,22 +356,22 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(1500)] public void GenericToBytes_Aligned(int count) { - int destCount = count * sizeof(Foo); - AlignedFoo[] source = AlignedFoo.CreateArray(count + 2); - byte[] dest = new byte[destCount + sizeof(AlignedFoo) * 2]; + int destCount = count * sizeof(TestStructs.Foo); + TestStructs.AlignedFoo[] source = TestStructs.AlignedFoo.CreateArray(count + 2); + byte[] dest = new byte[destCount + sizeof(TestStructs.AlignedFoo) * 2]; - Span apSource = new Span(source, 1); - Span apDest = new Span(dest, sizeof(AlignedFoo)); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, sizeof(TestStructs.AlignedFoo)); - SpanHelper.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(AlignedFoo)); + SpanHelper.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(TestStructs.AlignedFoo)); AssertNotDefault(source, 1); - Assert.False(ElementsAreEqual(source, dest, 0)); - Assert.True(ElementsAreEqual(source, dest, 1)); - Assert.True(ElementsAreEqual(source, dest, 2)); - Assert.True(ElementsAreEqual(source, dest, count - 1)); - Assert.False(ElementsAreEqual(source, dest, count)); + Assert.False((bool)ElementsAreEqual(source, dest, 0)); + Assert.True((bool)ElementsAreEqual(source, dest, 1)); + Assert.True((bool)ElementsAreEqual(source, dest, 2)); + Assert.True((bool)ElementsAreEqual(source, dest, count - 1)); + Assert.False((bool)ElementsAreEqual(source, dest, count)); } [Theory] @@ -389,9 +390,9 @@ namespace SixLabors.ImageSharp.Tests.Common AssertNotDefault(source, 1); - Assert.True(ElementsAreEqual(source, dest, 0)); - Assert.True(ElementsAreEqual(source, dest, count - 1)); - Assert.False(ElementsAreEqual(source, dest, count)); + Assert.True((bool)ElementsAreEqual(source, dest, 0)); + Assert.True((bool)ElementsAreEqual(source, dest, count - 1)); + Assert.False((bool)ElementsAreEqual(source, dest, count)); } [Theory] @@ -399,22 +400,22 @@ namespace SixLabors.ImageSharp.Tests.Common [InlineData(1500)] public void BytesToGeneric(int count) { - int srcCount = count * sizeof(Foo); + int srcCount = count * sizeof(TestStructs.Foo); byte[] source = CreateTestBytes(srcCount); - Foo[] dest = new Foo[count + 2]; + TestStructs.Foo[] dest = new TestStructs.Foo[count + 2]; Span apSource = new Span(source); - Span apDest = new Span(dest); + Span apDest = new Span(dest); - SpanHelper.Copy(apSource, apDest.AsBytes(), count * sizeof(Foo)); + SpanHelper.Copy(apSource, apDest.AsBytes(), count * sizeof(TestStructs.Foo)); - AssertNotDefault(source, sizeof(Foo) + 1); + AssertNotDefault(source, sizeof(TestStructs.Foo) + 1); AssertNotDefault(dest, 1); - Assert.True(ElementsAreEqual(dest, source, 0)); - Assert.True(ElementsAreEqual(dest, source, 1)); - Assert.True(ElementsAreEqual(dest, source, count - 1)); - Assert.False(ElementsAreEqual(dest, source, count)); + Assert.True((bool)ElementsAreEqual(dest, source, 0)); + Assert.True((bool)ElementsAreEqual(dest, source, 1)); + Assert.True((bool)ElementsAreEqual(dest, source, count - 1)); + Assert.False((bool)ElementsAreEqual(dest, source, count)); } [Fact] @@ -436,29 +437,29 @@ namespace SixLabors.ImageSharp.Tests.Common } } - internal static bool ElementsAreEqual(Foo[] array, byte[] rawArray, int index) + internal static bool ElementsAreEqual(TestStructs.Foo[] array, byte[] rawArray, int index) { - fixed (Foo* pArray = array) + fixed (TestStructs.Foo* pArray = array) fixed (byte* pRaw = rawArray) { - Foo* pCasted = (Foo*)pRaw; + TestStructs.Foo* pCasted = (TestStructs.Foo*)pRaw; - Foo val1 = pArray[index]; - Foo val2 = pCasted[index]; + TestStructs.Foo val1 = pArray[index]; + TestStructs.Foo val2 = pCasted[index]; return val1.Equals(val2); } } - internal static bool ElementsAreEqual(AlignedFoo[] array, byte[] rawArray, int index) + internal static bool ElementsAreEqual(TestStructs.AlignedFoo[] array, byte[] rawArray, int index) { - fixed (AlignedFoo* pArray = array) + fixed (TestStructs.AlignedFoo* pArray = array) fixed (byte* pRaw = rawArray) { - AlignedFoo* pCasted = (AlignedFoo*)pRaw; + TestStructs.AlignedFoo* pCasted = (TestStructs.AlignedFoo*)pRaw; - AlignedFoo val1 = pArray[index]; - AlignedFoo val2 = pCasted[index]; + TestStructs.AlignedFoo val1 = pArray[index]; + TestStructs.AlignedFoo val2 = pCasted[index]; return val1.Equals(val2); } diff --git a/tests/ImageSharp.Tests/Common/TestStructs.cs b/tests/ImageSharp.Tests/Memory/TestStructs.cs similarity index 96% rename from tests/ImageSharp.Tests/Common/TestStructs.cs rename to tests/ImageSharp.Tests/Memory/TestStructs.cs index 87cf9a632..608e3c6cb 100644 --- a/tests/ImageSharp.Tests/Common/TestStructs.cs +++ b/tests/ImageSharp.Tests/Memory/TestStructs.cs @@ -1,10 +1,10 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using Xunit; - -namespace SixLabors.ImageSharp.Tests.Common +namespace SixLabors.ImageSharp.Tests.Memory { + using Xunit; + public static class TestStructs { public struct Foo