OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 var array = [1,2,3,1,2,3,1,2,3,1,2,3]; | 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 lenght 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"; |
29 | 42 |
30 // ---------------------------------------------------------------------- | 43 // ---------------------------------------------------------------------- |
31 // Array.prototype.indexOf. | 44 // Array.prototype.indexOf. |
32 // ---------------------------------------------------------------------- | 45 // ---------------------------------------------------------------------- |
33 | 46 |
34 // Negative cases. | 47 // Negative cases. |
35 assertEquals([].indexOf(1), -1); | 48 assertEquals(-1, [].indexOf(1)); |
36 assertEquals(array.indexOf(4), -1); | 49 assertEquals(-1, array.indexOf(4)); |
37 assertEquals(array.indexOf(3, array.length), -1); | 50 assertEquals(-1, array.indexOf(3, array.length)); |
38 | 51 |
39 assertEquals(array.indexOf(3), 2); | 52 assertEquals(2, array.indexOf(3)); |
40 // Negative index out of range. | 53 // Negative index out of range. |
41 assertEquals(array.indexOf(1, -17), 0); | 54 assertEquals(0, array.indexOf(1, -17)); |
42 // Negative index in rage. | 55 // Negative index in rage. |
43 assertEquals(array.indexOf(1, -11), 3); | 56 assertEquals(3, array.indexOf(1, -11)); |
44 // Index in range. | 57 // Index in range. |
45 assertEquals(array.indexOf(1, 1), 3); | 58 assertEquals(3, array.indexOf(1, 1)); |
46 assertEquals(array.indexOf(1, 3), 3); | 59 assertEquals(3, array.indexOf(1, 3)); |
47 assertEquals(array.indexOf(1, 4), 6); | 60 assertEquals(6, array.indexOf(1, 4)); |
| 61 |
| 62 // Find undefined, not holes. |
| 63 assertEquals(3, undef_array.indexOf(undefined)); |
| 64 assertEquals(3, undef_array.indexOf(undefined, 3)); |
| 65 assertEquals(7, undef_array.indexOf(undefined, 4)); |
| 66 assertEquals(7, undef_array.indexOf(undefined, 7)); |
| 67 assertEquals(-1, undef_array.indexOf(undefined, 8)); |
| 68 assertEquals(3, undef_array.indexOf(undefined, -11)); |
| 69 assertEquals(3, undef_array.indexOf(undefined, -8)); |
| 70 assertEquals(7, undef_array.indexOf(undefined, -7)); |
| 71 assertEquals(7, undef_array.indexOf(undefined, -4)); |
| 72 assertEquals(-1, undef_array.indexOf(undefined, -3)); |
| 73 |
| 74 // Find in sparse array. |
| 75 assertEquals(100, sparse_array.indexOf(3)); |
| 76 assertEquals(900, sparse_array.indexOf(3, 101)); |
| 77 assertEquals(-1, sparse_array.indexOf(3, 901)); |
| 78 assertEquals(100, sparse_array.indexOf(3, -42000)); |
| 79 assertEquals(900, sparse_array.indexOf(3, 101 - 42000)); |
| 80 assertEquals(-1, sparse_array.indexOf(3, 901 - 42000)); |
| 81 |
| 82 assertEquals(300, sparse_array.indexOf(4)); |
| 83 assertEquals(700, sparse_array.indexOf(4, 301)); |
| 84 assertEquals(-1, sparse_array.indexOf(4, 701)); |
| 85 assertEquals(300, sparse_array.indexOf(4, -42000)); |
| 86 assertEquals(700, sparse_array.indexOf(4, 301 - 42000)); |
| 87 assertEquals(-1, sparse_array.indexOf(4, 701 - 42000)); |
| 88 |
| 89 assertEquals(200, sparse_array.indexOf(undefined)); |
| 90 assertEquals(800, sparse_array.indexOf(undefined, 201)); |
| 91 assertEquals(-1, sparse_array.indexOf(undefined, 801)); |
| 92 assertEquals(200, sparse_array.indexOf(undefined, -42000)); |
| 93 assertEquals(800, sparse_array.indexOf(undefined, 201 - 42000)); |
| 94 assertEquals(-1, sparse_array.indexOf(undefined, 801 - 42000)); |
| 95 |
48 | 96 |
49 // ---------------------------------------------------------------------- | 97 // ---------------------------------------------------------------------- |
50 // Array.prototype.lastIndexOf. | 98 // Array.prototype.lastIndexOf. |
51 // ---------------------------------------------------------------------- | 99 // ---------------------------------------------------------------------- |
52 | 100 |
53 // Negative cases. | 101 // Negative cases. |
54 assertEquals([].lastIndexOf(1), -1); | 102 assertEquals(-1, [].lastIndexOf(1)); |
55 assertEquals(array.lastIndexOf(1, -17), -1); | 103 assertEquals(-1, array.lastIndexOf(1, -17)); |
56 | 104 |
57 assertEquals(array.lastIndexOf(1), 9); | 105 assertEquals(9, array.lastIndexOf(1)); |
58 // Index out of range. | 106 // Index out of range. |
59 assertEquals(array.lastIndexOf(1, array.length), 9); | 107 assertEquals(9, array.lastIndexOf(1, array.length)); |
60 // Index in range. | 108 // Index in range. |
61 assertEquals(array.lastIndexOf(1, 2), 0); | 109 assertEquals(0, array.lastIndexOf(1, 2)); |
62 assertEquals(array.lastIndexOf(1, 4), 3); | 110 assertEquals(3, array.lastIndexOf(1, 4)); |
63 assertEquals(array.lastIndexOf(1, 3), 3); | 111 assertEquals(3, array.lastIndexOf(1, 3)); |
64 // Negative index in range. | 112 // Negative index in range. |
65 assertEquals(array.lastIndexOf(1, -11), 0); | 113 assertEquals(0, array.lastIndexOf(1, -11)); |
66 | 114 |
| 115 // Find undefined, not holes. |
| 116 assertEquals(7, undef_array.lastIndexOf(undefined)); |
| 117 assertEquals(-1, undef_array.lastIndexOf(undefined, 2)); |
| 118 assertEquals(3, undef_array.lastIndexOf(undefined, 3)); |
| 119 assertEquals(3, undef_array.lastIndexOf(undefined, 6)); |
| 120 assertEquals(7, undef_array.lastIndexOf(undefined, 7)); |
| 121 assertEquals(7, undef_array.lastIndexOf(undefined, -1)); |
| 122 assertEquals(-1, undef_array.lastIndexOf(undefined, -9)); |
| 123 assertEquals(3, undef_array.lastIndexOf(undefined, -8)); |
| 124 assertEquals(3, undef_array.lastIndexOf(undefined, -5)); |
| 125 assertEquals(7, undef_array.lastIndexOf(undefined, -4)); |
| 126 |
| 127 // Find in sparse array. |
| 128 assertEquals(900, sparse_array.lastIndexOf(3)); |
| 129 assertEquals(100, sparse_array.lastIndexOf(3, 899)); |
| 130 assertEquals(-1, sparse_array.lastIndexOf(3, 99)); |
| 131 assertEquals(900, sparse_array.lastIndexOf(3, -1)); |
| 132 assertEquals(100, sparse_array.lastIndexOf(3, 899 - 42000)); |
| 133 assertEquals(-1, sparse_array.lastIndexOf(3, 99 - 42000)); |
| 134 |
| 135 assertEquals(700, sparse_array.lastIndexOf(4)); |
| 136 assertEquals(300, sparse_array.lastIndexOf(4, 699)); |
| 137 assertEquals(-1, sparse_array.lastIndexOf(4, 299)); |
| 138 assertEquals(700, sparse_array.lastIndexOf(4, -1)); |
| 139 assertEquals(300, sparse_array.lastIndexOf(4, 699 - 42000)); |
| 140 assertEquals(-1, sparse_array.lastIndexOf(4, 299 - 42000)); |
| 141 |
| 142 assertEquals(800, sparse_array.lastIndexOf(undefined)); |
| 143 assertEquals(200, sparse_array.lastIndexOf(undefined, 799)); |
| 144 assertEquals(-1, sparse_array.lastIndexOf(undefined, 199)); |
| 145 assertEquals(800, sparse_array.lastIndexOf(undefined, -1)); |
| 146 assertEquals(200, sparse_array.lastIndexOf(undefined, 799 - 42000)); |
| 147 assertEquals(-1, sparse_array.lastIndexOf(undefined, 199 - 42000)); |
OLD | NEW |