jscript: Don't unescape '\v'.
[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 ok(1E0 === 1, "1E0 === 1 is false");
37 ok(1000000*1000000 === 1000000000000, "1000000*1000000 === 1000000000000 is false");
38 ok(8.64e15 === 8640000000000000, "8.64e15 !== 8640000000000000");
39 ok(1e2147483648 === Infinity, "1e2147483648 !== Infinity");
40
41 ok(1 !== 2, "1 !== 2 is false");
42 ok(null !== undefined, "null !== undefined is false");
43
44 ok(1 == 1, "1 == 1 is false");
45 ok(!(1 == 2), "!(1 == 2) is false");
46 ok(1.0 == 1, "1.0 == 1 is false");
47 ok("abc" == "abc", "\"abc\" == \"abc\" is false");
48 ok(true == true, "true == true is false");
49 ok(null == null, "null == null is false");
50 ok(undefined == undefined, "undefined == undefined is false");
51 ok(undefined == null, "undefined == null is false");
52 ok(true == 1, "true == 1 is false");
53 ok(!(true == 2), "true == 2");
54 ok(0 == false, "0 == false is false");
55
56 ok(1 != 2, "1 != 2 is false");
57 ok(false != 1, "false != 1 is false");
58
59 var trueVar = true;
60 ok(trueVar, "trueVar is not true");
61
62 ok(ScriptEngine.length === 0, "ScriptEngine.length is not 0");
63
64 function testFunc1(x, y) {
65     ok(this !== undefined, "this is undefined");
66     ok(x === true, "x is not true");
67     ok(y === "test", "y is not \"test\"");
68     ok(arguments.length === 2, "arguments.length is not 2");
69     ok(arguments["0"] === true, "arguments[0] is not true");
70     ok(arguments["1"] === "test", "arguments[1] is not \"test\"");
71     ok(arguments.callee === testFunc1, "arguments.calee !== testFunc1");
72     ok(testFunc1.arguments === arguments, "testFunc1.arguments = " + testFunc1.arguments);
73
74     return true;
75 }
76
77 ok(testFunc1.length === 2, "testFunc1.length is not 2");
78 ok(testFunc1.arguments === null, "testFunc1.arguments = " + testFunc1.arguments);
79
80 ok(Object.prototype !== undefined, "Object.prototype is undefined");
81 ok(Object.prototype.prototype === undefined, "Object.prototype is not undefined");
82 ok(String.prototype !== undefined, "String.prototype is undefined");
83 ok(Array.prototype !== undefined, "Array.prototype is undefined");
84 ok(Boolean.prototype !== undefined, "Boolean.prototype is undefined");
85 ok(Number.prototype !== undefined, "Number.prototype is undefined");
86 ok(RegExp.prototype !== undefined, "RegExp.prototype is undefined");
87 ok(Math !== undefined, "Math is undefined");
88 ok(Math.prototype === undefined, "Math.prototype is not undefined");
89 ok(Function.prototype !== undefined, "Function.prototype is undefined");
90 ok(Function.prototype.prototype === undefined, "Function.prototype.prototype is not undefined");
91 ok(Date.prototype !== undefined, "Date.prototype is undefined");
92 ok(Date.prototype.prototype === undefined, "Date.prototype is not undefined");
93
94 Function.prototype.test = true;
95 ok(testFunc1.test === true, "testFunc1.test !== true");
96 ok(Function.test === true, "Function.test !== true");
97
98 ok(typeof(0) === "number", "typeof(0) is not number");
99 ok(typeof(1.5) === "number", "typeof(1.5) is not number");
100 ok(typeof("abc") === "string", "typeof(\"abc\") is not string");
101 ok(typeof("") === "string", "typeof(\"\") is not string");
102 ok(typeof(true) === "boolean", "typeof(true) is not boolean");
103 ok(typeof(null) === "object", "typeof(null) is not object");
104 ok(typeof(undefined) === "undefined", "typeof(undefined) is not undefined");
105 ok(typeof(Math) === "object", "typeof(Math) is not object");
106 ok(typeof(String.prototype) === "object", "typeof(String.prototype) is not object");
107 ok(typeof(testFunc1) === "function", "typeof(testFunc1) is not function");
108 ok(typeof(String) === "function", "typeof(String) is not function");
109 ok(typeof(ScriptEngine) === "function", "typeof(ScriptEngine) is not function");
110 ok(typeof(this) === "object", "typeof(this) is not object");
111
112 ok(testFunc1(true, "test") === true, "testFunc1 not returned true");
113
114 ok(testFunc1.arguments === null, "testFunc1.arguments = " + testFunc1.arguments);
115
116 function testRecFunc(x) {
117     ok(testRecFunc.arguments === arguments, "testRecFunc.arguments = " + testRecFunc.arguments);
118     if(x) {
119         testRecFunc(false);
120         ok(testRecFunc.arguments === arguments, "testRecFunc.arguments = " + testRecFunc.arguments);
121         ok(testRecFunc.arguments[0] === true, "testRecFunc.arguments.x = " + testRecFunc.arguments[0]);
122     }
123 }
124
125 testRecFunc.arguments = 5;
126 ok(testRecFunc.arguments === null, "testRecFunc.arguments = " + testRecFunc.arguments);
127 testRecFunc(true);
128 ok(testRecFunc.arguments === null, "testRecFunc.arguments = " + testRecFunc.arguments);
129
130 tmp = (function() {1;})();
131 ok(tmp === undefined, "tmp = " + tmp);
132 tmp = eval("1;");
133 ok(tmp === 1, "tmp = " + tmp);
134
135 var obj1 = new Object();
136 ok(typeof(obj1) === "object", "typeof(obj1) is not object");
137 ok(obj1.constructor === Object, "unexpected obj1.constructor");
138 obj1.test = true;
139 obj1.func = function () {
140     ok(this === obj1, "this is not obj1");
141     ok(this.test === true, "this.test is not true");
142     ok(arguments.length === 1, "arguments.length is not 1");
143     ok(arguments["0"] === true, "arguments[0] is not true");
144     ok(typeof(arguments.callee) === "function", "typeof(arguments.calee) = " + typeof(arguments.calee));
145
146     return "test";
147 };
148
149 ok(obj1.func(true) === "test", "obj1.func(true) is not \"test\"");
150
151 function testConstr1() {
152     this.var1 = 1;
153
154     ok(this !== undefined, "this is undefined");
155     ok(arguments.length === 1, "arguments.length is not 1");
156     ok(arguments["0"] === true, "arguments[0] is not 1");
157     ok(arguments.callee === testConstr1, "arguments.calee !== testConstr1");
158
159     return false;
160 }
161
162 testConstr1.prototype.pvar = 1;
163
164 var obj2 = new testConstr1(true);
165 ok(typeof(obj2) === "object", "typeof(obj2) is not object");
166 ok(obj2.constructor === testConstr1, "unexpected obj2.constructor");
167 ok(obj2.pvar === 1, "obj2.pvar is not 1");
168
169 testConstr1.prototype.pvar = 2;
170 ok(obj2.pvar === 2, "obj2.pvar is not 2");
171
172 obj2.pvar = 3;
173 testConstr1.prototype.pvar = 1;
174 ok(obj2.pvar === 3, "obj2.pvar is not 3");
175
176 obj1 = new Object();
177 function testConstr3() {
178     return obj1;
179 }
180
181 obj2 = new testConstr3();
182 ok(obj1 === obj2, "obj1 != obj2");
183
184 function testConstr4() {
185     return 2;
186 }
187
188 obj2 = new testConstr3();
189 ok(typeof(obj2) === "object", "typeof(obj2) = " + typeof(obj2));
190
191 var obj3 = new Object;
192 ok(typeof(obj3) === "object", "typeof(obj3) is not object");
193
194 for(var iter in "test")
195     ok(false, "unexpected forin call, test = " + iter);
196
197 for(var iter in null)
198     ok(false, "unexpected forin call, test = " + iter);
199
200 for(var iter in false)
201     ok(false, "unexpected forin call, test = " + iter);
202
203 tmp = 0;
204 if(true)
205     tmp = 1;
206 else
207     ok(false, "else evaluated");
208 ok(tmp === 1, "tmp !== 1, if not evaluated?");
209
210 tmp = 0;
211 if(1 === 0)
212     ok(false, "if evaluated");
213 else
214     tmp = 1;
215 ok(tmp === 1, "tmp !== 1, if not evaluated?");
216
217 if(false)
218     ok(false, "if(false) evaluated");
219
220 tmp = 0;
221 if(true)
222     tmp = 1;
223 ok(tmp === 1, "tmp !== 1, if(true) not evaluated?");
224
225 if(false) {
226 }else {
227 }
228
229 var obj3 = { prop1: 1,  prop2: typeof(false) };
230 ok(obj3.prop1 === 1, "obj3.prop1 is not 1");
231 ok(obj3.prop2 === "boolean", "obj3.prop2 is not \"boolean\"");
232 ok(obj3.constructor === Object, "unexpected obj3.constructor");
233
234 {
235     var blockVar = 1;
236     blockVar = 2;
237 }
238 ok(blockVar === 2, "blockVar !== 2");
239
240 ok((true ? 1 : 2) === 1, "conditional expression true is not 1");
241 ok((0 === 2 ? 1 : 2) === 2, "conditional expression true is not 2");
242
243 ok(getVT(undefined) === "VT_EMPTY", "getVT(undefined) is not VT_EMPTY");
244 ok(getVT(null) === "VT_NULL", "getVT(null) is not VT_NULL");
245 ok(getVT(0) === "VT_I4", "getVT(0) is not VT_I4");
246 ok(getVT(0.5) === "VT_R8", "getVT(1.5) is not VT_R8");
247 ok(getVT("test") === "VT_BSTR", "getVT(\"test\") is not VT_BSTR");
248 ok(getVT(Math) === "VT_DISPATCH", "getVT(Math) is not VT_DISPATCH");
249 ok(getVT(false) === "VT_BOOL", "getVT(false) is not VT_BOOL");
250
251 tmp = 2+2;
252 ok(tmp === 4, "2+2 !== 4");
253 ok(getVT(tmp) === "VT_I4", "getVT(2+2) !== VT_I4");
254
255 tmp = 2+2.5;
256 ok(tmp === 4.5, "2+2.5 !== 4.5");
257 ok(getVT(tmp) === "VT_R8", "getVT(2+2.5) !== VT_R8");
258
259 tmp = 1.5+2.5;
260 ok(tmp === 4, "1.4+2.5 !== 4");
261 ok(getVT(tmp) === "VT_I4", "getVT(1.5+2.5) !== VT_I4");
262
263 tmp = 4-2;
264 ok(tmp === 2, "4-2 !== 2");
265 ok(getVT(tmp) === "VT_I4", "getVT(4-2) !== VT_I4");
266
267 tmp = 4.5-2;
268 ok(tmp === 2.5, "4.5-2 !== 2.5");
269 ok(getVT(tmp) === "VT_R8", "getVT(4-2) !== VT_R8");
270
271 tmp = -2;
272 ok(tmp === 0-2, "-2 !== 0-2");
273 ok(getVT(tmp) === "VT_I4", "getVT(-2) !== VT_I4");
274
275 tmp = 2*3;
276 ok(tmp === 6, "2*3 !== 6");
277 ok(getVT(tmp) === "VT_I4", "getVT(2*3) !== VT_I4");
278
279 tmp = 2*3.5;
280 ok(tmp === 7, "2*3.5 !== 7");
281 ok(getVT(tmp) === "VT_I4", "getVT(2*3.5) !== VT_I4");
282
283 tmp = 2.5*3.5;
284 /* FIXME: the parser loses precision */
285 /* ok(tmp === 8.75, "2.5*3.5 !== 8.75"); */
286 ok(tmp > 8.749999 && tmp < 8.750001, "2.5*3.5 !== 8.75");
287 ok(getVT(tmp) === "VT_R8", "getVT(2.5*3.5) !== VT_R8");
288
289 tmp = 4/2;
290 ok(tmp === 2, "4/2 !== 2");
291 ok(getVT(tmp) === "VT_I4", "getVT(4/2) !== VT_I4");
292
293 tmp = 4.5/1.5;
294 ok(tmp === 3, "4.5/1.5 !== 3");
295 ok(getVT(tmp) === "VT_I4", "getVT(4.5/1.5) !== VT_I4");
296
297 tmp = 3/2;
298 ok(tmp === 1.5, "3/2 !== 1.5");
299 ok(getVT(tmp) === "VT_R8", "getVT(3/2) !== VT_R8");
300
301 tmp = 3%2;
302 ok(tmp === 1, "3%2 = " + tmp);
303
304 tmp = 4%2;
305 ok(tmp ===0, "4%2 = " + tmp);
306
307 tmp = 3.5%1.5;
308 ok(tmp === 0.5, "3.5%1.5 = " + tmp);
309
310 tmp = 3%true;
311 ok(tmp === 0, "3%true = " + tmp);
312
313 tmp = "ab" + "cd";
314 ok(tmp === "abcd", "\"ab\" + \"cd\" !== \"abcd\"");
315
316 tmp = 1;
317 ok((tmp += 1) === 2, "tmp += 1 !== 2");
318 ok(tmp === 2, "tmp !== 2");
319
320 tmp = 2;
321 ok((tmp -= 1) === 1, "tmp -= 1 !== 1");
322 ok(tmp === 1, "tmp !=== 1");
323
324 tmp = 2;
325 ok((tmp *= 1.5) === 3, "tmp *= 1.5 !== 3");
326 ok(tmp === 3, "tmp !=== 3");
327
328 tmp = 5;
329 ok((tmp /= 2) === 2.5, "tmp /= 2 !== 2.5");
330 ok(tmp === 2.5, "tmp !=== 2.5");
331
332 tmp = 3;
333 ok((tmp %= 2) === 1, "tmp %= 2 !== 1");
334 ok(tmp === 1, "tmp !== 1");
335
336 tmp = 8;
337 ok((tmp <<= 1) === 16, "tmp <<= 1 !== 16");
338
339 tmp = 8;
340 ok((tmp >>= 1) === 4, "tmp >>= 1 !== 4");
341
342 tmp = 8;
343 ok((tmp >>>= 1) === 4, "tmp >>>= 1 !== 4");
344
345 tmp = 3 || ok(false, "second or expression called");
346 ok(tmp === 3, "3 || (...) is not 3");
347
348 tmp = false || 2;
349 ok(tmp === 2, "false || 2 is not 2");
350
351 tmp = 0 && ok(false, "second and expression called");
352 ok(tmp === 0, "0 && (...) is not 0");
353
354 tmp = true && "test";
355 ok(tmp === "test", "true && \"test\" is not \"test\"");
356
357 tmp = true && 0;
358 ok(tmp === 0, "true && 0 is not 0");
359
360 tmp = 3 | 4;
361 ok(tmp === 7, "3 | 4 !== 7");
362 ok(getVT(tmp) === "VT_I4", "getVT(3|4) = " + getVT(tmp));
363
364 tmp = 3.5 | 0;
365 ok(tmp === 3, "3.5 | 0 !== 3");
366 ok(getVT(tmp) === "VT_I4", "getVT(3.5|0) = " + getVT(tmp));
367
368 tmp = -3.5 | 0;
369 ok(tmp === -3, "-3.5 | 0 !== -3");
370 ok(getVT(tmp) === "VT_I4", "getVT(3.5|0) = " + getVT(tmp));
371
372 tmp = 0 | NaN;
373 ok(tmp === 0, "0 | NaN = " + tmp);
374
375 tmp = 0 | Infinity;
376 ok(tmp === 0, "0 | NaN = " + tmp);
377
378 tmp = 0 | (-Infinity);
379 ok(tmp === 0, "0 | NaN = " + tmp);
380
381 tmp = 10;
382 ok((tmp |= 0x10) === 26, "tmp(10) |= 0x10 !== 26");
383 ok(getVT(tmp) === "VT_I4", "getVT(tmp |= 10) = " + getVT(tmp));
384
385 tmp = 3 & 5;
386 ok(tmp === 1, "3 & 5 !== 1");
387 ok(getVT(tmp) === "VT_I4", "getVT(3|5) = " + getVT(tmp));
388
389 tmp = 3.5 & 0xffff;
390 ok(tmp === 3, "3.5 & 0xffff !== 3 ");
391 ok(getVT(tmp) === "VT_I4", "getVT(3.5&0xffff) = " + getVT(tmp));
392
393 tmp = (-3.5) & 0xffffffff;
394 ok(tmp === -3, "-3.5 & 0xffff !== -3");
395 ok(getVT(tmp) === "VT_I4", "getVT(3.5&0xffff) = " + getVT(tmp));
396
397 tmp = 2 << 3;
398 ok(tmp === 16, "2 << 3 = " + tmp);
399
400 tmp = 2 << 35;
401 ok(tmp === 16, "2 << 35 = " + tmp);
402
403 tmp = 8 >> 2;
404 ok(tmp === 2, "8 >> 2 = " + tmp);
405
406 tmp = -64 >> 4;
407 ok(tmp === -4, "-64 >> 4 = " + tmp);
408
409 tmp = 8 >>> 2;
410 ok(tmp === 2, "8 >> 2 = " + tmp);
411
412 tmp = -64 >>> 4;
413 ok(tmp === 0x0ffffffc, "-64 >>> 4 = " + tmp);
414
415 tmp = 4 >>> NaN;
416 ok(tmp === 4, "4 >>> NaN = " + tmp);
417
418 tmp = 10;
419 ok((tmp &= 8) === 8, "tmp(10) &= 8 !== 8");
420 ok(getVT(tmp) === "VT_I4", "getVT(tmp &= 8) = " + getVT(tmp));
421
422 tmp = 0xf0f0^0xff00;
423 ok(tmp === 0x0ff0, "0xf0f0^0xff00 !== 0x0ff0");
424 ok(getVT(tmp) === "VT_I4", "getVT(0xf0f0^0xff00) = " + getVT(tmp));
425
426 tmp = 5;
427 ok((tmp ^= 3) === 6, "tmp(5) ^= 3 !== 6");
428 ok(getVT(tmp) === "VT_I4", "getVT(tmp ^= 3) = " + getVT(tmp));
429
430 tmp = ~1;
431 ok(tmp === -2, "~1 !== -2");
432 ok(getVT(tmp) === "VT_I4", "getVT(~1) = " + getVT(tmp));
433
434 ok((3,4) === 4, "(3,4) !== 4");
435
436 ok(+3 === 3, "+3 !== 3");
437 ok(+true === 1, "+true !== 1");
438 ok(+false === 0, "+false !== 0");
439 ok(+null === 0, "+null !== 0");
440 ok(+"0" === 0, "+'0' !== 0");
441 ok(+"3" === 3, "+'3' !== 3");
442 ok(+"-3" === -3, "+'-3' !== -3");
443 ok(+"0xff" === 255, "+'0xff' !== 255");
444 ok(+"3e3" === 3000, "+'3e3' !== 3000");
445
446 tmp = new Number(1);
447 ok(+tmp === 1, "+(new Number(1)) = " + (+tmp));
448 ok(tmp.constructor === Number, "unexpected tmp.constructor");
449 tmp = new String("1");
450 ok(+tmp === 1, "+(new String('1')) = " + (+tmp));
451 ok(tmp.constructor === String, "unexpected tmp.constructor");
452
453 ok("" + 0 === "0", "\"\" + 0 !== \"0\"");
454 ok("" + 123 === "123", "\"\" + 123 !== \"123\"");
455 ok("" + (-5) === "-5", "\"\" + (-5) !== \"-5\"");
456 ok("" + null === "null", "\"\" + null !== \"null\"");
457 ok("" + undefined === "undefined", "\"\" + undefined !== \"undefined\"");
458 ok("" + true === "true", "\"\" + true !== \"true\"");
459 ok("" + false === "false", "\"\" + false !== \"false\"");
460 ok("" + 0.5 === "0.5", "'' + 0.5 = " + 0.5);
461 ok("" + (-0.5432) === "-0.5432", "'' + (-0.5432) = " + (-0.5432));
462
463 ok(1 < 3.4, "1 < 3.4 failed");
464 ok(!(3.4 < 1), "3.4 < 1");
465 ok("abc" < "abcd", "abc < abcd failed");
466 ok("abcd" < "abce", "abce < abce failed");
467 ok("" < "x", "\"\" < \"x\" failed");
468 ok(!(0 < 0), "0 < 0");
469
470 ok(1 <= 3.4, "1 <= 3.4 failed");
471 ok(!(3.4 <= 1), "3.4 <= 1");
472 ok("abc" <= "abcd", "abc <= abcd failed");
473 ok("abcd" <= "abce", "abce <= abce failed");
474 ok("" <= "x", "\"\" <= \"x\" failed");
475 ok(0 <= 0, "0 <= 0 failed");
476
477 ok(3.4 > 1, "3.4 > 1 failed");
478 ok(!(1 > 3.4), "1 > 3.4");
479 ok("abcd" > "abc", "abc > abcd failed");
480 ok("abce" > "abcd", "abce > abce failed");
481 ok("x" > "", "\"x\" > \"\" failed");
482 ok(!(0 > 0), "0 > 0");
483
484 ok(3.4 >= 1, "3.4 >= 1 failed");
485 ok(!(1 >= 3.4), "1 >= 3.4");
486 ok("abcd" >= "abc", "abc >= abcd failed");
487 ok("abce" >= "abcd", "abce >= abce failed");
488 ok("x" >= "", "\"x\" >= \"\" failed");
489 ok(0 >= 0, "0 >= 0");
490
491 tmp = 1;
492 ok(++tmp === 2, "++tmp (1) is not 2");
493 ok(tmp === 2, "incremented tmp is not 2");
494 ok(--tmp === 1, "--tmp (2) is not 1");
495 ok(tmp === 1, "decremented tmp is not 1");
496 ok(tmp++ === 1, "tmp++ (1) is not 1");
497 ok(tmp === 2, "incremented tmp(1) is not 2");
498 ok(tmp-- === 2, "tmp-- (2) is not 2");
499 ok(tmp === 1, "decremented tmp is not 1");
500
501 String.prototype.test = true;
502 ok("".test === true, "\"\".test is not true");
503
504 Boolean.prototype.test = true;
505 ok(true.test === true, "true.test is not true");
506
507 Number.prototype.test = true;
508 ok((0).test === true, "(0).test is not true");
509 ok((0.5).test === true, "(0.5).test is not true");
510
511 var state = "";
512 try {
513     ok(state === "", "try: state = " + state);
514     state = "try";
515 }catch(ex) {
516     ok(false, "unexpected catch");
517 }
518 ok(state === "try", "state = " + state + " expected try");
519
520 state = "";
521 try {
522     ok(state === "", "try: state = " + state);
523     state = "try";
524 }finally {
525     ok(state === "try", "finally: state = " + state);
526     state = "finally";
527 }
528 ok(state === "finally", "state = " + state + " expected finally");
529
530 state = "";
531 try {
532     ok(state === "", "try: state = " + state);
533     state = "try";
534 }catch(ex) {
535     ok(false, "unexpected catch");
536 }finally {
537     ok(state === "try", "finally: state = " + state);
538     state = "finally";
539 }
540 ok(state === "finally", "state = " + state + " expected finally");
541
542 var state = "";
543 try {
544     ok(state === "", "try: state = " + state);
545     state = "try";
546     throw "except";
547 }catch(ex) {
548     ok(state === "try", "catch: state = " + state);
549     ok(ex === "except", "ex is not \"except\"");
550     state = "catch";
551 }
552 ok(state === "catch", "state = " + state + " expected catch");
553
554 var state = "";
555 try {
556     ok(state === "", "try: state = " + state);
557     state = "try";
558     throw true;
559 }catch(ex) {
560     ok(state === "try", "catch: state = " + state);
561     ok(ex === true, "ex is not true");
562     state = "catch";
563 }finally {
564     ok(state === "catch", "finally: state = " + state);
565     state = "finally";
566 }
567 ok(state === "finally", "state = " + state + " expected finally");
568
569 var state = "";
570 try {
571     ok(state === "", "try: state = " + state);
572     state = "try";
573     try { throw true; } finally {}
574 }catch(ex) {
575     ok(state === "try", "catch: state = " + state);
576     ok(ex === true, "ex is not true");
577     state = "catch";
578 }finally {
579     ok(state === "catch", "finally: state = " + state);
580     state = "finally";
581 }
582 ok(state === "finally", "state = " + state + " expected finally");
583
584 var state = "";
585 try {
586     ok(state === "", "try: state = " + state);
587     state = "try";
588     try { throw "except"; } catch(ex) { throw true; }
589 }catch(ex) {
590     ok(state === "try", "catch: state = " + state);
591     ok(ex === true, "ex is not true");
592     state = "catch";
593 }finally {
594     ok(state === "catch", "finally: state = " + state);
595     state = "finally";
596 }
597 ok(state === "finally", "state = " + state + " expected finally");
598
599 function throwFunc(x) {
600     throw x;
601 }
602
603 var state = "";
604 try {
605     ok(state === "", "try: state = " + state);
606     state = "try";
607     throwFunc(true);
608 }catch(ex) {
609     ok(state === "try", "catch: state = " + state);
610     ok(ex === true, "ex is not true");
611     state = "catch";
612 }finally {
613     ok(state === "catch", "finally: state = " + state);
614     state = "finally";
615 }
616 ok(state === "finally", "state = " + state + " expected finally");
617
618 state = "";
619 switch(1) {
620 case "1":
621     ok(false, "unexpected case \"1\"");
622 case 1:
623     ok(state === "", "case 1: state = " + state);
624     state = "1";
625 default:
626     ok(state === "1", "default: state = " + state);
627     state = "default";
628 case false:
629     ok(state === "default", "case false: state = " + state);
630     state = "false";
631 }
632 ok(state === "false", "state = " + state);
633
634 state = "";
635 switch("") {
636 case "1":
637 case 1:
638     ok(false, "unexpected case 1");
639 default:
640     ok(state === "", "default: state = " + state);
641     state = "default";
642 case false:
643     ok(state === "default", "case false: state = " + state);
644     state = "false";
645 }
646 ok(state === "false", "state = " + state);
647
648 state = "";
649 switch(1) {
650 case "1":
651     ok(false, "unexpected case \"1\"");
652 case 1:
653     ok(state === "", "case 1: state = " + state);
654     state = "1";
655 default:
656     ok(state === "1", "default: state = " + state);
657     state = "default";
658     break;
659 case false:
660     ok(false, "unexpected case false");
661 }
662 ok(state === "default", "state = " + state);
663
664 tmp = eval("1");
665 ok(tmp === 1, "eval(\"1\") !== 1");
666 eval("{ ok(tmp === 1, 'eval: tmp !== 1'); } tmp = 2;");
667 ok(tmp === 2, "tmp !== 2");
668
669 ok(eval(false) === false, "eval(false) !== false");
670 ok(eval() === undefined, "eval() !== undefined");
671
672 tmp = eval("1", "2");
673 ok(tmp === 1, "eval(\"1\", \"2\") !== 1");
674
675 var state = "";
676 try {
677     ok(state === "", "try: state = " + state);
678     state = "try";
679     eval("throwFunc(true);");
680 }catch(ex) {
681     ok(state === "try", "catch: state = " + state);
682     ok(ex === true, "ex is not true");
683     state = "catch";
684 }finally {
685     ok(state === "catch", "finally: state = " + state);
686     state = "finally";
687 }
688 ok(state === "finally", "state = " + state + " expected finally");
689
690 tmp = [,,1,2,,,true];
691 ok(tmp.length === 7, "tmp.length !== 7");
692 ok(tmp["0"] === undefined, "tmp[0] is not undefined");
693 ok(tmp["3"] === 2, "tmp[3] !== 2");
694 ok(tmp["6"] === true, "tmp[6] !== true");
695 ok(tmp[2] === 1, "tmp[2] !== 1");
696
697 ok([1,].length === 2, "[1,].length !== 2");
698 ok([,,].length === 3, "[,,].length !== 3");
699 ok([,].length === 2, "[].length != 2");
700 ok([].length === 0, "[].length != 0");
701
702 tmp = 0;
703 while(tmp < 4) {
704     ok(tmp < 4, "tmp >= 4");
705     tmp++;
706 }
707 ok(tmp === 4, "tmp !== 4");
708
709 tmp = 0;
710 while(true) {
711     ok(tmp < 4, "tmp >= 4");
712     tmp++;
713     if(tmp === 4) {
714         break;
715         ok(false, "break did not break");
716     }
717 }
718 ok(tmp === 4, "tmp !== 4");
719
720 tmp = 0;
721 do {
722     ok(tmp < 4, "tmp >= 4");
723     tmp++;
724 } while(tmp < 4);
725 ok(tmp === 4, "tmp !== 4");
726
727 tmp = 0;
728 do {
729     ok(tmp === 0, "tmp !=== 0");
730     tmp++;
731 } while(false);
732 ok(tmp === 1, "tmp !== 1");
733
734 tmp = 0;
735 do {
736     ok(tmp < 4, "tmp >= 4");
737     tmp++;
738 } while(tmp < 4)
739 ok(tmp === 4, "tmp !== 4")
740
741 tmp = 0;
742 while(tmp < 4) {
743     tmp++;
744     if(tmp === 2) {
745         continue;
746         ok(false, "break did not break");
747     }
748     ok(tmp <= 4 && tmp != 2, "tmp = " + tmp);
749 }
750 ok(tmp === 4, "tmp !== 4");
751
752 for(tmp=0; tmp < 4; tmp++)
753     ok(tmp < 4, "tmp = " + tmp);
754 ok(tmp === 4, "tmp !== 4");
755
756 for(tmp=0; tmp < 4; tmp++) {
757     if(tmp === 2)
758         break;
759     ok(tmp < 2, "tmp = " + tmp);
760 }
761 ok(tmp === 2, "tmp !== 2");
762
763 for(tmp=0; tmp < 4; tmp++) {
764     if(tmp === 2)
765         continue;
766     ok(tmp < 4 && tmp != 2, "tmp = " + tmp);
767 }
768 ok(tmp === 4, "tmp !== 4");
769
770 for(var fi=0; fi < 4; fi++)
771     ok(fi < 4, "fi = " + fi);
772 ok(fi === 4, "fi !== 4");
773
774 ok((void 1) === undefined, "(void 1) !== undefined");
775
776 var inobj = new Object();
777
778 for(var iter in inobj)
779     ok(false, "unexpected iter = " + iter);
780
781 inobj.test = true;
782 tmp = 0;
783 for(iter in inobj) {
784     ok(iter == "test", "unexpected iter = " + iter);
785     tmp++;
786 }
787 ok(tmp === 1, "for..in tmp = " + tmp);
788
789 function forinTestObj() {}
790
791 forinTestObj.prototype.test3 = true;
792
793 var arr = new Array();
794 inobj = new forinTestObj();
795 inobj.test1 = true;
796 inobj.test2 = true;
797
798 tmp = 0;
799 for(iter in inobj) {
800     arr[iter] = true;
801     tmp++;
802 }
803
804 ok(tmp === 3, "for..in tmp = " + tmp);
805 ok(arr["test1"] === true, "arr[test1] !== true");
806 ok(arr["test2"] === true, "arr[test2] !== true");
807 ok(arr["test3"] === true, "arr[test3] !== true");
808
809 tmp = new Object();
810 tmp.test = false;
811 ok((delete tmp.test) === true, "delete returned false");
812 ok(typeof(tmp.test) === "undefined", "tmp.test type = " + typeof(tmp.test));
813 for(iter in tmp)
814     ok(false, "tmp has prop " + iter);
815
816 tmp.testWith = true;
817 with(tmp)
818     ok(testWith === true, "testWith !== true");
819
820 if(false) {
821     var varTest1 = true;
822 }
823
824 ok(varTest1 === undefined, "varTest1 = " + varTest1);
825 ok(varTest2 === undefined, "varTest2 = " + varTest1);
826
827 var varTest2;
828
829 function varTestFunc(varTest3) {
830     var varTest3;
831
832     ok(varTest3 === 3, "varTest3 = " + varTest3);
833     ok(varTest4 === undefined, "varTest4 = " + varTest4);
834
835     var varTest4;
836 }
837
838 varTestFunc(3);
839
840 deleteTest = 1;
841 delete deleteTest;
842 try {
843     tmp = deleteTest;
844     ok(false, "deleteTest not throwed exception?");
845 }catch(ex) {}
846
847 if (false)
848     if (true)
849         ok(false, "if evaluated");
850     else
851         ok(false, "else should be associated with nearest if statement");
852
853 if (true)
854     if (false)
855         ok(false, "if evaluated");
856     else
857         ok(true, "else should be associated with nearest if statement");
858
859 function instanceOfTest() {}
860 tmp = new instanceOfTest();
861
862 ok((tmp instanceof instanceOfTest) === true, "tmp is not instance of instanceOfTest");
863 ok((tmp instanceof Object) === true, "tmp is not instance of Object");
864 ok((tmp instanceof String) === false, "tmp is instance of String");
865
866 instanceOfTest.prototype = new Object();
867 ok((tmp instanceof instanceOfTest) === false, "tmp is instance of instanceOfTest");
868 ok((tmp instanceof Object) === true, "tmp is not instance of Object");
869
870 ok((1 instanceof Object) === false, "1 is instance of Object");
871 ok((false instanceof Boolean) === false, "false is instance of Boolean");
872 ok(("" instanceof Object) === false, "'' is instance of Object");
873
874 (function () {
875     ok((arguments instanceof Object) === true, "argument is not instance of Object");
876     ok((arguments instanceof Array) === false, "argument is not instance of Array");
877     ok(arguments.toString() === "[object Object]", "arguments.toString() = " + arguments.toString());
878 })(1,2);
879
880 obj = new String();
881 ok(("length" in obj) === true, "length is not in obj");
882 ok(("isPrototypeOf" in obj) === true, "isPrototypeOf is not in obj");
883 ok(("abc" in obj) === false, "test is in obj");
884 obj.abc = 1;
885 ok(("abc" in obj) === true, "test is not in obj");
886 ok(("1" in obj) === false, "1 is in obj");
887
888 obj = [1,2,3];
889 ok((1 in obj) === true, "1 is not in obj");
890
891 obj = new Object();
892 try {
893     obj.prop["test"];
894     ok(false, "expected exception");
895 }catch(e) {}
896 ok(!("prop" in obj), "prop in obj");
897
898 ok(isNaN(NaN) === true, "isNaN(NaN) !== true");
899 ok(isNaN(0.5) === false, "isNaN(0.5) !== false");
900 ok(isNaN(Infinity) === false, "isNaN(Infinity) !== false");
901 ok(isNaN() === true, "isNaN() !== true");
902 ok(isNaN(NaN, 0) === true, "isNaN(NaN, 0) !== true");
903 ok(isNaN(0.5, NaN) === false, "isNaN(0.5, NaN) !== false");
904 ok(isNaN(+undefined) === true, "isNaN(+undefined) !== true");
905
906 ok(isFinite(0.5) === true, "isFinite(0.5) !== true");
907 ok(isFinite(Infinity) === false, "isFinite(Infinity) !== false");
908 ok(isFinite(-Infinity) === false, "isFinite(Infinity) !== false");
909 ok(isFinite(NaN) === false, "isFinite(NaN) !== false");
910 ok(isFinite(0.5, NaN) === true, "isFinite(0.5, NaN) !== true");
911 ok(isFinite(NaN, 0.5) === false, "isFinite(NaN, 0.5) !== false");
912 ok(isFinite() === false, "isFinite() !== false");
913
914 ok((1 < NaN) === false, "(1 < NaN) !== false");
915 ok((1 > NaN) === false, "(1 > NaN) !== false");
916 ok((1 <= NaN) === false, "(1 <= NaN) !== false");
917 ok((1 >= NaN) === false, "(1 >= NaN) !== false");
918 ok((NaN < 1) === false, "(NaN < 1) !== false");
919 ok((NaN > 1) === false, "(NaN > 1) !== false");
920 ok((NaN <= 1) === false, "(NaN <= 1) !== false");
921 ok((NaN >= 1) === false, "(NaN >= 1) !== false");
922 ok((Infinity < 2) === false, "(Infinity < 2) !== false");
923 ok((Infinity > 2) === true, "(Infinity > 2) !== true");
924 ok((-Infinity < 2) === true, "(-Infinity < 2) !== true");
925
926 ok(isNaN(+"test") === true, "isNaN(+'test') !== true");
927 ok(isNaN(+"123t") === true, "isNaN(+'123t') !== true");
928 ok(isNaN(+"Infinity x") === true, "isNaN(+'Infinity x') !== true");
929 ok(+"Infinity" === Infinity, "+'Infinity' !== Infinity");
930 ok(+" Infinity " === Infinity, "+' Infinity ' !== Infinity");
931 ok(+"-Infinity" === -Infinity, "+'-Infinity' !== -Infinity");
932
933 ok((NaN !== NaN) === true, "(NaN !== NaN) !== true");
934 ok((NaN === NaN) === false, "(NaN === NaN) !== false");
935 ok((Infinity !== NaN) === true, "(Infinity !== NaN) !== true");
936 ok((Infinity !== NaN) === true, "(Infinity !== NaN) !== true");
937 ok((0 === NaN) === false, "(0 === NaN) !== false");
938
939 ok((NaN != NaN) === true, "(NaN !== NaN) != true");
940 ok((NaN == NaN) === false, "(NaN === NaN) != false");
941 ok((Infinity != NaN) === true, "(Infinity != NaN) !== true");
942 ok((Infinity != NaN) === true, "(Infinity != NaN) !== true");
943 ok((0 == NaN) === false, "(0 === NaN) != false");
944
945 // escape tests
946 var escapeTests = [
947     ["\'", "\\'", 39],
948     ["\"", "\\\"", 34],
949     ["\\", "\\\\", 92],
950     ["\b", "\\b", 8],
951     ["\t", "\\t", 9],
952     ["\n", "\\n", 10],
953     ["\v", "\\v", 118],
954     ["\f", "\\f", 12],
955     ["\r", "\\r", 13],
956     ["\xf3", "\\xf3", 0xf3],
957     ["\u1234", "\\u1234", 0x1234],
958     ["\a", "\\a", 97],
959     ["\?", "\\?", 63]
960 ];
961
962 for(i=0; i<escapeTests.length; i++) {
963     tmp = escapeTests[i][0].charCodeAt(0);
964     ok(tmp === escapeTests[i][2], "escaped '" + escapeTests[i][1] + "' = " + tmp + " expected " + escapeTests[i][2]);
965 }
966
967 tmp = !+"\v1";
968 ok(tmp === true, '!+"\v1" = ' + tmp);
969
970 ok(typeof(testFunc2) === "function", "typeof(testFunc2) = " + typeof(testFunc2));
971 tmp = testFunc2(1);
972 ok(tmp === 2, "testFunc2(1) = " + tmp);
973 function testFunc2(x) { return x+1; }
974
975 ok(typeof(testFunc3) === "function", "typeof(testFunc3) = " + typeof(testFunc3));
976 tmp = testFunc3(1);
977 ok(tmp === 3, "testFunc3(1) = " + tmp);
978 tmp = function testFunc3(x) { return x+2; };
979
980 tmp = testFunc4(1);
981 ok(tmp === 5, "testFunc4(1) = " + tmp);
982 tmp = function testFunc4(x) { return x+3; };
983 tmp = testFunc4(1);
984 testFunc4 = 1;
985 ok(testFunc4 === 1, "testFunc4 = " + testFunc4);
986 ok(tmp === 5, "testFunc4(1) = " + tmp);
987 tmp = function testFunc4(x) { return x+4; };
988 ok(testFunc4 === 1, "testFunc4 = " + testFunc4);
989
990 function testEmbededFunctions() {
991     ok(typeof(testFunc5) === "function", "typeof(testFunc5) = " + typeof(testFunc5));
992     tmp = testFunc5(1);
993     ok(tmp === 3, "testFunc5(1) = " + tmp);
994     tmp = function testFunc5(x) { return x+2; };
995
996     tmp = testFunc6(1);
997     ok(tmp === 5, "testFunc6(1) = " + tmp);
998     tmp = function testFunc6(x) { return x+3; };
999     tmp = testFunc6(1);
1000     ok(tmp === 5, "testFunc6(1) = " + tmp);
1001     tmp = function testFunc6(x) { return x+4; };
1002     testFunc6 = 1;
1003     ok(testFunc6 === 1, "testFunc4 = " + testFunc6);
1004 }
1005
1006 testEmbededFunctions();
1007
1008 date = new Date();
1009 date.toString = function() { return "toString"; }
1010 ok(""+date === "toString", "''+date = " + date);
1011 date.toString = function() { return this; }
1012 ok(""+date === ""+date.valueOf(), "''+date = " + date);
1013
1014 str = new String("test");
1015 str.valueOf = function() { return "valueOf"; }
1016 ok(""+str === "valueOf", "''+str = " + str);
1017 str.valueOf = function() { return new Date(); }
1018 ok(""+str === "test", "''+str = " + str);
1019
1020 ok((function (){return 1;})() === 1, "(function (){return 1;})() = " + (function (){return 1;})());
1021
1022 var re = /=(\?|%3F)/g;
1023 ok(re.source === "=(\\?|%3F)", "re.source = " + re.source);
1024
1025 tmp = new Array();
1026 for(var i=0; i<2; i++)
1027     tmp[i] = /b/;
1028 ok(tmp[0] != tmp[1], "tmp[0] == tmp [1]");
1029
1030 ok(createNullBSTR() === '', "createNullBSTR() !== ''");
1031
1032 ok(getVT(nullDisp) === "VT_DISPATCH", "getVT(nullDisp) = " + getVT(nullDisp));
1033 ok(typeof(nullDisp) === "object", "typeof(nullDisp) = " + typeof(nullDisp));
1034 ok(nullDisp === nullDisp, "nullDisp !== nullDisp");
1035 ok(nullDisp !== re, "nullDisp === re");
1036 ok(nullDisp === null, "nullDisp === null");
1037 ok(nullDisp == null, "nullDisp == null");
1038 ok(getVT(true && nullDisp) === "VT_DISPATCH",
1039    "getVT(0 && nullDisp) = " + getVT(true && nullDisp));
1040 ok(!nullDisp === true, "!nullDisp = " + !nullDisp);
1041 ok(String(nullDisp) === "null", "String(nullDisp) = " + String(nullDisp));
1042 ok(nullDisp != new Object(), "nullDisp == new Object()");
1043 ok(new Object() != nullDisp, "new Object() == nullDisp");
1044 ok((typeof Object(nullDisp)) === "object", "typeof Object(nullDisp) !== 'object'");
1045 tmp = getVT(Object(nullDisp));
1046 ok(tmp === "VT_DISPATCH", "getVT(Object(nullDisp) = " + tmp);
1047 tmp = Object(nullDisp).toString();
1048 ok(tmp === "[object Object]", "Object(nullDisp).toString() = " + tmp);
1049
1050 function do_test() {}
1051 function nosemicolon() {} nosemicolon();
1052 function () {} nosemicolon();
1053
1054 if(false) {
1055     function in_if_false() { return true; } ok(false, "!?");
1056 }
1057
1058 ok(in_if_false(), "in_if_false failed");
1059
1060 ok(typeof(doesnotexist) === "undefined", "typeof(doesnotexist) = " + typeof(doesnotexist));
1061
1062 (function() { newValue = 1; })();
1063 ok(newValue === 1, "newValue = " + newValue);
1064
1065 obj = {undefined: 3};
1066
1067 /* Keep this test in the end of file */
1068 undefined = 6;
1069 ok(undefined === 6, "undefined = " + undefined);
1070
1071 NaN = 6;
1072 ok(NaN === 6, "NaN !== 6");
1073
1074 Infinity = 6;
1075 ok(Infinity === 6, "Infinity !== 6");
1076
1077 Math = 6;
1078 ok(Math === 6, "NaN !== 6");
1079
1080 reportSuccess();