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 matrix = new Matrix (123, 24, 82, 16, 47, 30);
135 AssertEquals ("N#1", false, matrix.IsIdentity);
137 matrix = new Matrix (1, 0, 0, 1, 0, 0);
138 AssertEquals ("N#2", true, matrix.IsIdentity);
142 public void IsOffsetX ()
144 Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
145 AssertEquals ("X#1", 47, matrix.OffsetX);
149 public void IsOffsetY ()
151 Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
152 AssertEquals ("Y#1", 30, matrix.OffsetY);
155 // Elements Property is checked implicity in other test
165 Matrix matsrc = new Matrix (10, 20, 30, 40, 50, 60);
166 Matrix matrix = matsrc.Clone ();
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]);
178 public void HashCode ()
180 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
181 Matrix clone = matrix.Clone ();
182 Assert ("HashCode/Clone", matrix.GetHashCode () != clone.GetHashCode ());
184 Matrix matrix2 = new Matrix (10, 20, 30, 40, 50, 60);
185 Assert ("HashCode/Identical", matrix.GetHashCode () != matrix2.GetHashCode ());
191 Matrix matrix = new Matrix (51, 52, 53, 54, 55, 56);
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]);
204 public void Rotate ()
206 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
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]);
218 [ExpectedException (typeof (ArgumentException))]
219 public void Rotate_InvalidOrder ()
221 new Matrix ().Rotate (180, (MatrixOrder) Int32.MinValue);
225 public void RotateAt ()
227 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
228 matrix.RotateAt (180, new PointF (10, 10));
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]);
239 [ExpectedException (typeof (ArgumentException))]
240 public void RotateAt_InvalidOrder ()
242 new Matrix ().RotateAt (180, new PointF (10, 10), (MatrixOrder) Int32.MinValue);
246 [ExpectedException (typeof (ArgumentNullException))]
247 public void Multiply_Null ()
249 new Matrix (10, 20, 30, 40, 50, 60).Multiply (null);
253 public void Multiply ()
255 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
256 matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60));
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]);
267 [ExpectedException (typeof (ArgumentNullException))]
268 public void Multiply_Null_Order ()
270 new Matrix (10, 20, 30, 40, 50, 60).Multiply (null, MatrixOrder.Append);
274 public void Multiply_Append ()
276 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
277 matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Append);
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]);
288 public void Multiply_Prepend ()
290 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
291 matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Prepend);
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]);
302 [ExpectedException (typeof (ArgumentException))]
303 public void Multiply_InvalidOrder ()
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);
310 public void Equals ()
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);
316 AssertEquals ("E#1", true, mat1.Equals (mat2));
317 AssertEquals ("E#2", false, mat2.Equals (mat3));
318 AssertEquals ("E#3", false, mat1.Equals (mat3));
322 public void Invert ()
324 Matrix matrix = new Matrix (1, 2, 3, 4, 5, 6);
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]);
336 public void Invert_Translation ()
338 Matrix matrix = new Matrix (1, 0, 0, 1, 8, 8);
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]);
351 public void Invert_Identity ()
353 Matrix matrix = new Matrix ();
354 Assert ("IsIdentity", matrix.IsIdentity);
355 Assert ("IsInvertible", matrix.IsInvertible);
357 Assert ("IsIdentity-2", matrix.IsIdentity);
358 Assert ("IsInvertible-2", matrix.IsInvertible);
364 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
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]);
374 matrix.Scale (0.5f, 0.25f);
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]);
385 public void Scale_Negative ()
387 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
388 matrix.Scale (-2, -4);
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]);
399 [ExpectedException (typeof (ArgumentException))]
400 public void Scale_InvalidOrder ()
402 new Matrix ().Scale (2, 1, (MatrixOrder) Int32.MinValue);
408 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
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]);
418 matrix = new Matrix (5, 3, 9, 2, 2, 1);
419 matrix.Shear (10, 20);
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]);
430 [ExpectedException (typeof (ArgumentException))]
431 public void Shear_InvalidOrder ()
433 new Matrix ().Shear (-1, 1, (MatrixOrder) Int32.MinValue);
437 public void TransformPoints ()
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);
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);
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);
457 [ExpectedException (typeof (ArgumentNullException))]
458 public void TransformPoints_Point_Null ()
460 new Matrix ().TransformPoints ((Point[]) null);
464 [ExpectedException (typeof (ArgumentNullException))]
465 public void TransformPoints_PointF_Null ()
467 new Matrix ().TransformPoints ((PointF[]) null);
471 [ExpectedException (typeof (ArgumentException))]
472 public void TransformPoints_Point_Empty ()
474 new Matrix ().TransformPoints (new Point[0]);
478 [ExpectedException (typeof (ArgumentException))]
479 public void TransformPoints_PointF_Empty ()
481 new Matrix ().TransformPoints (new PointF[0]);
485 public void TransformVectors ()
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);
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);
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);
505 [ExpectedException (typeof (ArgumentNullException))]
506 public void TransformVectors_Point_Null ()
508 new Matrix ().TransformVectors ((Point[]) null);
512 [ExpectedException (typeof (ArgumentNullException))]
513 public void TransformVectors_PointF_Null ()
515 new Matrix ().TransformVectors ((PointF[]) null);
519 [ExpectedException (typeof (ArgumentException))]
520 public void TransformVectors_Point_Empty ()
522 new Matrix ().TransformVectors (new Point[0]);
526 [ExpectedException (typeof (ArgumentException))]
527 public void TransformVectors_PointF_Empty ()
529 new Matrix ().TransformVectors (new PointF[0]);
533 public void Translate ()
535 Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
536 matrix.Translate (5, 10);
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]);
547 [ExpectedException (typeof (ArgumentException))]
548 public void Translate_InvalidOrder ()
550 new Matrix ().Translate (-1, 1, (MatrixOrder) Int32.MinValue);
554 [ExpectedException (typeof (ArgumentNullException))]
555 public void VectorTransformPoints_Null ()
557 new Matrix ().VectorTransformPoints ((Point[]) null);
561 [ExpectedException (typeof (ArgumentException))]
562 public void VectorTransformPoints_Empty ()
564 new Matrix ().VectorTransformPoints (new Point[0]);