1 // Tests for System.Drawing.Rectangle.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 TestRectangle {
47 public void GetReady ()
49 rect_0 = new Rectangle (10, 10, 40, 40);
50 rect_1 = new Rectangle (5, 5, 5, 5);
51 rect_2 = Rectangle.Empty;
52 rect_3 = new Rectangle (new Point (25, 25), new Size (0, 0));
53 rect_4 = new Rectangle (new Point (25, 252), new Size (10, 20));
54 rect_5 = new Rectangle (40, 40, 50, 50);
58 public void Contains ()
60 Assert.IsFalse (rect_0.Contains (5, 5), "a");
61 Assert.IsTrue (rect_0.Contains (12, 12), "b");
62 Assert.IsTrue (rect_0.Contains (10, 10), "c");
63 Assert.IsFalse (rect_0.Contains (10, 50), "d");
64 Assert.IsFalse (rect_0.Contains (50, 10), "e");
65 Assert.IsTrue (rect_0.Contains (new Rectangle (20, 20, 15, 15)), "f");
66 Assert.IsFalse (rect_0.Contains (new Rectangle (5, 5, 20, 20)), "g");
67 Assert.IsTrue (rect_2.Contains (rect_2), "h");
73 Assert.AreEqual (rect_2.X, 0, "X");
74 Assert.AreEqual (rect_2.Y, 0, "Y");
75 Assert.AreEqual (rect_2.Width, 0, "Width");
76 Assert.AreEqual (rect_2.Height, 0, "Height");
80 public void IsEmpty ()
82 Assert.IsFalse (rect_0.IsEmpty, "0");
83 Assert.IsTrue (rect_2.IsEmpty, "2");
84 Assert.IsFalse (rect_3.IsEmpty, "3");
88 public void GetContents ()
90 Assert.AreEqual (rect_4.Right, rect_4.X + rect_4.Width, "Right");
91 Assert.AreEqual (rect_4.Left, rect_4.X, "Left");
92 Assert.AreEqual (rect_4.Bottom, rect_4.Y + rect_4.Height, "Bottom");
93 Assert.AreEqual (rect_4.Top, rect_4.Y, "Top");
97 public void IntersectsWith ()
99 Assert.IsFalse (rect_0.IntersectsWith (rect_1), "0 N 1");
100 Assert.IsFalse (rect_0.IntersectsWith (rect_2), "0 N 2");
101 Assert.IsTrue (rect_0.IntersectsWith (rect_5), "0 N 5");
102 Assert.IsTrue (rect_5.IntersectsWith (rect_0), "5 N 0");
103 Assert.IsFalse (rect_0.IntersectsWith (rect_4), "0 N 4");
107 public void Location ()
109 Assert.AreEqual (new Point (25, 252), rect_4.Location, "Location-1");
110 Point p = new Point (11, 121);
112 Assert.AreEqual (p, rect_4.Location, "Location-2");
113 Assert.AreEqual (rect_4.X, 11, "X");
114 Assert.AreEqual (rect_4.Y, 121, "Y");
117 Assert.AreEqual (new Point (10, 15), rect_4.Location, "Location-3");
123 Assert.AreEqual (rect_4.Width, 10, "X");
124 Assert.AreEqual (rect_4.Height, 20, "Y");
127 Assert.AreEqual (rect_4.Size, new Size (40, 100), "Size");
128 rect_4.Size = new Size (1, 2);
129 Assert.AreEqual (rect_4.Width, 1, "Width");
130 Assert.AreEqual (rect_4.Height, 2, "Height");
134 public void ConvertFromRectangleF ()
136 Assert.AreEqual (rect_0, Rectangle.Ceiling (
137 new RectangleF (9.9F, 9.1F, 39.04F, 39.999F)), "Ceiling");
138 Assert.AreEqual (rect_0, Rectangle.Round (
139 new RectangleF (9.5F, 10.499F, 40.01F, 39.6F)), "Round");
140 Assert.AreEqual (rect_0, Rectangle.Truncate (
141 new RectangleF (10.999F, 10.01F, 40.3F, 40.0F)), "Truncate");
145 public void GetHashCodeTest ()
147 Assert.IsTrue (rect_0.GetHashCode () != rect_1.GetHashCode ());
151 public void Inflate ()
153 rect_0.Inflate (new Size (8, 5));
154 Assert.AreEqual (new Rectangle (2, 5, 56, 50), rect_0, "INF#1");
155 rect_1.Inflate (4, 4);
156 Assert.AreEqual (new Rectangle (1, 1, 13, 13), rect_1, "INF#2");
157 Assert.AreEqual (new Rectangle (30, 20, 70, 90),
158 Rectangle.Inflate (rect_5, 10, 20), "INF#3");
159 Assert.AreEqual (new Rectangle (40, 40, 50, 50), rect_5, "INF#4");
163 public void Intersect ()
165 Assert.AreEqual (new Rectangle (40, 40, 10, 10),
166 Rectangle.Intersect (rect_0, rect_5), "INT#1");
167 Assert.AreEqual (new Rectangle (10, 10, 40, 40), rect_0, "INT#2");
168 rect_0.Intersect (rect_5);
169 Assert.AreEqual (new Rectangle (40, 40, 10, 10), rect_0, "INT#3");
170 Assert.AreEqual (Rectangle.Empty, Rectangle.Intersect (rect_1, rect_5), "INT#4");
172 // Two rectangles touching each other
173 Assert.AreEqual (new Rectangle (3, 0, 0, 7), Rectangle.Intersect (new Rectangle (0, 0, 3, 7), new Rectangle (3, 0, 8, 14)), "INT#5");
177 public void Offset ()
179 rect_0.Offset (5, 5);
180 Assert.AreEqual (new Rectangle (15, 15, 40, 40), rect_0, "OFS#1");
181 rect_1.Offset (new Point (7, 0));
182 Assert.AreEqual (new Rectangle (12, 5, 5, 5), rect_1, "OFS#2");
186 public void ToStringTest ()
188 Assert.AreEqual ("{X=10,Y=10,Width=40,Height=40}", rect_0.ToString (), "0");
189 Assert.AreEqual ("{X=5,Y=5,Width=5,Height=5}", rect_1.ToString (), "1");
190 Assert.AreEqual ("{X=0,Y=0,Width=0,Height=0}", rect_2.ToString (), "2");
191 Assert.AreEqual ("{X=25,Y=25,Width=0,Height=0}", rect_3.ToString (), "3");
195 public void FromTRLB ()
197 Assert.AreEqual (rect_0, Rectangle.FromLTRB (10, 10, 50, 50), "0");
198 Assert.AreEqual (rect_1, Rectangle.FromLTRB (5, 5, 10, 10), "1");
199 Assert.AreEqual (rect_2, Rectangle.FromLTRB (0, 0, 0, 0), "2");
205 Assert.AreEqual (RectangleF.FromLTRB (5, 5, 50, 50), RectangleF.Union (rect_0, rect_1));
209 public void Operator_Equal ()
211 RectangleF r0 = new RectangleF (1, 2, 3, 4);
213 Assert.IsTrue (r0 == r1, "self");
214 Assert.IsFalse (r0 == new RectangleF (0, 2, 3, 4), "X");
215 Assert.IsFalse (r0 == new RectangleF (1, 0, 3, 4), "Y");
216 Assert.IsFalse (r0 == new RectangleF (1, 2, 0, 4), "Width");
217 Assert.IsFalse (r0 == new RectangleF (1, 2, 3, 0), "Height");
221 public void Operator_NotEqual ()
223 RectangleF r0 = new RectangleF (1, 2, 3, 4);
225 Assert.IsFalse (r0 != r1, "self");
226 Assert.IsTrue (r0 != new RectangleF (0, 2, 3, 4), "X");
227 Assert.IsTrue (r0 != new RectangleF (1, 0, 3, 4), "Y");
228 Assert.IsTrue (r0 != new RectangleF (1, 2, 0, 4), "Width");
229 Assert.IsTrue (r0 != new RectangleF (1, 2, 3, 0), "Height");