Merge pull request #5355 from kumpera/fix_58379
[mono.git] / mcs / class / WindowsBase / Test / System.Windows.Media / MatrixTest.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Chris Toshok (toshok@ximian.com)
24 //
25
26 using System;
27 using System.Windows;
28 using System.Windows.Media;
29 using NUnit.Framework;
30
31 namespace MonoTests.System.Windows.Media {
32
33         [TestFixture]
34         public class MatrixTest {
35                 const double DELTA = 0.000000001d;
36
37                 void CheckMatrix (Matrix expected, Matrix actual)
38                 {
39                         Assert.AreEqual (expected.M11, actual.M11, DELTA);
40                         Assert.AreEqual (expected.M12, actual.M12, DELTA);
41                         Assert.AreEqual (expected.M21, actual.M21, DELTA);
42                         Assert.AreEqual (expected.M22, actual.M22, DELTA);
43                         Assert.AreEqual (expected.OffsetX, actual.OffsetX, DELTA);
44                         Assert.AreEqual (expected.OffsetY, actual.OffsetY, DELTA);
45                 }
46
47                 [Test]
48                 public void TestAccessors ()
49                 {
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);
57                 }
58
59                 [Test]
60                 public void Append ()
61                 {
62                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
63                         m.Append (m);
64                         CheckMatrix (new Matrix (7, 10, 15, 22, 28, 40), m);
65                 }
66
67                 [Test]
68                 public void Equals ()
69                 {
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)));
78
79                         Assert.IsFalse (m.Equals (0));
80                         Assert.IsTrue (m.Equals ((object)m));
81                 }
82
83                 [Test]
84                 public void Determinant ()
85                 {
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);
91                 }
92
93                 [Test]
94                 public void HasInverse ()
95                 {
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);
102                 }
103
104                 [Test]
105                 public void IsIdentity ()
106                 {
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);
110                 }
111
112                 [Test]
113                 [ExpectedException (typeof (InvalidOperationException))] // "Transform is not invertible."
114                 public void InvertException1 ()
115                 {
116                         Matrix m = new Matrix (2, 2, 2, 2, 0, 0);
117                         m.Invert ();
118                 }
119
120                 [Test]
121                 public void Invert ()
122                 {
123                         Matrix m;
124
125                         m = new Matrix (1, 0, 0, 1, 0, 0);
126                         m.Invert ();
127                         CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), m);
128
129                         m = new Matrix (1, 0, 0, 1, 5, 5);
130                         m.Invert ();
131                         CheckMatrix (new Matrix (1, 0, 0, 1, -5, -5), m);
132
133                         m = new Matrix (1, 0, 0, 2, 5, 5);
134                         m.Invert ();
135                         CheckMatrix (new Matrix (1, 0, 0, 0.5, -5, -2.5), m);
136
137                         m = new Matrix (0, 2, 4, 0, 5, 5);
138                         m.Invert ();
139                         CheckMatrix (new Matrix (0, 0.25, 0.5, 0, -2.5, -1.25), m);
140                 }
141
142                 [Test]
143                 public void Identity ()
144                 {
145                         CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), Matrix.Identity);
146                 }
147
148                 [Test]
149                 public void Multiply ()
150                 {
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)));
154
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)));
158                 }
159
160                 [Test]
161                 public void Parse ()
162                 {
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?
167                 }
168
169                 [Test]
170                 public void Prepend ()
171                 {
172                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
173                         m.Prepend (new Matrix (2, 4, 6, 8, 10, 12));
174
175                         CheckMatrix (new Matrix (14, 20, 30, 44, 51, 74), m);
176                 }
177
178                 [Test]
179                 public void Rotate ()
180                 {
181                         Matrix m = Matrix.Identity;
182                         m.Rotate (45);
183                         CheckMatrix (new Matrix (0.707106781186548,
184                                                  0.707106781186547,
185                                                  -0.707106781186547,
186                                                  0.707106781186548, 0, 0), m);
187
188                         m = new Matrix (1, 2, 3, 4, 5, 6);
189                         m.Rotate (33);
190                         CheckMatrix (new Matrix (-0.25060750208463,
191                                                  2.22198017090588,
192                                                  0.337455563776164,
193                                                  4.98859937682678,
194                                                  0.925518629636958,
195                                                  7.75521858274768), m);
196                 }
197
198                 [Test]
199                 public void RotateAt ()
200                 {
201                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
202                         m.RotateAt (33, 5, 5);
203
204                         CheckMatrix (new Matrix (-0.25060750208463,
205                                                  2.22198017090588,
206                                                  0.337455563776164,
207                                                  4.98859937682678,
208                                                  4.45536096498497,
209                                                  5.83867056794542), m);
210                 }
211
212                 [Test]
213                 public void RotateAtPrepend ()
214                 {
215                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
216                         m.RotateAtPrepend (33, 5, 5);
217
218                         CheckMatrix (new Matrix (2.47258767299051,
219                                                  3.85589727595096,
220                                                  1.97137266882125,
221                                                  2.26540420175164,
222                                                  2.78019829094125,
223                                                  5.39349261148701), m);
224                 }
225
226                 [Test]
227                 public void RotatePrepend ()
228                 {
229                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
230                         m.RotatePrepend (33);
231
232                         CheckMatrix (new Matrix (2.47258767299051,
233                                                  3.85589727595096,
234                                                  1.97137266882125,
235                                                  2.26540420175164,
236                                                  5, 6), m);
237                 }
238
239                 [Test]
240                 public void Scale ()
241                 {
242                         Matrix m = Matrix.Identity;
243
244                         m.Scale (5, 6);
245                         CheckMatrix (new Matrix (5, 0, 0, 6, 0, 0), m);
246
247                         m = new Matrix (1, 2, 2, 1, 3, 3);
248                         m.Scale (5, 5);
249                         CheckMatrix (new Matrix (5, 10, 10, 5, 15, 15), m);
250                 }
251
252                 [Test]
253                 public void ScaleAt ()
254                 {
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);
258
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);
262
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);
266                 }
267
268                 [Test]
269                 public void ScaleAtPrepend ()
270                 {
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);
274
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);
278
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);
282                 }
283
284                 [Test]
285                 public void ScalePrepend ()
286                 {
287                         Matrix m = Matrix.Identity;
288
289                         m.ScalePrepend (5, 6);
290                         CheckMatrix (new Matrix (5, 0, 0, 6, 0, 0), m);
291
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);
295                 }
296
297                 [Test]
298                 public void SetIdentity ()
299                 {
300                         Matrix m = new Matrix (5, 5, 5, 5, 5, 5);
301                         m.SetIdentity ();
302                         CheckMatrix (Matrix.Identity, m);
303                 }
304
305                 [Test]
306                 public void Skew ()
307                 {
308                         Matrix m = Matrix.Identity;
309
310                         m.Skew (10, 15);
311                         CheckMatrix (new Matrix (1,
312                                                  0.267949192431123,
313                                                  0.176326980708465,
314                                                  1, 0, 0), m);
315
316                         m = new Matrix (1, 2, 2, 1, 3, 3);
317                         m.Skew (10, 15);
318                         CheckMatrix (new Matrix (1.35265396141693,
319                                                  2.26794919243112,
320                                                  2.17632698070847,
321                                                  1.53589838486225,
322                                                  3.52898094212539,
323                                                  3.80384757729337), m);
324                 }
325
326                 [Test]
327                 public void SkewPrepend ()
328                 {
329                         Matrix m = Matrix.Identity;
330
331                         m.SkewPrepend (10, 15);
332                         CheckMatrix (new Matrix (1,
333                                                  0.267949192431123,
334                                                  0.176326980708465,
335                                                  1, 0, 0), m);
336
337                         m = new Matrix (1, 2, 2, 1, 3, 3);
338                         m.SkewPrepend (10, 15);
339                         CheckMatrix (new Matrix (1.53589838486225,
340                                                  2.26794919243112,
341                                                  2.17632698070847,
342                                                  1.35265396141693,
343                                                  3, 3), m);
344                 }
345
346                 [Test]
347                 public void ToStringTest ()
348                 {
349                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
350                         Assert.AreEqual ("1,2,3,4,5,6", m.ToString());
351                         m = Matrix.Identity;
352                         Assert.AreEqual ("Identity", m.ToString());
353                 }
354
355                 [Test]
356                 public void PointTransform ()
357                 {
358                         Matrix m = new Matrix (2, 0, 0, 2, 4, 4);
359
360                         Point p = new Point (5, 6);
361
362                         Assert.AreEqual (new Point (14, 16), m.Transform (p));
363
364                         Point[] ps = new Point[10];
365                         for (int i = 0; i < ps.Length; i ++)
366                                 ps[i] = new Point (3 * i, 2 * i);
367
368                         m.Transform (ps);
369
370                         for (int i = 0; i < ps.Length; i ++)
371                                 Assert.AreEqual (m.Transform (new Point (3 * i, 2 * i)), ps[i]);
372                 }
373
374                 [Test]
375                 public void VectorTransform ()
376                 {
377                         Matrix m = new Matrix (2, 0, 0, 2, 4, 4);
378
379                         Vector p = new Vector (5, 6);
380
381                         Assert.AreEqual (new Vector (10, 12), m.Transform (p));
382
383                         Vector[] ps = new Vector[10];
384                         for (int i = 0; i < ps.Length; i ++)
385                                 ps[i] = new Vector (3 * i, 2 * i);
386
387                         m.Transform (ps);
388
389                         for (int i = 0; i < ps.Length; i ++)
390                                 Assert.AreEqual (m.Transform (new Vector (3 * i, 2 * i)), ps[i]);
391                 }
392
393                 [Test]
394                 public void Translate ()
395                 {
396                         Matrix m = new Matrix (1, 0, 0, 1, 0, 0);
397                         m.Translate (5, 5);
398                         CheckMatrix (new Matrix (1, 0, 0, 1, 5, 5), m);
399
400                         m = new Matrix (2, 0, 0, 2, 0, 0);
401                         m.Translate (5, 5);
402                         CheckMatrix (new Matrix (2, 0, 0, 2, 5, 5), m);
403                 }
404
405                 [Test]
406                 public void TranslatePrepend ()
407                 {
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);
411
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);
415                 }
416
417         }
418 }
419