jscript: Added Math.floor implementation.
[wine/multimedia.git] / dlls / jscript / tests / api.js
blobd613eb1a26345f33b58ec62862da813514fbcbca
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, i;
21 i = parseInt("0");
22 ok(i === 0, "parseInt('0') = " + i);
23 i = parseInt("123");
24 ok(i === 123, "parseInt('123') = " + i);
25 i = parseInt("-123");
26 ok(i === -123, "parseInt('-123') = " + i);
27 i = parseInt("0xff");
28 ok(i === 0xff, "parseInt('0xff') = " + i);
29 i = parseInt("11", 8);
30 ok(i === 9, "parseInt('11', 8) = " + i);
31 i = parseInt("1j", 22);
32 ok(i === 41, "parseInt('1j', 32) = " + i);
33 i = parseInt("123", 0);
34 ok(i === 123, "parseInt('123', 0) = " + i);
35 i = parseInt("123", 10, "test");
36 ok(i === 123, "parseInt('123', 10, 'test') = " + i);
37 i = parseInt("11", "8");
38 ok(i === 9, "parseInt('11', '8') = " + i);
40 tmp = encodeURI("abc");
41 ok(tmp === "abc", "encodeURI('abc') = " + tmp);
42 tmp = encodeURI("{abc}");
43 ok(tmp === "%7Babc%7D", "encodeURI('{abc}') = " + tmp);
44 tmp = encodeURI("");
45 ok(tmp === "", "encodeURI('') = " + tmp);
46 tmp = encodeURI("\01\02\03\04");
47 ok(tmp === "%01%02%03%04", "encodeURI('\\01\\02\\03\\04') = " + tmp);
48 tmp = encodeURI("{#@}");
49 ok(tmp === "%7B#@%7D", "encodeURI('{#@}') = " + tmp);
50 tmp = encodeURI("\xa1 ");
51 ok(tmp === "%C2%A1%20", "encodeURI(\\xa1 ) = " + tmp);
52 tmp = encodeURI("\xffff");
53 ok(tmp.length === 8, "encodeURI('\\xffff').length = " + tmp.length);
54 tmp = encodeURI("abcABC123;/?:@&=+$,-_.!~*'()");
55 ok(tmp === "abcABC123;/?:@&=+$,-_.!~*'()", "encodeURI('abcABC123;/?:@&=+$,-_.!~*'()') = " + tmp);
56 tmp = encodeURI();
57 ok(tmp === "undefined", "encodeURI() = " + tmp);
58 tmp = encodeURI("abc", "test");
59 ok(tmp === "abc", "encodeURI('abc') = " + tmp);
61 tmp = "" + new Object();
62 ok(tmp === "[object Object]", "'' + new Object() = " + tmp);
64 ok("".length === 0, "\"\".length = " + "".length);
65 ok(getVT("".length) == "VT_I4", "\"\".length = " + "".length);
66 ok("abc".length === 3, "\"abc\".length = " + "abc".length);
67 ok(String.prototype.length === 0, "String.prototype.length = " + String.prototype.length);
69 tmp = "".toString();
70 ok(tmp === "", "''.toString() = " + tmp);
71 tmp = "test".toString();
72 ok(tmp === "test", "''.toString() = " + tmp);
73 tmp = "test".toString(3);
74 ok(tmp === "test", "''.toString(3) = " + tmp);
76 tmp = "".valueOf();
77 ok(tmp === "", "''.valueOf() = " + tmp);
78 tmp = "test".valueOf();
79 ok(tmp === "test", "''.valueOf() = " + tmp);
80 tmp = "test".valueOf(3);
81 ok(tmp === "test", "''.valueOf(3) = " + tmp);
83 var str = new String("test");
84 ok(str.toString() === "test", "str.toString() = " + str.toString());
85 var str = new String();
86 ok(str.toString() === "", "str.toString() = " + str.toString());
87 var str = new String("test", "abc");
88 ok(str.toString() === "test", "str.toString() = " + str.toString());
90 tmp = "value " + str;
91 ok(tmp === "value test", "'value ' + str = " + tmp);
93 tmp = String();
94 ok(tmp === "", "String() = " + tmp);
95 tmp = String(false);
96 ok(tmp === "false", "String(false) = " + tmp);
97 tmp = String(null);
98 ok(tmp === "null", "String(null) = " + tmp);
99 tmp = String("test");
100 ok(tmp === "test", "String('test') = " + tmp);
101 tmp = String("test", "abc");
102 ok(tmp === "test", "String('test','abc') = " + tmp);
104 tmp = "abc".charAt(0);
105 ok(tmp === "a", "'abc',charAt(0) = " + tmp);
106 tmp = "abc".charAt(1);
107 ok(tmp === "b", "'abc',charAt(1) = " + tmp);
108 tmp = "abc".charAt(2);
109 ok(tmp === "c", "'abc',charAt(2) = " + tmp);
110 tmp = "abc".charAt(3);
111 ok(tmp === "", "'abc',charAt(3) = " + tmp);
112 tmp = "abc".charAt(4);
113 ok(tmp === "", "'abc',charAt(4) = " + tmp);
114 tmp = "abc".charAt();
115 ok(tmp === "a", "'abc',charAt() = " + tmp);
116 tmp = "abc".charAt(-1);
117 ok(tmp === "", "'abc',charAt(-1) = " + tmp);
118 tmp = "abc".charAt(0,2);
119 ok(tmp === "a", "'abc',charAt(0.2) = " + tmp);
121 tmp = "abc".charCodeAt(0);
122 ok(tmp === 0x61, "'abc'.charCodeAt(0) = " + tmp);
123 tmp = "abc".charCodeAt(1);
124 ok(tmp === 0x62, "'abc'.charCodeAt(1) = " + tmp);
125 tmp = "abc".charCodeAt(2);
126 ok(tmp === 0x63, "'abc'.charCodeAt(2) = " + tmp);
127 tmp = "abc".charCodeAt();
128 ok(tmp === 0x61, "'abc'.charCodeAt() = " + tmp);
129 tmp = "abc".charCodeAt(true);
130 ok(tmp === 0x62, "'abc'.charCodeAt(true) = " + tmp);
131 tmp = "abc".charCodeAt(0,2);
132 ok(tmp === 0x61, "'abc'.charCodeAt(0,2) = " + tmp);
134 tmp = "abcd".substring(1,3);
135 ok(tmp === "bc", "'abcd'.substring(1,3) = " + tmp);
136 tmp = "abcd".substring(-1,3);
137 ok(tmp === "abc", "'abcd'.substring(-1,3) = " + tmp);
138 tmp = "abcd".substring(1,6);
139 ok(tmp === "bcd", "'abcd'.substring(1,6) = " + tmp);
140 tmp = "abcd".substring(3,1);
141 ok(tmp === "bc", "'abcd'.substring(3,1) = " + tmp);
142 tmp = "abcd".substring(2,2);
143 ok(tmp === "", "'abcd'.substring(2,2) = " + tmp);
144 tmp = "abcd".substring(true,"3");
145 ok(tmp === "bc", "'abcd'.substring(true,'3') = " + tmp);
146 tmp = "abcd".substring(1,3,2);
147 ok(tmp === "bc", "'abcd'.substring(1,3,2) = " + tmp);
148 tmp = "abcd".substring();
149 ok(tmp === "abcd", "'abcd'.substring() = " + tmp);
151 tmp = "abcd".slice(1,3);
152 ok(tmp === "bc", "'abcd'.slice(1,3) = " + tmp);
153 tmp = "abcd".slice(1,-1);
154 ok(tmp === "bc", "'abcd'.slice(1,-1) = " + tmp);
155 tmp = "abcd".slice(-3,3);
156 ok(tmp === "bc", "'abcd'.slice(-3,3) = " + tmp);
157 tmp = "abcd".slice(-6,3);
158 ok(tmp === "abc", "'abcd'.slice(-6,3) = " + tmp);
159 tmp = "abcd".slice(3,1);
160 ok(tmp === "", "'abcd'.slice(3,1) = " + tmp);
161 tmp = "abcd".slice(true,3);
162 ok(tmp === "bc", "'abcd'.slice(true,3) = " + tmp);
163 tmp = "abcd".slice();
164 ok(tmp === "abcd", "'abcd'.slice() = " + tmp);
165 tmp = "abcd".slice(1);
166 ok(tmp === "bcd", "'abcd'.slice(1) = " + tmp);
168 tmp = "abc".concat(["d",1],2,false);
169 ok(tmp === "abcd,12false", "concat returned " + tmp);
170 var arr = new Array(2,"a");
171 arr.concat = String.prototype.concat;
172 tmp = arr.concat("d");
173 ok(tmp === "2,ad", "arr.concat = " + tmp);
175 m = "a+bcabc".match("a+");
176 ok(typeof(m) === "object", "typeof m is not object");
177 ok(m.length === 1, "m.length is not 1");
178 ok(m["0"] === "a", "m[0] is not \"ab\"");
180 r = "- [test] -".replace("[test]", "success");
181 ok(r === "- success -", "r = " + r + " expected '- success -'");
183 r = "- [test] -".replace("[test]", "success", "test");
184 ok(r === "- success -", "r = " + r + " expected '- success -'");
186 r = "test".replace();
187 ok(r === "test", "r = " + r + " expected 'test'");
189 function replaceFunc3(m, off, str) {
190     ok(arguments.length === 3, "arguments.length = " + arguments.length);
191     ok(m === "[test]", "m = " + m + " expected [test1]");
192     ok(off === 1, "off = " + off + " expected 0");
193     ok(str === "-[test]-", "str = " + arguments[3]);
194     return "ret";
197 r = "-[test]-".replace("[test]", replaceFunc3);
198 ok(r === "-ret-", "r = " + r + " expected '-ret-'");
200 r = "-[test]-".replace("[test]", replaceFunc3, "test");
201 ok(r === "-ret-", "r = " + r + " expected '-ret-'");
203 r = "1,2,3".split(",");
204 ok(typeof(r) === "object", "typeof(r) = " + typeof(r));
205 ok(r.length === 3, "r.length = " + r.length);
206 ok(r[0] === "1", "r[0] = " + r[0]);
207 ok(r[1] === "2", "r[1] = " + r[1]);
208 ok(r[2] === "3", "r[2] = " + r[2]);
211 r = "1,2,3".split(",*");
212 ok(r.length === 1, "r.length = " + r.length);
213 ok(r[0] === "1,2,3", "r[0] = " + r[0]);
215 r = "123".split("");
216 ok(r.length === 3, "r.length = " + r.length);
217 ok(r[0] === "1", "r[0] = " + r[0]);
218 ok(r[1] === "2", "r[1] = " + r[1]);
219 ok(r[2] === "3", "r[2] = " + r[2]);
221 r = "123".split(2);
222 ok(r.length === 2, "r.length = " + r.length);
223 ok(r[0] === "1", "r[0] = " + r[0]);
224 ok(r[1] === "3", "r[1] = " + r[1]);
226 r = "1,2,".split(",");
227 ok(typeof(r) === "object", "typeof(r) = " + typeof(r));
228 ok(r.length === 3, "r.length = " + r.length);
229 ok(r[0] === "1", "r[0] = " + r[0]);
230 ok(r[1] === "2", "r[1] = " + r[1]);
231 ok(r[2] === "", "r[2] = " + r[2]);
233 tmp = "abcd".indexOf("bc",0);
234 ok(tmp === 1, "indexOf = " + tmp);
235 tmp = "abcd".indexOf("bc",1);
236 ok(tmp === 1, "indexOf = " + tmp);
237 tmp = "abcd".indexOf("bc");
238 ok(tmp === 1, "indexOf = " + tmp);
239 tmp = "abcd".indexOf("ac");
240 ok(tmp === -1, "indexOf = " + tmp);
241 tmp = "abcd".indexOf("bc",2);
242 ok(tmp === -1, "indexOf = " + tmp);
243 tmp = "abcd".indexOf("a",0);
244 ok(tmp === 0, "indexOf = " + tmp);
245 tmp = "abcd".indexOf("bc",0,"test");
246 ok(tmp === 1, "indexOf = " + tmp);
247 tmp = "abcd".indexOf();
248 ok(tmp == -1, "indexOf = " + tmp);
250 var arr = new Array();
251 ok(typeof(arr) === "object", "arr () is not object");
252 ok((arr.length === 0), "arr.length is not 0");
253 ok(arr["0"] === undefined, "arr[0] is not undefined");
255 var arr = new Array(1, 2, "test");
256 ok(typeof(arr) === "object", "arr (1,2,test) is not object");
257 ok((arr.length === 3), "arr.length is not 3");
258 ok(arr["0"] === 1, "arr[0] is not 1");
259 ok(arr["1"] === 2, "arr[1] is not 2");
260 ok(arr["2"] === "test", "arr[2] is not \"test\"");
262 arr["7"] = true;
263 ok((arr.length === 8), "arr.length is not 8");
265 tmp = "" + [];
266 ok(tmp === "", "'' + [] = " + tmp);
267 tmp = "" + [1,true];
268 ok(tmp === "1,true", "'' + [1,true] = " + tmp);
270 var arr = new Array(6);
271 ok(typeof(arr) === "object", "arr (6) is not object");
272 ok((arr.length === 6), "arr.length is not 6");
273 ok(arr["0"] === undefined, "arr[0] is not undefined");
275 ok(arr.push() === 6, "arr.push() !== 6");
276 ok(arr.push(1) === 7, "arr.push(1) !== 7");
277 ok(arr[6] === 1, "arr[6] != 1");
278 ok(arr.length === 7, "arr.length != 10");
279 ok(arr.push(true, 'b', false) === 10, "arr.push(true, 'b', false) !== 10");
280 ok(arr[8] === "b", "arr[8] != 'b'");
281 ok(arr.length === 10, "arr.length != 10");
283 arr = [3,4,5];
284 tmp = arr.pop();
285 ok(arr.length === 2, "arr.length = " + arr.length);
286 ok(tmp === 5, "pop() = " + tmp);
287 tmp = arr.pop(2);
288 ok(arr.length === 1, "arr.length = " + arr.length);
289 ok(tmp === 4, "pop() = " + tmp);
290 tmp = arr.pop();
291 ok(arr.length === 0, "arr.length = " + arr.length);
292 ok(tmp === 3, "pop() = " + tmp);
293 for(tmp in arr)
294     ok(false, "not deleted " + tmp);
295 tmp = arr.pop();
296 ok(arr.length === 0, "arr.length = " + arr.length);
297 ok(tmp === undefined, "tmp = " + tmp);
298 arr = [,,,,,];
299 tmp = arr.pop();
300 ok(arr.length === 5, "arr.length = " + arr.length);
301 ok(tmp === undefined, "tmp = " + tmp);
303 arr = [1,2,null,false,undefined,,"a"];
305 tmp = arr.join();
306 ok(tmp === "1,2,,false,,,a", "arr.join() = " + tmp);
307 tmp = arr.join(";");
308 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
309 tmp = arr.join(";","test");
310 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
311 tmp = arr.join("");
312 ok(tmp === "12falsea", "arr.join('') = " + tmp);
314 tmp = arr.toString();
315 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
316 tmp = arr.toString("test");
317 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
319 arr = [5,true,2,-1,3,false,"2.5"];
320 tmp = arr.sort(function(x,y) { return y-x; });
321 ok(tmp === arr, "tmp !== arr");
322 tmp = [5,3,"2.5",2,true,false,-1];
323 for(var i=0; i < arr.length; i++)
324     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
326 arr = [5,false,2,0,"abc",3,"a",-1];
327 tmp = arr.sort();
328 ok(tmp === arr, "tmp !== arr");
329 tmp = [-1,0,2,3,5,"a","abc",false];
330 for(var i=0; i < arr.length; i++)
331     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
333 arr = ["a", "b", "ab"];
334 tmp = ["a", "ab", "b"];
335 ok(arr.sort() === arr, "arr.sort() !== arr");
336 for(var i=0; i < arr.length; i++)
337     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
339 var num = new Number(6);
340 arr = [0,1,2];
341 tmp = arr.concat(3, [4,5], num);
342 ok(tmp !== arr, "tmp === arr");
343 for(var i=0; i<6; i++)
344     ok(tmp[i] === i, "tmp[" + i + "] = " + tmp[i]);
345 ok(tmp[6] === num, "tmp[6] !== num");
346 ok(tmp.length === 7, "tmp.length = " + tmp.length);
348 arr = [].concat();
349 ok(arr.length === 0, "arr.length = " + arr.length);
351 arr = [1,];
352 tmp = arr.concat([2]);
353 ok(tmp.length === 3, "tmp.length = " + tmp.length);
354 ok(tmp[1] === undefined, "tmp[1] = " + tmp[1]);
356 var num = new Number(2);
357 ok(num.toString() === "2", "num(2).toString !== 2");
358 var num = new Number();
359 ok(num.toString() === "0", "num().toString !== 0");
361 ok(Number() === 0, "Number() = " + Number());
362 ok(Number(false) === 0, "Number(false) = " + Number(false));
363 ok(Number("43") === 43, "Number('43') = " + Number("43"));
365 tmp = (new Number(1)).valueOf();
366 ok(tmp === 1, "(new Number(1)).valueOf = " + tmp);
367 tmp = (new Number(1,2)).valueOf();
368 ok(tmp === 1, "(new Number(1,2)).valueOf = " + tmp);
369 tmp = (new Number()).valueOf();
370 ok(tmp === 0, "(new Number()).valueOf = " + tmp);
371 tmp = Number.prototype.valueOf();
372 ok(tmp === 0, "Number.prototype.valueOf = " + tmp);
374 tmp = Math.min(1);
375 ok(tmp === 1, "Math.min(1) = " + tmp);
377 tmp = Math.min(1, false);
378 ok(tmp === 0, "Math.min(1, false) = " + tmp);
380 tmp = Math.min();
381 ok(tmp === Infinity, "Math.min() = " + tmp);
383 tmp = Math.min(1, NaN, -Infinity, false);
384 ok(isNaN(tmp), "Math.min(1, NaN, -Infinity, false) is not NaN");
386 tmp = Math.min(1, false, true, null, -3);
387 ok(tmp === -3, "Math.min(1, false, true, null, -3) = " + tmp);
389 tmp = Math.max(1);
390 ok(tmp === 1, "Math.max(1) = " + tmp);
392 tmp = Math.max(true, 0);
393 ok(tmp === 1, "Math.max(true, 0) = " + tmp);
395 tmp = Math.max(-2, false, true, null, 1);
396 ok(tmp === 1, "Math.max(-2, false, true, null, 1) = " + tmp);
398 tmp = Math.max();
399 ok(tmp === -Infinity, "Math.max() = " + tmp);
401 tmp = Math.max(true, NaN, 0);
402 ok(isNaN(tmp), "Math.max(true, NaN, 0) is not NaN");
404 tmp = Math.round(0.5);
405 ok(tmp === 1, "Math.round(0.5) = " + tmp);
407 tmp = Math.round(-0.5);
408 ok(tmp === 0, "Math.round(-0.5) = " + tmp);
410 tmp = Math.round(1.1);
411 ok(tmp === 1, "Math.round(1.1) = " + tmp);
413 tmp = Math.round(true);
414 ok(tmp === 1, "Math.round(true) = " + tmp);
416 tmp = Math.round(1.1, 3, 4);
417 ok(tmp === 1, "Math.round(1.1, 3, 4) = " + tmp);
419 tmp = Math.ceil(0.5);
420 ok(tmp === 1, "Math.ceil(0.5) = " + tmp);
422 tmp = Math.ceil(-0.5);
423 ok(tmp === 0, "Math.ceil(-0.5) = " + tmp);
425 tmp = Math.ceil(1.1);
426 ok(tmp === 2, "Math.round(1.1) = " + tmp);
428 tmp = Math.ceil(true);
429 ok(tmp === 1, "Math.ceil(true) = " + tmp);
431 tmp = Math.ceil(1.1, 3, 4);
432 ok(tmp === 2, "Math.ceil(1.1, 3, 4) = " + tmp);
434 tmp = Math.floor(0.5);
435 ok(tmp === 0, "Math.floor(0.5) = " + tmp);
437 tmp = Math.floor(-0.5);
438 ok(tmp === -1, "Math.floor(-0.5) = " + tmp);
440 tmp = Math.floor(1.1);
441 ok(tmp === 1, "Math.floor(1.1) = " + tmp);
443 tmp = Math.floor(true);
444 ok(tmp === 1, "Math.floor(true) = " + tmp);
446 tmp = Math.floor(1.1, 3, 4);
447 ok(tmp === 1, "Math.floor(1.1, 3, 4) = " + tmp);
449 tmp = Math.floor();
450 ok(isNaN(tmp), "floor is not NaN");
452 tmp = Math.abs(3);
453 ok(tmp === 3, "Math.abs(3) = " + tmp);
455 tmp = Math.abs(-3);
456 ok(tmp === 3, "Math.abs(-3) = " + tmp);
458 tmp = Math.abs(true);
459 ok(tmp === 1, "Math.abs(true) = " + tmp);
461 tmp = Math.abs();
462 ok(isNaN(tmp), "Math.abs() is not NaN");
464 tmp = Math.abs(NaN);
465 ok(isNaN(tmp), "Math.abs() is not NaN");
467 tmp = Math.abs(-Infinity);
468 ok(tmp === Infinity, "Math.abs(-Infinite) = " + tmp);
470 tmp = Math.abs(-3, 2);
471 ok(tmp === 3, "Math.abs(-3, 2) = " + tmp);
473 tmp = Math.pow(2, 2);
474 ok(tmp === 4, "Math.pow(2, 2) = " + tmp);
476 tmp = Math.pow(4, 0.5);
477 ok(tmp === 2, "Math.pow(2, 2) = " + tmp);
479 tmp = Math.pow(2, 2, 3);
480 ok(tmp === 4, "Math.pow(2, 2, 3) = " + tmp);
482 var func = function  (a) {
483         var a = 1;
484         if(a) return;
485     }.toString();
486 ok(func.toString() === "function  (a) {\n        var a = 1;\n        if(a) return;\n    }",
487    "func.toString() = " + func.toString());
488 ok("" + func === "function  (a) {\n        var a = 1;\n        if(a) return;\n    }",
489    "'' + func.toString() = " + func);
491 function testFuncToString(x,y) {
492     return x+y;
495 ok(testFuncToString.toString() === "function testFuncToString(x,y) {\n    return x+y;\n}",
496    "testFuncToString.toString() = " + testFuncToString.toString());
497 ok("" + testFuncToString === "function testFuncToString(x,y) {\n    return x+y;\n}",
498    "'' + testFuncToString = " + testFuncToString);
500 var date = new Date();
502 date = new Date(100);
503 ok(date.getTime() === 100, "date.getTime() = " + date.getTime());
504 ok(Date.prototype.getTime() === 0, "date.prototype.getTime() = " + Date.prototype.getTime());
506 reportSuccess();