2 // Tests for System.Drawing.Drawing2D.Matrix.cs
5 // Jordi Mas i Hernandez <jordi@ximian.com>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005-2006 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
33 using System.Drawing.Drawing2D;
34 using System.Security.Permissions;
36 namespace MonoTests.System.Drawing.Drawing2D
39 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
40 public class MatrixTest : Assertion {
42 private Matrix default_matrix;
43 private Rectangle rect;
44 private RectangleF rectf;
47 public void FixtureSetUp ()
49 default_matrix = new Matrix ();
53 public void Constructor_Default ()
55 Matrix matrix = new Matrix ();
56 AssertEquals ("C#1", 6, matrix.Elements.Length);
60 public void Constructor_SixFloats ()
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]);
73 public void Constructor_Float ()
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]);
86 [ExpectedException (typeof (ArgumentNullException))]
87 public void Constructor_Int_Null ()
89 new Matrix (rect, null);
93 [ExpectedException (typeof (ArgumentException))]
94 public void Constructor_Int_Empty ()
96 new Matrix (rect, new Point[0]);
100 [ExpectedException (typeof (ArgumentNullException))]
101 public void Constructor_Float_Null ()
103 new Matrix (rectf, null);
107 [ExpectedException (typeof (ArgumentException))]
108 public void Constructor_Float_Empty ()
110 new Matrix (rectf, new PointF[0]);
116 public void Invertible ()
118 Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
119 AssertEquals ("I#1", false, matrix.IsInvertible);
121 matrix = new Matrix (156, 46, 0, 0, 106, 19);
122 AssertEquals ("I#2", false, matrix.IsInvertible);
124 matrix = new Matrix (146, 66, 158, 104, 42, 150);
125 AssertEquals ("I#3", true, matrix.IsInvertible);
127 matrix = new Matrix (119, 140, 145, 74, 102, 58);
128 AssertEquals ("I#4", true, matrix.IsInvertible);
132 public void IsIdentity ()
134 Matrix identity = new Matrix ();
135 Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
136 AssertEquals ("N#1-identity", false, matrix.IsIdentity);
137 Assert ("N#1-equals", !identity.Equals (matrix));
139 matrix = new Matrix (1, 0, 0, 1, 0, 0);
140 AssertEquals ("N#2-identity", true, matrix.IsIdentity);
141 Assert ("N#2-equals", identity.Equals (matrix));
143 // so what's the required precision ?
145 matrix = new Matrix (1.1f, 0.1f, -0.1f, 0.9f, 0, 0);
146 Assert ("N#3-identity", !matrix.IsIdentity);
147 Assert ("N#3-equals", !identity.Equals (matrix));
149 matrix = new Matrix (1.01f, 0.01f, -0.01f, 0.99f, 0, 0);
150 Assert ("N#4-identity", !matrix.IsIdentity);
151 Assert ("N#4-equals", !identity.Equals (matrix));
153 matrix = new Matrix (1.001f, 0.001f, -0.001f, 0.999f, 0, 0);
154 Assert ("N#5-identity", !matrix.IsIdentity);
155 Assert ("N#5-equals", !identity.Equals (matrix));
157 matrix = new Matrix (1.0001f, 0.0001f, -0.0001f, 0.9999f, 0, 0);
158 Assert ("N#6-identity", matrix.IsIdentity);
160 Assert ("N#6-equals", !identity.Equals (matrix));
162 matrix = new Matrix (1.0009f, 0.0009f, -0.0009f, 0.99995f, 0, 0);
163 Assert ("N#7-identity", !matrix.IsIdentity);
164 Assert ("N#7-equals", !identity.Equals (matrix));
168 public void IsOffsetX ()
170 Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
171 AssertEquals ("X#1", 47, matrix.OffsetX);
175 public void IsOffsetY ()
177 Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
178 AssertEquals ("Y#1", 30, matrix.OffsetY);
181 // Elements Property is checked implicity in other test
191 Matrix matsrc = new Matrix (10, 20, 30, 40, 50, 60);
192 Matrix matrix = matsrc.Clone ();
194 AssertEquals ("D#1", 6, matrix.Elements.Length);
195 AssertEquals ("D#2", 10, matrix.Elements[0]);
196 AssertEquals ("D#3", 20, matrix.Elements[1]);
197 AssertEquals ("D#4", 30, matrix.Elements[2]);
198 AssertEquals ("D#5", 40, matrix.Elements[3]);
199 AssertEquals ("D#6", 50, matrix.Elements[4]);
200 AssertEquals ("D#7", 60, matrix.Elements[5]);
204 public void HashCode ()
206 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
207 Matrix clone = matrix.Clone ();
208 Assert ("HashCode/Clone", matrix.GetHashCode () != clone.GetHashCode ());
210 Matrix matrix2 = new Matrix (10, 20, 30, 40, 50, 60);
211 Assert ("HashCode/Identical", matrix.GetHashCode () != matrix2.GetHashCode ());
217 Matrix matrix = new Matrix (51, 52, 53, 54, 55, 56);
220 AssertEquals ("F#1", 6, matrix.Elements.Length);
221 AssertEquals ("F#2", 1, matrix.Elements[0]);
222 AssertEquals ("F#3", 0, matrix.Elements[1]);
223 AssertEquals ("F#4", 0, matrix.Elements[2]);
224 AssertEquals ("F#5", 1, matrix.Elements[3]);
225 AssertEquals ("F#6", 0, matrix.Elements[4]);
226 AssertEquals ("F#7", 0, matrix.Elements[5]);
230 public void Rotate ()
232 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
235 AssertEquals ("H#1", -10, matrix.Elements[0]);
236 AssertEquals ("H#2", -20, matrix.Elements[1]);
237 AssertEquals ("H#3", -30, matrix.Elements[2]);
238 AssertEquals ("H#4", -40, matrix.Elements[3]);
239 AssertEquals ("H#5", 50, matrix.Elements[4]);
240 AssertEquals ("H#6", 60, matrix.Elements[5]);
244 public void Rotate_45_135 ()
246 Matrix matrix = new Matrix ();
247 Assert ("original.IsIdentity", matrix.IsIdentity);
250 Assert ("+45.!IsIdentity", !matrix.IsIdentity);
251 float[] elements = matrix.Elements;
252 AssertEquals ("45#1", 0.7071068, elements[0]);
253 AssertEquals ("45#2", 0.7071068, elements[1]);
254 AssertEquals ("45#3", -0.7071068, elements[2]);
255 AssertEquals ("45#4", 0.7071068, elements[3]);
256 AssertEquals ("45#5", 0, elements[4]);
257 AssertEquals ("45#6", 0, elements[5]);
260 Assert ("+135.!IsIdentity", !matrix.IsIdentity);
261 elements = matrix.Elements;
262 AssertEquals ("180#1", -1, elements[0], 0.0001);
263 AssertEquals ("180#2", 0, elements[1], 0.0001);
264 AssertEquals ("180#3", 0, elements[2], 0.0001);
265 AssertEquals ("180#4", -1, elements[3], 0.0001);
266 AssertEquals ("180#5", 0, elements[4]);
267 AssertEquals ("180#6", 0, elements[5]);
271 public void Rotate_90_270_Matrix ()
273 Matrix matrix = new Matrix ();
274 Assert ("original.IsIdentity", matrix.IsIdentity);
277 Assert ("+90.!IsIdentity", !matrix.IsIdentity);
278 float[] elements = matrix.Elements;
279 AssertEquals ("90#1", 0, elements[0], 0.0001);
280 AssertEquals ("90#2", 1, elements[1], 0.0001);
281 AssertEquals ("90#3", -1, elements[2], 0.0001);
282 AssertEquals ("90#4", 0, elements[3], 0.0001);
283 AssertEquals ("90#5", 0, elements[4]);
284 AssertEquals ("90#6", 0, elements[5]);
287 // this isn't a perfect 1, 0, 0, 1, 0, 0 matrix - but close enough
288 Assert ("360.IsIdentity", matrix.IsIdentity);
289 Assert ("360.Equals", !new Matrix ().Equals (matrix));
293 [ExpectedException (typeof (ArgumentException))]
294 public void Rotate_InvalidOrder ()
296 new Matrix ().Rotate (180, (MatrixOrder) Int32.MinValue);
300 public void RotateAt ()
302 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
303 matrix.RotateAt (180, new PointF (10, 10));
305 AssertEquals ("I#1", -10, matrix.Elements[0]);
306 AssertEquals ("I#2", -20, matrix.Elements[1]);
307 AssertEquals ("I#3", -30, matrix.Elements[2]);
308 AssertEquals ("I#4", -40, matrix.Elements[3]);
309 AssertEquals ("I#5", 850, matrix.Elements[4]);
310 AssertEquals ("I#6", 1260, matrix.Elements[5]);
314 [ExpectedException (typeof (ArgumentException))]
315 public void RotateAt_InvalidOrder ()
317 new Matrix ().RotateAt (180, new PointF (10, 10), (MatrixOrder) Int32.MinValue);
321 [ExpectedException (typeof (ArgumentNullException))]
322 public void Multiply_Null ()
324 new Matrix (10, 20, 30, 40, 50, 60).Multiply (null);
328 public void Multiply ()
330 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
331 matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60));
333 AssertEquals ("J#1", 700, matrix.Elements[0]);
334 AssertEquals ("J#2", 1000, matrix.Elements[1]);
335 AssertEquals ("J#3", 1500, matrix.Elements[2]);
336 AssertEquals ("J#4", 2200, matrix.Elements[3]);
337 AssertEquals ("J#5", 2350, matrix.Elements[4]);
338 AssertEquals ("J#6", 3460, matrix.Elements[5]);
342 [ExpectedException (typeof (ArgumentNullException))]
343 public void Multiply_Null_Order ()
345 new Matrix (10, 20, 30, 40, 50, 60).Multiply (null, MatrixOrder.Append);
349 public void Multiply_Append ()
351 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
352 matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Append);
354 AssertEquals ("J#1", 700, matrix.Elements[0]);
355 AssertEquals ("J#2", 1000, matrix.Elements[1]);
356 AssertEquals ("J#3", 1500, matrix.Elements[2]);
357 AssertEquals ("J#4", 2200, matrix.Elements[3]);
358 AssertEquals ("J#5", 2350, matrix.Elements[4]);
359 AssertEquals ("J#6", 3460, matrix.Elements[5]);
363 public void Multiply_Prepend ()
365 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
366 matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Prepend);
368 AssertEquals ("J#1", 700, matrix.Elements[0]);
369 AssertEquals ("J#2", 1000, matrix.Elements[1]);
370 AssertEquals ("J#3", 1500, matrix.Elements[2]);
371 AssertEquals ("J#4", 2200, matrix.Elements[3]);
372 AssertEquals ("J#5", 2350, matrix.Elements[4]);
373 AssertEquals ("J#6", 3460, matrix.Elements[5]);
377 [ExpectedException (typeof (ArgumentException))]
378 public void Multiply_InvalidOrder ()
380 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
381 matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), (MatrixOrder)Int32.MinValue);
385 public void Equals ()
387 Matrix mat1 = new Matrix (10, 20, 30, 40, 50, 60);
388 Matrix mat2 = new Matrix (10, 20, 30, 40, 50, 60);
389 Matrix mat3 = new Matrix (10, 20, 30, 40, 50, 10);
391 AssertEquals ("E#1", true, mat1.Equals (mat2));
392 AssertEquals ("E#2", false, mat2.Equals (mat3));
393 AssertEquals ("E#3", false, mat1.Equals (mat3));
397 public void Invert ()
399 Matrix matrix = new Matrix (1, 2, 3, 4, 5, 6);
402 AssertEquals ("V#1", -2, matrix.Elements[0]);
403 AssertEquals ("V#2", 1, matrix.Elements[1]);
404 AssertEquals ("V#3", 1.5, matrix.Elements[2]);
405 AssertEquals ("V#4", -0.5, matrix.Elements[3]);
406 AssertEquals ("V#5", 1, matrix.Elements[4]);
407 AssertEquals ("V#6", -2, matrix.Elements[5]);
411 public void Invert_Translation ()
413 Matrix matrix = new Matrix (1, 0, 0, 1, 8, 8);
416 float[] elements = matrix.Elements;
417 AssertEquals ("#1", 1, elements[0]);
418 AssertEquals ("#2", 0, elements[1]);
419 AssertEquals ("#3", 0, elements[2]);
420 AssertEquals ("#4", 1, elements[3]);
421 AssertEquals ("#5", -8, elements[4]);
422 AssertEquals ("#6", -8, elements[5]);
426 public void Invert_Identity ()
428 Matrix matrix = new Matrix ();
429 Assert ("IsIdentity", matrix.IsIdentity);
430 Assert ("IsInvertible", matrix.IsInvertible);
432 Assert ("IsIdentity-2", matrix.IsIdentity);
433 Assert ("IsInvertible-2", matrix.IsInvertible);
439 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
442 AssertEquals ("S#1", 20, matrix.Elements[0]);
443 AssertEquals ("S#2", 40, matrix.Elements[1]);
444 AssertEquals ("S#3", 120, matrix.Elements[2]);
445 AssertEquals ("S#4", 160, matrix.Elements[3]);
446 AssertEquals ("S#5", 50, matrix.Elements[4]);
447 AssertEquals ("S#6", 60, matrix.Elements[5]);
449 matrix.Scale (0.5f, 0.25f);
451 AssertEquals ("SB#1", 10, matrix.Elements[0]);
452 AssertEquals ("SB#2", 20, matrix.Elements[1]);
453 AssertEquals ("SB#3", 30, matrix.Elements[2]);
454 AssertEquals ("SB#4", 40, matrix.Elements[3]);
455 AssertEquals ("SB#5", 50, matrix.Elements[4]);
456 AssertEquals ("SB#6", 60, matrix.Elements[5]);
460 public void Scale_Negative ()
462 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
463 matrix.Scale (-2, -4);
465 AssertEquals ("S#1", -20, matrix.Elements[0]);
466 AssertEquals ("S#2", -40, matrix.Elements[1]);
467 AssertEquals ("S#3", -120, matrix.Elements[2]);
468 AssertEquals ("S#4", -160, matrix.Elements[3]);
469 AssertEquals ("S#5", 50, matrix.Elements[4]);
470 AssertEquals ("S#6", 60, matrix.Elements[5]);
474 [ExpectedException (typeof (ArgumentException))]
475 public void Scale_InvalidOrder ()
477 new Matrix ().Scale (2, 1, (MatrixOrder) Int32.MinValue);
483 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
486 AssertEquals ("H#1", 130, matrix.Elements[0]);
487 AssertEquals ("H#2", 180, matrix.Elements[1]);
488 AssertEquals ("H#3", 50, matrix.Elements[2]);
489 AssertEquals ("H#4", 80, matrix.Elements[3]);
490 AssertEquals ("H#5", 50, matrix.Elements[4]);
491 AssertEquals ("H#6", 60, matrix.Elements[5]);
493 matrix = new Matrix (5, 3, 9, 2, 2, 1);
494 matrix.Shear (10, 20);
496 AssertEquals ("H#7", 185, matrix.Elements[0]);
497 AssertEquals ("H#8", 43, matrix.Elements[1]);
498 AssertEquals ("H#9", 59, matrix.Elements[2]);
499 AssertEquals ("H#10", 32, matrix.Elements[3]);
500 AssertEquals ("H#11", 2, matrix.Elements[4]);
501 AssertEquals ("H#12", 1, matrix.Elements[5]);
505 [ExpectedException (typeof (ArgumentException))]
506 public void Shear_InvalidOrder ()
508 new Matrix ().Shear (-1, 1, (MatrixOrder) Int32.MinValue);
512 public void TransformPoints ()
514 Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
515 PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
516 matrix.TransformPoints (pointsF);
518 AssertEquals ("K#1", 38, pointsF[0].X);
519 AssertEquals ("K#2", 52, pointsF[0].Y);
520 AssertEquals ("K#3", 66, pointsF[1].X);
521 AssertEquals ("K#4", 92, pointsF[1].Y);
523 Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
524 matrix.TransformPoints (points);
525 AssertEquals ("K#5", 38, pointsF[0].X);
526 AssertEquals ("K#6", 52, pointsF[0].Y);
527 AssertEquals ("K#7", 66, pointsF[1].X);
528 AssertEquals ("K#8", 92, pointsF[1].Y);
532 [ExpectedException (typeof (ArgumentNullException))]
533 public void TransformPoints_Point_Null ()
535 new Matrix ().TransformPoints ((Point[]) null);
539 [ExpectedException (typeof (ArgumentNullException))]
540 public void TransformPoints_PointF_Null ()
542 new Matrix ().TransformPoints ((PointF[]) null);
546 [ExpectedException (typeof (ArgumentException))]
547 public void TransformPoints_Point_Empty ()
549 new Matrix ().TransformPoints (new Point[0]);
553 [ExpectedException (typeof (ArgumentException))]
554 public void TransformPoints_PointF_Empty ()
556 new Matrix ().TransformPoints (new PointF[0]);
560 public void TransformVectors ()
562 Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
563 PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
564 matrix.TransformVectors (pointsF);
566 AssertEquals ("N#1", 28, pointsF[0].X);
567 AssertEquals ("N#2", 40, pointsF[0].Y);
568 AssertEquals ("N#3", 56, pointsF[1].X);
569 AssertEquals ("N#4", 80, pointsF[1].Y);
571 Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
572 matrix.TransformVectors (points);
573 AssertEquals ("N#5", 28, pointsF[0].X);
574 AssertEquals ("N#6", 40, pointsF[0].Y);
575 AssertEquals ("N#7", 56, pointsF[1].X);
576 AssertEquals ("N#8", 80, pointsF[1].Y);
580 [ExpectedException (typeof (ArgumentNullException))]
581 public void TransformVectors_Point_Null ()
583 new Matrix ().TransformVectors ((Point[]) null);
587 [ExpectedException (typeof (ArgumentNullException))]
588 public void TransformVectors_PointF_Null ()
590 new Matrix ().TransformVectors ((PointF[]) null);
594 [ExpectedException (typeof (ArgumentException))]
595 public void TransformVectors_Point_Empty ()
597 new Matrix ().TransformVectors (new Point[0]);
601 [ExpectedException (typeof (ArgumentException))]
602 public void TransformVectors_PointF_Empty ()
604 new Matrix ().TransformVectors (new PointF[0]);
608 public void Translate ()
610 Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
611 matrix.Translate (5, 10);
613 AssertEquals ("Y#1", 2, matrix.Elements[0]);
614 AssertEquals ("Y#2", 4, matrix.Elements[1]);
615 AssertEquals ("Y#3", 6, matrix.Elements[2]);
616 AssertEquals ("Y#4", 8, matrix.Elements[3]);
617 AssertEquals ("Y#5", 80, matrix.Elements[4]);
618 AssertEquals ("Y#6", 112, matrix.Elements[5]);
622 [ExpectedException (typeof (ArgumentException))]
623 public void Translate_InvalidOrder ()
625 new Matrix ().Translate (-1, 1, (MatrixOrder) Int32.MinValue);
629 [ExpectedException (typeof (ArgumentNullException))]
630 public void VectorTransformPoints_Null ()
632 new Matrix ().VectorTransformPoints ((Point[]) null);
636 [ExpectedException (typeof (ArgumentException))]
637 public void VectorTransformPoints_Empty ()
639 new Matrix ().VectorTransformPoints (new Point[0]);