jscript: Added minus expression implementation.
[wine] / dlls / jscript / tests / lang.js
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 var tmp;
20
21 ok(true, "true is not true?");
22 ok(!false, "!false is not true");
23 ok(!undefined, "!undefined is not true");
24 ok(!null, "!null is not true");
25 ok(!0, "!0 is not true");
26 ok(!0.0, "!0.0 is not true");
27
28 ok(1 === 1, "1 === 1 is false");
29 ok(!(1 === 2), "!(1 === 2) is false");
30 ok(1.0 === 1, "1.0 === 1 is false");
31 ok("abc" === "abc", "\"abc\" === \"abc\" is false");
32 ok(true === true, "true === true is false");
33 ok(null === null, "null === null is false");
34 ok(undefined === undefined, "undefined === undefined is false");
35 ok(!(undefined === null), "!(undefined === null) is false");
36
37 ok(1 !== 2, "1 !== 2 is false");
38 ok(null !== undefined, "null !== undefined is false");
39
40 var trueVar = true;
41 ok(trueVar, "trueVar is not true");
42
43 ok(ScriptEngine.length === 0, "ScriptEngine.length is not 0");
44
45 function testFunc1(x, y) {
46     ok(this !== undefined, "this is undefined");
47     ok(x === true, "x is not 1");
48     ok(y === "test", "y is not \"test\"");
49     ok(arguments.length === 2, "arguments.length is not 2");
50     ok(arguments["0"] === true, "arguments[0] is not true");
51     ok(arguments["1"] === "test", "arguments[1] is not \"test\"");
52
53     return true;
54 }
55
56 ok(testFunc1.length === 2, "testFunc1.length is not 2");
57
58 ok(Object.prototype !== undefined, "Object.prototype is undefined");
59 ok(Object.prototype.prototype === undefined, "Object.prototype is not undefined");
60 ok(String.prototype !== undefined, "String.prototype is undefined");
61 ok(Array.prototype !== undefined, "Array.prototype is undefined");
62 ok(Boolean.prototype !== undefined, "Boolean.prototype is undefined");
63 ok(Number.prototype !== undefined, "Number.prototype is undefined");
64 ok(RegExp.prototype !== undefined, "RegExp.prototype is undefined");
65 ok(Math !== undefined, "Math is undefined");
66 ok(Math.prototype === undefined, "Math.prototype is not undefined");
67
68 ok(typeof(0) === "number", "typeof(0) is not number");
69 ok(typeof(1.5) === "number", "typeof(1.5) is not number");
70 ok(typeof("abc") === "string", "typeof(\"abc\") is not string");
71 ok(typeof("") === "string", "typeof(\"\") is not string");
72 ok(typeof(true) === "boolean", "typeof(true) is not boolean");
73 ok(typeof(null) === "object", "typeof(null) is not object");
74 ok(typeof(undefined) === "undefined", "typeof(undefined) is not undefined");
75 ok(typeof(Math) === "object", "typeof(Math) is not object");
76 ok(typeof(String.prototype) === "object", "typeof(String.prototype) is not object");
77 ok(typeof(testFunc1) === "function", "typeof(testFunc1) is not function");
78 ok(typeof(String) === "function", "typeof(String) is not function");
79 ok(typeof(ScriptEngine) === "function", "typeof(ScriptEngine) is not function");
80 ok(typeof(this) === "object", "typeof(this) is not object");
81
82 ok(testFunc1(true, "test") === true, "testFunc1 not returned true");
83
84 var obj1 = new Object();
85 ok(typeof(obj1) === "object", "typeof(obj1) is not object");
86 obj1.test = true;
87 obj1.func = function () {
88     ok(this === obj1, "this is not obj1");
89     ok(this.test === true, "this.test is not true");
90     ok(arguments.length === 1, "arguments.length is not 1");
91     ok(arguments["0"] === true, "arguments[0] is not true");
92
93     return "test";
94 };
95
96 ok(obj1.func(true) === "test", "obj1.func(true) is not \"test\"");
97
98 function testConstr1() {
99     this.var1 = 1;
100
101     ok(this !== undefined, "this is undefined");
102     ok(arguments.length === 1, "arguments.length is not 1");
103     ok(arguments["0"] === true, "arguments[0] is not 1");
104
105     return false;
106 }
107
108 testConstr1.prototype.pvar = 1;
109
110 var obj2 = new testConstr1(true);
111 ok(typeof(obj2) === "object", "typeof(obj2) is not object");
112 ok(obj2.pvar === 1, "obj2.pvar is not 1");
113
114 testConstr1.prototype.pvar = 2;
115 ok(obj2.pvar === 2, "obj2.pvar is not 2");
116
117 obj2.pvar = 3;
118 testConstr1.prototype.pvar = 1;
119 ok(obj2.pvar === 3, "obj2.pvar is not 3");
120
121 tmp = 0;
122 if(true)
123     tmp = 1;
124 else
125     ok(false, "else evaluated");
126 ok(tmp === 1, "tmp !== 1, if not evaluated?");
127
128 tmp = 0;
129 if(1 === 0)
130     ok(false, "if evaluated");
131 else
132     tmp = 1;
133 ok(tmp === 1, "tmp !== 1, if not evaluated?");
134
135 if(false)
136     ok(false, "if(false) evaluated");
137
138 tmp = 0;
139 if(true)
140     tmp = 1;
141 ok(tmp === 1, "tmp !== 1, if(true) not evaluated?");
142
143 var obj3 = { prop1: 1,  prop2: typeof(false) };
144 ok(obj3.prop1 === 1, "obj3.prop1 is not 1");
145 ok(obj3.prop2 === "boolean", "obj3.prop2 is not \"boolean\"");
146
147 {
148     var blockVar = 1;
149     blockVar = 2;
150 }
151 ok(blockVar === 2, "blockVar !== 2");
152
153 ok((true ? 1 : 2) === 1, "conditional expression true is not 1");
154 ok((0 === 2 ? 1 : 2) === 2, "conditional expression true is not 2");
155
156 ok(getVT(undefined) === "VT_EMPTY", "getVT(undefined) is not VT_EMPTY");
157 ok(getVT(null) === "VT_NULL", "getVT(null) is not VT_NULL");
158 ok(getVT(0) === "VT_I4", "getVT(0) is not VT_I4");
159 ok(getVT(0.5) === "VT_R8", "getVT(1.5) is not VT_R8");
160 ok(getVT("test") === "VT_BSTR", "getVT(\"test\") is not VT_BSTR");
161 ok(getVT(Math) === "VT_DISPATCH", "getVT(Math) is not VT_DISPATCH");
162 ok(getVT(false) === "VT_BOOL", "getVT(false) is not VT_BOOL");
163
164 tmp = 2+2;
165 ok(tmp === 4, "2+2 !== 4");
166 ok(getVT(tmp) === "VT_I4", "getVT(2+2) !== VT_I4");
167
168 tmp = 2+2.5;
169 ok(tmp === 4.5, "2+2.5 !== 4.5");
170 ok(getVT(tmp) === "VT_R8", "getVT(2+2.5) !== VT_R8");
171
172 tmp = 1.5+2.5;
173 ok(tmp === 4, "1.4+2.5 !== 4");
174 ok(getVT(tmp) === "VT_I4", "getVT(1.5+2.5) !== VT_I4");
175
176 tmp = 4-2;
177 ok(tmp === 2, "4-2 !== 2");
178 ok(getVT(tmp) === "VT_I4", "getVT(4-2) !== VT_I4");
179
180 tmp = 4.5-2;
181 ok(tmp === 2.5, "4.5-2 !== 2.5");
182 ok(getVT(tmp) === "VT_R8", "getVT(4-2) !== VT_R8");
183
184 tmp = -2;
185 ok(tmp === 0-2, "-2 !== 0-2");
186 ok(getVT(tmp) === "VT_I4", "getVT(-2) !== VT_I4");
187
188 tmp = 2*3;
189 ok(tmp === 6, "2*3 !== 6");
190 ok(getVT(tmp) === "VT_I4", "getVT(2*3) !== VT_I4");
191
192 tmp = 2*3.5;
193 ok(tmp === 7, "2*3.5 !== 7");
194 ok(getVT(tmp) === "VT_I4", "getVT(2*3.5) !== VT_I4");
195
196 tmp = 2.5*3.5;
197 ok(tmp === 8.75, "2.5*3.5 !== 8.75");
198 ok(getVT(tmp) === "VT_R8", "getVT(2.5*3.5) !== VT_R8");
199
200 tmp = 4/2;
201 ok(tmp === 2, "4/2 !== 2");
202 ok(getVT(tmp) === "VT_I4", "getVT(4/2) !== VT_I4");
203
204 tmp = 4.5/1.5;
205 ok(tmp === 3, "4.5/1.5 !== 3");
206 ok(getVT(tmp) === "VT_I4", "getVT(4.5/1.5) !== VT_I4");
207
208 tmp = 3/2;
209 ok(tmp === 1.5, "3/2 !== 1.5");
210 ok(getVT(tmp) === "VT_R8", "getVT(3/2) !== VT_R8");
211
212 tmp = "ab" + "cd";
213 ok(tmp === "abcd", "\"ab\" + \"cd\" !== \"abcd\"");
214
215 tmp = 1;
216 ok((tmp += 1) === 2, "tmp += 1 !== 2");
217 ok(tmp === 2, "tmp !== 2");
218
219 tmp = 2;
220 ok((tmp -= 1) === 1, "tmp -= 1 !== 1");
221 ok(tmp === 1, "tmp !=== 1");
222
223 tmp = 2;
224 ok((tmp *= 1.5) === 3, "tmp *= 1.5 !== 3");
225 ok(tmp === 3, "tmp !=== 3");
226
227 tmp = 5;
228 ok((tmp /= 2) === 2.5, "tmp /= 2 !== 2.5");
229 ok(tmp === 2.5, "tmp !=== 2.5");
230
231 tmp = 3 || ok(false, "second or expression called");
232 ok(tmp === 3, "3 || (...) is not 3");
233
234 tmp = false || 2;
235 ok(tmp === 2, "false || 2 is not 2");
236
237 tmp = 0 && ok(false, "second and expression called");
238 ok(tmp === 0, "0 && (...) is not 0");
239
240 tmp = true && "test";
241 ok(tmp === "test", "true && \"test\" is not \"test\"");
242
243 tmp = true && 0;
244 ok(tmp === 0, "true && 0 is not 0");
245
246 ok(1 < 3.4, "1 < 3.4 failed");
247 ok(!(3.4 < 1), "3.4 < 1");
248 ok("abc" < "abcd", "abc < abcd failed");
249 ok("abcd" < "abce", "abce < abce failed");
250 ok("" < "x", "\"\" < \"x\" failed");
251 ok(!(0 < 0), "0 < 0");
252
253 ok(1 <= 3.4, "1 <= 3.4 failed");
254 ok(!(3.4 <= 1), "3.4 <= 1");
255 ok("abc" <= "abcd", "abc <= abcd failed");
256 ok("abcd" <= "abce", "abce <= abce failed");
257 ok("" <= "x", "\"\" <= \"x\" failed");
258 ok(0 <= 0, "0 <= 0 failed");
259
260 ok(3.4 > 1, "3.4 > 1 failed");
261 ok(!(1 > 3.4), "1 > 3.4");
262 ok("abcd" > "abc", "abc > abcd failed");
263 ok("abce" > "abcd", "abce > abce failed");
264 ok("x" > "", "\"x\" > \"\" failed");
265 ok(!(0 > 0), "0 > 0");
266
267 ok(3.4 >= 1, "3.4 >= 1 failed");
268 ok(!(1 >= 3.4), "1 >= 3.4");
269 ok("abcd" >= "abc", "abc >= abcd failed");
270 ok("abce" >= "abcd", "abce >= abce failed");
271 ok("x" >= "", "\"x\" >= \"\" failed");
272 ok(0 >= 0, "0 >= 0");
273
274 tmp = 1;
275 ok(++tmp === 2, "++tmp (1) is not 2");
276 ok(tmp === 2, "incremented tmp is not 2");
277 ok(--tmp === 1, "--tmp (2) is not 1");
278 ok(tmp === 1, "decremented tmp is not 1");
279 ok(tmp++ === 1, "tmp++ (1) is not 1");
280 ok(tmp === 2, "incremented tmp(1) is not 2");
281 ok(tmp-- === 2, "tmp-- (2) is not 2");
282 ok(tmp === 1, "decremented tmp is not 1");
283
284 String.prototype.test = true;
285 ok("".test === true, "\"\".test is not true");
286
287 Boolean.prototype.test = true;
288 ok(true.test === true, "true.test is not true");
289
290 Number.prototype.test = true;
291 ok((0).test === true, "(0).test is not true");
292 ok((0.5).test === true, "(0.5).test is not true");
293
294 var state = "";
295 try {
296     ok(state === "", "try: state = " + state);
297     state = "try";
298 }catch(ex) {
299     ok(false, "unexpected catch");
300 }
301 ok(state === "try", "state = " + state + " expected try");
302
303 state = "";
304 try {
305     ok(state === "", "try: state = " + state);
306     state = "try";
307 }finally {
308     ok(state === "try", "funally: state = " + state);
309     state = "finally";
310 }
311 ok(state === "finally", "state = " + state + " expected finally");
312
313 state = "";
314 try {
315     ok(state === "", "try: state = " + state);
316     state = "try";
317 }catch(ex) {
318     ok(false, "unexpected catch");
319 }finally {
320     ok(state === "try", "funally: state = " + state);
321     state = "finally";
322 }
323 ok(state === "finally", "state = " + state + " expected finally");
324
325 var state = "";
326 try {
327     ok(state === "", "try: state = " + state);
328     state = "try";
329     throw "except";
330 }catch(ex) {
331     ok(state === "try", "catch: state = " + state);
332     ok(ex === "except", "ex is not \"except\"");
333     state = "catch";
334 }
335 ok(state === "catch", "state = " + state + " expected catch");
336
337 var state = "";
338 try {
339     ok(state === "", "try: state = " + state);
340     state = "try";
341     throw true;
342 }catch(ex) {
343     ok(state === "try", "catch: state = " + state);
344     ok(ex === true, "ex is not true");
345     state = "catch";
346 }finally {
347     ok(state === "catch", "funally: state = " + state);
348     state = "finally";
349 }
350 ok(state === "finally", "state = " + state + " expected finally");
351
352 var state = "";
353 try {
354     ok(state === "", "try: state = " + state);
355     state = "try";
356     try { throw true; } finally {}
357 }catch(ex) {
358     ok(state === "try", "catch: state = " + state);
359     ok(ex === true, "ex is not true");
360     state = "catch";
361 }finally {
362     ok(state === "catch", "funally: state = " + state);
363     state = "finally";
364 }
365 ok(state === "finally", "state = " + state + " expected finally");
366
367 var state = "";
368 try {
369     ok(state === "", "try: state = " + state);
370     state = "try";
371     try { throw "except"; } catch(ex) { throw true; }
372 }catch(ex) {
373     ok(state === "try", "catch: state = " + state);
374     ok(ex === true, "ex is not true");
375     state = "catch";
376 }finally {
377     ok(state === "catch", "funally: state = " + state);
378     state = "finally";
379 }
380 ok(state === "finally", "state = " + state + " expected finally");
381
382 function throwFunc(x) {
383     throw x;
384 }
385
386 var state = "";
387 try {
388     ok(state === "", "try: state = " + state);
389     state = "try";
390     throwFunc(true);
391 }catch(ex) {
392     ok(state === "try", "catch: state = " + state);
393     ok(ex === true, "ex is not true");
394     state = "catch";
395 }finally {
396     ok(state === "catch", "funally: state = " + state);
397     state = "finally";
398 }
399 ok(state === "finally", "state = " + state + " expected finally");
400
401 reportSuccess();