[GDIPLUS_WINETEST]
[reactos.git] / rostests / winetests / gdiplus / matrix.c
1 /*
2 * Unit test suite for matrices
3 *
4 * Copyright (C) 2007 Google (Evan Stade)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <math.h>
22
23 #include "windows.h"
24 #include <stdio.h>
25 #include "gdiplus.h"
26 #include "wine/test.h"
27
28 #define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)
29 #define expectf(expected, got) ok(fabs(expected - got) < 0.0001, "Expected %.2f, got %.2f\n", expected, got)
30
31 static void test_constructor_destructor(void)
32 {
33 GpStatus status;
34 GpMatrix *matrix = NULL;
35
36 status = GdipCreateMatrix2(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &matrix);
37 expect(Ok, status);
38 ok(matrix != NULL, "Expected matrix to be initialized\n");
39
40 status = GdipDeleteMatrix(NULL);
41 expect(InvalidParameter, status);
42
43 status = GdipDeleteMatrix(matrix);
44 expect(Ok, status);
45 }
46
47 typedef struct{
48 REAL X;
49 REAL Y;
50 } real_point;
51
52 static real_point transform_points[] = {
53 {1000.00, 2600.00}, /*0*/
54 {855.00, 2390.00}, /*1*/
55 {700.00, 2200.00}, /*2*/
56 {565.00, 1970.00}, /*3*/
57 {400.00, 1800.00}, /*4*/
58 {275.00, 1550.00}, /*5*/
59 {100.00, 1400.00}, /*6*/
60 {-15.00, 1130.00}, /*7*/
61 {-200.00, 1000.00}, /*8*/
62 {-305.00, 710.00} /*9*/
63 };
64
65 static void test_transform(void)
66 {
67 GpStatus status;
68 GpMatrix *matrix = NULL;
69 GpPointF pts[10];
70 INT i;
71 BOOL match;
72
73 for(i = 0; i < 10; i ++){
74 pts[i].X = i * 5.0 * (REAL)(i % 2);
75 pts[i].Y = 50.0 - i * 5.0;
76 }
77
78 GdipCreateMatrix2(1.0, -2.0, 30.0, 40.0, -500.0, 600.0, &matrix);
79
80 status = GdipTransformMatrixPoints(matrix, pts, 10);
81 expect(Ok, status);
82
83 for(i = 0; i < 10; i ++){
84 match = fabs(transform_points[i].X - pts[i].X) < 2.0
85 && fabs(transform_points[i].Y - pts[i].Y) < 2.0;
86
87 ok(match, "Expected #%d to be (%.2f, %.2f) but got (%.2f, %.2f)\n", i,
88 transform_points[i].X, transform_points[i].Y, pts[i].X, pts[i].Y);
89 }
90
91 GdipDeleteMatrix(matrix);
92 }
93
94 static void test_isinvertible(void)
95 {
96 GpStatus status;
97 GpMatrix *matrix = NULL;
98 BOOL result;
99
100 /* NULL arguments */
101 status = GdipIsMatrixInvertible(NULL, &result);
102 expect(InvalidParameter, status);
103 status = GdipIsMatrixInvertible((GpMatrix*)0xdeadbeef, NULL);
104 expect(InvalidParameter, status);
105 status = GdipIsMatrixInvertible(NULL, NULL);
106 expect(InvalidParameter, status);
107
108 /* invertible */
109 GdipCreateMatrix2(1.0, 1.2, 2.3, -1.0, 2.0, 3.0, &matrix);
110 status = GdipIsMatrixInvertible(matrix, &result);
111 expect(Ok, status);
112 expect(TRUE, result);
113 GdipDeleteMatrix(matrix);
114
115 /* noninvertible */
116 GdipCreateMatrix2(2.0, -1.0, 6.0, -3.0, 2.2, 3.0, &matrix);
117 status = GdipIsMatrixInvertible(matrix, &result);
118 expect(Ok, status);
119 expect(FALSE, result);
120 GdipDeleteMatrix(matrix);
121 }
122
123 static void test_invert(void)
124 {
125 GpStatus status;
126 GpMatrix *matrix = NULL;
127 GpMatrix *inverted = NULL;
128 BOOL equal = FALSE;
129
130 /* NULL */
131 status = GdipInvertMatrix(NULL);
132 expect(InvalidParameter, status);
133
134 /* noninvertible */
135 GdipCreateMatrix2(2.0, -1.0, 6.0, -3.0, 2.2, 3.0, &matrix);
136 status = GdipInvertMatrix(matrix);
137 expect(InvalidParameter, status);
138 GdipDeleteMatrix(matrix);
139
140 /* invertible */
141 GdipCreateMatrix2(3.0, -2.0, 5.0, 2.0, 6.0, 3.0, &matrix);
142 status = GdipInvertMatrix(matrix);
143 expect(Ok, status);
144 GdipCreateMatrix2(2.0/16.0, 2.0/16.0, -5.0/16.0, 3.0/16.0, 3.0/16.0, -21.0/16.0, &inverted);
145 GdipIsMatrixEqual(matrix, inverted, &equal);
146 expect(TRUE, equal);
147
148 GdipDeleteMatrix(inverted);
149 GdipDeleteMatrix(matrix);
150 }
151
152 static void test_shear(void)
153 {
154 GpStatus status;
155 GpMatrix *matrix = NULL;
156 GpMatrix *sheared = NULL;
157 BOOL equal;
158
159 /* NULL */
160 status = GdipShearMatrix(NULL, 0.0, 0.0, MatrixOrderPrepend);
161 expect(InvalidParameter, status);
162
163 /* X only shearing, MatrixOrderPrepend */
164 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix);
165 status = GdipShearMatrix(matrix, 1.5, 0.0, MatrixOrderPrepend);
166 expect(Ok, status);
167 GdipCreateMatrix2(1.0, 2.0, 5.5, 2.0, 6.0, 3.0, &sheared);
168 GdipIsMatrixEqual(matrix, sheared, &equal);
169 expect(TRUE, equal);
170 GdipDeleteMatrix(sheared);
171 GdipDeleteMatrix(matrix);
172
173 /* X only shearing, MatrixOrderAppend */
174 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix);
175 status = GdipShearMatrix(matrix, 1.5, 0.0, MatrixOrderAppend);
176 expect(Ok, status);
177 GdipCreateMatrix2(4.0, 2.0, 2.5, -1.0, 10.5, 3.0, &sheared);
178 GdipIsMatrixEqual(matrix, sheared, &equal);
179 expect(TRUE, equal);
180 GdipDeleteMatrix(sheared);
181 GdipDeleteMatrix(matrix);
182
183 /* Y only shearing, MatrixOrderPrepend */
184 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix);
185 status = GdipShearMatrix(matrix, 0.0, 1.5, MatrixOrderPrepend);
186 expect(Ok, status);
187 GdipCreateMatrix2(7.0, 0.5, 4.0, -1.0, 6.0, 3.0, &sheared);
188 GdipIsMatrixEqual(matrix, sheared, &equal);
189 expect(TRUE, equal);
190 GdipDeleteMatrix(sheared);
191 GdipDeleteMatrix(matrix);
192
193 /* Y only shearing, MatrixOrderAppend */
194 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix);
195 status = GdipShearMatrix(matrix, 0.0, 1.5, MatrixOrderAppend);
196 expect(Ok, status);
197 GdipCreateMatrix2(1.0, 3.5, 4.0, 5.0, 6.0, 12.0, &sheared);
198 GdipIsMatrixEqual(matrix, sheared, &equal);
199 expect(TRUE, equal);
200 GdipDeleteMatrix(sheared);
201 GdipDeleteMatrix(matrix);
202
203 /* X,Y shearing, MatrixOrderPrepend */
204 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix);
205 status = GdipShearMatrix(matrix, 4.0, 1.5, MatrixOrderPrepend);
206 expect(Ok, status);
207 GdipCreateMatrix2(7.0, 0.5, 8.0, 7.0, 6.0, 3.0, &sheared);
208 GdipIsMatrixEqual(matrix, sheared, &equal);
209 expect(TRUE, equal);
210 GdipDeleteMatrix(sheared);
211 GdipDeleteMatrix(matrix);
212
213 /* X,Y shearing, MatrixOrderAppend */
214 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix);
215 status = GdipShearMatrix(matrix, 4.0, 1.5, MatrixOrderAppend);
216 expect(Ok, status);
217 GdipCreateMatrix2(9.0, 3.5, 0.0, 5.0, 18.0, 12.0, &sheared);
218 GdipIsMatrixEqual(matrix, sheared, &equal);
219 expect(TRUE, equal);
220 GdipDeleteMatrix(sheared);
221 GdipDeleteMatrix(matrix);
222 }
223
224 static void test_constructor3(void)
225 {
226 /* MSDN is on crack. GdipCreateMatrix3 makes a matrix that transforms the
227 * corners of the given rectangle to the three points given. */
228 GpMatrix *matrix;
229 REAL values[6];
230 GpRectF rc;
231 GpPointF pt[3];
232 GpStatus stat;
233
234 rc.X = 10;
235 rc.Y = 10;
236 rc.Width = 10;
237 rc.Height = 10;
238
239 pt[0].X = 10;
240 pt[0].Y = 10;
241 pt[1].X = 20;
242 pt[1].Y = 10;
243 pt[2].X = 10;
244 pt[2].Y = 20;
245
246 stat = GdipCreateMatrix3(&rc, pt, &matrix);
247 expect(Ok, stat);
248
249 stat = GdipGetMatrixElements(matrix, values);
250 expect(Ok, stat);
251
252 expectf(1.0, values[0]);
253 expectf(0.0, values[1]);
254 expectf(0.0, values[2]);
255 expectf(1.0, values[3]);
256 expectf(0.0, values[4]);
257 expectf(0.0, values[5]);
258
259 GdipDeleteMatrix(matrix);
260
261 pt[0].X = 20;
262 pt[0].Y = 10;
263 pt[1].X = 40;
264 pt[1].Y = 10;
265 pt[2].X = 20;
266 pt[2].Y = 20;
267
268 stat = GdipCreateMatrix3(&rc, pt, &matrix);
269 expect(Ok, stat);
270
271 stat = GdipGetMatrixElements(matrix, values);
272 expect(Ok, stat);
273
274 expectf(2.0, values[0]);
275 expectf(0.0, values[1]);
276 expectf(0.0, values[2]);
277 expectf(1.0, values[3]);
278 expectf(0.0, values[4]);
279 expectf(0.0, values[5]);
280
281 GdipDeleteMatrix(matrix);
282
283 pt[0].X = 10;
284 pt[0].Y = 20;
285 pt[1].X = 20;
286 pt[1].Y = 30;
287 pt[2].X = 10;
288 pt[2].Y = 30;
289
290 stat = GdipCreateMatrix3(&rc, pt, &matrix);
291 expect(Ok, stat);
292
293 stat = GdipGetMatrixElements(matrix, values);
294 expect(Ok, stat);
295
296 expectf(1.0, values[0]);
297 expectf(1.0, values[1]);
298 expectf(0.0, values[2]);
299 expectf(1.0, values[3]);
300 expectf(0.0, values[4]);
301 expectf(0.0, values[5]);
302
303 GdipDeleteMatrix(matrix);}
304
305 START_TEST(matrix)
306 {
307 struct GdiplusStartupInput gdiplusStartupInput;
308 ULONG_PTR gdiplusToken;
309
310 gdiplusStartupInput.GdiplusVersion = 1;
311 gdiplusStartupInput.DebugEventCallback = NULL;
312 gdiplusStartupInput.SuppressBackgroundThread = 0;
313 gdiplusStartupInput.SuppressExternalCodecs = 0;
314
315 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
316
317 test_constructor_destructor();
318 test_transform();
319 test_isinvertible();
320 test_invert();
321 test_shear();
322 test_constructor3();
323
324 GdiplusShutdown(gdiplusToken);
325 }