Make automated FSCommand invocation tests show player-side output.
[gnash.git] / testsuite / libcore.all / MatrixTest.cpp
blob9b6a5a386cb41342949f6133fd923fe5bf9df4c2
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 #include "gnashconfig.h"
21 #endif
23 #include "SWFMatrix.h"
24 #include "Point2d.h"
25 #include <ostream>
26 #include <cmath>
28 #include "check.h"
30 using namespace gnash;
32 // for double comparison
33 struct D {
34 double _d;
35 double _t; // tolerance
37 D(double d) : _d(d), _t(1e-3) {}
39 // Set tolerance
40 D(double d, double t) : _d(d), _t(t) {}
42 // Return true if the difference between the two
43 // doubles is below the minimum tolerance defined for the two
44 bool operator==(const D& d)
46 double tol = std::min(_t, d._t);
47 double delta = std::abs(_d - d._d);
48 bool ret = delta < tol;
49 //cout << "D " << _d << "operator==(const D " << d._d <<") returning " << ret << " (delta is " << delta << ") " << endl;
50 return ret;
53 std::ostream& operator<<(std::ostream& os, const D& d)
55 return os << d._d << " [tol: " << d._t << "]";
58 int
59 main(int /*argc*/, char** /*argv*/)
61 //
62 // Test boost types, SWFMatrix design is rely on this.
63 //
64 // Note: If any of the following tests fails, your boost library
65 // is bogus or hasn't been installed properly.
66 //
67 check_equals(sizeof(std::int8_t), 1);
68 check_equals(sizeof(std::uint8_t), 1);
69 check_equals(sizeof(std::int16_t), 2);
70 check_equals(sizeof(std::uint16_t), 2);
71 check_equals(sizeof(std::int32_t), 4);
72 check_equals(sizeof(std::uint32_t), 4);
73 check_equals(sizeof(std::int64_t), 8);
74 check_equals(sizeof(std::uint64_t), 8);
76 //
77 // Test identity SWFMatrix.
78 //
79 SWFMatrix identity;
80 check_equals(identity.get_x_scale(), 1);
81 check_equals(identity.get_y_scale(), 1);
82 check_equals(identity.get_rotation(), 0);
83 check_equals(identity.get_x_translation(), 0);
84 check_equals(identity.get_y_translation(), 0);
86 check_equals(identity.invert(), identity);
89 // Test parameter setting and getting, interfaces for AS.
91 SWFMatrix m1;
92 m1.set_scale_rotation(1, 3, 0);
93 check_equals(m1.get_x_scale(), 1);
94 check_equals(m1.get_y_scale(), 3);
95 check_equals(m1.get_rotation(), 0);
96 check_equals(m1.get_x_translation(), 0);
97 check_equals(m1.get_y_translation(), 0);
99 m1.set_scale(1.5, 2.5);
100 check_equals(D(m1.get_x_scale()), 1.5);
101 check_equals(D(m1.get_y_scale()), 2.5);
102 check_equals(D(m1.get_rotation()), 0);
103 check_equals(m1.get_x_translation(), 0);
104 check_equals(m1.get_y_translation(), 0);
106 m1.set_scale(34, 4);
107 check_equals(D(m1.get_x_scale()), 34);
108 check_equals(D(m1.get_y_scale()), 4);
109 check_equals(D(m1.get_rotation()), 0);
110 check_equals(m1.get_x_translation(), 0);
111 check_equals(m1.get_y_translation(), 0);
113 m1.set_scale_rotation(1, 1, 2);
114 check_equals(D(m1.get_x_scale()), 1);
115 check_equals(D(m1.get_y_scale()), 1);
116 check_equals(D(m1.get_rotation()), 2);
117 check_equals(m1.get_x_translation(), 0);
118 check_equals(m1.get_y_translation(), 0);
120 m1.set_x_scale(2);
121 check_equals(D(m1.get_x_scale()), 2);
122 check_equals(D(m1.get_y_scale()), 1);
123 check_equals(D(m1.get_rotation()), 2);
124 check_equals(m1.get_x_translation(), 0);
125 check_equals(m1.get_y_translation(), 0);
127 m1.set_scale(1, 2);
128 check_equals(D(m1.get_x_scale()), 1);
129 check_equals(D(m1.get_y_scale()), 2);
130 check_equals(D(m1.get_rotation()), 2);
131 check_equals(m1.get_x_translation(), 0);
132 check_equals(m1.get_y_translation(), 0);
134 m1.set_rotation(0);
135 check_equals(D(m1.get_x_scale()), 1);
136 check_equals(D(m1.get_y_scale()), 2);
137 check_equals(D(m1.get_rotation()), 0);
138 check_equals(m1.get_x_translation(), 0);
139 check_equals(m1.get_y_translation(), 0);
141 m1.set_translation(5, 6);
142 check_equals(D(m1.get_x_scale()), 1);
143 check_equals(D(m1.get_y_scale()), 2);
144 check_equals(D(m1.get_rotation()), 0);
145 check_equals(m1.get_x_translation(), 5);
146 check_equals(m1.get_y_translation(), 6);
148 m1.set_rotation(2);
149 check_equals(D(m1.get_x_scale()), 1);
150 check_equals(D(m1.get_y_scale()), 2);
151 check_equals(D(m1.get_rotation()), 2);
152 check_equals(m1.get_x_translation(), 5);
153 check_equals(m1.get_y_translation(), 6);
155 SWFMatrix m2;
156 check_equals(D(m2.get_rotation()), 0);
157 m2.set_x_scale(16);
158 check_equals(D(m2.get_x_scale()), 16);
159 check_equals(D(m2.get_y_scale()), 1);
160 check_equals(D(m2.get_rotation()), 0);
161 m2.set_x_scale(-16);
162 check_equals(D(m2.get_x_scale()), 16);
163 check_equals(D(m2.get_y_scale()), 1);
164 check_equals(D(m2.get_rotation()), 3.14159);
165 m2.set_x_scale(16);
166 check_equals(D(m2.get_x_scale()), 16);
167 check_equals(D(m2.get_y_scale()), 1);
168 check_equals(D(m2.get_rotation()), 3.14159);
169 m2.set_x_scale(16);
170 m2.set_y_scale(-64);
171 check_equals(D(m2.get_x_scale()), 16);
172 check_equals(D(m2.get_y_scale()), 64);
173 check_equals(D(m2.get_rotation()), 3.14159);
174 m2.set_x_scale(16);
175 m2.set_x_scale(-128);
176 check_equals(D(m2.get_x_scale()), 128);
177 check_equals(D(m2.get_y_scale()), 64);
178 check_equals(D(m2.get_rotation()), 0);
181 // Test SWFMatrix concatenation
183 m1.concatenate_scale(2, 2);
184 check_equals(D(m1.get_x_scale()), 2);
185 check_equals(D(m1.get_y_scale()), 4);
186 check_equals(D(m1.get_rotation()), 2);
187 check_equals(m1.get_x_translation(), 5);
188 check_equals(m1.get_y_translation(), 6);
190 m1.concatenate_scale(3, 3);
191 check_equals(D(m1.get_x_scale()), 6);
192 check_equals(D(m1.get_y_scale()), 12);
193 check_equals(D(m1.get_rotation()), 2);
194 check_equals(m1.get_x_translation(), 5);
195 check_equals(m1.get_y_translation(), 6);
197 m1.concatenate_scale(2, 1);
198 check_equals(D(m1.get_x_scale()), 12);
199 check_equals(D(m1.get_y_scale()), 12);
200 check_equals(D(m1.get_rotation()), 2);
201 check_equals(m1.get_x_translation(), 5);
202 check_equals(m1.get_y_translation(), 6);
205 // Test SWFMatrix transformations
207 point p1(0, 0);
208 point p2(64, 64);
209 point r;
211 m1.set_identity();
212 // Make a distance of 64 become a distance of 20 ..
213 m1.set_scale(20.0/64, 20.0/64);
215 m1.transform(&r, p1);
216 check_equals(r.x, 0);
217 check_equals(r.y, 0);
219 m1.transform(&r, p2);
220 check_equals(r.x, 20);
221 check_equals(r.y, 20);
223 // Translate points to have the origin at 32,32
224 // (coordinates expressed in prior-to-scaling SWFMatrix)
225 m1.concatenate_translation(-32, -32);
227 m1.transform(&r, p1);
228 check_equals(r.x, -10);
229 check_equals(r.y, -10);
231 m1.transform(&r, p2);
232 check_equals(r.x, 10);
233 check_equals(r.y, 10);
235 // Apply a final scaling by 10 keeping the current origin
236 // (reached after translation)
237 SWFMatrix final;
238 final.set_scale(10, 10);
239 final.concatenate(m1);
240 m1 = final;
242 m1.transform(&r, p1);
243 check_equals(r.x, -100);
244 check_equals(r.y, -100);
246 m1.transform(&r, p2);
247 check_equals(r.x, 100);
248 check_equals(r.y, 100);
251 // Test SWFMatrix invertion
253 m1.set_identity();
254 m1.set_translation(50*20, -30*20);
255 m1.set_scale(0.5, 2);
256 m1.set_rotation(90*3.141593/180);
258 SWFMatrix m1_inverse = m1;
259 m1_inverse.invert();
260 // concatenate the inverse SWFMatrix and original SWFMatrix.
261 m1_inverse.concatenate(m1);
262 // the result is expected to be an identity SWFMatrix.
263 check_equals(m1_inverse, identity);
265 m1 = SWFMatrix(4, 0, 0, 4, 20, 20);
267 m1_inverse = m1;
268 m1_inverse.invert();
270 check_equals(m1_inverse.a(), 16384 * 65536);
271 check_equals(m1_inverse.b(), 0);
272 check_equals(m1_inverse.tx(), -16384 * 20);
273 check_equals(m1_inverse.c(), 0);
274 check_equals(m1_inverse.d(), 16384 * 65536);
275 check_equals(m1_inverse.ty(), -16384 * 20);
277 // concatenate the inverse SWFMatrix and original SWFMatrix.
278 m1_inverse.concatenate(m1);
279 // the result is expected to be an identity SWFMatrix.
280 check_equals(m1_inverse, identity);
281 return 0;