Skip to content

Commit 2cfbb08

Browse files
committed
Merge pull request dotnet#6099 from hughbe/drawing-primitive-tests
Add some more tests to Drawing.Primitives
2 parents 4221edb + e1a9f14 commit 2cfbb08

File tree

6 files changed

+242
-12
lines changed

6 files changed

+242
-12
lines changed

src/System.Drawing.Primitives/tests/PointFTests.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Globalization;
6+
67
using Xunit;
78

89
namespace System.Drawing.PrimitivesTests
@@ -56,6 +57,12 @@ public void CoordinatesTest(float x, float y)
5657
PointF p = new PointF(x, y);
5758
Assert.Equal(x, p.X);
5859
Assert.Equal(y, p.Y);
60+
61+
p.X = 10;
62+
Assert.Equal(10, p.X);
63+
64+
p.Y = -10.123f;
65+
Assert.Equal(-10.123, p.Y, 3);
5966
}
6067

6168
[Theory]
@@ -118,9 +125,29 @@ public void EqualityTest(float x, float y)
118125
}
119126

120127
[Fact]
121-
public void ToStringTest()
128+
public static void EqualityTest_NotPointF()
129+
{
130+
var point = new PointF(0, 0);
131+
Assert.False(point.Equals(null));
132+
Assert.False(point.Equals(0));
133+
Assert.False(point.Equals(new Point(0, 0)));
134+
}
135+
136+
[Fact]
137+
public static void GetHashCodeTest()
122138
{
123-
PointF p = new PointF(0, 0);
139+
var point = new PointF(10, 10);
140+
Assert.Equal(point.GetHashCode(), new PointF(10, 10).GetHashCode());
141+
Assert.NotEqual(point.GetHashCode(), new PointF(20, 10).GetHashCode());
142+
Assert.NotEqual(point.GetHashCode(), new PointF(10, 20).GetHashCode());
143+
}
144+
145+
[Theory]
146+
[InlineData(0, 0)]
147+
[InlineData(5.1, -5.123)]
148+
public void ToStringTest(float x, float y)
149+
{
150+
PointF p = new PointF(x, y);
124151
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "{{X={0}, Y={1}}}", p.X, p.Y), p.ToString());
125152
}
126153
}

src/System.Drawing.Primitives/tests/PointTests.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,42 @@ public void EqualityTest(int x, int y)
169169
}
170170

171171
[Fact]
172-
public void ToStringTest()
172+
public static void EqualityTest_NotPoint()
173173
{
174-
Point p = new Point(0, 0);
174+
var point = new Point(0, 0);
175+
Assert.False(point.Equals(null));
176+
Assert.False(point.Equals(0));
177+
Assert.False(point.Equals(new PointF(0, 0)));
178+
}
179+
180+
[Fact]
181+
public static void GetHashCodeTest()
182+
{
183+
var point = new Point(10, 10);
184+
Assert.Equal(point.GetHashCode(), new Point(10, 10).GetHashCode());
185+
Assert.NotEqual(point.GetHashCode(), new Point(20, 10).GetHashCode());
186+
Assert.NotEqual(point.GetHashCode(), new Point(10, 20).GetHashCode());
187+
}
188+
189+
[Theory]
190+
[InlineData(0, 0, 0, 0)]
191+
[InlineData(1, -2, 3, -4)]
192+
public void ConversionTest(int x, int y, int width, int height)
193+
{
194+
Rectangle rect = new Rectangle(x, y, width, height);
195+
RectangleF rectF = rect;
196+
Assert.Equal(x, rectF.X);
197+
Assert.Equal(y, rectF.Y);
198+
Assert.Equal(width, rectF.Width);
199+
Assert.Equal(height, rectF.Height);
200+
}
201+
202+
[Theory]
203+
[InlineData(0, 0)]
204+
[InlineData(5, -5)]
205+
public void ToStringTest(int x, int y)
206+
{
207+
Point p = new Point(x, y);
175208
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "{{X={0},Y={1}}}", p.X, p.Y), p.ToString());
176209
}
177210
}

src/System.Drawing.Primitives/tests/RectangleFTests.cs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Collections.Generic;
56
using System.Globalization;
7+
68
using Xunit;
79

810
namespace System.Drawing.PrimitivesTest
@@ -78,6 +80,32 @@ public void IsEmptyTest()
7880
Assert.False(new RectangleF(0, 0, 10, 10).IsEmpty);
7981
}
8082

83+
[Theory]
84+
[InlineData(0, 0)]
85+
[InlineData(float.MaxValue, float.MinValue)]
86+
public static void LocationSetTest(float x, float y)
87+
{
88+
var point = new PointF(x, y);
89+
var rect = new RectangleF(10, 10, 10, 10);
90+
rect.Location = point;
91+
Assert.Equal(point, rect.Location);
92+
Assert.Equal(point.X, rect.X);
93+
Assert.Equal(point.Y, rect.Y);
94+
}
95+
96+
[Theory]
97+
[InlineData(0, 0)]
98+
[InlineData(float.MaxValue, float.MinValue)]
99+
public static void SizeSetTest(float x, float y)
100+
{
101+
var size = new SizeF(x, y);
102+
var rect = new RectangleF(10, 10, 10, 10);
103+
rect.Size = size;
104+
Assert.Equal(size, rect.Size);
105+
Assert.Equal(size.Width, rect.Width);
106+
Assert.Equal(size.Height, rect.Height);
107+
}
108+
81109
[Theory]
82110
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
83111
[InlineData(float.MaxValue, 0, 0, float.MaxValue)]
@@ -91,6 +119,27 @@ public void EqualityTest(float x, float y, float width, float height)
91119
Assert.False(rect1 == rect2);
92120
Assert.False(rect1.Equals(rect2));
93121
}
122+
123+
[Fact]
124+
public static void EqualityTest_NotRectangleF()
125+
{
126+
var rectangle = new RectangleF(0, 0, 0, 0);
127+
Assert.False(rectangle.Equals(null));
128+
Assert.False(rectangle.Equals(0));
129+
Assert.False(rectangle.Equals(new Rectangle(0, 0, 0, 0)));
130+
}
131+
132+
[Fact]
133+
public static void GetHashCodeTest()
134+
{
135+
var rect1 = new RectangleF(10, 10, 10, 10);
136+
var rect2 = new RectangleF(10, 10, 10, 10);
137+
Assert.Equal(rect1.GetHashCode(), rect2.GetHashCode());
138+
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(20, 10, 10, 10).GetHashCode());
139+
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(10, 20, 10, 10).GetHashCode());
140+
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(10, 10, 20, 10).GetHashCode());
141+
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(10, 10, 10, 20).GetHashCode());
142+
}
94143

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

192+
[Fact]
193+
public static void Intersect_IntersectingRects_Test()
194+
{
195+
var rect1 = new RectangleF(0, 0, 5, 5);
196+
var rect2 = new RectangleF(1, 1, 3, 3);
197+
var expected = new RectangleF(1, 1, 3, 3);
198+
199+
Assert.Equal(expected, RectangleF.Intersect(rect1, rect2));
200+
}
201+
143202
[Theory]
144203
[InlineData(0, 0, 0, 0)]
145204
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
@@ -179,11 +238,13 @@ public void OffsetTest(float x, float y, float width, float height)
179238
Assert.Equal(expectedRect, r1);
180239
}
181240

182-
[Fact]
183-
public void ToStringTest()
241+
[Theory]
242+
[InlineData(0, 0, 0, 0)]
243+
[InlineData(5, -5, 0.2, -1.3)]
244+
public void ToStringTest(float x, float y, float width, float height)
184245
{
185-
string expected = "{X=0,Y=0,Width=0,Height=0}";
186-
Assert.Equal(expected, RectangleF.Empty.ToString());
246+
var r = new RectangleF(x, y, width, height);
247+
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "{{X={0},Y={1},Width={2},Height={3}}}", r.X, r.Y, r.Width, r.Height), r.ToString());
187248
}
188249
}
189250
}

src/System.Drawing.Primitives/tests/RectangleTests.cs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,32 @@ public void DimensionsTest(int x, int y, int width, int height)
9898
Assert.Equal(y + height, rect.Bottom);
9999
}
100100

101+
[Theory]
102+
[InlineData(0, 0)]
103+
[InlineData(int.MaxValue, int.MinValue)]
104+
public static void LocationSetTest(int x, int y)
105+
{
106+
var point = new Point(x, y);
107+
var rect = new Rectangle(10, 10, 10, 10);
108+
rect.Location = point;
109+
Assert.Equal(point, rect.Location);
110+
Assert.Equal(point.X, rect.X);
111+
Assert.Equal(point.Y, rect.Y);
112+
}
113+
114+
[Theory]
115+
[InlineData(0, 0)]
116+
[InlineData(int.MaxValue, int.MinValue)]
117+
public static void SizeSetTest(int x, int y)
118+
{
119+
var size = new Size(x, y);
120+
var rect = new Rectangle(10, 10, 10, 10);
121+
rect.Size = size;
122+
Assert.Equal(size, rect.Size);
123+
Assert.Equal(size.Width, rect.Width);
124+
Assert.Equal(size.Height, rect.Height);
125+
}
126+
101127
[Theory]
102128
[InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)]
103129
[InlineData(int.MaxValue, 0, int.MinValue, 0)]
@@ -113,6 +139,27 @@ public void EqualityTest(int x, int y, int width, int height)
113139
Assert.False(rect1.Equals(rect2));
114140
}
115141

142+
[Fact]
143+
public static void EqualityTest_NotRectangle()
144+
{
145+
var rectangle = new Rectangle(0, 0, 0, 0);
146+
Assert.False(rectangle.Equals(null));
147+
Assert.False(rectangle.Equals(0));
148+
Assert.False(rectangle.Equals(new RectangleF(0, 0, 0, 0)));
149+
}
150+
151+
[Fact]
152+
public static void GetHashCodeTest()
153+
{
154+
var rect1 = new Rectangle(10, 10, 10, 10);
155+
var rect2 = new Rectangle(10, 10, 10, 10);
156+
Assert.Equal(rect1.GetHashCode(), rect2.GetHashCode());
157+
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(20, 10, 10, 10).GetHashCode());
158+
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(10, 20, 10, 10).GetHashCode());
159+
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(10, 10, 20, 10).GetHashCode());
160+
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(10, 10, 10, 20).GetHashCode());
161+
}
162+
116163
[Theory]
117164
[InlineData(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue)]
118165
[InlineData(float.MinValue, float.MaxValue, float.MinValue, float.MaxValue)]
@@ -154,6 +201,8 @@ public void InflateTest(int x, int y, int width, int height)
154201
Rectangle rect = new Rectangle(x, y, width, height);
155202
Rectangle inflatedRect = new Rectangle(x - width, y - height, width + 2 * width, height + 2 * height);
156203

204+
Assert.Equal(inflatedRect, Rectangle.Inflate(rect, width, height));
205+
157206
rect.Inflate(width, height);
158207
Assert.Equal(inflatedRect, rect);
159208

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

229+
[Fact]
230+
public static void Intersect_IntersectingRects_Test()
231+
{
232+
var rect1 = new Rectangle(0, 0, 5, 5);
233+
var rect2 = new Rectangle(1, 1, 3, 3);
234+
var expected = new Rectangle(1, 1, 3, 3);
235+
236+
Assert.Equal(expected, Rectangle.Intersect(rect1, rect2));
237+
}
238+
180239
[Theory]
181240
[InlineData(0, 0, 0, 0)]
182241
[InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)]
@@ -216,11 +275,13 @@ public void OffsetTest(int x, int y, int width, int height)
216275
Assert.Equal(expectedRect, r1);
217276
}
218277

219-
[Fact]
220-
public void ToStringTest()
278+
[Theory]
279+
[InlineData(0, 0, 0, 0)]
280+
[InlineData(5, -5, 0, 1)]
281+
public void ToStringTest(int x, int y, int width, int height)
221282
{
222-
string expected = "{X=0,Y=0,Width=0,Height=0}";
223-
Assert.Equal(expected, Rectangle.Empty.ToString());
283+
var r = new Rectangle(x, y, width, height);
284+
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "{{X={0},Y={1},Width={2},Height={3}}}", r.X, r.Y, r.Width, r.Height), r.ToString());
224285
}
225286
}
226287
}

src/System.Drawing.Primitives/tests/SizeFTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public void NonDefaultConstructorAndDimensionsTest(float width, float height)
3232

3333
Assert.Equal(width, s1.Width);
3434
Assert.Equal(height, s1.Height);
35+
36+
s1.Width = 10;
37+
Assert.Equal(10, s1.Width);
38+
39+
s1.Height = -10.123f;
40+
Assert.Equal(-10.123, s1.Height, 3);
3541
}
3642

3743
[Fact]
@@ -94,6 +100,24 @@ public void EqualityTest(float width, float height)
94100
Assert.False(sLeft.Equals(sRight));
95101
}
96102

103+
[Fact]
104+
public static void EqualityTest_NotSizeF()
105+
{
106+
var size = new SizeF(0, 0);
107+
Assert.False(size.Equals(null));
108+
Assert.False(size.Equals(0));
109+
Assert.False(size.Equals(new Size(0, 0)));
110+
}
111+
112+
[Fact]
113+
public static void GetHashCodeTest()
114+
{
115+
var size = new SizeF(10, 10);
116+
Assert.Equal(size.GetHashCode(), new SizeF(10, 10).GetHashCode());
117+
Assert.NotEqual(size.GetHashCode(), new SizeF(20, 10).GetHashCode());
118+
Assert.NotEqual(size.GetHashCode(), new SizeF(10, 20).GetHashCode());
119+
}
120+
97121
[Theory]
98122
[InlineData(float.MaxValue, float.MinValue)]
99123
[InlineData(float.MinValue, float.MinValue)]

src/System.Drawing.Primitives/tests/SizeTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public void NonDefaultConstructorTest(int width, int height)
2626
Size s2 = new Size(new Point(width, height));
2727

2828
Assert.Equal(s1, s2);
29+
30+
s1.Width = 10;
31+
Assert.Equal(10, s1.Width);
32+
33+
s1.Height = -10;
34+
Assert.Equal(-10, s1.Height);
2935
}
3036

3137
[Fact]
@@ -137,6 +143,24 @@ public void EqualityTest(int width, int height)
137143
Assert.Equal(p1.GetHashCode(), p3.GetHashCode());
138144
}
139145

146+
[Fact]
147+
public static void EqualityTest_NotSize()
148+
{
149+
var size = new Size(0, 0);
150+
Assert.False(size.Equals(null));
151+
Assert.False(size.Equals(0));
152+
Assert.False(size.Equals(new SizeF(0, 0)));
153+
}
154+
155+
[Fact]
156+
public static void GetHashCodeTest()
157+
{
158+
var size = new Size(10, 10);
159+
Assert.Equal(size.GetHashCode(), new Size(10, 10).GetHashCode());
160+
Assert.NotEqual(size.GetHashCode(), new Size(20, 10).GetHashCode());
161+
Assert.NotEqual(size.GetHashCode(), new Size(10, 20).GetHashCode());
162+
}
163+
140164
[Fact]
141165
public void ToStringTest()
142166
{

0 commit comments

Comments
 (0)