jscript: Added Array.pop implementation.
[wine] / dlls / jscript / tests / api.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, i;
20
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);
39
40 tmp = "" + new Object();
41 ok(tmp === "[object Object]", "'' + new Object() = " + tmp);
42
43 ok("".length === 0, "\"\".length = " + "".length);
44 ok(getVT("".length) == "VT_I4", "\"\".length = " + "".length);
45 ok("abc".length === 3, "\"abc\".length = " + "abc".length);
46 ok(String.prototype.length === 0, "String.prototype.length = " + String.prototype.length);
47
48 tmp = "".toString();
49 ok(tmp === "", "''.toString() = " + tmp);
50 tmp = "test".toString();
51 ok(tmp === "test", "''.toString() = " + tmp);
52 tmp = "test".toString(3);
53 ok(tmp === "test", "''.toString(3) = " + tmp);
54
55 tmp = "".valueOf();
56 ok(tmp === "", "''.valueOf() = " + tmp);
57 tmp = "test".valueOf();
58 ok(tmp === "test", "''.valueOf() = " + tmp);
59 tmp = "test".valueOf(3);
60 ok(tmp === "test", "''.valueOf(3) = " + tmp);
61
62 var str = new String("test");
63 ok(str.toString() === "test", "str.toString() = " + str.toString());
64 var str = new String();
65 ok(str.toString() === "", "str.toString() = " + str.toString());
66 var str = new String("test", "abc");
67 ok(str.toString() === "test", "str.toString() = " + str.toString());
68
69 tmp = "value " + str;
70 ok(tmp === "value test", "'value ' + str = " + tmp);
71
72 tmp = String();
73 ok(tmp === "", "String() = " + tmp);
74 tmp = String(false);
75 ok(tmp === "false", "String(false) = " + tmp);
76 tmp = String(null);
77 ok(tmp === "null", "String(null) = " + tmp);
78 tmp = String("test");
79 ok(tmp === "test", "String('test') = " + tmp);
80 tmp = String("test", "abc");
81 ok(tmp === "test", "String('test','abc') = " + tmp);
82
83 tmp = "abc".charAt(0);
84 ok(tmp === "a", "'abc',charAt(0) = " + tmp);
85 tmp = "abc".charAt(1);
86 ok(tmp === "b", "'abc',charAt(1) = " + tmp);
87 tmp = "abc".charAt(2);
88 ok(tmp === "c", "'abc',charAt(2) = " + tmp);
89 tmp = "abc".charAt(3);
90 ok(tmp === "", "'abc',charAt(3) = " + tmp);
91 tmp = "abc".charAt(4);
92 ok(tmp === "", "'abc',charAt(4) = " + tmp);
93 tmp = "abc".charAt();
94 ok(tmp === "a", "'abc',charAt() = " + tmp);
95 tmp = "abc".charAt(-1);
96 ok(tmp === "", "'abc',charAt(-1) = " + tmp);
97 tmp = "abc".charAt(0,2);
98 ok(tmp === "a", "'abc',charAt(0.2) = " + tmp);
99
100 tmp = "abc".charCodeAt(0);
101 ok(tmp === 0x61, "'abc'.charCodeAt(0) = " + tmp);
102 tmp = "abc".charCodeAt(1);
103 ok(tmp === 0x62, "'abc'.charCodeAt(1) = " + tmp);
104 tmp = "abc".charCodeAt(2);
105 ok(tmp === 0x63, "'abc'.charCodeAt(2) = " + tmp);
106 tmp = "abc".charCodeAt();
107 ok(tmp === 0x61, "'abc'.charCodeAt() = " + tmp);
108 tmp = "abc".charCodeAt(true);
109 ok(tmp === 0x62, "'abc'.charCodeAt(true) = " + tmp);
110 tmp = "abc".charCodeAt(0,2);
111 ok(tmp === 0x61, "'abc'.charCodeAt(0,2) = " + tmp);
112
113 tmp = "abcd".substring(1,3);
114 ok(tmp === "bc", "'abcd'.substring(1,3) = " + tmp);
115 tmp = "abcd".substring(-1,3);
116 ok(tmp === "abc", "'abcd'.substring(-1,3) = " + tmp);
117 tmp = "abcd".substring(1,6);
118 ok(tmp === "bcd", "'abcd'.substring(1,6) = " + tmp);
119 tmp = "abcd".substring(3,1);
120 ok(tmp === "bc", "'abcd'.substring(3,1) = " + tmp);
121 tmp = "abcd".substring(2,2);
122 ok(tmp === "", "'abcd'.substring(2,2) = " + tmp);
123 tmp = "abcd".substring(true,"3");
124 ok(tmp === "bc", "'abcd'.substring(true,'3') = " + tmp);
125 tmp = "abcd".substring(1,3,2);
126 ok(tmp === "bc", "'abcd'.substring(1,3,2) = " + tmp);
127 tmp = "abcd".substring();
128 ok(tmp === "abcd", "'abcd'.substring() = " + tmp);
129
130 tmp = "abcd".slice(1,3);
131 ok(tmp === "bc", "'abcd'.slice(1,3) = " + tmp);
132 tmp = "abcd".slice(1,-1);
133 ok(tmp === "bc", "'abcd'.slice(1,-1) = " + tmp);
134 tmp = "abcd".slice(-3,3);
135 ok(tmp === "bc", "'abcd'.slice(-3,3) = " + tmp);
136 tmp = "abcd".slice(-6,3);
137 ok(tmp === "abc", "'abcd'.slice(-6,3) = " + tmp);
138 tmp = "abcd".slice(3,1);
139 ok(tmp === "", "'abcd'.slice(3,1) = " + tmp);
140 tmp = "abcd".slice(true,3);
141 ok(tmp === "bc", "'abcd'.slice(true,3) = " + tmp);
142 tmp = "abcd".slice();
143 ok(tmp === "abcd", "'abcd'.slice() = " + tmp);
144 tmp = "abcd".slice(1);
145 ok(tmp === "bcd", "'abcd'.slice(1) = " + tmp);
146
147 tmp = "abc".concat(["d",1],2,false);
148 ok(tmp === "abcd,12false", "concat returned " + tmp);
149 var arr = new Array(2,"a");
150 arr.concat = String.prototype.concat;
151 tmp = arr.concat("d");
152 ok(tmp === "2,ad", "arr.concat = " + tmp);
153
154 m = "a+bcabc".match("a+");
155 ok(typeof(m) === "object", "typeof m is not object");
156 ok(m.length === 1, "m.length is not 1");
157 ok(m["0"] === "a", "m[0] is not \"ab\"");
158
159 r = "- [test] -".replace("[test]", "success");
160 ok(r === "- success -", "r = " + r + " expected '- success -'");
161
162 r = "- [test] -".replace("[test]", "success", "test");
163 ok(r === "- success -", "r = " + r + " expected '- success -'");
164
165 r = "test".replace();
166 ok(r === "test", "r = " + r + " expected 'test'");
167
168 function replaceFunc3(m, off, str) {
169     ok(arguments.length === 3, "arguments.length = " + arguments.length);
170     ok(m === "[test]", "m = " + m + " expected [test1]");
171     ok(off === 1, "off = " + off + " expected 0");
172     ok(str === "-[test]-", "str = " + arguments[3]);
173     return "ret";
174 }
175
176 r = "-[test]-".replace("[test]", replaceFunc3);
177 ok(r === "-ret-", "r = " + r + " expected '-ret-'");
178
179 r = "-[test]-".replace("[test]", replaceFunc3, "test");
180 ok(r === "-ret-", "r = " + r + " expected '-ret-'");
181
182 tmp = "abcd".indexOf("bc",0);
183 ok(tmp === 1, "indexOf = " + tmp);
184 tmp = "abcd".indexOf("bc",1);
185 ok(tmp === 1, "indexOf = " + tmp);
186 tmp = "abcd".indexOf("bc");
187 ok(tmp === 1, "indexOf = " + tmp);
188 tmp = "abcd".indexOf("ac");
189 ok(tmp === -1, "indexOf = " + tmp);
190 tmp = "abcd".indexOf("bc",2);
191 ok(tmp === -1, "indexOf = " + tmp);
192 tmp = "abcd".indexOf("a",0);
193 ok(tmp === 0, "indexOf = " + tmp);
194 tmp = "abcd".indexOf("bc",0,"test");
195 ok(tmp === 1, "indexOf = " + tmp);
196 tmp = "abcd".indexOf();
197 ok(tmp == -1, "indexOf = " + tmp);
198
199 var arr = new Array();
200 ok(typeof(arr) === "object", "arr () is not object");
201 ok((arr.length === 0), "arr.length is not 0");
202 ok(arr["0"] === undefined, "arr[0] is not undefined");
203
204 var arr = new Array(1, 2, "test");
205 ok(typeof(arr) === "object", "arr (1,2,test) is not object");
206 ok((arr.length === 3), "arr.length is not 3");
207 ok(arr["0"] === 1, "arr[0] is not 1");
208 ok(arr["1"] === 2, "arr[1] is not 2");
209 ok(arr["2"] === "test", "arr[2] is not \"test\"");
210
211 arr["7"] = true;
212 ok((arr.length === 8), "arr.length is not 8");
213
214 tmp = "" + [];
215 ok(tmp === "", "'' + [] = " + tmp);
216 tmp = "" + [1,true];
217 ok(tmp === "1,true", "'' + [1,true] = " + tmp);
218
219 var arr = new Array(6);
220 ok(typeof(arr) === "object", "arr (6) is not object");
221 ok((arr.length === 6), "arr.length is not 6");
222 ok(arr["0"] === undefined, "arr[0] is not undefined");
223
224 ok(arr.push() === 6, "arr.push() !== 6");
225 ok(arr.push(1) === 7, "arr.push(1) !== 7");
226 ok(arr[6] === 1, "arr[6] != 1");
227 ok(arr.length === 7, "arr.length != 10");
228 ok(arr.push(true, 'b', false) === 10, "arr.push(true, 'b', false) !== 10");
229 ok(arr[8] === "b", "arr[8] != 'b'");
230 ok(arr.length === 10, "arr.length != 10");
231
232 arr = [3,4,5];
233 tmp = arr.pop();
234 ok(arr.length === 2, "arr.length = " + arr.length);
235 ok(tmp === 5, "pop() = " + tmp);
236 tmp = arr.pop(2);
237 ok(arr.length === 1, "arr.length = " + arr.length);
238 ok(tmp === 4, "pop() = " + tmp);
239 tmp = arr.pop();
240 ok(arr.length === 0, "arr.length = " + arr.length);
241 ok(tmp === 3, "pop() = " + tmp);
242 for(tmp in arr)
243     ok(false, "not deleted " + tmp);
244 tmp = arr.pop();
245 ok(arr.length === 0, "arr.length = " + arr.length);
246 ok(tmp === undefined, "tmp = " + tmp);
247 arr = [,,,,,];
248 tmp = arr.pop();
249 ok(arr.length === 5, "arr.length = " + arr.length);
250 ok(tmp === undefined, "tmp = " + tmp);
251
252 arr = [1,2,null,false,undefined,,"a"];
253
254 tmp = arr.join();
255 ok(tmp === "1,2,,false,,,a", "arr.join() = " + tmp);
256 tmp = arr.join(";");
257 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
258 tmp = arr.join(";","test");
259 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
260 tmp = arr.join("");
261 ok(tmp === "12falsea", "arr.join('') = " + tmp);
262
263 tmp = arr.toString();
264 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
265 tmp = arr.toString("test");
266 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
267
268 arr = [5,true,2,-1,3,false,"2.5"];
269 tmp = arr.sort(function(x,y) { return y-x; });
270 ok(tmp === arr, "tmp !== arr");
271 tmp = [5,3,"2.5",2,true,false,-1];
272 for(var i=0; i < arr.length; i++)
273     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
274
275 arr = [5,false,2,0,"abc",3,"a",-1];
276 tmp = arr.sort();
277 ok(tmp === arr, "tmp !== arr");
278 tmp = [-1,0,2,3,5,"a","abc",false];
279 for(var i=0; i < arr.length; i++)
280     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
281
282 arr = ["a", "b", "ab"];
283 tmp = ["a", "ab", "b"];
284 ok(arr.sort() === arr, "arr.sort() !== arr");
285 for(var i=0; i < arr.length; i++)
286     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
287
288 var num = new Number(6);
289 arr = [0,1,2];
290 tmp = arr.concat(3, [4,5], num);
291 ok(tmp !== arr, "tmp === arr");
292 for(var i=0; i<6; i++)
293     ok(tmp[i] === i, "tmp[" + i + "] = " + tmp[i]);
294 ok(tmp[6] === num, "tmp[6] !== num");
295 ok(tmp.length === 7, "tmp.length = " + tmp.length);
296
297 arr = [].concat();
298 ok(arr.length === 0, "arr.length = " + arr.length);
299
300 arr = [1,];
301 tmp = arr.concat([2]);
302 ok(tmp.length === 3, "tmp.length = " + tmp.length);
303 ok(tmp[1] === undefined, "tmp[1] = " + tmp[1]);
304
305 var num = new Number(2);
306 ok(num.toString() === "2", "num(2).toString !== 2");
307 var num = new Number();
308 ok(num.toString() === "0", "num().toString !== 0");
309
310 ok(Number() === 0, "Number() = " + Number());
311 ok(Number(false) === 0, "Number(false) = " + Number(false));
312 ok(Number("43") === 43, "Number('43') = " + Number("43"));
313
314 tmp = Math.min(1);
315 ok(tmp === 1, "Math.min(1) = " + tmp);
316
317 tmp = Math.min(1, false);
318 ok(tmp === 0, "Math.min(1, false) = " + tmp);
319
320 tmp = Math.min(1, false, true, null, -3);
321 ok(tmp === -3, "Math.min(1, false, true, null, -3) = " + tmp);
322
323 tmp = Math.max(1);
324 ok(tmp === 1, "Math.max(1) = " + tmp);
325
326 tmp = Math.max(true, 0);
327 ok(tmp === 1, "Math.max(true, 0) = " + tmp);
328
329 tmp = Math.max(-2, false, true, null, 1);
330 ok(tmp === 1, "Math.max(-2, false, true, null, 1) = " + tmp);
331
332 tmp = Math.round(0.5);
333 ok(tmp === 1, "Math.round(0.5) = " + tmp);
334
335 tmp = Math.round(-0.5);
336 ok(tmp === 0, "Math.round(-0.5) = " + tmp);
337
338 tmp = Math.round(1.1);
339 ok(tmp === 1, "Math.round(1.1) = " + tmp);
340
341 tmp = Math.round(true);
342 ok(tmp === 1, "Math.round(true) = " + tmp);
343
344 tmp = Math.round(1.1, 3, 4);
345 ok(tmp === 1, "Math.round(1.1, 3, 4) = " + tmp);
346
347 tmp = Math.ceil(0.5);
348 ok(tmp === 1, "Math.ceil(0.5) = " + tmp);
349
350 tmp = Math.ceil(-0.5);
351 ok(tmp === 0, "Math.ceil(-0.5) = " + tmp);
352
353 tmp = Math.ceil(1.1);
354 ok(tmp === 2, "Math.round(1.1) = " + tmp);
355
356 tmp = Math.ceil(true);
357 ok(tmp === 1, "Math.ceil(true) = " + tmp);
358
359 tmp = Math.ceil(1.1, 3, 4);
360 ok(tmp === 2, "Math.ceil(1.1, 3, 4) = " + tmp);
361
362 tmp = Math.abs(3);
363 ok(tmp === 3, "Math.abs(3) = " + tmp);
364
365 tmp = Math.abs(-3);
366 ok(tmp === 3, "Math.abs(-3) = " + tmp);
367
368 tmp = Math.abs(true);
369 ok(tmp === 1, "Math.abs(true) = " + tmp);
370
371 tmp = Math.abs(-3, 2);
372 ok(tmp === 3, "Math.abs(-3, 2) = " + tmp);
373
374 tmp = Math.pow(2, 2);
375 ok(tmp === 4, "Math.pow(2, 2) = " + tmp);
376
377 tmp = Math.pow(4, 0.5);
378 ok(tmp === 2, "Math.pow(2, 2) = " + tmp);
379
380 tmp = Math.pow(2, 2, 3);
381 ok(tmp === 4, "Math.pow(2, 2, 3) = " + tmp);
382
383 var func = function  (a) {
384         var a = 1;
385         if(a) return;
386     }.toString();
387 ok(func.toString() === "function  (a) {\n        var a = 1;\n        if(a) return;\n    }",
388    "func.toString() = " + func.toString());
389 ok("" + func === "function  (a) {\n        var a = 1;\n        if(a) return;\n    }",
390    "'' + func.toString() = " + func);
391
392 function testFuncToString(x,y) {
393     return x+y;
394 }
395
396 ok(testFuncToString.toString() === "function testFuncToString(x,y) {\n    return x+y;\n}",
397    "testFuncToString.toString() = " + testFuncToString.toString());
398 ok("" + testFuncToString === "function testFuncToString(x,y) {\n    return x+y;\n}",
399    "'' + testFuncToString = " + testFuncToString);
400
401 reportSuccess();