Merge pull request #5560 from kumpera/wasm-work-p3
[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.Globalization;
28 using System.Windows;
29 using System.Windows.Media;
30 using NUnit.Framework;
31
32 namespace MonoTests.System.Windows.Media {
33
34         [TestFixture]
35         public class MatrixTest {
36                 const double DELTA = 0.000000001d;
37
38                 void CheckMatrix (Matrix expected, Matrix actual)
39                 {
40                         Assert.AreEqual (expected.M11, actual.M11, DELTA);
41                         Assert.AreEqual (expected.M12, actual.M12, DELTA);
42                         Assert.AreEqual (expected.M21, actual.M21, DELTA);
43                         Assert.AreEqual (expected.M22, actual.M22, DELTA);
44                         Assert.AreEqual (expected.OffsetX, actual.OffsetX, DELTA);
45                         Assert.AreEqual (expected.OffsetY, actual.OffsetY, DELTA);
46                 }
47
48                 [Test]
49                 public void TestAccessors ()
50                 {
51                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
52                         Assert.AreEqual (1, m.M11);
53                         Assert.AreEqual (2, m.M12);
54                         Assert.AreEqual (3, m.M21);
55                         Assert.AreEqual (4, m.M22);
56                         Assert.AreEqual (5, m.OffsetX);
57                         Assert.AreEqual (6, m.OffsetY);
58                 }
59
60                 [Test]
61                 public void Append ()
62                 {
63                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
64                         m.Append (m);
65                         CheckMatrix (new Matrix (7, 10, 15, 22, 28, 40), m);
66                 }
67
68                 [Test]
69                 public void Equals ()
70                 {
71                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
72                         Assert.IsTrue  (m.Equals (new Matrix (1, 2, 3, 4, 5, 6)));
73                         Assert.IsFalse (m.Equals (new Matrix (0, 2, 3, 4, 5, 6)));
74                         Assert.IsFalse (m.Equals (new Matrix (1, 0, 3, 4, 5, 6)));
75                         Assert.IsFalse (m.Equals (new Matrix (1, 2, 0, 4, 5, 6)));
76                         Assert.IsFalse (m.Equals (new Matrix (1, 2, 3, 0, 5, 6)));
77                         Assert.IsFalse (m.Equals (new Matrix (1, 2, 3, 4, 0, 6)));
78                         Assert.IsFalse (m.Equals (new Matrix (1, 2, 3, 4, 5, 0)));
79
80                         Assert.IsFalse (m.Equals (0));
81                         Assert.IsTrue (m.Equals ((object)m));
82                 }
83
84                 [Test]
85                 public void Determinant ()
86                 {
87                         Assert.AreEqual (0, (new Matrix (2, 2, 2, 2, 0, 0)).Determinant);
88                         Assert.AreEqual (-6, (new Matrix (1, 4, 2, 2, 0, 0)).Determinant);
89                         Assert.AreEqual (1, (new Matrix (1, 0, 0, 1, 0, 0)).Determinant);
90                         Assert.AreEqual (1, (new Matrix (1, 0, 0, 1, 5, 5)).Determinant);
91                         Assert.AreEqual (-1, (new Matrix (0, 1, 1, 0, 5, 5)).Determinant);
92                 }
93
94                 [Test]
95                 public void HasInverse ()
96                 {
97                         /* same matrices as in Determinant() */
98                         Assert.IsFalse ((new Matrix (2, 2, 2, 2, 0, 0)).HasInverse);
99                         Assert.IsTrue ((new Matrix (1, 4, 2, 2, 0, 0)).HasInverse);
100                         Assert.IsTrue  ((new Matrix (1, 0, 0, 1, 0, 0)).HasInverse);
101                         Assert.IsTrue  ((new Matrix (1, 0, 0, 1, 5, 5)).HasInverse);
102                         Assert.IsTrue  ((new Matrix (0, 1, 1, 0, 5, 5)).HasInverse);
103                 }
104
105                 [Test]
106                 public void IsIdentity ()
107                 {
108                         Assert.IsTrue (Matrix.Identity.IsIdentity);;
109                         Assert.IsFalse ((new Matrix (1, 0, 0, 1, 5, 5)).IsIdentity);
110                         Assert.IsFalse ((new Matrix (5, 5, 5, 5, 5, 5)).IsIdentity);
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (InvalidOperationException))] // "Transform is not invertible."
115                 public void InvertException1 ()
116                 {
117                         Matrix m = new Matrix (2, 2, 2, 2, 0, 0);
118                         m.Invert ();
119                 }
120
121                 [Test]
122                 public void Invert ()
123                 {
124                         Matrix m;
125
126                         m = new Matrix (1, 0, 0, 1, 0, 0);
127                         m.Invert ();
128                         CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), m);
129
130                         m = new Matrix (1, 0, 0, 1, 5, 5);
131                         m.Invert ();
132                         CheckMatrix (new Matrix (1, 0, 0, 1, -5, -5), m);
133
134                         m = new Matrix (1, 0, 0, 2, 5, 5);
135                         m.Invert ();
136                         CheckMatrix (new Matrix (1, 0, 0, 0.5, -5, -2.5), m);
137
138                         m = new Matrix (0, 2, 4, 0, 5, 5);
139                         m.Invert ();
140                         CheckMatrix (new Matrix (0, 0.25, 0.5, 0, -2.5, -1.25), m);
141                 }
142
143                 [Test]
144                 public void Identity ()
145                 {
146                         CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), Matrix.Identity);
147                 }
148
149                 [Test]
150                 public void Multiply ()
151                 {
152                         CheckMatrix (new Matrix (5, 0, 0, 5, 10, 10),
153                                      Matrix.Multiply (new Matrix (1, 0, 0, 1, 2, 2),
154                                                       new Matrix (5, 0, 0, 5, 0, 0)));
155
156                         CheckMatrix (new Matrix (0, 0, 0, 0, 10, 10),
157                                      Matrix.Multiply (new Matrix (1, 0, 0, 1, 0, 0),
158                                                       new Matrix (0, 0, 0, 0, 10, 10)));
159                 }
160
161                 [Test]
162                 public void Parse ()
163                 {
164                         CheckMatrix (Matrix.Identity, Matrix.Parse ("Identity"));
165                         CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), Matrix.Parse ("1, 0, 0, 1, 0, 0"));
166                         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"));
167                         // XXX what about locales where . and , are switched?
168                 }
169
170                 [Test]
171                 public void Prepend ()
172                 {
173                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
174                         m.Prepend (new Matrix (2, 4, 6, 8, 10, 12));
175
176                         CheckMatrix (new Matrix (14, 20, 30, 44, 51, 74), m);
177                 }
178
179                 [Test]
180                 public void Rotate ()
181                 {
182                         Matrix m = Matrix.Identity;
183                         m.Rotate (45);
184                         CheckMatrix (new Matrix (0.707106781186548,
185                                                  0.707106781186547,
186                                                  -0.707106781186547,
187                                                  0.707106781186548, 0, 0), m);
188
189                         m = new Matrix (1, 2, 3, 4, 5, 6);
190                         m.Rotate (33);
191                         CheckMatrix (new Matrix (-0.25060750208463,
192                                                  2.22198017090588,
193                                                  0.337455563776164,
194                                                  4.98859937682678,
195                                                  0.925518629636958,
196                                                  7.75521858274768), m);
197                 }
198
199                 [Test]
200                 public void RotateAt ()
201                 {
202                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
203                         m.RotateAt (33, 5, 5);
204
205                         CheckMatrix (new Matrix (-0.25060750208463,
206                                                  2.22198017090588,
207                                                  0.337455563776164,
208                                                  4.98859937682678,
209                                                  4.45536096498497,
210                                                  5.83867056794542), m);
211                 }
212
213                 [Test]
214                 public void RotateAtPrepend ()
215                 {
216                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
217                         m.RotateAtPrepend (33, 5, 5);
218
219                         CheckMatrix (new Matrix (2.47258767299051,
220                                                  3.85589727595096,
221                                                  1.97137266882125,
222                                                  2.26540420175164,
223                                                  2.78019829094125,
224                                                  5.39349261148701), m);
225                 }
226
227                 [Test]
228                 public void RotatePrepend ()
229                 {
230                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
231                         m.RotatePrepend (33);
232
233                         CheckMatrix (new Matrix (2.47258767299051,
234                                                  3.85589727595096,
235                                                  1.97137266882125,
236                                                  2.26540420175164,
237                                                  5, 6), m);
238                 }
239
240                 [Test]
241                 public void Scale ()
242                 {
243                         Matrix m = Matrix.Identity;
244
245                         m.Scale (5, 6);
246                         CheckMatrix (new Matrix (5, 0, 0, 6, 0, 0), m);
247
248                         m = new Matrix (1, 2, 2, 1, 3, 3);
249                         m.Scale (5, 5);
250                         CheckMatrix (new Matrix (5, 10, 10, 5, 15, 15), m);
251                 }
252
253                 [Test]
254                 public void ScaleAt ()
255                 {
256                         Matrix m = new Matrix (1, 0, 0, 1, 2, 2);
257                         m.ScaleAt (2, 2, 0, 0);
258                         CheckMatrix (new Matrix (2, 0, 0, 2, 4, 4), m);
259
260                         m = new Matrix (1, 0, 0, 1, 2, 2);
261                         m.ScaleAt (2, 2, 4, 4);
262                         CheckMatrix (new Matrix (2, 0, 0, 2, 0, 0), m);
263
264                         m = new Matrix (1, 0, 0, 1, 2, 2);
265                         m.ScaleAt (2, 2, 2, 2);
266                         CheckMatrix (new Matrix (2, 0, 0, 2, 2, 2), m);
267                 }
268
269                 [Test]
270                 public void ScaleAtPrepend ()
271                 {
272                         Matrix m = new Matrix (1, 0, 0, 1, 2, 2);
273                         m.ScaleAtPrepend (2, 2, 0, 0);
274                         CheckMatrix (new Matrix (2, 0, 0, 2, 2, 2), m);
275
276                         m = new Matrix (1, 0, 0, 1, 2, 2);
277                         m.ScaleAtPrepend (2, 2, 4, 4);
278                         CheckMatrix (new Matrix (2, 0, 0, 2, -2, -2), m);
279
280                         m = new Matrix (1, 0, 0, 1, 2, 2);
281                         m.ScaleAtPrepend (2, 2, 2, 2);
282                         CheckMatrix (new Matrix (2, 0, 0, 2, 0, 0), m);
283                 }
284
285                 [Test]
286                 public void ScalePrepend ()
287                 {
288                         Matrix m = Matrix.Identity;
289
290                         m.ScalePrepend (5, 6);
291                         CheckMatrix (new Matrix (5, 0, 0, 6, 0, 0), m);
292
293                         m = new Matrix (1, 2, 2, 1, 3, 3);
294                         m.ScalePrepend (5, 5);
295                         CheckMatrix (new Matrix (5, 10, 10, 5, 3, 3), m);
296                 }
297
298                 [Test]
299                 public void SetIdentity ()
300                 {
301                         Matrix m = new Matrix (5, 5, 5, 5, 5, 5);
302                         m.SetIdentity ();
303                         CheckMatrix (Matrix.Identity, m);
304                 }
305
306                 [Test]
307                 public void Skew ()
308                 {
309                         Matrix m = Matrix.Identity;
310
311                         m.Skew (10, 15);
312                         CheckMatrix (new Matrix (1,
313                                                  0.267949192431123,
314                                                  0.176326980708465,
315                                                  1, 0, 0), m);
316
317                         m = new Matrix (1, 2, 2, 1, 3, 3);
318                         m.Skew (10, 15);
319                         CheckMatrix (new Matrix (1.35265396141693,
320                                                  2.26794919243112,
321                                                  2.17632698070847,
322                                                  1.53589838486225,
323                                                  3.52898094212539,
324                                                  3.80384757729337), m);
325                 }
326
327                 [Test]
328                 public void SkewPrepend ()
329                 {
330                         Matrix m = Matrix.Identity;
331
332                         m.SkewPrepend (10, 15);
333                         CheckMatrix (new Matrix (1,
334                                                  0.267949192431123,
335                                                  0.176326980708465,
336                                                  1, 0, 0), m);
337
338                         m = new Matrix (1, 2, 2, 1, 3, 3);
339                         m.SkewPrepend (10, 15);
340                         CheckMatrix (new Matrix (1.53589838486225,
341                                                  2.26794919243112,
342                                                  2.17632698070847,
343                                                  1.35265396141693,
344                                                  3, 3), m);
345                 }
346
347                 [Test]
348                 public void ToStringTest ()
349                 {
350                         Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
351                         Assert.AreEqual ("1,2,3,4,5,6", m.ToString(CultureInfo.InvariantCulture));
352                         m = Matrix.Identity;
353                         Assert.AreEqual ("Identity", m.ToString());
354                 }
355
356                 [Test]
357                 public void PointTransform ()
358                 {
359                         Matrix m = new Matrix (2, 0, 0, 2, 4, 4);
360
361                         Point p = new Point (5, 6);
362
363                         Assert.AreEqual (new Point (14, 16), m.Transform (p));
364
365                         Point[] ps = new Point[10];
366                         for (int i = 0; i < ps.Length; i ++)
367                                 ps[i] = new Point (3 * i, 2 * i);
368
369                         m.Transform (ps);
370
371                         for (int i = 0; i < ps.Length; i ++)
372                                 Assert.AreEqual (m.Transform (new Point (3 * i, 2 * i)), ps[i]);
373                 }
374
375                 [Test]
376                 public void VectorTransform ()
377                 {
378                         Matrix m = new Matrix (2, 0, 0, 2, 4, 4);
379
380                         Vector p = new Vector (5, 6);
381
382                         Assert.AreEqual (new Vector (10, 12), m.Transform (p));
383
384                         Vector[] ps = new Vector[10];
385                         for (int i = 0; i < ps.Length; i ++)
386                                 ps[i] = new Vector (3 * i, 2 * i);
387
388                         m.Transform (ps);
389
390                         for (int i = 0; i < ps.Length; i ++)
391                                 Assert.AreEqual (m.Transform (new Vector (3 * i, 2 * i)), ps[i]);
392                 }
393
394                 [Test]
395                 public void Translate ()
396                 {
397                         Matrix m = new Matrix (1, 0, 0, 1, 0, 0);
398                         m.Translate (5, 5);
399                         CheckMatrix (new Matrix (1, 0, 0, 1, 5, 5), m);
400
401                         m = new Matrix (2, 0, 0, 2, 0, 0);
402                         m.Translate (5, 5);
403                         CheckMatrix (new Matrix (2, 0, 0, 2, 5, 5), m);
404                 }
405
406                 [Test]
407                 public void TranslatePrepend ()
408                 {
409                         Matrix m = new Matrix (1, 0, 0, 1, 0, 0);
410                         m.TranslatePrepend (5, 5);
411                         CheckMatrix (new Matrix (1, 0, 0, 1, 5, 5), m);
412
413                         m = new Matrix (2, 0, 0, 2, 0, 0);
414                         m.TranslatePrepend (5, 5);
415                         CheckMatrix (new Matrix (2, 0, 0, 2, 10, 10), m);
416                 }
417
418         }
419 }
420