0a71c9dd8cab9910c93db808c6a5f09bd166f794
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Drawing2D / TestMatrix.cs
1 //
2 // Tests for System.Drawing.Drawing2D.Matrix.cs
3 //
4 // Authors:
5 //      Jordi Mas i Hernandez <jordi@ximian.com>
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005-2006 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 NUnit.Framework;
31 using System;
32 using System.Drawing;
33 using System.Drawing.Drawing2D;
34 using System.Security.Permissions;
35
36 namespace MonoTests.System.Drawing.Drawing2D
37 {
38         [TestFixture]
39         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
40         public class MatrixTest : Assertion {
41
42                 private Matrix default_matrix;
43                 private Rectangle rect;
44                 private RectangleF rectf;
45
46                 [TestFixtureSetUp]
47                 public void FixtureSetUp ()
48                 {
49                         default_matrix = new Matrix ();
50                 }
51
52                 [Test]
53                 public void Constructor_Default ()
54                 {
55                         Matrix matrix = new Matrix ();
56                         AssertEquals ("C#1", 6, matrix.Elements.Length);
57                 }
58
59                 [Test]
60                 public void Constructor_SixFloats ()
61                 {
62                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
63                         AssertEquals ("C#2", 6, matrix.Elements.Length);
64                         AssertEquals ("C#3", 10, matrix.Elements[0]);
65                         AssertEquals ("C#4", 20, matrix.Elements[1]);
66                         AssertEquals ("C#5", 30, matrix.Elements[2]);
67                         AssertEquals ("C#6", 40, matrix.Elements[3]);
68                         AssertEquals ("C#7", 50, matrix.Elements[4]);
69                         AssertEquals ("C#8", 60, matrix.Elements[5]);
70                 }
71
72                 [Test]
73                 public void Constructor_Float ()
74                 {
75                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
76                         AssertEquals ("C#2", 6, matrix.Elements.Length);
77                         AssertEquals ("C#3", 10, matrix.Elements[0]);
78                         AssertEquals ("C#4", 20, matrix.Elements[1]);
79                         AssertEquals ("C#5", 30, matrix.Elements[2]);
80                         AssertEquals ("C#6", 40, matrix.Elements[3]);
81                         AssertEquals ("C#7", 50, matrix.Elements[4]);
82                         AssertEquals ("C#8", 60, matrix.Elements[5]);
83                 }
84
85                 [Test]
86                 [ExpectedException (typeof (ArgumentNullException))]
87                 public void Constructor_Int_Null ()
88                 {
89                         new Matrix (rect, null);
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (ArgumentException))]
94                 public void Constructor_Int_Empty ()
95                 {
96                         new Matrix (rect, new Point[0]);
97                 }
98
99                 [Test]
100                 [ExpectedException (typeof (ArgumentNullException))]
101                 public void Constructor_Float_Null ()
102                 {
103                         new Matrix (rectf, null);
104                 }
105
106                 [Test]
107                 [ExpectedException (typeof (ArgumentException))]
108                 public void Constructor_Float_Empty ()
109                 {
110                         new Matrix (rectf, new PointF[0]);
111                 }
112
113                 // Properties
114
115                 [Test]
116                 public void Invertible ()
117                 {
118                         Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
119                         AssertEquals ("I#1", false, matrix.IsInvertible);
120
121                         matrix = new Matrix (156, 46, 0, 0, 106, 19);
122                         AssertEquals ("I#2", false, matrix.IsInvertible);
123
124                         matrix = new Matrix (146, 66, 158, 104, 42, 150);
125                         AssertEquals ("I#3", true, matrix.IsInvertible);
126
127                         matrix = new Matrix (119, 140, 145, 74, 102, 58);
128                         AssertEquals ("I#4", true, matrix.IsInvertible);
129                 }
130                 
131                 [Test]
132                 public void IsIdentity ()
133                 {
134                         Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
135                         AssertEquals ("N#1", false, matrix.IsIdentity);
136                         
137                         matrix = new Matrix (1, 0, 0, 1, 0, 0);
138                         AssertEquals ("N#2", true, matrix.IsIdentity);                  
139                 }
140                 
141                 [Test]
142                 public void IsOffsetX ()
143                 {
144                         Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
145                         AssertEquals ("X#1", 47, matrix.OffsetX);                       
146                 }
147                 
148                 [Test]
149                 public void IsOffsetY ()
150                 {
151                         Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
152                         AssertEquals ("Y#1", 30, matrix.OffsetY);                       
153                 }
154                 
155                 // Elements Property is checked implicity in other test
156
157                 //
158                 // Methods
159                 //
160                 
161
162                 [Test]
163                 public void Clone ()
164                 {
165                         Matrix matsrc = new Matrix (10, 20, 30, 40, 50, 60);
166                         Matrix matrix  = matsrc.Clone ();
167
168                         AssertEquals ("D#1", 6, matrix.Elements.Length);
169                         AssertEquals ("D#2", 10, matrix.Elements[0]);
170                         AssertEquals ("D#3", 20, matrix.Elements[1]);
171                         AssertEquals ("D#4", 30, matrix.Elements[2]);
172                         AssertEquals ("D#5", 40, matrix.Elements[3]);
173                         AssertEquals ("D#6", 50, matrix.Elements[4]);
174                         AssertEquals ("D#7", 60, matrix.Elements[5]);
175                 }
176
177                 [Test]
178                 public void HashCode ()
179                 {
180                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
181                         Matrix clone = matrix.Clone ();
182                         Assert ("HashCode/Clone", matrix.GetHashCode () != clone.GetHashCode ());
183
184                         Matrix matrix2 = new Matrix (10, 20, 30, 40, 50, 60);
185                         Assert ("HashCode/Identical", matrix.GetHashCode () != matrix2.GetHashCode ());
186                 }
187
188                 [Test]
189                 public void Reset ()
190                 {
191                         Matrix matrix = new Matrix (51, 52, 53, 54, 55, 56);
192                         matrix.Reset ();
193
194                         AssertEquals ("F#1", 6, matrix.Elements.Length);
195                         AssertEquals ("F#2", 1, matrix.Elements[0]);
196                         AssertEquals ("F#3", 0, matrix.Elements[1]);
197                         AssertEquals ("F#4", 0, matrix.Elements[2]);
198                         AssertEquals ("F#5", 1, matrix.Elements[3]);
199                         AssertEquals ("F#6", 0, matrix.Elements[4]);
200                         AssertEquals ("F#7", 0, matrix.Elements[5]);
201                 }
202
203                 [Test]
204                 public void Rotate ()
205                 {
206                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
207                         matrix.Rotate (180);
208
209                         AssertEquals ("H#1", -10, matrix.Elements[0]);
210                         AssertEquals ("H#2", -20, matrix.Elements[1]);
211                         AssertEquals ("H#3", -30, matrix.Elements[2]);
212                         AssertEquals ("H#4", -40, matrix.Elements[3]);
213                         AssertEquals ("H#5", 50, matrix.Elements[4]);
214                         AssertEquals ("H#6", 60, matrix.Elements[5]);
215                 }
216
217                 [Test]
218                 [ExpectedException (typeof (ArgumentException))]
219                 public void Rotate_InvalidOrder ()
220                 {
221                         new Matrix ().Rotate (180, (MatrixOrder) Int32.MinValue);
222                 }
223
224                 [Test]
225                 public void RotateAt ()
226                 {
227                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
228                         matrix.RotateAt (180, new PointF (10, 10));
229
230                         AssertEquals ("I#1", -10, matrix.Elements[0]);
231                         AssertEquals ("I#2", -20, matrix.Elements[1]);
232                         AssertEquals ("I#3", -30, matrix.Elements[2]);
233                         AssertEquals ("I#4", -40, matrix.Elements[3]);
234                         AssertEquals ("I#5", 850, matrix.Elements[4]);
235                         AssertEquals ("I#6", 1260, matrix.Elements[5]);
236                 }
237
238                 [Test]
239                 [ExpectedException (typeof (ArgumentException))]
240                 public void RotateAt_InvalidOrder ()
241                 {
242                         new Matrix ().RotateAt (180, new PointF (10, 10), (MatrixOrder) Int32.MinValue);
243                 }
244
245                 [Test]
246                 [ExpectedException (typeof (ArgumentNullException))]
247                 public void Multiply_Null ()
248                 {
249                         new Matrix (10, 20, 30, 40, 50, 60).Multiply (null);
250                 }
251
252                 [Test]
253                 public void Multiply ()
254                 {
255                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
256                         matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60));
257
258                         AssertEquals ("J#1", 700, matrix.Elements[0]);
259                         AssertEquals ("J#2", 1000, matrix.Elements[1]);
260                         AssertEquals ("J#3", 1500, matrix.Elements[2]);
261                         AssertEquals ("J#4", 2200, matrix.Elements[3]);
262                         AssertEquals ("J#5", 2350, matrix.Elements[4]);
263                         AssertEquals ("J#6", 3460, matrix.Elements[5]);
264                 }
265
266                 [Test]
267                 [ExpectedException (typeof (ArgumentNullException))]
268                 public void Multiply_Null_Order ()
269                 {
270                         new Matrix (10, 20, 30, 40, 50, 60).Multiply (null, MatrixOrder.Append);
271                 }
272
273                 [Test]
274                 public void Multiply_Append ()
275                 {
276                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
277                         matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Append);
278
279                         AssertEquals ("J#1", 700, matrix.Elements[0]);
280                         AssertEquals ("J#2", 1000, matrix.Elements[1]);
281                         AssertEquals ("J#3", 1500, matrix.Elements[2]);
282                         AssertEquals ("J#4", 2200, matrix.Elements[3]);
283                         AssertEquals ("J#5", 2350, matrix.Elements[4]);
284                         AssertEquals ("J#6", 3460, matrix.Elements[5]);
285                 }
286
287                 [Test]
288                 public void Multiply_Prepend ()
289                 {
290                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
291                         matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Prepend);
292
293                         AssertEquals ("J#1", 700, matrix.Elements[0]);
294                         AssertEquals ("J#2", 1000, matrix.Elements[1]);
295                         AssertEquals ("J#3", 1500, matrix.Elements[2]);
296                         AssertEquals ("J#4", 2200, matrix.Elements[3]);
297                         AssertEquals ("J#5", 2350, matrix.Elements[4]);
298                         AssertEquals ("J#6", 3460, matrix.Elements[5]);
299                 }
300
301                 [Test]
302                 [ExpectedException (typeof (ArgumentException))]
303                 public void Multiply_InvalidOrder ()
304                 {
305                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
306                         matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), (MatrixOrder)Int32.MinValue);
307                 }
308
309                 [Test]
310                 public void Equals ()
311                 {
312                         Matrix mat1 = new Matrix (10, 20, 30, 40, 50, 60);
313                         Matrix mat2 = new Matrix (10, 20, 30, 40, 50, 60);
314                         Matrix mat3 = new Matrix (10, 20, 30, 40, 50, 10);
315
316                         AssertEquals ("E#1", true, mat1.Equals (mat2));
317                         AssertEquals ("E#2", false, mat2.Equals (mat3));
318                         AssertEquals ("E#3", false, mat1.Equals (mat3));
319                 }
320                 
321                 [Test]
322                 public void Invert ()
323                 {
324                         Matrix matrix = new Matrix (1, 2, 3, 4, 5, 6);
325                         matrix.Invert ();
326                         
327                         AssertEquals ("V#1", -2, matrix.Elements[0]);
328                         AssertEquals ("V#2", 1, matrix.Elements[1]);
329                         AssertEquals ("V#3", 1.5, matrix.Elements[2]);
330                         AssertEquals ("V#4", -0.5, matrix.Elements[3]);
331                         AssertEquals ("V#5", 1, matrix.Elements[4]);
332                         AssertEquals ("V#6", -2, matrix.Elements[5]);                   
333                 }
334
335                 [Test]
336                 public void Invert_Translation ()
337                 {
338                         Matrix matrix = new Matrix (1, 0, 0, 1, 8, 8);
339                         matrix.Invert ();
340
341                         float[] elements = matrix.Elements;
342                         AssertEquals ("#1", 1, elements[0]);
343                         AssertEquals ("#2", 0, elements[1]);
344                         AssertEquals ("#3", 0, elements[2]);
345                         AssertEquals ("#4", 1, elements[3]);
346                         AssertEquals ("#5", -8, elements[4]);
347                         AssertEquals ("#6", -8, elements[5]);
348                 }
349
350                 [Test]
351                 public void Invert_Identity ()
352                 {
353                         Matrix matrix = new Matrix ();
354                         Assert ("IsIdentity", matrix.IsIdentity);
355                         Assert ("IsInvertible", matrix.IsInvertible);
356                         matrix.Invert ();
357                         Assert ("IsIdentity-2", matrix.IsIdentity);
358                         Assert ("IsInvertible-2", matrix.IsInvertible);
359                 }
360
361                 [Test]
362                 public void Scale ()
363                 {
364                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
365                         matrix.Scale (2, 4);
366
367                         AssertEquals ("S#1", 20, matrix.Elements[0]);
368                         AssertEquals ("S#2", 40, matrix.Elements[1]);
369                         AssertEquals ("S#3", 120, matrix.Elements[2]);
370                         AssertEquals ("S#4", 160, matrix.Elements[3]);
371                         AssertEquals ("S#5", 50, matrix.Elements[4]);
372                         AssertEquals ("S#6", 60, matrix.Elements[5]);
373
374                         matrix.Scale (0.5f, 0.25f);
375
376                         AssertEquals ("SB#1", 10, matrix.Elements[0]);
377                         AssertEquals ("SB#2", 20, matrix.Elements[1]);
378                         AssertEquals ("SB#3", 30, matrix.Elements[2]);
379                         AssertEquals ("SB#4", 40, matrix.Elements[3]);
380                         AssertEquals ("SB#5", 50, matrix.Elements[4]);
381                         AssertEquals ("SB#6", 60, matrix.Elements[5]);
382                 }
383
384                 [Test]
385                 public void Scale_Negative ()
386                 {
387                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
388                         matrix.Scale (-2, -4);
389
390                         AssertEquals ("S#1", -20, matrix.Elements[0]);
391                         AssertEquals ("S#2", -40, matrix.Elements[1]);
392                         AssertEquals ("S#3", -120, matrix.Elements[2]);
393                         AssertEquals ("S#4", -160, matrix.Elements[3]);
394                         AssertEquals ("S#5", 50, matrix.Elements[4]);
395                         AssertEquals ("S#6", 60, matrix.Elements[5]);
396                 }
397
398                 [Test]
399                 [ExpectedException (typeof (ArgumentException))]
400                 public void Scale_InvalidOrder ()
401                 {
402                         new Matrix ().Scale (2, 1, (MatrixOrder) Int32.MinValue);
403                 }
404                 
405                 [Test]
406                 public void Shear ()
407                 {
408                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
409                         matrix.Shear (2, 4);
410
411                         AssertEquals ("H#1", 130, matrix.Elements[0]);
412                         AssertEquals ("H#2", 180, matrix.Elements[1]);
413                         AssertEquals ("H#3", 50, matrix.Elements[2]);
414                         AssertEquals ("H#4", 80, matrix.Elements[3]);
415                         AssertEquals ("H#5", 50, matrix.Elements[4]);
416                         AssertEquals ("H#6", 60, matrix.Elements[5]);
417                         
418                         matrix = new Matrix (5, 3, 9, 2, 2, 1);
419                         matrix.Shear  (10, 20);                 
420                         
421                         AssertEquals ("H#7", 185, matrix.Elements[0]);
422                         AssertEquals ("H#8", 43, matrix.Elements[1]);
423                         AssertEquals ("H#9", 59, matrix.Elements[2]);
424                         AssertEquals ("H#10", 32, matrix.Elements[3]);
425                         AssertEquals ("H#11", 2, matrix.Elements[4]);
426                         AssertEquals ("H#12", 1, matrix.Elements[5]);                       
427                 }
428
429                 [Test]
430                 [ExpectedException (typeof (ArgumentException))]
431                 public void Shear_InvalidOrder ()
432                 {
433                         new Matrix ().Shear (-1, 1, (MatrixOrder) Int32.MinValue);
434                 }
435                 
436                 [Test]
437                 public void TransformPoints ()
438                 {
439                         Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
440                         PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
441                         matrix.TransformPoints (pointsF);
442                                                 
443                         AssertEquals ("K#1", 38, pointsF[0].X);
444                         AssertEquals ("K#2", 52, pointsF[0].Y);
445                         AssertEquals ("K#3", 66, pointsF[1].X);
446                         AssertEquals ("K#4", 92, pointsF[1].Y);
447                         
448                         Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
449                         matrix.TransformPoints (points);
450                         AssertEquals ("K#5", 38, pointsF[0].X);
451                         AssertEquals ("K#6", 52, pointsF[0].Y);
452                         AssertEquals ("K#7", 66, pointsF[1].X);
453                         AssertEquals ("K#8", 92, pointsF[1].Y);                                             
454                 }
455
456                 [Test]
457                 [ExpectedException (typeof (ArgumentNullException))]
458                 public void TransformPoints_Point_Null ()
459                 {
460                         new Matrix ().TransformPoints ((Point[]) null);
461                 }
462
463                 [Test]
464                 [ExpectedException (typeof (ArgumentNullException))]
465                 public void TransformPoints_PointF_Null ()
466                 {
467                         new Matrix ().TransformPoints ((PointF[]) null);
468                 }
469
470                 [Test]
471                 [ExpectedException (typeof (ArgumentException))]
472                 public void TransformPoints_Point_Empty ()
473                 {
474                         new Matrix ().TransformPoints (new Point[0]);
475                 }
476
477                 [Test]
478                 [ExpectedException (typeof (ArgumentException))]
479                 public void TransformPoints_PointF_Empty ()
480                 {
481                         new Matrix ().TransformPoints (new PointF[0]);
482                 }
483                 
484                 [Test]
485                 public void TransformVectors  ()
486                 {
487                         Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
488                         PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
489                         matrix.TransformVectors (pointsF);
490                                                 
491                         AssertEquals ("N#1", 28, pointsF[0].X);
492                         AssertEquals ("N#2", 40, pointsF[0].Y);
493                         AssertEquals ("N#3", 56, pointsF[1].X);
494                         AssertEquals ("N#4", 80, pointsF[1].Y);
495                         
496                         Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
497                         matrix.TransformVectors (points);
498                         AssertEquals ("N#5", 28, pointsF[0].X);
499                         AssertEquals ("N#6", 40, pointsF[0].Y);
500                         AssertEquals ("N#7", 56, pointsF[1].X);
501                         AssertEquals ("N#8", 80, pointsF[1].Y);                                             
502                 }
503
504                 [Test]
505                 [ExpectedException (typeof (ArgumentNullException))]
506                 public void TransformVectors_Point_Null ()
507                 {
508                         new Matrix ().TransformVectors ((Point[]) null);
509                 }
510
511                 [Test]
512                 [ExpectedException (typeof (ArgumentNullException))]
513                 public void TransformVectors_PointF_Null ()
514                 {
515                         new Matrix ().TransformVectors ((PointF[]) null);
516                 }
517
518                 [Test]
519                 [ExpectedException (typeof (ArgumentException))]
520                 public void TransformVectors_Point_Empty ()
521                 {
522                         new Matrix ().TransformVectors (new Point[0]);
523                 }
524
525                 [Test]
526                 [ExpectedException (typeof (ArgumentException))]
527                 public void TransformVectors_PointF_Empty ()
528                 {
529                         new Matrix ().TransformVectors (new PointF[0]);
530                 }
531
532                 [Test]
533                 public void Translate  ()
534                 {
535                         Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);                        
536                         matrix.Translate (5, 10);
537                                                 
538                         AssertEquals ("Y#1", 2, matrix.Elements[0]);
539                         AssertEquals ("Y#2", 4, matrix.Elements[1]);
540                         AssertEquals ("Y#3", 6, matrix.Elements[2]);
541                         AssertEquals ("Y#4", 8, matrix.Elements[3]);
542                         AssertEquals ("Y#5", 80, matrix.Elements[4]);
543                         AssertEquals ("Y#6", 112, matrix.Elements[5]);  
544                 }
545
546                 [Test]
547                 [ExpectedException (typeof (ArgumentException))]
548                 public void Translate_InvalidOrder ()
549                 {
550                         new Matrix ().Translate (-1, 1, (MatrixOrder) Int32.MinValue);
551                 }
552
553                 [Test]
554                 [ExpectedException (typeof (ArgumentNullException))]
555                 public void VectorTransformPoints_Null ()
556                 {
557                         new Matrix ().VectorTransformPoints ((Point[]) null);
558                 }
559
560                 [Test]
561                 [ExpectedException (typeof (ArgumentException))]
562                 public void VectorTransformPoints_Empty ()
563                 {
564                         new Matrix ().VectorTransformPoints (new Point[0]);
565                 }
566         }
567 }