Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestRegion.cs
1 //
2 // Region class testing unit
3 //
4 // Authors:
5 //   Jordi Mas, jordi@ximian.com
6 //   Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004-2008 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Drawing.Imaging;
32 using System.Drawing;
33 using System.Drawing.Drawing2D;
34 using System.Security.Permissions;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Drawing
38 {
39
40         [TestFixture]
41         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42         public class TestRegion
43         {
44                 /* For debugging */
45                 public static void DumpRegion (Region rgn)
46                 {
47                         Matrix matrix = new Matrix ();
48                         RectangleF [] rects = rgn.GetRegionScans (matrix);
49
50                         for (int i = 0; i < rects.Length; i++)
51                                 Console.WriteLine ( rects[i]);
52                 }
53
54                 private Bitmap bitmap;
55                 private Graphics graphic;
56
57                 [TestFixtureSetUp]
58                 public void FixtureSetUp ()
59                 {
60                         bitmap = new Bitmap (10, 10);
61                         graphic = Graphics.FromImage (bitmap);
62                 }
63
64                 [Test]
65                 public void TestBounds()
66                 {
67                         Bitmap bmp = new Bitmap (600, 800);
68                         Graphics dc = Graphics.FromImage (bmp);
69                         Rectangle rect1, rect2;
70                         Region rgn1, rgn2;
71                         RectangleF bounds;
72
73                         rect1 = new Rectangle (500, 30, 60, 80);
74                         rect2 = new Rectangle (520, 40, 60, 80);
75                         rgn1 = new Region(rect1);
76                         rgn2 = new Region(rect2);
77                         rgn1.Union(rgn2);
78
79                         bounds = rgn1.GetBounds (dc);
80
81                         Assert.AreEqual (500, bounds.X);
82                         Assert.AreEqual (30, bounds.Y);
83                         Assert.AreEqual (80, bounds.Width);
84                         Assert.AreEqual (90, bounds.Height);
85                 }
86
87                 [Test]
88                 public void TestCloneAndEquals()
89                 {
90                         Bitmap bmp = new Bitmap (600, 800);
91                         Graphics dc = Graphics.FromImage (bmp);
92                         Rectangle rect1, rect2;
93                         Region rgn1, rgn2;
94                         RectangleF [] rects;
95                         RectangleF [] rects2;
96                         Matrix matrix = new Matrix ();
97
98                         rect1 = new Rectangle (500, 30, 60, 80);
99                         rect2 = new Rectangle (520, 40, 60, 80);
100                         rgn1 = new Region (rect1);
101                         rgn1.Union (rect2);
102                         rgn2 = rgn1.Clone ();
103
104                         rects = rgn1.GetRegionScans (matrix);
105                         rects2 = rgn2.GetRegionScans (matrix);
106
107                         Assert.AreEqual (rects.Length, rects2.Length);
108
109                         for (int i = 0; i < rects.Length; i++) {
110
111                                 Assert.AreEqual (rects[i].X, rects[i].X);
112                                 Assert.AreEqual (rects[i].Y, rects[i].Y);
113                                 Assert.AreEqual (rects[i].Width, rects[i].Width);
114                                 Assert.AreEqual (rects[i].Height, rects[i].Height);
115                         }
116
117                         Assert.AreEqual (true, rgn1.Equals (rgn2, dc));
118                 }
119
120                  /*Tests infinite, empty, etc*/
121                 [Test]
122                 public void TestInfiniteAndEmpty()
123                 {
124                         Bitmap bmp = new Bitmap (600, 800);
125                         Graphics dc = Graphics.FromImage (bmp);
126                         Rectangle rect1, rect2;
127                         Region rgn1;
128                         RectangleF [] rects;
129                         Matrix matrix = new Matrix ();
130
131                         rect1 = new Rectangle (500, 30, 60, 80);
132                         rect2 = new Rectangle (520, 40, 60, 80);
133                         rgn1 = new Region (rect1);
134                         rgn1.Union (rect2);
135
136                         Assert.AreEqual (false, rgn1.IsEmpty (dc));
137                         Assert.AreEqual (false, rgn1.IsInfinite (dc));
138
139                         rgn1.MakeEmpty();
140                         Assert.AreEqual (true, rgn1.IsEmpty (dc));
141
142                         rgn1 = new Region (rect1);
143                         rgn1.Union (rect2);
144                         rgn1.MakeInfinite ();
145                         rects = rgn1.GetRegionScans (matrix);
146
147                         Assert.AreEqual (1, rects.Length);
148                         Assert.AreEqual (-4194304, rects[0].X);
149                         Assert.AreEqual (-4194304, rects[0].Y);
150                         Assert.AreEqual (8388608, rects[0].Width);
151                         Assert.AreEqual (8388608, rects[0].Height);
152                         Assert.AreEqual (true, rgn1.IsInfinite (dc));
153                 }
154
155
156                 [Test]
157                 public void TestUnionGroup1 ()
158                 {
159                         Bitmap bmp = new Bitmap (600, 800);
160                         Graphics dc = Graphics.FromImage (bmp);
161                         Matrix matrix = new Matrix ();
162                         Rectangle rect1, rect2, rect3, rect4;
163                         Region rgn1, rgn2, rgn3, rgn4;
164                         RectangleF [] rects;
165
166                         rect1 = new Rectangle (500, 30, 60, 80);
167                         rect2 = new Rectangle (520, 40, 60, 80);
168                         rgn1 = new Region(rect1);
169                         rgn2 = new Region(rect2);
170                         rgn1.Union(rgn2);
171                         rects = rgn1.GetRegionScans (matrix);
172
173                         Assert.AreEqual (3, rects.Length);
174                         Assert.AreEqual (500, rects[0].X);
175                         Assert.AreEqual (30, rects[0].Y);
176                         Assert.AreEqual (60, rects[0].Width);
177                         Assert.AreEqual (10, rects[0].Height);
178
179                         Assert.AreEqual (500, rects[1].X);
180                         Assert.AreEqual (40, rects[1].Y);
181                         Assert.AreEqual (80, rects[1].Width);
182                         Assert.AreEqual (70, rects[1].Height);
183
184                         Assert.AreEqual (520, rects[2].X);
185                         Assert.AreEqual (110, rects[2].Y);
186                         Assert.AreEqual (60, rects[2].Width);
187                         Assert.AreEqual (10, rects[2].Height);
188
189                         rect1 = new Rectangle (20, 180, 40, 50);
190                         rect2 = new Rectangle (50, 190, 40, 50);
191                         rect3 = new Rectangle (70, 210, 30, 50);
192                         rgn1 = new Region (rect1);
193                         rgn2 = new Region (rect2);
194                         rgn3 = new Region (rect3);
195
196                         rgn1.Union (rgn2);
197                         rgn1.Union (rgn3);
198                         rects = rgn1.GetRegionScans (matrix);
199                         Assert.AreEqual (5, rects.Length);
200
201                         Assert.AreEqual (20, rects[0].X);
202                         Assert.AreEqual (180, rects[0].Y);
203                         Assert.AreEqual (40, rects[0].Width);
204                         Assert.AreEqual (10, rects[0].Height);
205
206                         Assert.AreEqual (20, rects[1].X);
207                         Assert.AreEqual (190, rects[1].Y);
208                         Assert.AreEqual (70, rects[1].Width);
209                         Assert.AreEqual (20, rects[1].Height);
210
211                         Assert.AreEqual (20, rects[2].X);
212                         Assert.AreEqual (210, rects[2].Y);
213                         Assert.AreEqual (80, rects[2].Width);
214                         Assert.AreEqual (20, rects[2].Height);
215
216                         Assert.AreEqual (50, rects[3].X);
217                         Assert.AreEqual (230, rects[3].Y);
218                         Assert.AreEqual (50, rects[3].Width);
219                         Assert.AreEqual (10, rects[3].Height);
220
221                         Assert.AreEqual (70, rects[4].X);
222                         Assert.AreEqual (240, rects[4].Y);
223                         Assert.AreEqual (30, rects[4].Width);
224                         Assert.AreEqual (20, rects[4].Height);
225
226                         rect1 = new Rectangle (20, 330, 40, 50);
227                         rect2 = new Rectangle (50, 340, 40, 50);
228                         rect3 = new Rectangle (70, 360, 30, 50);
229                         rect4 = new Rectangle (80, 400, 30, 10);
230                         rgn1 = new Region (rect1);
231                         rgn2 = new Region (rect2);
232                         rgn3 = new Region (rect3);
233                         rgn4 = new Region (rect4);
234
235                         rgn1.Union (rgn2);
236                         rgn1.Union (rgn3);
237                         rgn1.Union (rgn4);
238
239                         rects = rgn1.GetRegionScans (matrix);
240
241                         Assert.AreEqual (6, rects.Length);
242
243                         Assert.AreEqual (20, rects[0].X);
244                         Assert.AreEqual (330, rects[0].Y);
245                         Assert.AreEqual (40, rects[0].Width);
246                         Assert.AreEqual (10, rects[0].Height);
247
248                         Assert.AreEqual (20, rects[1].X);
249                         Assert.AreEqual (340, rects[1].Y);
250                         Assert.AreEqual (70, rects[1].Width);
251                         Assert.AreEqual (20, rects[1].Height);
252
253                         Assert.AreEqual (20, rects[2].X);
254                         Assert.AreEqual (360, rects[2].Y);
255                         Assert.AreEqual (80, rects[2].Width);
256                         Assert.AreEqual (20, rects[2].Height);
257
258                         Assert.AreEqual (50, rects[3].X);
259                         Assert.AreEqual (380, rects[3].Y);
260                         Assert.AreEqual (50, rects[3].Width);
261                         Assert.AreEqual (10, rects[3].Height);
262
263                         Assert.AreEqual (70, rects[4].X);
264                         Assert.AreEqual (390, rects[4].Y);
265                         Assert.AreEqual (30, rects[4].Width);
266                         Assert.AreEqual (10, rects[4].Height);
267
268                         Assert.AreEqual (70, rects[5].X);
269                         Assert.AreEqual (400, rects[5].Y);
270                         Assert.AreEqual (40, rects[5].Width);
271                         Assert.AreEqual (10, rects[5].Height);
272
273                         rect1 = new Rectangle (10, 20, 50, 50);
274                         rect2 = new Rectangle (100, 100, 60, 60);
275                         rect3 = new Rectangle (200, 200, 80, 80);
276
277                         rgn1 = new Region (rect1);
278                         rgn1.Union (rect2);
279                         rgn1.Union (rect3);
280
281                         rects = rgn1.GetRegionScans (matrix);
282
283                         Assert.AreEqual (3, rects.Length);
284
285                         Assert.AreEqual (10, rects[0].X);
286                         Assert.AreEqual (20, rects[0].Y);
287                         Assert.AreEqual (50, rects[0].Width);
288                         Assert.AreEqual (50, rects[0].Height);
289
290                         Assert.AreEqual (100, rects[1].X);
291                         Assert.AreEqual (100, rects[1].Y);
292                         Assert.AreEqual (60, rects[1].Width);
293                         Assert.AreEqual (60, rects[1].Height);
294
295                         Assert.AreEqual (200, rects[2].X);
296                         Assert.AreEqual (200, rects[2].Y);
297                         Assert.AreEqual (80, rects[2].Width);
298                         Assert.AreEqual (80, rects[2].Height);
299                 }
300
301                 void AssertEqualRectangles (RectangleF rect1, RectangleF rect2, string text)
302                 {
303                         Assert.AreEqual (rect1.X, rect2.X, text + ".X");
304                         Assert.AreEqual (rect1.Y, rect2.Y, text + ".Y");
305                         Assert.AreEqual (rect1.Width, rect2.Width, text + ".Width");
306                         Assert.AreEqual (rect1.Height, rect2.Height, text + ".Height");
307                 }
308
309                 [Test]
310                 public void TestUnionGroup2 ()
311                 {
312                         RectangleF[] rects;
313                         Region r1  = new Region ();
314                         Rectangle rect2 = Rectangle.Empty;
315                         Rectangle rect1 = Rectangle.Empty;
316                         Rectangle rect3 = Rectangle.Empty;
317                         Rectangle rect4 = Rectangle.Empty;
318
319                         { // TEST1: Not intersecting rects. Union just adds them
320
321                                 rect1 = new Rectangle (20, 20, 20, 20);
322                                 rect2 = new Rectangle (20, 80, 20, 10);
323                                 rect3 = new Rectangle (60, 60, 30, 10);
324
325                                 r1 = new Region (rect1);
326                                 r1.Union (rect2);
327                                 r1.Union (rect3);
328
329                                 rects = r1.GetRegionScans (new Matrix ());
330                                 Assert.AreEqual (3, rects.Length, "TUG1Test1");
331                                 AssertEqualRectangles (new RectangleF (20, 20, 20, 20), rects[0], "TUG1Test2");
332                                 AssertEqualRectangles (new RectangleF (60, 60, 30, 10), rects[1], "TUG1Test3");
333                                 AssertEqualRectangles (new RectangleF (20, 80, 20, 10), rects[2], "TUG1Test4");
334                         }
335
336                         { // TEST2: Intersecting from the right
337                                  /*
338                                  *  -----------
339                                  *  |         |
340                                  *  |     |-------- |
341                                  *  |     |         |
342                                  *  |     |-------- |
343                                  *  |         |
344                                  *  ----------|
345                                  *
346                                  */
347
348                                 rect1 = new Rectangle (10, 10, 100, 100);
349                                 rect2 = new Rectangle (40, 60, 100, 20);
350                                 r1 = new Region (rect1);
351                                 r1.Union (rect2);
352
353                                 rects = r1.GetRegionScans (new Matrix ());
354                                 Assert.AreEqual  (3, rects.Length, "TUG2Test1");
355                                 AssertEqualRectangles (new RectangleF (10, 10, 100, 50), rects[0], "TUG2Test2");
356                                 AssertEqualRectangles (new RectangleF (10, 60, 130, 20), rects[1], "TUG2Test3");
357                                 AssertEqualRectangles (new RectangleF (10, 80, 100, 30), rects[2], "TUG2Test4");
358                         }
359
360                         { // TEST3: Intersecting from the right
361                                  /*
362                                  *      -----------
363                                  *      |         |
364                                  * |-------- |    |
365                                  * |         |    |
366                                  * |-------- |    |
367                                  *      |         |
368                                  *      ----------|
369                                  *
370                                  */
371
372                                 rect1 = new Rectangle (70, 10, 100, 100);
373                                 rect2 = new Rectangle (40, 60, 100, 20);
374
375                                 r1 = new Region (rect1);
376                                 r1.Union (rect2);
377
378                                 rects = r1.GetRegionScans (new Matrix ());
379                                 Assert.AreEqual  (3, rects.Length, "TUG3Test1");
380                                 AssertEqualRectangles (new RectangleF (70, 10, 100, 50), rects[0], "TUG3Test2");
381                                 AssertEqualRectangles (new RectangleF (40, 60, 130, 20), rects[1], "TUG3Test3");
382                                 AssertEqualRectangles (new RectangleF (70, 80, 100, 30), rects[2], "TUG3Test4");
383                         }
384
385                         { // TEST4: Intersecting from the top
386                                  /*
387                                  *         -----
388                                  *         |   |
389                                  *      -----------
390                                  *      |  |   |  |
391                                  *      |  -----  |
392                                  *      |         |
393                                  *      |         |
394                                  *      ----------|
395                                  *
396                                  */
397
398                                 rect1 = new Rectangle (40, 100, 100, 100);
399                                 rect2 = new Rectangle (70, 80, 50, 40);
400                                 r1 = new Region (rect1);
401                                 r1.Union (rect2);
402
403                                 rects = r1.GetRegionScans (new Matrix ());
404                                 Assert.AreEqual  (2, rects.Length, "TUG4Test1");
405                                 AssertEqualRectangles (new RectangleF (70, 80, 50, 20), rects[0], "TUG4Test2");
406                                 AssertEqualRectangles (new RectangleF (40, 100, 100, 100), rects[1], "TUG4Test3");
407                         }
408
409                         { // TEST5: Intersecting from the bottom
410                                  /*
411
412                                  *      -----------
413                                  *      |         |
414                                  *      |         |
415                                  *      |         |
416                                  *      |  |   |  |
417                                  *      |--|   |--|
418                                  *         |   |
419                                  *         -----
420                                  */
421
422                                 rect1 = new Rectangle (40, 10, 100, 100);
423                                 rect2 = new Rectangle (70, 80, 50, 40);
424
425                                 r1 = new Region (rect1);
426                                 r1.Union (rect2);
427
428                                 rects = r1.GetRegionScans (new Matrix ());
429                                 Assert.AreEqual  (2, rects.Length, "TUG5Test1");
430                                 AssertEqualRectangles (new RectangleF (40, 10, 100, 100), rects[0], "TUG5Test2");
431                                 AssertEqualRectangles (new RectangleF (70, 110, 50, 10), rects[1], "TUG5Test3");
432                         }
433
434                         { // TEST6: Multiple regions, two separted by zero pixels
435
436                                 rect1 = new Rectangle (30, 30, 80, 80);
437                                 rect2 = new Rectangle (45, 45, 200, 200);
438                                 rect3 = new Rectangle (160, 260, 10, 10);
439                                 rect4 = new Rectangle (170, 260, 10, 10);
440
441                                 r1 = new Region (rect1);
442                                 r1.Union (rect2);
443                                 r1.Union (rect3);
444                                 r1.Union (rect4);
445
446                                 rects = r1.GetRegionScans (new Matrix ());
447                                 Assert.AreEqual  (4, rects.Length, "TUG6Test1");
448                                 AssertEqualRectangles (new RectangleF (30, 30, 80, 15), rects[0], "TUG6Test2");
449                                 AssertEqualRectangles (new RectangleF (30, 45, 215, 65), rects[1], "TUG6Test3");
450                                 AssertEqualRectangles (new RectangleF (45, 110, 200, 135), rects[2], "TUG6Test4");
451                                 AssertEqualRectangles (new RectangleF (160, 260, 20, 10), rects[3], "TUG6Test5");
452                         }
453                 }
454
455
456                 [Test]
457                 public void TestComplementGroup1 ()
458                 {
459                         RectangleF[] rects;
460                         Region r1  = new Region ();
461                         Region r2 = new Region ();
462                         Rectangle rect1 = Rectangle.Empty;
463                         Rectangle rect2 = Rectangle.Empty;
464                         Rectangle rect3 = Rectangle.Empty;
465                         Rectangle rect4 = Rectangle.Empty;
466                         Rectangle rect5 = Rectangle.Empty;
467                         Rectangle rect6 = Rectangle.Empty;
468                         Rectangle rect7 = Rectangle.Empty;
469
470
471                         { // TEST1
472
473                                 rect1 = new Rectangle (20, 20, 20, 20);
474                                 rect2 = new Rectangle (20, 80, 20, 10);
475                                 rect3 = new Rectangle (60, 60, 30, 10);
476
477                                 r1 = new Region (rect1);
478                                 r2 = new Region (rect2);
479                                 r2.Union (rect3);
480                                 r1.Complement (r2);
481
482                                 rects = r1.GetRegionScans (new Matrix ());
483                                 Assert.AreEqual  (2, rects.Length, "TCG1Test1");
484                                 AssertEqualRectangles (new RectangleF (60, 60, 30, 10), rects[0], "TCG1Test2");
485                                 AssertEqualRectangles (new RectangleF (20, 80, 20, 10), rects[1], "TCG1Test3");
486                         }
487
488
489                         { // TEST2
490
491                                 rect1 = new Rectangle (10, 10, 100, 100);
492                                 rect2 = new Rectangle (40, 60, 100, 20);
493
494                                 r1 = new Region (rect1);
495                                 r1.Complement (rect2);
496
497                                 rects = r1.GetRegionScans (new Matrix ());
498                                 Assert.AreEqual  (1, rects.Length, "TCG2Test1");
499                                 AssertEqualRectangles (new RectangleF (110, 60, 30, 20), rects[0], "TCG2Test2");
500                         }
501
502                         { // TEST3
503
504                                 rect1 = new Rectangle (70, 10, 100, 100);
505                                 rect2 = new Rectangle (40, 60, 100, 20);
506
507                                 r1 = new Region (rect1);
508                                 r1.Complement (rect2);
509
510                                 rects = r1.GetRegionScans (new Matrix ());
511                                 Assert.AreEqual  (1, rects.Length, "TCG3Test1");
512                                 AssertEqualRectangles (new RectangleF (40, 60, 30, 20), rects[0], "TCG3Test2");
513                         }
514
515                         { // TEST4
516
517                                 rect1 = new Rectangle (40, 100, 100, 100);
518                                 rect2 = new Rectangle (70, 80, 50, 40);
519
520                                 r1 = new Region (rect1);
521                                 r1.Complement (rect2);
522
523                                 rects = r1.GetRegionScans (new Matrix ());
524                                 Assert.AreEqual  (1, rects.Length, "TCG4Test1");
525                                 AssertEqualRectangles (new RectangleF (70, 80, 50, 20), rects[0], "TCG4Test2");
526                         }
527
528                         { // TEST5
529
530                                 rect1 = new Rectangle (40, 10, 100, 100);
531                                 rect2 = new Rectangle (70, 80, 50, 40);
532
533                                 r1 = new Region (rect1);
534                                 r1.Complement (rect2);
535
536                                 rects = r1.GetRegionScans (new Matrix ());
537                                 Assert.AreEqual  (1, rects.Length, "TCG5Test1");
538                                 AssertEqualRectangles (new RectangleF (70, 110, 50, 10), rects[0], "TCG5Test2");
539                         }
540
541                         { // TEST6: Multiple regions
542
543                                 rect1 = new Rectangle (30, 30, 80, 80);
544                                 rect2 = new Rectangle (45, 45, 200, 200);
545                                 rect3 = new Rectangle (160, 260, 10, 10);
546                                 rect4 = new Rectangle (170, 260, 10, 10);
547
548                                 r1 = new Region (rect1);
549                                 r1.Complement (rect2);
550                                 r1.Complement (rect3);
551                                 r1.Complement (rect4);
552
553                                 rects = r1.GetRegionScans (new Matrix ());
554                                 Assert.AreEqual  (1, rects.Length, "TCG6Test1");
555                                 AssertEqualRectangles (new RectangleF (170, 260, 10, 10), rects[0], "TCG6Test2");
556                         }
557
558                 }
559
560                 [Test]
561                 public void TestComplementGroup2 ()
562                 {
563
564                         Bitmap bmp = new Bitmap (600, 800);
565                         Graphics dc = Graphics.FromImage (bmp);
566                         Matrix matrix = new Matrix ();
567                         Rectangle rect1, rect2;
568                         Region rgn1, rgn2;
569                         RectangleF [] rects;
570
571                         rect1 = new Rectangle (20, 30, 60, 80);
572                         rect2 = new Rectangle (50, 40, 60, 80);
573                         rgn1 = new Region (rect1);
574                         rgn2 = new Region (rect2);
575                         dc.DrawRectangle (Pens.Green, rect1);
576                         dc.DrawRectangle (Pens.Red, rect2);
577                         rgn1.Complement (rgn2);
578                         dc.FillRegion (Brushes.Blue, rgn1);
579                         dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
580
581                         rects = rgn1.GetRegionScans (matrix);
582
583                         Assert.AreEqual (2, rects.Length);
584
585                         Assert.AreEqual (80, rects[0].X);
586                         Assert.AreEqual (40, rects[0].Y);
587                         Assert.AreEqual (30, rects[0].Width);
588                         Assert.AreEqual (70, rects[0].Height);
589
590                         Assert.AreEqual (50, rects[1].X);
591                         Assert.AreEqual (110, rects[1].Y);
592                         Assert.AreEqual (60, rects[1].Width);
593                         Assert.AreEqual (10, rects[1].Height);
594
595                 }
596
597
598                 [Test]
599                 public void TestExcludeGroup1 ()
600                 {
601                         RectangleF[] rects;
602                         Region r1  = new Region ();
603                         Region r2 = new Region ();
604                         Rectangle rect1 = Rectangle.Empty;
605                         Rectangle rect2 = Rectangle.Empty;
606                         Rectangle rect3 = Rectangle.Empty;
607                         Rectangle rect4 = Rectangle.Empty;
608                         Rectangle rect5 = Rectangle.Empty;
609                         Rectangle rect6 = Rectangle.Empty;
610                         Rectangle rect7 = Rectangle.Empty;
611
612
613                         { // TEST1: Not intersecting rects. Exclude just adds them
614
615                                 rect1 = new Rectangle (20, 20, 20, 20);
616                                 rect2 = new Rectangle (20, 80, 20, 10);
617                                 rect3 = new Rectangle (60, 60, 30, 10);
618
619                                 r1 = new Region (rect1);
620                                 r2 = new Region (rect2);
621                                 r2.Union (rect3);
622                                 r1.Exclude (r2);
623
624                                 rects = r1.GetRegionScans (new Matrix ());
625                                 Assert.AreEqual  (1, rects.Length, "TEG1Test1");
626                                 AssertEqualRectangles (new RectangleF (20, 20, 20, 20), rects[0], "TEG1Test2");
627                         }
628
629                         { // TEST2: Excluding from the right
630                                  /*
631                                  *  -----------
632                                  *  |         |
633                                  *  |     |-------- |
634                                  *  |     |         |
635                                  *  |     |-------- |
636                                  *  |         |
637                                  *  ----------|
638                                  *
639                                  */
640
641                                 rect1 = new Rectangle (10, 10, 100, 100);
642                                 rect2 = new Rectangle (40, 60, 100, 20);
643                                 r1 = new Region (rect1);
644                                 r1.Exclude (rect2);
645
646                                 rects = r1.GetRegionScans (new Matrix ());
647                                 Assert.AreEqual  (3, rects.Length, "TEG2Test1");
648                                 AssertEqualRectangles (new RectangleF (10, 10, 100, 50), rects[0], "TEG2Test2");
649                                 AssertEqualRectangles (new RectangleF (10, 60, 30, 20), rects[1], "TEG2Test3");
650                                 AssertEqualRectangles (new RectangleF (10, 80, 100, 30), rects[2], "TEG2Test4");
651                         }
652
653
654                         { // TEST3: Intersecting from the right
655                                  /*
656                                  *      -----------
657                                  *      |         |
658                                  * |-------- |    |
659                                  * |         |    |
660                                  * |-------- |    |
661                                  *      |         |
662                                  *      ----------|
663                                  *
664                                  */
665
666                                 rect1 = new Rectangle (70, 10, 100, 100);
667                                 rect2 = new Rectangle (40, 60, 100, 20);
668
669                                 r1 = new Region (rect1);
670                                 r1.Exclude (rect2);
671
672                                 rects = r1.GetRegionScans (new Matrix ());
673                                 Assert.AreEqual  (3, rects.Length, "TEG3Test1");
674                                 AssertEqualRectangles (new RectangleF (70, 10, 100, 50), rects[0], "TEG3Test2");
675                                 AssertEqualRectangles (new RectangleF (140, 60, 30, 20), rects[1], "TEG3Test3");
676                                 AssertEqualRectangles (new RectangleF (70, 80, 100, 30), rects[2], "TEG3Test4");
677                         }
678
679
680                         { // TEST4: Intersecting from the top
681                                  /*
682                                  *         -----
683                                  *         |   |
684                                  *      -----------
685                                  *      |  |   |  |
686                                  *      |  -----  |
687                                  *      |         |
688                                  *      |         |
689                                  *      ----------|
690                                  *
691                                  */
692
693                                 rect1 = new Rectangle (40, 100, 100, 100);
694                                 rect2 = new Rectangle (70, 80, 50, 40);
695
696                                 r1 = new Region (rect1);
697                                 r1.Exclude (rect2);
698
699                                 rects = r1.GetRegionScans (new Matrix ());
700                                 Assert.AreEqual  (3, rects.Length, "TEG4Test1");
701                                 AssertEqualRectangles (new RectangleF (40, 100, 30, 20), rects[0], "TEG4Test2");
702                                 AssertEqualRectangles (new RectangleF (120, 100, 20, 20), rects[1], "TEG4Test3");
703                                 AssertEqualRectangles (new RectangleF (40, 120, 100, 80), rects[2], "TEG4Test4");
704                         }
705
706
707                         { // TEST5: Intersecting from the bottom
708                                  /*
709                                  *      -----------
710                                  *      |         |
711                                  *      |         |
712                                  *      |         |
713                                  *      |  |   |  |
714                                  *      |--|   |--|
715                                  *         |   |
716                                  *         -----
717                                  *
718                                  */
719
720                                 rect1 = new Rectangle (40, 10, 100, 100);
721                                 rect2 = new Rectangle (70, 80, 50, 40);
722
723                                 r1 = new Region (rect1);
724                                 r1.Exclude (rect2);
725
726                                 rects = r1.GetRegionScans (new Matrix ());
727                                 Assert.AreEqual (3, rects.Length, "TEG5Test1");
728                                 AssertEqualRectangles (new RectangleF (40, 10, 100, 70), rects[0], "TEG5Test2");
729                                 AssertEqualRectangles (new RectangleF (40, 80, 30, 30), rects[1], "TEG5Test3");
730                                 AssertEqualRectangles (new RectangleF (120, 80, 20, 30), rects[2], "TEG5Test4");
731                         }
732
733
734                         { // TEST6: Multiple regions
735
736                                 rect1 = new Rectangle (30, 30, 80, 80);
737                                 rect2 = new Rectangle (45, 45, 200, 200);
738                                 rect3 = new Rectangle (160, 260, 10, 10);
739                                 rect4 = new Rectangle (170, 260, 10, 10);
740
741                                 r1 = new Region (rect1);
742                                 r1.Exclude (rect2);
743                                 r1.Exclude (rect3);
744                                 r1.Exclude (rect4);
745
746                                 rects = r1.GetRegionScans (new Matrix ());
747                                 Assert.AreEqual (2, rects.Length, "TEG6Test1");
748                                 AssertEqualRectangles (new RectangleF (30, 30, 80, 15), rects[0], "TEG6Test2");
749                                 AssertEqualRectangles (new RectangleF (30, 45, 15, 65), rects[1], "TEG6Test3");
750                         }
751
752
753                         { // TEST7: Intersecting from the top with a larger rect
754                                  /*
755                                  *    -----------------
756                                  *    |               |
757                                  *    | -----------   |
758                                  *      |  |   |  |
759                                  *      |  -----  |
760                                  *      |         |
761                                  *      |         |
762                                  *      ----------|
763                                  *
764                                  */
765
766                                 rect1 = new Rectangle (50, 100, 100, 100);
767                                 rect2 = new Rectangle (30, 70, 150, 40);
768
769                                 r1 = new Region (rect1);
770                                 r1.Exclude (rect2);
771
772                                 rects = r1.GetRegionScans (new Matrix ());
773                                 Assert.AreEqual (1, rects.Length, "TEG7Test1");
774                                 AssertEqualRectangles (new RectangleF (50, 110, 100, 90), rects[0], "TEG7Test2");
775                         }
776
777                         { // TEST8: Intersecting from the right with a larger rect
778                                  /*
779                                  *
780                                  * |--------|
781                                  * |        |
782                                  * |    -----------
783                                  * |    |         |
784                                  * |    |         |
785                                  * |    |         |
786                                  * |    |         |
787                                  * |    |         |
788                                  * |    ----------|
789                                  * |-------|
790                                  */
791
792                                 rect1 = new Rectangle (70, 60, 100, 70);
793                                 rect2 = new Rectangle (40, 10, 100, 150);
794
795                                 r1 = new Region (rect1);
796                                 r1.Exclude (rect2);
797
798                                 rects = r1.GetRegionScans (new Matrix ());
799                                 Assert.AreEqual (1, rects.Length, "TEG8Test1");
800                                 AssertEqualRectangles (new RectangleF (140, 60, 30, 70), rects[0], "TEG8Test2");
801
802                         }
803
804                         { // TEST9: Intersecting from the left with a larger rect
805                                  /*
806                                  *
807                                  *              |--------|
808                                  *              |        |
809                                  *      -----------      |
810                                  *      |         |      |
811                                  *      |         |      |
812                                  *      |         |      |
813                                  *      |         |      |
814                                  *      |         |      |
815                                  *      ----------|      |
816                                  *              |--------|
817                                  *
818                                  */
819
820
821                                 rect1 = new Rectangle (70, 60, 100, 70);
822                                 rect2 = new Rectangle (100, 10, 100, 150);
823
824                                 r1 = new Region (rect1);
825                                 r1.Exclude (rect2);
826
827                                 rects = r1.GetRegionScans (new Matrix ());
828                                 Assert.AreEqual (1, rects.Length, "TEG9Test1");
829                                 AssertEqualRectangles (new RectangleF (70, 60, 30, 70), rects[0], "TEG9Test2");
830                         }
831
832
833                         { // TEST10: Intersecting from the bottom with a larger rect
834                                  /*
835                                  * *
836                                  *              |--------|
837                                  *              |        |
838                                  *              |        |
839                                  *              |        |
840                                  *        --------------------
841                                  *        |                  |
842                                  *        |                  |
843                                  *        |------------------|
844                                  */
845
846
847                                 rect1 = new Rectangle (20, 20, 100, 100);
848                                 rect2 = new Rectangle (10, 80, 140, 150);
849
850                                 r1 = new Region (rect1);
851                                 r1.Exclude (rect2);
852
853                                 rects = r1.GetRegionScans (new Matrix ());
854                                 Assert.AreEqual (1, rects.Length, "TEG10Test1");
855                                 AssertEqualRectangles (new RectangleF (20, 20, 100, 60), rects[0], "TEG10Test2");
856                         }
857
858
859                 }
860
861                 [Test]
862                 public void TestExcludeGroup2 ()
863                 {
864                         Bitmap bmp = new Bitmap (600, 800);
865                         Graphics dc = Graphics.FromImage (bmp);
866                         Matrix matrix = new Matrix ();
867                         Rectangle rect1, rect2;
868                         Region rgn1;
869                         RectangleF [] rects;
870
871                         rect1 = new Rectangle (130, 30, 60, 80);
872                         rect2 = new Rectangle (170, 40, 60, 80);
873                         rgn1 = new Region (rect1);
874                         rgn1.Exclude (rect2);
875                         rects = rgn1.GetRegionScans (matrix);
876
877                         Assert.AreEqual (2, rects.Length);
878
879                         Assert.AreEqual (130, rects[0].X);
880                         Assert.AreEqual (30, rects[0].Y);
881                         Assert.AreEqual (60, rects[0].Width);
882                         Assert.AreEqual (10, rects[0].Height);
883
884                         Assert.AreEqual (130, rects[1].X);
885                         Assert.AreEqual (40, rects[1].Y);
886                         Assert.AreEqual (40, rects[1].Width);
887                         Assert.AreEqual (70, rects[1].Height);
888                 }
889
890                 [Test]
891                 public void ExcludeBug402613 ()
892                 {
893                         Region r = new Region();
894                         r.MakeInfinite ();
895                         r.Exclude (new Rectangle (387,292,189,133));
896                         r.Exclude (new Rectangle (387,66,189,133));
897                         Assert.IsTrue (r.IsVisible (new Rectangle (66,292,189,133)));
898                 }
899
900                 [Test]
901                 public void TestIntersect()
902                 {
903
904
905                         Bitmap bmp = new Bitmap (600, 800);
906                         Graphics dc = Graphics.FromImage (bmp);
907                         Matrix matrix = new Matrix ();
908                         RectangleF [] rects;
909                         RectangleF rect3, rect4;
910                         Region rgn3, rgn4;
911
912                         /* Two simple areas */
913                         Rectangle rect1 = new Rectangle (260, 30, 60, 80);
914                         Rectangle rect2 = new Rectangle (290, 40, 60, 80);
915                         Region rgn1 = new Region (rect1);
916                         Region rgn2 = new Region (rect2);
917                         rgn1.Intersect (rgn2);
918
919                         rects = rgn1.GetRegionScans (matrix);
920                         Assert.AreEqual (1, rects.Length);
921
922                         Assert.AreEqual (290, rects[0].X);
923                         Assert.AreEqual (40, rects[0].Y);
924                         Assert.AreEqual (30, rects[0].Width);
925                         Assert.AreEqual (70, rects[0].Height);                                                  
926
927                         /* No intersect */
928                         rect1 = new Rectangle (20, 330, 40, 50);
929                         rect2 = new Rectangle (50, 340, 40, 50);
930                         rect3 = new Rectangle (70, 360, 30, 50);
931                         rect4 = new Rectangle (80, 400, 30, 10);
932                         rgn1 = new Region (rect1);
933                         rgn2 = new Region (rect2);
934                         rgn3 = new Region (rect3);
935                         rgn4 = new Region (rect4);
936
937                         rgn1.Intersect (rgn2);
938                         rgn1.Intersect (rgn3);
939                         rgn1.Intersect (rgn4);
940                         rects = rgn1.GetRegionScans (matrix);
941                         Assert.AreEqual (0, rects.Length);
942                 }
943
944                 [Test]
945                 public void TestXor()
946                 {
947                         Bitmap bmp = new Bitmap (600, 800);
948                         Graphics dc = Graphics.FromImage (bmp);
949                         Matrix matrix = new Matrix ();
950                         RectangleF [] rects;
951
952                         Rectangle rect1 = new Rectangle (380, 30, 60, 80);
953                         Rectangle rect2 = new Rectangle (410, 40, 60, 80);
954                         Region rgn1 = new Region (rect1);
955                         Region rgn2 = new Region (rect2);
956                         rgn1.Xor (rgn2);
957
958
959                         rects = rgn1.GetRegionScans (matrix);
960
961                         Assert.AreEqual (4, rects.Length);
962
963                         Assert.AreEqual (380, rects[0].X);
964                         Assert.AreEqual (30, rects[0].Y);
965                         Assert.AreEqual (60, rects[0].Width);
966                         Assert.AreEqual (10, rects[0].Height);
967
968                         Assert.AreEqual (380, rects[1].X);
969                         Assert.AreEqual (40, rects[1].Y);
970                         Assert.AreEqual (30, rects[1].Width);
971                         Assert.AreEqual (70, rects[1].Height);
972
973                         Assert.AreEqual (440, rects[2].X);
974                         Assert.AreEqual (40, rects[2].Y);
975                         Assert.AreEqual (30, rects[2].Width);
976                         Assert.AreEqual (70, rects[2].Height);
977
978                         Assert.AreEqual (410, rects[3].X);
979                         Assert.AreEqual (110, rects[3].Y);
980                         Assert.AreEqual (60, rects[3].Width);
981                         Assert.AreEqual (10, rects[3].Height);
982                 }
983
984
985                 [Test]
986                 public void TestIsVisible()
987                 {
988                         Bitmap bmp = new Bitmap (600, 800);
989                         Graphics dc = Graphics.FromImage (bmp);
990                         Rectangle rect1, rect2;
991                         Region rgn1, rgn2;
992                         Matrix matrix = new Matrix ();
993
994                         rect1 = new Rectangle (500, 30, 60, 80);
995                         rect2 = new Rectangle (520, 40, 60, 80);
996
997                         rgn1 = new Region (new RectangleF (0, 0, 10,10));
998                         Assert.AreEqual (false, rgn1.IsVisible (0,0,0,1));
999
1000                         rgn1 = new Region (rect1);
1001                         Assert.AreEqual (false, rgn1.IsVisible (500,29));
1002                         Assert.AreEqual (true, rgn1.IsVisible (500,30));
1003                         Assert.AreEqual (true, rgn1.IsVisible (rect1));
1004                         Assert.AreEqual (true, rgn1.IsVisible (rect2));
1005                         Assert.AreEqual (false, rgn1.IsVisible (new Rectangle (50,50,2,5)));
1006
1007                         Rectangle r = new Rectangle (1,1, 2,1);
1008                         rgn2 = new Region (r);
1009                         Assert.AreEqual (true, rgn2.IsVisible (r));
1010                         Assert.AreEqual (true, rgn2.IsVisible (new Rectangle (1,1, 2,2)));
1011                         Assert.AreEqual (true, rgn2.IsVisible (new Rectangle (1,1, 10,10)));
1012                         Assert.AreEqual (true, rgn2.IsVisible (new Rectangle (1,1, 1,1)));
1013                         Assert.AreEqual (false, rgn2.IsVisible (new Rectangle (2,2, 1,1)));
1014                         Assert.AreEqual (false, rgn2.IsVisible (new Rectangle (0,0, 1,1)));
1015                         Assert.AreEqual (false, rgn2.IsVisible (new Rectangle (3,3, 1,1)));
1016
1017                         Assert.AreEqual (false, rgn2.IsVisible (0,0));
1018                         Assert.AreEqual (false, rgn2.IsVisible (1,0));
1019                         Assert.AreEqual (false, rgn2.IsVisible (2,0));
1020                         Assert.AreEqual (false, rgn2.IsVisible (3,0));
1021                         Assert.AreEqual (false, rgn2.IsVisible (0,1));
1022                         Assert.AreEqual (true, rgn2.IsVisible (1,1));
1023                         Assert.AreEqual (true, rgn2.IsVisible (2,1));
1024                         Assert.AreEqual (false, rgn2.IsVisible (3,1));
1025                         Assert.AreEqual (false, rgn2.IsVisible (0,2));
1026                         Assert.AreEqual (false, rgn2.IsVisible (1,2));
1027                         Assert.AreEqual (false, rgn2.IsVisible (2,2));
1028                         Assert.AreEqual (false, rgn2.IsVisible (3,2));
1029
1030
1031                 }
1032
1033                 [Test]
1034                 public void TestTranslate()
1035                 {
1036                         Region rgn1 = new Region (new RectangleF (10, 10, 120,120));
1037                         rgn1.Translate (30,20);
1038                         Matrix matrix = new Matrix ();
1039
1040                         RectangleF [] rects = rgn1.GetRegionScans (matrix);
1041
1042                         Assert.AreEqual (1, rects.Length);
1043
1044                         Assert.AreEqual (40, rects[0].X);
1045                         Assert.AreEqual (30, rects[0].Y);
1046                         Assert.AreEqual (120, rects[0].Width);
1047                         Assert.AreEqual (120, rects[0].Height);
1048                 }
1049
1050                 [Test]
1051                 public void Constructor_GraphicsPath_Null ()
1052                 {
1053                         GraphicsPath gp = null;
1054                         Assert.Throws<ArgumentNullException> (() => new Region (gp));
1055                 }
1056
1057                 [Test]
1058                 public void Constructor_RegionData_Null ()
1059                 {
1060                         RegionData rd = null;
1061                         Assert.Throws<ArgumentNullException> (() => new Region (rd));
1062                 }
1063
1064                 [Test]
1065                 public void Union_GraphicsPath_Null ()
1066                 {
1067                         GraphicsPath gp = null;
1068                         Assert.Throws<ArgumentNullException> (() => new Region ().Union (gp));
1069                 }
1070
1071                 [Test]
1072                 public void Union_Region_Null ()
1073                 {
1074                         Region r = null;
1075                         Assert.Throws<ArgumentNullException> (() => new Region ().Union (r));
1076                 }
1077
1078                 [Test]
1079                 public void Union_Region_Infinite ()
1080                 {
1081                         // default ctor creates an infinite region
1082                         Region r = new Region ();
1083                         CheckEmpty ("default .ctor", r);
1084                         // union-ing to infinity doesn't change the results
1085                         r.Union (new Rectangle (10, 10, 100, 100));
1086                         CheckEmpty ("U infinity", r);
1087                 }
1088
1089                 [Test]
1090                 public void Intersect_GraphicsPath_Null ()
1091                 {
1092                         GraphicsPath gp = null;
1093                         Assert.Throws<ArgumentNullException> (() => new Region ().Intersect (gp));
1094                 }
1095
1096                 [Test]
1097                 public void Intersect_Region_Null ()
1098                 {
1099                         Region r = null;
1100                         Assert.Throws<ArgumentNullException> (() => new Region ().Intersect (r));
1101                 }
1102
1103                 [Test]
1104                 public void Complement_GraphicsPath_Null ()
1105                 {
1106                         GraphicsPath gp = null;
1107                         Assert.Throws<ArgumentNullException> (() => new Region ().Complement (gp));
1108                 }
1109
1110                 [Test]
1111                 public void Complement_Region_Null ()
1112                 {
1113                         Region r = null;
1114                         Assert.Throws<ArgumentNullException> (() => new Region ().Complement (r));
1115                 }
1116
1117                 [Test]
1118                 public void Exclude_GraphicsPath_Null ()
1119                 {
1120                         GraphicsPath gp = null;
1121                         Assert.Throws<ArgumentNullException> (() => new Region ().Exclude (gp));
1122                 }
1123
1124                 [Test]
1125                 public void Exclude_Region_Null ()
1126                 {
1127                         Region r = null;
1128                         Assert.Throws<ArgumentNullException> (() => new Region ().Exclude (r));
1129                 }
1130
1131                 [Test]
1132                 public void Xor_GraphicsPath_Null ()
1133                 {
1134                         GraphicsPath gp = null;
1135                         Assert.Throws<ArgumentNullException> (() => new Region ().Xor (gp));
1136                 }
1137
1138                 [Test]
1139                 public void Xor_Region_Null ()
1140                 {
1141                         Region r = null;
1142                         Assert.Throws<ArgumentNullException> (() => new Region ().Xor (r));
1143                 }
1144
1145                 [Test]
1146                 public void GetBounds_Null ()
1147                 {
1148                         Assert.Throws<ArgumentNullException> (() => new Region ().GetBounds (null));
1149                 }
1150
1151                 [Test]
1152                 public void IsVisible_IntIntNull ()
1153                 {
1154                         Assert.IsTrue (new Region ().IsVisible (0, 0, null));
1155                 }
1156
1157                 [Test]
1158                 public void IsVisible_IntIntIntIntNull ()
1159                 {
1160                         Assert.IsFalse (new Region ().IsVisible (0, 0, 0, 0, null));
1161                 }
1162
1163                 [Test]
1164                 public void IsVisible_PointNull ()
1165                 {
1166                         Point p = new Point ();
1167                         Assert.IsTrue (new Region ().IsVisible (p, null));
1168                 }
1169
1170                 [Test]
1171                 public void IsVisible_PointFNull ()
1172                 {
1173                         PointF p = new PointF ();
1174                         Assert.IsTrue (new Region ().IsVisible (p, null));
1175                 }
1176
1177                 [Test]
1178                 public void IsVisible_RectangleNull ()
1179                 {
1180                         Rectangle r = new Rectangle ();
1181                         Assert.IsFalse (new Region ().IsVisible (r, null));
1182                 }
1183
1184                 [Test]
1185                 public void IsVisible_RectangleFNull ()
1186                 {
1187                         RectangleF r = new RectangleF ();
1188                         Assert.IsFalse (new Region ().IsVisible (r, null));
1189                 }
1190
1191                 [Test]
1192                 public void IsVisible_SingleSingleNull ()
1193                 {
1194                         Assert.IsTrue (new Region ().IsVisible (0f, 0f, null));
1195                 }
1196
1197                 [Test]
1198                 public void IsVisible_SingleSingleSingleSingleNull ()
1199                 {
1200                         Assert.IsFalse (new Region ().IsVisible (0f, 0f, 0f, 0f, null));
1201                 }
1202
1203                 [Test]
1204                 public void IsEmpty_Null ()
1205                 {
1206                         Assert.Throws<ArgumentNullException> (() => new Region ().IsEmpty (null));
1207                 }
1208
1209                 [Test]
1210                 public void IsInfinite_Null ()
1211                 {
1212                         Assert.Throws<ArgumentNullException> (() => new Region ().IsInfinite (null));
1213                 }
1214
1215                 [Test]
1216                 public void Equals_NullGraphics ()
1217                 {
1218                         Assert.Throws<ArgumentNullException> (() => new Region ().Equals (null, Graphics.FromImage (new Bitmap (10, 10))));
1219                 }
1220
1221                 [Test]
1222                 public void Equals_RegionNull ()
1223                 {
1224                         Assert.Throws<ArgumentNullException> (() => new Region ().Equals (new Region (), null));
1225                 }
1226
1227                 [Test]
1228                 [Category ("NotWorking")] // caused regression in SWF
1229                 public void GetHrgn_Null ()
1230                 {
1231                         Assert.Throws<ArgumentNullException> (() => new Region ().GetHrgn (null));
1232                 }
1233
1234                 [Test]
1235                 public void GetRegionScans_Null ()
1236                 {
1237                         Assert.Throws<ArgumentNullException> (() => new Region ().GetRegionScans (null));
1238                 }
1239
1240                 [Test]
1241                 public void Transform_Null ()
1242                 {
1243                         Assert.Throws<ArgumentNullException> (() => new Region ().Transform (null));
1244                 }
1245
1246                 // an "empty ctor" Region is infinite
1247                 private void CheckEmpty (string prefix, Region region)
1248                 {
1249                         Assert.IsFalse (region.IsEmpty (graphic), prefix + "IsEmpty");
1250                         Assert.IsTrue (region.IsInfinite (graphic), prefix + "graphic");
1251
1252                         RectangleF rect = region.GetBounds (graphic);
1253                         Assert.AreEqual (-4194304f, rect.X, prefix + "GetBounds.X");
1254                         Assert.AreEqual (-4194304f, rect.Y, prefix + "GetBounds.Y");
1255                         Assert.AreEqual (8388608f, rect.Width, prefix + "GetBounds.Width");
1256                         Assert.AreEqual (8388608f, rect.Height, prefix + "GetBounds.Height");
1257                 }
1258
1259                 [Test]
1260                 public void Region_Empty ()
1261                 {
1262                         Region region = new Region ();
1263                         CheckEmpty ("Empty.", region);
1264
1265                         Region clone = region.Clone ();
1266                         CheckEmpty ("Clone.", region);
1267
1268                         RegionData data = region.GetRegionData ();
1269                         Region r2 = new Region (data);
1270                         CheckEmpty ("RegionData.", region);
1271                 }
1272
1273                 [Test]
1274                 [Category ("NotWorking")]
1275                 public void Region_Infinite_MultipleRectangles ()
1276                 {
1277                         Region region = new Region ();
1278                         Assert.IsTrue (region.IsInfinite (graphic), "Empty.IsInfinite");
1279
1280                         GraphicsPath gp = new GraphicsPath ();
1281                         gp.AddRectangle (new Rectangle (-4194304, -4194304, 8388608, 8388608));
1282                         region = new Region (gp);
1283                         Assert.IsTrue (region.IsInfinite (graphic), "OneRectangle.IsInfinite");
1284
1285                         gp.AddRectangle (new Rectangle (1, 1, 2, 2));
1286                         region = new Region (gp);
1287                         Assert.IsFalse (region.IsInfinite (graphic), "TwoOverlappingRectangle.IsInfinite");
1288
1289                         gp = new GraphicsPath ();
1290                         gp.AddRectangle (new Rectangle (-4194304, -4194304, 4194304, 8388608));
1291                         gp.AddRectangle (new Rectangle (0, -4194304, 4194304, 8388608));
1292                         Assert.IsFalse (region.IsInfinite (graphic), "TwoSideBySideRectangle.IsInfinite");
1293                 }
1294
1295                 [Test]
1296                 public void Rectangle_GetRegionScans ()
1297                 {
1298                         Matrix matrix = new Matrix ();
1299                         GraphicsPath gp = new GraphicsPath ();
1300                         gp.AddRectangle (new Rectangle (10, 10, 10, 10));
1301                         Region region = new Region (gp);
1302                         Assert.AreEqual (1, region.GetRegionScans (matrix).Length, "1");
1303
1304                         gp.AddRectangle (new Rectangle (20, 20, 20, 20));
1305                         region = new Region (gp);
1306                         Assert.AreEqual (2, region.GetRegionScans (matrix).Length, "2");
1307                 }
1308
1309                 [Test]
1310                 public void InfinityExclude ()
1311                 {
1312                         using (Region r = new Region ()) {
1313                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1314                                 r.Exclude (new Rectangle (5, 5, 10, 10));
1315                                 Assert.IsFalse (r.IsInfinite (graphic), "after");
1316                                 RectangleF bounds = r.GetBounds (graphic);
1317                                 Assert.AreEqual (-4194304, bounds.X, "X");
1318                                 Assert.AreEqual (-4194304, bounds.Y, "Y");
1319                                 Assert.AreEqual (8388608, bounds.Width, "Width");
1320                                 Assert.AreEqual (8388608, bounds.Height, "Height");
1321                         }
1322                 }
1323
1324                 [Test]
1325                 public void InfinityIntersect ()
1326                 {
1327                         using (Region r = new Region ()) {
1328                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1329                                 r.Intersect (new Rectangle (-10, -10, 20, 20));
1330                                 Assert.IsFalse (r.IsInfinite (graphic), "after");
1331                                 RectangleF bounds = r.GetBounds (graphic);
1332                                 Assert.AreEqual (-10, bounds.X, "X");
1333                                 Assert.AreEqual (-10, bounds.Y, "Y");
1334                                 Assert.AreEqual (20, bounds.Width, "Width");
1335                                 Assert.AreEqual (20, bounds.Height, "Height");
1336                         }
1337                 }
1338
1339                 [Test]
1340                 public void InfinityIntersectTranslate ()
1341                 {
1342                         using (Region r = new Region ()) {
1343                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1344                                 r.Intersect (new Rectangle (-10, -10, 20, 20));
1345                                 r.Translate (10, 10);
1346                                 RectangleF bounds = r.GetBounds (graphic);
1347                                 Assert.AreEqual (0, bounds.X, "X");
1348                                 Assert.AreEqual (0, bounds.Y, "Y");
1349                                 Assert.AreEqual (20, bounds.Width, "Width");
1350                                 Assert.AreEqual (20, bounds.Height, "Height");
1351                         }
1352                 }
1353
1354                 [Test]
1355                 public void InfinityIntersectScale ()
1356                 {
1357                         using (Region r = new Region ()) {
1358                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1359                                 r.Intersect (new Rectangle (-10, -10, 20, 20));
1360                                 using (Matrix m = new Matrix ()) {
1361                                         m.Scale (2, 0.5f);
1362                                         r.Transform (m);
1363                                 }
1364                                 RectangleF bounds = r.GetBounds (graphic);
1365                                 Assert.AreEqual (-20, bounds.X, "X");
1366                                 Assert.AreEqual (-5, bounds.Y, "Y");
1367                                 Assert.AreEqual (40, bounds.Width, "Width");
1368                                 Assert.AreEqual (10, bounds.Height, "Height");
1369                         }
1370                 }
1371
1372                 [Test]
1373                 public void InfinityIntersectTransform ()
1374                 {
1375                         using (Region r = new Region ()) {
1376                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1377                                 r.Intersect (new Rectangle (-10, -10, 20, 20));
1378                                 using (Matrix m = new Matrix (2, 0, 0, 0.5f, 10, 10)) {
1379                                         r.Transform (m);
1380                                 }
1381                                 RectangleF bounds = r.GetBounds (graphic);
1382                                 Assert.AreEqual (-10, bounds.X, "X");
1383                                 Assert.AreEqual (5, bounds.Y, "Y");
1384                                 Assert.AreEqual (40, bounds.Width, "Width");
1385                                 Assert.AreEqual (10, bounds.Height, "Height");
1386                         }
1387                 }
1388
1389                 [Test]
1390                 public void InfinityTranslate ()
1391                 {
1392                         using (Region r = new Region ()) {
1393                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1394                                 r.Translate (10, 10);
1395                                 Assert.IsTrue (r.IsInfinite (graphic), "after");
1396                                 CheckEmpty ("InfinityTranslate", r);
1397                         }
1398                 }
1399
1400                 [Test]
1401                 public void InfinityScaleUp ()
1402                 {
1403                         using (Region r = new Region ()) {
1404                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1405                                 using (Matrix m = new Matrix ()) {
1406                                         m.Scale (2, 2);
1407                                         r.Transform (m);
1408                                 }
1409                                 Assert.IsTrue (r.IsInfinite (graphic), "after");
1410                                 CheckEmpty ("InfinityScaleUp", r);
1411                         }
1412                 }
1413
1414                 [Test]
1415                 public void InfinityScaleDown ()
1416                 {
1417                         using (Region r = new Region ()) {
1418                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1419                                 using (Matrix m = new Matrix ()) {
1420                                         m.Scale (0.5f, 0.5f);
1421                                         r.Transform (m);
1422                                 }
1423                                 Assert.IsTrue (r.IsInfinite (graphic), "after");
1424                                 CheckEmpty ("InfinityScaleDown", r);
1425                         }
1426                 }
1427
1428                 [Test]
1429                 public void InfinityRotate ()
1430                 {
1431                         using (Region r = new Region ()) {
1432                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1433                                 using (Matrix m = new Matrix ()) {
1434                                         m.Rotate (45);
1435                                         r.Transform (m);
1436                                 }
1437                                 Assert.IsTrue (r.IsInfinite (graphic), "after");
1438                                 CheckEmpty ("InfinityRotate", r);
1439                         }
1440                 }
1441
1442                 [Test]
1443                 public void Intersect_383878 ()
1444                 {
1445                         using (Region clipRegion = new Region ()) {
1446                                 clipRegion.MakeInfinite ();
1447
1448                                 Rectangle smaller = new Rectangle (5, 5, -10, -10);
1449
1450                                 clipRegion.Intersect (smaller);
1451                                 Assert.IsFalse (clipRegion.IsEmpty (graphic), "IsEmpty");
1452                                 Assert.IsFalse (clipRegion.IsInfinite (graphic), "IsInfinite");
1453
1454                                 RectangleF [] rects = clipRegion.GetRegionScans (new Matrix ());
1455                                 Assert.AreEqual (1, rects.Length, "Length");
1456                                 Assert.AreEqual (new RectangleF (-5, -5, 10, 10), rects [0], "0");
1457                         }
1458                 }
1459
1460                 [Test]
1461                 public void Complement_383878 ()
1462                 {
1463                         using (Region clipRegion = new Region ()) {
1464                                 clipRegion.MakeInfinite ();
1465
1466                                 Rectangle smaller = new Rectangle (5, 5, -10, -10);
1467                                 Rectangle bigger = new Rectangle (-5, -5, 12, 12);
1468
1469                                 clipRegion.Intersect (smaller);
1470                                 clipRegion.Complement (bigger);
1471
1472                                 Assert.IsFalse (clipRegion.IsEmpty (graphic), "IsEmpty");
1473                                 Assert.IsFalse (clipRegion.IsInfinite (graphic), "IsInfinite");
1474
1475                                 RectangleF [] rects = clipRegion.GetRegionScans (new Matrix ());
1476                                 Assert.AreEqual (2, rects.Length, "Length");
1477                                 Assert.AreEqual (new RectangleF (5, -5, 2, 10), rects [0], "0");
1478                                 Assert.AreEqual (new RectangleF (-5, 5, 12, 2), rects [1], "1");
1479                         }
1480                 }
1481         }
1482
1483         [TestFixture]
1484         // the test cases in this fixture aren't restricted wrt running unmanaged code
1485         public class RegionTestUnmanaged {
1486                 private Bitmap bitmap;
1487                 private Graphics graphic;
1488
1489                 [TestFixtureSetUp]
1490                 public void FixtureSetUp ()
1491                 {
1492                         bitmap = new Bitmap (10, 10);
1493                         graphic = Graphics.FromImage (bitmap);
1494                 }
1495
1496                 // Note: Test cases calling GetHrng will leak memory unless ReleaseHrgn
1497                 // (which only exists in 2.0) is called.
1498
1499                 [Test]
1500                 public void GetHrgn_Infinite_MakeEmpty ()
1501                 {
1502                         Region r = new Region ();
1503                         Assert.IsFalse (r.IsEmpty (graphic), "!Empty");
1504                         Assert.IsTrue (r.IsInfinite (graphic), "Infinite");
1505                         Assert.AreEqual (IntPtr.Zero, r.GetHrgn (graphic), "Handle==0");
1506
1507                         r.MakeEmpty ();
1508                         Assert.IsTrue (r.IsEmpty (graphic), "Empty");
1509                         Assert.IsFalse (r.IsInfinite (graphic), "!Infinite");
1510                         IntPtr h = r.GetHrgn (graphic);
1511                         Assert.IsFalse (h == IntPtr.Zero, "Handle!=0");
1512                         r.ReleaseHrgn (h);
1513                 }
1514
1515                 [Test]
1516                 public void GetHrgn_Empty_MakeInfinite ()
1517                 {
1518                         Region r = new Region (new GraphicsPath ());
1519                         Assert.IsTrue (r.IsEmpty (graphic), "Empty");
1520                         Assert.IsFalse (r.IsInfinite (graphic), "!Infinite");
1521                         IntPtr h = r.GetHrgn (graphic);
1522                         Assert.IsFalse (h == IntPtr.Zero, "Handle!=0");
1523
1524                         r.MakeInfinite ();
1525                         Assert.IsFalse (r.IsEmpty (graphic), "!Empty");
1526                         Assert.IsTrue (r.IsInfinite (graphic), "Infinite");
1527                         Assert.AreEqual (IntPtr.Zero, r.GetHrgn (graphic), "Handle==0");
1528                         r.ReleaseHrgn (h);
1529                 }
1530
1531                 [Test]
1532                 public void GetHrgn_TwiceFromSameRegionInstance ()
1533                 {
1534                         Region r = new Region (new GraphicsPath ());
1535                         IntPtr h1 = r.GetHrgn (graphic);
1536                         IntPtr h2 = r.GetHrgn (graphic);
1537                         Assert.IsFalse (h1 == h2, "Handle_1!=Handle_2");
1538                         r.ReleaseHrgn (h1);
1539                         r.ReleaseHrgn (h2);
1540                 }
1541
1542                 [Test]
1543                 public void GetHrgn_FromHrgn ()
1544                 {
1545                         Region r1 = new Region (new GraphicsPath ());
1546                         IntPtr h1 = r1.GetHrgn (graphic);
1547                         Assert.IsFalse (h1 == IntPtr.Zero, "Handle_1!=0");
1548
1549                         Region r2 = Region.FromHrgn (h1);
1550                         IntPtr h2 = r2.GetHrgn (graphic);
1551                         Assert.IsFalse (h2 == IntPtr.Zero, "Handle_2!=0");
1552                         Assert.IsFalse (h1 == h2, "Handle_1!=Handle_2");
1553                         r1.ReleaseHrgn (h1);
1554                         r2.ReleaseHrgn (h2);
1555                 }
1556
1557                 [Test]
1558                 public void FromHrgn_Zero ()
1559                 {
1560                         Assert.Throws<ArgumentException> (() => Region.FromHrgn (IntPtr.Zero));
1561                 }
1562                 [Test]
1563                 public void ReleaseHrng_Zero ()
1564                 {
1565                         Region r = new Region (new GraphicsPath ());
1566                         Assert.Throws<ArgumentNullException> (() => r.ReleaseHrgn (IntPtr.Zero));
1567                 }
1568
1569                 [Test]
1570                 public void ReleaseHrng ()
1571                 {
1572                         Region r = new Region (new GraphicsPath ());
1573                         IntPtr ptr = r.GetHrgn (graphic);
1574                         Assert.IsFalse (IntPtr.Zero == ptr, "ptr");
1575                         r.ReleaseHrgn (ptr);
1576                 }
1577         }
1578 }