2007-01-15 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestRectangle.cs
1 // Tests for System.Drawing.Rectangle.cs
2
3 // Copyright (C) 2005, 2006 Novell, Inc (http://www.novell.com)
4 //
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:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
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.
23 //
24 // Author: Jordi Mas i Hernandez <jordi@ximian.com>
25 //
26
27 using NUnit.Framework;
28 using System;
29 using System.Drawing;
30 using System.Security.Permissions;
31
32 namespace MonoTests.System.Drawing
33 {
34
35         [TestFixture]
36         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
37         public class TestRectangle {
38
39                 Rectangle rect_0;
40                 Rectangle rect_1;
41                 Rectangle rect_2;
42                 Rectangle rect_3;
43                 Rectangle rect_4;
44                 Rectangle rect_5;
45
46                 [SetUp]
47                 public void GetReady ()
48                 {
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);
55                 }
56
57                 [Test]
58                 public void Contains ()
59                 {
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");
68                 }
69
70                 [Test]
71                 public void Empty ()
72                 {
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");
77                 }
78
79                 [Test]
80                 public void IsEmpty ()
81                 {
82                         Assert.IsFalse (rect_0.IsEmpty, "0");
83                         Assert.IsTrue (rect_2.IsEmpty, "2");
84                         Assert.IsFalse (rect_3.IsEmpty, "3");
85                 }
86
87                 [Test]
88                 public void GetContents ()
89                 {
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");
94                 }
95
96                 [Test]
97                 public void IntersectsWith  ()
98                 {                                               
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");
104                 }
105
106                 [Test]
107                 public void Location ()
108                 {
109                         Assert.AreEqual (new Point (25, 252), rect_4.Location, "Location-1");
110                         Point p = new Point (11, 121);
111                         rect_4.Location = p;
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");
115                         rect_4.X = 10;
116                         rect_4.Y = 15;
117                         Assert.AreEqual (new Point (10, 15), rect_4.Location, "Location-3");
118                 }
119
120                 [Test]
121                 public void Size ()
122                 {
123                         Assert.AreEqual (rect_4.Width, 10, "X");
124                         Assert.AreEqual (rect_4.Height, 20, "Y");
125                         rect_4.Width = 40;
126                         rect_4.Height = 100;
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");
131                 }
132
133                 [Test]
134                 public void ConvertFromRectangleF ()
135                 {
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");
142                 }
143
144                 [Test]
145                 public void GetHashCodeTest ()
146                 {
147                         Assert.IsTrue (rect_0.GetHashCode () != rect_1.GetHashCode ());
148                 }
149
150                 [Test]
151                 public void Inflate ()
152                 {
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");
160                 }
161
162                 [Test]
163                 public void Intersect ()
164                 {
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");
171
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");
174                 }
175
176                 [Test]
177                 public void Offset ()
178                 {
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");
183                 }
184
185                 [Test]
186                 public void ToStringTest ()
187                 {
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");
192                 }
193
194                 [Test]
195                 public void FromTRLB ()
196                 {
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");
200                 }
201
202                 [Test]
203                 public void Union ()
204                 {
205                         Assert.AreEqual (RectangleF.FromLTRB (5, 5, 50, 50), RectangleF.Union (rect_0, rect_1));
206                 }
207
208                 [Test]
209                 public void Operator_Equal ()
210                 {
211                         RectangleF r0 = new RectangleF (1, 2, 3, 4);
212                         RectangleF r1 = r0;
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");
218                 }
219
220                 [Test]
221                 public void Operator_NotEqual ()
222                 {
223                         RectangleF r0 = new RectangleF (1, 2, 3, 4);
224                         RectangleF r1 = r0;
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");
230                 }
231         }
232 }
233