push 4e98c31ec75caed2ea3040ac2e710b58ba1ca0e1
[wine/hacks.git] / dlls / jscript / tests / api.js
blob93f06b95a6d0c0c5b3797a80c12249ff0121dc2e
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 = "" + new Object();
41 ok(tmp === "[object Object]", "'' + new Object() = " + tmp);
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);
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);
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);
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());
69 tmp = "value " + str;
70 ok(tmp === "value test", "'value ' + str = " + tmp);
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);
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);
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);
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);
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);
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);
154 var arr = new Array();
155 ok(typeof(arr) === "object", "arr () is not object");
156 ok((arr.length === 0), "arr.length is not 0");
157 ok(arr["0"] === undefined, "arr[0] is not undefined");
159 var arr = new Array(1, 2, "test");
160 ok(typeof(arr) === "object", "arr (1,2,test) is not object");
161 ok((arr.length === 3), "arr.length is not 3");
162 ok(arr["0"] === 1, "arr[0] is not 1");
163 ok(arr["1"] === 2, "arr[1] is not 2");
164 ok(arr["2"] === "test", "arr[2] is not \"test\"");
166 arr["7"] = true;
167 ok((arr.length === 8), "arr.length is not 8");
169 tmp = "" + [];
170 ok(tmp === "", "'' + [] = " + tmp);
171 tmp = "" + [1,true];
172 ok(tmp === "1,true", "'' + [1,true] = " + tmp);
174 var arr = new Array(6);
175 ok(typeof(arr) === "object", "arr (6) is not object");
176 ok((arr.length === 6), "arr.length is not 6");
177 ok(arr["0"] === undefined, "arr[0] is not undefined");
179 ok(arr.push() === 6, "arr.push() !== 6");
180 ok(arr.push(1) === 7, "arr.push(1) !== 7");
181 ok(arr[6] === 1, "arr[6] != 1");
182 ok(arr.length === 7, "arr.length != 10");
183 ok(arr.push(true, 'b', false) === 10, "arr.push(true, 'b', false) !== 10");
184 ok(arr[8] === "b", "arr[8] != 'b'");
185 ok(arr.length === 10, "arr.length != 10");
187 arr = [1,2,null,false,undefined,,"a"];
189 tmp = arr.join();
190 ok(tmp === "1,2,,false,,,a", "arr.join() = " + tmp);
191 tmp = arr.join(";");
192 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
193 tmp = arr.join(";","test");
194 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
195 tmp = arr.join("");
196 ok(tmp === "12falsea", "arr.join('') = " + tmp);
198 tmp = arr.toString();
199 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
200 tmp = arr.toString("test");
201 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
203 arr = [5,true,2,-1,3,false,"2.5"];
204 tmp = arr.sort(function(x,y) { return y-x; });
205 ok(tmp === arr, "tmp !== arr");
206 tmp = [5,3,"2.5",2,true,false,-1];
207 for(var i=0; i < arr.length; i++)
208     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
210 arr = [5,false,2,0,"abc",3,"a",-1];
211 tmp = arr.sort();
212 ok(tmp === arr, "tmp !== arr");
213 tmp = [-1,0,2,3,5,"a","abc",false];
214 for(var i=0; i < arr.length; i++)
215     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
217 arr = ["a", "b", "ab"];
218 tmp = ["a", "ab", "b"];
219 ok(arr.sort() === arr, "arr.sort() !== arr");
220 for(var i=0; i < arr.length; i++)
221     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
223 var num = new Number(6);
224 arr = [0,1,2];
225 tmp = arr.concat(3, [4,5], num);
226 ok(tmp !== arr, "tmp === arr");
227 for(var i=0; i<6; i++)
228     ok(tmp[i] === i, "tmp[" + i + "] = " + tmp[i]);
229 ok(tmp[6] === num, "tmp[6] !== num");
230 ok(tmp.length === 7, "tmp.length = " + tmp.length);
232 arr = [].concat();
233 ok(arr.length === 0, "arr.length = " + arr.lrngth);
235 arr = [1,];
236 tmp = arr.concat([2]);
237 ok(tmp.length === 3, "tmp.length = " + tmp.length);
238 ok(tmp[1] === undefined, "tmp[1] = " + tmp[1]);
240 var num = new Number(2);
241 ok(num.toString() === "2", "num(2).toString !== 2");
242 var num = new Number();
243 ok(num.toString() === "0", "num().toString !== 0");
245 ok(Number() === 0, "Number() = " + Number());
246 ok(Number(false) === 0, "Number(false) = " + Number(false));
247 ok(Number("43") === 43, "Number('43') = " + Number("43"));
249 tmp = Math.min(1);
250 ok(tmp === 1, "Math.min(1) = " + tmp);
252 tmp = Math.min(1, false);
253 ok(tmp === 0, "Math.min(1, false) = " + tmp);
255 tmp = Math.min(1, false, true, null, -3);
256 ok(tmp === -3, "Math.min(1, false, true, null, -3) = " + tmp);
258 tmp = Math.max(1);
259 ok(tmp === 1, "Math.max(1) = " + tmp);
261 tmp = Math.max(true, 0);
262 ok(tmp === 1, "Math.max(true, 0) = " + tmp);
264 tmp = Math.max(-2, false, true, null, 1);
265 ok(tmp === 1, "Math.max(-2, false, true, null, 1) = " + tmp);
267 tmp = Math.round(0.5);
268 ok(tmp === 1, "Math.round(0.5) = " + tmp);
270 tmp = Math.round(-0.5);
271 ok(tmp === 0, "Math.round(-0.5) = " + tmp);
273 tmp = Math.round(1.1);
274 ok(tmp === 1, "Math.round(1.1) = " + tmp);
276 tmp = Math.round(true);
277 ok(tmp === 1, "Math.round(true) = " + tmp);
279 tmp = Math.round(1.1, 3, 4);
280 ok(tmp === 1, "Math.round(1.1, 3, 4) = " + tmp);
282 tmp = Math.ceil(0.5);
283 ok(tmp === 1, "Math.ceil(0.5) = " + tmp);
285 tmp = Math.ceil(-0.5);
286 ok(tmp === 0, "Math.ceil(-0.5) = " + tmp);
288 tmp = Math.ceil(1.1);
289 ok(tmp === 2, "Math.round(1.1) = " + tmp);
291 tmp = Math.ceil(true);
292 ok(tmp === 1, "Math.ceil(true) = " + tmp);
294 tmp = Math.ceil(1.1, 3, 4);
295 ok(tmp === 2, "Math.ceil(1.1, 3, 4) = " + tmp);
297 tmp = Math.abs(3);
298 ok(tmp === 3, "Math.abs(3) = " + tmp);
300 tmp = Math.abs(-3);
301 ok(tmp === 3, "Math.abs(-3) = " + tmp);
303 tmp = Math.abs(true);
304 ok(tmp === 1, "Math.abs(true) = " + tmp);
306 tmp = Math.abs(-3, 2);
307 ok(tmp === 3, "Math.abs(-3, 2) = " + tmp);
309 tmp = Math.pow(2, 2);
310 ok(tmp === 4, "Math.pow(2, 2) = " + tmp);
312 tmp = Math.pow(4, 0.5);
313 ok(tmp === 2, "Math.pow(2, 2) = " + tmp);
315 tmp = Math.pow(2, 2, 3);
316 ok(tmp === 4, "Math.pow(2, 2, 3) = " + tmp);
318 var func = function  (a) {
319         var a = 1;
320         if(a) return;
321     }.toString();
322 ok(func.toString() === "function  (a) {\n        var a = 1;\n        if(a) return;\n    }",
323    "func.toString() = " + func.toString());
324 ok("" + func === "function  (a) {\n        var a = 1;\n        if(a) return;\n    }",
325    "'' + func.toString() = " + func);
327 function testFuncToString(x,y) {
328     return x+y;
331 ok(testFuncToString.toString() === "function testFuncToString(x,y) {\n    return x+y;\n}",
332    "testFuncToString.toString() = " + testFuncToString.toString());
333 ok("" + testFuncToString === "function testFuncToString(x,y) {\n    return x+y;\n}",
334    "'' + testFuncToString = " + testFuncToString);
336 reportSuccess();