1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * @fileoverview Test indexing on strings with [].
33 assertEquals("Foo", foo);
34 assertEquals("F", foo[0]);
35 assertEquals("o", foo[1]);
36 assertEquals("o", foo[2]);
38 // Test string keyed load IC.
39 for (var i = 0; i < 10; i++) {
40 assertEquals("F", foo[0]);
41 assertEquals("o", foo[1]);
42 assertEquals("o", foo[2]);
43 assertEquals("F", (foo[0] + "BarBazQuuxFooBarQuux")[0]);
46 assertEquals("F", foo["0" + ""], "string index");
47 assertEquals("o", foo["1"], "string index");
48 assertEquals("o", foo["2"], "string index");
50 assertEquals("undefined", typeof(foo[3]), "out of range");
51 // SpiderMonkey 1.5 fails this next one. So does FF 2.0.6.
52 assertEquals("undefined", typeof(foo[-1]), "known failure in SpiderMonkey 1.5");
53 assertEquals("undefined", typeof(foo[-2]), "negative index");
56 assertEquals("Foo", foo);
59 assertEquals("Foo", foo);
60 assertEquals("undefined", typeof(foo[3]), "out of range");
61 assertEquals("undefined", typeof(foo[-2]), "negative index");
63 var S = new String("foo");
64 assertEquals(Object("foo"), S);
65 assertEquals("f", S[0], "string object");
66 assertEquals("f", S["0"], "string object");
68 assertEquals("f", S[0], "string object");
69 assertEquals("f", S["0"], "string object");
71 assertEquals('spider', S[-2]);
73 assertEquals('monkey', S[3]);
75 assertEquals("Fu", S.foo);
77 // In FF this is ignored I think. In V8 it puts a property on the String object
78 // but you won't ever see it because it is hidden by the 0th character in the
79 // string. The net effect is pretty much the same.
81 assertEquals("f", S[0], "string object");
82 assertEquals("f", S["0"], "string object");
84 assertEquals(true, 0 in S, "0 in");
85 assertEquals(false, -1 in S, "-1 in");
86 assertEquals(true, 2 in S, "2 in");
87 assertEquals(true, 3 in S, "3 in");
88 assertEquals(false, 4 in S, "3 in");
90 assertEquals(true, "0" in S, '"0" in');
91 assertEquals(false, "-1" in S, '"-1" in');
92 assertEquals(true, "2" in S, '"2" in');
93 assertEquals(true, "3" in S, '"3" in');
94 assertEquals(false, "4" in S, '"3" in');
96 assertEquals(true, S.hasOwnProperty(0), "0 hasOwnProperty");
97 assertEquals(false, S.hasOwnProperty(-1), "-1 hasOwnProperty");
98 assertEquals(true, S.hasOwnProperty(2), "2 hasOwnProperty");
99 assertEquals(true, S.hasOwnProperty(3), "3 hasOwnProperty");
100 assertEquals(false, S.hasOwnProperty(4), "3 hasOwnProperty");
102 assertEquals(true, S.hasOwnProperty("0"), '"0" hasOwnProperty');
103 assertEquals(false, S.hasOwnProperty("-1"), '"-1" hasOwnProperty');
104 assertEquals(true, S.hasOwnProperty("2"), '"2" hasOwnProperty');
105 assertEquals(true, S.hasOwnProperty("3"), '"3" hasOwnProperty');
106 assertEquals(false, S.hasOwnProperty("4"), '"3" hasOwnProperty');
108 assertEquals(true, "foo".hasOwnProperty(0), "foo 0 hasOwnProperty");
109 assertEquals(false, "foo".hasOwnProperty(-1), "foo -1 hasOwnProperty");
110 assertEquals(true, "foo".hasOwnProperty(2), "foo 2 hasOwnProperty");
111 assertEquals(false, "foo".hasOwnProperty(4), "foo 3 hasOwnProperty");
113 assertEquals(true, "foo".hasOwnProperty("0"), 'foo "0" hasOwnProperty');
114 assertEquals(false, "foo".hasOwnProperty("-1"), 'foo "-1" hasOwnProperty');
115 assertEquals(true, "foo".hasOwnProperty("2"), 'foo "2" hasOwnProperty');
116 assertEquals(false, "foo".hasOwnProperty("4"), 'foo "3" hasOwnProperty');
118 //assertEquals(true, 0 in "foo", "0 in");
119 //assertEquals(false, -1 in "foo", "-1 in");
120 //assertEquals(true, 2 in "foo", "2 in");
121 //assertEquals(false, 3 in "foo", "3 in");
123 //assertEquals(true, "0" in "foo", '"0" in');
124 //assertEquals(false, "-1" in "foo", '"-1" in');
125 //assertEquals(true, "2" in "foo", '"2" in');
126 //assertEquals(false, "3" in "foo", '"3" in');
129 assertEquals("undefined", typeof(S[3]));
130 assertEquals(false, 3 in S);
131 assertEquals(false, "3" in S);
133 var N = new Number(43);
134 assertEquals(Object(43), N);
136 assertEquals("Alpha", N[-2]);
138 assertEquals("Zappa", N[0]);
139 assertEquals("Zappa", N["0"]);
141 var A = ["V", "e", "t", "t", "e", "r"];
142 var A2 = (A[0] = "v");
143 assertEquals('v', A[0]);
144 assertEquals('v', A2);
146 var S = new String("Onkel");
147 var S2 = (S[0] = 'o');
148 assertEquals('O', S[0]);
149 assertEquals('o', S2);
152 var s2 = (s[0] = 't');
153 assertEquals('T', s[0]);
154 assertEquals('t', s2);
156 var S2 = (S[-2] = 'o');
157 assertEquals('o', S[-2]);
158 assertEquals('o', S2);
160 var s2 = (s[-2] = 't');
161 assertEquals('undefined', typeof(s[-2]));
162 assertEquals('t', s2);
164 // Make sure enough of the one-char string cache is filled.
166 for (var i = 1; i < 128; i++) {
167 var c = String.fromCharCode(i);
170 var alphaStr = alpha.join("");
173 for (var i = 1; i < 128; i++) {
174 assertEquals(alpha[i], alphaStr[i]);
175 assertEquals(String.fromCharCode(i), alphaStr[i]);
178 // Test for keyed ic.
179 var foo = ['a12', ['a', 2, 'c'], 'a31', 42];
180 var results = [1, 2, 3, NaN];
181 for (var i = 0; i < 200; ++i) {
182 var index = Math.floor(i / 50);
183 var receiver = foo[index];
184 var expected = results[index];
185 var actual = +(receiver[1]);
186 assertEquals(expected, actual);
189 var keys = [0, '1', 2, 3.0, -1, 10];
190 var str = 'abcd', arr = ['a', 'b', 'c', 'd', undefined, undefined];
191 for (var i = 0; i < 300; ++i) {
192 var index = Math.floor(i / 50);
193 var key = keys[index];
194 var expected = arr[index];
195 var actual = str[key];
196 assertEquals(expected, actual);
199 // Test heap number case.
200 var keys = [0, Math.floor(2) * 0.5];
201 var str = 'ab', arr = ['a', 'b'];
202 for (var i = 0; i < 100; ++i) {
203 var index = Math.floor(i / 50);
204 var key = keys[index];
205 var expected = arr[index];
206 var actual = str[key];
207 assertEquals(expected, actual);
210 // Test negative zero case.
211 var keys = [0, -0.0];
212 var str = 'ab', arr = ['a', 'a'];
213 for (var i = 0; i < 100; ++i) {
214 var index = Math.floor(i / 50);
215 var key = keys[index];
216 var expected = arr[index];
217 var actual = str[key];
218 assertEquals(expected, actual);
221 // Test "not-an-array-index" case.
223 var str = 'ab', arr = ['a', undefined];
224 for (var i = 0; i < 100; ++i) {
225 var index = Math.floor(i / 50);
226 var key = keys[index];
227 var expected = arr[index];
228 var actual = str[key];
229 assertEquals(expected, actual);
232 // Test out of range case.
234 var str = 'ab', arr = ['a', undefined];
235 for (var i = 0; i < 100; ++i) {
236 var index = Math.floor(i / 50);
237 var key = keys[index];
238 var expected = arr[index];
239 var actual = str[key];
240 assertEquals(expected, actual);
244 var str = 'ab', arr = ['a', undefined];
245 for (var i = 0; i < 100; ++i) {
246 var index = Math.floor(i / 50);
247 var key = keys[index];
248 var expected = arr[index];
249 var actual = str[key];
250 assertEquals(expected, actual);
253 // Test two byte string.
254 var str = '\u0427', arr = ['\u0427'];
255 for (var i = 0; i < 50; ++i) {
256 var expected = arr[0];
258 assertEquals(expected, actual);