From 7c7af28da6027877729124145d785614927ffdda Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Tue, 19 Sep 2017 21:26:42 +0100 Subject: [PATCH] Add unit tests --- tests/ImageSharp.Tests/Image/ImageTests.cs | 60 +++++++++++++++++++ .../ImageProviders/TestPatternProvider.cs | 21 ++++--- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/tests/ImageSharp.Tests/Image/ImageTests.cs b/tests/ImageSharp.Tests/Image/ImageTests.cs index a9952f5c48..00338e0bd0 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.PixelFormats; @@ -109,5 +110,64 @@ namespace SixLabors.ImageSharp.Tests Assert.Equal("image/png", mime.DefaultMimeType); } } + + [Theory] + [WithTestPatternImages(10, 10, PixelTypes.Rgba32)] + public void CloneFrame(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image img = provider.GetImage()) + { + img.Frames.Add(new ImageFrame(10, 10));// add a frame anyway + using (Image cloned = img.Clone(0)) + { + Assert.Equal(2, img.Frames.Count); + cloned.ComparePixelBufferTo(img.GetPixelSpan()); + } + } + } + + [Theory] + [WithTestPatternImages(10, 10, PixelTypes.Rgba32)] + public void CloneFrameAs(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image img = provider.GetImage()) + { + img.Frames.Add(new ImageFrame(10, 10));// add a frame anyway + using (Image cloned = img.CloneAs(0)) + { + for (var x = 0; x < img.Width; x++) + { + for (var y = 0; y < img.Height; y++) + { + Bgra32 pixelClone = cloned[x, y]; + Bgra32 pixelSource = default(Bgra32); + img[x, y].ToBgra32(ref pixelSource); + Assert.Equal(pixelSource, pixelClone); + } + } + Assert.Equal(2, img.Frames.Count); + } + } + } + + [Theory] + [WithTestPatternImages(10, 10, PixelTypes.Rgba32)] + public void ExtractFrame(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image img = provider.GetImage()) + { + var sourcePixelData = img.GetPixelSpan().ToArray(); + + img.Frames.Add(new ImageFrame(10, 10)); + using (Image cloned = img.Extract(0)) + { + Assert.Equal(1, img.Frames.Count); + cloned.ComparePixelBufferTo(sourcePixelData); + } + } + } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs index 6cee83566a..0b48170879 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs @@ -77,6 +77,11 @@ namespace SixLabors.ImageSharp.Tests int top = 0; int bottom = pixels.Height / 2; int stride = pixels.Width / 12; + if (stride < 1) + { + stride = 1; + } + TPixel[] c = { NamedColors.HotPink, NamedColors.Blue @@ -197,14 +202,14 @@ namespace SixLabors.ImageSharp.Tests Rgba32 t = new Rgba32(0); for (int x = left; x < right; x++) - for (int y = top; y < bottom; y++) - { - t.PackedValue += stepsPerPixel; - Vector4 v = t.ToVector4(); - //v.W = (x - left) / (float)left; - c.PackFromVector4(v); - pixels[x, y] = c; - } + for (int y = top; y < bottom; y++) + { + t.PackedValue += stepsPerPixel; + Vector4 v = t.ToVector4(); + //v.W = (x - left) / (float)left; + c.PackFromVector4(v); + pixels[x, y] = c; + } } } }