jscript: Added String.substring 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;
20
21 tmp = "" + new Object();
22 ok(tmp === "[object Object]", "'' + new Object() = " + tmp);
23
24 ok("".length === 0, "\"\".length = " + "".length);
25 ok(getVT("".length) == "VT_I4", "\"\".length = " + "".length);
26 ok("abc".length === 3, "\"abc\".length = " + "abc".length);
27 ok(String.prototype.length === 0, "String.prototype.length = " + String.prototype.length);
28
29 tmp = "abc".charAt(0);
30 ok(tmp === "a", "'abc',charAt(0) = " + tmp);
31 tmp = "abc".charAt(1);
32 ok(tmp === "b", "'abc',charAt(1) = " + tmp);
33 tmp = "abc".charAt(2);
34 ok(tmp === "c", "'abc',charAt(2) = " + tmp);
35 tmp = "abc".charAt(3);
36 ok(tmp === "", "'abc',charAt(3) = " + tmp);
37 tmp = "abc".charAt(4);
38 ok(tmp === "", "'abc',charAt(4) = " + tmp);
39 tmp = "abc".charAt();
40 ok(tmp === "a", "'abc',charAt() = " + tmp);
41 tmp = "abc".charAt(-1);
42 ok(tmp === "", "'abc',charAt(-1) = " + tmp);
43 tmp = "abc".charAt(0,2);
44 ok(tmp === "a", "'abc',charAt(0.2) = " + tmp);
45
46 tmp = "abcd".substring(1,3);
47 ok(tmp === "bc", "'abcd'.substring(1,3) = " + tmp);
48 tmp = "abcd".substring(-1,3);
49 ok(tmp === "abc", "'abcd'.substring(-1,3) = " + tmp);
50 tmp = "abcd".substring(1,6);
51 ok(tmp === "bcd", "'abcd'.substring(1,6) = " + tmp);
52 tmp = "abcd".substring(3,1);
53 ok(tmp === "bc", "'abcd'.substring(3,1) = " + tmp);
54 tmp = "abcd".substring(2,2);
55 ok(tmp === "", "'abcd'.substring(2,2) = " + tmp);
56 tmp = "abcd".substring(true,"3");
57 ok(tmp === "bc", "'abcd'.substring(true,'3') = " + tmp);
58 tmp = "abcd".substring(1,3,2);
59 ok(tmp === "bc", "'abcd'.substring(1,3,2) = " + tmp);
60 tmp = "abcd".substring();
61 ok(tmp === "abcd", "'abcd'.substring() = " + tmp);
62
63 var arr = new Array();
64 ok(typeof(arr) === "object", "arr () is not object");
65 ok((arr.length === 0), "arr.length is not 0");
66 ok(arr["0"] === undefined, "arr[0] is not undefined");
67
68 var arr = new Array(1, 2, "test");
69 ok(typeof(arr) === "object", "arr (1,2,test) is not object");
70 ok((arr.length === 3), "arr.length is not 3");
71 ok(arr["0"] === 1, "arr[0] is not 1");
72 ok(arr["1"] === 2, "arr[1] is not 2");
73 ok(arr["2"] === "test", "arr[2] is not \"test\"");
74
75 arr["7"] = true;
76 ok((arr.length === 8), "arr.length is not 8");
77
78 tmp = "" + [];
79 ok(tmp === "", "'' + [] = " + tmp);
80 tmp = "" + [1,true];
81 ok(tmp === "1,true", "'' + [1,true] = " + tmp);
82
83 var arr = new Array(6);
84 ok(typeof(arr) === "object", "arr (6) is not object");
85 ok((arr.length === 6), "arr.length is not 6");
86 ok(arr["0"] === undefined, "arr[0] is not undefined");
87
88 ok(arr.push() === 6, "arr.push() !== 6");
89 ok(arr.push(1) === 7, "arr.push(1) !== 7");
90 ok(arr[6] === 1, "arr[6] != 1");
91 ok(arr.length === 7, "arr.length != 10");
92 ok(arr.push(true, 'b', false) === 10, "arr.push(true, 'b', false) !== 10");
93 ok(arr[8] === "b", "arr[8] != 'b'");
94 ok(arr.length === 10, "arr.length != 10");
95
96 arr = [1,2,null,false,undefined,,"a"];
97
98 tmp = arr.join();
99 ok(tmp === "1,2,,false,,,a", "arr.join() = " + tmp);
100 tmp = arr.join(";");
101 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
102 tmp = arr.join(";","test");
103 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
104 tmp = arr.join("");
105 ok(tmp === "12falsea", "arr.join('') = " + tmp);
106
107 tmp = arr.toString();
108 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
109 tmp = arr.toString("test");
110 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
111
112 arr = [5,true,2,-1,3,false,"2.5"];
113 tmp = arr.sort(function(x,y) { return y-x; });
114 ok(tmp === arr, "tmp !== arr");
115 tmp = [5,3,"2.5",2,true,false,-1];
116 for(var i=0; i < arr.length; i++)
117     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
118
119 arr = [5,false,2,0,"abc",3,"a",-1];
120 tmp = arr.sort();
121 ok(tmp === arr, "tmp !== arr");
122 tmp = [-1,0,2,3,5,"a","abc",false];
123 for(var i=0; i < arr.length; i++)
124     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
125
126 arr = ["a", "b", "ab"];
127 tmp = ["a", "ab", "b"];
128 ok(arr.sort() === arr, "arr.sort() !== arr");
129 for(var i=0; i < arr.length; i++)
130     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
131
132 var num = new Number(6);
133 arr = [0,1,2];
134 tmp = arr.concat(3, [4,5], num);
135 ok(tmp !== arr, "tmp === arr");
136 for(var i=0; i<6; i++)
137     ok(tmp[i] === i, "tmp[" + i + "] = " + tmp[i]);
138 ok(tmp[6] === num, "tmp[6] !== num");
139 ok(tmp.length === 7, "tmp.length = " + tmp.length);
140
141 arr = [].concat();
142 ok(arr.length === 0, "arr.length = " + arr.lrngth);
143
144 arr = [1,];
145 tmp = arr.concat([2]);
146 ok(tmp.length === 3, "tmp.length = " + tmp.length);
147 ok(tmp[1] === undefined, "tmp[1] = " + tmp[1]);
148
149 var num = new Number(2);
150 ok(num.toString() === "2", "num(2).toString !== 2");
151 var num = new Number();
152 ok(num.toString() === "0", "num().toString !== 0");
153
154 ok(Number() === 0, "Number() = " + Number());
155 ok(Number(false) === 0, "Number(false) = " + Number(false));
156 ok(Number("43") === 43, "Number('43') = " + Number("43"));
157
158 reportSuccess();