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
28var array = [1,2,3,1,2,3,1,2,3,1,2,3];
29var undef_array = [0,,2,undefined,4,,6,undefined,8,,10];
30// Sparse arrays with length 42000.
31var sparse_array = [];
32sparse_array[100] = 3;
33sparse_array[200] = undefined;
34sparse_array[300] = 4;
35sparse_array[400] = 5;
36sparse_array[500] = 6;
37sparse_array[600] = 5;
38sparse_array[700] = 4;
39sparse_array[800] = undefined;
40sparse_array[900] = 3
41sparse_array[41999] = "filler";
42
43var dense_object = { 0: 42, 1: 37, length: 2 };
44var sparse_object = { 0: 42, 100000: 37, length: 200000 };
45var funky_object = { 10:42, 100000: 42, 100001: 37, length: 50000 };
46var infinite_object = { 10: 42, 100000: 37, length: Infinity };
47
48// ----------------------------------------------------------------------
49// Array.prototype.indexOf.
50// ----------------------------------------------------------------------
51
52// Negative cases.
53assertEquals(-1, [].indexOf(1));
54assertEquals(-1, array.indexOf(4));
55assertEquals(-1, array.indexOf(3, array.length));
56
57assertEquals(2, array.indexOf(3));
58// Negative index out of range.
59assertEquals(0, array.indexOf(1, -17));
60// Negative index in rage.
61assertEquals(3, array.indexOf(1, -11));
62// Index in range.
63assertEquals(3, array.indexOf(1, 1));
64assertEquals(3, array.indexOf(1, 3));
65assertEquals(6, array.indexOf(1, 4));
66
67// Find undefined, not holes.
68assertEquals(3, undef_array.indexOf(undefined));
69assertEquals(3, undef_array.indexOf(undefined, 3));
70assertEquals(7, undef_array.indexOf(undefined, 4));
71assertEquals(7, undef_array.indexOf(undefined, 7));
72assertEquals(-1, undef_array.indexOf(undefined, 8));
73assertEquals(3, undef_array.indexOf(undefined, -11));
74assertEquals(3, undef_array.indexOf(undefined, -8));
75assertEquals(7, undef_array.indexOf(undefined, -7));
76assertEquals(7, undef_array.indexOf(undefined, -4));
77assertEquals(-1, undef_array.indexOf(undefined, -3));
78
79// Find in sparse array.
80assertEquals(100, sparse_array.indexOf(3));
81assertEquals(900, sparse_array.indexOf(3, 101));
82assertEquals(-1, sparse_array.indexOf(3, 901));
83assertEquals(100, sparse_array.indexOf(3, -42000));
84assertEquals(900, sparse_array.indexOf(3, 101 - 42000));
85assertEquals(-1, sparse_array.indexOf(3, 901 - 42000));
86
87assertEquals(300, sparse_array.indexOf(4));
88assertEquals(700, sparse_array.indexOf(4, 301));
89assertEquals(-1, sparse_array.indexOf(4, 701));
90assertEquals(300, sparse_array.indexOf(4, -42000));
91assertEquals(700, sparse_array.indexOf(4, 301 - 42000));
92assertEquals(-1, sparse_array.indexOf(4, 701 - 42000));
93
94assertEquals(200, sparse_array.indexOf(undefined));
95assertEquals(800, sparse_array.indexOf(undefined, 201));
96assertEquals(-1, sparse_array.indexOf(undefined, 801));
97assertEquals(200, sparse_array.indexOf(undefined, -42000));
98assertEquals(800, sparse_array.indexOf(undefined, 201 - 42000));
99assertEquals(-1, sparse_array.indexOf(undefined, 801 - 42000));
100
101// Find in non-arrays.
102assertEquals(0, Array.prototype.indexOf.call(dense_object, 42));
103assertEquals(1, Array.prototype.indexOf.call(dense_object, 37));
104assertEquals(-1, Array.prototype.indexOf.call(dense_object, 87));
105
106assertEquals(0, Array.prototype.indexOf.call(sparse_object, 42));
107assertEquals(100000, Array.prototype.indexOf.call(sparse_object, 37));
108assertEquals(-1, Array.prototype.indexOf.call(sparse_object, 87));
109
110assertEquals(10, Array.prototype.indexOf.call(funky_object, 42));
111assertEquals(-1, Array.prototype.indexOf.call(funky_object, 42, 15));
112assertEquals(-1, Array.prototype.indexOf.call(funky_object, 37));
113
114assertEquals(-1, Array.prototype.indexOf.call(infinite_object, 42));
115
116// ----------------------------------------------------------------------
117// Array.prototype.lastIndexOf.
118// ----------------------------------------------------------------------
119
120// Negative cases.
121assertEquals(-1, [].lastIndexOf(1));
122assertEquals(-1, array.lastIndexOf(1, -17));
123
124assertEquals(9, array.lastIndexOf(1));
125// Index out of range.
126assertEquals(9, array.lastIndexOf(1, array.length));
127// Index in range.
128assertEquals(0, array.lastIndexOf(1, 2));
129assertEquals(3, array.lastIndexOf(1, 4));
130assertEquals(3, array.lastIndexOf(1, 3));
131// Negative index in range.
132assertEquals(0, array.lastIndexOf(1, -11));
133
134// Find undefined, not holes.
135assertEquals(7, undef_array.lastIndexOf(undefined));
136assertEquals(-1, undef_array.lastIndexOf(undefined, 2));
137assertEquals(3, undef_array.lastIndexOf(undefined, 3));
138assertEquals(3, undef_array.lastIndexOf(undefined, 6));
139assertEquals(7, undef_array.lastIndexOf(undefined, 7));
140assertEquals(7, undef_array.lastIndexOf(undefined, -1));
141assertEquals(-1, undef_array.lastIndexOf(undefined, -9));
142assertEquals(3, undef_array.lastIndexOf(undefined, -8));
143assertEquals(3, undef_array.lastIndexOf(undefined, -5));
144assertEquals(7, undef_array.lastIndexOf(undefined, -4));
145
146// Find in sparse array.
147assertEquals(900, sparse_array.lastIndexOf(3));
148assertEquals(100, sparse_array.lastIndexOf(3, 899));
149assertEquals(-1, sparse_array.lastIndexOf(3, 99));
150assertEquals(900, sparse_array.lastIndexOf(3, -1));
151assertEquals(100, sparse_array.lastIndexOf(3, 899 - 42000));
152assertEquals(-1, sparse_array.lastIndexOf(3, 99 - 42000));
153
154assertEquals(700, sparse_array.lastIndexOf(4));
155assertEquals(300, sparse_array.lastIndexOf(4, 699));
156assertEquals(-1, sparse_array.lastIndexOf(4, 299));
157assertEquals(700, sparse_array.lastIndexOf(4, -1));
158assertEquals(300, sparse_array.lastIndexOf(4, 699 - 42000));
159assertEquals(-1, sparse_array.lastIndexOf(4, 299 - 42000));
160
161assertEquals(800, sparse_array.lastIndexOf(undefined));
162assertEquals(200, sparse_array.lastIndexOf(undefined, 799));
163assertEquals(-1, sparse_array.lastIndexOf(undefined, 199));
164assertEquals(800, sparse_array.lastIndexOf(undefined, -1));
165assertEquals(200, sparse_array.lastIndexOf(undefined, 799 - 42000));
166assertEquals(-1, sparse_array.lastIndexOf(undefined, 199 - 42000));
167
168assertEquals(0, Array.prototype.lastIndexOf.call(dense_object, 42));
169assertEquals(1, Array.prototype.lastIndexOf.call(dense_object, 37));
170assertEquals(0, Array.prototype.lastIndexOf.call(sparse_object, 42));
171assertEquals(100000, Array.prototype.lastIndexOf.call(sparse_object, 37));
172
173//Find in non-arrays.
174assertEquals(0, Array.prototype.lastIndexOf.call(dense_object, 42));
175assertEquals(1, Array.prototype.lastIndexOf.call(dense_object, 37));
176assertEquals(-1, Array.prototype.lastIndexOf.call(dense_object, 87));
177
178assertEquals(0, Array.prototype.lastIndexOf.call(sparse_object, 42));
179assertEquals(100000, Array.prototype.lastIndexOf.call(sparse_object, 37));
180assertEquals(-1, Array.prototype.lastIndexOf.call(sparse_object, 87));
181
182assertEquals(10, Array.prototype.lastIndexOf.call(funky_object, 42, 15));
183assertEquals(10, Array.prototype.lastIndexOf.call(funky_object, 42));
184assertEquals(-1, Array.prototype.lastIndexOf.call(funky_object, 37));
185
186assertEquals(-1, Array.prototype.lastIndexOf.call(infinite_object, 42));
187