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