1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
23 // Chris Toshok (toshok@ximian.com)
28 using System.Windows.Media;
29 using NUnit.Framework;
31 namespace MonoTests.System.Windows.Media {
34 [Category ("NotWorking")]
35 public class MatrixTest {
37 void CheckMatrix (Matrix expected, Matrix actual)
39 Assert.AreEqual (expected.M11, actual.M11);
40 Assert.AreEqual (expected.M12, actual.M12);
41 Assert.AreEqual (expected.M21, actual.M21);
42 Assert.AreEqual (expected.M22, actual.M22);
43 Assert.AreEqual (expected.OffsetX, actual.OffsetX);
44 Assert.AreEqual (expected.OffsetY, actual.OffsetY);
48 public void TestAccessors ()
50 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
51 Assert.AreEqual (1, m.M11);
52 Assert.AreEqual (2, m.M12);
53 Assert.AreEqual (3, m.M21);
54 Assert.AreEqual (4, m.M22);
55 Assert.AreEqual (5, m.OffsetX);
56 Assert.AreEqual (6, m.OffsetY);
62 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
64 CheckMatrix (new Matrix (7, 10, 15, 22, 28, 40), m);
70 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
71 Assert.IsTrue (m.Equals (new Matrix (1, 2, 3, 4, 5, 6)));
72 Assert.IsFalse (m.Equals (new Matrix (0, 2, 3, 4, 5, 6)));
73 Assert.IsFalse (m.Equals (new Matrix (1, 0, 3, 4, 5, 6)));
74 Assert.IsFalse (m.Equals (new Matrix (1, 2, 0, 4, 5, 6)));
75 Assert.IsFalse (m.Equals (new Matrix (1, 2, 3, 0, 5, 6)));
76 Assert.IsFalse (m.Equals (new Matrix (1, 2, 3, 4, 0, 6)));
77 Assert.IsFalse (m.Equals (new Matrix (1, 2, 3, 4, 5, 0)));
79 Assert.IsFalse (m.Equals (0));
80 Assert.IsTrue (m.Equals ((object)m));
84 public void Determinant ()
86 Assert.AreEqual (0, (new Matrix (2, 2, 2, 2, 0, 0)).Determinant);
87 Assert.AreEqual (-6, (new Matrix (1, 4, 2, 2, 0, 0)).Determinant);
88 Assert.AreEqual (1, (new Matrix (1, 0, 0, 1, 0, 0)).Determinant);
89 Assert.AreEqual (1, (new Matrix (1, 0, 0, 1, 5, 5)).Determinant);
90 Assert.AreEqual (-1, (new Matrix (0, 1, 1, 0, 5, 5)).Determinant);
94 public void HasInverse ()
96 /* same matrices as in Determinant() */
97 Assert.IsFalse ((new Matrix (2, 2, 2, 2, 0, 0)).HasInverse);
98 Assert.IsTrue ((new Matrix (1, 4, 2, 2, 0, 0)).HasInverse);
99 Assert.IsTrue ((new Matrix (1, 0, 0, 1, 0, 0)).HasInverse);
100 Assert.IsTrue ((new Matrix (1, 0, 0, 1, 5, 5)).HasInverse);
101 Assert.IsTrue ((new Matrix (0, 1, 1, 0, 5, 5)).HasInverse);
105 public void IsIdentity ()
107 Assert.IsTrue (Matrix.Identity.IsIdentity);;
108 Assert.IsFalse ((new Matrix (1, 0, 0, 1, 5, 5)).IsIdentity);
109 Assert.IsFalse ((new Matrix (5, 5, 5, 5, 5, 5)).IsIdentity);
113 [ExpectedException (typeof (InvalidOperationException))] // "Transform is not invertible."
114 public void InvertException1 ()
116 Matrix m = new Matrix (2, 2, 2, 2, 0, 0);
121 public void Invert ()
125 m = new Matrix (1, 0, 0, 1, 0, 0);
127 CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), m);
129 m = new Matrix (1, 0, 0, 1, 5, 5);
131 CheckMatrix (new Matrix (1, 0, 0, 1, -5, -5), m);
133 m = new Matrix (1, 0, 0, 2, 5, 5);
135 CheckMatrix (new Matrix (1, 0, 0, 0.5, -5, -2.5), m);
137 m = new Matrix (0, 2, 4, 0, 5, 5);
139 CheckMatrix (new Matrix (0, 0.25, 0.5, 0, -2.5, -1.25), m);
143 public void Identity ()
145 CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), Matrix.Identity);
149 public void Multiply ()
151 CheckMatrix (new Matrix (5, 0, 0, 5, 10, 10),
152 Matrix.Multiply (new Matrix (1, 0, 0, 1, 2, 2),
153 new Matrix (5, 0, 0, 5, 0, 0)));
155 CheckMatrix (new Matrix (0, 0, 0, 0, 10, 10),
156 Matrix.Multiply (new Matrix (1, 0, 0, 1, 0, 0),
157 new Matrix (0, 0, 0, 0, 10, 10)));
163 CheckMatrix (Matrix.Identity, Matrix.Parse ("Identity"));
164 CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), Matrix.Parse ("1, 0, 0, 1, 0, 0"));
165 CheckMatrix (new Matrix (0.1, 0.2, 0.3, 0.4, 0.5, 0.6), Matrix.Parse ("0.1,0.2,0.3,0.4,0.5,0.6"));
166 // XXX what about locales where . and , are switched?
170 public void Prepend ()
172 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
173 m.Prepend (new Matrix (2, 4, 6, 8, 10, 12));
175 CheckMatrix (new Matrix (14, 20, 30, 44, 51, 74), m);
179 public void Rotate ()
181 Matrix m = Matrix.Identity;
183 CheckMatrix (new Matrix (0.707106781186548,
186 0.707106781186548, 0, 0), m);
188 m = new Matrix (1, 2, 3, 4, 5, 6);
190 CheckMatrix (new Matrix (-0.25060750208463,
195 7.75521858274768), m);
199 public void RotateAt ()
201 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
202 m.RotateAt (33, 5, 5);
204 CheckMatrix (new Matrix (-0.25060750208463,
209 5.83867056794542), m);
213 public void RotateAtPrepend ()
215 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
216 m.RotateAtPrepend (33, 5, 5);
218 CheckMatrix (new Matrix (2.47258767299051,
223 5.39349261148701), m);
227 public void RotatePrepend ()
229 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
230 m.RotatePrepend (33);
232 CheckMatrix (new Matrix (2.47258767299051,
242 Matrix m = Matrix.Identity;
245 CheckMatrix (new Matrix (5, 0, 0, 6, 0, 0), m);
247 m = new Matrix (1, 2, 2, 1, 3, 3);
249 CheckMatrix (new Matrix (5, 10, 10, 5, 15, 15), m);
253 public void ScaleAt ()
255 Matrix m = new Matrix (1, 0, 0, 1, 2, 2);
256 m.ScaleAt (2, 2, 0, 0);
257 CheckMatrix (new Matrix (2, 0, 0, 2, 4, 4), m);
259 m = new Matrix (1, 0, 0, 1, 2, 2);
260 m.ScaleAt (2, 2, 4, 4);
261 CheckMatrix (new Matrix (2, 0, 0, 2, 0, 0), m);
263 m = new Matrix (1, 0, 0, 1, 2, 2);
264 m.ScaleAt (2, 2, 2, 2);
265 CheckMatrix (new Matrix (2, 0, 0, 2, 2, 2), m);
269 public void ScaleAtPrepend ()
271 Matrix m = new Matrix (1, 0, 0, 1, 2, 2);
272 m.ScaleAtPrepend (2, 2, 0, 0);
273 CheckMatrix (new Matrix (2, 0, 0, 2, 2, 2), m);
275 m = new Matrix (1, 0, 0, 1, 2, 2);
276 m.ScaleAtPrepend (2, 2, 4, 4);
277 CheckMatrix (new Matrix (2, 0, 0, 2, -2, -2), m);
279 m = new Matrix (1, 0, 0, 1, 2, 2);
280 m.ScaleAtPrepend (2, 2, 2, 2);
281 CheckMatrix (new Matrix (2, 0, 0, 2, 0, 0), m);
285 public void ScalePrepend ()
287 Matrix m = Matrix.Identity;
289 m.ScalePrepend (5, 6);
290 CheckMatrix (new Matrix (5, 0, 0, 6, 0, 0), m);
292 m = new Matrix (1, 2, 2, 1, 3, 3);
293 m.ScalePrepend (5, 5);
294 CheckMatrix (new Matrix (5, 10, 10, 5, 3, 3), m);
298 public void SetIdentity ()
300 Matrix m = new Matrix (5, 5, 5, 5, 5, 5);
302 CheckMatrix (Matrix.Identity, m);
308 Matrix m = Matrix.Identity;
311 CheckMatrix (new Matrix (1,
316 m = new Matrix (1, 2, 2, 1, 3, 3);
318 CheckMatrix (new Matrix (1.35265396141693,
323 3.80384757729337), m);
327 public void SkewPrepend ()
329 Matrix m = Matrix.Identity;
331 m.SkewPrepend (10, 15);
332 CheckMatrix (new Matrix (1,
337 m = new Matrix (1, 2, 2, 1, 3, 3);
338 m.SkewPrepend (10, 15);
339 CheckMatrix (new Matrix (1.53589838486225,
347 public void ToStringTest ()
349 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
350 Assert.AreEqual ("1,2,3,4,5,6", m.ToString());
352 Assert.AreEqual ("Identity", m.ToString());
356 public void PointTransform ()
358 Matrix m = new Matrix (2, 0, 0, 2, 4, 4);
360 Point p = new Point (5, 6);
362 Assert.AreEqual (new Point (14, 16), m.Transform (p));
364 Point[] ps = new Point[10];
365 for (int i = 0; i < ps.Length; i ++)
366 ps[i] = new Point (3 * i, 2 * i);
370 for (int i = 0; i < ps.Length; i ++)
371 Assert.AreEqual (m.Transform (new Point (3 * i, 2 * i)), ps[i]);
375 public void VectorTransform ()
377 Matrix m = new Matrix (2, 0, 0, 2, 4, 4);
379 Vector p = new Vector (5, 6);
381 Assert.AreEqual (new Vector (10, 12), m.Transform (p));
383 Vector[] ps = new Vector[10];
384 for (int i = 0; i < ps.Length; i ++)
385 ps[i] = new Vector (3 * i, 2 * i);
389 for (int i = 0; i < ps.Length; i ++)
390 Assert.AreEqual (m.Transform (new Vector (3 * i, 2 * i)), ps[i]);
394 public void Translate ()
396 Matrix m = new Matrix (1, 0, 0, 1, 0, 0);
398 CheckMatrix (new Matrix (1, 0, 0, 1, 5, 5), m);
400 m = new Matrix (2, 0, 0, 2, 0, 0);
402 CheckMatrix (new Matrix (2, 0, 0, 2, 5, 5), m);
406 public void TranslatePrepend ()
408 Matrix m = new Matrix (1, 0, 0, 1, 0, 0);
409 m.TranslatePrepend (5, 5);
410 CheckMatrix (new Matrix (1, 0, 0, 1, 5, 5), m);
412 m = new Matrix (2, 0, 0, 2, 0, 0);
413 m.TranslatePrepend (5, 5);
414 CheckMatrix (new Matrix (2, 0, 0, 2, 10, 10), m);