2005-10-04 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Drawing2D / TestMatrix.cs
1 //
2 // Tests for System.Drawing.Drawing2D.Matrix.cs
3 //
4 // Author:
5 //  Jordi Mas i Hernandez <jordi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30 using System;
31 using System.Drawing;
32 using System.Drawing.Drawing2D;
33 using System.Security.Permissions;
34
35 namespace MonoTests.System.Drawing.Drawing2D
36 {
37         [TestFixture]
38         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
39         public class MatrixTest : Assertion
40         {
41                 [TearDown]
42                 public void TearDown () { }
43
44                 [SetUp]
45                 public void SetUp () { }
46
47                 [Test]
48                 public void Constructors ()
49                 {
50                         {
51                                 Matrix matrix = new Matrix ();
52                                 AssertEquals ("C#1", 6, matrix.Elements.Length);
53                         }
54
55                         {
56
57                                 Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
58                                 AssertEquals ("C#2", 6, matrix.Elements.Length);
59                                 AssertEquals ("C#3", 10, matrix.Elements[0]);
60                                 AssertEquals ("C#4", 20, matrix.Elements[1]);
61                                 AssertEquals ("C#5", 30, matrix.Elements[2]);
62                                 AssertEquals ("C#6", 40, matrix.Elements[3]);
63                                 AssertEquals ("C#7", 50, matrix.Elements[4]);
64                                 AssertEquals ("C#8", 60, matrix.Elements[5]);
65                         }
66                 }
67
68                 // Properties
69
70                 [Test]
71                 public void Invertible ()
72                 {
73                         Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
74                         AssertEquals ("I#1", false, matrix.IsInvertible);
75
76                         matrix = new Matrix (156, 46, 0, 0, 106, 19);
77                         AssertEquals ("I#2", false, matrix.IsInvertible);
78
79                         matrix = new Matrix (146, 66, 158, 104, 42, 150);
80                         AssertEquals ("I#3", true, matrix.IsInvertible);
81
82                         matrix = new Matrix (119, 140, 145, 74, 102, 58);
83                         AssertEquals ("I#4", true, matrix.IsInvertible);
84                 }
85                 
86                 [Test]
87                 public void IsIdentity ()
88                 {
89                         Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
90                         AssertEquals ("N#1", false, matrix.IsIdentity);
91                         
92                         matrix = new Matrix (1, 0, 0, 1, 0, 0);
93                         AssertEquals ("N#2", true, matrix.IsIdentity);                  
94                 }
95                 
96                 [Test]
97                 public void IsOffsetX ()
98                 {
99                         Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
100                         AssertEquals ("X#1", 47, matrix.OffsetX);                       
101                 }
102                 
103                 [Test]
104                 public void IsOffsetY ()
105                 {
106                         Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
107                         AssertEquals ("Y#1", 30, matrix.OffsetY);                       
108                 }
109                 
110                 // Elements Property is checked implicity in other test
111
112                 //
113                 // Methods
114                 //
115                 
116
117                 [Test]
118                 public void Clone ()
119                 {
120                         Matrix matsrc = new Matrix (10, 20, 30, 40, 50, 60);
121                         Matrix matrix  = matsrc.Clone ();
122
123                         AssertEquals ("D#1", 6, matrix.Elements.Length);
124                         AssertEquals ("D#2", 10, matrix.Elements[0]);
125                         AssertEquals ("D#3", 20, matrix.Elements[1]);
126                         AssertEquals ("D#4", 30, matrix.Elements[2]);
127                         AssertEquals ("D#5", 40, matrix.Elements[3]);
128                         AssertEquals ("D#6", 50, matrix.Elements[4]);
129                         AssertEquals ("D#7", 60, matrix.Elements[5]);
130                 }
131
132                 [Test]
133                 public void Reset ()
134                 {
135                         Matrix matrix = new Matrix (51, 52, 53, 54, 55, 56);
136                         matrix.Reset ();
137
138                         AssertEquals ("F#1", 6, matrix.Elements.Length);
139                         AssertEquals ("F#2", 1, matrix.Elements[0]);
140                         AssertEquals ("F#3", 0, matrix.Elements[1]);
141                         AssertEquals ("F#4", 0, matrix.Elements[2]);
142                         AssertEquals ("F#5", 1, matrix.Elements[3]);
143                         AssertEquals ("F#6", 0, matrix.Elements[4]);
144                         AssertEquals ("F#7", 0, matrix.Elements[5]);
145                 }
146
147                 [Test]
148                 public void Rotate ()
149                 {
150                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
151                         matrix.Rotate (180);
152
153                         AssertEquals ("H#1", -10, matrix.Elements[0]);
154                         AssertEquals ("H#2", -20, matrix.Elements[1]);
155                         AssertEquals ("H#3", -30, matrix.Elements[2]);
156                         AssertEquals ("H#4", -40, matrix.Elements[3]);
157                         AssertEquals ("H#5", 50, matrix.Elements[4]);
158                         AssertEquals ("H#6", 60, matrix.Elements[5]);
159                 }
160
161                 [Test]
162                 public void RotateAt ()
163                 {
164                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
165                         matrix.RotateAt (180, new PointF (10, 10));
166
167                         AssertEquals ("I#1", -10, matrix.Elements[0]);
168                         AssertEquals ("I#2", -20, matrix.Elements[1]);
169                         AssertEquals ("I#3", -30, matrix.Elements[2]);
170                         AssertEquals ("I#4", -40, matrix.Elements[3]);
171                         AssertEquals ("I#5", 850, matrix.Elements[4]);
172                         AssertEquals ("I#6", 1260, matrix.Elements[5]);
173                 }
174
175                 [Test]
176                 public void Multiply ()
177                 {
178                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
179                         matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60));
180
181                         AssertEquals ("J#1", 700, matrix.Elements[0]);
182                         AssertEquals ("J#2", 1000, matrix.Elements[1]);
183                         AssertEquals ("J#3", 1500, matrix.Elements[2]);
184                         AssertEquals ("J#4", 2200, matrix.Elements[3]);
185                         AssertEquals ("J#5", 2350, matrix.Elements[4]);
186                         AssertEquals ("J#6", 3460, matrix.Elements[5]);
187                 }
188
189                 [Test]
190                 public void Equals ()
191                 {
192                         Matrix mat1 = new Matrix (10, 20, 30, 40, 50, 60);
193                         Matrix mat2 = new Matrix (10, 20, 30, 40, 50, 60);
194                         Matrix mat3 = new Matrix (10, 20, 30, 40, 50, 10);
195
196                         AssertEquals ("E#1", true, mat1.Equals (mat2));
197                         AssertEquals ("E#2", false, mat2.Equals (mat3));
198                         AssertEquals ("E#3", false, mat1.Equals (mat3));
199                 }
200                 
201                 [Test]
202                 public void Invert ()
203                 {
204                         Matrix matrix = new Matrix (1, 2, 3, 4, 5, 6);
205                         matrix.Invert ();
206                         
207                         AssertEquals ("V#1", -2, matrix.Elements[0]);
208                         AssertEquals ("V#2", 1, matrix.Elements[1]);
209                         AssertEquals ("V#3", 1.5, matrix.Elements[2]);
210                         AssertEquals ("V#4", -0.5, matrix.Elements[3]);
211                         AssertEquals ("V#5", 1, matrix.Elements[4]);
212                         AssertEquals ("V#6", -2, matrix.Elements[5]);                   
213                 }
214                 
215                 [Test]
216                 public void Scale ()
217                 {
218                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
219                         matrix.Scale (2, 4);
220
221                         AssertEquals ("S#1", 20, matrix.Elements[0]);
222                         AssertEquals ("S#2", 40, matrix.Elements[1]);
223                         AssertEquals ("S#3", 120, matrix.Elements[2]);
224                         AssertEquals ("S#4", 160, matrix.Elements[3]);
225                         AssertEquals ("S#5", 50, matrix.Elements[4]);
226                         AssertEquals ("S#6", 60, matrix.Elements[5]);                   
227                 }
228                 
229                 [Test]
230                 public void Shear ()
231                 {
232                         Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
233                         matrix.Shear (2, 4);
234
235                         AssertEquals ("H#1", 130, matrix.Elements[0]);
236                         AssertEquals ("H#2", 180, matrix.Elements[1]);
237                         AssertEquals ("H#3", 50, matrix.Elements[2]);
238                         AssertEquals ("H#4", 80, matrix.Elements[3]);
239                         AssertEquals ("H#5", 50, matrix.Elements[4]);
240                         AssertEquals ("H#6", 60, matrix.Elements[5]);
241                         
242                         matrix = new Matrix (5, 3, 9, 2, 2, 1);
243                         matrix.Shear  (10, 20);                 
244                         
245                         AssertEquals ("H#7", 185, matrix.Elements[0]);
246                         AssertEquals ("H#8", 43, matrix.Elements[1]);
247                         AssertEquals ("H#9", 59, matrix.Elements[2]);
248                         AssertEquals ("H#10", 32, matrix.Elements[3]);
249                         AssertEquals ("H#11", 2, matrix.Elements[4]);
250                         AssertEquals ("H#12", 1, matrix.Elements[5]);                       
251                 }
252                 
253                 [Test]
254                 public void TransformPoints ()
255                 {
256                         Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
257                         PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
258                         matrix.TransformPoints (pointsF);
259                                                 
260                         AssertEquals ("K#1", 38, pointsF[0].X);
261                         AssertEquals ("K#2", 52, pointsF[0].Y);
262                         AssertEquals ("K#3", 66, pointsF[1].X);
263                         AssertEquals ("K#4", 92, pointsF[1].Y);
264                         
265                         Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
266                         matrix.TransformPoints (points);
267                         AssertEquals ("K#5", 38, pointsF[0].X);
268                         AssertEquals ("K#6", 52, pointsF[0].Y);
269                         AssertEquals ("K#7", 66, pointsF[1].X);
270                         AssertEquals ("K#8", 92, pointsF[1].Y);                                             
271                 }
272                 
273                 [Test]
274                 public void TransformVectors  ()
275                 {
276                         Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
277                         PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
278                         matrix.TransformVectors (pointsF);
279                                                 
280                         AssertEquals ("N#1", 28, pointsF[0].X);
281                         AssertEquals ("N#2", 40, pointsF[0].Y);
282                         AssertEquals ("N#3", 56, pointsF[1].X);
283                         AssertEquals ("N#4", 80, pointsF[1].Y);
284                         
285                         Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
286                         matrix.TransformVectors (points);
287                         AssertEquals ("N#5", 28, pointsF[0].X);
288                         AssertEquals ("N#6", 40, pointsF[0].Y);
289                         AssertEquals ("N#7", 56, pointsF[1].X);
290                         AssertEquals ("N#8", 80, pointsF[1].Y);                                             
291                 }               
292                 
293                 [Test]
294                 public void Translate  ()
295                 {
296                         Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);                        
297                         matrix.Translate (5, 10);
298                                                 
299                         AssertEquals ("Y#1", 2, matrix.Elements[0]);
300                         AssertEquals ("Y#2", 4, matrix.Elements[1]);
301                         AssertEquals ("Y#3", 6, matrix.Elements[2]);
302                         AssertEquals ("Y#4", 8, matrix.Elements[3]);
303                         AssertEquals ("Y#5", 80, matrix.Elements[4]);
304                         AssertEquals ("Y#6", 112, matrix.Elements[5]);                  
305                                                                             
306                 }                       
307         }
308 }