jscript: Added '|' expression implementation.
[wine] / dlls / jscript / tests / lang.js
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 var tmp;
20
21 ok(true, "true is not true?");
22 ok(!false, "!false is not true");
23 ok(!undefined, "!undefined is not true");
24 ok(!null, "!null is not true");
25 ok(!0, "!0 is not true");
26 ok(!0.0, "!0.0 is not true");
27
28 ok(1 === 1, "1 === 1 is false");
29 ok(!(1 === 2), "!(1 === 2) is false");
30 ok(1.0 === 1, "1.0 === 1 is false");
31 ok("abc" === "abc", "\"abc\" === \"abc\" is false");
32 ok(true === true, "true === true is false");
33 ok(null === null, "null === null is false");
34 ok(undefined === undefined, "undefined === undefined is false");
35 ok(!(undefined === null), "!(undefined === null) is false");
36
37 ok(1 !== 2, "1 !== 2 is false");
38 ok(null !== undefined, "null !== undefined is false");
39
40 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");
51
52 ok(1 != 2, "1 != 2 is false");
53 ok(false != 1, "false != 1 is false");
54
55 var trueVar = true;
56 ok(trueVar, "trueVar is not true");
57
58 ok(ScriptEngine.length === 0, "ScriptEngine.length is not 0");
59
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\"");
67
68     return true;
69 }
70
71 ok(testFunc1.length === 2, "testFunc1.length is not 2");
72
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");
82
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");
96
97 ok(testFunc1(true, "test") === true, "testFunc1 not returned true");
98
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");
107
108     return "test";
109 };
110
111 ok(obj1.func(true) === "test", "obj1.func(true) is not \"test\"");
112
113 function testConstr1() {
114     this.var1 = 1;
115
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");
119
120     return false;
121 }
122
123 testConstr1.prototype.pvar = 1;
124
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");
128
129 testConstr1.prototype.pvar = 2;
130 ok(obj2.pvar === 2, "obj2.pvar is not 2");
131
132 obj2.pvar = 3;
133 testConstr1.prototype.pvar = 1;
134 ok(obj2.pvar === 3, "obj2.pvar is not 3");
135
136 var obj3 = new Object;
137 ok(typeof(obj3) === "object", "typeof(obj3) is not object");
138
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?");
145
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?");
152
153 if(false)
154     ok(false, "if(false) evaluated");
155
156 tmp = 0;
157 if(true)
158     tmp = 1;
159 ok(tmp === 1, "tmp !== 1, if(true) not evaluated?");
160
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\"");
164
165 {
166     var blockVar = 1;
167     blockVar = 2;
168 }
169 ok(blockVar === 2, "blockVar !== 2");
170
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");
173
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");
181
182 tmp = 2+2;
183 ok(tmp === 4, "2+2 !== 4");
184 ok(getVT(tmp) === "VT_I4", "getVT(2+2) !== VT_I4");
185
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");
189
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");
193
194 tmp = 4-2;
195 ok(tmp === 2, "4-2 !== 2");
196 ok(getVT(tmp) === "VT_I4", "getVT(4-2) !== VT_I4");
197
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");
201
202 tmp = -2;
203 ok(tmp === 0-2, "-2 !== 0-2");
204 ok(getVT(tmp) === "VT_I4", "getVT(-2) !== VT_I4");
205
206 tmp = 2*3;
207 ok(tmp === 6, "2*3 !== 6");
208 ok(getVT(tmp) === "VT_I4", "getVT(2*3) !== VT_I4");
209
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");
213
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");
217
218 tmp = 4/2;
219 ok(tmp === 2, "4/2 !== 2");
220 ok(getVT(tmp) === "VT_I4", "getVT(4/2) !== VT_I4");
221
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");
225
226 tmp = 3/2;
227 ok(tmp === 1.5, "3/2 !== 1.5");
228 ok(getVT(tmp) === "VT_R8", "getVT(3/2) !== VT_R8");
229
230 tmp = "ab" + "cd";
231 ok(tmp === "abcd", "\"ab\" + \"cd\" !== \"abcd\"");
232
233 tmp = 1;
234 ok((tmp += 1) === 2, "tmp += 1 !== 2");
235 ok(tmp === 2, "tmp !== 2");
236
237 tmp = 2;
238 ok((tmp -= 1) === 1, "tmp -= 1 !== 1");
239 ok(tmp === 1, "tmp !=== 1");
240
241 tmp = 2;
242 ok((tmp *= 1.5) === 3, "tmp *= 1.5 !== 3");
243 ok(tmp === 3, "tmp !=== 3");
244
245 tmp = 5;
246 ok((tmp /= 2) === 2.5, "tmp /= 2 !== 2.5");
247 ok(tmp === 2.5, "tmp !=== 2.5");
248
249 tmp = 3 || ok(false, "second or expression called");
250 ok(tmp === 3, "3 || (...) is not 3");
251
252 tmp = false || 2;
253 ok(tmp === 2, "false || 2 is not 2");
254
255 tmp = 0 && ok(false, "second and expression called");
256 ok(tmp === 0, "0 && (...) is not 0");
257
258 tmp = true && "test";
259 ok(tmp === "test", "true && \"test\" is not \"test\"");
260
261 tmp = true && 0;
262 ok(tmp === 0, "true && 0 is not 0");
263
264 tmp = 3 | 4;
265 ok(tmp === 7, "3 | 4 !== 7");
266 ok(getVT(tmp) === "VT_I4", "getVT(3|4) = " + getVT(tmp));
267
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));
271
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));
275
276 ok(1 < 3.4, "1 < 3.4 failed");
277 ok(!(3.4 < 1), "3.4 < 1");
278 ok("abc" < "abcd", "abc < abcd failed");
279 ok("abcd" < "abce", "abce < abce failed");
280 ok("" < "x", "\"\" < \"x\" failed");
281 ok(!(0 < 0), "0 < 0");
282
283 ok(1 <= 3.4, "1 <= 3.4 failed");
284 ok(!(3.4 <= 1), "3.4 <= 1");
285 ok("abc" <= "abcd", "abc <= abcd failed");
286 ok("abcd" <= "abce", "abce <= abce failed");
287 ok("" <= "x", "\"\" <= \"x\" failed");
288 ok(0 <= 0, "0 <= 0 failed");
289
290 ok(3.4 > 1, "3.4 > 1 failed");
291 ok(!(1 > 3.4), "1 > 3.4");
292 ok("abcd" > "abc", "abc > abcd failed");
293 ok("abce" > "abcd", "abce > abce failed");
294 ok("x" > "", "\"x\" > \"\" failed");
295 ok(!(0 > 0), "0 > 0");
296
297 ok(3.4 >= 1, "3.4 >= 1 failed");
298 ok(!(1 >= 3.4), "1 >= 3.4");
299 ok("abcd" >= "abc", "abc >= abcd failed");
300 ok("abce" >= "abcd", "abce >= abce failed");
301 ok("x" >= "", "\"x\" >= \"\" failed");
302 ok(0 >= 0, "0 >= 0");
303
304 tmp = 1;
305 ok(++tmp === 2, "++tmp (1) is not 2");
306 ok(tmp === 2, "incremented tmp is not 2");
307 ok(--tmp === 1, "--tmp (2) is not 1");
308 ok(tmp === 1, "decremented tmp is not 1");
309 ok(tmp++ === 1, "tmp++ (1) is not 1");
310 ok(tmp === 2, "incremented tmp(1) is not 2");
311 ok(tmp-- === 2, "tmp-- (2) is not 2");
312 ok(tmp === 1, "decremented tmp is not 1");
313
314 String.prototype.test = true;
315 ok("".test === true, "\"\".test is not true");
316
317 Boolean.prototype.test = true;
318 ok(true.test === true, "true.test is not true");
319
320 Number.prototype.test = true;
321 ok((0).test === true, "(0).test is not true");
322 ok((0.5).test === true, "(0.5).test is not true");
323
324 var state = "";
325 try {
326     ok(state === "", "try: state = " + state);
327     state = "try";
328 }catch(ex) {
329     ok(false, "unexpected catch");
330 }
331 ok(state === "try", "state = " + state + " expected try");
332
333 state = "";
334 try {
335     ok(state === "", "try: state = " + state);
336     state = "try";
337 }finally {
338     ok(state === "try", "funally: state = " + state);
339     state = "finally";
340 }
341 ok(state === "finally", "state = " + state + " expected finally");
342
343 state = "";
344 try {
345     ok(state === "", "try: state = " + state);
346     state = "try";
347 }catch(ex) {
348     ok(false, "unexpected catch");
349 }finally {
350     ok(state === "try", "funally: state = " + state);
351     state = "finally";
352 }
353 ok(state === "finally", "state = " + state + " expected finally");
354
355 var state = "";
356 try {
357     ok(state === "", "try: state = " + state);
358     state = "try";
359     throw "except";
360 }catch(ex) {
361     ok(state === "try", "catch: state = " + state);
362     ok(ex === "except", "ex is not \"except\"");
363     state = "catch";
364 }
365 ok(state === "catch", "state = " + state + " expected catch");
366
367 var state = "";
368 try {
369     ok(state === "", "try: state = " + state);
370     state = "try";
371     throw true;
372 }catch(ex) {
373     ok(state === "try", "catch: state = " + state);
374     ok(ex === true, "ex is not true");
375     state = "catch";
376 }finally {
377     ok(state === "catch", "funally: state = " + state);
378     state = "finally";
379 }
380 ok(state === "finally", "state = " + state + " expected finally");
381
382 var state = "";
383 try {
384     ok(state === "", "try: state = " + state);
385     state = "try";
386     try { throw true; } finally {}
387 }catch(ex) {
388     ok(state === "try", "catch: state = " + state);
389     ok(ex === true, "ex is not true");
390     state = "catch";
391 }finally {
392     ok(state === "catch", "funally: state = " + state);
393     state = "finally";
394 }
395 ok(state === "finally", "state = " + state + " expected finally");
396
397 var state = "";
398 try {
399     ok(state === "", "try: state = " + state);
400     state = "try";
401     try { throw "except"; } catch(ex) { throw true; }
402 }catch(ex) {
403     ok(state === "try", "catch: state = " + state);
404     ok(ex === true, "ex is not true");
405     state = "catch";
406 }finally {
407     ok(state === "catch", "funally: state = " + state);
408     state = "finally";
409 }
410 ok(state === "finally", "state = " + state + " expected finally");
411
412 function throwFunc(x) {
413     throw x;
414 }
415
416 var state = "";
417 try {
418     ok(state === "", "try: state = " + state);
419     state = "try";
420     throwFunc(true);
421 }catch(ex) {
422     ok(state === "try", "catch: state = " + state);
423     ok(ex === true, "ex is not true");
424     state = "catch";
425 }finally {
426     ok(state === "catch", "funally: state = " + state);
427     state = "finally";
428 }
429 ok(state === "finally", "state = " + state + " expected finally");
430
431 state = "";
432 switch(1) {
433 case "1":
434     ok(false, "unexpected case \"1\"");
435 case 1:
436     ok(state === "", "case 1: state = " + state);
437     state = "1";
438 default:
439     ok(state === "1", "default: state = " + state);
440     state = "default";
441 case false:
442     ok(state === "default", "case false: state = " + state);
443     state = "false";
444 }
445 ok(state === "false", "state = " + state);
446
447 state = "";
448 switch("") {
449 case "1":
450 case 1:
451     ok(false, "unexpected case 1");
452 default:
453     ok(state === "", "default: state = " + state);
454     state = "default";
455 case false:
456     ok(state === "default", "case false: state = " + state);
457     state = "false";
458 }
459 ok(state === "false", "state = " + state);
460
461 state = "";
462 switch(1) {
463 case "1":
464     ok(false, "unexpected case \"1\"");
465 case 1:
466     ok(state === "", "case 1: state = " + state);
467     state = "1";
468 default:
469     ok(state === "1", "default: state = " + state);
470     state = "default";
471     break;
472 case false:
473     ok(false, "unexpected case false");
474 }
475 ok(state === "default", "state = " + state);
476
477 reportSuccess();