jscript: Added String.indexOf 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 = [1,2,null,false,undefined,,"a"];
233
234 tmp = arr.join();
235 ok(tmp === "1,2,,false,,,a", "arr.join() = " + tmp);
236 tmp = arr.join(";");
237 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
238 tmp = arr.join(";","test");
239 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
240 tmp = arr.join("");
241 ok(tmp === "12falsea", "arr.join('') = " + tmp);
242
243 tmp = arr.toString();
244 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
245 tmp = arr.toString("test");
246 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
247
248 arr = [5,true,2,-1,3,false,"2.5"];
249 tmp = arr.sort(function(x,y) { return y-x; });
250 ok(tmp === arr, "tmp !== arr");
251 tmp = [5,3,"2.5",2,true,false,-1];
252 for(var i=0; i < arr.length; i++)
253     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
254
255 arr = [5,false,2,0,"abc",3,"a",-1];
256 tmp = arr.sort();
257 ok(tmp === arr, "tmp !== arr");
258 tmp = [-1,0,2,3,5,"a","abc",false];
259 for(var i=0; i < arr.length; i++)
260     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
261
262 arr = ["a", "b", "ab"];
263 tmp = ["a", "ab", "b"];
264 ok(arr.sort() === arr, "arr.sort() !== arr");
265 for(var i=0; i < arr.length; i++)
266     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
267
268 var num = new Number(6);
269 arr = [0,1,2];
270 tmp = arr.concat(3, [4,5], num);
271 ok(tmp !== arr, "tmp === arr");
272 for(var i=0; i<6; i++)
273     ok(tmp[i] === i, "tmp[" + i + "] = " + tmp[i]);
274 ok(tmp[6] === num, "tmp[6] !== num");
275 ok(tmp.length === 7, "tmp.length = " + tmp.length);
276
277 arr = [].concat();
278 ok(arr.length === 0, "arr.length = " + arr.lrngth);
279
280 arr = [1,];
281 tmp = arr.concat([2]);
282 ok(tmp.length === 3, "tmp.length = " + tmp.length);
283 ok(tmp[1] === undefined, "tmp[1] = " + tmp[1]);
284
285 var num = new Number(2);
286 ok(num.toString() === "2", "num(2).toString !== 2");
287 var num = new Number();
288 ok(num.toString() === "0", "num().toString !== 0");
289
290 ok(Number() === 0, "Number() = " + Number());
291 ok(Number(false) === 0, "Number(false) = " + Number(false));
292 ok(Number("43") === 43, "Number('43') = " + Number("43"));
293
294 tmp = Math.min(1);
295 ok(tmp === 1, "Math.min(1) = " + tmp);
296
297 tmp = Math.min(1, false);
298 ok(tmp === 0, "Math.min(1, false) = " + tmp);
299
300 tmp = Math.min(1, false, true, null, -3);
301 ok(tmp === -3, "Math.min(1, false, true, null, -3) = " + tmp);
302
303 tmp = Math.max(1);
304 ok(tmp === 1, "Math.max(1) = " + tmp);
305
306 tmp = Math.max(true, 0);
307 ok(tmp === 1, "Math.max(true, 0) = " + tmp);
308
309 tmp = Math.max(-2, false, true, null, 1);
310 ok(tmp === 1, "Math.max(-2, false, true, null, 1) = " + tmp);
311
312 tmp = Math.round(0.5);
313 ok(tmp === 1, "Math.round(0.5) = " + tmp);
314
315 tmp = Math.round(-0.5);
316 ok(tmp === 0, "Math.round(-0.5) = " + tmp);
317
318 tmp = Math.round(1.1);
319 ok(tmp === 1, "Math.round(1.1) = " + tmp);
320
321 tmp = Math.round(true);
322 ok(tmp === 1, "Math.round(true) = " + tmp);
323
324 tmp = Math.round(1.1, 3, 4);
325 ok(tmp === 1, "Math.round(1.1, 3, 4) = " + tmp);
326
327 tmp = Math.ceil(0.5);
328 ok(tmp === 1, "Math.ceil(0.5) = " + tmp);
329
330 tmp = Math.ceil(-0.5);
331 ok(tmp === 0, "Math.ceil(-0.5) = " + tmp);
332
333 tmp = Math.ceil(1.1);
334 ok(tmp === 2, "Math.round(1.1) = " + tmp);
335
336 tmp = Math.ceil(true);
337 ok(tmp === 1, "Math.ceil(true) = " + tmp);
338
339 tmp = Math.ceil(1.1, 3, 4);
340 ok(tmp === 2, "Math.ceil(1.1, 3, 4) = " + tmp);
341
342 tmp = Math.abs(3);
343 ok(tmp === 3, "Math.abs(3) = " + tmp);
344
345 tmp = Math.abs(-3);
346 ok(tmp === 3, "Math.abs(-3) = " + tmp);
347
348 tmp = Math.abs(true);
349 ok(tmp === 1, "Math.abs(true) = " + tmp);
350
351 tmp = Math.abs(-3, 2);
352 ok(tmp === 3, "Math.abs(-3, 2) = " + tmp);
353
354 tmp = Math.pow(2, 2);
355 ok(tmp === 4, "Math.pow(2, 2) = " + tmp);
356
357 tmp = Math.pow(4, 0.5);
358 ok(tmp === 2, "Math.pow(2, 2) = " + tmp);
359
360 tmp = Math.pow(2, 2, 3);
361 ok(tmp === 4, "Math.pow(2, 2, 3) = " + tmp);
362
363 var func = function  (a) {
364         var a = 1;
365         if(a) return;
366     }.toString();
367 ok(func.toString() === "function  (a) {\n        var a = 1;\n        if(a) return;\n    }",
368    "func.toString() = " + func.toString());
369 ok("" + func === "function  (a) {\n        var a = 1;\n        if(a) return;\n    }",
370    "'' + func.toString() = " + func);
371
372 function testFuncToString(x,y) {
373     return x+y;
374 }
375
376 ok(testFuncToString.toString() === "function testFuncToString(x,y) {\n    return x+y;\n}",
377    "testFuncToString.toString() = " + testFuncToString.toString());
378 ok("" + testFuncToString === "function testFuncToString(x,y) {\n    return x+y;\n}",
379    "'' + testFuncToString = " + testFuncToString);
380
381 reportSuccess();