Browse Source

[SL.Core] Merge pull request SixLabors/Core#26 from synercoder/deconstructors

Added deconstructors to the primitives
pull/1087/head
James Jackson-South 7 years ago
committed by GitHub
parent
commit
b3f3e86804
  1. 13
      src/SixLabors.Core/Primitives/Point.cs
  2. 13
      src/SixLabors.Core/Primitives/PointF.cs
  3. 17
      src/SixLabors.Core/Primitives/Rectangle.cs
  4. 17
      src/SixLabors.Core/Primitives/RectangleF.cs
  5. 13
      src/SixLabors.Core/Primitives/Size.cs
  6. 13
      src/SixLabors.Core/Primitives/SizeF.cs
  7. 17
      tests/SixLabors.Core.Tests/Primitives/PointFTests.cs
  8. 17
      tests/SixLabors.Core.Tests/Primitives/PointTests.cs
  9. 32
      tests/SixLabors.Core.Tests/Primitives/RectangleFTests.cs
  10. 32
      tests/SixLabors.Core.Tests/Primitives/RectangleTests.cs
  11. 15
      tests/SixLabors.Core.Tests/Primitives/SizeFTests.cs
  12. 17
      tests/SixLabors.Core.Tests/Primitives/SizeTests.cs

13
src/SixLabors.Core/Primitives/Point.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -235,6 +235,17 @@ namespace SixLabors.Primitives
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point Transform(Point point, Matrix3x2 matrix) => Round(Vector2.Transform(new Vector2(point.X, point.Y), matrix));
/// <summary>
/// Deconstructs this point into two integers
/// </summary>
/// <param name="x">The out value for X</param>
/// <param name="y">The out value for Y</param>
public void Deconstruct(out int x, out int y)
{
x = this.X;
y = this.Y;
}
/// <summary>
/// Translates this <see cref="Point"/> by the specified amount.
/// </summary>

13
src/SixLabors.Core/Primitives/PointF.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -247,6 +247,17 @@ namespace SixLabors.Primitives
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PointF Transform(PointF point, Matrix3x2 matrix) => Vector2.Transform(point, matrix);
/// <summary>
/// Deconstructs this point into two floats
/// </summary>
/// <param name="x">The out value for X</param>
/// <param name="y">The out value for Y</param>
public void Deconstruct(out float x, out float y)
{
x = this.X;
y = this.Y;
}
/// <summary>
/// Translates this <see cref="PointF"/> by the specified amount.
/// </summary>

17
src/SixLabors.Core/Primitives/Rectangle.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -320,6 +320,21 @@ namespace SixLabors.Primitives
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
/// <summary>
/// Deconstructs this rectangle into four integers
/// </summary>
/// <param name="x">The out value for X</param>
/// <param name="y">The out value for Y</param>
/// <param name="width">The out value for the width</param>
/// <param name="height">The out value for the height</param>
public void Deconstruct(out int x, out int y, out int width, out int height)
{
x = this.X;
y = this.Y;
width = this.Width;
height = this.Height;
}
/// <summary>
/// Creates a Rectangle that represents the intersection between this Rectangle and the <paramref name="rectangle"/>.
/// </summary>

17
src/SixLabors.Core/Primitives/RectangleF.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -259,6 +259,21 @@ namespace SixLabors.Primitives
return new RectangleF(x1, y1, x2 - x1, y2 - y1);
}
/// <summary>
/// Deconstructs this rectangle into four floats
/// </summary>
/// <param name="x">The out value for X</param>
/// <param name="y">The out value for Y</param>
/// <param name="width">The out value for the width</param>
/// <param name="height">The out value for the height</param>
public void Deconstruct(out float x, out float y, out float width, out float height)
{
x = this.X;
y = this.Y;
width = this.Width;
height = this.Height;
}
/// <summary>
/// Creates a RectangleF that represents the intersection between this RectangleF and the <paramref name="rectangle"/>.
/// </summary>

13
src/SixLabors.Core/Primitives/Size.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -251,6 +251,17 @@ namespace SixLabors.Primitives
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Size Truncate(SizeF size) => new Size(unchecked((int)size.Width), unchecked((int)size.Height));
/// <summary>
/// Deconstructs this size into two integers
/// </summary>
/// <param name="width">The out value for the width</param>
/// <param name="height">The out value for the height</param>
public void Deconstruct(out int width, out int height)
{
width = this.Width;
height = this.Height;
}
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(this.Width, this.Height);

13
src/SixLabors.Core/Primitives/SizeF.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -197,6 +197,17 @@ namespace SixLabors.Primitives
return new SizeF(v.X, v.Y);
}
/// <summary>
/// Deconstructs this size into two floats
/// </summary>
/// <param name="width">The out value for the width</param>
/// <param name="height">The out value for the height</param>
public void Deconstruct(out float width, out float height)
{
width = this.Width;
height = this.Height;
}
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(this.Width, this.Height);

17
tests/SixLabors.Core.Tests/Primitives/PointFTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -191,5 +191,20 @@ namespace SixLabors.Primitives.Tests
var p = new PointF(5.1F, -5.123F);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "PointF [ X={0}, Y={1} ]", p.X, p.Y), p.ToString());
}
[Theory]
[InlineData(float.MaxValue, float.MinValue)]
[InlineData(float.MinValue, float.MinValue)]
[InlineData(float.MaxValue, float.MaxValue)]
[InlineData(0, 0)]
public void DeconstructTest(float x, float y)
{
PointF p = new PointF(x, y);
(float deconstructedX, float deconstructedY) = p;
Assert.Equal(x, deconstructedX);
Assert.Equal(y, deconstructedY);
}
}
}

17
tests/SixLabors.Core.Tests/Primitives/PointTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -239,5 +239,20 @@ namespace SixLabors.Primitives.Tests
var p = new Point(5, -5);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "Point [ X={0}, Y={1} ]", p.X, p.Y), p.ToString());
}
[Theory]
[InlineData(int.MaxValue, int.MinValue)]
[InlineData(int.MinValue, int.MinValue)]
[InlineData(int.MaxValue, int.MaxValue)]
[InlineData(0, 0)]
public void DeconstructTest(int x, int y)
{
Point p = new Point(x, y);
(int deconstructedX, int deconstructedY) = p;
Assert.Equal(x, deconstructedX);
Assert.Equal(y, deconstructedY);
}
}
}

32
tests/SixLabors.Core.Tests/Primitives/RectangleFTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -252,5 +252,35 @@ namespace SixLabors.Primitives.Tests
var r = new RectangleF(5, 5.1F, 1.3F, 1);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "RectangleF [ X={0}, Y={1}, Width={2}, Height={3} ]", r.X, r.Y, r.Width, r.Height), r.ToString());
}
[Theory]
[InlineData(float.MinValue, float.MaxValue, float.MaxValue, float.MaxValue)]
[InlineData(float.MinValue, float.MaxValue, float.MaxValue, float.MinValue)]
[InlineData(float.MinValue, float.MaxValue, float.MinValue, float.MaxValue)]
[InlineData(float.MinValue, float.MaxValue, float.MinValue, float.MinValue)]
[InlineData(float.MinValue, float.MinValue, float.MaxValue, float.MaxValue)]
[InlineData(float.MinValue, float.MinValue, float.MaxValue, float.MinValue)]
[InlineData(float.MinValue, float.MinValue, float.MinValue, float.MaxValue)]
[InlineData(float.MinValue, float.MinValue, float.MinValue, float.MinValue)]
[InlineData(float.MaxValue, float.MaxValue, float.MaxValue, float.MaxValue)]
[InlineData(float.MaxValue, float.MaxValue, float.MaxValue, float.MinValue)]
[InlineData(float.MaxValue, float.MaxValue, float.MinValue, float.MaxValue)]
[InlineData(float.MaxValue, float.MaxValue, float.MinValue, float.MinValue)]
[InlineData(float.MaxValue, float.MinValue, float.MaxValue, float.MaxValue)]
[InlineData(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue)]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MinValue)]
[InlineData(0, 0, 0, 0)]
public void DeconstructTest(float x, float y, float width, float height)
{
RectangleF r = new RectangleF(x, y, width, height);
(float dx, float dy, float dw, float dh) = r;
Assert.Equal(x, dx);
Assert.Equal(y, dy);
Assert.Equal(width, dw);
Assert.Equal(height, dh);
}
}
}

32
tests/SixLabors.Core.Tests/Primitives/RectangleTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -294,5 +294,35 @@ namespace SixLabors.Primitives.Tests
var r = new Rectangle(5, -5, 0, 1);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "Rectangle [ X={0}, Y={1}, Width={2}, Height={3} ]", r.X, r.Y, r.Width, r.Height), r.ToString());
}
[Theory]
[InlineData(int.MinValue, int.MaxValue, int.MaxValue, int.MaxValue)]
[InlineData(int.MinValue, int.MaxValue, int.MaxValue, int.MinValue)]
[InlineData(int.MinValue, int.MaxValue, int.MinValue, int.MaxValue)]
[InlineData(int.MinValue, int.MaxValue, int.MinValue, int.MinValue)]
[InlineData(int.MinValue, int.MinValue, int.MaxValue, int.MaxValue)]
[InlineData(int.MinValue, int.MinValue, int.MaxValue, int.MinValue)]
[InlineData(int.MinValue, int.MinValue, int.MinValue, int.MaxValue)]
[InlineData(int.MinValue, int.MinValue, int.MinValue, int.MinValue)]
[InlineData(int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue)]
[InlineData(int.MaxValue, int.MaxValue, int.MaxValue, int.MinValue)]
[InlineData(int.MaxValue, int.MaxValue, int.MinValue, int.MaxValue)]
[InlineData(int.MaxValue, int.MaxValue, int.MinValue, int.MinValue)]
[InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MaxValue)]
[InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)]
[InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)]
[InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MinValue)]
[InlineData(0, 0, 0, 0)]
public void DeconstructTest(int x, int y, int width, int height)
{
Rectangle r = new Rectangle(x, y, width, height);
(int dx, int dy, int dw, int dh) = r;
Assert.Equal(x, dx);
Assert.Equal(y, dy);
Assert.Equal(width, dw);
Assert.Equal(height, dh);
}
}
}

15
tests/SixLabors.Core.Tests/Primitives/SizeFTests.cs

@ -231,5 +231,20 @@ namespace SixLabors.Primitives.Tests
SizeF expected = new SizeF(width / divisor, height / divisor);
Assert.Equal(expected, size / divisor);
}
[Theory]
[InlineData(float.MaxValue, float.MinValue)]
[InlineData(float.MinValue, float.MinValue)]
[InlineData(float.MaxValue, float.MaxValue)]
[InlineData(0, 0)]
public void DeconstructTest(float width, float height)
{
SizeF s = new SizeF(width, height);
(float deconstructedWidth, float deconstructedHeight) = s;
Assert.Equal(width, deconstructedWidth);
Assert.Equal(height, deconstructedHeight);
}
}
}

17
tests/SixLabors.Core.Tests/Primitives/SizeTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -360,5 +360,20 @@ namespace SixLabors.Primitives.Tests
expected = new SizeF(width / divisor, height / divisor);
Assert.Equal(expected, size / divisor);
}
[Theory]
[InlineData(int.MaxValue, int.MinValue)]
[InlineData(int.MinValue, int.MinValue)]
[InlineData(int.MaxValue, int.MaxValue)]
[InlineData(0, 0)]
public void DeconstructTest(int width, int height)
{
Size s = new Size(width, height);
(int deconstructedWidth, int deconstructedHeight) = s;
Assert.Equal(width, deconstructedWidth);
Assert.Equal(height, deconstructedHeight);
}
}
}
Loading…
Cancel
Save