15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2008 the V8 project authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification, are permitted provided that the following conditions are
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met:
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions of source code must retain the above copyright
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       notice, this list of conditions and the following disclaimer.
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//     * Redistributions in binary form must reproduce the above
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//       copyright notice, this list of conditions and the following
100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//       disclaimer in the documentation and/or other materials provided
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       with the distribution.
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       contributors may be used to endorse or promote products derived
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       from this software without specific prior written permission.
15ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch//
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// This file contains a number of tests of array functions and their
297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// interaction with properties in the prototype chain.
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// The behavior of SpiderMonkey is slightly different for arrays (see
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// below).  Our behavior is consistent and matches the bahavior of
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// KJS.
340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvar proto = { length:3, 0:'zero', 1:'one', 2:'two' }
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)function constructor() {};
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)constructor.prototype = proto;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Set elements on the array prototype.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Array.prototype[0] = 'zero';
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Array.prototype[1] = 'one';
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Array.prototype[2] = 'two';
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ----------------------------------------------------------------------
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Helper functions.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ----------------------------------------------------------------------
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)function assertHasOwnProperties(object, limit) {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (var i = 0; i < limit; i++) {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assertTrue(object.hasOwnProperty(i));
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
54a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// ----------------------------------------------------------------------
55a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// shift.
56a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// ----------------------------------------------------------------------
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)function 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