2 * Unit test suite for matrices
4 * Copyright (C) 2007 Google (Evan Stade)
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.
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.
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
23 #define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)
24 #define expectf(expected, got) ok(fabs(expected - got) < 0.0001, "Expected %.2f, got %.2f\n", expected, got)
26 static BOOL
compare_float(float f
, float g
, unsigned int ulps
)
36 if (abs(x
- y
) > ulps
)
42 static void test_constructor_destructor(void)
45 GpMatrix
*matrix
= NULL
;
47 status
= GdipCreateMatrix2(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &matrix
);
49 ok(matrix
!= NULL
, "Expected matrix to be initialized\n");
51 status
= GdipDeleteMatrix(NULL
);
52 expect(InvalidParameter
, status
);
54 status
= GdipDeleteMatrix(matrix
);
63 static real_point transform_points
[] = {
64 {1000.00, 2600.00}, /*0*/
65 {855.00, 2390.00}, /*1*/
66 {700.00, 2200.00}, /*2*/
67 {565.00, 1970.00}, /*3*/
68 {400.00, 1800.00}, /*4*/
69 {275.00, 1550.00}, /*5*/
70 {100.00, 1400.00}, /*6*/
71 {-15.00, 1130.00}, /*7*/
72 {-200.00, 1000.00}, /*8*/
73 {-305.00, 710.00} /*9*/
76 static void test_transform(void)
79 GpMatrix
*matrix
= NULL
;
84 for(i
= 0; i
< 10; i
++){
85 pts
[i
].X
= i
* 5.0 * (REAL
)(i
% 2);
86 pts
[i
].Y
= 50.0 - i
* 5.0;
89 GdipCreateMatrix2(1.0, -2.0, 30.0, 40.0, -500.0, 600.0, &matrix
);
91 status
= GdipTransformMatrixPoints(matrix
, pts
, 0);
92 expect(InvalidParameter
, status
);
94 status
= GdipTransformMatrixPoints(matrix
, pts
, 10);
97 for(i
= 0; i
< 10; i
++){
98 match
= fabs(transform_points
[i
].X
- pts
[i
].X
) < 2.0
99 && fabs(transform_points
[i
].Y
- pts
[i
].Y
) < 2.0;
101 ok(match
, "Expected #%d to be (%.2f, %.2f) but got (%.2f, %.2f)\n", i
,
102 transform_points
[i
].X
, transform_points
[i
].Y
, pts
[i
].X
, pts
[i
].Y
);
105 GdipDeleteMatrix(matrix
);
108 static void test_isinvertible(void)
111 GpMatrix
*matrix
= NULL
;
115 status
= GdipIsMatrixInvertible(NULL
, &result
);
116 expect(InvalidParameter
, status
);
117 status
= GdipIsMatrixInvertible((GpMatrix
*)0xdeadbeef, NULL
);
118 expect(InvalidParameter
, status
);
119 status
= GdipIsMatrixInvertible(NULL
, NULL
);
120 expect(InvalidParameter
, status
);
123 GdipCreateMatrix2(1.0, 1.2, 2.3, -1.0, 2.0, 3.0, &matrix
);
124 status
= GdipIsMatrixInvertible(matrix
, &result
);
126 expect(TRUE
, result
);
127 GdipDeleteMatrix(matrix
);
130 GdipCreateMatrix2(2.0, -1.0, 6.0, -3.0, 2.2, 3.0, &matrix
);
131 status
= GdipIsMatrixInvertible(matrix
, &result
);
133 expect(FALSE
, result
);
134 GdipDeleteMatrix(matrix
);
137 static void test_invert(void)
140 GpMatrix
*matrix
= NULL
;
141 GpMatrix
*inverted
= NULL
;
146 status
= GdipInvertMatrix(NULL
);
147 expect(InvalidParameter
, status
);
150 GdipCreateMatrix2(2.0, -1.0, 6.0, -3.0, 2.2, 3.0, &matrix
);
151 status
= GdipInvertMatrix(matrix
);
152 expect(InvalidParameter
, status
);
153 GdipDeleteMatrix(matrix
);
156 GdipCreateMatrix2(3.0, -2.0, 5.0, 2.0, 6.0, 3.0, &matrix
);
157 status
= GdipInvertMatrix(matrix
);
159 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
);
160 GdipIsMatrixEqual(matrix
, inverted
, &equal
);
162 GdipDeleteMatrix(matrix
);
164 GdipCreateMatrix2(0.0006, 0, 0, 0.0006, 400, 400, &matrix
);
165 status
= GdipInvertMatrix(matrix
);
167 status
= GdipGetMatrixElements(matrix
, elems
);
169 ok(compare_float(elems
[0], 1666.666504, 1), "elems[0] = %.10g\n", elems
[0]);
170 ok(compare_float(elems
[1], 0, 0), "elems[1] = %.10g\n", elems
[1]);
171 ok(compare_float(elems
[2], 0, 0), "elems[2] = %.10g\n", elems
[2]);
172 ok(compare_float(elems
[3], 1666.666504, 1), "elems[3] = %.10g\n", elems
[3]);
173 ok(compare_float(elems
[4], -666666.6875, 1), "elems[4] = %.10g\n", elems
[4]);
174 ok(compare_float(elems
[5], -666666.6875, 1), "elems[5] = %.10g\n", elems
[5]);
176 GdipDeleteMatrix(inverted
);
177 GdipDeleteMatrix(matrix
);
180 static void test_shear(void)
183 GpMatrix
*matrix
= NULL
;
184 GpMatrix
*sheared
= NULL
;
188 status
= GdipShearMatrix(NULL
, 0.0, 0.0, MatrixOrderPrepend
);
189 expect(InvalidParameter
, status
);
191 /* X only shearing, MatrixOrderPrepend */
192 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix
);
193 status
= GdipShearMatrix(matrix
, 1.5, 0.0, MatrixOrderPrepend
);
195 GdipCreateMatrix2(1.0, 2.0, 5.5, 2.0, 6.0, 3.0, &sheared
);
196 GdipIsMatrixEqual(matrix
, sheared
, &equal
);
198 GdipDeleteMatrix(sheared
);
199 GdipDeleteMatrix(matrix
);
201 /* X only shearing, MatrixOrderAppend */
202 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix
);
203 status
= GdipShearMatrix(matrix
, 1.5, 0.0, MatrixOrderAppend
);
205 GdipCreateMatrix2(4.0, 2.0, 2.5, -1.0, 10.5, 3.0, &sheared
);
206 GdipIsMatrixEqual(matrix
, sheared
, &equal
);
208 GdipDeleteMatrix(sheared
);
209 GdipDeleteMatrix(matrix
);
211 /* Y only shearing, MatrixOrderPrepend */
212 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix
);
213 status
= GdipShearMatrix(matrix
, 0.0, 1.5, MatrixOrderPrepend
);
215 GdipCreateMatrix2(7.0, 0.5, 4.0, -1.0, 6.0, 3.0, &sheared
);
216 GdipIsMatrixEqual(matrix
, sheared
, &equal
);
218 GdipDeleteMatrix(sheared
);
219 GdipDeleteMatrix(matrix
);
221 /* Y only shearing, MatrixOrderAppend */
222 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix
);
223 status
= GdipShearMatrix(matrix
, 0.0, 1.5, MatrixOrderAppend
);
225 GdipCreateMatrix2(1.0, 3.5, 4.0, 5.0, 6.0, 12.0, &sheared
);
226 GdipIsMatrixEqual(matrix
, sheared
, &equal
);
228 GdipDeleteMatrix(sheared
);
229 GdipDeleteMatrix(matrix
);
231 /* X,Y shearing, MatrixOrderPrepend */
232 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix
);
233 status
= GdipShearMatrix(matrix
, 4.0, 1.5, MatrixOrderPrepend
);
235 GdipCreateMatrix2(7.0, 0.5, 8.0, 7.0, 6.0, 3.0, &sheared
);
236 GdipIsMatrixEqual(matrix
, sheared
, &equal
);
238 GdipDeleteMatrix(sheared
);
239 GdipDeleteMatrix(matrix
);
241 /* X,Y shearing, MatrixOrderAppend */
242 GdipCreateMatrix2(1.0, 2.0, 4.0, -1.0, 6.0, 3.0, &matrix
);
243 status
= GdipShearMatrix(matrix
, 4.0, 1.5, MatrixOrderAppend
);
245 GdipCreateMatrix2(9.0, 3.5, 0.0, 5.0, 18.0, 12.0, &sheared
);
246 GdipIsMatrixEqual(matrix
, sheared
, &equal
);
248 GdipDeleteMatrix(sheared
);
249 GdipDeleteMatrix(matrix
);
252 static void test_constructor3(void)
254 /* MSDN is on crack. GdipCreateMatrix3 makes a matrix that transforms the
255 * corners of the given rectangle to the three points given. */
274 stat
= GdipCreateMatrix3(&rc
, pt
, &matrix
);
277 stat
= GdipGetMatrixElements(matrix
, values
);
280 expectf(1.0, values
[0]);
281 expectf(0.0, values
[1]);
282 expectf(0.0, values
[2]);
283 expectf(1.0, values
[3]);
284 expectf(0.0, values
[4]);
285 expectf(0.0, values
[5]);
287 GdipDeleteMatrix(matrix
);
296 stat
= GdipCreateMatrix3(&rc
, pt
, &matrix
);
299 stat
= GdipGetMatrixElements(matrix
, values
);
302 expectf(2.0, values
[0]);
303 expectf(0.0, values
[1]);
304 expectf(0.0, values
[2]);
305 expectf(1.0, values
[3]);
306 expectf(0.0, values
[4]);
307 expectf(0.0, values
[5]);
309 GdipDeleteMatrix(matrix
);
318 stat
= GdipCreateMatrix3(&rc
, pt
, &matrix
);
321 stat
= GdipGetMatrixElements(matrix
, values
);
324 expectf(1.0, values
[0]);
325 expectf(1.0, values
[1]);
326 expectf(0.0, values
[2]);
327 expectf(1.0, values
[3]);
328 expectf(0.0, values
[4]);
329 expectf(0.0, values
[5]);
331 GdipDeleteMatrix(matrix
);
334 static void test_isidentity(void)
340 stat
= GdipIsMatrixIdentity(NULL
, NULL
);
341 expect(InvalidParameter
, stat
);
343 stat
= GdipIsMatrixIdentity(NULL
, &result
);
344 expect(InvalidParameter
, stat
);
346 stat
= GdipCreateMatrix2(1.0, 0.0, 0.0, 1.0, 0.0, 0.0, &matrix
);
349 stat
= GdipIsMatrixIdentity(matrix
, NULL
);
350 expect(InvalidParameter
, stat
);
353 stat
= GdipIsMatrixIdentity(matrix
, &result
);
355 ok(!!result
, "got %d\n", result
);
357 stat
= GdipSetMatrixElements(matrix
, 1.0, 0.0, 0.0, 1.0, 0.1, 0.0);
361 stat
= GdipIsMatrixIdentity(matrix
, &result
);
363 ok(!result
, "got %d\n", result
);
365 GdipDeleteMatrix(matrix
);
370 struct GdiplusStartupInput gdiplusStartupInput
;
371 ULONG_PTR gdiplusToken
;
373 int (CDECL
* _controlfp_s
)(unsigned int *cur
, unsigned int newval
, unsigned int mask
);
375 /* Enable all FP exceptions except _EM_INEXACT, which gdi32 can trigger */
376 hmsvcrt
= LoadLibraryA("msvcrt");
377 _controlfp_s
= (void*)GetProcAddress(hmsvcrt
, "_controlfp_s");
378 if (_controlfp_s
) _controlfp_s(0, 0, 0x0008001e);
380 gdiplusStartupInput
.GdiplusVersion
= 1;
381 gdiplusStartupInput
.DebugEventCallback
= NULL
;
382 gdiplusStartupInput
.SuppressBackgroundThread
= 0;
383 gdiplusStartupInput
.SuppressExternalCodecs
= 0;
385 GdiplusStartup(&gdiplusToken
, &gdiplusStartupInput
, NULL
);
387 test_constructor_destructor();
395 GdiplusShutdown(gdiplusToken
);