1402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// Copyright 2010 the V8 project authors. All rights reserved.
2402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// Redistribution and use in source and binary forms, with or without
3402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// modification, are permitted provided that the following conditions are
4402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// met:
5402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//
6402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//     * Redistributions of source code must retain the above copyright
7402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       notice, this list of conditions and the following disclaimer.
8402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//     * Redistributions in binary form must reproduce the above
9402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       copyright notice, this list of conditions and the following
10402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       disclaimer in the documentation and/or other materials provided
11402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       with the distribution.
12402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//     * Neither the name of Google Inc. nor the names of its
13402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       contributors may be used to endorse or promote products derived
14402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       from this software without specific prior written permission.
15402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//
16402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
28402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// Check that shifting array of holes keeps it as array of holes
29402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu(function() {
30402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  var array = new Array(10);
31402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  array.shift();
32402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertFalse(0 in array);
33402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu})();
34402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
35402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// Now check the case with array of holes and some elements on prototype.
36402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu(function() {
37402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  var len = 9;
38402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  var array = new Array(len);
39402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  Array.prototype[3] = "@3";
40402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  Array.prototype[7] = "@7";
41402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
42402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertEquals(len, array.length);
43402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  for (var i = 0; i < array.length; i++) {
44402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu    assertEquals(array[i], Array.prototype[i]);
45402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  }
46402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
47402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  array.shift();
48402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
49402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertEquals(len - 1, array.length);
50402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  // Note that shift copies values from prototype into the array.
51402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertEquals(array[2], Array.prototype[3]);
52402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertTrue(array.hasOwnProperty(2));
53402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
54402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertEquals(array[6], Array.prototype[7]);
55402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertTrue(array.hasOwnProperty(6));
56402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
57402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  // ... but keeps the rest as holes:
58402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  Array.prototype[5] = "@5";
59402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertEquals(array[5], Array.prototype[5]);
60402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertFalse(array.hasOwnProperty(5));
61402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
62402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertEquals(array[3], Array.prototype[3]);
63402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertFalse(array.hasOwnProperty(3));
64402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
65402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertEquals(array[7], Array.prototype[7]);
66402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertFalse(array.hasOwnProperty(7));
67402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
68402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertTrue(delete Array.prototype[3]);
69402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertTrue(delete Array.prototype[5]);
70402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  assertTrue(delete Array.prototype[7]);
71402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu})();
7225f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen
7325f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen// Now check the case with array of holes and some elements on prototype
7425f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen// which is an array itself.
7525f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen(function() {
7625f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  var len = 9;
7725f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  var array = new Array(len);
7825f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  var array_proto = new Array();
7925f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  array_proto[3] = "@3";
8025f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  array_proto[7] = "@7";
8125f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  array.__proto__ = array_proto;
8225f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen
8325f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertEquals(len, array.length);
8425f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  for (var i = 0; i < array.length; i++) {
8525f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen    assertEquals(array[i], array_proto[i]);
8625f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  }
8725f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen
8825f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  array.shift();
8925f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen
9025f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertEquals(len - 1, array.length);
9125f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  // Note that shift copies values from prototype into the array.
9225f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertEquals(array[2], array_proto[3]);
9325f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertTrue(array.hasOwnProperty(2));
9425f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen
9525f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertEquals(array[6], array_proto[7]);
9625f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertTrue(array.hasOwnProperty(6));
9725f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen
9825f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  // ... but keeps the rest as holes:
9925f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  array_proto[5] = "@5";
10025f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertEquals(array[5], array_proto[5]);
10125f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertFalse(array.hasOwnProperty(5));
10225f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen
10325f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertEquals(array[3], array_proto[3]);
10425f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertFalse(array.hasOwnProperty(3));
10525f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen
10625f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertEquals(array[7], array_proto[7]);
10725f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  assertFalse(array.hasOwnProperty(7));
10825f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen})();
109