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 // We use the has() function to avoid relying on a functioning 29 // implementation of 'in'. 30 function has(o, k) { return typeof o[k] !== 'undefined'; } 31 32 assertTrue(delete null); 33 assertTrue(delete 2); 34 assertTrue(delete 'foo'); 35 assertTrue(delete Number(7)); 36 assertTrue(delete new Number(8)); 37 38 assertTrue(delete {}.x); 39 assertTrue(delete {}.y); 40 assertTrue(delete {}.toString); 41 42 x = 42; 43 assertEquals(42, x); 44 assertTrue(delete x); 45 assertTrue(typeof x === 'undefined', "x is gone"); 46 47 /**** 48 * This test relies on DontDelete attributes. This is not 49 * working yet. 50 51 var y = 87; // should have DontDelete attribute 52 assertEquals(87, y); 53 assertFalse(delete y, "don't delete"); 54 assertFalse(typeof y === 'undefined'); 55 assertEquals(87, y); 56 */ 57 58 var o = { x: 42, y: 87 }; 59 assertTrue(has(o, 'x')); 60 assertTrue(has(o, 'y')); 61 assertTrue(delete o.x); 62 assertFalse(has(o, 'x')); 63 assertTrue(has(o, 'y')); 64 assertTrue(delete o['y']); 65 assertFalse(has(o, 'x')); 66 assertFalse(has(o, 'y')); 67 68 69 var o = {}; 70 for (var i = 0x0020; i < 0x02ff; i+=2) { 71 o[String.fromCharCode(i)] = i; 72 o[String.fromCharCode(i+1)] = i+1; 73 } 74 for (var i = 0x0020; i < 0x02ff; i+=2) { 75 assertTrue(delete o[String.fromCharCode(i)]); 76 } 77 for (var i = 0x0020; i < 0x02ff; i+=2) { 78 assertFalse(has(o, String.fromCharCode(i)), "deleted (" + i + ")"); 79 assertTrue(has(o, String.fromCharCode(i+1)), "still here (" + i + ")"); 80 } 81 82 83 var a = [0,1,2]; 84 assertTrue(has(a, 0)); 85 assertTrue(delete a[0]); 86 assertFalse(has(a, 0), "delete 0"); 87 assertEquals(1, a[1]); 88 assertEquals(2, a[2]); 89 assertTrue(delete a[100], "delete 100"); 90 assertTrue(delete a[Math.pow(2,31)-1], "delete 2^31-1"); 91 assertFalse(has(a, 0), "delete 0"); 92 assertEquals(1, a[1]); 93 assertEquals(2, a[2]); 94 95 96 var a = [0,1,2]; 97 assertEquals(3, a.length); 98 assertTrue(delete a[2]); 99 assertEquals(3, a.length); 100 assertTrue(delete a[0]); 101 assertEquals(3, a.length); 102 assertTrue(delete a[1]); 103 assertEquals(3, a.length); 104 105 106 var o = {}; 107 o[Math.pow(2,30)-1] = 0; 108 o[Math.pow(2,31)-1] = 0; 109 o[1] = 0; 110 assertTrue(delete o[0]); 111 assertTrue(delete o[Math.pow(2,30)]); 112 assertFalse(has(o, 0), "delete 0"); 113 assertFalse(has(o, Math.pow(2,30))); 114 assertTrue(has(o, 1)); 115 assertTrue(has(o, Math.pow(2,30)-1)); 116 assertTrue(has(o, Math.pow(2,31)-1)); 117 118 assertTrue(delete o[Math.pow(2,30)-1]); 119 assertTrue(has(o, 1)); 120 assertFalse(has(o, Math.pow(2,30)-1), "delete 2^30-1"); 121 assertTrue(has(o, Math.pow(2,31)-1)); 122 123 assertTrue(delete o[1]); 124 assertFalse(has(o, 1), "delete 1"); 125 assertFalse(has(o, Math.pow(2,30)-1), "delete 2^30-1"); 126 assertTrue(has(o, Math.pow(2,31)-1)); 127 128 assertTrue(delete o[Math.pow(2,31)-1]); 129 assertFalse(has(o, 1), "delete 1"); 130 assertFalse(has(o, Math.pow(2,30)-1), "delete 2^30-1"); 131 assertFalse(has(o, Math.pow(2,31)-1), "delete 2^31-1"); 132 133 134 var a = []; 135 a[Math.pow(2,30)-1] = 0; 136 a[Math.pow(2,31)-1] = 0; 137 a[1] = 0; 138 assertTrue(delete a[0]); 139 assertTrue(delete a[Math.pow(2,30)]); 140 assertFalse(has(a, 0), "delete 0"); 141 assertFalse(has(a, Math.pow(2,30)), "delete 2^30"); 142 assertTrue(has(a, 1)); 143 assertTrue(has(a, Math.pow(2,30)-1)); 144 assertTrue(has(a, Math.pow(2,31)-1)); 145 assertEquals(Math.pow(2,31), a.length); 146 147 assertTrue(delete a[Math.pow(2,30)-1]); 148 assertTrue(has(a, 1)); 149 assertFalse(has(a, Math.pow(2,30)-1), "delete 2^30-1"); 150 assertTrue(has(a, Math.pow(2,31)-1)); 151 assertEquals(Math.pow(2,31), a.length); 152 153 assertTrue(delete a[1]); 154 assertFalse(has(a, 1), "delete 1"); 155 assertFalse(has(a, Math.pow(2,30)-1), "delete 2^30-1"); 156 assertTrue(has(a, Math.pow(2,31)-1)); 157 assertEquals(Math.pow(2,31), a.length); 158 159 assertTrue(delete a[Math.pow(2,31)-1]); 160 assertFalse(has(a, 1), "delete 1"); 161 assertFalse(has(a, Math.pow(2,30)-1), "delete 2^30-1"); 162 assertFalse(has(a, Math.pow(2,31)-1), "delete 2^31-1"); 163 assertEquals(Math.pow(2,31), a.length); 164