Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / v8 / test / mjsunit / delete.js
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 var y = 87; // should have DontDelete attribute
48 assertEquals(87, y);
49 assertFalse(delete y, "don't delete");
50 assertFalse(typeof y === 'undefined');
51 assertEquals(87, y);
52
53 var o = { x: 42, y: 87 };
54 assertTrue(has(o, 'x'));
55 assertTrue(has(o, 'y'));
56 assertTrue(delete o.x);
57 assertFalse(has(o, 'x'));
58 assertTrue(has(o, 'y'));
59 assertTrue(delete o['y']);
60 assertFalse(has(o, 'x'));
61 assertFalse(has(o, 'y'));
62
63
64 var o = {};
65 for (var i = 0x0020; i < 0x02ff; i+=2) {
66   o[String.fromCharCode(i)] = i;
67   o[String.fromCharCode(i+1)] = i+1;
68 }
69 for (var i = 0x0020; i < 0x02ff; i+=2) {
70   assertTrue(delete o[String.fromCharCode(i)]);
71 }
72 for (var i = 0x0020; i < 0x02ff; i+=2) {
73   assertFalse(has(o, String.fromCharCode(i)), "deleted (" + i + ")");
74   assertTrue(has(o, String.fromCharCode(i+1)), "still here (" + i + ")");
75 }
76
77
78 var a = [0,1,2];
79 assertTrue(has(a, 0));
80 assertTrue(delete a[0]);
81 assertFalse(has(a, 0), "delete 0");
82 assertEquals(1, a[1]);
83 assertEquals(2, a[2]);
84 assertTrue(delete a[100], "delete 100");
85 assertTrue(delete a[Math.pow(2,31)-1], "delete 2^31-1");
86 assertFalse(has(a, 0), "delete 0");
87 assertEquals(1, a[1]);
88 assertEquals(2, a[2]);
89
90
91 var a = [0,1,2];
92 assertEquals(3, a.length);
93 assertTrue(delete a[2]);
94 assertEquals(3, a.length);
95 assertTrue(delete a[0]);
96 assertEquals(3, a.length);
97 assertTrue(delete a[1]);
98 assertEquals(3, a.length);
99
100
101 var o = {};
102 o[Math.pow(2,30)-1] = 0;
103 o[Math.pow(2,31)-1] = 0;
104 o[1] = 0;
105 assertTrue(delete o[0]);
106 assertTrue(delete o[Math.pow(2,30)]);
107 assertFalse(has(o, 0), "delete 0");
108 assertFalse(has(o, Math.pow(2,30)));
109 assertTrue(has(o, 1));
110 assertTrue(has(o, Math.pow(2,30)-1));
111 assertTrue(has(o, Math.pow(2,31)-1));
112
113 assertTrue(delete o[Math.pow(2,30)-1]);
114 assertTrue(has(o, 1));
115 assertFalse(has(o, Math.pow(2,30)-1), "delete 2^30-1");
116 assertTrue(has(o, Math.pow(2,31)-1));
117
118 assertTrue(delete o[1]);
119 assertFalse(has(o, 1), "delete 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[Math.pow(2,31)-1]);
124 assertFalse(has(o, 1), "delete 1");
125 assertFalse(has(o, Math.pow(2,30)-1), "delete 2^30-1");
126 assertFalse(has(o, Math.pow(2,31)-1), "delete 2^31-1");
127
128
129 var a = [];
130 a[Math.pow(2,30)-1] = 0;
131 a[Math.pow(2,31)-1] = 0;
132 a[1] = 0;
133 assertTrue(delete a[0]);
134 assertTrue(delete a[Math.pow(2,30)]);
135 assertFalse(has(a, 0), "delete 0");
136 assertFalse(has(a, Math.pow(2,30)), "delete 2^30");
137 assertTrue(has(a, 1));
138 assertTrue(has(a, Math.pow(2,30)-1));
139 assertTrue(has(a, Math.pow(2,31)-1));
140 assertEquals(Math.pow(2,31), a.length);
141
142 assertTrue(delete a[Math.pow(2,30)-1]);
143 assertTrue(has(a, 1));
144 assertFalse(has(a, Math.pow(2,30)-1), "delete 2^30-1");
145 assertTrue(has(a, Math.pow(2,31)-1));
146 assertEquals(Math.pow(2,31), a.length);
147
148 assertTrue(delete a[1]);
149 assertFalse(has(a, 1), "delete 1");
150 assertFalse(has(a, Math.pow(2,30)-1), "delete 2^30-1");
151 assertTrue(has(a, Math.pow(2,31)-1));
152 assertEquals(Math.pow(2,31), a.length);
153
154 assertTrue(delete a[Math.pow(2,31)-1]);
155 assertFalse(has(a, 1), "delete 1");
156 assertFalse(has(a, Math.pow(2,30)-1), "delete 2^30-1");
157 assertFalse(has(a, Math.pow(2,31)-1), "delete 2^31-1");
158 assertEquals(Math.pow(2,31), a.length);
159
160 // Check that a LoadIC for a dictionary field works, even
161 // when the dictionary probe misses.
162 function load_deleted_property_using_IC() {
163   var x = new Object();
164   x.a = 3;
165   x.b = 4;
166   x.c = 5;
167
168   delete x.c;
169   assertEquals(3, load_a(x));
170   assertEquals(3, load_a(x));
171   delete x.a;
172   assertTrue(typeof load_a(x) === 'undefined', "x.a is gone");
173   assertTrue(typeof load_a(x) === 'undefined', "x.a is gone");
174 }
175
176 function load_a(x) {
177   return x.a;
178 }
179
180 load_deleted_property_using_IC();