1 // Tests for System.Drawing.RectangleF.cs
3 // Copyright (C) 2005, 2006 Novell, Inc (http://www.novell.com)
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 // Author: Jordi Mas i Hernandez <jordi@ximian.com>
27 using NUnit.Framework;
30 using System.Security.Permissions;
32 namespace MonoTests.System.Drawing
36 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
37 public class TestRectangleF {
48 public void GetReady ()
50 rect_0 = new RectangleF (new PointF (10, 10), new SizeF (40, 40));
51 rect_1 = new RectangleF (5, 5, 5, 5);
52 rect_2 = RectangleF.Empty;
53 rect_3 = new RectangleF (25, 25, 0, 0);
54 rect_4 = new RectangleF (25, 252, 10, 20);
55 rect_5 = new RectangleF (40, 40, 50, 50);
56 rect_6 = new RectangleF (40, 40, 0, 50);
60 public void Contains ()
62 Assert.IsFalse (rect_0.Contains (5, 5), "a");
63 Assert.IsTrue (rect_0.Contains (12, 12), "b");
64 Assert.IsTrue (rect_0.Contains (10, 10), "c");
65 Assert.IsFalse (rect_0.Contains (10, 50), "d");
66 Assert.IsFalse (rect_0.Contains (10, 50F - float.Epsilon), "e");
67 Assert.IsTrue (rect_0.Contains (10, 49.9F), "f");
68 Assert.IsFalse (rect_0.Contains (50, 10), "g");
72 public void ContainsF ()
75 RectangleF outer = new RectangleF (100, 150, 300, 300);
76 RectangleF inner = new RectangleF (139.3323f, 188.4053f, 140.2086f, 210.3129f);
78 Assert.IsTrue (outer.Contains (inner), "a");
84 Assert.AreEqual (rect_2.X, 0, "X");
85 Assert.AreEqual (rect_2.Y, 0, "Y");
86 Assert.AreEqual (rect_2.Width, 0, "Width");
87 Assert.AreEqual (rect_2.Height, 0, "Height");
91 public void IsEmpty ()
93 Assert.IsFalse (rect_0.IsEmpty, "0");
94 Assert.IsTrue (rect_2.IsEmpty, "2");
95 Assert.IsTrue (rect_3.IsEmpty, "3");
96 Assert.IsTrue (rect_6.IsEmpty, "6");
97 Assert.IsTrue (new RectangleF (0, 0, -1, -1).IsEmpty, "negative w/h");
101 public void GetContents ()
103 Assert.AreEqual (rect_4.Right, rect_4.X + rect_4.Width, "Right");
104 Assert.AreEqual (rect_4.Left, rect_4.X, "Left");
105 Assert.AreEqual (rect_4.Bottom, rect_4.Y + rect_4.Height, "Bottom");
106 Assert.AreEqual (rect_4.Top, rect_4.Y, "Top");
110 public void IntersectsWith ()
112 Assert.IsFalse (rect_0.IntersectsWith (rect_1), "0 N 1");
113 Assert.IsFalse (rect_0.IntersectsWith (rect_2), "0 N 2");
114 Assert.IsTrue (rect_0.IntersectsWith (rect_5), "0 N 5");
115 Assert.IsTrue (rect_5.IntersectsWith (rect_0), "5 N 0");
116 Assert.IsFalse (rect_0.IntersectsWith (rect_4), "0 N 4");
120 public void Location ()
122 Assert.AreEqual (new PointF (25, 252), rect_4.Location, "Location");
123 PointF p = new PointF (11, 121);
125 Assert.AreEqual (p, rect_4.Location, "Localtion-2");
126 Assert.AreEqual (rect_4.X, 11, "X");
127 Assert.AreEqual (rect_4.Y, 121, "Y");
130 Assert.AreEqual (new PointF (10, 15), rect_4.Location, "Localtion-3");
136 Assert.AreEqual (rect_4.Width, 10, "Width-1");
137 Assert.AreEqual (rect_4.Height, 20, "Height-1");
140 Assert.AreEqual (rect_4.Size, new SizeF (40, 100), "Size");
141 rect_4.Size = new SizeF (1, 2);
142 Assert.AreEqual (rect_4.Width, 1, "Width-2");
143 Assert.AreEqual (rect_4.Height, 2, "Height-2");
147 public void GetHashCodeTest ()
149 Assert.IsTrue (rect_0.GetHashCode () != rect_1.GetHashCode ());
153 public void Inflate ()
155 rect_0.Inflate (new SizeF (8, 5));
156 Assert.AreEqual (new RectangleF (2, 5, 56, 50), rect_0, "INF#1");
157 rect_1.Inflate (4, 4);
158 Assert.AreEqual (new RectangleF (1, 1, 13, 13), rect_1, "INF#2");
159 Assert.AreEqual (new RectangleF (30, 20, 70, 90),
160 RectangleF.Inflate (rect_5, 10, 20), "INF#3");
161 Assert.AreEqual (new RectangleF (40, 40, 50, 50), rect_5, "INF#4");
165 public void Intersect ()
167 Assert.AreEqual (new RectangleF (40, 40, 10, 10),
168 RectangleF.Intersect (rect_0, rect_5), "INT#1");
169 Assert.AreEqual (new RectangleF (10, 10, 40, 40), rect_0, "INT#2");
170 rect_0.Intersect (rect_5);
171 Assert.AreEqual (new RectangleF (40, 40, 10, 10), rect_0, "INT#3");
172 Assert.AreEqual (RectangleF.Empty, RectangleF.Intersect (rect_1, rect_5), "INT#4");
176 public void Offset ()
178 rect_0.Offset (5, 5);
179 Assert.AreEqual (new RectangleF (15, 15, 40, 40), rect_0, "OFS#1");
180 rect_1.Offset (new Point (7, 0));
181 Assert.AreEqual (new RectangleF (12, 5, 5, 5), rect_1, "OFS#2");
185 public void ToStringTest ()
187 Assert.AreEqual ("{X=10,Y=10,Width=40,Height=40}", rect_0.ToString (), "0");
188 Assert.AreEqual ("{X=5,Y=5,Width=5,Height=5}", rect_1.ToString (), "1");
189 Assert.AreEqual ("{X=0,Y=0,Width=0,Height=0}", rect_2.ToString (), "2");
190 Assert.AreEqual ("{X=25,Y=25,Width=0,Height=0}", rect_3.ToString (), "3");
194 public void RectangleToRectangleF ()
196 Rectangle r = new Rectangle (1, 2, 3, 4);
198 Assert.AreEqual (new RectangleF (1F, 2F, 3F, 4F), rf);
204 Assert.AreEqual (RectangleF.FromLTRB (5, 5, 50, 50), RectangleF.Union (rect_0, rect_1));
208 public void Operator_Equal ()
210 RectangleF r0 = new RectangleF (1, 2, 3, 4);
212 Assert.IsTrue (r0 == r1, "self");
213 Assert.IsFalse (r0 == new RectangleF (0, 2, 3, 4), "X");
214 Assert.IsFalse (r0 == new RectangleF (1, 0, 3, 4), "Y");
215 Assert.IsFalse (r0 == new RectangleF (1, 2, 0, 4), "Width");
216 Assert.IsFalse (r0 == new RectangleF (1, 2, 3, 0), "Height");
220 public void Operator_NotEqual ()
222 RectangleF r0 = new RectangleF (1, 2, 3, 4);
224 Assert.IsFalse (r0 != r1, "self");
225 Assert.IsTrue (r0 != new RectangleF (0, 2, 3, 4), "X");
226 Assert.IsTrue (r0 != new RectangleF (1, 0, 3, 4), "Y");
227 Assert.IsTrue (r0 != new RectangleF (1, 2, 0, 4), "Width");
228 Assert.IsTrue (r0 != new RectangleF (1, 2, 3, 0), "Height");
232 public void NegativeWidth ()
234 RectangleF r = new RectangleF (0, 0, -1, 10);
235 Assert.AreEqual (0, r.X, "X");
236 Assert.AreEqual (0, r.Y, "Y");
237 Assert.AreEqual (-1, r.Width, "Width");
238 Assert.AreEqual (10, r.Height, "Height");
242 public void NegativeHeight ()
244 RectangleF r = new RectangleF (10, 10, 10, -1);
245 Assert.AreEqual (10, r.X, "X");
246 Assert.AreEqual (10, r.Y, "Y");
247 Assert.AreEqual (10, r.Width, "Width");
248 Assert.AreEqual (-1, r.Height, "Height");
252 public void EdgeIntersection ()
254 // https://bugzilla.novell.com/show_bug.cgi?id=431587
255 RectangleF one = new RectangleF(10, 10, 10, 10);
256 RectangleF two = new RectangleF(20, 10, 10, 10);
259 Assert.IsTrue (one.IsEmpty, "Empty");
260 Assert.AreEqual (20f, one.X, "X");
261 Assert.AreEqual (10f, one.Y, "Y");
262 Assert.AreEqual (0f, one.Width, "Width");
263 Assert.AreEqual (10f, one.Height, "Height");