Skip to content

Commit

Permalink
Merge pull request dotnet/corefx#6099 from hughbe/drawing-primitive-t…
Browse files Browse the repository at this point in the history
…ests

Add some more tests to Drawing.Primitives

Commit migrated from dotnet/corefx@2cfbb08
  • Loading branch information
stephentoub committed Feb 14, 2016
2 parents 13d2ec4 + d8a4ca9 commit 54a9b90
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 12 deletions.
31 changes: 29 additions & 2 deletions src/libraries/System.Drawing.Primitives/tests/PointFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Globalization;

using Xunit;

namespace System.Drawing.PrimitivesTests
Expand Down Expand Up @@ -56,6 +57,12 @@ public void CoordinatesTest(float x, float y)
PointF p = new PointF(x, y);
Assert.Equal(x, p.X);
Assert.Equal(y, p.Y);

p.X = 10;
Assert.Equal(10, p.X);

p.Y = -10.123f;
Assert.Equal(-10.123, p.Y, 3);
}

[Theory]
Expand Down Expand Up @@ -118,9 +125,29 @@ public void EqualityTest(float x, float y)
}

[Fact]
public void ToStringTest()
public static void EqualityTest_NotPointF()
{
var point = new PointF(0, 0);
Assert.False(point.Equals(null));
Assert.False(point.Equals(0));
Assert.False(point.Equals(new Point(0, 0)));
}

[Fact]
public static void GetHashCodeTest()
{
PointF p = new PointF(0, 0);
var point = new PointF(10, 10);
Assert.Equal(point.GetHashCode(), new PointF(10, 10).GetHashCode());
Assert.NotEqual(point.GetHashCode(), new PointF(20, 10).GetHashCode());
Assert.NotEqual(point.GetHashCode(), new PointF(10, 20).GetHashCode());
}

[Theory]
[InlineData(0, 0)]
[InlineData(5.1, -5.123)]
public void ToStringTest(float x, float y)
{
PointF p = new PointF(x, y);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "{{X={0}, Y={1}}}", p.X, p.Y), p.ToString());
}
}
Expand Down
37 changes: 35 additions & 2 deletions src/libraries/System.Drawing.Primitives/tests/PointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,42 @@ public void EqualityTest(int x, int y)
}

[Fact]
public void ToStringTest()
public static void EqualityTest_NotPoint()
{
Point p = new Point(0, 0);
var point = new Point(0, 0);
Assert.False(point.Equals(null));
Assert.False(point.Equals(0));
Assert.False(point.Equals(new PointF(0, 0)));
}

[Fact]
public static void GetHashCodeTest()
{
var point = new Point(10, 10);
Assert.Equal(point.GetHashCode(), new Point(10, 10).GetHashCode());
Assert.NotEqual(point.GetHashCode(), new Point(20, 10).GetHashCode());
Assert.NotEqual(point.GetHashCode(), new Point(10, 20).GetHashCode());
}

[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(1, -2, 3, -4)]
public void ConversionTest(int x, int y, int width, int height)
{
Rectangle rect = new Rectangle(x, y, width, height);
RectangleF rectF = rect;
Assert.Equal(x, rectF.X);
Assert.Equal(y, rectF.Y);
Assert.Equal(width, rectF.Width);
Assert.Equal(height, rectF.Height);
}

[Theory]
[InlineData(0, 0)]
[InlineData(5, -5)]
public void ToStringTest(int x, int y)
{
Point p = new Point(x, y);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "{{X={0},Y={1}}}", p.X, p.Y), p.ToString());
}
}
Expand Down
69 changes: 65 additions & 4 deletions src/libraries/System.Drawing.Primitives/tests/RectangleFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Globalization;

using Xunit;

namespace System.Drawing.PrimitivesTest
Expand Down Expand Up @@ -78,6 +80,32 @@ public void IsEmptyTest()
Assert.False(new RectangleF(0, 0, 10, 10).IsEmpty);
}

[Theory]
[InlineData(0, 0)]
[InlineData(float.MaxValue, float.MinValue)]
public static void LocationSetTest(float x, float y)
{
var point = new PointF(x, y);
var rect = new RectangleF(10, 10, 10, 10);
rect.Location = point;
Assert.Equal(point, rect.Location);
Assert.Equal(point.X, rect.X);
Assert.Equal(point.Y, rect.Y);
}

[Theory]
[InlineData(0, 0)]
[InlineData(float.MaxValue, float.MinValue)]
public static void SizeSetTest(float x, float y)
{
var size = new SizeF(x, y);
var rect = new RectangleF(10, 10, 10, 10);
rect.Size = size;
Assert.Equal(size, rect.Size);
Assert.Equal(size.Width, rect.Width);
Assert.Equal(size.Height, rect.Height);
}

[Theory]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
[InlineData(float.MaxValue, 0, 0, float.MaxValue)]
Expand All @@ -91,6 +119,27 @@ public void EqualityTest(float x, float y, float width, float height)
Assert.False(rect1 == rect2);
Assert.False(rect1.Equals(rect2));
}

[Fact]
public static void EqualityTest_NotRectangleF()
{
var rectangle = new RectangleF(0, 0, 0, 0);
Assert.False(rectangle.Equals(null));
Assert.False(rectangle.Equals(0));
Assert.False(rectangle.Equals(new Rectangle(0, 0, 0, 0)));
}

[Fact]
public static void GetHashCodeTest()
{
var rect1 = new RectangleF(10, 10, 10, 10);
var rect2 = new RectangleF(10, 10, 10, 10);
Assert.Equal(rect1.GetHashCode(), rect2.GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(20, 10, 10, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(10, 20, 10, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(10, 10, 20, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(10, 10, 10, 20).GetHashCode());
}

[Theory]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
Expand Down Expand Up @@ -140,6 +189,16 @@ public void IntersectTest(float x, float y, float width, float height)
Assert.False(rect1.IntersectsWith(expectedRect));
}

[Fact]
public static void Intersect_IntersectingRects_Test()
{
var rect1 = new RectangleF(0, 0, 5, 5);
var rect2 = new RectangleF(1, 1, 3, 3);
var expected = new RectangleF(1, 1, 3, 3);

Assert.Equal(expected, RectangleF.Intersect(rect1, rect2));
}

[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
Expand Down Expand Up @@ -179,11 +238,13 @@ public void OffsetTest(float x, float y, float width, float height)
Assert.Equal(expectedRect, r1);
}

[Fact]
public void ToStringTest()
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(5, -5, 0.2, -1.3)]
public void ToStringTest(float x, float y, float width, float height)
{
string expected = "{X=0,Y=0,Width=0,Height=0}";
Assert.Equal(expected, RectangleF.Empty.ToString());
var r = new RectangleF(x, y, width, height);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "{{X={0},Y={1},Width={2},Height={3}}}", r.X, r.Y, r.Width, r.Height), r.ToString());
}
}
}
69 changes: 65 additions & 4 deletions src/libraries/System.Drawing.Primitives/tests/RectangleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ public void DimensionsTest(int x, int y, int width, int height)
Assert.Equal(y + height, rect.Bottom);
}

[Theory]
[InlineData(0, 0)]
[InlineData(int.MaxValue, int.MinValue)]
public static void LocationSetTest(int x, int y)
{
var point = new Point(x, y);
var rect = new Rectangle(10, 10, 10, 10);
rect.Location = point;
Assert.Equal(point, rect.Location);
Assert.Equal(point.X, rect.X);
Assert.Equal(point.Y, rect.Y);
}

[Theory]
[InlineData(0, 0)]
[InlineData(int.MaxValue, int.MinValue)]
public static void SizeSetTest(int x, int y)
{
var size = new Size(x, y);
var rect = new Rectangle(10, 10, 10, 10);
rect.Size = size;
Assert.Equal(size, rect.Size);
Assert.Equal(size.Width, rect.Width);
Assert.Equal(size.Height, rect.Height);
}

[Theory]
[InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)]
[InlineData(int.MaxValue, 0, int.MinValue, 0)]
Expand All @@ -113,6 +139,27 @@ public void EqualityTest(int x, int y, int width, int height)
Assert.False(rect1.Equals(rect2));
}

[Fact]
public static void EqualityTest_NotRectangle()
{
var rectangle = new Rectangle(0, 0, 0, 0);
Assert.False(rectangle.Equals(null));
Assert.False(rectangle.Equals(0));
Assert.False(rectangle.Equals(new RectangleF(0, 0, 0, 0)));
}

[Fact]
public static void GetHashCodeTest()
{
var rect1 = new Rectangle(10, 10, 10, 10);
var rect2 = new Rectangle(10, 10, 10, 10);
Assert.Equal(rect1.GetHashCode(), rect2.GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(20, 10, 10, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(10, 20, 10, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(10, 10, 20, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(10, 10, 10, 20).GetHashCode());
}

[Theory]
[InlineData(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue)]
[InlineData(float.MinValue, float.MaxValue, float.MinValue, float.MaxValue)]
Expand Down Expand Up @@ -154,6 +201,8 @@ public void InflateTest(int x, int y, int width, int height)
Rectangle rect = new Rectangle(x, y, width, height);
Rectangle inflatedRect = new Rectangle(x - width, y - height, width + 2 * width, height + 2 * height);

Assert.Equal(inflatedRect, Rectangle.Inflate(rect, width, height));

rect.Inflate(width, height);
Assert.Equal(inflatedRect, rect);

Expand All @@ -177,6 +226,16 @@ public void IntersectTest(int x, int y, int width, int height)
Assert.False(rect.IntersectsWith(expectedRect));
}

[Fact]
public static void Intersect_IntersectingRects_Test()
{
var rect1 = new Rectangle(0, 0, 5, 5);
var rect2 = new Rectangle(1, 1, 3, 3);
var expected = new Rectangle(1, 1, 3, 3);

Assert.Equal(expected, Rectangle.Intersect(rect1, rect2));
}

[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)]
Expand Down Expand Up @@ -216,11 +275,13 @@ public void OffsetTest(int x, int y, int width, int height)
Assert.Equal(expectedRect, r1);
}

[Fact]
public void ToStringTest()
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(5, -5, 0, 1)]
public void ToStringTest(int x, int y, int width, int height)
{
string expected = "{X=0,Y=0,Width=0,Height=0}";
Assert.Equal(expected, Rectangle.Empty.ToString());
var r = new Rectangle(x, y, width, height);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "{{X={0},Y={1},Width={2},Height={3}}}", r.X, r.Y, r.Width, r.Height), r.ToString());
}
}
}
24 changes: 24 additions & 0 deletions src/libraries/System.Drawing.Primitives/tests/SizeFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public void NonDefaultConstructorAndDimensionsTest(float width, float height)

Assert.Equal(width, s1.Width);
Assert.Equal(height, s1.Height);

s1.Width = 10;
Assert.Equal(10, s1.Width);

s1.Height = -10.123f;
Assert.Equal(-10.123, s1.Height, 3);
}

[Fact]
Expand Down Expand Up @@ -94,6 +100,24 @@ public void EqualityTest(float width, float height)
Assert.False(sLeft.Equals(sRight));
}

[Fact]
public static void EqualityTest_NotSizeF()
{
var size = new SizeF(0, 0);
Assert.False(size.Equals(null));
Assert.False(size.Equals(0));
Assert.False(size.Equals(new Size(0, 0)));
}

[Fact]
public static void GetHashCodeTest()
{
var size = new SizeF(10, 10);
Assert.Equal(size.GetHashCode(), new SizeF(10, 10).GetHashCode());
Assert.NotEqual(size.GetHashCode(), new SizeF(20, 10).GetHashCode());
Assert.NotEqual(size.GetHashCode(), new SizeF(10, 20).GetHashCode());
}

[Theory]
[InlineData(float.MaxValue, float.MinValue)]
[InlineData(float.MinValue, float.MinValue)]
Expand Down
24 changes: 24 additions & 0 deletions src/libraries/System.Drawing.Primitives/tests/SizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public void NonDefaultConstructorTest(int width, int height)
Size s2 = new Size(new Point(width, height));

Assert.Equal(s1, s2);

s1.Width = 10;
Assert.Equal(10, s1.Width);

s1.Height = -10;
Assert.Equal(-10, s1.Height);
}

[Fact]
Expand Down Expand Up @@ -137,6 +143,24 @@ public void EqualityTest(int width, int height)
Assert.Equal(p1.GetHashCode(), p3.GetHashCode());
}

[Fact]
public static void EqualityTest_NotSize()
{
var size = new Size(0, 0);
Assert.False(size.Equals(null));
Assert.False(size.Equals(0));
Assert.False(size.Equals(new SizeF(0, 0)));
}

[Fact]
public static void GetHashCodeTest()
{
var size = new Size(10, 10);
Assert.Equal(size.GetHashCode(), new Size(10, 10).GetHashCode());
Assert.NotEqual(size.GetHashCode(), new Size(20, 10).GetHashCode());
Assert.NotEqual(size.GetHashCode(), new Size(10, 20).GetHashCode());
}

[Fact]
public void ToStringTest()
{
Expand Down

0 comments on commit 54a9b90

Please sign in to comment.