| OLD | NEW |
| (Empty) | |
| 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 // Verify preservation of the hole when shifting |
| 29 arr = [1, 2, 3, 4, 5]; |
| 30 assertTrue(2 in arr); |
| 31 assertTrue(delete arr[2]); |
| 32 assertFalse(2 in arr); |
| 33 assertArrayEquals([1,2,,4,5], arr); |
| 34 assertEquals(1, arr.shift()); |
| 35 assertArrayEquals([2,,4,5], arr); |
| 36 assertTrue(0 in arr); |
| 37 assertFalse(1 in arr); |
| 38 assertEquals(4, arr.length); |
| 39 assertTrue(delete arr[3]); |
| 40 assertEquals(4, arr.length); |
| 41 assertArrayEquals([2,,4,,], arr); |
| 42 assertEquals(2, arr.shift()); |
| 43 assertEquals(undefined, arr.shift()); |
| 44 assertEquals(4, arr.shift()); |
| 45 assertEquals(undefined, arr.shift()); |
| 46 assertEquals(0, arr.length); |
| 47 |
| 48 // Verify shifting over sparse arrays |
| 49 arr = []; |
| 50 arr[5000] = 5000; |
| 51 assertEquals(5001, arr.length); |
| 52 for (var i = 0; i < 5000; i++) { |
| 53 assertFalse(0 in arr); |
| 54 var element = arr.shift(); |
| 55 assertEquals(undefined, element); |
| 56 assertEquals(5000 - i, arr.length); |
| 57 } |
| 58 assertEquals(5000, arr.shift()); |
| 59 assertEquals(0, arr.length); |
| 60 |
| 61 // A bigger sparse array. Uses splice too. |
| 62 arr = []; |
| 63 for (var i = 0; i < 50000; i+= 500) { |
| 64 arr[i] = 1; |
| 65 } |
| 66 for (var i = 0; i < 50000; i+=500) { |
| 67 assertTrue(0 in arr); |
| 68 assertFalse(1 in arr); |
| 69 assertFalse(499 in arr); |
| 70 assertEquals(1, arr.shift()); |
| 71 arr.splice(0,499); |
| 72 } |
| 73 assertEquals(0, arr.length); |
| 74 |
| 75 // Another sparse array with a couple of filled blocks. |
| 76 arr = []; |
| 77 for (var i = 2000; i < 2100; i++) { |
| 78 arr[i] = i; |
| 79 } |
| 80 for (var i = 10000; i < 10100; i++) { |
| 81 arr[i] = i; |
| 82 } |
| 83 assertEquals(10100, arr.length); |
| 84 |
| 85 // Splice off the first empty chunk 0-1999 |
| 86 assertFalse(0 in arr); |
| 87 assertFalse(1999 in arr); |
| 88 arr2 = arr.splice(0,2000); |
| 89 assertFalse(0 in arr2); |
| 90 assertFalse(1999 in arr2); |
| 91 assertEquals(8100, arr.length); |
| 92 |
| 93 // Verify the first filled chunk 2000-2099 |
| 94 assertTrue(0 in arr); |
| 95 assertTrue(99 in arr); |
| 96 for (var i = 0; i < 100; i++) { |
| 97 assertTrue(0 in arr); |
| 98 assertEquals(2000 + i, arr.shift()); |
| 99 } |
| 100 |
| 101 // Splice off the next chunk 2100-9999 |
| 102 assertFalse(0 in arr); |
| 103 assertFalse(7899 in arr); |
| 104 assertTrue(7900 in arr); |
| 105 arr2 = arr.splice(0,7900); |
| 106 assertEquals(100, arr.length); |
| 107 |
| 108 // Verify the second filled chunk 10000-10099 |
| 109 assertTrue(0 in arr); |
| 110 assertTrue(99 in arr); |
| 111 for (var i = 0; i < 100; i++) { |
| 112 assertTrue(0 in arr); |
| 113 assertEquals(10000 + i, arr.shift()); |
| 114 } |
| OLD | NEW |