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