string-index.js 8.72 KB
Newer Older
1
// Copyright 2008 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28 29
// Flags: --allow-natives-syntax

30 31 32 33 34 35 36 37 38 39
/**
 * @fileoverview Test indexing on strings with [].
 */

var foo = "Foo";
assertEquals("Foo", foo);
assertEquals("F", foo[0]);
assertEquals("o", foo[1]);
assertEquals("o", foo[2]);

40 41 42 43 44 45 46 47
// Test string keyed load IC.
for (var i = 0; i < 10; i++) {
  assertEquals("F", foo[0]);
  assertEquals("o", foo[1]);
  assertEquals("o", foo[2]);
  assertEquals("F", (foo[0] + "BarBazQuuxFooBarQuux")[0]);
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
assertEquals("F", foo["0" + ""], "string index");
assertEquals("o", foo["1"], "string index");
assertEquals("o", foo["2"], "string index");

assertEquals("undefined", typeof(foo[3]), "out of range");
// SpiderMonkey 1.5 fails this next one.  So does FF 2.0.6.
assertEquals("undefined", typeof(foo[-1]), "known failure in SpiderMonkey 1.5");
assertEquals("undefined", typeof(foo[-2]), "negative index");

foo[0] = "f";
assertEquals("Foo", foo);

foo[3] = "t";
assertEquals("Foo", foo);
assertEquals("undefined", typeof(foo[3]), "out of range");
assertEquals("undefined", typeof(foo[-2]), "negative index");

var S = new String("foo");
66
assertEquals(Object("foo"), S);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
assertEquals("f", S[0], "string object");
assertEquals("f", S["0"], "string object");
S[0] = 'bente';
assertEquals("f", S[0], "string object");
assertEquals("f", S["0"], "string object");
S[-2] = 'spider';
assertEquals('spider', S[-2]);
S[3] = 'monkey';
assertEquals('monkey', S[3]);
S['foo'] = 'Fu';
assertEquals("Fu", S.foo);

// In FF this is ignored I think.  In V8 it puts a property on the String object
// but you won't ever see it because it is hidden by the 0th character in the
// string.  The net effect is pretty much the same.
S["0"] = 'bente';
assertEquals("f", S[0], "string object");
assertEquals("f", S["0"], "string object");

assertEquals(true, 0 in S, "0 in");
assertEquals(false, -1 in S, "-1 in");
assertEquals(true, 2 in S, "2 in");
assertEquals(true, 3 in S, "3 in");
assertEquals(false, 4 in S, "3 in");

assertEquals(true, "0" in S, '"0" in');
assertEquals(false, "-1" in S, '"-1" in');
assertEquals(true, "2" in S, '"2" in');
assertEquals(true, "3" in S, '"3" in');
assertEquals(false, "4" in S, '"3" in');

assertEquals(true, S.hasOwnProperty(0), "0 hasOwnProperty");
assertEquals(false, S.hasOwnProperty(-1), "-1 hasOwnProperty");
assertEquals(true, S.hasOwnProperty(2), "2 hasOwnProperty");
assertEquals(true, S.hasOwnProperty(3), "3 hasOwnProperty");
assertEquals(false, S.hasOwnProperty(4), "3 hasOwnProperty");

assertEquals(true, S.hasOwnProperty("0"), '"0" hasOwnProperty');
assertEquals(false, S.hasOwnProperty("-1"), '"-1" hasOwnProperty');
assertEquals(true, S.hasOwnProperty("2"), '"2" hasOwnProperty');
assertEquals(true, S.hasOwnProperty("3"), '"3" hasOwnProperty');
assertEquals(false, S.hasOwnProperty("4"), '"3" hasOwnProperty');

assertEquals(true, "foo".hasOwnProperty(0), "foo 0 hasOwnProperty");
assertEquals(false, "foo".hasOwnProperty(-1), "foo -1 hasOwnProperty");
assertEquals(true, "foo".hasOwnProperty(2), "foo 2 hasOwnProperty");
assertEquals(false, "foo".hasOwnProperty(4), "foo 3 hasOwnProperty");

assertEquals(true, "foo".hasOwnProperty("0"), 'foo "0" hasOwnProperty');
assertEquals(false, "foo".hasOwnProperty("-1"), 'foo "-1" hasOwnProperty');
assertEquals(true, "foo".hasOwnProperty("2"), 'foo "2" hasOwnProperty');
assertEquals(false, "foo".hasOwnProperty("4"), 'foo "3" hasOwnProperty');

//assertEquals(true, 0 in "foo", "0 in");
//assertEquals(false, -1 in "foo", "-1 in");
//assertEquals(true, 2 in "foo", "2 in");
//assertEquals(false, 3 in "foo", "3 in");
//
//assertEquals(true, "0" in "foo", '"0" in');
//assertEquals(false, "-1" in "foo", '"-1" in');
//assertEquals(true, "2" in "foo", '"2" in');
//assertEquals(false, "3" in "foo", '"3" in');

delete S[3];
assertEquals("undefined", typeof(S[3]));
assertEquals(false, 3 in S);
assertEquals(false, "3" in S);

var N = new Number(43);
136
assertEquals(Object(43), N);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
N[-2] = "Alpha";
assertEquals("Alpha", N[-2]);
N[0] = "Zappa";
assertEquals("Zappa", N[0]);
assertEquals("Zappa", N["0"]);

var A = ["V", "e", "t", "t", "e", "r"];
var A2 = (A[0] = "v");
assertEquals('v', A[0]);
assertEquals('v', A2);

var S = new String("Onkel");
var S2 = (S[0] = 'o');
assertEquals('O', S[0]);
assertEquals('o', S2);

var s = "Tante";
var s2 = (s[0] = 't');
assertEquals('T', s[0]);
assertEquals('t', s2);

var S2 = (S[-2] = 'o');
assertEquals('o', S[-2]);
assertEquals('o', S2);

var s2 = (s[-2] = 't');
assertEquals('undefined', typeof(s[-2]));
assertEquals('t', s2);
165 166 167 168 169 170 171 172 173 174 175 176 177 178

// Make sure enough of the one-char string cache is filled.
var alpha = ['@'];
for (var i = 1; i < 128; i++) {
  var c = String.fromCharCode(i);
  alpha[i] = c[0];
}
var alphaStr = alpha.join("");

// Now test chars.
for (var i = 1; i < 128; i++) {
  assertEquals(alpha[i], alphaStr[i]);
  assertEquals(String.fromCharCode(i), alphaStr[i]);
}
179 180 181 182 183 184 185 186 187 188 189 190

// Test for keyed ic.
var foo = ['a12', ['a', 2, 'c'], 'a31', 42];
var results = [1, 2, 3, NaN];
for (var i = 0; i < 200; ++i) {
  var index = Math.floor(i / 50);
  var receiver = foo[index];
  var expected = results[index];
  var actual = +(receiver[1]);
  assertEquals(expected, actual);
}

191 192 193
var keys = [0, '1', 2, 3.0, -1, 10];
var str = 'abcd', arr = ['a', 'b', 'c', 'd', undefined, undefined];
for (var i = 0; i < 300; ++i) {
194 195 196 197 198 199
  var index = Math.floor(i / 50);
  var key = keys[index];
  var expected = arr[index];
  var actual = str[key];
  assertEquals(expected, actual);
}
200 201 202 203 204 205 206 207 208 209 210 211

// Test heap number case.
var keys = [0, Math.floor(2) * 0.5];
var str = 'ab', arr = ['a', 'b'];
for (var i = 0; i < 100; ++i) {
  var index = Math.floor(i / 50);
  var key = keys[index];
  var expected = arr[index];
  var actual = str[key];
  assertEquals(expected, actual);
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
// Test negative zero case.
var keys = [0, -0.0];
var str = 'ab', arr = ['a', 'a'];
for (var i = 0; i < 100; ++i) {
  var index = Math.floor(i / 50);
  var key = keys[index];
  var expected = arr[index];
  var actual = str[key];
  assertEquals(expected, actual);
}

// Test "not-an-array-index" case.
var keys = [0, 0.5];
var str = 'ab', arr = ['a', undefined];
for (var i = 0; i < 100; ++i) {
  var index = Math.floor(i / 50);
  var key = keys[index];
  var expected = arr[index];
  var actual = str[key];
  assertEquals(expected, actual);
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
// Test out of range case.
var keys = [0, -1];
var str = 'ab', arr = ['a', undefined];
for (var i = 0; i < 100; ++i) {
  var index = Math.floor(i / 50);
  var key = keys[index];
  var expected = arr[index];
  var actual = str[key];
  assertEquals(expected, actual);
}

var keys = [0, 10];
var str = 'ab', arr = ['a', undefined];
for (var i = 0; i < 100; ++i) {
  var index = Math.floor(i / 50);
  var key = keys[index];
  var expected = arr[index];
  var actual = str[key];
  assertEquals(expected, actual);
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268
// Test out of range with a heap number case.
var num = Math.floor(4) * 0.5;
// TODO(mvstanton): figure out a reliable way to get a heap number every time.
// assertFalse(!%_IsSmi(num));
var keys = [0, num];
var str = 'ab', arr = ['a', undefined];
for (var i = 0; i < 100; ++i) {
  var index = Math.floor(i / 50);
  var key = keys[index];
  var expected = arr[index];
  var actual = str[key];
  assertEquals(expected, actual);
}

269 270 271 272 273 274
// Test two byte string.
var str = '\u0427', arr = ['\u0427'];
for (var i = 0; i < 50; ++i) {
  var expected = arr[0];
  var actual = str[0];
  assertEquals(expected, actual);
275
}