jscript: Added continue statement implementation.
[wine/multimedia.git] / dlls / jscript / tests / lang.js
blob74a84efd66d2534107a555019b148f73f2a79a30
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  */
19 var tmp;
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");
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");
37 ok(1 !== 2, "1 !== 2 is false");
38 ok(null !== undefined, "null !== undefined is false");
40 ok(1 == 1, "1 == 1 is false");
41 ok(!(1 == 2), "!(1 == 2) is false");
42 ok(1.0 == 1, "1.0 == 1 is false");
43 ok("abc" == "abc", "\"abc\" == \"abc\" is false");
44 ok(true == true, "true == true is false");
45 ok(null == null, "null == null is false");
46 ok(undefined == undefined, "undefined == undefined is false");
47 ok(undefined == null, "undefined == null is false");
48 ok(true == 1, "true == 1 is false");
49 ok(!(true == 2), "true == 2");
50 ok(0 == false, "0 == false is false");
52 ok(1 != 2, "1 != 2 is false");
53 ok(false != 1, "false != 1 is false");
55 var trueVar = true;
56 ok(trueVar, "trueVar is not true");
58 ok(ScriptEngine.length === 0, "ScriptEngine.length is not 0");
60 function testFunc1(x, y) {
61     ok(this !== undefined, "this is undefined");
62     ok(x === true, "x is not 1");
63     ok(y === "test", "y is not \"test\"");
64     ok(arguments.length === 2, "arguments.length is not 2");
65     ok(arguments["0"] === true, "arguments[0] is not true");
66     ok(arguments["1"] === "test", "arguments[1] is not \"test\"");
68     return true;
71 ok(testFunc1.length === 2, "testFunc1.length is not 2");
73 ok(Object.prototype !== undefined, "Object.prototype is undefined");
74 ok(Object.prototype.prototype === undefined, "Object.prototype is not undefined");
75 ok(String.prototype !== undefined, "String.prototype is undefined");
76 ok(Array.prototype !== undefined, "Array.prototype is undefined");
77 ok(Boolean.prototype !== undefined, "Boolean.prototype is undefined");
78 ok(Number.prototype !== undefined, "Number.prototype is undefined");
79 ok(RegExp.prototype !== undefined, "RegExp.prototype is undefined");
80 ok(Math !== undefined, "Math is undefined");
81 ok(Math.prototype === undefined, "Math.prototype is not undefined");
83 ok(typeof(0) === "number", "typeof(0) is not number");
84 ok(typeof(1.5) === "number", "typeof(1.5) is not number");
85 ok(typeof("abc") === "string", "typeof(\"abc\") is not string");
86 ok(typeof("") === "string", "typeof(\"\") is not string");
87 ok(typeof(true) === "boolean", "typeof(true) is not boolean");
88 ok(typeof(null) === "object", "typeof(null) is not object");
89 ok(typeof(undefined) === "undefined", "typeof(undefined) is not undefined");
90 ok(typeof(Math) === "object", "typeof(Math) is not object");
91 ok(typeof(String.prototype) === "object", "typeof(String.prototype) is not object");
92 ok(typeof(testFunc1) === "function", "typeof(testFunc1) is not function");
93 ok(typeof(String) === "function", "typeof(String) is not function");
94 ok(typeof(ScriptEngine) === "function", "typeof(ScriptEngine) is not function");
95 ok(typeof(this) === "object", "typeof(this) is not object");
97 ok(testFunc1(true, "test") === true, "testFunc1 not returned true");
99 var obj1 = new Object();
100 ok(typeof(obj1) === "object", "typeof(obj1) is not object");
101 obj1.test = true;
102 obj1.func = function () {
103     ok(this === obj1, "this is not obj1");
104     ok(this.test === true, "this.test is not true");
105     ok(arguments.length === 1, "arguments.length is not 1");
106     ok(arguments["0"] === true, "arguments[0] is not true");
108     return "test";
111 ok(obj1.func(true) === "test", "obj1.func(true) is not \"test\"");
113 function testConstr1() {
114     this.var1 = 1;
116     ok(this !== undefined, "this is undefined");
117     ok(arguments.length === 1, "arguments.length is not 1");
118     ok(arguments["0"] === true, "arguments[0] is not 1");
120     return false;
123 testConstr1.prototype.pvar = 1;
125 var obj2 = new testConstr1(true);
126 ok(typeof(obj2) === "object", "typeof(obj2) is not object");
127 ok(obj2.pvar === 1, "obj2.pvar is not 1");
129 testConstr1.prototype.pvar = 2;
130 ok(obj2.pvar === 2, "obj2.pvar is not 2");
132 obj2.pvar = 3;
133 testConstr1.prototype.pvar = 1;
134 ok(obj2.pvar === 3, "obj2.pvar is not 3");
136 var obj3 = new Object;
137 ok(typeof(obj3) === "object", "typeof(obj3) is not object");
139 tmp = 0;
140 if(true)
141     tmp = 1;
142 else
143     ok(false, "else evaluated");
144 ok(tmp === 1, "tmp !== 1, if not evaluated?");
146 tmp = 0;
147 if(1 === 0)
148     ok(false, "if evaluated");
149 else
150     tmp = 1;
151 ok(tmp === 1, "tmp !== 1, if not evaluated?");
153 if(false)
154     ok(false, "if(false) evaluated");
156 tmp = 0;
157 if(true)
158     tmp = 1;
159 ok(tmp === 1, "tmp !== 1, if(true) not evaluated?");
161 var obj3 = { prop1: 1,  prop2: typeof(false) };
162 ok(obj3.prop1 === 1, "obj3.prop1 is not 1");
163 ok(obj3.prop2 === "boolean", "obj3.prop2 is not \"boolean\"");
166     var blockVar = 1;
167     blockVar = 2;
169 ok(blockVar === 2, "blockVar !== 2");
171 ok((true ? 1 : 2) === 1, "conditional expression true is not 1");
172 ok((0 === 2 ? 1 : 2) === 2, "conditional expression true is not 2");
174 ok(getVT(undefined) === "VT_EMPTY", "getVT(undefined) is not VT_EMPTY");
175 ok(getVT(null) === "VT_NULL", "getVT(null) is not VT_NULL");
176 ok(getVT(0) === "VT_I4", "getVT(0) is not VT_I4");
177 ok(getVT(0.5) === "VT_R8", "getVT(1.5) is not VT_R8");
178 ok(getVT("test") === "VT_BSTR", "getVT(\"test\") is not VT_BSTR");
179 ok(getVT(Math) === "VT_DISPATCH", "getVT(Math) is not VT_DISPATCH");
180 ok(getVT(false) === "VT_BOOL", "getVT(false) is not VT_BOOL");
182 tmp = 2+2;
183 ok(tmp === 4, "2+2 !== 4");
184 ok(getVT(tmp) === "VT_I4", "getVT(2+2) !== VT_I4");
186 tmp = 2+2.5;
187 ok(tmp === 4.5, "2+2.5 !== 4.5");
188 ok(getVT(tmp) === "VT_R8", "getVT(2+2.5) !== VT_R8");
190 tmp = 1.5+2.5;
191 ok(tmp === 4, "1.4+2.5 !== 4");
192 ok(getVT(tmp) === "VT_I4", "getVT(1.5+2.5) !== VT_I4");
194 tmp = 4-2;
195 ok(tmp === 2, "4-2 !== 2");
196 ok(getVT(tmp) === "VT_I4", "getVT(4-2) !== VT_I4");
198 tmp = 4.5-2;
199 ok(tmp === 2.5, "4.5-2 !== 2.5");
200 ok(getVT(tmp) === "VT_R8", "getVT(4-2) !== VT_R8");
202 tmp = -2;
203 ok(tmp === 0-2, "-2 !== 0-2");
204 ok(getVT(tmp) === "VT_I4", "getVT(-2) !== VT_I4");
206 tmp = 2*3;
207 ok(tmp === 6, "2*3 !== 6");
208 ok(getVT(tmp) === "VT_I4", "getVT(2*3) !== VT_I4");
210 tmp = 2*3.5;
211 ok(tmp === 7, "2*3.5 !== 7");
212 ok(getVT(tmp) === "VT_I4", "getVT(2*3.5) !== VT_I4");
214 tmp = 2.5*3.5;
215 ok(tmp === 8.75, "2.5*3.5 !== 8.75");
216 ok(getVT(tmp) === "VT_R8", "getVT(2.5*3.5) !== VT_R8");
218 tmp = 4/2;
219 ok(tmp === 2, "4/2 !== 2");
220 ok(getVT(tmp) === "VT_I4", "getVT(4/2) !== VT_I4");
222 tmp = 4.5/1.5;
223 ok(tmp === 3, "4.5/1.5 !== 3");
224 ok(getVT(tmp) === "VT_I4", "getVT(4.5/1.5) !== VT_I4");
226 tmp = 3/2;
227 ok(tmp === 1.5, "3/2 !== 1.5");
228 ok(getVT(tmp) === "VT_R8", "getVT(3/2) !== VT_R8");
230 tmp = "ab" + "cd";
231 ok(tmp === "abcd", "\"ab\" + \"cd\" !== \"abcd\"");
233 tmp = 1;
234 ok((tmp += 1) === 2, "tmp += 1 !== 2");
235 ok(tmp === 2, "tmp !== 2");
237 tmp = 2;
238 ok((tmp -= 1) === 1, "tmp -= 1 !== 1");
239 ok(tmp === 1, "tmp !=== 1");
241 tmp = 2;
242 ok((tmp *= 1.5) === 3, "tmp *= 1.5 !== 3");
243 ok(tmp === 3, "tmp !=== 3");
245 tmp = 5;
246 ok((tmp /= 2) === 2.5, "tmp /= 2 !== 2.5");
247 ok(tmp === 2.5, "tmp !=== 2.5");
249 tmp = 3 || ok(false, "second or expression called");
250 ok(tmp === 3, "3 || (...) is not 3");
252 tmp = false || 2;
253 ok(tmp === 2, "false || 2 is not 2");
255 tmp = 0 && ok(false, "second and expression called");
256 ok(tmp === 0, "0 && (...) is not 0");
258 tmp = true && "test";
259 ok(tmp === "test", "true && \"test\" is not \"test\"");
261 tmp = true && 0;
262 ok(tmp === 0, "true && 0 is not 0");
264 tmp = 3 | 4;
265 ok(tmp === 7, "3 | 4 !== 7");
266 ok(getVT(tmp) === "VT_I4", "getVT(3|4) = " + getVT(tmp));
268 tmp = 3.5 | 0;
269 ok(tmp === 3, "3.5 | 0 !== 3");
270 ok(getVT(tmp) === "VT_I4", "getVT(3.5|0) = " + getVT(tmp));
272 tmp = -3.5 | 0;
273 ok(tmp === -3, "-3.5 | 0 !== -3");
274 ok(getVT(tmp) === "VT_I4", "getVT(3.5|0) = " + getVT(tmp));
276 tmp = 10;
277 ok((tmp |= 0x10) === 26, "tmp(10) |= 0x10 !== 26");
278 ok(getVT(tmp) === "VT_I4", "getVT(tmp |= 10) = " + getVT(tmp));
280 tmp = 3 & 5;
281 ok(tmp === 1, "3 & 5 !== 1");
282 ok(getVT(tmp) === "VT_I4", "getVT(3|5) = " + getVT(tmp));
284 tmp = 3.5 & 0xffff;
285 ok(tmp === 3, "3.5 & 0xffff !== 3 ");
286 ok(getVT(tmp) === "VT_I4", "getVT(3.5&0xffff) = " + getVT(tmp));
288 tmp = (-3.5) & 0xffffffff;
289 ok(tmp === -3, "-3.5 & 0xffff !== -3");
290 ok(getVT(tmp) === "VT_I4", "getVT(3.5&0xffff) = " + getVT(tmp));
292 tmp = 10;
293 ok((tmp &= 8) === 8, "tmp(10) &= 8 !== 8");
294 ok(getVT(tmp) === "VT_I4", "getVT(tmp &= 8) = " + getVT(tmp));
296 tmp = 0xf0f0^0xff00;
297 ok(tmp === 0x0ff0, "0xf0f0^0xff00 !== 0x0ff0");
298 ok(getVT(tmp) === "VT_I4", "getVT(0xf0f0^0xff00) = " + getVT(tmp));
300 tmp = 5;
301 ok((tmp ^= 3) === 6, "tmp(5) ^= 3 !== 6");
302 ok(getVT(tmp) === "VT_I4", "getVT(tmp ^= 3) = " + getVT(tmp));
304 tmp = ~1;
305 ok(tmp === -2, "~1 !== -2");
306 ok(getVT(tmp) === "VT_I4", "getVT(~1) = " + getVT(tmp));
308 ok((3,4) === 4, "(3,4) !== 4");
310 ok(+3 === 3, "+3 !== 3");
311 ok(+true === 1, "+true !== 1");
312 ok(+false === 0, "+false !== 0");
313 ok(+null === 0, "+null !== 0");
315 ok("" + 0 === "0", "\"\" + 0 !== \"0\"");
316 ok("" + 123 === "123", "\"\" + 123 !== \"123\"");
317 ok("" + (-5) === "-5", "\"\" + (-5) !== \"-5\"");
319 ok(1 < 3.4, "1 < 3.4 failed");
320 ok(!(3.4 < 1), "3.4 < 1");
321 ok("abc" < "abcd", "abc < abcd failed");
322 ok("abcd" < "abce", "abce < abce failed");
323 ok("" < "x", "\"\" < \"x\" failed");
324 ok(!(0 < 0), "0 < 0");
326 ok(1 <= 3.4, "1 <= 3.4 failed");
327 ok(!(3.4 <= 1), "3.4 <= 1");
328 ok("abc" <= "abcd", "abc <= abcd failed");
329 ok("abcd" <= "abce", "abce <= abce failed");
330 ok("" <= "x", "\"\" <= \"x\" failed");
331 ok(0 <= 0, "0 <= 0 failed");
333 ok(3.4 > 1, "3.4 > 1 failed");
334 ok(!(1 > 3.4), "1 > 3.4");
335 ok("abcd" > "abc", "abc > abcd failed");
336 ok("abce" > "abcd", "abce > abce failed");
337 ok("x" > "", "\"x\" > \"\" failed");
338 ok(!(0 > 0), "0 > 0");
340 ok(3.4 >= 1, "3.4 >= 1 failed");
341 ok(!(1 >= 3.4), "1 >= 3.4");
342 ok("abcd" >= "abc", "abc >= abcd failed");
343 ok("abce" >= "abcd", "abce >= abce failed");
344 ok("x" >= "", "\"x\" >= \"\" failed");
345 ok(0 >= 0, "0 >= 0");
347 tmp = 1;
348 ok(++tmp === 2, "++tmp (1) is not 2");
349 ok(tmp === 2, "incremented tmp is not 2");
350 ok(--tmp === 1, "--tmp (2) is not 1");
351 ok(tmp === 1, "decremented tmp is not 1");
352 ok(tmp++ === 1, "tmp++ (1) is not 1");
353 ok(tmp === 2, "incremented tmp(1) is not 2");
354 ok(tmp-- === 2, "tmp-- (2) is not 2");
355 ok(tmp === 1, "decremented tmp is not 1");
357 String.prototype.test = true;
358 ok("".test === true, "\"\".test is not true");
360 Boolean.prototype.test = true;
361 ok(true.test === true, "true.test is not true");
363 Number.prototype.test = true;
364 ok((0).test === true, "(0).test is not true");
365 ok((0.5).test === true, "(0.5).test is not true");
367 var state = "";
368 try {
369     ok(state === "", "try: state = " + state);
370     state = "try";
371 }catch(ex) {
372     ok(false, "unexpected catch");
374 ok(state === "try", "state = " + state + " expected try");
376 state = "";
377 try {
378     ok(state === "", "try: state = " + state);
379     state = "try";
380 }finally {
381     ok(state === "try", "funally: state = " + state);
382     state = "finally";
384 ok(state === "finally", "state = " + state + " expected finally");
386 state = "";
387 try {
388     ok(state === "", "try: state = " + state);
389     state = "try";
390 }catch(ex) {
391     ok(false, "unexpected catch");
392 }finally {
393     ok(state === "try", "funally: state = " + state);
394     state = "finally";
396 ok(state === "finally", "state = " + state + " expected finally");
398 var state = "";
399 try {
400     ok(state === "", "try: state = " + state);
401     state = "try";
402     throw "except";
403 }catch(ex) {
404     ok(state === "try", "catch: state = " + state);
405     ok(ex === "except", "ex is not \"except\"");
406     state = "catch";
408 ok(state === "catch", "state = " + state + " expected catch");
410 var state = "";
411 try {
412     ok(state === "", "try: state = " + state);
413     state = "try";
414     throw true;
415 }catch(ex) {
416     ok(state === "try", "catch: state = " + state);
417     ok(ex === true, "ex is not true");
418     state = "catch";
419 }finally {
420     ok(state === "catch", "funally: state = " + state);
421     state = "finally";
423 ok(state === "finally", "state = " + state + " expected finally");
425 var state = "";
426 try {
427     ok(state === "", "try: state = " + state);
428     state = "try";
429     try { throw true; } finally {}
430 }catch(ex) {
431     ok(state === "try", "catch: state = " + state);
432     ok(ex === true, "ex is not true");
433     state = "catch";
434 }finally {
435     ok(state === "catch", "funally: state = " + state);
436     state = "finally";
438 ok(state === "finally", "state = " + state + " expected finally");
440 var state = "";
441 try {
442     ok(state === "", "try: state = " + state);
443     state = "try";
444     try { throw "except"; } catch(ex) { throw true; }
445 }catch(ex) {
446     ok(state === "try", "catch: state = " + state);
447     ok(ex === true, "ex is not true");
448     state = "catch";
449 }finally {
450     ok(state === "catch", "funally: state = " + state);
451     state = "finally";
453 ok(state === "finally", "state = " + state + " expected finally");
455 function throwFunc(x) {
456     throw x;
459 var state = "";
460 try {
461     ok(state === "", "try: state = " + state);
462     state = "try";
463     throwFunc(true);
464 }catch(ex) {
465     ok(state === "try", "catch: state = " + state);
466     ok(ex === true, "ex is not true");
467     state = "catch";
468 }finally {
469     ok(state === "catch", "funally: state = " + state);
470     state = "finally";
472 ok(state === "finally", "state = " + state + " expected finally");
474 state = "";
475 switch(1) {
476 case "1":
477     ok(false, "unexpected case \"1\"");
478 case 1:
479     ok(state === "", "case 1: state = " + state);
480     state = "1";
481 default:
482     ok(state === "1", "default: state = " + state);
483     state = "default";
484 case false:
485     ok(state === "default", "case false: state = " + state);
486     state = "false";
488 ok(state === "false", "state = " + state);
490 state = "";
491 switch("") {
492 case "1":
493 case 1:
494     ok(false, "unexpected case 1");
495 default:
496     ok(state === "", "default: state = " + state);
497     state = "default";
498 case false:
499     ok(state === "default", "case false: state = " + state);
500     state = "false";
502 ok(state === "false", "state = " + state);
504 state = "";
505 switch(1) {
506 case "1":
507     ok(false, "unexpected case \"1\"");
508 case 1:
509     ok(state === "", "case 1: state = " + state);
510     state = "1";
511 default:
512     ok(state === "1", "default: state = " + state);
513     state = "default";
514     break;
515 case false:
516     ok(false, "unexpected case false");
518 ok(state === "default", "state = " + state);
520 tmp = eval("1");
521 ok(tmp === 1, "eval(\"1\") !== 1");
522 eval("{ ok(tmp === 1, 'eval: tmp !== 1'); } tmp = 2;");
523 ok(tmp === 2, "tmp !== 2");
525 ok(eval(false) === false, "eval(false) !== false");
526 ok(eval() === undefined, "eval() !== undefined");
528 tmp = eval("1", "2");
529 ok(tmp === 1, "eval(\"1\", \"2\") !== 1");
531 var state = "";
532 try {
533     ok(state === "", "try: state = " + state);
534     state = "try";
535     eval("throwFunc(true);");
536 }catch(ex) {
537     ok(state === "try", "catch: state = " + state);
538     ok(ex === true, "ex is not true");
539     state = "catch";
540 }finally {
541     ok(state === "catch", "funally: state = " + state);
542     state = "finally";
544 ok(state === "finally", "state = " + state + " expected finally");
546 tmp = [,,1,2,,,true];
547 ok(tmp.length === 7, "tmp.length !== 7");
548 ok(tmp["0"] === undefined, "tmp[0] is not undefined");
549 ok(tmp["3"] === 2, "tmp[3] !== 2");
550 ok(tmp["6"] === true, "tmp[6] !== true");
551 ok(tmp[2] === 1, "tmp[2] !== 1");
553 tmp = 0;
554 while(tmp < 4) {
555     ok(tmp < 4, "tmp >= 4");
556     tmp++;
558 ok(tmp === 4, "tmp !== 4");
560 tmp = 0;
561 while(true) {
562     ok(tmp < 4, "tmp >= 4");
563     tmp++;
564     if(tmp === 4) {
565         break;
566         ok(false, "break did not break");
567     }
569 ok(tmp === 4, "tmp !== 4");
571 tmp = 0;
572 do {
573     ok(tmp < 4, "tmp >= 4");
574     tmp++;
575 } while(tmp < 4);
576 ok(tmp === 4, "tmp !== 4");
578 tmp = 0;
579 do {
580     ok(tmp === 0, "tmp !=== 0");
581     tmp++;
582 } while(false);
583 ok(tmp === 1, "tmp !== 4");
585 tmp = 0;
586 while(tmp < 4) {
587     tmp++;
588     if(tmp === 2) {
589         continue;
590         ok(false, "break did not break");
591     }
592     ok(tmp <= 4 && tmp != 2, "tmp = " + tmp);
594 ok(tmp === 4, "tmp !== 4");
596 reportSuccess();