assignment.js revision 402d937239b0e2fd11bf2f4fe972ad78aa9fd481
1// Copyright 2010 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// Tests for compound assignments at the top level
29
30z = 2;
31z += 4;
32
33assertEquals(z, 6);
34
35a = new Array(10);
36
37a[2] += 7;
38a[2] = 15;
39a[2] += 2;
40
41assertEquals(17, a[2]);
42
43b = new Object();
44b.foo = 5;
45b.foo += 12;
46
47assertEquals(17, b.foo);
48
49// Test compound assignments in an anonymous function with local variables.
50(function () {
51  var z = 2;
52  z += 4;
53
54  assertEquals(z, 6);
55
56  var a = new Array(10);
57
58  a[2] += 7;
59  a[2] = 15;
60  a[2] += 2;
61
62  assertEquals(17, a[2]);
63
64  var b = new Object();
65  b.foo = 5;
66  b.foo += 12;
67
68  assertEquals(17, b.foo);
69})();
70
71// Test compound assignments in an anonymous function with global variables.
72(function () {
73  z = 2;
74  z += 4;
75
76  assertEquals(z, 6);
77
78  a = new Array(10);
79
80  a[2] += 7;
81  a[2] = 15;
82  a[2] += 2;
83
84  assertEquals(17, a[2]);
85
86  b = new Object();
87  b.foo = 5;
88  b.foo += 12;
89
90  assertEquals(17, b.foo);
91})();
92
93// Test compound assignments in a named function with local variables.
94function foo() {
95  var z = 3;
96  z += 4;
97
98  assertEquals(z, 7);
99
100  var a = new Array(10);
101
102  a[2] += 7;
103  a[2] = 15;
104  a[2] += 2;
105
106  assertEquals(17, a[2]);
107
108  var b = new Object();
109  b.foo = 5;
110  b.foo += 12;
111
112  assertEquals(17, b.foo);
113}
114
115foo();
116
117// Test compound assignments in a named function with global variables.
118function bar() {
119  z = 2;
120  z += 5;
121
122  assertEquals(z, 7);
123
124  a = new Array(10);
125
126  a[2] += 7;
127  a[2] = 15;
128  a[2] += 2;
129
130  assertEquals(17, a[2]);
131
132  b = new Object();
133  b.foo = 5;
134  b.foo += 12;
135
136  assertEquals(17, b.foo);
137}
138
139bar();
140
141// Entire series of tests repeated, in loops.
142// -------------------------------------------
143// Tests for compound assignments in a loop at the top level
144
145for (i = 0; i < 5; ++i) {
146  z = 2;
147  z += 4;
148
149  assertEquals(z, 6);
150
151  a = new Array(10);
152
153  a[2] += 7;
154  a[2] = 15;
155  a[2] += 2;
156
157  assertEquals(17, a[2]);
158
159  b = new Object();
160  b.foo = 5;
161  b.foo += 12;
162
163  assertEquals(17, b.foo);
164}
165
166// Test compound assignments in an anonymous function with local variables.
167(function () {
168  for (var i = 0; i < 5; ++i) {
169    var z = 2;
170    z += 4;
171
172    assertEquals(z, 6);
173
174    var a = new Array(10);
175
176    a[2] += 7;
177    a[2] = 15;
178    a[2] += 2;
179
180    assertEquals(17, a[2]);
181
182    var b = new Object();
183    b.foo = 5;
184    b.foo += 12;
185
186    assertEquals(17, b.foo);
187  }
188})();
189
190// Test compound assignments in an anonymous function with global variables.
191(function () {
192  for (i = 0; i < 5; ++i) {
193    z = 2;
194    z += 4;
195
196    assertEquals(z, 6);
197
198    a = new Array(10);
199
200    a[2] += 7;
201    a[2] = 15;
202    a[2] += 2;
203
204    assertEquals(17, a[2]);
205
206    b = new Object();
207    b.foo = 5;
208    b.foo += 12;
209
210    assertEquals(17, b.foo);
211  }
212})();
213
214// Test compound assignments in a named function with local variables.
215function foo_loop() {
216  for (i = 0; i < 5; ++i) {
217    var z = 3;
218    z += 4;
219
220    assertEquals(z, 7);
221
222    var a = new Array(10);
223
224    a[2] += 7;
225    a[2] = 15;
226    a[2] += 2;
227
228    assertEquals(17, a[2]);
229
230    var b = new Object();
231    b.foo = 5;
232    b.foo += 12;
233
234    assertEquals(17, b.foo);
235  }
236}
237
238foo_loop();
239
240// Test compound assignments in a named function with global variables.
241function bar_loop() {
242  for (i = 0; i < 5; ++i) {
243    z = 2;
244    z += 5;
245
246    assertEquals(z, 7);
247
248    a = new Array(10);
249
250    a[2] += 7;
251    a[2] = 15;
252    a[2] += 2;
253
254    assertEquals(17, a[2]);
255
256    b = new Object();
257    b.foo = 5;
258    b.foo += 12;
259
260    assertEquals(17, b.foo);
261  }
262}
263
264bar_loop();
265