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
4// met:
5//
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.
15//
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.
27
28// This file contains a number of tests of array functions and their
29// interaction with properties in the prototype chain.
30//
31// The behavior of SpiderMonkey is slightly different for arrays (see
32// below).  Our behavior is consistent and matches the bahavior of
33// KJS.
34
35var proto = { length:3, 0:'zero', 1:'one', 2:'two' }
36function constructor() {};
37constructor.prototype = proto;
38
39// Set elements on the array prototype.
40Array.prototype[0] = 'zero';
41Array.prototype[1] = 'one';
42Array.prototype[2] = 'two';
43
44// ----------------------------------------------------------------------
45// Helper functions.
46// ----------------------------------------------------------------------
47function assertHasOwnProperties(object, limit) {
48  for (var i = 0; i < limit; i++) {
49    assertTrue(object.hasOwnProperty(i));
50  }
51}
52
53
54// ----------------------------------------------------------------------
55// shift.
56// ----------------------------------------------------------------------
57
58function runTest1() {
59  var nonArray = new constructor();
60  var array = ['zero', , 'two'];
61  // Shift away the zero.
62  assertEquals('zero', array.shift());
63  assertEquals('zero', Array.prototype.shift.call(nonArray));
64  // Check that the local object has properties 0 and 1 with the right
65  // values.
66  assertEquals(2, array.length);
67  assertEquals(2, nonArray.length);
68  assertHasOwnProperties(array, 2);
69  assertHasOwnProperties(nonArray, 2);
70  // Note: Spidermonkey is inconsistent here.  It treats arrays
71  // differently from non-arrays.  It only consults the prototype for
72  // non-arrays.  Therefore, array[0] is undefined in Spidermonkey and
73  // 'one' in V8 and KJS.
74  assertEquals('one', array[0]);
75  assertEquals('one', nonArray[0]);
76  assertEquals('two', array[1]);
77  assertEquals('two', nonArray[1]);
78  // Get index 2 from the prototype.
79  assertEquals('two', array[2]);
80  assertEquals('two', nonArray[2]);
81}
82
83runTest1();
84
85// ----------------------------------------------------------------------
86// unshift.
87// ----------------------------------------------------------------------
88
89runTest2 = function() {
90  var nonArray = new constructor();
91  var array = ['zero', , 'two'];
92  // Unshift a new 'zero'.
93  assertEquals(4, array.unshift('zero'));
94  assertEquals(4, Array.prototype.unshift.call(nonArray, 'zero'));
95  // Check that the local object has properties 0 through 3 with the
96  // right values.
97  assertEquals(4, array.length);
98  assertEquals(4, nonArray.length);
99  assertHasOwnProperties(array, 4);
100  assertHasOwnProperties(nonArray, 4);
101  assertEquals('zero', array[0]);
102  assertEquals('zero', nonArray[0]);
103  assertEquals('zero', array[1]);
104  assertEquals('zero', nonArray[1]);
105  // Again Spidermonkey is inconsistent.  array[2] is undefined
106  // instead of 'one'.
107  assertEquals('one', array[2]);
108  assertEquals('one', nonArray[2]);
109  assertEquals('two', array[3]);
110  assertEquals('two', nonArray[3]);
111}
112
113runTest2();
114
115
116// ----------------------------------------------------------------------
117// splice
118// ----------------------------------------------------------------------
119
120runTest3 = function() {
121  var nonArray = new constructor();
122  var array = ['zero', , 'two'];
123  // Delete the first element by splicing in nothing.
124  assertArrayEquals(['zero'], array.splice(0, 1));
125  assertArrayEquals(['zero'], Array.prototype.splice.call(nonArray, 0, 1));
126  // Check that the local object has properties 0 and 1 with the right
127  // values.
128  assertEquals(2, array.length);
129  assertEquals(2, nonArray.length);
130  assertHasOwnProperties(array, 2);
131  assertHasOwnProperties(nonArray, 2);
132  // Again Spidermonkey is inconsistent.  array[0] is undefined
133  // instead of 'one'.
134  assertEquals('one', array[0]);
135  assertEquals('one', nonArray[0]);
136  assertEquals('two', array[1]);
137  assertEquals('two', nonArray[1]);
138  // Get index 2 from the prototype.
139  assertEquals('two', array[2]);
140  assertEquals('two', nonArray[2]);
141};
142
143runTest3();
144
145
146// ----------------------------------------------------------------------
147// slice
148// ----------------------------------------------------------------------
149
150runTest4 = function() {
151  var nonArray = new constructor();
152  var array = ['zero', , 'two'];
153  // Again Spidermonkey is inconsistent.  (array.slice(0, 3))[1] is
154  // undefined instead of 'one'.
155  assertArrayEquals(['zero', 'one', 'two'], array.slice(0, 3));
156  assertArrayEquals(['zero', 'one', 'two'], Array.prototype.slice.call(nonArray, 0, 3));
157};
158
159runTest4();
160